[SimplifyCFG] convert if-else chain to switch; NFC

Fix formatting of related function names while changing the code.
This commit is contained in:
Sanjay Patel 2020-03-13 10:27:02 -04:00
parent 86eb2c3991
commit cbeffa3f6c
1 changed files with 52 additions and 49 deletions

View File

@ -192,16 +192,17 @@ class SimplifyCFGOpt {
bool FoldValueComparisonIntoPredecessors(Instruction *TI, bool FoldValueComparisonIntoPredecessors(Instruction *TI,
IRBuilder<> &Builder); IRBuilder<> &Builder);
bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder); bool simplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder); bool simplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
bool SimplifySingleResume(ResumeInst *RI); bool simplifySingleResume(ResumeInst *RI);
bool SimplifyCommonResume(ResumeInst *RI); bool simplifyCommonResume(ResumeInst *RI);
bool SimplifyCleanupReturn(CleanupReturnInst *RI); bool simplifyCleanupReturn(CleanupReturnInst *RI);
bool SimplifyUnreachable(UnreachableInst *UI); bool simplifyUnreachable(UnreachableInst *UI);
bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder); bool simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
bool SimplifyIndirectBr(IndirectBrInst *IBI); bool simplifyIndirectBr(IndirectBrInst *IBI);
bool SimplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder); bool simplifyBranch(BranchInst *Branch, IRBuilder<> &Builder);
bool SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder); bool simplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder);
bool simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder);
bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI, bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
IRBuilder<> &Builder); IRBuilder<> &Builder);
@ -3863,19 +3864,19 @@ static bool SimplifyBranchOnICmpChain(BranchInst *BI, IRBuilder<> &Builder,
return true; return true;
} }
bool SimplifyCFGOpt::SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder) { bool SimplifyCFGOpt::simplifyResume(ResumeInst *RI, IRBuilder<> &Builder) {
if (isa<PHINode>(RI->getValue())) if (isa<PHINode>(RI->getValue()))
return SimplifyCommonResume(RI); return simplifyCommonResume(RI);
else if (isa<LandingPadInst>(RI->getParent()->getFirstNonPHI()) && else if (isa<LandingPadInst>(RI->getParent()->getFirstNonPHI()) &&
RI->getValue() == RI->getParent()->getFirstNonPHI()) RI->getValue() == RI->getParent()->getFirstNonPHI())
// The resume must unwind the exception that caused control to branch here. // The resume must unwind the exception that caused control to branch here.
return SimplifySingleResume(RI); return simplifySingleResume(RI);
return false; return false;
} }
// Simplify resume that is shared by several landing pads (phi of landing pad). // 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(); BasicBlock *BB = RI->getParent();
// Check that there are no other instructions except for debug intrinsics // 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. // 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(); BasicBlock *BB = RI->getParent();
auto *LPInst = cast<LandingPadInst>(BB->getFirstNonPHI()); auto *LPInst = cast<LandingPadInst>(BB->getFirstNonPHI());
assert(RI->getValue() == LPInst && assert(RI->getValue() == LPInst &&
@ -4149,7 +4150,7 @@ static bool mergeCleanupPad(CleanupReturnInst *RI) {
return true; return true;
} }
bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) { bool SimplifyCFGOpt::simplifyCleanupReturn(CleanupReturnInst *RI) {
// It is possible to transiantly have an undef cleanuppad operand because we // It is possible to transiantly have an undef cleanuppad operand because we
// have deleted some, but not all, dead blocks. // have deleted some, but not all, dead blocks.
// Eventually, this block will be deleted. // Eventually, this block will be deleted.
@ -4165,7 +4166,7 @@ bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) {
return false; return false;
} }
bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) { bool SimplifyCFGOpt::simplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) {
BasicBlock *BB = RI->getParent(); BasicBlock *BB = RI->getParent();
if (!BB->getFirstNonPHIOrDbg()->isTerminator()) if (!BB->getFirstNonPHIOrDbg()->isTerminator())
return false; return false;
@ -4219,7 +4220,7 @@ bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) {
return false; return false;
} }
bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) { bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
BasicBlock *BB = UI->getParent(); BasicBlock *BB = UI->getParent();
bool Changed = false; bool Changed = false;
@ -5690,7 +5691,7 @@ static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder,
return true; return true;
} }
bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) { bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
BasicBlock *BB = SI->getParent(); BasicBlock *BB = SI->getParent();
if (isValueEqualityComparison(SI)) { if (isValueEqualityComparison(SI)) {
@ -5741,7 +5742,7 @@ bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
return false; return false;
} }
bool SimplifyCFGOpt::SimplifyIndirectBr(IndirectBrInst *IBI) { bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
BasicBlock *BB = IBI->getParent(); BasicBlock *BB = IBI->getParent();
bool Changed = false; bool Changed = false;
@ -5856,7 +5857,12 @@ static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI,
return false; 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) { IRBuilder<> &Builder) {
BasicBlock *BB = BI->getParent(); BasicBlock *BB = BI->getParent();
BasicBlock *Succ = BI->getSuccessor(0); BasicBlock *Succ = BI->getSuccessor(0);
@ -5917,7 +5923,7 @@ static BasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) {
return PredPred; return PredPred;
} }
bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) { bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
BasicBlock *BB = BI->getParent(); BasicBlock *BB = BI->getParent();
const Function *Fn = BB->getParent(); const Function *Fn = BB->getParent();
if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing)) if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing))
@ -6140,33 +6146,30 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
if (PN->getNumIncomingValues() == 2) if (PN->getNumIncomingValues() == 2)
Changed |= FoldTwoEntryPHINode(PN, TTI, DL); Changed |= FoldTwoEntryPHINode(PN, TTI, DL);
Builder.SetInsertPoint(BB->getTerminator()); Instruction *Terminator = BB->getTerminator();
if (auto *BI = dyn_cast<BranchInst>(BB->getTerminator())) { Builder.SetInsertPoint(Terminator);
if (BI->isUnconditional()) { switch (Terminator->getOpcode()) {
if (SimplifyUncondBranch(BI, Builder)) case Instruction::Br:
return true; Changed |= simplifyBranch(cast<BranchInst>(Terminator), Builder);
} else { break;
if (SimplifyCondBranch(BI, Builder)) case Instruction::Ret:
return true; Changed |= simplifyReturn(cast<ReturnInst>(Terminator), Builder);
} break;
} else if (auto *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { case Instruction::Resume:
if (SimplifyReturn(RI, Builder)) Changed |= simplifyResume(cast<ResumeInst>(Terminator), Builder);
return true; break;
} else if (auto *RI = dyn_cast<ResumeInst>(BB->getTerminator())) { case Instruction::CleanupRet:
if (SimplifyResume(RI, Builder)) Changed |= simplifyCleanupReturn(cast<CleanupReturnInst>(Terminator));
return true; break;
} else if (auto *RI = dyn_cast<CleanupReturnInst>(BB->getTerminator())) { case Instruction::Switch:
if (SimplifyCleanupReturn(RI)) Changed |= simplifySwitch(cast<SwitchInst>(Terminator), Builder);
return true; break;
} else if (auto *SI = dyn_cast<SwitchInst>(BB->getTerminator())) { case Instruction::Unreachable:
if (SimplifySwitch(SI, Builder)) Changed |= simplifyUnreachable(cast<UnreachableInst>(Terminator));
return true; break;
} else if (auto *UI = dyn_cast<UnreachableInst>(BB->getTerminator())) { case Instruction::IndirectBr:
if (SimplifyUnreachable(UI)) Changed |= simplifyIndirectBr(cast<IndirectBrInst>(Terminator));
return true; break;
} else if (auto *IBI = dyn_cast<IndirectBrInst>(BB->getTerminator())) {
if (SimplifyIndirectBr(IBI))
return true;
} }
return Changed; return Changed;