Use IRBuilder while simplifying branch.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@131598 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Devang Patel
2011-05-18 23:18:47 +00:00
parent 22486c9aba
commit 02dd5418d2

View File

@@ -2023,7 +2023,8 @@ static bool TryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
/// SimplifyBranchOnICmpChain - The specified branch is a conditional branch. /// SimplifyBranchOnICmpChain - The specified branch is a conditional branch.
/// Check to see if it is branching on an or/and chain of icmp instructions, and /// Check to see if it is branching on an or/and chain of icmp instructions, and
/// fold it into a switch instruction if so. /// fold it into a switch instruction if so.
static bool SimplifyBranchOnICmpChain(BranchInst *BI, const TargetData *TD) { static bool SimplifyBranchOnICmpChain(BranchInst *BI, const TargetData *TD,
IRBuilder<> &Builder) {
Instruction *Cond = dyn_cast<Instruction>(BI->getCondition()); Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
if (Cond == 0) return false; if (Cond == 0) return false;
@@ -2079,11 +2080,12 @@ static bool SimplifyBranchOnICmpChain(BranchInst *BI, const TargetData *TD) {
BasicBlock *NewBB = BB->splitBasicBlock(BI, "switch.early.test"); BasicBlock *NewBB = BB->splitBasicBlock(BI, "switch.early.test");
// Remove the uncond branch added to the old block. // Remove the uncond branch added to the old block.
TerminatorInst *OldTI = BB->getTerminator(); TerminatorInst *OldTI = BB->getTerminator();
Builder.SetInsertPoint(OldTI);
if (TrueWhenEqual) if (TrueWhenEqual)
BranchInst::Create(EdgeBB, NewBB, ExtraCase, OldTI); Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
else else
BranchInst::Create(NewBB, EdgeBB, ExtraCase, OldTI); Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
OldTI->eraseFromParent(); OldTI->eraseFromParent();
@@ -2096,18 +2098,17 @@ static bool SimplifyBranchOnICmpChain(BranchInst *BI, const TargetData *TD) {
BB = NewBB; BB = NewBB;
} }
Builder.SetInsertPoint(BI);
// Convert pointer to int before we switch. // Convert pointer to int before we switch.
if (CompVal->getType()->isPointerTy()) { if (CompVal->getType()->isPointerTy()) {
assert(TD && "Cannot switch on pointer without TargetData"); assert(TD && "Cannot switch on pointer without TargetData");
CompVal = new PtrToIntInst(CompVal, CompVal = Builder.CreatePtrToInt(CompVal,
TD->getIntPtrType(CompVal->getContext()), TD->getIntPtrType(CompVal->getContext()),
"magicptr", BI); "magicptr");
cast<PtrToIntInst>(CompVal)->setDebugLoc(BI->getDebugLoc());
} }
// Create the new switch instruction now. // Create the new switch instruction now.
SwitchInst *New = SwitchInst::Create(CompVal, DefaultBB, Values.size(), BI); SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size());
New->setDebugLoc(BI->getDebugLoc());
// Add all of the 'cases' to the switch instruction. // Add all of the 'cases' to the switch instruction.
for (unsigned i = 0, e = Values.size(); i != e; ++i) for (unsigned i = 0, e = Values.size(); i != e; ++i)
@@ -2574,7 +2575,7 @@ bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
} }
// Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction. // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
if (SimplifyBranchOnICmpChain(BI, TD)) if (SimplifyBranchOnICmpChain(BI, TD, Builder))
return true; return true;
// We have a conditional branch to two blocks that are only reachable // We have a conditional branch to two blocks that are only reachable