From c4f7e8061de19172d4c3f70a80447505b792e450 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 3 Apr 2010 06:05:10 +0000 Subject: [PATCH] There is no guarantee that the increment and the branch are in the same block. Insert the new increment in the correct location. Also, more cleanups. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100271 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/IndVarSimplify.cpp | 69 ++++++++++-------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index d98693a8857..41b216b9270 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -666,12 +666,12 @@ void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) { unsigned BackEdge = IncomingEdge^1; // Check incoming value. - ConstantFP *InitValue = + ConstantFP *InitValueVal = dyn_cast(PH->getIncomingValue(IncomingEdge)); - if (!InitValue) return; + if (!InitValueVal) return; - uint64_t newInitValue; - if (!convertToInt(InitValue->getValueAPF(), newInitValue)) + uint64_t InitValue; + if (!convertToInt(InitValueVal->getValueAPF(), InitValue)) return; // Check IV increment. Reject this PH if increment operation is not @@ -682,10 +682,10 @@ void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) { // If this is not an add of the PHI with a constantfp, or if the constant fp // is not an integer, bail out. - ConstantFP *IncrValue = dyn_cast(Incr->getOperand(1)); - uint64_t newIncrValue; - if (IncrValue == 0 || Incr->getOperand(0) != PH || - !convertToInt(IncrValue->getValueAPF(), newIncrValue)) + ConstantFP *IncValueVal = dyn_cast(Incr->getOperand(1)); + uint64_t IntValue; + if (IncValueVal == 0 || Incr->getOperand(0) != PH || + !convertToInt(IncValueVal->getValueAPF(), IntValue)) return; // Check Incr uses. One user is PH and the other user is an exit condition @@ -703,6 +703,8 @@ void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) { EC = dyn_cast(U2); if (EC == 0 || !EC->hasOneUse() || !isa(EC->use_back())) return; + + BranchInst *TheBr = cast(EC->use_back()); // If it isn't a comparison with an integer-as-fp (the exit value), we can't // transform it. @@ -714,62 +716,47 @@ void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) { // Find new predicate for integer comparison. CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; switch (EC->getPredicate()) { + default: return; // Unknown comparison. case CmpInst::FCMP_OEQ: - case CmpInst::FCMP_UEQ: - NewPred = CmpInst::ICMP_EQ; - break; + case CmpInst::FCMP_UEQ: NewPred = CmpInst::ICMP_EQ; break; case CmpInst::FCMP_OGT: - case CmpInst::FCMP_UGT: - NewPred = CmpInst::ICMP_UGT; - break; + case CmpInst::FCMP_UGT: NewPred = CmpInst::ICMP_UGT; break; case CmpInst::FCMP_OGE: - case CmpInst::FCMP_UGE: - NewPred = CmpInst::ICMP_UGE; - break; + case CmpInst::FCMP_UGE: NewPred = CmpInst::ICMP_UGE; break; case CmpInst::FCMP_OLT: - case CmpInst::FCMP_ULT: - NewPred = CmpInst::ICMP_ULT; - break; + case CmpInst::FCMP_ULT: NewPred = CmpInst::ICMP_ULT; break; case CmpInst::FCMP_OLE: - case CmpInst::FCMP_ULE: - NewPred = CmpInst::ICMP_ULE; - break; - default: - break; + case CmpInst::FCMP_ULE: NewPred = CmpInst::ICMP_ULE; break; } - if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return; + const IntegerType *Int32Ty = Type::getInt32Ty(PH->getContext()); + // Insert new integer induction variable. - PHINode *NewPHI = PHINode::Create(Type::getInt32Ty(PH->getContext()), - PH->getName()+".int", PH); - NewPHI->addIncoming(ConstantInt::get(Type::getInt32Ty(PH->getContext()), - newInitValue), + PHINode *NewPHI = PHINode::Create(Int32Ty, PH->getName()+".int", PH); + NewPHI->addIncoming(ConstantInt::get(Int32Ty, InitValue), PH->getIncomingBlock(IncomingEdge)); - Value *NewAdd = BinaryOperator::CreateAdd(NewPHI, - ConstantInt::get(Type::getInt32Ty(PH->getContext()), - newIncrValue), - Incr->getName()+".int", Incr); + Value *NewAdd = + BinaryOperator::CreateAdd(NewPHI, ConstantInt::get(Int32Ty, IntValue), + Incr->getName()+".int", Incr); NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge)); // The back edge is edge 1 of newPHI, whatever it may have been in the // original PHI. - ConstantInt *NewEV = ConstantInt::get(Type::getInt32Ty(PH->getContext()), - ExitValue); + ConstantInt *NewEV = ConstantInt::get(Int32Ty, ExitValue); // FIXME: This is probably wrong. Value *LHS = NewPHI->getIncomingValue(1); Value *RHS = NewEV; - ICmpInst *NewEC = new ICmpInst(EC->getParent()->getTerminator(), - NewPred, LHS, RHS, EC->getName()); + ICmpInst *NewCompare = new ICmpInst(TheBr, NewPred, LHS, RHS, EC->getName()); // In the following deletions, PH may become dead and may be deleted. // Use a WeakVH to observe whether this happens. WeakVH WeakPH = PH; // Delete old, floating point, exit comparison instruction. - NewEC->takeName(EC); - EC->replaceAllUsesWith(NewEC); + NewCompare->takeName(EC); + EC->replaceAllUsesWith(NewCompare); RecursivelyDeleteTriviallyDeadInstructions(EC); // Delete old, floating point, increment instruction. @@ -780,7 +767,7 @@ void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) { // Give SIToFPInst preference over UIToFPInst because it is faster on // platforms that are widely used. if (WeakPH && !PH->use_empty()) { - if (useSIToFPInst(InitValue, ExitValueVal, newInitValue, ExitValue)) { + if (useSIToFPInst(InitValueVal, ExitValueVal, InitValue, ExitValue)) { SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv", PH->getParent()->getFirstNonPHI()); PH->replaceAllUsesWith(Conv); -- 2.34.1