25 #include "llvm/ADT/BitVector.h"
26 #include "llvm/ADT/SmallVector.h"
28 using namespace clang;
38 return isa<EnumConstantDecl>(DR->
getDecl());
43 return isa<IntegerLiteral>(Ex) || isa<StringLiteral>(Ex) ||
44 isa<CXXBoolLiteralExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) ||
45 isa<CharacterLiteral>(Ex) ||
53 if (
const DoStmt *DS = dyn_cast<DoStmt>(Term)) {
62 if (
const auto *DRE = dyn_cast<DeclRefExpr>(S))
63 if (
const auto *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()))
64 return FDecl->getIdentifier() &&
65 FDecl->getBuiltinID() == Builtin::BI__builtin_unreachable;
77 if (
const auto *CE = dyn_cast<CallExpr>(CS->getStmt())) {
78 return CE->getCallee()->IgnoreCasts() == S && CE->isBuiltinAssumeFalse(C);
90 for (
const CFGElement &CE : llvm::reverse(*Current)) {
92 if (
const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) {
95 if (
const Expr *RE = RS->getRetValue()) {
96 RE = RE->IgnoreParenCasts();
111 if (Current->getTerminator().isTemporaryDtorsBranch()) {
116 assert(Current->succ_size() == 2);
117 Current = *(Current->succ_begin() + 1);
118 }
else if (!Current->getTerminatorStmt() && Current->succ_size() == 1) {
121 Current = *Current->succ_begin();
122 if (Current->pred_size() > 1) {
133 llvm_unreachable(
"Broke out of infinite loop.");
141 Loc =
SM.getImmediateMacroCallerLoc(Loc);
149 bool IgnoreYES_NO =
false) {
163 if (MacroName ==
"YES" || MacroName ==
"NO")
169 if (MacroName ==
"false" || MacroName ==
"true")
189 bool IncludeIntegers =
true,
190 bool WrappedInParens =
false) {
194 if (
const auto *Ex = dyn_cast<Expr>(S))
195 S = Ex->IgnoreImplicit();
197 if (
const auto *Ex = dyn_cast<Expr>(S))
198 S = Ex->IgnoreCasts();
201 if (
const ParenExpr *PE = dyn_cast<ParenExpr>(S))
202 if (!PE->getBeginLoc().isMacroID())
204 IncludeIntegers,
true);
206 if (
const Expr *Ex = dyn_cast<Expr>(S))
207 S = Ex->IgnoreCasts();
209 bool IgnoreYES_NO =
false;
211 switch (S->getStmtClass()) {
212 case Stmt::CallExprClass: {
214 dyn_cast_or_null<FunctionDecl>(cast<CallExpr>(S)->getCalleeDecl());
215 return Callee ? Callee->isConstexpr() :
false;
217 case Stmt::DeclRefExprClass:
219 case Stmt::ObjCBoolLiteralExprClass:
222 case Stmt::CXXBoolLiteralExprClass:
223 case Stmt::IntegerLiteralClass: {
224 const Expr *E = cast<Expr>(S);
225 if (IncludeIntegers) {
226 if (SilenceableCondVal && !SilenceableCondVal->getBegin().isValid())
228 return WrappedInParens ||
233 case Stmt::MemberExprClass:
235 case Stmt::UnaryExprOrTypeTraitExprClass:
237 case Stmt::BinaryOperatorClass: {
248 case Stmt::UnaryOperatorClass: {
252 bool SilenceableCondValNotSet =
253 SilenceableCondVal && SilenceableCondVal->getBegin().isInvalid();
254 bool IsSubExprConfigValue =
256 IncludeIntegers, WrappedInParens);
259 if (SilenceableCondValNotSet &&
260 SilenceableCondVal->getBegin().isValid() &&
261 *SilenceableCondVal ==
264 return IsSubExprConfigValue;
274 if (
const VarDecl *VD = dyn_cast<VarDecl>(D)) {
282 if (!VD->hasLocalStorage())
287 return VD->getType().isLocalConstQualified();
296 if (isa<SwitchStmt>(Term))
299 if (isa<BinaryOperator>(Term)) {
309 llvm::BitVector &Reachable,
311 bool IncludeSometimesUnreachableEdges) {
327 while (!WL.empty()) {
328 const CFGBlock *item = WL.pop_back_val();
337 if (!IncludeSometimesUnreachableEdges)
338 TreatAllSuccessorsAsReachable =
false;
341 E = item->
succ_end(); I != E; ++I) {
344 const CFGBlock *UB = I->getPossiblyUnreachableBlock();
348 if (!TreatAllSuccessorsAsReachable.hasValue()) {
350 TreatAllSuccessorsAsReachable =
354 if (TreatAllSuccessorsAsReachable.getValue()) {
363 if (!Reachable[blockID]) {
364 Reachable.set(blockID);
376 llvm::BitVector &Reachable) {
386 llvm::BitVector Visited;
387 llvm::BitVector &Reachable;
395 DeferredLocsTy DeferredLocs;
399 : Visited(reachable.size()),
400 Reachable(reachable),
403 void enqueue(
const CFGBlock *block);
404 unsigned scanBackwards(
const CFGBlock *Start,
407 bool isDeadCodeRoot(
const CFGBlock *Block);
411 void reportDeadCode(
const CFGBlock *B,
417 void DeadCodeScan::enqueue(
const CFGBlock *block) {
419 if (Reachable[blockID] || Visited[blockID])
421 Visited[blockID] =
true;
422 WorkList.push_back(block);
426 bool isDeadRoot =
true;
429 E =
Block->pred_end(); I != E; ++I) {
430 if (
const CFGBlock *PredBlock = *I) {
431 unsigned blockID = PredBlock->getBlockID();
432 if (Visited[blockID]) {
436 if (!Reachable[blockID]) {
438 Visited[blockID] =
true;
439 WorkList.push_back(PredBlock);
449 if (S->getBeginLoc().isInvalid())
452 return BO->getOpcode() != BO_Comma;
459 const Stmt *S = CS->getStmt();
474 static int SrcCmp(
const std::pair<const CFGBlock *, const Stmt *> *p1,
475 const std::pair<const CFGBlock *, const Stmt *> *p2) {
476 if (p1->second->getBeginLoc() < p2->second->getBeginLoc())
478 if (p2->second->getBeginLoc() < p1->second->getBeginLoc())
489 while (!WorkList.empty()) {
494 if (Reachable[
Block->getBlockID()])
498 const Stmt *S = findDeadCode(Block);
503 E =
Block->pred_end(); I != E; ++I) {
511 if (S->getBeginLoc().isMacroID()) {
516 if (isDeadCodeRoot(Block)) {
517 reportDeadCode(Block, S, CB);
524 DeferredLocs.push_back(std::make_pair(Block, S));
530 if (!DeferredLocs.empty()) {
531 llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(),
SrcCmp);
532 for (
const auto &I : DeferredLocs) {
534 if (Reachable[
Block->getBlockID()])
536 reportDeadCode(Block, I.second, CB);
549 if (
const Expr *Ex = dyn_cast<Expr>(S))
550 S = Ex->IgnoreParenImpCasts();
552 switch (S->getStmtClass()) {
553 case Expr::BinaryOperatorClass: {
557 case Expr::UnaryOperatorClass: {
562 case Expr::CompoundAssignOperatorClass: {
568 case Expr::BinaryConditionalOperatorClass:
569 case Expr::ConditionalOperatorClass: {
571 cast<AbstractConditionalOperator>(S);
574 case Expr::MemberExprClass: {
579 case Expr::ArraySubscriptExprClass: {
585 case Expr::CStyleCastExprClass: {
590 case Expr::CXXFunctionalCastExprClass: {
595 case Stmt::CXXTryStmtClass: {
596 return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc();
598 case Expr::ObjCBridgedCastExprClass: {
605 R1 = S->getSourceRange();
606 return S->getBeginLoc();
609 void DeadCodeScan::reportDeadCode(
const CFGBlock *B,
615 if (isa<BreakStmt>(S)) {
635 if (
const ForStmt *FS = dyn_cast<ForStmt>(LoopTarget)) {
636 const Expr *Inc = FS->getInc();
651 if (
const CFGBlock *PredBlock = PI->getPossiblyUnreachableBlock()) {
652 const Stmt *TermCond =
653 PredBlock->getTerminatorCondition(
false);
668 namespace clang {
namespace reachable_code {
670 void Callback::anchor() { }
673 llvm::BitVector &Reachable) {
687 unsigned numReachable =
703 for (
const CFGBlock *block : *cfg) {
709 numReachable += DS.scanBackwards(block, CB);
711 if (numReachable == cfg->getNumBlockIDs())