5 namespace DominatorTreesAndDominanceFrontiers
7 template <
typename CFGFilterFunction >
void TemplatedDominatorTree <
8 CFGFilterFunction >::writeDot(
char *filename)
10 std::ofstream f(filename);
12 f <<
"digraph \"G" << filename <<
"\" {" << std::endl;
14 int dtSize = getSize();
15 for (
int i = 0; i < dtSize; i++)
17 f <<
"\"" << i <<
"\" [label = \"ID " <<i<<
"\\n"<<
escapeString(getCFGNodeFromID(i).
20 f <<
"\"" << getImDomID(i) <<
"\" -> \"" << i <<
"\";" << std::endl;
22 f <<
"}" << std::endl;
30 PRE_DOMINATOR) ? (head->cfgForBeginning()) : (head->cfgForEnd()))
35 calculateImmediateDominators();
40 CFGFilterFunction >::depthFirstSearch()
42 std::vector < VirtualCFG::FilteredCFGNode < CFGFilterFunction > >workList;
43 workList.push_back(cfgRoot);
44 std::vector<int>parentWorkList;
45 parentWorkList.push_back(0);
48 while (workList.size() > 0)
53 parent=parentWorkList.back();parentWorkList.pop_back();
55 std::cout <<
"DFS: processing node <"<<current.
toString()<<
">"<<nodeCount<<std::endl;
58 if (nodeToIdMap.count(current) == 0)
61 std::cout <<
"\tThis node has not been process, assigning ID "<<nodeCount<<std::endl;
65 nodeToIdMap[current] = nodeCount;
67 idToNode.push_back(current);
68 semi.push_back(nodeCount);
71 dfsParent.push_back(parent);
72 buckets.push_back(std::set < int >());
74 std::vector < VirtualCFG::FilteredCFGEdge < CFGFilterFunction > >
outEdges =
75 this->getDirectionModifiedOutEdges(current);
76 for (
unsigned int i = 0; i < outEdges.size(); i++)
78 workList.push_back(this->target(outEdges[i]));
79 parentWorkList.push_back(nodeCount);
85 std::cout <<
"Depth First Search order of CFG-Nodes" << std::endl;
88 typename std::map < VirtualCFG::FilteredCFGNode < CFGFilterFunction >,
int>::iterator i;
90 for (i = nodeToIdMap.begin(); i != nodeToIdMap.end(); i++)
92 std::cout << (*i).first.toString() <<
"\t" << (*i).second << std::endl;
99 CFGFilterFunction >::link(
int ancest,
int current)
105 CFGFilterFunction >::eval(
int node)
109 while (tmp > -1 &&
ancestor[tmp] != -1)
111 if (semi[cmp] > semi[tmp])
133 CFGFilterFunction >::calculateImmediateDominators()
137 std::cout <<
"init:"<<std::endl;
139 std::cout <<std::endl;
140 for (
int i = idToNode.size() - 1; i > 0; i--)
142 std::cout <<
"dfs#"<<i<<std::endl;
144 std::cout<<std::endl;
146 int dfsP = dfsParent[i];
153 std::vector < VirtualCFG::FilteredCFGEdge < CFGFilterFunction > >
inEdges =
154 getDirectionModifiedInEdges(current);
155 for (
unsigned int j = 0; j < inEdges.size(); j++)
157 int nodeID = nodeToIdMap[source(inEdges[j])];
158 std::cout <<
"v="<<nodeID<<std::endl;
160 int possiblySmallestSemiDom = eval(nodeID);
161 std::cout <<
"u=EVAL("<<nodeID<<
")="<<possiblySmallestSemiDom<<std::endl;
162 std::cout <<
"semi("<<possiblySmallestSemiDom<<
")="<<semi[possiblySmallestSemiDom]<<
" < semi("<<i<<
")="<<semi[i];
163 if (semi[possiblySmallestSemiDom] < semi[i])
165 std::cout <<
" -> semi("<<i<<
")="<<semi[possiblySmallestSemiDom];
166 semi[i] = semi[possiblySmallestSemiDom];
168 std::cout <<std::endl;
172 std::cout <<
"add "<<i<<
" to bucket semi["<<i<<
"]"<<semi[i]<<std::endl;
173 buckets[semi[i]].insert(i);
174 std::cout <<
"link "<<dfsP<<
","<<i<<std::endl;
178 for (std::set < int >::iterator j = buckets[dfsP].begin();
179 j != buckets[dfsP].end(); j++)
181 int tmpNodeID = eval(*j);
184 if (semi[tmpNodeID] < dfsP)
185 idom[(*j)] = tmpNodeID;
191 buckets[dfsP].clear();
193 for (
unsigned int i = 1; i < idToNode.size(); i++)
195 if (idom[i] != semi[i])
197 idom[i] = idom[idom[i]];
201 std::cout <<
"Dominators:" << std::endl;
202 for (
unsigned int i = 0; i < idToNode.size(); i++)
204 std::cout <<
">" << idToNode[i].toString() <<
"<: " << idom[i] << std::endl;
208 template <
typename CFGFilterFunction >
void TemplatedDominatorTree <
209 CFGFilterFunction >::calculateImmediateDominators()
212 for (
int i = idToNode.size() - 1; i > 0; i--)
215 int dfsP = dfsParent[i];
221 std::vector < VirtualCFG::FilteredCFGEdge < CFGFilterFunction > >inEdges =
222 this->getDirectionModifiedInEdges(current);
223 for (
unsigned int j = 0; j < inEdges.size(); j++)
225 int nodeID = nodeToIdMap[this->source(inEdges[j])];
228 int possiblySmallestSemiDom = eval(nodeID);
229 if (semi[possiblySmallestSemiDom] < semi[i])
231 semi[i] = semi[possiblySmallestSemiDom];
236 buckets[semi[i]].insert(i);
240 for (std::set < int >::iterator j = buckets[dfsP].begin();
241 j != buckets[dfsP].end(); j++)
243 int tmpNodeID = eval(*j);
246 if (semi[tmpNodeID] < dfsP)
247 idom[(*j)] = tmpNodeID;
252 buckets[dfsP].clear();
254 for (
unsigned int i = 1; i < idToNode.size(); i++)
256 if (idom[i] != semi[i])
258 idom[i] = idom[idom[i]];