Skip to content

Commit 6b0c4c9

Browse files
author
Cameron Zwarich
committed
Move DominanceFrontier from VMCore to Analysis.
llvm-svn: 123747
1 parent 62ea26f commit 6b0c4c9

File tree

5 files changed

+140
-130
lines changed

5 files changed

+140
-130
lines changed

llvm/lib/Analysis/Analysis.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void llvm::initializeAnalysis(PassRegistry &Registry) {
2828
initializeCFGOnlyViewerPass(Registry);
2929
initializeCFGOnlyPrinterPass(Registry);
3030
initializePrintDbgInfoPass(Registry);
31+
initializeDominanceFrontierPass(Registry);
3132
initializeDomViewerPass(Registry);
3233
initializeDomPrinterPass(Registry);
3334
initializeDomOnlyViewerPass(Registry);

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_llvm_library(LLVMAnalysis
1212
DbgInfoPrinter.cpp
1313
DebugInfo.cpp
1414
DIBuilder.cpp
15+
DominanceFrontier.cpp
1516
DomPrinter.cpp
1617
IVUsers.cpp
1718
InlineCost.cpp
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
//===- DominanceFrontier.cpp - Dominance Frontier Calculation -------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "llvm/Analysis/DominanceFrontier.h"
11+
#include "llvm/Support/Debug.h"
12+
#include "llvm/ADT/SmallPtrSet.h"
13+
#include "llvm/Assembly/Writer.h"
14+
#include "llvm/Support/raw_ostream.h"
15+
using namespace llvm;
16+
17+
char DominanceFrontier::ID = 0;
18+
INITIALIZE_PASS_BEGIN(DominanceFrontier, "domfrontier",
19+
"Dominance Frontier Construction", true, true)
20+
INITIALIZE_PASS_DEPENDENCY(DominatorTree)
21+
INITIALIZE_PASS_END(DominanceFrontier, "domfrontier",
22+
"Dominance Frontier Construction", true, true)
23+
24+
namespace {
25+
class DFCalculateWorkObject {
26+
public:
27+
DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
28+
const DomTreeNode *N,
29+
const DomTreeNode *PN)
30+
: currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
31+
BasicBlock *currentBB;
32+
BasicBlock *parentBB;
33+
const DomTreeNode *Node;
34+
const DomTreeNode *parentNode;
35+
};
36+
}
37+
38+
const DominanceFrontier::DomSetType &
39+
DominanceFrontier::calculate(const DominatorTree &DT,
40+
const DomTreeNode *Node) {
41+
BasicBlock *BB = Node->getBlock();
42+
DomSetType *Result = NULL;
43+
44+
std::vector<DFCalculateWorkObject> workList;
45+
SmallPtrSet<BasicBlock *, 32> visited;
46+
47+
workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
48+
do {
49+
DFCalculateWorkObject *currentW = &workList.back();
50+
assert (currentW && "Missing work object.");
51+
52+
BasicBlock *currentBB = currentW->currentBB;
53+
BasicBlock *parentBB = currentW->parentBB;
54+
const DomTreeNode *currentNode = currentW->Node;
55+
const DomTreeNode *parentNode = currentW->parentNode;
56+
assert (currentBB && "Invalid work object. Missing current Basic Block");
57+
assert (currentNode && "Invalid work object. Missing current Node");
58+
DomSetType &S = Frontiers[currentBB];
59+
60+
// Visit each block only once.
61+
if (visited.count(currentBB) == 0) {
62+
visited.insert(currentBB);
63+
64+
// Loop over CFG successors to calculate DFlocal[currentNode]
65+
for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
66+
SI != SE; ++SI) {
67+
// Does Node immediately dominate this successor?
68+
if (DT[*SI]->getIDom() != currentNode)
69+
S.insert(*SI);
70+
}
71+
}
72+
73+
// At this point, S is DFlocal. Now we union in DFup's of our children...
74+
// Loop through and visit the nodes that Node immediately dominates (Node's
75+
// children in the IDomTree)
76+
bool visitChild = false;
77+
for (DomTreeNode::const_iterator NI = currentNode->begin(),
78+
NE = currentNode->end(); NI != NE; ++NI) {
79+
DomTreeNode *IDominee = *NI;
80+
BasicBlock *childBB = IDominee->getBlock();
81+
if (visited.count(childBB) == 0) {
82+
workList.push_back(DFCalculateWorkObject(childBB, currentBB,
83+
IDominee, currentNode));
84+
visitChild = true;
85+
}
86+
}
87+
88+
// If all children are visited or there is any child then pop this block
89+
// from the workList.
90+
if (!visitChild) {
91+
92+
if (!parentBB) {
93+
Result = &S;
94+
break;
95+
}
96+
97+
DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
98+
DomSetType &parentSet = Frontiers[parentBB];
99+
for (; CDFI != CDFE; ++CDFI) {
100+
if (!DT.properlyDominates(parentNode, DT[*CDFI]))
101+
parentSet.insert(*CDFI);
102+
}
103+
workList.pop_back();
104+
}
105+
106+
} while (!workList.empty());
107+
108+
return *Result;
109+
}
110+
111+
void DominanceFrontierBase::print(raw_ostream &OS, const Module* ) const {
112+
for (const_iterator I = begin(), E = end(); I != E; ++I) {
113+
OS << " DomFrontier for BB ";
114+
if (I->first)
115+
WriteAsOperand(OS, I->first, false);
116+
else
117+
OS << " <<exit node>>";
118+
OS << " is:\t";
119+
120+
const std::set<BasicBlock*> &BBs = I->second;
121+
122+
for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
123+
I != E; ++I) {
124+
OS << ' ';
125+
if (*I)
126+
WriteAsOperand(OS, *I, false);
127+
else
128+
OS << "<<exit node>>";
129+
}
130+
OS << "\n";
131+
}
132+
}
133+
134+
void DominanceFrontierBase::dump() const {
135+
print(dbgs());
136+
}
137+

llvm/lib/VMCore/Core.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ using namespace llvm;
3737

3838
void llvm::initializeCore(PassRegistry &Registry) {
3939
initializeDominatorTreePass(Registry);
40-
initializeDominanceFrontierPass(Registry);
4140
initializePrintModulePassPass(Registry);
4241
initializePrintFunctionPassPass(Registry);
4342
initializeVerifierPass(Registry);

llvm/lib/VMCore/Dominators.cpp

Lines changed: 1 addition & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "llvm/Analysis/DominanceFrontier.h"
17+
#include "llvm/Analysis/Dominators.h"
1818
#include "llvm/Support/CFG.h"
1919
#include "llvm/Support/Compiler.h"
2020
#include "llvm/Support/Debug.h"
@@ -106,131 +106,3 @@ bool DominatorTree::dominates(const Instruction *A, const Instruction *B) const{
106106

107107
return &*I == A;
108108
}
109-
110-
111-
112-
//===----------------------------------------------------------------------===//
113-
// DominanceFrontier Implementation
114-
//===----------------------------------------------------------------------===//
115-
116-
char DominanceFrontier::ID = 0;
117-
INITIALIZE_PASS_BEGIN(DominanceFrontier, "domfrontier",
118-
"Dominance Frontier Construction", true, true)
119-
INITIALIZE_PASS_DEPENDENCY(DominatorTree)
120-
INITIALIZE_PASS_END(DominanceFrontier, "domfrontier",
121-
"Dominance Frontier Construction", true, true)
122-
123-
namespace {
124-
class DFCalculateWorkObject {
125-
public:
126-
DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
127-
const DomTreeNode *N,
128-
const DomTreeNode *PN)
129-
: currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
130-
BasicBlock *currentBB;
131-
BasicBlock *parentBB;
132-
const DomTreeNode *Node;
133-
const DomTreeNode *parentNode;
134-
};
135-
}
136-
137-
const DominanceFrontier::DomSetType &
138-
DominanceFrontier::calculate(const DominatorTree &DT,
139-
const DomTreeNode *Node) {
140-
BasicBlock *BB = Node->getBlock();
141-
DomSetType *Result = NULL;
142-
143-
std::vector<DFCalculateWorkObject> workList;
144-
SmallPtrSet<BasicBlock *, 32> visited;
145-
146-
workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
147-
do {
148-
DFCalculateWorkObject *currentW = &workList.back();
149-
assert (currentW && "Missing work object.");
150-
151-
BasicBlock *currentBB = currentW->currentBB;
152-
BasicBlock *parentBB = currentW->parentBB;
153-
const DomTreeNode *currentNode = currentW->Node;
154-
const DomTreeNode *parentNode = currentW->parentNode;
155-
assert (currentBB && "Invalid work object. Missing current Basic Block");
156-
assert (currentNode && "Invalid work object. Missing current Node");
157-
DomSetType &S = Frontiers[currentBB];
158-
159-
// Visit each block only once.
160-
if (visited.count(currentBB) == 0) {
161-
visited.insert(currentBB);
162-
163-
// Loop over CFG successors to calculate DFlocal[currentNode]
164-
for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
165-
SI != SE; ++SI) {
166-
// Does Node immediately dominate this successor?
167-
if (DT[*SI]->getIDom() != currentNode)
168-
S.insert(*SI);
169-
}
170-
}
171-
172-
// At this point, S is DFlocal. Now we union in DFup's of our children...
173-
// Loop through and visit the nodes that Node immediately dominates (Node's
174-
// children in the IDomTree)
175-
bool visitChild = false;
176-
for (DomTreeNode::const_iterator NI = currentNode->begin(),
177-
NE = currentNode->end(); NI != NE; ++NI) {
178-
DomTreeNode *IDominee = *NI;
179-
BasicBlock *childBB = IDominee->getBlock();
180-
if (visited.count(childBB) == 0) {
181-
workList.push_back(DFCalculateWorkObject(childBB, currentBB,
182-
IDominee, currentNode));
183-
visitChild = true;
184-
}
185-
}
186-
187-
// If all children are visited or there is any child then pop this block
188-
// from the workList.
189-
if (!visitChild) {
190-
191-
if (!parentBB) {
192-
Result = &S;
193-
break;
194-
}
195-
196-
DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
197-
DomSetType &parentSet = Frontiers[parentBB];
198-
for (; CDFI != CDFE; ++CDFI) {
199-
if (!DT.properlyDominates(parentNode, DT[*CDFI]))
200-
parentSet.insert(*CDFI);
201-
}
202-
workList.pop_back();
203-
}
204-
205-
} while (!workList.empty());
206-
207-
return *Result;
208-
}
209-
210-
void DominanceFrontierBase::print(raw_ostream &OS, const Module* ) const {
211-
for (const_iterator I = begin(), E = end(); I != E; ++I) {
212-
OS << " DomFrontier for BB ";
213-
if (I->first)
214-
WriteAsOperand(OS, I->first, false);
215-
else
216-
OS << " <<exit node>>";
217-
OS << " is:\t";
218-
219-
const std::set<BasicBlock*> &BBs = I->second;
220-
221-
for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
222-
I != E; ++I) {
223-
OS << ' ';
224-
if (*I)
225-
WriteAsOperand(OS, *I, false);
226-
else
227-
OS << "<<exit node>>";
228-
}
229-
OS << "\n";
230-
}
231-
}
232-
233-
void DominanceFrontierBase::dump() const {
234-
print(dbgs());
235-
}
236-

0 commit comments

Comments
 (0)