switch the DomTreeNodes and IDoms maps in idom/postidom to a

DenseMap instead of an std::map.  This speeds up postdomtree
by about 25% and domtree by about 23%.  It also speeds up clients,
for example, domfrontier by 11%, mem2reg by 4% and ADCE by 6%.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40826 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-08-04 23:48:07 +00:00
parent f12f8def39
commit 0a5f83c22c
4 changed files with 30 additions and 27 deletions

View File

@ -23,6 +23,7 @@
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include <set> #include <set>
#include "llvm/ADT/DenseMap.h"
namespace llvm { namespace llvm {
@ -103,7 +104,7 @@ class DominatorTreeBase : public DominatorBase {
protected: protected:
void reset(); void reset();
typedef std::map<BasicBlock*, DomTreeNode*> DomTreeNodeMapType; typedef DenseMap<BasicBlock*, DomTreeNode*> DomTreeNodeMapType;
DomTreeNodeMapType DomTreeNodes; DomTreeNodeMapType DomTreeNodes;
DomTreeNode *RootNode; DomTreeNode *RootNode;
@ -120,7 +121,7 @@ protected:
InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0){} InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0){}
}; };
std::map<BasicBlock*, BasicBlock*> IDoms; DenseMap<BasicBlock*, BasicBlock*> IDoms;
// Vertex - Map the DFS number to the BasicBlock* // Vertex - Map the DFS number to the BasicBlock*
std::vector<BasicBlock*> Vertex; std::vector<BasicBlock*> Vertex;
@ -141,8 +142,8 @@ protected:
/// block. This is the same as using operator[] on this class. /// block. This is the same as using operator[] on this class.
/// ///
inline DomTreeNode *getNode(BasicBlock *BB) const { inline DomTreeNode *getNode(BasicBlock *BB) const {
DomTreeNodeMapType::const_iterator i = DomTreeNodes.find(BB); DomTreeNodeMapType::const_iterator I = DomTreeNodes.find(BB);
return (i != DomTreeNodes.end()) ? i->second : 0; return I != DomTreeNodes.end() ? I->second : 0;
} }
inline DomTreeNode *operator[](BasicBlock *BB) const { inline DomTreeNode *operator[](BasicBlock *BB) const {
@ -302,7 +303,7 @@ private:
BasicBlock *Eval(BasicBlock *v); BasicBlock *Eval(BasicBlock *v);
void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo); void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo);
inline BasicBlock *getIDom(BasicBlock *BB) const { inline BasicBlock *getIDom(BasicBlock *BB) const {
std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB); DenseMap<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
return I != IDoms.end() ? I->second : 0; return I != IDoms.end() ? I->second : 0;
} }
}; };

View File

@ -45,7 +45,7 @@ private:
void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo); void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo);
inline BasicBlock *getIDom(BasicBlock *BB) const { inline BasicBlock *getIDom(BasicBlock *BB) const {
std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB); DenseMap<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
return I != IDoms.end() ? I->second : 0; return I != IDoms.end() ? I->second : 0;
} }
}; };

View File

@ -111,14 +111,19 @@ void PostDominatorTree::calculate(Function &F) {
// Step #0: Scan the function looking for the root nodes of the post-dominance // Step #0: Scan the function looking for the root nodes of the post-dominance
// relationships. These blocks, which have no successors, end with return and // relationships. These blocks, which have no successors, end with return and
// unwind instructions. // unwind instructions.
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
if (succ_begin(I) == succ_end(I)) { TerminatorInst *Insn = I->getTerminator();
Instruction *Insn = I->getTerminator(); if (Insn->getNumSuccessors() == 0) {
// Unreachable block is not a root node. // Unreachable block is not a root node.
if (!isa<UnreachableInst>(Insn)) if (!isa<UnreachableInst>(Insn))
Roots.push_back(I); Roots.push_back(I);
} }
// Prepopulate maps so that we don't get iterator invalidation issues later.
IDoms[I] = 0;
DomTreeNodes[I] = 0;
}
Vertex.push_back(0); Vertex.push_back(0);
// Step #1: Number blocks in depth-first order and initialize variables used // Step #1: Number blocks in depth-first order and initialize variables used

View File

@ -212,8 +212,7 @@ void DominatorTree::Compress(BasicBlock *VIn) {
std::vector<BasicBlock *> Work; std::vector<BasicBlock *> Work;
std::set<BasicBlock *> Visited; std::set<BasicBlock *> Visited;
InfoRec &VInInfo = Info[VIn]; BasicBlock *VInAncestor = Info[VIn].Ancestor;
BasicBlock *VInAncestor = VInInfo.Ancestor;
InfoRec &VInVAInfo = Info[VInAncestor]; InfoRec &VInVAInfo = Info[VInAncestor];
if (VInVAInfo.Ancestor != 0) if (VInVAInfo.Ancestor != 0)
@ -233,7 +232,7 @@ void DominatorTree::Compress(BasicBlock *VIn) {
} }
Work.pop_back(); Work.pop_back();
// Update VINfo based on Ancestor info // Update VInfo based on Ancestor info
if (VAInfo.Ancestor == 0) if (VAInfo.Ancestor == 0)
continue; continue;
BasicBlock *VAncestorLabel = VAInfo.Label; BasicBlock *VAncestorLabel = VAInfo.Label;
@ -366,17 +365,16 @@ void DominatorTree::calculate(Function& F) {
// Loop over all of the reachable blocks in the function... // Loop over all of the reachable blocks in the function...
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
if (BasicBlock *ImmDom = getIDom(I)) { // Reachable block. if (BasicBlock *ImmDom = getIDom(I)) { // Reachable block.
DomTreeNode *&BBNode = DomTreeNodes[I]; DomTreeNode *BBNode = DomTreeNodes[I];
if (!BBNode) { // Haven't calculated this node yet? if (BBNode) continue; // Haven't calculated this node yet?
// Get or calculate the node for the immediate dominator // Get or calculate the node for the immediate dominator
DomTreeNode *IDomNode = getNodeForBlock(ImmDom); DomTreeNode *IDomNode = getNodeForBlock(ImmDom);
// Add a new tree node for this BasicBlock, and link it as a child of // Add a new tree node for this BasicBlock, and link it as a child of
// IDomNode // IDomNode
DomTreeNode *C = new DomTreeNode(I, IDomNode); DomTreeNode *C = new DomTreeNode(I, IDomNode);
DomTreeNodes[I] = C; DomTreeNodes[I] = IDomNode->addChild(C);
BBNode = IDomNode->addChild(C);
}
} }
// Free temporary memory used to construct idom's // Free temporary memory used to construct idom's
@ -387,8 +385,7 @@ void DominatorTree::calculate(Function& F) {
updateDFSNumbers(); updateDFSNumbers();
} }
void DominatorTreeBase::updateDFSNumbers() void DominatorTreeBase::updateDFSNumbers() {
{
int dfsnum = 0; int dfsnum = 0;
// Iterate over all nodes in depth first order. // Iterate over all nodes in depth first order.
for (unsigned i = 0, e = Roots.size(); i != e; ++i) for (unsigned i = 0, e = Roots.size(); i != e; ++i)