blob: 0cf0f9050504e423a6daa180b8d68c7d0f94afef [file] [log] [blame]
Tobias Grosserf96b0062010-07-22 07:46:31 +00001//===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
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// Print out the region tree of a function using dotty/graphviz.
10//===----------------------------------------------------------------------===//
11
12#include "llvm/Analysis/RegionInfo.h"
13#include "llvm/Analysis/RegionIterator.h"
14#include "llvm/Analysis/RegionPrinter.h"
15#include "llvm/Analysis/Passes.h"
16#include "llvm/Analysis/DOTGraphTraitsPass.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/ADT/PostOrderIterator.h"
19#include "llvm/ADT/DepthFirstIterator.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/raw_ostream.h"
23
24using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27/// onlySimpleRegion - Show only the simple regions in the RegionViewer.
28static cl::opt<bool>
29onlySimpleRegions("only-simple-regions",
30 cl::desc("Show only simple regions in the graphviz viewer"),
31 cl::Hidden,
32 cl::init(false));
33
34namespace llvm {
35template<>
36struct DOTGraphTraits<RegionNode*> : public DefaultDOTGraphTraits {
37
38 DOTGraphTraits (bool isSimple=false)
39 : DefaultDOTGraphTraits(isSimple) {}
40
41 std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
42
43 if (!Node->isSubRegion()) {
44 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
45
46 if (isSimple())
47 return DOTGraphTraits<const Function*>
48 ::getSimpleNodeLabel(BB, BB->getParent());
49 else
50 return DOTGraphTraits<const Function*>
51 ::getCompleteNodeLabel(BB, BB->getParent());
52 }
53
54 return "Not implemented";
55 }
56};
57
58template<>
59struct DOTGraphTraits<RegionInfo*> : public DOTGraphTraits<RegionNode*> {
60
61 DOTGraphTraits (bool isSimple=false)
62 : DOTGraphTraits<RegionNode*>(isSimple) {}
63
64 static std::string getGraphName(RegionInfo *DT) {
65 return "Region Graph";
66 }
67
68 std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
69 return DOTGraphTraits<RegionNode*>::getNodeLabel(Node,
70 G->getTopLevelRegion());
71 }
72
73 // Print the cluster of the subregions. This groups the single basic blocks
74 // and adds a different background color for each group.
75 static void printRegionCluster(const Region *R, GraphWriter<RegionInfo*> &GW,
76 unsigned depth = 0) {
77 raw_ostream &O = GW.getOStream();
78 O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(R)
79 << " {\n";
80 O.indent(2 * (depth + 1)) << "label = \"\";\n";
81
82 if (!onlySimpleRegions || R->isSimple()) {
83 O.indent(2 * (depth + 1)) << "style = filled;\n";
84 O.indent(2 * (depth + 1)) << "color = "
85 << ((R->getDepth() * 2 % 12) + 1) << "\n";
86
87 } else {
88 O.indent(2 * (depth + 1)) << "style = solid;\n";
89 O.indent(2 * (depth + 1)) << "color = "
90 << ((R->getDepth() * 2 % 12) + 2) << "\n";
91 }
92
93 for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
94 printRegionCluster(*RI, GW, depth + 1);
95
96 RegionInfo *RI = R->getRegionInfo();
97
98 for (Region::const_block_iterator BI = R->block_begin(),
99 BE = R->block_end(); BI != BE; ++BI) {
100 BasicBlock *BB = (*BI)->getNodeAs<BasicBlock>();
101 if (RI->getRegionFor(BB) == R)
102 O.indent(2 * (depth + 1)) << "Node"
103 << static_cast<const void*>(RI->getTopLevelRegion()->getBBNode(BB))
104 << ";\n";
105 }
106
107 O.indent(2 * depth) << "}\n";
108 }
109
110 static void addCustomGraphFeatures(const RegionInfo* RI,
111 GraphWriter<RegionInfo*> &GW) {
112 raw_ostream &O = GW.getOStream();
113 O << "\tcolorscheme = \"paired12\"\n";
114 printRegionCluster(RI->getTopLevelRegion(), GW, 4);
115 }
116};
117} //end namespace llvm
118
119namespace {
120
121struct RegionViewer
122 : public DOTGraphTraitsViewer<RegionInfo, false> {
123 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000124 RegionViewer() : DOTGraphTraitsViewer<RegionInfo, false>("reg", ID){
125 initializeRegionViewerPass(*PassRegistry::getPassRegistry());
126 }
Tobias Grosserf96b0062010-07-22 07:46:31 +0000127};
Tobias Grosserf96b0062010-07-22 07:46:31 +0000128char RegionViewer::ID = 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000129
130struct RegionOnlyViewer
131 : public DOTGraphTraitsViewer<RegionInfo, true> {
132 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000133 RegionOnlyViewer() : DOTGraphTraitsViewer<RegionInfo, true>("regonly", ID) {
134 initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
135 }
Tobias Grosserf96b0062010-07-22 07:46:31 +0000136};
Tobias Grosserf96b0062010-07-22 07:46:31 +0000137char RegionOnlyViewer::ID = 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000138
139struct RegionPrinter
140 : public DOTGraphTraitsPrinter<RegionInfo, false> {
141 static char ID;
142 RegionPrinter() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000143 DOTGraphTraitsPrinter<RegionInfo, false>("reg", ID) {
144 initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
145 }
Tobias Grosserf96b0062010-07-22 07:46:31 +0000146};
Owen Anderson71802342010-10-07 04:13:08 +0000147char RegionPrinter::ID = 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000148} //end anonymous namespace
149
Tobias Grosserf96b0062010-07-22 07:46:31 +0000150INITIALIZE_PASS(RegionPrinter, "dot-regions",
Owen Andersonce665bd2010-10-07 22:25:06 +0000151 "Print regions of function to 'dot' file", true, true)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000152
Owen Anderson71802342010-10-07 04:13:08 +0000153INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
Owen Andersonce665bd2010-10-07 22:25:06 +0000154 true, true)
Owen Anderson71802342010-10-07 04:13:08 +0000155
156INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
157 "View regions of function (with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000158 true, true)
Owen Anderson71802342010-10-07 04:13:08 +0000159
Dan Gohman811edc12010-08-02 18:50:06 +0000160namespace {
161
Tobias Grosserf96b0062010-07-22 07:46:31 +0000162struct RegionOnlyPrinter
163 : public DOTGraphTraitsPrinter<RegionInfo, true> {
164 static char ID;
165 RegionOnlyPrinter() :
Owen Anderson081c34b2010-10-19 17:21:58 +0000166 DOTGraphTraitsPrinter<RegionInfo, true>("reg", ID) {
167 initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
168 }
Tobias Grosserf96b0062010-07-22 07:46:31 +0000169};
170
Dan Gohman811edc12010-08-02 18:50:06 +0000171}
172
Tobias Grosserf96b0062010-07-22 07:46:31 +0000173char RegionOnlyPrinter::ID = 0;
174INITIALIZE_PASS(RegionOnlyPrinter, "dot-regions-only",
175 "Print regions of function to 'dot' file "
176 "(with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000177 true, true)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000178
179FunctionPass* llvm::createRegionViewerPass() {
180 return new RegionViewer();
181}
182
183FunctionPass* llvm::createRegionOnlyViewerPass() {
184 return new RegionOnlyViewer();
185}
186
187FunctionPass* llvm::createRegionPrinterPass() {
188 return new RegionPrinter();
189}
190
191FunctionPass* llvm::createRegionOnlyPrinterPass() {
192 return new RegionOnlyPrinter();
193}
194