Some fixes to get MachineDomTree working better.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43541 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2007-10-31 03:30:14 +00:00
parent 25f1d08619
commit 5d32ec4cb0
4 changed files with 62 additions and 18 deletions

View File

@ -273,21 +273,24 @@ void Calculate(DominatorTreeBase<typename GraphTraits<NodeT>::NodeType>& DT,
// which postdominates all real exits if there are multiple exit blocks.
typename GraphT::NodeType* Root = DT.Roots.size() == 1 ? DT.Roots[0]
: 0;
DT.DomTreeNodes[Root] = DT.RootNode = new DomTreeNode(Root, 0);
DT.DomTreeNodes[Root] = DT.RootNode =
new DomTreeNodeBase<typename GraphT::NodeType>(Root, 0);
// Loop over all of the reachable blocks in the function...
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
for (typename FuncT::iterator I = F.begin(), E = F.end(); I != E; ++I)
if (typename GraphT::NodeType* ImmDom = DT.getIDom(I)) {
// Reachable block.
DomTreeNode *BBNode = DT.DomTreeNodes[I];
DomTreeNodeBase<typename GraphT::NodeType> *BBNode = DT.DomTreeNodes[I];
if (BBNode) continue; // Haven't calculated this node yet?
// Get or calculate the node for the immediate dominator
DomTreeNode *IDomNode = DT.getNodeForBlock(ImmDom);
DomTreeNodeBase<typename GraphT::NodeType> *IDomNode =
DT.getNodeForBlock(ImmDom);
// Add a new tree node for this BasicBlock, and link it as a child of
// IDomNode
DomTreeNode *C = new DomTreeNode(I, IDomNode);
DomTreeNodeBase<typename GraphT::NodeType> *C =
new DomTreeNodeBase<typename GraphT::NodeType>(I, IDomNode);
DT.DomTreeNodes[I] = IDomNode->addChild(C);
}

View File

@ -132,6 +132,7 @@ private:
};
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
template<class NodeT>
static std::ostream &operator<<(std::ostream &o,
@ -156,7 +157,6 @@ static void PrintDomTree(const DomTreeNodeBase<NodeT> *N, std::ostream &o,
}
typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
//===----------------------------------------------------------------------===//
/// DominatorTree - Calculate the immediate dominator tree for a function.
@ -607,6 +607,12 @@ protected:
return I != IDoms.end() ? I->second : 0;
}
inline void addRoot(NodeT* BB) {
// Unreachable block is not a root node.
if (!isa<UnreachableInst>(&BB->back()))
this->Roots.push_back(BB);
}
public:
/// recalculate - compute a dominator tree for the given function
template<class FT>
@ -615,9 +621,9 @@ public:
reset();
// Initialize roots
this->Roots.push_back(&F.getEntryBlock());
this->IDoms[&F.getEntryBlock()] = 0;
this->DomTreeNodes[&F.getEntryBlock()] = 0;
this->Roots.push_back(&F.front());
this->IDoms[&F.front()] = 0;
this->DomTreeNodes[&F.front()] = 0;
this->Vertex.push_back(0);
Calculate<FT, NodeT*>(*this, F);
@ -627,13 +633,10 @@ public:
reset(); // Reset from the last time we were run...
// Initialize the roots list
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
TerminatorInst *Insn = I->getTerminator();
if (Insn->getNumSuccessors() == 0) {
// Unreachable block is not a root node.
if (!isa<UnreachableInst>(Insn))
this->Roots.push_back(I);
}
for (typename FT::iterator I = F.begin(), E = F.end(); I != E; ++I) {
if (std::distance(GraphTraits<FT*>::child_begin(I),
GraphTraits<FT*>::child_end(I)) == 0)
addRoot(I);
// Prepopulate maps so that we don't get iterator invalidation issues later.
this->IDoms[I] = 0;

View File

@ -24,7 +24,17 @@
namespace llvm {
void WriteAsOperand(std::ostream &, const MachineBasicBlock*, bool t) { }
inline void WriteAsOperand(std::ostream &, const MachineBasicBlock*, bool t) { }
template<>
inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
this->Roots.push_back(MBB);
}
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
//===-------------------------------------
/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
@ -60,7 +70,11 @@ public:
return DT->getRootNode();
}
virtual bool runOnFunction(Function &F);
virtual bool runOnMachineFunction(MachineFunction &F) {
DT->recalculate(F);
return false;
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();

View File

@ -0,0 +1,24 @@
//===- MachineDominators.cpp - Machine Dominator Calculation --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements simple dominator construction algorithms for finding
// forward dominators on machine functions.
//
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/MachineDominators.h"
using namespace llvm;
TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
char MachineDominatorTree::ID = 0;
static RegisterPass<MachineDominatorTree>
E("machinedomtree", "MachineDominator Tree Construction", true);