From cd0a5bb96c10e5febef08c6e4618a99fdd3fbb99 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Tue, 31 Oct 2017 17:06:32 +0000 Subject: [IndVarSimplify] Simplify code using a dictionary Possibly very slightly slower, but this code is not performance critical and the readability benefit alone is huge. llvm-svn: 317012 --- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index bceb02a3c21..fce7f8b81ba 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -193,27 +193,19 @@ bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp, // cheaply, where cheaply means "we don't need to emit any new // instructions". - Value *NewLHS = nullptr, *NewRHS = nullptr; - - if (S == InvariantLHS || X == InvariantLHS) - NewLHS = - ICmp->getOperand(S == InvariantLHS ? IVOperIdx : (1 - IVOperIdx)); - - if (S == InvariantRHS || X == InvariantRHS) - NewRHS = - ICmp->getOperand(S == InvariantRHS ? IVOperIdx : (1 - IVOperIdx)); - + SmallDenseMap CheapExpansions; + CheapExpansions[S] = ICmp->getOperand(IVOperIdx); + CheapExpansions[X] = ICmp->getOperand(1 - IVOperIdx); + // TODO: Support multiple entry loops? (We currently bail out of these in // the IndVarSimplify pass) if (auto *BB = L->getLoopPredecessor()) { - Value *Incoming = PN->getIncomingValue(PN->getBasicBlockIndex(BB)); + Value *Incoming = PN->getIncomingValueForBlock(BB); const SCEV *IncomingS = SE->getSCEV(Incoming); - - if (!NewLHS && IncomingS == InvariantLHS) - NewLHS = Incoming; - if (!NewRHS && IncomingS == InvariantRHS) - NewRHS = Incoming; + CheapExpansions[IncomingS] = Incoming; } + Value *NewLHS = CheapExpansions[InvariantLHS]; + Value *NewRHS = CheapExpansions[InvariantRHS]; if (!NewLHS || !NewRHS) // We could not find an existing value to replace either LHS or RHS. -- cgit v1.2.3