summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp
diff options
context:
space:
mode:
authorDevang Patel <dpatel@apple.com>2009-03-30 22:24:10 +0000
committerDevang Patel <dpatel@apple.com>2009-03-30 22:24:10 +0000
commit6e68bd007a574375790a7d98f5859bd0ffee0d83 (patch)
tree2e295a57c78eee81bd9fef7446f86a708f410baf /llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp
parent7857bd2743096639529c4a6144135e5dd18a8a8c (diff)
downloadbcm5719-llvm-6e68bd007a574375790a7d98f5859bd0ffee0d83.tar.gz
bcm5719-llvm-6e68bd007a574375790a7d98f5859bd0ffee0d83.zip
Loop Index Split can eliminate a loop if it can determin if loop body is executed only once. There was a bug in determining IV based value of the iteration for which the loop body is executed. Fix it.
llvm-svn: 68071
Diffstat (limited to 'llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp')
-rw-r--r--llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp32
1 files changed, 24 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp b/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp
index 98e5cc15d6c..a8cfaaf3b84 100644
--- a/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopIndexSplit.cpp
@@ -48,6 +48,7 @@
#include "llvm/Analysis/Dominators.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Support/Compiler.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/Statistic.h"
@@ -345,10 +346,25 @@ bool LoopIndexSplit::processOneIterationLoop() {
if (!L->isLoopInvariant(SplitValue))
return false;
Instruction *OPI = dyn_cast<Instruction>(OPV);
- if (!OPI) return false;
+ if (!OPI)
+ return false;
if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L))
return false;
-
+ Value *StartValue = IVStartValue;
+ Value *ExitValue = IVExitValue;;
+
+ if (OPV != IndVar) {
+ // If BR operand is IV based then use this operand to calculate
+ // effective conditions for loop body.
+ BinaryOperator *BOPV = dyn_cast<BinaryOperator>(OPV);
+ if (!BOPV)
+ return false;
+ if (BOPV->getOpcode() != Instruction::Add)
+ return false;
+ StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR);
+ ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR);
+ }
+
if (!cleanBlock(Header))
return false;
@@ -399,13 +415,13 @@ bool LoopIndexSplit::processOneIterationLoop() {
// and i32 c1, c2
Instruction *C1 = new ICmpInst(ExitCondition->isSignedPredicate() ?
ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
- SplitValue, IVStartValue, "lisplit", BR);
+ SplitValue, StartValue, "lisplit", BR);
CmpInst::Predicate C2P = ExitCondition->getPredicate();
BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
if (LatchBR->getOperand(0) != Header)
C2P = CmpInst::getInversePredicate(C2P);
- Instruction *C2 = new ICmpInst(C2P, SplitValue, IVExitValue, "lisplit", BR);
+ Instruction *C2 = new ICmpInst(C2P, SplitValue, ExitValue, "lisplit", BR);
Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR);
SplitCondition->replaceAllUsesWith(NSplitCond);
@@ -419,11 +435,11 @@ bool LoopIndexSplit::processOneIterationLoop() {
if (Header != *SI)
LatchSucc = *SI;
}
- LatchBR->setUnconditionalDest(LatchSucc);
- // Remove IVIncrement
- IVIncrement->replaceAllUsesWith(UndefValue::get(IVIncrement->getType()));
- IVIncrement->eraseFromParent();
+ // Clean up latch block.
+ Value *LatchBRCond = LatchBR->getCondition();
+ LatchBR->setUnconditionalDest(LatchSucc);
+ RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond);
LPM->deleteLoopFromQueue(L);
OpenPOWER on IntegriCloud