2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2015 Ecole Normale Superieure. All rights reserved.
4 * Copyright 2015-2017 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>
58 #include "clang_compatibility.h"
62 #include "expr_plus.h"
65 #include "inlined_calls.h"
66 #include "killed_locals.h"
71 #include "scop_plus.h"
72 #include "substituter.h"
74 #include "tree2scop.h"
77 using namespace clang
;
79 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
89 return pet_op_post_inc
;
91 return pet_op_post_dec
;
93 return pet_op_pre_inc
;
95 return pet_op_pre_dec
;
101 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
105 return pet_op_add_assign
;
107 return pet_op_sub_assign
;
109 return pet_op_mul_assign
;
111 return pet_op_div_assign
;
113 return pet_op_and_assign
;
115 return pet_op_xor_assign
;
117 return pet_op_or_assign
;
119 return pet_op_assign
;
161 #ifdef GETTYPEINFORETURNSTYPEINFO
163 static int size_in_bytes(ASTContext
&context
, QualType type
)
165 return context
.getTypeInfo(type
).Width
/ 8;
170 static int size_in_bytes(ASTContext
&context
, QualType type
)
172 return context
.getTypeInfo(type
).first
/ 8;
177 /* Check if the element type corresponding to the given array type
178 * has a const qualifier.
180 static bool const_base(QualType qt
)
182 const Type
*type
= qt
.getTypePtr();
184 if (type
->isPointerType())
185 return const_base(type
->getPointeeType());
186 if (type
->isArrayType()) {
187 const ArrayType
*atype
;
188 type
= type
->getCanonicalTypeInternal().getTypePtr();
189 atype
= cast
<ArrayType
>(type
);
190 return const_base(atype
->getElementType());
193 return qt
.isConstQualified();
198 std::map
<const Type
*, pet_expr
*>::iterator it
;
199 std::map
<FunctionDecl
*, pet_function_summary
*>::iterator it_s
;
201 for (it
= type_size
.begin(); it
!= type_size
.end(); ++it
)
202 pet_expr_free(it
->second
);
203 for (it_s
= summary_cache
.begin(); it_s
!= summary_cache
.end(); ++it_s
)
204 pet_function_summary_free(it_s
->second
);
206 isl_id_to_pet_expr_free(id_size
);
207 isl_union_map_free(value_bounds
);
210 /* Report a diagnostic on the range "range", unless autodetect is set.
212 void PetScan::report(SourceRange range
, unsigned id
)
214 if (options
->autodetect
)
217 SourceLocation loc
= range
.getBegin();
218 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
219 DiagnosticBuilder B
= diag
.Report(loc
, id
) << range
;
222 /* Report a diagnostic on "stmt", unless autodetect is set.
224 void PetScan::report(Stmt
*stmt
, unsigned id
)
226 report(stmt
->getSourceRange(), id
);
229 /* Report a diagnostic on "decl", unless autodetect is set.
231 void PetScan::report(Decl
*decl
, unsigned id
)
233 report(decl
->getSourceRange(), id
);
236 /* Called if we found something we (currently) cannot handle.
237 * We'll provide more informative warnings later.
239 * We only actually complain if autodetect is false.
241 void PetScan::unsupported(Stmt
*stmt
)
243 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
244 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
249 /* Report an unsupported unary operator, unless autodetect is set.
251 void PetScan::report_unsupported_unary_operator(Stmt
*stmt
)
253 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
254 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
255 "this type of unary operator is not supported");
259 /* Report an unsupported binary operator, unless autodetect is set.
261 void PetScan::report_unsupported_binary_operator(Stmt
*stmt
)
263 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
264 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
265 "this type of binary operator is not supported");
269 /* Report an unsupported statement type, unless autodetect is set.
271 void PetScan::report_unsupported_statement_type(Stmt
*stmt
)
273 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
274 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
275 "this type of statement is not supported");
279 /* Report a missing prototype, unless autodetect is set.
281 void PetScan::report_prototype_required(Stmt
*stmt
)
283 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
284 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
285 "prototype required");
289 /* Report a missing increment, unless autodetect is set.
291 void PetScan::report_missing_increment(Stmt
*stmt
)
293 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
294 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
295 "missing increment");
299 /* Report a missing summary function, unless autodetect is set.
301 void PetScan::report_missing_summary_function(Stmt
*stmt
)
303 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
304 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
305 "missing summary function");
309 /* Report a missing summary function body, unless autodetect is set.
311 void PetScan::report_missing_summary_function_body(Stmt
*stmt
)
313 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
314 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
315 "missing summary function body");
319 /* Report an unsupported argument in a call to an inlined function,
320 * unless autodetect is set.
322 void PetScan::report_unsupported_inline_function_argument(Stmt
*stmt
)
324 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
325 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
326 "unsupported inline function call argument");
330 /* Report an unsupported type of declaration, unless autodetect is set.
332 void PetScan::report_unsupported_declaration(Decl
*decl
)
334 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
335 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
336 "unsupported declaration");
340 /* Report an unbalanced pair of scop/endscop pragmas, unless autodetect is set.
342 void PetScan::report_unbalanced_pragmas(SourceLocation scop
,
343 SourceLocation endscop
)
345 if (options
->autodetect
)
348 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
350 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
351 "unbalanced endscop pragma");
352 DiagnosticBuilder B2
= diag
.Report(endscop
, id
);
355 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Note
,
356 "corresponding scop pragma");
357 DiagnosticBuilder B
= diag
.Report(scop
, id
);
361 /* Report a return statement in an unsupported context,
362 * unless autodetect is set.
364 void PetScan::report_unsupported_return(Stmt
*stmt
)
366 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
367 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
368 "return statements not supported in this context");
372 /* Report a return statement that does not appear at the end of a function,
373 * unless autodetect is set.
375 void PetScan::report_return_not_at_end_of_function(Stmt
*stmt
)
377 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
378 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
379 "return statement must be final statement in function");
383 /* Extract an integer from "val", which is assumed to be non-negative.
385 static __isl_give isl_val
*extract_unsigned(isl_ctx
*ctx
,
386 const llvm::APInt
&val
)
389 const uint64_t *data
;
391 data
= val
.getRawData();
392 n
= val
.getNumWords();
393 return isl_val_int_from_chunks(ctx
, n
, sizeof(uint64_t), data
);
396 /* Extract an integer from "val". If "is_signed" is set, then "val"
397 * is signed. Otherwise it it unsigned.
399 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
, bool is_signed
,
402 int is_negative
= is_signed
&& val
.isNegative();
408 v
= extract_unsigned(ctx
, val
);
415 /* Extract an integer from "expr".
417 __isl_give isl_val
*PetScan::extract_int(isl_ctx
*ctx
, IntegerLiteral
*expr
)
419 const Type
*type
= expr
->getType().getTypePtr();
420 bool is_signed
= type
->hasSignedIntegerRepresentation();
422 return ::extract_int(ctx
, is_signed
, expr
->getValue());
425 /* Extract an integer from "expr".
426 * Return NULL if "expr" does not (obviously) represent an integer.
428 __isl_give isl_val
*PetScan::extract_int(clang::ParenExpr
*expr
)
430 return extract_int(expr
->getSubExpr());
433 /* Extract an integer from "expr".
434 * Return NULL if "expr" does not (obviously) represent an integer.
436 __isl_give isl_val
*PetScan::extract_int(clang::Expr
*expr
)
438 if (expr
->getStmtClass() == Stmt::IntegerLiteralClass
)
439 return extract_int(ctx
, cast
<IntegerLiteral
>(expr
));
440 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
441 return extract_int(cast
<ParenExpr
>(expr
));
447 /* Extract a pet_expr from the APInt "val", which is assumed
448 * to be non-negative.
450 __isl_give pet_expr
*PetScan::extract_expr(const llvm::APInt
&val
)
452 return pet_expr_new_int(extract_unsigned(ctx
, val
));
455 /* Return the number of bits needed to represent the type of "decl",
456 * if it is an integer type. Otherwise return 0.
457 * If qt is signed then return the opposite of the number of bits.
459 static int get_type_size(ValueDecl
*decl
)
461 return pet_clang_get_type_size(decl
->getType(), decl
->getASTContext());
464 /* Bound parameter "pos" of "set" to the possible values of "decl".
466 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
467 unsigned pos
, ValueDecl
*decl
)
473 ctx
= isl_set_get_ctx(set
);
474 type_size
= get_type_size(decl
);
476 isl_die(ctx
, isl_error_invalid
, "not an integer type",
477 return isl_set_free(set
));
479 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, 0);
480 bound
= isl_val_int_from_ui(ctx
, type_size
);
481 bound
= isl_val_2exp(bound
);
482 bound
= isl_val_sub_ui(bound
, 1);
483 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
, bound
);
485 bound
= isl_val_int_from_ui(ctx
, -type_size
- 1);
486 bound
= isl_val_2exp(bound
);
487 bound
= isl_val_sub_ui(bound
, 1);
488 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
,
489 isl_val_copy(bound
));
490 bound
= isl_val_neg(bound
);
491 bound
= isl_val_sub_ui(bound
, 1);
492 set
= isl_set_lower_bound_val(set
, isl_dim_param
, pos
, bound
);
498 __isl_give pet_expr
*PetScan::extract_index_expr(ImplicitCastExpr
*expr
)
500 return extract_index_expr(expr
->getSubExpr());
503 /* Construct a pet_expr representing an index expression for an access
504 * to the variable referenced by "expr".
506 * If "expr" references an enum constant, then return an integer expression
507 * instead, representing the value of the enum constant.
509 __isl_give pet_expr
*PetScan::extract_index_expr(DeclRefExpr
*expr
)
511 return extract_index_expr(expr
->getDecl());
514 /* Construct a pet_expr representing an index expression for an access
515 * to the variable "decl".
517 * If "decl" is an enum constant, then we return an integer expression
518 * instead, representing the value of the enum constant.
520 __isl_give pet_expr
*PetScan::extract_index_expr(ValueDecl
*decl
)
524 if (isa
<EnumConstantDecl
>(decl
))
525 return extract_expr(cast
<EnumConstantDecl
>(decl
));
527 id
= pet_id_from_decl(ctx
, decl
);
528 return pet_id_create_index_expr(id
);
531 /* Construct a pet_expr representing the index expression "expr"
532 * Return NULL on error.
534 * If "expr" is a reference to an enum constant, then return
535 * an integer expression instead, representing the value of the enum constant.
537 __isl_give pet_expr
*PetScan::extract_index_expr(Expr
*expr
)
539 switch (expr
->getStmtClass()) {
540 case Stmt::ImplicitCastExprClass
:
541 return extract_index_expr(cast
<ImplicitCastExpr
>(expr
));
542 case Stmt::DeclRefExprClass
:
543 return extract_index_expr(cast
<DeclRefExpr
>(expr
));
544 case Stmt::ArraySubscriptExprClass
:
545 return extract_index_expr(cast
<ArraySubscriptExpr
>(expr
));
546 case Stmt::IntegerLiteralClass
:
547 return extract_expr(cast
<IntegerLiteral
>(expr
));
548 case Stmt::MemberExprClass
:
549 return extract_index_expr(cast
<MemberExpr
>(expr
));
556 /* Extract an index expression from the given array subscript expression.
558 * We first extract an index expression from the base.
559 * This will result in an index expression with a range that corresponds
560 * to the earlier indices.
561 * We then extract the current index and let
562 * pet_expr_access_subscript combine the two.
564 __isl_give pet_expr
*PetScan::extract_index_expr(ArraySubscriptExpr
*expr
)
566 Expr
*base
= expr
->getBase();
567 Expr
*idx
= expr
->getIdx();
571 base_expr
= extract_index_expr(base
);
572 index
= extract_expr(idx
);
574 base_expr
= pet_expr_access_subscript(base_expr
, index
);
579 /* Extract an index expression from a member expression.
581 * If the base access (to the structure containing the member)
586 * and the member is called "f", then the member access is of
591 * If the member access is to an anonymous struct, then simply return
595 * If the member access in the source code is of the form
599 * then it is treated as
603 __isl_give pet_expr
*PetScan::extract_index_expr(MemberExpr
*expr
)
605 Expr
*base
= expr
->getBase();
606 FieldDecl
*field
= cast
<FieldDecl
>(expr
->getMemberDecl());
607 pet_expr
*base_index
;
610 base_index
= extract_index_expr(base
);
612 if (expr
->isArrow()) {
613 pet_expr
*index
= pet_expr_new_int(isl_val_zero(ctx
));
614 base_index
= pet_expr_access_subscript(base_index
, index
);
617 if (field
->isAnonymousStructOrUnion())
620 id
= pet_id_from_decl(ctx
, field
);
622 return pet_expr_access_member(base_index
, id
);
625 /* Mark the given access pet_expr as a write.
627 static __isl_give pet_expr
*mark_write(__isl_take pet_expr
*access
)
629 access
= pet_expr_access_set_write(access
, 1);
630 access
= pet_expr_access_set_read(access
, 0);
635 /* Mark the given (read) access pet_expr as also possibly being written.
636 * That is, initialize the may write access relation from the may read relation
637 * and initialize the must write access relation to the empty relation.
639 static __isl_give pet_expr
*mark_may_write(__isl_take pet_expr
*expr
)
641 isl_union_map
*access
;
642 isl_union_map
*empty
;
644 access
= pet_expr_access_get_dependent_access(expr
,
645 pet_expr_access_may_read
);
646 empty
= isl_union_map_empty(isl_union_map_get_space(access
));
647 expr
= pet_expr_access_set_access(expr
, pet_expr_access_may_write
,
649 expr
= pet_expr_access_set_access(expr
, pet_expr_access_must_write
,
655 /* Construct a pet_expr representing a unary operator expression.
657 __isl_give pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
663 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
664 if (op
== pet_op_last
) {
665 report_unsupported_unary_operator(expr
);
669 arg
= extract_expr(expr
->getSubExpr());
671 if (expr
->isIncrementDecrementOp() &&
672 pet_expr_get_type(arg
) == pet_expr_access
) {
673 arg
= mark_write(arg
);
674 arg
= pet_expr_access_set_read(arg
, 1);
677 type_size
= pet_clang_get_type_size(expr
->getType(), ast_context
);
678 return pet_expr_new_unary(type_size
, op
, arg
);
681 /* Construct a pet_expr representing a binary operator expression.
683 * If the top level operator is an assignment and the LHS is an access,
684 * then we mark that access as a write. If the operator is a compound
685 * assignment, the access is marked as both a read and a write.
687 __isl_give pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
693 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
694 if (op
== pet_op_last
) {
695 report_unsupported_binary_operator(expr
);
699 lhs
= extract_expr(expr
->getLHS());
700 rhs
= extract_expr(expr
->getRHS());
702 if (expr
->isAssignmentOp() &&
703 pet_expr_get_type(lhs
) == pet_expr_access
) {
704 lhs
= mark_write(lhs
);
705 if (expr
->isCompoundAssignmentOp())
706 lhs
= pet_expr_access_set_read(lhs
, 1);
709 type_size
= pet_clang_get_type_size(expr
->getType(), ast_context
);
710 return pet_expr_new_binary(type_size
, op
, lhs
, rhs
);
713 /* Construct a pet_tree for a variable declaration and
714 * add the declaration to the list of declarations
715 * inside the current compound statement.
717 __isl_give pet_tree
*PetScan::extract(Decl
*decl
)
723 if (!isa
<VarDecl
>(decl
)) {
724 report_unsupported_declaration(decl
);
728 vd
= cast
<VarDecl
>(decl
);
729 declarations
.push_back(vd
);
731 lhs
= extract_access_expr(vd
);
732 lhs
= mark_write(lhs
);
734 tree
= pet_tree_new_decl(lhs
);
736 rhs
= extract_expr(vd
->getInit());
737 tree
= pet_tree_new_decl_init(lhs
, rhs
);
743 /* Construct a pet_tree for a variable declaration statement.
744 * If the declaration statement declares multiple variables,
745 * then return a group of pet_trees, one for each declared variable.
747 __isl_give pet_tree
*PetScan::extract(DeclStmt
*stmt
)
752 if (!stmt
->isSingleDecl()) {
753 const DeclGroup
&group
= stmt
->getDeclGroup().getDeclGroup();
755 tree
= pet_tree_new_block(ctx
, 0, n
);
757 for (unsigned i
= 0; i
< n
; ++i
) {
761 tree_i
= extract(group
[i
]);
762 loc
= construct_pet_loc(group
[i
]->getSourceRange(),
764 tree_i
= pet_tree_set_loc(tree_i
, loc
);
765 tree
= pet_tree_block_add_child(tree
, tree_i
);
771 return extract(stmt
->getSingleDecl());
774 /* Construct a pet_expr representing a conditional operation.
776 __isl_give pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
778 pet_expr
*cond
, *lhs
, *rhs
;
780 cond
= extract_expr(expr
->getCond());
781 lhs
= extract_expr(expr
->getTrueExpr());
782 rhs
= extract_expr(expr
->getFalseExpr());
784 return pet_expr_new_ternary(cond
, lhs
, rhs
);
787 __isl_give pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
789 return extract_expr(expr
->getSubExpr());
792 /* Construct a pet_expr representing a floating point value.
794 * If the floating point literal does not appear in a macro,
795 * then we use the original representation in the source code
796 * as the string representation. Otherwise, we use the pretty
797 * printer to produce a string representation.
799 __isl_give pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
803 const LangOptions
&LO
= PP
.getLangOpts();
804 SourceLocation loc
= expr
->getLocation();
806 if (!loc
.isMacroID()) {
807 SourceManager
&SM
= PP
.getSourceManager();
808 unsigned len
= Lexer::MeasureTokenLength(loc
, SM
, LO
);
809 s
= string(SM
.getCharacterData(loc
), len
);
811 llvm::raw_string_ostream
S(s
);
812 expr
->printPretty(S
, 0, PrintingPolicy(LO
));
815 d
= expr
->getValueAsApproximateDouble();
816 return pet_expr_new_double(ctx
, d
, s
.c_str());
819 /* Extract an index expression from "expr" and then convert it into
820 * an access pet_expr.
822 * If "expr" is a reference to an enum constant, then return
823 * an integer expression instead, representing the value of the enum constant.
825 __isl_give pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
829 index
= extract_index_expr(expr
);
831 if (pet_expr_get_type(index
) == pet_expr_int
)
834 return pet_expr_access_from_index(expr
->getType(), index
, ast_context
);
837 /* Extract an index expression from "decl" and then convert it into
838 * an access pet_expr.
840 __isl_give pet_expr
*PetScan::extract_access_expr(ValueDecl
*decl
)
842 return pet_expr_access_from_index(decl
->getType(),
843 extract_index_expr(decl
), ast_context
);
846 __isl_give pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
848 return extract_expr(expr
->getSubExpr());
851 /* Extract an assume statement from the argument "expr"
852 * of a __builtin_assume or __pencil_assume statement.
854 __isl_give pet_expr
*PetScan::extract_assume(Expr
*expr
)
856 return pet_expr_new_unary(0, pet_op_assume
, extract_expr(expr
));
859 /* If "expr" is an address-of operator, then return its argument.
860 * Otherwise, return NULL.
862 static Expr
*extract_addr_of_arg(Expr
*expr
)
866 if (expr
->getStmtClass() != Stmt::UnaryOperatorClass
)
868 op
= cast
<UnaryOperator
>(expr
);
869 if (op
->getOpcode() != UO_AddrOf
)
871 return op
->getSubExpr();
874 /* Construct a pet_expr corresponding to the function call argument "expr".
875 * The argument appears in position "pos" of a call to function "fd".
877 * If we are passing along a pointer to an array element
878 * or an entire row or even higher dimensional slice of an array,
879 * then the function being called may write into the array.
881 * We assume here that if the function is declared to take a pointer
882 * to a const type, then the function may only perform a read
883 * and that otherwise, it may either perform a read or a write (or both).
884 * We only perform this check if "detect_writes" is set.
886 __isl_give pet_expr
*PetScan::extract_argument(FunctionDecl
*fd
, int pos
,
887 Expr
*expr
, bool detect_writes
)
891 int is_addr
= 0, is_partial
= 0;
893 expr
= pet_clang_strip_casts(expr
);
894 arg
= extract_addr_of_arg(expr
);
899 res
= extract_expr(expr
);
902 if (pet_clang_array_depth(expr
->getType()) > 0)
904 if (detect_writes
&& (is_addr
|| is_partial
) &&
905 pet_expr_get_type(res
) == pet_expr_access
) {
907 if (!fd
->hasPrototype()) {
908 report_prototype_required(expr
);
909 return pet_expr_free(res
);
911 parm
= fd
->getParamDecl(pos
);
912 if (!const_base(parm
->getType()))
913 res
= mark_may_write(res
);
917 res
= pet_expr_new_unary(0, pet_op_address_of
, res
);
921 /* Find the first FunctionDecl with the given name.
922 * "call" is the corresponding call expression and is only used
923 * for reporting errors.
925 * Return NULL on error.
927 FunctionDecl
*PetScan::find_decl_from_name(CallExpr
*call
, string name
)
929 TranslationUnitDecl
*tu
= ast_context
.getTranslationUnitDecl();
930 DeclContext::decl_iterator begin
= tu
->decls_begin();
931 DeclContext::decl_iterator end
= tu
->decls_end();
932 for (DeclContext::decl_iterator i
= begin
; i
!= end
; ++i
) {
933 FunctionDecl
*fd
= dyn_cast
<FunctionDecl
>(*i
);
936 if (fd
->getName().str().compare(name
) != 0)
940 report_missing_summary_function_body(call
);
943 report_missing_summary_function(call
);
947 /* Return the FunctionDecl for the summary function associated to the
948 * function called by "call".
950 * In particular, if the pencil option is set, then
951 * search for an annotate attribute formatted as
952 * "pencil_access(name)", where "name" is the name of the summary function.
954 * If no summary function was specified, then return the FunctionDecl
955 * that is actually being called.
957 * Return NULL on error.
959 FunctionDecl
*PetScan::get_summary_function(CallExpr
*call
)
961 FunctionDecl
*decl
= call
->getDirectCallee();
965 if (!options
->pencil
)
968 specific_attr_iterator
<AnnotateAttr
> begin
, end
, i
;
969 begin
= decl
->specific_attr_begin
<AnnotateAttr
>();
970 end
= decl
->specific_attr_end
<AnnotateAttr
>();
971 for (i
= begin
; i
!= end
; ++i
) {
972 string attr
= (*i
)->getAnnotation().str();
974 const char prefix
[] = "pencil_access(";
975 size_t start
= attr
.find(prefix
);
976 if (start
== string::npos
)
978 start
+= strlen(prefix
);
979 string name
= attr
.substr(start
, attr
.find(')') - start
);
981 return find_decl_from_name(call
, name
);
987 /* Is "name" the name of an assume statement?
988 * "pencil" indicates whether pencil builtins and pragmas should be supported.
989 * "__builtin_assume" is always accepted.
990 * If "pencil" is set, then "__pencil_assume" is also accepted.
992 static bool is_assume(int pencil
, const string
&name
)
994 if (name
== "__builtin_assume")
996 return pencil
&& name
== "__pencil_assume";
999 /* Construct a pet_expr representing a function call.
1001 * If this->call2id is not NULL and it contains a mapping for this call,
1002 * then this means that the corresponding function has been inlined.
1003 * Return a pet_expr that reads from the variable that
1004 * stores the return value of the inlined call.
1006 * In the special case of a "call" to __builtin_assume or __pencil_assume,
1007 * construct an assume expression instead.
1009 * In the case of a "call" to __pencil_kill, the arguments
1010 * are neither read nor written (only killed), so there
1011 * is no need to check for writes to these arguments.
1013 * __pencil_assume and __pencil_kill are only recognized
1014 * when the pencil option is set.
1016 __isl_give pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1018 pet_expr
*res
= NULL
;
1024 if (call2id
&& call2id
->find(expr
) != call2id
->end())
1025 return pet_expr_access_from_id(isl_id_copy(call2id
[0][expr
]),
1028 fd
= expr
->getDirectCallee();
1034 name
= fd
->getDeclName().getAsString();
1035 n_arg
= expr
->getNumArgs();
1037 if (n_arg
== 1 && is_assume(options
->pencil
, name
))
1038 return extract_assume(expr
->getArg(0));
1039 is_kill
= options
->pencil
&& name
== "__pencil_kill";
1041 res
= pet_expr_new_call(ctx
, name
.c_str(), n_arg
);
1045 for (unsigned i
= 0; i
< n_arg
; ++i
) {
1046 Expr
*arg
= expr
->getArg(i
);
1047 res
= pet_expr_set_arg(res
, i
,
1048 PetScan::extract_argument(fd
, i
, arg
, !is_kill
));
1051 fd
= get_summary_function(expr
);
1053 return pet_expr_free(res
);
1055 res
= set_summary(res
, fd
);
1060 /* Construct a pet_expr representing a (C style) cast.
1062 __isl_give pet_expr
*PetScan::extract_expr(CStyleCastExpr
*expr
)
1067 arg
= extract_expr(expr
->getSubExpr());
1071 type
= expr
->getTypeAsWritten();
1072 return pet_expr_new_cast(type
.getAsString().c_str(), arg
);
1075 /* Construct a pet_expr representing an integer.
1077 __isl_give pet_expr
*PetScan::extract_expr(IntegerLiteral
*expr
)
1079 return pet_expr_new_int(extract_int(expr
));
1082 /* Construct a pet_expr representing the integer enum constant "ecd".
1084 __isl_give pet_expr
*PetScan::extract_expr(EnumConstantDecl
*ecd
)
1087 const llvm::APSInt
&init
= ecd
->getInitVal();
1088 v
= ::extract_int(ctx
, init
.isSigned(), init
);
1089 return pet_expr_new_int(v
);
1092 /* Try and construct a pet_expr representing "expr".
1094 __isl_give pet_expr
*PetScan::extract_expr(Expr
*expr
)
1096 switch (expr
->getStmtClass()) {
1097 case Stmt::UnaryOperatorClass
:
1098 return extract_expr(cast
<UnaryOperator
>(expr
));
1099 case Stmt::CompoundAssignOperatorClass
:
1100 case Stmt::BinaryOperatorClass
:
1101 return extract_expr(cast
<BinaryOperator
>(expr
));
1102 case Stmt::ImplicitCastExprClass
:
1103 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1104 case Stmt::ArraySubscriptExprClass
:
1105 case Stmt::DeclRefExprClass
:
1106 case Stmt::MemberExprClass
:
1107 return extract_access_expr(expr
);
1108 case Stmt::IntegerLiteralClass
:
1109 return extract_expr(cast
<IntegerLiteral
>(expr
));
1110 case Stmt::FloatingLiteralClass
:
1111 return extract_expr(cast
<FloatingLiteral
>(expr
));
1112 case Stmt::ParenExprClass
:
1113 return extract_expr(cast
<ParenExpr
>(expr
));
1114 case Stmt::ConditionalOperatorClass
:
1115 return extract_expr(cast
<ConditionalOperator
>(expr
));
1116 case Stmt::CallExprClass
:
1117 return extract_expr(cast
<CallExpr
>(expr
));
1118 case Stmt::CStyleCastExprClass
:
1119 return extract_expr(cast
<CStyleCastExpr
>(expr
));
1126 /* Check if the given initialization statement is an assignment.
1127 * If so, return that assignment. Otherwise return NULL.
1129 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1131 BinaryOperator
*ass
;
1133 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1136 ass
= cast
<BinaryOperator
>(init
);
1137 if (ass
->getOpcode() != BO_Assign
)
1143 /* Check if the given initialization statement is a declaration
1144 * of a single variable.
1145 * If so, return that declaration. Otherwise return NULL.
1147 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1151 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1154 decl
= cast
<DeclStmt
>(init
);
1156 if (!decl
->isSingleDecl())
1159 return decl
->getSingleDecl();
1162 /* Given the assignment operator in the initialization of a for loop,
1163 * extract the induction variable, i.e., the (integer)variable being
1166 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1173 lhs
= init
->getLHS();
1174 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1179 ref
= cast
<DeclRefExpr
>(lhs
);
1180 decl
= ref
->getDecl();
1181 type
= decl
->getType().getTypePtr();
1183 if (!type
->isIntegerType()) {
1191 /* Given the initialization statement of a for loop and the single
1192 * declaration in this initialization statement,
1193 * extract the induction variable, i.e., the (integer) variable being
1196 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1200 vd
= cast
<VarDecl
>(decl
);
1202 const QualType type
= vd
->getType();
1203 if (!type
->isIntegerType()) {
1208 if (!vd
->getInit()) {
1216 /* Check that op is of the form iv++ or iv--.
1217 * Return a pet_expr representing "1" or "-1" accordingly.
1219 __isl_give pet_expr
*PetScan::extract_unary_increment(
1220 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
1226 if (!op
->isIncrementDecrementOp()) {
1231 sub
= op
->getSubExpr();
1232 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1237 ref
= cast
<DeclRefExpr
>(sub
);
1238 if (ref
->getDecl() != iv
) {
1243 if (op
->isIncrementOp())
1244 v
= isl_val_one(ctx
);
1246 v
= isl_val_negone(ctx
);
1248 return pet_expr_new_int(v
);
1251 /* Check if op is of the form
1255 * and return the increment "expr - iv" as a pet_expr.
1257 __isl_give pet_expr
*PetScan::extract_binary_increment(BinaryOperator
*op
,
1258 clang::ValueDecl
*iv
)
1263 pet_expr
*expr
, *expr_iv
;
1265 if (op
->getOpcode() != BO_Assign
) {
1271 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1276 ref
= cast
<DeclRefExpr
>(lhs
);
1277 if (ref
->getDecl() != iv
) {
1282 expr
= extract_expr(op
->getRHS());
1283 expr_iv
= extract_expr(lhs
);
1285 type_size
= pet_clang_get_type_size(iv
->getType(), ast_context
);
1286 return pet_expr_new_binary(type_size
, pet_op_sub
, expr
, expr_iv
);
1289 /* Check that op is of the form iv += cst or iv -= cst
1290 * and return a pet_expr corresponding to cst or -cst accordingly.
1292 __isl_give pet_expr
*PetScan::extract_compound_increment(
1293 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
1299 BinaryOperatorKind opcode
;
1301 opcode
= op
->getOpcode();
1302 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1306 if (opcode
== BO_SubAssign
)
1310 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1315 ref
= cast
<DeclRefExpr
>(lhs
);
1316 if (ref
->getDecl() != iv
) {
1321 expr
= extract_expr(op
->getRHS());
1324 type_size
= pet_clang_get_type_size(op
->getType(), ast_context
);
1325 expr
= pet_expr_new_unary(type_size
, pet_op_minus
, expr
);
1331 /* Check that the increment of the given for loop increments
1332 * (or decrements) the induction variable "iv" and return
1333 * the increment as a pet_expr if successful.
1335 __isl_give pet_expr
*PetScan::extract_increment(clang::ForStmt
*stmt
,
1338 Stmt
*inc
= stmt
->getInc();
1341 report_missing_increment(stmt
);
1345 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1346 return extract_unary_increment(cast
<UnaryOperator
>(inc
), iv
);
1347 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1348 return extract_compound_increment(
1349 cast
<CompoundAssignOperator
>(inc
), iv
);
1350 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1351 return extract_binary_increment(cast
<BinaryOperator
>(inc
), iv
);
1357 /* Construct a pet_tree for a while loop.
1359 * If we were only able to extract part of the body, then simply
1362 __isl_give pet_tree
*PetScan::extract(WhileStmt
*stmt
)
1367 tree
= extract(stmt
->getBody());
1370 pe_cond
= extract_expr(stmt
->getCond());
1371 tree
= pet_tree_new_while(pe_cond
, tree
);
1376 /* Construct a pet_tree for a for statement.
1377 * The for loop is required to be of one of the following forms
1379 * for (i = init; condition; ++i)
1380 * for (i = init; condition; --i)
1381 * for (i = init; condition; i += constant)
1382 * for (i = init; condition; i -= constant)
1384 * We extract a pet_tree for the body and then include it in a pet_tree
1385 * of type pet_tree_for.
1387 * As a special case, we also allow a for loop of the form
1391 * in which case we return a pet_tree of type pet_tree_infinite_loop.
1393 * If we were only able to extract part of the body, then simply
1396 __isl_give pet_tree
*PetScan::extract_for(ForStmt
*stmt
)
1398 BinaryOperator
*ass
;
1406 pet_expr
*pe_init
, *pe_inc
, *pe_iv
, *pe_cond
;
1408 independent
= is_current_stmt_marked_independent();
1410 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc()) {
1411 tree
= extract(stmt
->getBody());
1414 tree
= pet_tree_new_infinite_loop(tree
);
1418 init
= stmt
->getInit();
1423 if ((ass
= initialization_assignment(init
)) != NULL
) {
1424 iv
= extract_induction_variable(ass
);
1427 rhs
= ass
->getRHS();
1428 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
1429 VarDecl
*var
= extract_induction_variable(init
, decl
);
1433 rhs
= var
->getInit();
1435 unsupported(stmt
->getInit());
1439 declared
= !initialization_assignment(stmt
->getInit());
1440 tree
= extract(stmt
->getBody());
1443 pe_iv
= extract_access_expr(iv
);
1444 pe_iv
= mark_write(pe_iv
);
1445 pe_init
= extract_expr(rhs
);
1446 if (!stmt
->getCond())
1447 pe_cond
= pet_expr_new_int(isl_val_one(ctx
));
1449 pe_cond
= extract_expr(stmt
->getCond());
1450 pe_inc
= extract_increment(stmt
, iv
);
1451 tree
= pet_tree_new_for(independent
, declared
, pe_iv
, pe_init
, pe_cond
,
1456 /* Store the names of the variables declared in decl_context
1457 * in the set declared_names. Make sure to only do this once by
1458 * setting declared_names_collected.
1460 void PetScan::collect_declared_names()
1462 DeclContext
*DC
= decl_context
;
1463 DeclContext::decl_iterator it
;
1465 if (declared_names_collected
)
1468 for (it
= DC
->decls_begin(); it
!= DC
->decls_end(); ++it
) {
1472 if (!isa
<NamedDecl
>(D
))
1474 named
= cast
<NamedDecl
>(D
);
1475 declared_names
.insert(named
->getName().str());
1478 declared_names_collected
= true;
1481 /* Add the names in "names" that are not also in this->declared_names
1482 * to this->used_names.
1483 * It is up to the caller to make sure that declared_names has been
1484 * populated, if needed.
1486 void PetScan::add_new_used_names(const std::set
<std::string
> &names
)
1488 std::set
<std::string
>::const_iterator it
;
1490 for (it
= names
.begin(); it
!= names
.end(); ++it
) {
1491 if (declared_names
.find(*it
) != declared_names
.end())
1493 used_names
.insert(*it
);
1497 /* Is the name "name" used in any declaration other than "decl"?
1499 * If the name was found to be in use before, the consider it to be in use.
1500 * Otherwise, check the DeclContext of the function containing the scop
1501 * as well as all ancestors of this DeclContext for declarations
1502 * other than "decl" that declare something called "name".
1504 bool PetScan::name_in_use(const string
&name
, Decl
*decl
)
1507 DeclContext::decl_iterator it
;
1509 if (used_names
.find(name
) != used_names
.end())
1512 for (DC
= decl_context
; DC
; DC
= DC
->getParent()) {
1513 for (it
= DC
->decls_begin(); it
!= DC
->decls_end(); ++it
) {
1519 if (!isa
<NamedDecl
>(D
))
1521 named
= cast
<NamedDecl
>(D
);
1522 if (named
->getName().str() == name
)
1530 /* Generate a new name based on "name" that is not in use.
1531 * Do so by adding a suffix _i, with i an integer.
1533 string
PetScan::generate_new_name(const string
&name
)
1538 std::ostringstream oss
;
1539 oss
<< name
<< "_" << n_rename
++;
1540 new_name
= oss
.str();
1541 } while (name_in_use(new_name
, NULL
));
1546 /* Try and construct a pet_tree corresponding to a compound statement.
1548 * "skip_declarations" is set if we should skip initial declarations
1549 * in the children of the compound statements.
1551 * Collect a new set of declarations for the current compound statement.
1552 * If any of the names in these declarations is also used by another
1553 * declaration reachable from the current function, then rename it
1554 * to a name that is not already in use.
1555 * In particular, keep track of the old and new names in a pet_substituter
1556 * and apply the substitutions to the pet_tree corresponding to the
1557 * compound statement.
1559 __isl_give pet_tree
*PetScan::extract(CompoundStmt
*stmt
,
1560 bool skip_declarations
)
1563 std::vector
<VarDecl
*> saved_declarations
;
1564 std::vector
<VarDecl
*>::iterator it
;
1565 pet_substituter substituter
;
1567 saved_declarations
= declarations
;
1568 declarations
.clear();
1569 tree
= extract(stmt
->children(), true, skip_declarations
, stmt
);
1570 for (it
= declarations
.begin(); it
!= declarations
.end(); ++it
) {
1573 VarDecl
*decl
= *it
;
1574 string name
= decl
->getName().str();
1575 bool in_use
= name_in_use(name
, decl
);
1577 used_names
.insert(name
);
1581 name
= generate_new_name(name
);
1582 id
= pet_id_from_name_and_decl(ctx
, name
.c_str(), decl
);
1583 expr
= pet_expr_access_from_id(id
, ast_context
);
1584 id
= pet_id_from_decl(ctx
, decl
);
1585 substituter
.add_sub(id
, expr
);
1586 used_names
.insert(name
);
1588 tree
= substituter
.substitute(tree
);
1589 declarations
= saved_declarations
;
1594 /* Return the file offset of the expansion location of "Loc".
1596 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
1598 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
1601 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
1603 /* Return a SourceLocation for the location after the first semicolon
1604 * after "loc". If Lexer::findLocationAfterToken is available, we simply
1605 * call it and also skip trailing spaces and newline.
1607 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
1608 const LangOptions
&LO
)
1610 return Lexer::findLocationAfterToken(loc
, tok::semi
, SM
, LO
, true);
1615 /* Return a SourceLocation for the location after the first semicolon
1616 * after "loc". If Lexer::findLocationAfterToken is not available,
1617 * we look in the underlying character data for the first semicolon.
1619 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
1620 const LangOptions
&LO
)
1623 const char *s
= SM
.getCharacterData(loc
);
1625 semi
= strchr(s
, ';');
1627 return SourceLocation();
1628 return loc
.getFileLocWithOffset(semi
+ 1 - s
);
1633 /* If the token at "loc" is the first token on the line, then return
1634 * a location referring to the start of the line and set *indent
1635 * to the indentation of "loc"
1636 * Otherwise, return "loc" and set *indent to "".
1638 * This function is used to extend a scop to the start of the line
1639 * if the first token of the scop is also the first token on the line.
1641 * We look for the first token on the line. If its location is equal to "loc",
1642 * then the latter is the location of the first token on the line.
1644 static SourceLocation
move_to_start_of_line_if_first_token(SourceLocation loc
,
1645 SourceManager
&SM
, const LangOptions
&LO
, char **indent
)
1647 std::pair
<FileID
, unsigned> file_offset_pair
;
1648 llvm::StringRef file
;
1651 SourceLocation token_loc
, line_loc
;
1655 loc
= SM
.getExpansionLoc(loc
);
1656 col
= SM
.getExpansionColumnNumber(loc
);
1657 line_loc
= loc
.getLocWithOffset(1 - col
);
1658 file_offset_pair
= SM
.getDecomposedLoc(line_loc
);
1659 file
= SM
.getBufferData(file_offset_pair
.first
, NULL
);
1660 pos
= file
.data() + file_offset_pair
.second
;
1662 Lexer
lexer(SM
.getLocForStartOfFile(file_offset_pair
.first
), LO
,
1663 file
.begin(), pos
, file
.end());
1664 lexer
.LexFromRawLexer(tok
);
1665 token_loc
= tok
.getLocation();
1667 s
= SM
.getCharacterData(line_loc
);
1668 *indent
= strndup(s
, token_loc
== loc
? col
- 1 : 0);
1670 if (token_loc
== loc
)
1676 /* Construct a pet_loc corresponding to the region covered by "range".
1677 * If "skip_semi" is set, then we assume "range" is followed by
1678 * a semicolon and also include this semicolon.
1680 __isl_give pet_loc
*PetScan::construct_pet_loc(SourceRange range
,
1683 SourceLocation loc
= range
.getBegin();
1684 SourceManager
&SM
= PP
.getSourceManager();
1685 const LangOptions
&LO
= PP
.getLangOpts();
1686 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
1687 unsigned start
, end
;
1690 loc
= move_to_start_of_line_if_first_token(loc
, SM
, LO
, &indent
);
1691 start
= getExpansionOffset(SM
, loc
);
1692 loc
= range
.getEnd();
1694 loc
= location_after_semi(loc
, SM
, LO
);
1696 loc
= PP
.getLocForEndOfToken(loc
);
1697 end
= getExpansionOffset(SM
, loc
);
1699 return pet_loc_alloc(ctx
, start
, end
, line
, indent
);
1702 /* Convert a top-level pet_expr to an expression pet_tree.
1704 __isl_give pet_tree
*PetScan::extract(__isl_take pet_expr
*expr
,
1705 SourceRange range
, bool skip_semi
)
1710 tree
= pet_tree_new_expr(expr
);
1711 loc
= construct_pet_loc(range
, skip_semi
);
1712 tree
= pet_tree_set_loc(tree
, loc
);
1717 /* Construct a pet_tree for an if statement.
1719 __isl_give pet_tree
*PetScan::extract(IfStmt
*stmt
)
1722 pet_tree
*tree
, *tree_else
;
1724 pe_cond
= extract_expr(stmt
->getCond());
1725 tree
= extract(stmt
->getThen());
1726 if (stmt
->getElse()) {
1727 tree_else
= extract(stmt
->getElse());
1728 if (options
->autodetect
) {
1729 if (tree
&& !tree_else
) {
1731 pet_expr_free(pe_cond
);
1734 if (!tree
&& tree_else
) {
1736 pet_expr_free(pe_cond
);
1740 tree
= pet_tree_new_if_else(pe_cond
, tree
, tree_else
);
1742 tree
= pet_tree_new_if(pe_cond
, tree
);
1746 /* Is "parent" a compound statement that has "stmt" as its final child?
1748 static bool final_in_compound(ReturnStmt
*stmt
, Stmt
*parent
)
1752 c
= dyn_cast
<CompoundStmt
>(parent
);
1756 StmtRange range
= c
->children();
1758 for (i
= range
.first
; i
!= range
.second
; ++i
)
1760 return last
== stmt
;
1765 /* Try and construct a pet_tree for a return statement "stmt".
1767 * Return statements are only allowed in a context where
1768 * this->return_root has been set.
1769 * Furthermore, "stmt" should appear as the last child
1770 * in the compound statement this->return_root.
1772 __isl_give pet_tree
*PetScan::extract(ReturnStmt
*stmt
)
1777 report_unsupported_return(stmt
);
1780 if (!final_in_compound(stmt
, return_root
)) {
1781 report_return_not_at_end_of_function(stmt
);
1785 val
= extract_expr(stmt
->getRetValue());
1786 return pet_tree_new_return(val
);
1789 /* Try and construct a pet_tree for a label statement.
1791 __isl_give pet_tree
*PetScan::extract(LabelStmt
*stmt
)
1796 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
1798 tree
= extract(stmt
->getSubStmt());
1799 tree
= pet_tree_set_label(tree
, label
);
1803 /* Update the location of "tree" to include the source range of "stmt".
1805 * Actually, we create a new location based on the source range of "stmt" and
1806 * then extend this new location to include the region of the original location.
1807 * This ensures that the line number of the final location refers to "stmt".
1809 __isl_give pet_tree
*PetScan::update_loc(__isl_take pet_tree
*tree
, Stmt
*stmt
)
1811 pet_loc
*loc
, *tree_loc
;
1813 tree_loc
= pet_tree_get_loc(tree
);
1814 loc
= construct_pet_loc(stmt
->getSourceRange(), false);
1815 loc
= pet_loc_update_start_end_from_loc(loc
, tree_loc
);
1816 pet_loc_free(tree_loc
);
1818 tree
= pet_tree_set_loc(tree
, loc
);
1822 /* Is "expr" of a type that can be converted to an access expression?
1824 static bool is_access_expr_type(Expr
*expr
)
1826 switch (expr
->getStmtClass()) {
1827 case Stmt::ArraySubscriptExprClass
:
1828 case Stmt::DeclRefExprClass
:
1829 case Stmt::MemberExprClass
:
1836 /* Tell the pet_inliner "inliner" about the formal arguments
1837 * in "fd" and the corresponding actual arguments in "call".
1838 * Return 0 if this was successful and -1 otherwise.
1840 * Any pointer argument is treated as an array.
1841 * The other arguments are treated as scalars.
1843 * In case of scalars, there is no restriction on the actual argument.
1844 * This actual argument is assigned to a variable with a name
1845 * that is derived from the name of the corresponding formal argument,
1846 * but made not to conflict with any variable names that are
1849 * In case of arrays, the actual argument needs to be an expression
1850 * of a type that can be converted to an access expression or the address
1851 * of such an expression, ignoring implicit and redundant casts.
1853 int PetScan::set_inliner_arguments(pet_inliner
&inliner
, CallExpr
*call
,
1858 n
= fd
->getNumParams();
1859 for (unsigned i
= 0; i
< n
; ++i
) {
1860 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
1861 QualType type
= parm
->getType();
1866 arg
= call
->getArg(i
);
1867 if (pet_clang_array_depth(type
) == 0) {
1868 string name
= parm
->getName().str();
1869 if (name_in_use(name
, NULL
))
1870 name
= generate_new_name(name
);
1871 used_names
.insert(name
);
1872 inliner
.add_scalar_arg(parm
, name
, extract_expr(arg
));
1875 arg
= pet_clang_strip_casts(arg
);
1876 sub
= extract_addr_of_arg(arg
);
1879 arg
= pet_clang_strip_casts(sub
);
1881 if (!is_access_expr_type(arg
)) {
1882 report_unsupported_inline_function_argument(arg
);
1885 expr
= extract_access_expr(arg
);
1888 inliner
.add_array_arg(parm
, expr
, is_addr
);
1894 /* Internal data structure for PetScan::substitute_array_sizes.
1895 * ps is the PetScan on which the method was called.
1896 * substituter is the substituter that is used to substitute variables
1897 * in the size expressions.
1899 struct pet_substitute_array_sizes_data
{
1901 pet_substituter
*substituter
;
1905 static int substitute_array_size(__isl_keep pet_tree
*tree
, void *user
);
1908 /* If "tree" is a declaration, then perform the substitutions
1909 * in data->substituter on its size expression and store the result
1910 * in the size expression cache of data->ps such that the modified expression
1911 * will be used in subsequent calls to get_array_size.
1913 static int substitute_array_size(__isl_keep pet_tree
*tree
, void *user
)
1915 struct pet_substitute_array_sizes_data
*data
;
1917 pet_expr
*var
, *size
;
1919 if (!pet_tree_is_decl(tree
))
1922 data
= (struct pet_substitute_array_sizes_data
*) user
;
1923 var
= pet_tree_decl_get_var(tree
);
1924 id
= pet_expr_access_get_id(var
);
1927 size
= data
->ps
->get_array_size(id
);
1928 size
= data
->substituter
->substitute(size
);
1929 data
->ps
->set_array_size(id
, size
);
1934 /* Perform the substitutions in "substituter" on all the arrays declared
1935 * inside "tree" and store the results in the size expression cache
1936 * such that the modified expressions will be used in subsequent calls
1937 * to get_array_size.
1939 int PetScan::substitute_array_sizes(__isl_keep pet_tree
*tree
,
1940 pet_substituter
*substituter
)
1942 struct pet_substitute_array_sizes_data data
= { this, substituter
};
1944 return pet_tree_foreach_sub_tree(tree
, &substitute_array_size
, &data
);
1947 /* Try and construct a pet_tree from the body of "fd" using the actual
1948 * arguments in "call" in place of the formal arguments.
1949 * "fd" is assumed to point to the declaration with a function body.
1950 * In particular, construct a block that consists of assignments
1951 * of (parts of) the actual arguments to temporary variables
1952 * followed by the inlined function body with the formal arguments
1953 * replaced by (expressions containing) these temporary variables.
1954 * If "return_id" is set, then it is used to store the return value
1955 * of the inlined function.
1957 * The actual inlining is taken care of by the pet_inliner object.
1958 * This function merely calls set_inliner_arguments to tell
1959 * the pet_inliner about the actual arguments, extracts a pet_tree
1960 * from the body of the called function and then passes this pet_tree
1961 * to the pet_inliner.
1962 * The body of the called function is allowed to have a return statement
1964 * The substitutions performed by the inliner are also applied
1965 * to the size expressions of the arrays declared in the inlined
1966 * function. These size expressions are not stored in the tree
1967 * itself, but rather in the size expression cache.
1969 * During the extraction of the function body, all variables names
1970 * that are declared in the calling function as well all variable
1971 * names that are known to be in use are considered to be in use
1972 * in the called function to ensure that there is no naming conflict.
1973 * Similarly, the additional names that are in use in the called function
1974 * are considered to be in use in the calling function as well.
1976 * The location of the pet_tree is reset to the call site to ensure
1977 * that the extent of the scop does not include the body of the called
1980 __isl_give pet_tree
*PetScan::extract_inlined_call(CallExpr
*call
,
1981 FunctionDecl
*fd
, __isl_keep isl_id
*return_id
)
1983 int save_autodetect
;
1986 pet_inliner
inliner(ctx
, n_arg
, ast_context
);
1988 if (set_inliner_arguments(inliner
, call
, fd
) < 0)
1991 save_autodetect
= options
->autodetect
;
1992 options
->autodetect
= 0;
1993 PetScan
body_scan(PP
, ast_context
, fd
, loc
, options
,
1994 isl_union_map_copy(value_bounds
), independent
);
1995 collect_declared_names();
1996 body_scan
.add_new_used_names(declared_names
);
1997 body_scan
.add_new_used_names(used_names
);
1998 body_scan
.return_root
= fd
->getBody();
1999 tree
= body_scan
.extract(fd
->getBody(), false);
2000 add_new_used_names(body_scan
.used_names
);
2001 options
->autodetect
= save_autodetect
;
2003 tree_loc
= construct_pet_loc(call
->getSourceRange(), true);
2004 tree
= pet_tree_set_loc(tree
, tree_loc
);
2006 substitute_array_sizes(tree
, &inliner
);
2008 return inliner
.inline_tree(tree
, return_id
);
2011 /* Try and construct a pet_tree corresponding
2012 * to the expression statement "stmt".
2014 * First look for function calls that have corresponding bodies
2015 * marked "inline". Extract the inlined functions in a pet_inlined_calls
2016 * object. Then extract the statement itself, replacing calls
2017 * to inlined function by accesses to the corresponding return variables, and
2018 * return the combined result.
2019 * If the outer expression is itself a call to an inlined function,
2020 * then it already appears as one of the inlined functions and
2021 * no separate pet_tree needs to be extracted for "stmt" itself.
2023 __isl_give pet_tree
*PetScan::extract_expr_stmt(Stmt
*stmt
)
2027 pet_inlined_calls
ic(this);
2030 if (ic
.calls
.size() >= 1 && ic
.calls
[0] == stmt
) {
2031 tree
= pet_tree_new_block(ctx
, 0, 0);
2033 call2id
= &ic
.call2id
;
2034 expr
= extract_expr(cast
<Expr
>(stmt
));
2035 tree
= extract(expr
, stmt
->getSourceRange(), true);
2038 tree
= ic
.add_inlined(tree
);
2042 /* Try and construct a pet_tree corresponding to "stmt".
2044 * If "stmt" is a compound statement, then "skip_declarations"
2045 * indicates whether we should skip initial declarations in the
2046 * compound statement.
2048 * If the constructed pet_tree is not a (possibly) partial representation
2049 * of "stmt", we update start and end of the pet_scop to those of "stmt".
2050 * In particular, if skip_declarations is set, then we may have skipped
2051 * declarations inside "stmt" and so the pet_scop may not represent
2052 * the entire "stmt".
2053 * Note that this function may be called with "stmt" referring to the entire
2054 * body of the function, including the outer braces. In such cases,
2055 * skip_declarations will be set and the braces will not be taken into
2056 * account in tree->loc.
2058 __isl_give pet_tree
*PetScan::extract(Stmt
*stmt
, bool skip_declarations
)
2062 set_current_stmt(stmt
);
2064 if (isa
<Expr
>(stmt
))
2065 return extract_expr_stmt(cast
<Expr
>(stmt
));
2067 switch (stmt
->getStmtClass()) {
2068 case Stmt::WhileStmtClass
:
2069 tree
= extract(cast
<WhileStmt
>(stmt
));
2071 case Stmt::ForStmtClass
:
2072 tree
= extract_for(cast
<ForStmt
>(stmt
));
2074 case Stmt::IfStmtClass
:
2075 tree
= extract(cast
<IfStmt
>(stmt
));
2077 case Stmt::CompoundStmtClass
:
2078 tree
= extract(cast
<CompoundStmt
>(stmt
), skip_declarations
);
2080 case Stmt::LabelStmtClass
:
2081 tree
= extract(cast
<LabelStmt
>(stmt
));
2083 case Stmt::ContinueStmtClass
:
2084 tree
= pet_tree_new_continue(ctx
);
2086 case Stmt::BreakStmtClass
:
2087 tree
= pet_tree_new_break(ctx
);
2089 case Stmt::DeclStmtClass
:
2090 tree
= extract(cast
<DeclStmt
>(stmt
));
2092 case Stmt::NullStmtClass
:
2093 tree
= pet_tree_new_block(ctx
, 0, 0);
2095 case Stmt::ReturnStmtClass
:
2096 tree
= extract(cast
<ReturnStmt
>(stmt
));
2099 report_unsupported_statement_type(stmt
);
2103 if (partial
|| skip_declarations
)
2106 return update_loc(tree
, stmt
);
2109 /* Given a sequence of statements "stmt_range" of which the first "n_decl"
2110 * are declarations and of which the remaining statements are represented
2111 * by "tree", try and extend "tree" to include the last sequence of
2112 * the initial declarations that can be completely extracted.
2114 * We start collecting the initial declarations and start over
2115 * whenever we come across a declaration that we cannot extract.
2116 * If we have been able to extract any declarations, then we
2117 * copy over the contents of "tree" at the end of the declarations.
2118 * Otherwise, we simply return the original "tree".
2120 __isl_give pet_tree
*PetScan::insert_initial_declarations(
2121 __isl_take pet_tree
*tree
, int n_decl
, StmtRange stmt_range
)
2129 n_stmt
= pet_tree_block_n_child(tree
);
2130 is_block
= pet_tree_block_get_block(tree
);
2131 res
= pet_tree_new_block(ctx
, is_block
, n_decl
+ n_stmt
);
2133 for (i
= stmt_range
.first
; n_decl
; ++i
, --n_decl
) {
2137 tree_i
= extract(child
);
2138 if (tree_i
&& !partial
) {
2139 res
= pet_tree_block_add_child(res
, tree_i
);
2142 pet_tree_free(tree_i
);
2144 if (pet_tree_block_n_child(res
) == 0)
2147 res
= pet_tree_new_block(ctx
, is_block
, n_decl
+ n_stmt
);
2150 if (pet_tree_block_n_child(res
) == 0) {
2155 for (j
= 0; j
< n_stmt
; ++j
) {
2158 tree_i
= pet_tree_block_get_child(tree
, j
);
2159 res
= pet_tree_block_add_child(res
, tree_i
);
2161 pet_tree_free(tree
);
2166 /* Try and construct a pet_tree corresponding to (part of)
2167 * a sequence of statements.
2169 * "block" is set if the sequence represents the children of
2170 * a compound statement.
2171 * "skip_declarations" is set if we should skip initial declarations
2172 * in the sequence of statements.
2173 * "parent" is the statement that has stmt_range as (some of) its children.
2175 * If autodetect is set, then we allow the extraction of only a subrange
2176 * of the sequence of statements. However, if there is at least one
2177 * kill and there is some subsequent statement for which we could not
2178 * construct a tree, then turn off the "block" property of the tree
2179 * such that no extra kill will be introduced at the end of the (partial)
2180 * block. If, on the other hand, the final range contains
2181 * no statements, then we discard the entire range.
2182 * If only a subrange of the sequence was extracted, but each statement
2183 * in the sequence was extracted completely, and if there are some
2184 * variable declarations in the sequence before or inside
2185 * the extracted subrange, then check if any of these variables are
2186 * not used after the extracted subrange. If so, add kills to these
2189 * If the entire range was extracted, apart from some initial declarations,
2190 * then we try and extend the range with the latest of those initial
2193 __isl_give pet_tree
*PetScan::extract(StmtRange stmt_range
, bool block
,
2194 bool skip_declarations
, Stmt
*parent
)
2198 bool has_kills
= false;
2199 bool partial_range
= false;
2200 bool outer_partial
= false;
2202 SourceManager
&SM
= PP
.getSourceManager();
2203 pet_killed_locals
kl(SM
);
2204 unsigned range_start
, range_end
;
2206 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
)
2209 tree
= pet_tree_new_block(ctx
, block
, j
);
2212 i
= stmt_range
.first
;
2213 if (skip_declarations
)
2214 for (; i
!= stmt_range
.second
; ++i
) {
2215 if ((*i
)->getStmtClass() != Stmt::DeclStmtClass
)
2217 if (options
->autodetect
)
2218 kl
.add_locals(cast
<DeclStmt
>(*i
));
2222 for (; i
!= stmt_range
.second
; ++i
) {
2226 tree_i
= extract(child
);
2227 if (pet_tree_block_n_child(tree
) != 0 && partial
) {
2228 pet_tree_free(tree_i
);
2231 if (child
->getStmtClass() == Stmt::DeclStmtClass
) {
2232 if (options
->autodetect
)
2233 kl
.add_locals(cast
<DeclStmt
>(child
));
2234 if (tree_i
&& block
)
2237 if (options
->autodetect
) {
2239 range_end
= getExpansionOffset(SM
,
2241 if (pet_tree_block_n_child(tree
) == 0)
2242 range_start
= getExpansionOffset(SM
,
2244 tree
= pet_tree_block_add_child(tree
, tree_i
);
2246 partial_range
= true;
2248 if (pet_tree_block_n_child(tree
) != 0 && !tree_i
)
2249 outer_partial
= partial
= true;
2251 tree
= pet_tree_block_add_child(tree
, tree_i
);
2254 if (partial
|| !tree
)
2263 tree
= pet_tree_block_set_block(tree
, 0);
2264 if (outer_partial
) {
2265 kl
.remove_accessed_after(parent
,
2266 range_start
, range_end
);
2267 tree
= add_kills(tree
, kl
.locals
);
2269 } else if (partial_range
) {
2270 if (pet_tree_block_n_child(tree
) == 0) {
2271 pet_tree_free(tree
);
2275 } else if (skip
> 0)
2276 tree
= insert_initial_declarations(tree
, skip
, stmt_range
);
2282 static __isl_give pet_expr
*get_array_size(__isl_keep pet_expr
*access
,
2284 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
2285 __isl_keep pet_context
*pc
, void *user
);
2288 /* Construct a pet_expr that holds the sizes of the array accessed
2290 * This function is used as a callback to pet_context_add_parameters,
2291 * which is also passed a pointer to the PetScan object.
2293 static __isl_give pet_expr
*get_array_size(__isl_keep pet_expr
*access
,
2296 PetScan
*ps
= (PetScan
*) user
;
2300 id
= pet_expr_access_get_id(access
);
2301 size
= ps
->get_array_size(id
);
2307 /* Construct and return a pet_array corresponding to the variable
2308 * accessed by "access".
2309 * This function is used as a callback to pet_scop_from_pet_tree,
2310 * which is also passed a pointer to the PetScan object.
2312 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
2313 __isl_keep pet_context
*pc
, void *user
)
2315 PetScan
*ps
= (PetScan
*) user
;
2319 id
= pet_expr_access_get_id(access
);
2320 array
= ps
->extract_array(id
, NULL
, pc
);
2326 /* Extract a function summary from the body of "fd".
2328 * We extract a scop from the function body in a context with as
2329 * parameters the integer arguments of the function.
2330 * We turn off autodetection (in case it was set) to ensure that
2331 * the entire function body is considered.
2332 * We then collect the accessed array elements and attach them
2333 * to the corresponding array arguments, taking into account
2334 * that the function body may access members of array elements.
2335 * The function body is allowed to have a return statement at the end.
2337 * The reason for representing the integer arguments as parameters in
2338 * the context is that if we were to instead start with a context
2339 * with the function arguments as initial dimensions, then we would not
2340 * be able to refer to them from the array extents, without turning
2341 * array extents into maps.
2343 * The result is stored in the summary_cache cache so that we can reuse
2344 * it if this method gets called on the same function again later on.
2346 __isl_give pet_function_summary
*PetScan::get_summary(FunctionDecl
*fd
)
2352 pet_function_summary
*summary
;
2355 int save_autodetect
;
2356 struct pet_scop
*scop
;
2358 isl_union_set
*may_read
, *may_write
, *must_write
;
2359 isl_union_map
*to_inner
;
2361 if (summary_cache
.find(fd
) != summary_cache
.end())
2362 return pet_function_summary_copy(summary_cache
[fd
]);
2364 space
= isl_space_set_alloc(ctx
, 0, 0);
2366 n
= fd
->getNumParams();
2367 summary
= pet_function_summary_alloc(ctx
, n
);
2368 for (unsigned i
= 0; i
< n
; ++i
) {
2369 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
2370 QualType type
= parm
->getType();
2373 if (!type
->isIntegerType())
2375 id
= pet_id_from_decl(ctx
, parm
);
2376 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2377 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0,
2379 summary
= pet_function_summary_set_int(summary
, i
, id
);
2382 save_autodetect
= options
->autodetect
;
2383 options
->autodetect
= 0;
2384 PetScan
body_scan(PP
, ast_context
, fd
, loc
, options
,
2385 isl_union_map_copy(value_bounds
), independent
);
2387 body_scan
.return_root
= fd
->getBody();
2388 tree
= body_scan
.extract(fd
->getBody(), false);
2390 domain
= isl_set_universe(space
);
2391 pc
= pet_context_alloc(domain
);
2392 pc
= pet_context_add_parameters(pc
, tree
,
2393 &::get_array_size
, &body_scan
);
2394 int_size
= size_in_bytes(ast_context
, ast_context
.IntTy
);
2395 scop
= pet_scop_from_pet_tree(tree
, int_size
,
2396 &::extract_array
, &body_scan
, pc
);
2397 scop
= scan_arrays(scop
, pc
);
2398 may_read
= isl_union_map_range(pet_scop_get_may_reads(scop
));
2399 may_write
= isl_union_map_range(pet_scop_get_may_writes(scop
));
2400 must_write
= isl_union_map_range(pet_scop_get_must_writes(scop
));
2401 to_inner
= pet_scop_compute_outer_to_inner(scop
);
2402 pet_scop_free(scop
);
2404 for (unsigned i
= 0; i
< n
; ++i
) {
2405 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
2406 QualType type
= parm
->getType();
2407 struct pet_array
*array
;
2409 isl_union_set
*data_set
;
2410 isl_union_set
*may_read_i
, *may_write_i
, *must_write_i
;
2412 if (pet_clang_array_depth(type
) == 0)
2415 array
= body_scan
.extract_array(parm
, NULL
, pc
);
2416 space
= array
? isl_set_get_space(array
->extent
) : NULL
;
2417 pet_array_free(array
);
2418 data_set
= isl_union_set_from_set(isl_set_universe(space
));
2419 data_set
= isl_union_set_apply(data_set
,
2420 isl_union_map_copy(to_inner
));
2421 may_read_i
= isl_union_set_intersect(
2422 isl_union_set_copy(may_read
),
2423 isl_union_set_copy(data_set
));
2424 may_write_i
= isl_union_set_intersect(
2425 isl_union_set_copy(may_write
),
2426 isl_union_set_copy(data_set
));
2427 must_write_i
= isl_union_set_intersect(
2428 isl_union_set_copy(must_write
), data_set
);
2429 summary
= pet_function_summary_set_array(summary
, i
,
2430 may_read_i
, may_write_i
, must_write_i
);
2433 isl_union_set_free(may_read
);
2434 isl_union_set_free(may_write
);
2435 isl_union_set_free(must_write
);
2436 isl_union_map_free(to_inner
);
2438 options
->autodetect
= save_autodetect
;
2439 pet_context_free(pc
);
2441 summary_cache
[fd
] = pet_function_summary_copy(summary
);
2446 /* If "fd" has a function body, then extract a function summary from
2447 * this body and attach it to the call expression "expr".
2449 * Even if a function body is available, "fd" itself may point
2450 * to a declaration without function body. We therefore first
2451 * replace it by the declaration that comes with a body (if any).
2453 __isl_give pet_expr
*PetScan::set_summary(__isl_take pet_expr
*expr
,
2456 pet_function_summary
*summary
;
2460 fd
= pet_clang_find_function_decl_with_body(fd
);
2464 summary
= get_summary(fd
);
2466 expr
= pet_expr_call_set_summary(expr
, summary
);
2471 /* Extract a pet_scop from "tree".
2473 * We simply call pet_scop_from_pet_tree with the appropriate arguments and
2474 * then add pet_arrays for all accessed arrays.
2475 * We populate the pet_context with assignments for all parameters used
2476 * inside "tree" or any of the size expressions for the arrays accessed
2477 * by "tree" so that they can be used in affine expressions.
2479 struct pet_scop
*PetScan::extract_scop(__isl_take pet_tree
*tree
)
2486 int_size
= size_in_bytes(ast_context
, ast_context
.IntTy
);
2488 domain
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
2489 pc
= pet_context_alloc(domain
);
2490 pc
= pet_context_add_parameters(pc
, tree
, &::get_array_size
, this);
2491 scop
= pet_scop_from_pet_tree(tree
, int_size
,
2492 &::extract_array
, this, pc
);
2493 scop
= scan_arrays(scop
, pc
);
2494 pet_context_free(pc
);
2499 /* Add a call to __pencil_kill to the end of "tree" that kills
2500 * all the variables in "locals" and return the result.
2502 * No location is added to the kill because the most natural
2503 * location would lie outside the scop. Attaching such a location
2504 * to this tree would extend the scope of the final result
2505 * to include the location.
2507 __isl_give pet_tree
*PetScan::add_kills(__isl_take pet_tree
*tree
,
2508 set
<ValueDecl
*> locals
)
2512 pet_tree
*kill
, *block
;
2513 set
<ValueDecl
*>::iterator it
;
2515 if (locals
.size() == 0)
2517 expr
= pet_expr_new_call(ctx
, "__pencil_kill", locals
.size());
2519 for (it
= locals
.begin(); it
!= locals
.end(); ++it
) {
2521 arg
= extract_access_expr(*it
);
2522 expr
= pet_expr_set_arg(expr
, i
++, arg
);
2524 kill
= pet_tree_new_expr(expr
);
2525 block
= pet_tree_new_block(ctx
, 0, 2);
2526 block
= pet_tree_block_add_child(block
, tree
);
2527 block
= pet_tree_block_add_child(block
, kill
);
2532 /* Check if the scop marked by the user is exactly this Stmt
2533 * or part of this Stmt.
2534 * If so, return a pet_scop corresponding to the marked region.
2535 * Otherwise, return NULL.
2537 * If the scop is not further nested inside a child of "stmt",
2538 * then check if there are any variable declarations before the scop
2539 * inside "stmt". If so, and if these variables are not used
2540 * after the scop, then add kills to the variables.
2542 * If the scop starts in the middle of one of the children, without
2543 * also ending in that child, then report an error.
2545 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
2547 SourceManager
&SM
= PP
.getSourceManager();
2548 unsigned start_off
, end_off
;
2551 start_off
= getExpansionOffset(SM
, begin_loc(stmt
));
2552 end_off
= getExpansionOffset(SM
, end_loc(stmt
));
2554 if (start_off
> loc
.end
)
2556 if (end_off
< loc
.start
)
2559 if (start_off
>= loc
.start
&& end_off
<= loc
.end
)
2560 return extract_scop(extract(stmt
));
2562 pet_killed_locals
kl(SM
);
2564 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
2565 Stmt
*child
= *start
;
2568 start_off
= getExpansionOffset(SM
, begin_loc(child
));
2569 end_off
= getExpansionOffset(SM
, end_loc(child
));
2570 if (start_off
< loc
.start
&& end_off
>= loc
.end
)
2572 if (start_off
>= loc
.start
)
2574 if (loc
.start
< end_off
) {
2575 report_unbalanced_pragmas(loc
.scop
, loc
.endscop
);
2578 if (isa
<DeclStmt
>(child
))
2579 kl
.add_locals(cast
<DeclStmt
>(child
));
2583 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
2585 start_off
= SM
.getFileOffset(begin_loc(child
));
2586 if (start_off
>= loc
.end
)
2590 kl
.remove_accessed_after(stmt
, loc
.start
, loc
.end
);
2592 tree
= extract(StmtRange(start
, end
), false, false, stmt
);
2593 tree
= add_kills(tree
, kl
.locals
);
2594 return extract_scop(tree
);
2597 /* Set the size of index "pos" of "array" to "size".
2598 * In particular, add a constraint of the form
2602 * to array->extent and a constraint of the form
2606 * to array->context.
2608 * The domain of "size" is assumed to be zero-dimensional.
2610 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
2611 __isl_take isl_pw_aff
*size
)
2624 valid
= isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
)));
2625 array
->context
= isl_set_intersect(array
->context
, valid
);
2627 dim
= isl_set_get_space(array
->extent
);
2628 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2629 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
2630 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
2631 index
= isl_pw_aff_alloc(univ
, aff
);
2633 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
2634 isl_set_dim(array
->extent
, isl_dim_set
));
2635 id
= isl_set_get_tuple_id(array
->extent
);
2636 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
2637 bound
= isl_pw_aff_lt_set(index
, size
);
2639 array
->extent
= isl_set_intersect(array
->extent
, bound
);
2641 if (!array
->context
|| !array
->extent
)
2642 return pet_array_free(array
);
2646 isl_pw_aff_free(size
);
2650 #ifdef HAVE_DECAYEDTYPE
2652 /* If "qt" is a decayed type, then set *decayed to true and
2653 * return the original type.
2655 static QualType
undecay(QualType qt
, bool *decayed
)
2657 const Type
*type
= qt
.getTypePtr();
2659 *decayed
= isa
<DecayedType
>(type
);
2661 qt
= cast
<DecayedType
>(type
)->getOriginalType();
2667 /* If "qt" is a decayed type, then set *decayed to true and
2668 * return the original type.
2669 * Since this version of clang does not define a DecayedType,
2670 * we cannot obtain the original type even if it had been decayed and
2671 * we set *decayed to false.
2673 static QualType
undecay(QualType qt
, bool *decayed
)
2681 /* Figure out the size of the array at position "pos" and all
2682 * subsequent positions from "qt" and update the corresponding
2683 * argument of "expr" accordingly.
2685 * The initial type (when pos is zero) may be a pointer type decayed
2686 * from an array type, if this initial type is the type of a function
2687 * argument. This only happens if the original array type has
2688 * a constant size in the outer dimension as otherwise we get
2689 * a VariableArrayType. Try and obtain this original type (if available) and
2690 * take the outer array size into account if it was marked static.
2692 __isl_give pet_expr
*PetScan::set_upper_bounds(__isl_take pet_expr
*expr
,
2693 QualType qt
, int pos
)
2695 const ArrayType
*atype
;
2697 bool decayed
= false;
2703 qt
= undecay(qt
, &decayed
);
2705 if (qt
->isPointerType()) {
2706 qt
= qt
->getPointeeType();
2707 return set_upper_bounds(expr
, qt
, pos
+ 1);
2709 if (!qt
->isArrayType())
2712 qt
= qt
->getCanonicalTypeInternal();
2713 atype
= cast
<ArrayType
>(qt
.getTypePtr());
2715 if (decayed
&& atype
->getSizeModifier() != ArrayType::Static
) {
2716 qt
= atype
->getElementType();
2717 return set_upper_bounds(expr
, qt
, pos
+ 1);
2720 if (qt
->isConstantArrayType()) {
2721 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
2722 size
= extract_expr(ca
->getSize());
2723 expr
= pet_expr_set_arg(expr
, pos
, size
);
2724 } else if (qt
->isVariableArrayType()) {
2725 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
2726 size
= extract_expr(vla
->getSizeExpr());
2727 expr
= pet_expr_set_arg(expr
, pos
, size
);
2730 qt
= atype
->getElementType();
2732 return set_upper_bounds(expr
, qt
, pos
+ 1);
2735 /* Construct a pet_expr that holds the sizes of the array represented by "id".
2736 * The returned expression is a call expression with as arguments
2737 * the sizes in each dimension. If we are unable to derive the size
2738 * in a given dimension, then the corresponding argument is set to infinity.
2739 * In fact, we initialize all arguments to infinity and then update
2740 * them if we are able to figure out the size.
2742 * The result is stored in the id_size cache so that it can be reused
2743 * if this method is called on the same array identifier later.
2744 * The result is also stored in the type_size cache in case
2745 * it gets called on a different array identifier with the same type.
2747 __isl_give pet_expr
*PetScan::get_array_size(__isl_keep isl_id
*id
)
2749 QualType qt
= pet_id_get_array_type(id
);
2751 pet_expr
*expr
, *inf
;
2752 const Type
*type
= qt
.getTypePtr();
2753 isl_maybe_pet_expr m
;
2755 m
= isl_id_to_pet_expr_try_get(id_size
, id
);
2756 if (m
.valid
< 0 || m
.valid
)
2758 if (type_size
.find(type
) != type_size
.end())
2759 return pet_expr_copy(type_size
[type
]);
2761 depth
= pet_clang_array_depth(qt
);
2762 inf
= pet_expr_new_int(isl_val_infty(ctx
));
2763 expr
= pet_expr_new_call(ctx
, "bounds", depth
);
2764 for (int i
= 0; i
< depth
; ++i
)
2765 expr
= pet_expr_set_arg(expr
, i
, pet_expr_copy(inf
));
2768 expr
= set_upper_bounds(expr
, qt
, 0);
2769 type_size
[type
] = pet_expr_copy(expr
);
2770 id_size
= isl_id_to_pet_expr_set(id_size
, isl_id_copy(id
),
2771 pet_expr_copy(expr
));
2776 /* Set the array size of the array identified by "id" to "size",
2777 * replacing any previously stored value.
2779 void PetScan::set_array_size(__isl_take isl_id
*id
, __isl_take pet_expr
*size
)
2781 id_size
= isl_id_to_pet_expr_set(id_size
, id
, size
);
2784 /* Does "expr" represent the "integer" infinity?
2786 static int is_infty(__isl_keep pet_expr
*expr
)
2791 if (pet_expr_get_type(expr
) != pet_expr_int
)
2793 v
= pet_expr_int_get_val(expr
);
2794 res
= isl_val_is_infty(v
);
2800 /* Figure out the dimensions of an array "array" and
2801 * update "array" accordingly.
2803 * We first construct a pet_expr that holds the sizes of the array
2804 * in each dimension. The resulting expression may containing
2805 * infinity values for dimension where we are unable to derive
2806 * a size expression.
2808 * The arguments of the size expression that have a value different from
2809 * infinity are then converted to an affine expression
2810 * within the context "pc" and incorporated into the size of "array".
2811 * If we are unable to convert a size expression to an affine expression or
2812 * if the size is not a (symbolic) constant,
2813 * then we leave the corresponding size of "array" untouched.
2815 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
2816 __isl_keep pet_context
*pc
)
2825 id
= isl_set_get_tuple_id(array
->extent
);
2827 return pet_array_free(array
);
2828 expr
= get_array_size(id
);
2831 n
= pet_expr_get_n_arg(expr
);
2832 for (int i
= 0; i
< n
; ++i
) {
2836 arg
= pet_expr_get_arg(expr
, i
);
2837 if (!is_infty(arg
)) {
2840 size
= pet_expr_extract_affine(arg
, pc
);
2841 dim
= isl_pw_aff_dim(size
, isl_dim_in
);
2843 array
= pet_array_free(array
);
2844 else if (isl_pw_aff_involves_nan(size
) ||
2845 isl_pw_aff_involves_dims(size
, isl_dim_in
, 0, dim
))
2846 isl_pw_aff_free(size
);
2848 size
= isl_pw_aff_drop_dims(size
,
2849 isl_dim_in
, 0, dim
);
2850 array
= update_size(array
, i
, size
);
2855 pet_expr_free(expr
);
2860 /* Does "decl" have a definition that we can keep track of in a pet_type?
2862 static bool has_printable_definition(RecordDecl
*decl
)
2864 if (!decl
->getDeclName())
2866 return decl
->getLexicalDeclContext() == decl
->getDeclContext();
2869 /* Add all TypedefType objects that appear when dereferencing "type"
2872 static void insert_intermediate_typedefs(PetTypes
*types
, QualType type
)
2874 type
= pet_clang_base_or_typedef_type(type
);
2875 while (isa
<TypedefType
>(type
)) {
2876 const TypedefType
*tt
;
2878 tt
= cast
<TypedefType
>(type
);
2879 types
->insert(tt
->getDecl());
2880 type
= tt
->desugar();
2881 type
= pet_clang_base_or_typedef_type(type
);
2885 /* Construct and return a pet_array corresponding to the variable
2886 * represented by "id".
2887 * In particular, initialize array->extent to
2889 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2891 * and then call set_upper_bounds to set the upper bounds on the indices
2892 * based on the type of the variable. The upper bounds are converted
2893 * to affine expressions within the context "pc".
2895 * If the base type is that of a record with a top-level definition or
2896 * of a typedef and if "types" is not null, then the RecordDecl or
2897 * TypedefType corresponding to the type, as well as any intermediate
2898 * TypedefType, is added to "types".
2900 * If the base type is that of a record with no top-level definition,
2901 * then we replace it by "<subfield>".
2903 * If the variable is a scalar, i.e., a zero-dimensional array,
2904 * then the "const" qualifier, if any, is removed from the base type.
2905 * This makes it easier for users of pet to turn initializations
2908 struct pet_array
*PetScan::extract_array(__isl_keep isl_id
*id
,
2909 PetTypes
*types
, __isl_keep pet_context
*pc
)
2911 struct pet_array
*array
;
2912 QualType qt
= pet_id_get_array_type(id
);
2913 int depth
= pet_clang_array_depth(qt
);
2914 QualType base
= pet_clang_base_type(qt
);
2918 array
= isl_calloc_type(ctx
, struct pet_array
);
2922 space
= isl_space_set_alloc(ctx
, 0, depth
);
2923 space
= isl_space_set_tuple_id(space
, isl_dim_set
, isl_id_copy(id
));
2925 array
->extent
= isl_set_nat_universe(space
);
2927 space
= isl_space_params_alloc(ctx
, 0);
2928 array
->context
= isl_set_universe(space
);
2930 array
= set_upper_bounds(array
, pc
);
2935 base
.removeLocalConst();
2936 name
= base
.getAsString();
2939 insert_intermediate_typedefs(types
, qt
);
2940 if (isa
<TypedefType
>(base
)) {
2941 types
->insert(cast
<TypedefType
>(base
)->getDecl());
2942 } else if (base
->isRecordType()) {
2943 RecordDecl
*decl
= pet_clang_record_decl(base
);
2944 TypedefNameDecl
*typedecl
;
2945 typedecl
= decl
->getTypedefNameForAnonDecl();
2947 types
->insert(typedecl
);
2948 else if (has_printable_definition(decl
))
2949 types
->insert(decl
);
2951 name
= "<subfield>";
2955 array
->element_type
= strdup(name
.c_str());
2956 array
->element_is_record
= base
->isRecordType();
2957 array
->element_size
= size_in_bytes(ast_context
, base
);
2962 /* Construct and return a pet_array corresponding to the variable "decl".
2964 struct pet_array
*PetScan::extract_array(ValueDecl
*decl
,
2965 PetTypes
*types
, __isl_keep pet_context
*pc
)
2970 id
= pet_id_from_decl(ctx
, decl
);
2971 array
= extract_array(id
, types
, pc
);
2977 /* Construct and return a pet_array corresponding to the sequence
2978 * of declarations represented by "decls".
2979 * The upper bounds of the array are converted to affine expressions
2980 * within the context "pc".
2981 * If the sequence contains a single declaration, then it corresponds
2982 * to a simple array access. Otherwise, it corresponds to a member access,
2983 * with the declaration for the substructure following that of the containing
2984 * structure in the sequence of declarations.
2985 * We start with the outermost substructure and then combine it with
2986 * information from the inner structures.
2988 * Additionally, keep track of all required types in "types".
2990 struct pet_array
*PetScan::extract_array(__isl_keep isl_id_list
*decls
,
2991 PetTypes
*types
, __isl_keep pet_context
*pc
)
2995 struct pet_array
*array
;
2997 id
= isl_id_list_get_id(decls
, 0);
2998 array
= extract_array(id
, types
, pc
);
3001 n
= isl_id_list_n_id(decls
);
3002 for (i
= 1; i
< n
; ++i
) {
3003 struct pet_array
*parent
;
3004 const char *base_name
, *field_name
;
3008 id
= isl_id_list_get_id(decls
, i
);
3009 array
= extract_array(id
, types
, pc
);
3012 return pet_array_free(parent
);
3014 base_name
= isl_set_get_tuple_name(parent
->extent
);
3015 field_name
= isl_set_get_tuple_name(array
->extent
);
3016 product_name
= pet_array_member_access_name(ctx
,
3017 base_name
, field_name
);
3019 array
->extent
= isl_set_product(isl_set_copy(parent
->extent
),
3022 array
->extent
= isl_set_set_tuple_name(array
->extent
,
3024 array
->context
= isl_set_intersect(array
->context
,
3025 isl_set_copy(parent
->context
));
3027 pet_array_free(parent
);
3030 if (!array
->extent
|| !array
->context
|| !product_name
)
3031 return pet_array_free(array
);
3037 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3038 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3039 std::set
<TypeDecl
*> &types_done
);
3040 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3041 TypedefNameDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3042 std::set
<TypeDecl
*> &types_done
);
3044 /* For each of the fields of "decl" that is itself a record type
3045 * or a typedef, or an array of such type, add a corresponding pet_type
3048 static struct pet_scop
*add_field_types(isl_ctx
*ctx
, struct pet_scop
*scop
,
3049 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3050 std::set
<TypeDecl
*> &types_done
)
3052 RecordDecl::field_iterator it
;
3054 for (it
= decl
->field_begin(); it
!= decl
->field_end(); ++it
) {
3055 QualType type
= it
->getType();
3057 type
= pet_clang_base_or_typedef_type(type
);
3058 if (isa
<TypedefType
>(type
)) {
3059 TypedefNameDecl
*typedefdecl
;
3061 typedefdecl
= cast
<TypedefType
>(type
)->getDecl();
3062 scop
= add_type(ctx
, scop
, typedefdecl
,
3063 PP
, types
, types_done
);
3064 } else if (type
->isRecordType()) {
3067 record
= pet_clang_record_decl(type
);
3068 scop
= add_type(ctx
, scop
, record
,
3069 PP
, types
, types_done
);
3076 /* Add a pet_type corresponding to "decl" to "scop", provided
3077 * it is a member of types.records and it has not been added before
3078 * (i.e., it is not a member of "types_done").
3080 * Since we want the user to be able to print the types
3081 * in the order in which they appear in the scop, we need to
3082 * make sure that types of fields in a structure appear before
3083 * that structure. We therefore call ourselves recursively
3084 * through add_field_types on the types of all record subfields.
3086 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3087 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3088 std::set
<TypeDecl
*> &types_done
)
3091 llvm::raw_string_ostream
S(s
);
3093 if (types
.records
.find(decl
) == types
.records
.end())
3095 if (types_done
.find(decl
) != types_done
.end())
3098 add_field_types(ctx
, scop
, decl
, PP
, types
, types_done
);
3100 if (strlen(decl
->getName().str().c_str()) == 0)
3103 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3106 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
3107 decl
->getName().str().c_str(), s
.c_str());
3108 if (!scop
->types
[scop
->n_type
])
3109 return pet_scop_free(scop
);
3111 types_done
.insert(decl
);
3118 /* Add a pet_type corresponding to "decl" to "scop", provided
3119 * it is a member of types.typedefs and it has not been added before
3120 * (i.e., it is not a member of "types_done").
3122 * If the underlying type is a structure, then we print the typedef
3123 * ourselves since clang does not print the definition of the structure
3124 * in the typedef. We also make sure in this case that the types of
3125 * the fields in the structure are added first.
3126 * Since the definition of the structure also gets printed this way,
3127 * add it to types_done such that it will not be printed again,
3128 * not even without the typedef.
3130 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3131 TypedefNameDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3132 std::set
<TypeDecl
*> &types_done
)
3135 llvm::raw_string_ostream
S(s
);
3136 QualType qt
= decl
->getUnderlyingType();
3138 if (types
.typedefs
.find(decl
) == types
.typedefs
.end())
3140 if (types_done
.find(decl
) != types_done
.end())
3143 if (qt
->isRecordType()) {
3144 RecordDecl
*rec
= pet_clang_record_decl(qt
);
3146 add_field_types(ctx
, scop
, rec
, PP
, types
, types_done
);
3148 rec
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3150 S
<< decl
->getName();
3151 types_done
.insert(rec
);
3153 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3157 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
3158 decl
->getName().str().c_str(), s
.c_str());
3159 if (!scop
->types
[scop
->n_type
])
3160 return pet_scop_free(scop
);
3162 types_done
.insert(decl
);
3169 /* Construct a list of pet_arrays, one for each array (or scalar)
3170 * accessed inside "scop", add this list to "scop" and return the result.
3171 * The upper bounds of the arrays are converted to affine expressions
3172 * within the context "pc".
3174 * The context of "scop" is updated with the intersection of
3175 * the contexts of all arrays, i.e., constraints on the parameters
3176 * that ensure that the arrays have a valid (non-negative) size.
3178 * If any of the extracted arrays refers to a member access or
3179 * has a typedef'd type as base type,
3180 * then also add the required types to "scop".
3181 * The typedef types are printed first because their definitions
3182 * may include the definition of a struct and these struct definitions
3183 * should not be printed separately. While the typedef definition
3184 * is being printed, the struct is marked as having been printed as well,
3185 * such that the later printing of the struct by itself can be prevented.
3187 * If the sequence of nested array declarations from which the pet_array
3188 * is extracted appears as the prefix of some other sequence,
3189 * then the pet_array is marked as "outer".
3190 * The arrays that already appear in scop->arrays at the start of
3191 * this function are assumed to be simple arrays, so they are not marked
3194 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
,
3195 __isl_keep pet_context
*pc
)
3198 array_desc_set arrays
, has_sub
;
3199 array_desc_set::iterator it
;
3201 std::set
<TypeDecl
*> types_done
;
3202 std::set
<clang::RecordDecl
*, less_name
>::iterator records_it
;
3203 std::set
<clang::TypedefNameDecl
*, less_name
>::iterator typedefs_it
;
3205 struct pet_array
**scop_arrays
;
3210 pet_scop_collect_arrays(scop
, arrays
);
3211 if (arrays
.size() == 0)
3214 n_array
= scop
->n_array
;
3216 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3217 n_array
+ arrays
.size());
3220 scop
->arrays
= scop_arrays
;
3222 for (it
= arrays
.begin(); it
!= arrays
.end(); ++it
) {
3223 isl_id_list
*list
= isl_id_list_copy(*it
);
3224 int n
= isl_id_list_n_id(list
);
3225 list
= isl_id_list_drop(list
, n
- 1, 1);
3226 has_sub
.insert(list
);
3229 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
3230 struct pet_array
*array
;
3231 array
= extract_array(*it
, &types
, pc
);
3232 scop
->arrays
[n_array
+ i
] = array
;
3233 if (!scop
->arrays
[n_array
+ i
])
3235 if (has_sub
.find(*it
) != has_sub
.end())
3238 scop
->context
= isl_set_intersect(scop
->context
,
3239 isl_set_copy(array
->context
));
3244 n
= types
.records
.size() + types
.typedefs
.size();
3248 scop
->types
= isl_alloc_array(ctx
, struct pet_type
*, n
);
3252 for (typedefs_it
= types
.typedefs
.begin();
3253 typedefs_it
!= types
.typedefs
.end(); ++typedefs_it
)
3254 scop
= add_type(ctx
, scop
, *typedefs_it
, PP
, types
, types_done
);
3256 for (records_it
= types
.records
.begin();
3257 records_it
!= types
.records
.end(); ++records_it
)
3258 scop
= add_type(ctx
, scop
, *records_it
, PP
, types
, types_done
);
3262 pet_scop_free(scop
);
3266 /* Bound all parameters in scop->context to the possible values
3267 * of the corresponding C variable.
3269 static struct pet_scop
*add_parameter_bounds(struct pet_scop
*scop
)
3276 n
= isl_set_dim(scop
->context
, isl_dim_param
);
3277 for (int i
= 0; i
< n
; ++i
) {
3281 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
3282 if (pet_nested_in_id(id
)) {
3284 isl_die(isl_set_get_ctx(scop
->context
),
3286 "unresolved nested parameter", goto error
);
3288 decl
= pet_id_get_decl(id
);
3291 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
3299 pet_scop_free(scop
);
3303 /* Construct a pet_scop from the given function.
3305 * If the scop was delimited by scop and endscop pragmas, then we override
3306 * the file offsets by those derived from the pragmas.
3308 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
3313 stmt
= fd
->getBody();
3315 if (options
->autodetect
) {
3316 set_current_stmt(stmt
);
3317 scop
= extract_scop(extract(stmt
, true));
3319 current_line
= loc
.start_line
;
3321 scop
= pet_scop_update_start_end(scop
, loc
.start
, loc
.end
);
3323 scop
= add_parameter_bounds(scop
);
3324 scop
= pet_scop_gist(scop
, value_bounds
);
3329 /* Update this->last_line and this->current_line based on the fact
3330 * that we are about to consider "stmt".
3332 void PetScan::set_current_stmt(Stmt
*stmt
)
3334 SourceLocation loc
= begin_loc(stmt
);
3335 SourceManager
&SM
= PP
.getSourceManager();
3337 last_line
= current_line
;
3338 current_line
= SM
.getExpansionLineNumber(loc
);
3341 /* Is the current statement marked by an independent pragma?
3342 * That is, is there an independent pragma on a line between
3343 * the line of the current statement and the line of the previous statement.
3344 * The search is not implemented very efficiently. We currently
3345 * assume that there are only a few independent pragmas, if any.
3347 bool PetScan::is_current_stmt_marked_independent()
3349 for (unsigned i
= 0; i
< independent
.size(); ++i
) {
3350 unsigned line
= independent
[i
].line
;
3352 if (last_line
< line
&& line
< current_line
)