aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2006-02-15 00:07:43 +0000
committerChris Lattner <sabre@nondot.org>2006-02-15 00:07:43 +0000
commitfed5d9dbd3e2b6c8d80949dd90360f7d35c889fc (patch)
tree6cf4e70a32c02994c55c24d1ce7e889c64d32e6e
parent7ce7bf2e62ab68a83949966b168b5e6cae9cb857 (diff)
downloadexternal_llvm-fed5d9dbd3e2b6c8d80949dd90360f7d35c889fc.zip
external_llvm-fed5d9dbd3e2b6c8d80949dd90360f7d35c889fc.tar.gz
external_llvm-fed5d9dbd3e2b6c8d80949dd90360f7d35c889fc.tar.bz2
pull some code out into a function
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26191 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Transforms/Scalar/LoopUnswitch.cpp46
1 files changed, 28 insertions, 18 deletions
diff --git a/lib/Transforms/Scalar/LoopUnswitch.cpp b/lib/Transforms/Scalar/LoopUnswitch.cpp
index f98e4d9..e4432b3 100644
--- a/lib/Transforms/Scalar/LoopUnswitch.cpp
+++ b/lib/Transforms/Scalar/LoopUnswitch.cpp
@@ -432,6 +432,28 @@ static Loop *CloneLoop(Loop *L, Loop *PL, std::map<const Value*, Value*> &VM,
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
/// 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
@@ -505,6 +527,7 @@ void LoopUnswitch::VersionLoop(Value *LIC, Constant *Val, Loop *L,
std::sort(ExitBlocks.begin(), ExitBlocks.end());
ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()),
ExitBlocks.end());
+
// Split all of the edges from inside the loop to their exit blocks. This
// unswitching trivial: no phi nodes to update.
unsigned NumBlocks = L->getBlocks().size();
@@ -583,26 +606,13 @@ void LoopUnswitch::VersionLoop(Value *LIC, Constant *Val, Loop *L,
RemapInstruction(I, ValueMap);
// Rewrite the original preheader to select between versions of the loop.
- assert(isa<BranchInst>(OrigPreheader->getTerminator()) &&
- cast<BranchInst>(OrigPreheader->getTerminator())->isUnconditional() &&
- OrigPreheader->getTerminator()->getSuccessor(0) == LoopBlocks[0] &&
+ BranchInst *OldBR = cast<BranchInst>(OrigPreheader->getTerminator());
+ assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] &&
"Preheader splitting did not work correctly!");
- // 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",
- 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);
+ // Emit the new branch that selects between the two versions of this loop.
+ EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR);
+ OldBR->eraseFromParent();
// Now we rewrite the original code to know that the condition is true and the
// new code to know that the condition is false.