diff options
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r-- | llvm/lib/Transforms/Scalar/JumpThreading.cpp | 56 | ||||
-rw-r--r-- | llvm/lib/Transforms/Utils/SSAUpdaterBulk.cpp | 25 |
2 files changed, 42 insertions, 39 deletions
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp index 29c707799bd..796b6abcbfd 100644 --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -66,6 +66,7 @@ #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/SSAUpdater.h" +#include "llvm/Transforms/Utils/SSAUpdaterBulk.h" #include "llvm/Transforms/Utils/ValueMapper.h" #include <algorithm> #include <cassert> @@ -1985,19 +1986,33 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB, // PHI nodes for NewBB now. AddPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping); + // Update the terminator of PredBB to jump to NewBB instead of BB. This + // eliminates predecessors from BB, which requires us to simplify any PHI + // nodes in BB. + TerminatorInst *PredTerm = PredBB->getTerminator(); + for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) + if (PredTerm->getSuccessor(i) == BB) { + BB->removePredecessor(PredBB, true); + PredTerm->setSuccessor(i, NewBB); + } + // If there were values defined in BB that are used outside the block, then we // now have to update all uses of the value to use either the original value, // the cloned value, or some PHI derived value. This can require arbitrary // PHI insertion, of which we are prepared to do, clean these up now. - SSAUpdater SSAUpdate; - SmallVector<Use*, 16> UsesToRename; + SSAUpdaterBulk SSAUpdate; + for (Instruction &I : *BB) { + SmallVector<Use*, 16> UsesToRename; + // Scan all uses of this instruction to see if it is used outside of its - // block, and if so, record them in UsesToRename. + // block, and if so, record them in UsesToRename. Also, skip phi operands + // from PredBB - we'll remove them anyway. for (Use &U : I.uses()) { Instruction *User = cast<Instruction>(U.getUser()); if (PHINode *UserPN = dyn_cast<PHINode>(User)) { - if (UserPN->getIncomingBlock(U) == BB) + if (UserPN->getIncomingBlock(U) == BB || + UserPN->getIncomingBlock(U) == PredBB) continue; } else if (User->getParent() == BB) continue; @@ -2008,35 +2023,24 @@ bool JumpThreadingPass::ThreadEdge(BasicBlock *BB, // If there are no uses outside the block, we're done with this instruction. if (UsesToRename.empty()) continue; + unsigned VarNum = SSAUpdate.AddVariable(I.getName(), I.getType()); - DEBUG(dbgs() << "JT: Renaming non-local uses of: " << I << "\n"); - - // We found a use of I outside of BB. Rename all uses of I that are outside - // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks - // with the two values we know. - SSAUpdate.Initialize(I.getType(), I.getName()); - SSAUpdate.AddAvailableValue(BB, &I); - SSAUpdate.AddAvailableValue(NewBB, ValueMapping[&I]); - - while (!UsesToRename.empty()) - SSAUpdate.RewriteUse(*UsesToRename.pop_back_val()); - DEBUG(dbgs() << "\n"); + // We found a use of I outside of BB - we need to rename all uses of I that + // are outside its block to be uses of the appropriate PHI node etc. + SSAUpdate.AddAvailableValue(VarNum, BB, &I); + SSAUpdate.AddAvailableValue(VarNum, NewBB, ValueMapping[&I]); + for (auto *U : UsesToRename) + SSAUpdate.AddUse(VarNum, U); } - // Ok, NewBB is good to go. Update the terminator of PredBB to jump to - // NewBB instead of BB. This eliminates predecessors from BB, which requires - // us to simplify any PHI nodes in BB. - TerminatorInst *PredTerm = PredBB->getTerminator(); - for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) - if (PredTerm->getSuccessor(i) == BB) { - BB->removePredecessor(PredBB, true); - PredTerm->setSuccessor(i, NewBB); - } - DDT->applyUpdates({{DominatorTree::Insert, NewBB, SuccBB}, {DominatorTree::Insert, PredBB, NewBB}, {DominatorTree::Delete, PredBB, BB}}); + // Apply all updates we queued with DDT and get the updated Dominator Tree. + DominatorTree *DT = &DDT->flush(); + SSAUpdate.RewriteAllUses(DT); + // At this point, the IR is fully up to date and consistent. Do a quick scan // over the new instructions and zap any that are constants or dead. This // frequently happens because of phi translation. diff --git a/llvm/lib/Transforms/Utils/SSAUpdaterBulk.cpp b/llvm/lib/Transforms/Utils/SSAUpdaterBulk.cpp index dbc671cc16f..61ceb9c9277 100644 --- a/llvm/lib/Transforms/Utils/SSAUpdaterBulk.cpp +++ b/llvm/lib/Transforms/Utils/SSAUpdaterBulk.cpp @@ -38,18 +38,19 @@ static BasicBlock *getUserBB(Use *U) { /// Add a new variable to the SSA rewriter. This needs to be called before /// AddAvailableValue or AddUse calls. -void SSAUpdaterBulk::AddVariable(unsigned Var, StringRef Name, Type *Ty) { - assert(Rewrites.find(Var) == Rewrites.end() && "Variable added twice!"); +unsigned SSAUpdaterBulk::AddVariable(StringRef Name, Type *Ty) { + unsigned Var = Rewrites.size(); DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": initialized with Ty = " << *Ty << ", Name = " << Name << "\n"); RewriteInfo RI(Name, Ty); - Rewrites[Var] = RI; + Rewrites.push_back(RI); + return Var; } /// Indicate that a rewritten value is available in the specified block with the /// specified value. void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) { - assert(Rewrites.find(Var) != Rewrites.end() && "Should add variable first!"); + assert(Var < Rewrites.size() && "Variable not found!"); DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added new available value" << *V << " in " << BB->getName() << "\n"); Rewrites[Var].Defines[BB] = V; @@ -58,7 +59,7 @@ void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) { /// Record a use of the symbolic value. This use will be updated with a /// rewritten value when RewriteAllUses is called. void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) { - assert(Rewrites.find(Var) != Rewrites.end() && "Should add variable first!"); + assert(Var < Rewrites.size() && "Variable not found!"); DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added a use" << *U->get() << " in " << getUserBB(U)->getName() << "\n"); Rewrites[Var].Uses.push_back(U); @@ -67,7 +68,7 @@ void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) { /// Return true if the SSAUpdater already has a value for the specified variable /// in the specified block. bool SSAUpdaterBulk::HasValueForBlock(unsigned Var, BasicBlock *BB) { - return Rewrites.count(Var) ? Rewrites[Var].Defines.count(BB) : false; + return (Var < Rewrites.size()) ? Rewrites[Var].Defines.count(BB) : false; } // Compute value at the given block BB. We either should already know it, or we @@ -126,16 +127,14 @@ ComputeLiveInBlocks(const SmallPtrSetImpl<BasicBlock *> &UsingBlocks, /// requested uses update. void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT, SmallVectorImpl<PHINode *> *InsertedPHIs) { - for (auto &P : Rewrites) { + for (auto &R : Rewrites) { // Compute locations for new phi-nodes. // For that we need to initialize DefBlocks from definitions in R.Defines, // UsingBlocks from uses in R.Uses, then compute LiveInBlocks, and then use // this set for computing iterated dominance frontier (IDF). // The IDF blocks are the blocks where we need to insert new phi-nodes. ForwardIDFCalculator IDF(*DT); - RewriteInfo &R = P.second; - DEBUG(dbgs() << "SSAUpdater: Var=" << P.first << ": rewriting " - << R.Uses.size() << " use(s)\n"); + DEBUG(dbgs() << "SSAUpdater: rewriting " << R.Uses.size() << " use(s)\n"); SmallPtrSet<BasicBlock *, 2> DefBlocks; for (auto &Def : R.Defines) @@ -165,7 +164,7 @@ void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT, } // Fill in arguments of the inserted PHIs. - for (auto PN : InsertedPHIsForVar) { + for (auto *PN : InsertedPHIsForVar) { BasicBlock *PBB = PN->getParent(); for (BasicBlock *Pred : PredCache.get(PBB)) PN->addIncoming(computeValueAt(Pred, R, DT), Pred); @@ -182,8 +181,8 @@ void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT, // Notify that users of the existing value that it is being replaced. if (OldVal != V && OldVal->hasValueHandle()) ValueHandleBase::ValueIsRAUWd(OldVal, V); - DEBUG(dbgs() << "SSAUpdater: Var=" << P.first << ": replacing" << *OldVal - << " with " << *V << "\n"); + DEBUG(dbgs() << "SSAUpdater: replacing " << *OldVal << " with " << *V + << "\n"); U->set(V); } } |