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);
94 if (
const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) {
97 if (
const Expr *RE = RS->getRetValue()) {
98 RE = RE->IgnoreParenCasts();
113 if (Current->getTerminator().isTemporaryDtorsBranch()) {
118 assert(Current->succ_size() == 2);
119 Current = *(Current->succ_begin() + 1);
120 }
else if (!Current->getTerminatorStmt() && Current->succ_size() == 1) {
123 Current = *Current->succ_begin();
124 if (Current->pred_size() > 1) {
135 llvm_unreachable(
"Broke out of infinite loop.");
143 Loc =
SM.getImmediateMacroCallerLoc(Loc);
151 bool IgnoreYES_NO =
false) {
165 if (MacroName ==
"YES" || MacroName ==
"NO")
171 if (MacroName ==
"false" || MacroName ==
"true")
191 bool IncludeIntegers =
true,
192 bool WrappedInParens =
false) {
196 if (
const auto *Ex = dyn_cast<Expr>(S))
197 S = Ex->IgnoreImplicit();
199 if (
const auto *Ex = dyn_cast<Expr>(S))
200 S = Ex->IgnoreCasts();
203 if (
const ParenExpr *PE = dyn_cast<ParenExpr>(S))
204 if (!PE->getBeginLoc().isMacroID())
206 IncludeIntegers,
true);
208 if (
const Expr *Ex = dyn_cast<Expr>(S))
209 S = Ex->IgnoreCasts();
211 bool IgnoreYES_NO =
false;
213 switch (S->getStmtClass()) {
214 case Stmt::CallExprClass: {
216 dyn_cast_or_null<FunctionDecl>(cast<CallExpr>(S)->getCalleeDecl());
217 return Callee ? Callee->isConstexpr() :
false;
219 case Stmt::DeclRefExprClass:
221 case Stmt::ObjCBoolLiteralExprClass:
224 case Stmt::CXXBoolLiteralExprClass:
225 case Stmt::IntegerLiteralClass: {
226 const Expr *E = cast<Expr>(S);
227 if (IncludeIntegers) {
228 if (SilenceableCondVal && !SilenceableCondVal->getBegin().isValid())
234 case Stmt::MemberExprClass:
236 case Stmt::UnaryExprOrTypeTraitExprClass:
238 case Stmt::BinaryOperatorClass: {
249 case Stmt::UnaryOperatorClass: {
253 bool SilenceableCondValNotSet =
254 SilenceableCondVal && SilenceableCondVal->getBegin().isInvalid();
255 bool IsSubExprConfigValue =
257 IncludeIntegers, WrappedInParens);
260 if (SilenceableCondValNotSet &&
261 SilenceableCondVal->getBegin().isValid() &&
262 *SilenceableCondVal ==
265 return IsSubExprConfigValue;
275 if (
const VarDecl *VD = dyn_cast<VarDecl>(D)) {
283 if (!VD->hasLocalStorage())
288 return VD->getType().isLocalConstQualified();
297 if (isa<SwitchStmt>(Term))
300 if (isa<BinaryOperator>(Term)) {
310 llvm::BitVector &Reachable,
312 bool IncludeSometimesUnreachableEdges) {
328 while (!WL.empty()) {
329 const CFGBlock *item = WL.pop_back_val();
338 if (!IncludeSometimesUnreachableEdges)
339 TreatAllSuccessorsAsReachable =
false;
342 E = item->
succ_end(); I != E; ++I) {
345 const CFGBlock *UB = I->getPossiblyUnreachableBlock();
349 if (!TreatAllSuccessorsAsReachable.hasValue()) {
351 TreatAllSuccessorsAsReachable =
355 if (TreatAllSuccessorsAsReachable.getValue()) {
364 if (!Reachable[blockID]) {
365 Reachable.set(blockID);
377 llvm::BitVector &Reachable) {
387 llvm::BitVector Visited;
388 llvm::BitVector &Reachable;
396 DeferredLocsTy DeferredLocs;
400 : Visited(reachable.size()),
401 Reachable(reachable),
404 void enqueue(
const CFGBlock *block);
405 unsigned scanBackwards(
const CFGBlock *Start,
408 bool isDeadCodeRoot(
const CFGBlock *Block);
412 void reportDeadCode(
const CFGBlock *B,
418 void DeadCodeScan::enqueue(
const CFGBlock *block) {
420 if (Reachable[blockID] || Visited[blockID])
422 Visited[blockID] =
true;
423 WorkList.push_back(block);
427 bool isDeadRoot =
true;
430 E =
Block->pred_end(); I != E; ++I) {
431 if (
const CFGBlock *PredBlock = *I) {
432 unsigned blockID = PredBlock->getBlockID();
433 if (Visited[blockID]) {
437 if (!Reachable[blockID]) {
439 Visited[blockID] =
true;
440 WorkList.push_back(PredBlock);
450 if (S->getBeginLoc().isInvalid())
453 return BO->getOpcode() != BO_Comma;
460 const Stmt *S = CS->getStmt();
475 static int SrcCmp(
const std::pair<const CFGBlock *, const Stmt *> *p1,
476 const std::pair<const CFGBlock *, const Stmt *> *p2) {
477 if (p1->second->getBeginLoc() < p2->second->getBeginLoc())
479 if (p2->second->getBeginLoc() < p1->second->getBeginLoc())
490 while (!WorkList.empty()) {
495 if (Reachable[
Block->getBlockID()])
499 const Stmt *S = findDeadCode(Block);
504 E =
Block->pred_end(); I != E; ++I) {
512 if (S->getBeginLoc().isMacroID()) {
517 if (isDeadCodeRoot(Block)) {
518 reportDeadCode(Block, S, CB);
525 DeferredLocs.push_back(std::make_pair(Block, S));
531 if (!DeferredLocs.empty()) {
532 llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(),
SrcCmp);
533 for (DeferredLocsTy::iterator I = DeferredLocs.begin(),
534 E = DeferredLocs.end(); I != E; ++I) {
536 if (Reachable[
Block->getBlockID()])
538 reportDeadCode(Block, I->second, CB);
551 if (
const Expr *Ex = dyn_cast<Expr>(S))
552 S = Ex->IgnoreParenImpCasts();
554 switch (S->getStmtClass()) {
555 case Expr::BinaryOperatorClass: {
559 case Expr::UnaryOperatorClass: {
564 case Expr::CompoundAssignOperatorClass: {
570 case Expr::BinaryConditionalOperatorClass:
571 case Expr::ConditionalOperatorClass: {
573 cast<AbstractConditionalOperator>(S);
576 case Expr::MemberExprClass: {
581 case Expr::ArraySubscriptExprClass: {
587 case Expr::CStyleCastExprClass: {
592 case Expr::CXXFunctionalCastExprClass: {
597 case Stmt::CXXTryStmtClass: {
598 return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc();
600 case Expr::ObjCBridgedCastExprClass: {
607 R1 = S->getSourceRange();
608 return S->getBeginLoc();
611 void DeadCodeScan::reportDeadCode(
const CFGBlock *B,
617 if (isa<BreakStmt>(S)) {
637 if (
const ForStmt *FS = dyn_cast<ForStmt>(LoopTarget)) {
638 const Expr *Inc = FS->getInc();
653 if (
const CFGBlock *PredBlock = PI->getPossiblyUnreachableBlock()) {
654 const Stmt *TermCond =
655 PredBlock->getTerminatorCondition(
false);
670 namespace clang {
namespace reachable_code {
672 void Callback::anchor() { }
675 llvm::BitVector &Reachable) {
689 unsigned numReachable =
714 numReachable += DS.scanBackwards(block, CB);