pull some code out into a function

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26191 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2006-02-15 00:07:43 +00:00
parent 7ce7bf2e62
commit fed5d9dbd3

View File

@ -432,6 +432,28 @@ static Loop *CloneLoop(Loop *L, Loop *PL, std::map<const Value*, Value*> &VM,
return New; return New;
} }
/// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values
/// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest. Insert the
/// code immediately before InsertPt.
static void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
BasicBlock *TrueDest,
BasicBlock *FalseDest,
Instruction *InsertPt) {
// Insert a conditional branch on LIC to the two preheaders. The original
// code is the true version and the new code is the false version.
Value *BranchVal = LIC;
if (!isa<ConstantBool>(BranchVal)) {
BranchVal = BinaryOperator::createSetEQ(LIC, Val, "tmp", InsertPt);
} else if (Val != ConstantBool::True) {
// We want to enter the new loop when the condition is true.
std::swap(TrueDest, FalseDest);
}
// Insert the new branch.
new BranchInst(TrueDest, FalseDest, BranchVal, InsertPt);
}
/// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable
/// condition in it (a cond branch from its header block to its latch block, /// condition in it (a cond branch from its header block to its latch block,
/// where the path through the loop that doesn't execute its body has no /// where the path through the loop that doesn't execute its body has no
@ -505,6 +527,7 @@ void LoopUnswitch::VersionLoop(Value *LIC, Constant *Val, Loop *L,
std::sort(ExitBlocks.begin(), ExitBlocks.end()); std::sort(ExitBlocks.begin(), ExitBlocks.end());
ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()), ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()),
ExitBlocks.end()); ExitBlocks.end());
// Split all of the edges from inside the loop to their exit blocks. This // Split all of the edges from inside the loop to their exit blocks. This
// unswitching trivial: no phi nodes to update. // unswitching trivial: no phi nodes to update.
unsigned NumBlocks = L->getBlocks().size(); unsigned NumBlocks = L->getBlocks().size();
@ -583,26 +606,13 @@ void LoopUnswitch::VersionLoop(Value *LIC, Constant *Val, Loop *L,
RemapInstruction(I, ValueMap); RemapInstruction(I, ValueMap);
// Rewrite the original preheader to select between versions of the loop. // Rewrite the original preheader to select between versions of the loop.
assert(isa<BranchInst>(OrigPreheader->getTerminator()) && BranchInst *OldBR = cast<BranchInst>(OrigPreheader->getTerminator());
cast<BranchInst>(OrigPreheader->getTerminator())->isUnconditional() && assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] &&
OrigPreheader->getTerminator()->getSuccessor(0) == LoopBlocks[0] &&
"Preheader splitting did not work correctly!"); "Preheader splitting did not work correctly!");
// Insert a conditional branch on LIC to the two preheaders. The original // Emit the new branch that selects between the two versions of this loop.
// code is the true version and the new code is the false version. EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR);
Value *BranchVal = LIC; OldBR->eraseFromParent();
if (!isa<ConstantBool>(BranchVal)) {
BranchVal = BinaryOperator::createSetEQ(LIC, Val, "tmp",
OrigPreheader->getTerminator());
} else if (Val != ConstantBool::True) {
// We want to enter the new loop when the condition is true.
BranchVal = BinaryOperator::createNot(BranchVal, "tmp",
OrigPreheader->getTerminator());
}
// Remove the unconditional branch to LoopBlocks[0] and insert the new branch.
OrigPreheader->getInstList().pop_back();
new BranchInst(NewBlocks[0], LoopBlocks[0], BranchVal, OrigPreheader);
// Now we rewrite the original code to know that the condition is true and the // Now we rewrite the original code to know that the condition is true and the
// new code to know that the condition is false. // new code to know that the condition is false.