From 8f40afe62db871230ac149de1c4ccc16ff48253a Mon Sep 17 00:00:00 2001 From: Evan Cheng Date: Sun, 15 Feb 2009 06:06:15 +0000 Subject: Fix pr3571: If stride is a value defined by an instruction, make sure it dominates the loop preheader. When IV users are strength reduced, the stride is inserted into the preheader. It could create a use before def situation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@64579 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/LoopStrengthReduce.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'lib/Transforms/Scalar/LoopStrengthReduce.cpp') diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp index 9aa5fe27da..055114c215 100644 --- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -400,7 +400,7 @@ static bool containsAddRecFromDifferentLoop(SCEVHandle S, Loop *L) { /// outer loop of the current loop. static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L, SCEVHandle &Start, SCEVHandle &Stride, - ScalarEvolution *SE) { + ScalarEvolution *SE, DominatorTree *DT) { SCEVHandle TheAddRec = Start; // Initialize to zero. // If the outer level is an AddExpr, the operands are all start values except @@ -437,11 +437,21 @@ static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L, Start = SE->getAddExpr(Start, AddRec->getOperand(0)); -#ifndef NDEBUG - if (!isa(AddRec->getOperand(1))) + if (!isa(AddRec->getOperand(1))) { + // If stride is an instruction, make sure it dominates the loop header. + // Otherwise we could end up with a use before def situation. + if (SCEVUnknown *SU = dyn_cast(AddRec->getOperand(1))) { + if (Instruction *I = dyn_cast(SU->getValue())) { + BasicBlock *StrideBB = I->getParent(); + BasicBlock *Preheader = L->getLoopPreheader(); + if (!DT->dominates(StrideBB, Preheader)) + return false; + } + } + DOUT << "[" << L->getHeader()->getName() << "] Variable stride: " << *AddRec << "\n"; -#endif + } Stride = AddRec->getOperand(1); return true; @@ -549,7 +559,7 @@ bool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L, // Get the start and stride for this expression. SCEVHandle Start = SE->getIntegerSCEV(0, ISE->getType()); SCEVHandle Stride = Start; - if (!getSCEVStartAndStride(ISE, L, Start, Stride, SE)) + if (!getSCEVStartAndStride(ISE, L, Start, Stride, SE, DT)) return false; // Non-reducible symbolic expression, bail out. std::vector IUsers; -- cgit v1.2.3-70-g09d2