Fixed some bugs.
[llvm/zpu.git] / lib / Analysis / DomPrinter.cpp
blob051e44b150b506a538d4145f76c6440ab626f595
1 //===- DomPrinter.cpp - DOT printer for the dominance trees ------------===//
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 // This file defines '-dot-dom' and '-dot-postdom' analysis passes, which emit
11 // a dom.<fnname>.dot or postdom.<fnname>.dot file for each function in the
12 // program, with a graph of the dominance/postdominance tree of that
13 // function.
15 // There are also passes available to directly call dotty ('-view-dom' or
16 // '-view-postdom'). By appending '-only' like '-dot-dom-only' only the
17 // names of the bbs are printed, but the content is hidden.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Analysis/DomPrinter.h"
23 #include "llvm/Analysis/Dominators.h"
24 #include "llvm/Analysis/DOTGraphTraitsPass.h"
25 #include "llvm/Analysis/PostDominators.h"
27 using namespace llvm;
29 namespace llvm {
30 template<>
31 struct DOTGraphTraits<DomTreeNode*> : public DefaultDOTGraphTraits {
33 DOTGraphTraits (bool isSimple=false)
34 : DefaultDOTGraphTraits(isSimple) {}
36 std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
38 BasicBlock *BB = Node->getBlock();
40 if (!BB)
41 return "Post dominance root node";
44 if (isSimple())
45 return DOTGraphTraits<const Function*>
46 ::getSimpleNodeLabel(BB, BB->getParent());
47 else
48 return DOTGraphTraits<const Function*>
49 ::getCompleteNodeLabel(BB, BB->getParent());
53 template<>
54 struct DOTGraphTraits<DominatorTree*> : public DOTGraphTraits<DomTreeNode*> {
56 DOTGraphTraits (bool isSimple=false)
57 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
59 static std::string getGraphName(DominatorTree *DT) {
60 return "Dominator tree";
63 std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
64 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
68 template<>
69 struct DOTGraphTraits<PostDominatorTree*>
70 : public DOTGraphTraits<DomTreeNode*> {
72 DOTGraphTraits (bool isSimple=false)
73 : DOTGraphTraits<DomTreeNode*>(isSimple) {}
75 static std::string getGraphName(PostDominatorTree *DT) {
76 return "Post dominator tree";
79 std::string getNodeLabel(DomTreeNode *Node, PostDominatorTree *G ) {
80 return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
85 namespace {
86 struct DomViewer
87 : public DOTGraphTraitsViewer<DominatorTree, false> {
88 static char ID;
89 DomViewer() : DOTGraphTraitsViewer<DominatorTree, false>("dom", ID){
90 initializeDomViewerPass(*PassRegistry::getPassRegistry());
94 struct DomOnlyViewer
95 : public DOTGraphTraitsViewer<DominatorTree, true> {
96 static char ID;
97 DomOnlyViewer() : DOTGraphTraitsViewer<DominatorTree, true>("domonly", ID){
98 initializeDomOnlyViewerPass(*PassRegistry::getPassRegistry());
102 struct PostDomViewer
103 : public DOTGraphTraitsViewer<PostDominatorTree, false> {
104 static char ID;
105 PostDomViewer() :
106 DOTGraphTraitsViewer<PostDominatorTree, false>("postdom", ID){
107 initializePostDomViewerPass(*PassRegistry::getPassRegistry());
111 struct PostDomOnlyViewer
112 : public DOTGraphTraitsViewer<PostDominatorTree, true> {
113 static char ID;
114 PostDomOnlyViewer() :
115 DOTGraphTraitsViewer<PostDominatorTree, true>("postdomonly", ID){
116 initializePostDomOnlyViewerPass(*PassRegistry::getPassRegistry());
119 } // end anonymous namespace
121 char DomViewer::ID = 0;
122 INITIALIZE_PASS(DomViewer, "view-dom",
123 "View dominance tree of function", false, false)
125 char DomOnlyViewer::ID = 0;
126 INITIALIZE_PASS(DomOnlyViewer, "view-dom-only",
127 "View dominance tree of function (with no function bodies)",
128 false, false)
130 char PostDomViewer::ID = 0;
131 INITIALIZE_PASS(PostDomViewer, "view-postdom",
132 "View postdominance tree of function", false, false)
134 char PostDomOnlyViewer::ID = 0;
135 INITIALIZE_PASS(PostDomOnlyViewer, "view-postdom-only",
136 "View postdominance tree of function "
137 "(with no function bodies)",
138 false, false)
140 namespace {
141 struct DomPrinter
142 : public DOTGraphTraitsPrinter<DominatorTree, false> {
143 static char ID;
144 DomPrinter() : DOTGraphTraitsPrinter<DominatorTree, false>("dom", ID) {
145 initializeDomPrinterPass(*PassRegistry::getPassRegistry());
149 struct DomOnlyPrinter
150 : public DOTGraphTraitsPrinter<DominatorTree, true> {
151 static char ID;
152 DomOnlyPrinter() : DOTGraphTraitsPrinter<DominatorTree, true>("domonly", ID) {
153 initializeDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
157 struct PostDomPrinter
158 : public DOTGraphTraitsPrinter<PostDominatorTree, false> {
159 static char ID;
160 PostDomPrinter() :
161 DOTGraphTraitsPrinter<PostDominatorTree, false>("postdom", ID) {
162 initializePostDomPrinterPass(*PassRegistry::getPassRegistry());
166 struct PostDomOnlyPrinter
167 : public DOTGraphTraitsPrinter<PostDominatorTree, true> {
168 static char ID;
169 PostDomOnlyPrinter() :
170 DOTGraphTraitsPrinter<PostDominatorTree, true>("postdomonly", ID) {
171 initializePostDomOnlyPrinterPass(*PassRegistry::getPassRegistry());
174 } // end anonymous namespace
178 char DomPrinter::ID = 0;
179 INITIALIZE_PASS(DomPrinter, "dot-dom",
180 "Print dominance tree of function to 'dot' file",
181 false, false)
183 char DomOnlyPrinter::ID = 0;
184 INITIALIZE_PASS(DomOnlyPrinter, "dot-dom-only",
185 "Print dominance tree of function to 'dot' file "
186 "(with no function bodies)",
187 false, false)
189 char PostDomPrinter::ID = 0;
190 INITIALIZE_PASS(PostDomPrinter, "dot-postdom",
191 "Print postdominance tree of function to 'dot' file",
192 false, false)
194 char PostDomOnlyPrinter::ID = 0;
195 INITIALIZE_PASS(PostDomOnlyPrinter, "dot-postdom-only",
196 "Print postdominance tree of function to 'dot' file "
197 "(with no function bodies)",
198 false, false)
200 // Create methods available outside of this file, to use them
201 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
202 // the link time optimization.
204 FunctionPass *llvm::createDomPrinterPass() {
205 return new DomPrinter();
208 FunctionPass *llvm::createDomOnlyPrinterPass() {
209 return new DomOnlyPrinter();
212 FunctionPass *llvm::createDomViewerPass() {
213 return new DomViewer();
216 FunctionPass *llvm::createDomOnlyViewerPass() {
217 return new DomOnlyViewer();
220 FunctionPass *llvm::createPostDomPrinterPass() {
221 return new PostDomPrinter();
224 FunctionPass *llvm::createPostDomOnlyPrinterPass() {
225 return new PostDomOnlyPrinter();
228 FunctionPass *llvm::createPostDomViewerPass() {
229 return new PostDomViewer();
232 FunctionPass *llvm::createPostDomOnlyViewerPass() {
233 return new PostDomOnlyViewer();