From cbeffa3f6c77274f2e5f44f0743e92095f30a9e7 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 13 Mar 2020 10:27:02 -0400 Subject: [PATCH] [SimplifyCFG] convert if-else chain to switch; NFC Fix formatting of related function names while changing the code. --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 101 +++++++++++++++--------------- 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 222f8af..241218b 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -192,16 +192,17 @@ class SimplifyCFGOpt { bool FoldValueComparisonIntoPredecessors(Instruction *TI, IRBuilder<> &Builder); - bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder); - bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder); - bool SimplifySingleResume(ResumeInst *RI); - bool SimplifyCommonResume(ResumeInst *RI); - bool SimplifyCleanupReturn(CleanupReturnInst *RI); - bool SimplifyUnreachable(UnreachableInst *UI); - bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder); - bool SimplifyIndirectBr(IndirectBrInst *IBI); - bool SimplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder); - bool SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder); + bool simplifyReturn(ReturnInst *RI, IRBuilder<> &Builder); + bool simplifyResume(ResumeInst *RI, IRBuilder<> &Builder); + bool simplifySingleResume(ResumeInst *RI); + bool simplifyCommonResume(ResumeInst *RI); + bool simplifyCleanupReturn(CleanupReturnInst *RI); + bool simplifyUnreachable(UnreachableInst *UI); + bool simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder); + bool simplifyIndirectBr(IndirectBrInst *IBI); + bool simplifyBranch(BranchInst *Branch, IRBuilder<> &Builder); + bool simplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder); + bool simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder); bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI, IRBuilder<> &Builder); @@ -3863,19 +3864,19 @@ static bool SimplifyBranchOnICmpChain(BranchInst *BI, IRBuilder<> &Builder, return true; } -bool SimplifyCFGOpt::SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder) { +bool SimplifyCFGOpt::simplifyResume(ResumeInst *RI, IRBuilder<> &Builder) { if (isa(RI->getValue())) - return SimplifyCommonResume(RI); + return simplifyCommonResume(RI); else if (isa(RI->getParent()->getFirstNonPHI()) && RI->getValue() == RI->getParent()->getFirstNonPHI()) // The resume must unwind the exception that caused control to branch here. - return SimplifySingleResume(RI); + return simplifySingleResume(RI); return false; } // Simplify resume that is shared by several landing pads (phi of landing pad). -bool SimplifyCFGOpt::SimplifyCommonResume(ResumeInst *RI) { +bool SimplifyCFGOpt::simplifyCommonResume(ResumeInst *RI) { BasicBlock *BB = RI->getParent(); // Check that there are no other instructions except for debug intrinsics @@ -3954,7 +3955,7 @@ bool SimplifyCFGOpt::SimplifyCommonResume(ResumeInst *RI) { } // Simplify resume that is only used by a single (non-phi) landing pad. -bool SimplifyCFGOpt::SimplifySingleResume(ResumeInst *RI) { +bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) { BasicBlock *BB = RI->getParent(); auto *LPInst = cast(BB->getFirstNonPHI()); assert(RI->getValue() == LPInst && @@ -4149,7 +4150,7 @@ static bool mergeCleanupPad(CleanupReturnInst *RI) { return true; } -bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) { +bool SimplifyCFGOpt::simplifyCleanupReturn(CleanupReturnInst *RI) { // It is possible to transiantly have an undef cleanuppad operand because we // have deleted some, but not all, dead blocks. // Eventually, this block will be deleted. @@ -4165,7 +4166,7 @@ bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) { return false; } -bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) { +bool SimplifyCFGOpt::simplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) { BasicBlock *BB = RI->getParent(); if (!BB->getFirstNonPHIOrDbg()->isTerminator()) return false; @@ -4219,7 +4220,7 @@ bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) { return false; } -bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) { +bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) { BasicBlock *BB = UI->getParent(); bool Changed = false; @@ -5690,7 +5691,7 @@ static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder, return true; } -bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) { +bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) { BasicBlock *BB = SI->getParent(); if (isValueEqualityComparison(SI)) { @@ -5741,7 +5742,7 @@ bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) { return false; } -bool SimplifyCFGOpt::SimplifyIndirectBr(IndirectBrInst *IBI) { +bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) { BasicBlock *BB = IBI->getParent(); bool Changed = false; @@ -5856,7 +5857,12 @@ static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI, return false; } -bool SimplifyCFGOpt::SimplifyUncondBranch(BranchInst *BI, +bool SimplifyCFGOpt::simplifyBranch(BranchInst *Branch, IRBuilder<> &Builder) { + return Branch->isUnconditional() ? simplifyUncondBranch(Branch, Builder) + : simplifyCondBranch(Branch, Builder); +} + +bool SimplifyCFGOpt::simplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder) { BasicBlock *BB = BI->getParent(); BasicBlock *Succ = BI->getSuccessor(0); @@ -5917,7 +5923,7 @@ static BasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) { return PredPred; } -bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) { +bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) { BasicBlock *BB = BI->getParent(); const Function *Fn = BB->getParent(); if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing)) @@ -6140,33 +6146,30 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) { if (PN->getNumIncomingValues() == 2) Changed |= FoldTwoEntryPHINode(PN, TTI, DL); - Builder.SetInsertPoint(BB->getTerminator()); - if (auto *BI = dyn_cast(BB->getTerminator())) { - if (BI->isUnconditional()) { - if (SimplifyUncondBranch(BI, Builder)) - return true; - } else { - if (SimplifyCondBranch(BI, Builder)) - return true; - } - } else if (auto *RI = dyn_cast(BB->getTerminator())) { - if (SimplifyReturn(RI, Builder)) - return true; - } else if (auto *RI = dyn_cast(BB->getTerminator())) { - if (SimplifyResume(RI, Builder)) - return true; - } else if (auto *RI = dyn_cast(BB->getTerminator())) { - if (SimplifyCleanupReturn(RI)) - return true; - } else if (auto *SI = dyn_cast(BB->getTerminator())) { - if (SimplifySwitch(SI, Builder)) - return true; - } else if (auto *UI = dyn_cast(BB->getTerminator())) { - if (SimplifyUnreachable(UI)) - return true; - } else if (auto *IBI = dyn_cast(BB->getTerminator())) { - if (SimplifyIndirectBr(IBI)) - return true; + Instruction *Terminator = BB->getTerminator(); + Builder.SetInsertPoint(Terminator); + switch (Terminator->getOpcode()) { + case Instruction::Br: + Changed |= simplifyBranch(cast(Terminator), Builder); + break; + case Instruction::Ret: + Changed |= simplifyReturn(cast(Terminator), Builder); + break; + case Instruction::Resume: + Changed |= simplifyResume(cast(Terminator), Builder); + break; + case Instruction::CleanupRet: + Changed |= simplifyCleanupReturn(cast(Terminator)); + break; + case Instruction::Switch: + Changed |= simplifySwitch(cast(Terminator), Builder); + break; + case Instruction::Unreachable: + Changed |= simplifyUnreachable(cast(Terminator)); + break; + case Instruction::IndirectBr: + Changed |= simplifyIndirectBr(cast(Terminator)); + break; } return Changed; -- 2.7.4