From: Owen Anderson Date: Sat, 7 Apr 2007 18:23:27 +0000 (+0000) Subject: Add DomSet back, and revert the changes to LoopSimplify. Apparently the X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e9ed4452bce4f5a7f8005d7ebd649a20c22ef268;p=oota-llvm.git Add DomSet back, and revert the changes to LoopSimplify. Apparently the ETForest updating mechanisms don't work as I thought they did. These changes will be reapplied once the issue is worked out. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35741 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Analysis/Dominators.h b/include/llvm/Analysis/Dominators.h index c359fb80fb1..1a6320fde34 100644 --- a/include/llvm/Analysis/Dominators.h +++ b/include/llvm/Analysis/Dominators.h @@ -10,7 +10,8 @@ // This file defines the following classes: // 1. ImmediateDominators: Calculates and holds a mapping between BasicBlocks // and their immediate dominator. -// 2. DominatorTree: Represent the ImmediateDominator as an explicit tree +// 2. DominatorSet: Calculates the [reverse] dominator set for a function +// 3. DominatorTree: Represent the ImmediateDominator as an explicit tree // structure. // 4. ETForest: Efficient data structure for dominance comparisons and // nearest-common-ancestor queries. @@ -174,6 +175,127 @@ private: void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo); }; + + +//===----------------------------------------------------------------------===// +/// DominatorSet - Maintain a set for every basic block in a +/// function, that represents the blocks that dominate the block. If the block +/// is unreachable in this function, the set will be empty. This cannot happen +/// for reachable code, because every block dominates at least itself. +/// +class DominatorSetBase : public DominatorBase { +public: + typedef std::set DomSetType; // Dom set for a bb + // Map of dom sets + typedef std::map DomSetMapType; +protected: + DomSetMapType Doms; +public: + DominatorSetBase(bool isPostDom) : DominatorBase(isPostDom) {} + + virtual void releaseMemory() { Doms.clear(); } + + // Accessor interface: + typedef DomSetMapType::const_iterator const_iterator; + typedef DomSetMapType::iterator iterator; + inline const_iterator begin() const { return Doms.begin(); } + inline iterator begin() { return Doms.begin(); } + inline const_iterator end() const { return Doms.end(); } + inline iterator end() { return Doms.end(); } + inline const_iterator find(BasicBlock* B) const { return Doms.find(B); } + inline iterator find(BasicBlock* B) { return Doms.find(B); } + + + /// getDominators - Return the set of basic blocks that dominate the specified + /// block. + /// + inline const DomSetType &getDominators(BasicBlock *BB) const { + const_iterator I = find(BB); + assert(I != end() && "BB not in function!"); + return I->second; + } + + /// isReachable - Return true if the specified basicblock is reachable. If + /// the block is reachable, we have dominator set information for it. + /// + bool isReachable(BasicBlock *BB) const { + return !getDominators(BB).empty(); + } + + /// dominates - Return true if A dominates B. + /// + inline bool dominates(BasicBlock *A, BasicBlock *B) const { + return getDominators(B).count(A) != 0; + } + + /// properlyDominates - Return true if A dominates B and A != B. + /// + bool properlyDominates(BasicBlock *A, BasicBlock *B) const { + return dominates(A, B) && A != B; + } + + /// print - Convert to human readable form + /// + virtual void print(std::ostream &OS, const Module* = 0) const; + void print(std::ostream *OS, const Module* M = 0) const { + if (OS) print(*OS, M); + } + + /// dominates - Return true if A dominates B. This performs the special + /// checks necessary if A and B are in the same basic block. + /// + bool dominates(Instruction *A, Instruction *B) const; + + //===--------------------------------------------------------------------===// + // API to update (Post)DominatorSet information based on modifications to + // the CFG... + + /// addBasicBlock - Call to update the dominator set with information about a + /// new block that was inserted into the function. + /// + void addBasicBlock(BasicBlock *BB, const DomSetType &Dominators) { + assert(find(BB) == end() && "Block already in DominatorSet!"); + Doms.insert(std::make_pair(BB, Dominators)); + } + + /// addDominator - If a new block is inserted into the CFG, then method may be + /// called to notify the blocks it dominates that it is in their set. + /// + void addDominator(BasicBlock *BB, BasicBlock *NewDominator) { + iterator I = find(BB); + assert(I != end() && "BB is not in DominatorSet!"); + I->second.insert(NewDominator); + } +}; + + +//===------------------------------------- +/// DominatorSet Class - Concrete subclass of DominatorSetBase that is used to +/// compute a normal dominator set. +/// +class DominatorSet : public DominatorSetBase { +public: + DominatorSet() : DominatorSetBase(false) {} + + virtual bool runOnFunction(Function &F); + + BasicBlock *getRoot() const { + assert(Roots.size() == 1 && "Should always have entry node!"); + return Roots[0]; + } + + /// getAnalysisUsage - This simply provides a dominator set + /// + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequired(); + AU.setPreservesAll(); + } + + // stub - dummy function, just ignore it + static int stub; +}; + + //===----------------------------------------------------------------------===// /// DominatorTree - Calculate the immediate dominator tree for a function. /// @@ -569,4 +691,7 @@ private: } // End llvm namespace +// Make sure that any clients of this file link in Dominators.cpp +FORCE_DEFINING_FILE_TO_BE_LINKED(DominatorSet) + #endif diff --git a/lib/Transforms/Utils/LoopSimplify.cpp b/lib/Transforms/Utils/LoopSimplify.cpp index 530e3b7a66a..044ca5b0fc3 100644 --- a/lib/Transforms/Utils/LoopSimplify.cpp +++ b/lib/Transforms/Utils/LoopSimplify.cpp @@ -64,10 +64,12 @@ namespace { virtual void getAnalysisUsage(AnalysisUsage &AU) const { // We need loop information to identify the loops... AU.addRequired(); + AU.addRequired(); AU.addRequired(); AU.addRequired(); AU.addPreserved(); + AU.addPreserved(); AU.addPreserved(); AU.addPreserved(); AU.addPreserved(); @@ -312,7 +314,7 @@ BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, // Can we eliminate this phi node now? if (Value *V = PN->hasConstantValue(true)) { if (!isa(V) || - getAnalysis().dominates(cast(V), PN)) { + getAnalysis().dominates(cast(V), PN)) { PN->replaceAllUsesWith(V); if (AA) AA->deleteValue(PN); BB->getInstList().erase(PN); @@ -540,9 +542,10 @@ Loop *LoopSimplify::SeparateNestedLoop(Loop *L) { // Determine which blocks should stay in L and which should be moved out to // the Outer loop now. + DominatorSet &DS = getAnalysis(); std::set BlocksInL; for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); PI!=E; ++PI) - if (EF->dominates(Header, *PI)) + if (DS.dominates(Header, *PI)) AddBlockAndPredsToSet(*PI, Header, BlocksInL); @@ -690,8 +693,33 @@ void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, ++succ_begin(NewBB) == succ_end(NewBB) && "NewBB should have a single successor!"); BasicBlock *NewBBSucc = *succ_begin(NewBB); - ETForest& ETF = getAnalysis(); - + DominatorSet &DS = getAnalysis(); + + // Update dominator information... The blocks that dominate NewBB are the + // intersection of the dominators of predecessors, plus the block itself. + // + DominatorSet::DomSetType NewBBDomSet = DS.getDominators(PredBlocks[0]); + { + unsigned i, e = PredBlocks.size(); + // It is possible for some preds to not be reachable, and thus have empty + // dominator sets (all blocks must dom themselves, so no domset would + // otherwise be empty). If we see any of these, don't intersect with them, + // as that would certainly leave the resultant set empty. + for (i = 1; NewBBDomSet.empty(); ++i) { + assert(i != e && "Didn't find reachable pred?"); + NewBBDomSet = DS.getDominators(PredBlocks[i]); + } + + // Intersect the rest of the non-empty sets. + for (; i != e; ++i) { + const DominatorSet::DomSetType &PredDS = DS.getDominators(PredBlocks[i]); + if (!PredDS.empty()) + set_intersect(NewBBDomSet, PredDS); + } + NewBBDomSet.insert(NewBB); // All blocks dominate themselves. + DS.addBasicBlock(NewBB, NewBBDomSet); + } + // The newly inserted basic block will dominate existing basic blocks iff the // PredBlocks dominate all of the non-pred blocks. If all predblocks dominate // the non-pred blocks, then they all must be the same block! @@ -700,14 +728,13 @@ void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, { BasicBlock *OnePred = PredBlocks[0]; unsigned i, e = PredBlocks.size(); - for (i = 1; !ETF.dominates(&OnePred->getParent()->getEntryBlock(), OnePred); ++i) { + for (i = 1; !DS.isReachable(OnePred); ++i) { assert(i != e && "Didn't find reachable pred?"); OnePred = PredBlocks[i]; } for (; i != e; ++i) - if (PredBlocks[i] != OnePred && - ETF.dominates(&PredBlocks[i]->getParent()->getEntryBlock(), OnePred)) { + if (PredBlocks[i] != OnePred && DS.isReachable(PredBlocks[i])) { NewBBDominatesNewBBSucc = false; break; } @@ -715,7 +742,7 @@ void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, if (NewBBDominatesNewBBSucc) for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc); PI != E; ++PI) - if (*PI != NewBB && !ETF.dominates(NewBBSucc, *PI)) { + if (*PI != NewBB && !DS.dominates(NewBBSucc, *PI)) { NewBBDominatesNewBBSucc = false; break; } @@ -728,31 +755,44 @@ void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, NewBBDominatesNewBBSucc = true; for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc); PI != E; ++PI) - if (*PI != NewBB && !ETF.dominates(NewBBSucc, *PI)) { + if (*PI != NewBB && !DS.dominates(NewBBSucc, *PI)) { NewBBDominatesNewBBSucc = false; break; } } - BasicBlock *NewBBIDom = 0; - + // If NewBB dominates some blocks, then it will dominate all blocks that + // NewBBSucc does. + if (NewBBDominatesNewBBSucc) { + Function *F = NewBB->getParent(); + for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) + if (DS.dominates(NewBBSucc, I)) + DS.addDominator(I, NewBB); + } + // Update immediate dominator information if we have it. + BasicBlock *NewBBIDom = 0; if (ImmediateDominators *ID = getAnalysisToUpdate()) { - unsigned i = 0; - for (i = 0; i < PredBlocks.size(); ++i) - if (ETF.dominates(&PredBlocks[i]->getParent()->getEntryBlock(), PredBlocks[i])) { - NewBBIDom = PredBlocks[i]; - break; - } - assert(i != PredBlocks.size() && "No reachable preds?"); - for (i = i + 1; i < PredBlocks.size(); ++i) { - if (ETF.dominates(&PredBlocks[i]->getParent()->getEntryBlock(), PredBlocks[i])) - NewBBIDom = ETF.nearestCommonDominator(NewBBIDom, PredBlocks[i]); + // To find the immediate dominator of the new exit node, we trace up the + // immediate dominators of a predecessor until we find a basic block that + // dominates the exit block. + // + BasicBlock *Dom = PredBlocks[0]; // Some random predecessor. + + // Find a reachable pred. + for (unsigned i = 1; !DS.isReachable(Dom); ++i) { + assert(i != PredBlocks.size() && "Didn't find reachable pred!"); + Dom = PredBlocks[i]; } - assert(NewBBIDom && "No immediate dominator found??"); - + + while (!NewBBDomSet.count(Dom)) { // Loop until we find a dominator. + assert(Dom != 0 && "No shared dominator found???"); + Dom = ID->get(Dom); + } + // Set the immediate dominator now... - ID->addNewBlock(NewBB, NewBBIDom); + ID->addNewBlock(NewBB, Dom); + NewBBIDom = Dom; // Reuse this if calculating DominatorTree info... // If NewBB strictly dominates other blocks, we need to update their idom's // now. The only block that need adjustment is the NewBBSucc block, whose @@ -765,21 +805,24 @@ void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, if (DominatorTree *DT = getAnalysisToUpdate()) { // If we don't have ImmediateDominator info around, calculate the idom as // above. - if (!NewBBIDom) { - unsigned i = 0; - for (i = 0; i < PredBlocks.size(); ++i) - if (ETF.dominates(&PredBlocks[i]->getParent()->getEntryBlock(), PredBlocks[i])) { - NewBBIDom = PredBlocks[i]; - break; - } - assert(i != PredBlocks.size() && "No reachable preds?"); - for (i = i + 1; i < PredBlocks.size(); ++i) { - if (ETF.dominates(&PredBlocks[i]->getParent()->getEntryBlock(), PredBlocks[i])) - NewBBIDom = ETF.nearestCommonDominator(NewBBIDom, PredBlocks[i]); + DominatorTree::Node *NewBBIDomNode; + if (NewBBIDom) { + NewBBIDomNode = DT->getNode(NewBBIDom); + } else { + // Scan all the pred blocks that were pulled out. Any individual one may + // actually be unreachable, which would mean it doesn't have dom info. + NewBBIDomNode = 0; + for (unsigned i = 0; !NewBBIDomNode; ++i) { + assert(i != PredBlocks.size() && "No reachable preds?"); + NewBBIDomNode = DT->getNode(PredBlocks[i]); } - assert(NewBBIDom && "No immediate dominator found??"); + + while (!NewBBDomSet.count(NewBBIDomNode->getBlock())) { + NewBBIDomNode = NewBBIDomNode->getIDom(); + assert(NewBBIDomNode && "No shared dominator found??"); + } + NewBBIDom = NewBBIDomNode->getBlock(); } - DominatorTree::Node *NewBBIDomNode = DT->getNode(NewBBIDom); // Create the new dominator tree node... and set the idom of NewBB. DominatorTree::Node *NewBBNode = DT->createNewNode(NewBB, NewBBIDomNode); @@ -814,7 +857,7 @@ void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, bool DominatesPred = false; for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI); PI != E; ++PI) - if (ETF.dominates(NewBB, *PI)) + if (DS.dominates(NewBB, *PI)) DominatesPred = true; if (!DominatesPred) Set.erase(SetI++); @@ -842,14 +885,8 @@ void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, for (unsigned i = 0, e = PredBlocks.size(); i != e; ++i) { BasicBlock *Pred = PredBlocks[i]; // Get all of the dominators of the predecessor... - // FIXME: There's probably a better way to do this... - std::vector PredDoms; - for (Function::iterator I = Pred->getParent()->begin(), - E = Pred->getParent()->end(); I != E; ++I) - if (ETF.dominates(&(*I), Pred)) - PredDoms.push_back(I); - - for (std::vector::const_iterator PDI = PredDoms.begin(), + const DominatorSet::DomSetType &PredDoms = DS.getDominators(Pred); + for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(), PDE = PredDoms.end(); PDI != PDE; ++PDI) { BasicBlock *PredDom = *PDI; @@ -863,12 +900,12 @@ void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, // We remove it unless there is a predecessor of NewBBSucc that we // dominate, but we don't strictly dominate NewBBSucc. bool ShouldRemove = true; - if (PredDom == NewBBSucc || !ETF.dominates(PredDom, NewBBSucc)) { + if (PredDom == NewBBSucc || !DS.dominates(PredDom, NewBBSucc)) { // Okay, we know that PredDom does not strictly dominate NewBBSucc. // Check to see if it dominates any predecessors of NewBBSucc. for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc); PI != E; ++PI) - if (ETF.dominates(PredDom, *PI)) { + if (DS.dominates(PredDom, *PI)) { ShouldRemove = false; break; } diff --git a/lib/VMCore/Dominators.cpp b/lib/VMCore/Dominators.cpp index 4988049c8b8..9bd51bf4d91 100644 --- a/lib/VMCore/Dominators.cpp +++ b/lib/VMCore/Dominators.cpp @@ -251,6 +251,113 @@ void ImmediateDominatorsBase::print(std::ostream &o, const Module* ) const { o << "\n"; } + + +//===----------------------------------------------------------------------===// +// DominatorSet Implementation +//===----------------------------------------------------------------------===// + +static RegisterPass +B("domset", "Dominator Set Construction", true); + +// dominates - Return true if A dominates B. This performs the special checks +// necessary if A and B are in the same basic block. +// +bool DominatorSetBase::dominates(Instruction *A, Instruction *B) const { + BasicBlock *BBA = A->getParent(), *BBB = B->getParent(); + if (BBA != BBB) return dominates(BBA, BBB); + + // It is not possible to determine dominance between two PHI nodes + // based on their ordering. + if (isa(A) && isa(B)) + return false; + + // Loop through the basic block until we find A or B. + BasicBlock::iterator I = BBA->begin(); + for (; &*I != A && &*I != B; ++I) /*empty*/; + + if(!IsPostDominators) { + // A dominates B if it is found first in the basic block. + return &*I == A; + } else { + // A post-dominates B if B is found first in the basic block. + return &*I == B; + } +} + + +// runOnFunction - This method calculates the forward dominator sets for the +// specified function. +// +bool DominatorSet::runOnFunction(Function &F) { + BasicBlock *Root = &F.getEntryBlock(); + Roots.clear(); + Roots.push_back(Root); + assert(pred_begin(Root) == pred_end(Root) && + "Root node has predecessors in function!"); + + ImmediateDominators &ID = getAnalysis(); + Doms.clear(); + if (Roots.empty()) return false; + + // Root nodes only dominate themselves. + for (unsigned i = 0, e = Roots.size(); i != e; ++i) + Doms[Roots[i]].insert(Roots[i]); + + // Loop over all of the blocks in the function, calculating dominator sets for + // each function. + for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) + if (BasicBlock *IDom = ID[I]) { // Get idom if block is reachable + DomSetType &DS = Doms[I]; + assert(DS.empty() && "Domset already filled in for this block?"); + DS.insert(I); // Blocks always dominate themselves + + // Insert all dominators into the set... + while (IDom) { + // If we have already computed the dominator sets for our immediate + // dominator, just use it instead of walking all the way up to the root. + DomSetType &IDS = Doms[IDom]; + if (!IDS.empty()) { + DS.insert(IDS.begin(), IDS.end()); + break; + } else { + DS.insert(IDom); + IDom = ID[IDom]; + } + } + } else { + // Ensure that every basic block has at least an empty set of nodes. This + // is important for the case when there is unreachable blocks. + Doms[I]; + } + + return false; +} + +namespace llvm { +static std::ostream &operator<<(std::ostream &o, + const std::set &BBs) { + for (std::set::const_iterator I = BBs.begin(), E = BBs.end(); + I != E; ++I) + if (*I) + WriteAsOperand(o, *I, false); + else + o << " <>"; + return o; +} +} + +void DominatorSetBase::print(std::ostream &o, const Module* ) const { + for (const_iterator I = begin(), E = end(); I != E; ++I) { + o << " DomSet For BB: "; + if (I->first) + WriteAsOperand(o, I->first, false); + else + o << " <>"; + o << " is:\t" << I->second << "\n"; + } +} + //===----------------------------------------------------------------------===// // DominatorTree Implementation //===----------------------------------------------------------------------===// @@ -421,20 +528,6 @@ DominanceFrontier::calculate(const DominatorTree &DT, return *Result; } -namespace llvm { -static std::ostream &operator<<(std::ostream &o, - const std::set &BBs) { - for (std::set::const_iterator I = BBs.begin(), E = BBs.end(); - I != E; ++I) - if (*I) - WriteAsOperand(o, *I, false); - else - o << " <>"; - return o; -} -} - - void DominanceFrontierBase::print(std::ostream &o, const Module* ) const { for (const_iterator I = begin(), E = end(); I != E; ++I) { o << " DomFrontier for BB"; @@ -978,3 +1071,5 @@ void ETForestBase::print(std::ostream &o, const Module *) const { } o << "\n"; } + +DEFINING_FILE_FOR(DominatorSet)