diff options
Diffstat (limited to 'llvm/lib/Transforms/Utils/LCSSA.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/LCSSA.cpp | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/llvm/lib/Transforms/Utils/LCSSA.cpp b/llvm/lib/Transforms/Utils/LCSSA.cpp index af79dc456ea6..c36b0533580b 100644 --- a/llvm/lib/Transforms/Utils/LCSSA.cpp +++ b/llvm/lib/Transforms/Utils/LCSSA.cpp @@ -40,7 +40,6 @@ #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" #include "llvm/IR/DebugInfo.h" #include "llvm/IR/Dominators.h" -#include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/PredIteratorCache.h" @@ -77,15 +76,14 @@ static bool isExitBlock(BasicBlock *BB, /// rewrite the uses. bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist, const DominatorTree &DT, const LoopInfo &LI, - ScalarEvolution *SE, IRBuilderBase &Builder, - SmallVectorImpl<PHINode *> *PHIsToRemove) { + ScalarEvolution *SE, + SmallVectorImpl<PHINode *> *PHIsToRemove, + SmallVectorImpl<PHINode *> *InsertedPHIs) { SmallVector<Use *, 16> UsesToRewrite; SmallSetVector<PHINode *, 16> LocalPHIsToRemove; PredIteratorCache PredCache; bool Changed = false; - IRBuilderBase::InsertPointGuard InsertPtGuard(Builder); - // Cache the Loop ExitBlocks across this loop. We expect to get a lot of // instructions within the same loops, computing the exit blocks is // expensive, and we're not mutating the loop structure. @@ -146,17 +144,14 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist, SmallVector<PHINode *, 16> AddedPHIs; SmallVector<PHINode *, 8> PostProcessPHIs; - SmallVector<PHINode *, 4> InsertedPHIs; - SSAUpdater SSAUpdate(&InsertedPHIs); + SmallVector<PHINode *, 4> LocalInsertedPHIs; + SSAUpdater SSAUpdate(&LocalInsertedPHIs); SSAUpdate.Initialize(I->getType(), I->getName()); - // Force re-computation of I, as some users now need to use the new PHI - // node. - if (SE) - SE->forgetValue(I); - // Insert the LCSSA phi's into all of the exit blocks dominated by the // value, and add them to the Phi's map. + bool HasSCEV = SE && SE->isSCEVable(I->getType()) && + SE->getExistingSCEV(I) != nullptr; for (BasicBlock *ExitBB : ExitBlocks) { if (!DT.dominates(DomNode, DT.getNode(ExitBB))) continue; @@ -164,9 +159,10 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist, // If we already inserted something for this BB, don't reprocess it. if (SSAUpdate.HasValueForBlock(ExitBB)) continue; - Builder.SetInsertPoint(&ExitBB->front()); - PHINode *PN = Builder.CreatePHI(I->getType(), PredCache.size(ExitBB), - I->getName() + ".lcssa"); + PHINode *PN = PHINode::Create(I->getType(), PredCache.size(ExitBB), + I->getName() + ".lcssa", &ExitBB->front()); + if (InsertedPHIs) + InsertedPHIs->push_back(PN); // Get the debug location from the original instruction. PN->setDebugLoc(I->getDebugLoc()); @@ -203,6 +199,13 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist, if (auto *OtherLoop = LI.getLoopFor(ExitBB)) if (!L->contains(OtherLoop)) PostProcessPHIs.push_back(PN); + + // If we have a cached SCEV for the original instruction, make sure the + // new LCSSA phi node is also cached. This makes sures that BECounts + // based on it will be invalidated when the LCSSA phi node is invalidated, + // which some passes rely on. + if (HasSCEV) + SE->getSCEV(PN); } // Rewrite all uses outside the loop in terms of the new PHIs we just @@ -256,10 +259,12 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist, // SSAUpdater might have inserted phi-nodes inside other loops. We'll need // to post-process them to keep LCSSA form. - for (PHINode *InsertedPN : InsertedPHIs) { + for (PHINode *InsertedPN : LocalInsertedPHIs) { if (auto *OtherLoop = LI.getLoopFor(InsertedPN->getParent())) if (!L->contains(OtherLoop)) PostProcessPHIs.push_back(InsertedPN); + if (InsertedPHIs) + InsertedPHIs->push_back(InsertedPN); } // Post process PHI instructions that were inserted into another disjoint @@ -392,14 +397,7 @@ bool llvm::formLCSSA(Loop &L, const DominatorTree &DT, const LoopInfo *LI, } } - IRBuilder<> Builder(L.getHeader()->getContext()); - Changed = formLCSSAForInstructions(Worklist, DT, *LI, SE, Builder); - - // If we modified the code, remove any caches about the loop from SCEV to - // avoid dangling entries. - // FIXME: This is a big hammer, can we clear the cache more selectively? - if (SE && Changed) - SE->forgetLoop(&L); + Changed = formLCSSAForInstructions(Worklist, DT, *LI, SE); assert(L.isLCSSAForm(DT)); |