LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RegionPrinter.cpp
Go to the documentation of this file.
1 //===- 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/Passes.h"
15 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Support/Debug.h"
23 
24 using namespace llvm;
25 
26 //===----------------------------------------------------------------------===//
27 /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
28 static cl::opt<bool>
29 onlySimpleRegions("only-simple-regions",
30  cl::desc("Show only simple regions in the graphviz viewer"),
31  cl::Hidden,
32  cl::init(false));
33 
34 namespace llvm {
35 template<>
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())
48  ::getSimpleNodeLabel(BB, BB->getParent());
49  else
52  }
53 
54  return "Not implemented";
55  }
56 };
57 
58 template<>
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) {
70  G->getTopLevelRegion());
71  }
72 
73  std::string getEdgeAttributes(RegionNode *srcNode,
75 
76  RegionNode *destNode = *CI;
77 
78  if (srcNode->isSubRegion() || destNode->isSubRegion())
79  return "";
80 
81  // In case of a backedge, do not use it to define the layout of the nodes.
82  BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
83  BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
84 
85  Region *R = RI->getRegionFor(destBB);
86 
87  while (R && R->getParent())
88  if (R->getParent()->getEntry() == destBB)
89  R = R->getParent();
90  else
91  break;
92 
93  if (R->getEntry() == destBB && R->contains(srcBB))
94  return "constraint=false";
95 
96  return "";
97  }
98 
99  // Print the cluster of the subregions. This groups the single basic blocks
100  // and adds a different background color for each group.
102  unsigned depth = 0) {
103  raw_ostream &O = GW.getOStream();
104  O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(R)
105  << " {\n";
106  O.indent(2 * (depth + 1)) << "label = \"\";\n";
107 
108  if (!onlySimpleRegions || R->isSimple()) {
109  O.indent(2 * (depth + 1)) << "style = filled;\n";
110  O.indent(2 * (depth + 1)) << "color = "
111  << ((R->getDepth() * 2 % 12) + 1) << "\n";
112 
113  } else {
114  O.indent(2 * (depth + 1)) << "style = solid;\n";
115  O.indent(2 * (depth + 1)) << "color = "
116  << ((R->getDepth() * 2 % 12) + 2) << "\n";
117  }
118 
119  for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
120  printRegionCluster(*RI, GW, depth + 1);
121 
122  RegionInfo *RI = R->getRegionInfo();
123 
125  BE = R->block_end(); BI != BE; ++BI)
126  if (RI->getRegionFor(*BI) == R)
127  O.indent(2 * (depth + 1)) << "Node"
128  << static_cast<const void*>(RI->getTopLevelRegion()->getBBNode(*BI))
129  << ";\n";
130 
131  O.indent(2 * depth) << "}\n";
132  }
133 
134  static void addCustomGraphFeatures(const RegionInfo* RI,
136  raw_ostream &O = GW.getOStream();
137  O << "\tcolorscheme = \"paired12\"\n";
138  printRegionCluster(RI->getTopLevelRegion(), GW, 4);
139  }
140 };
141 } //end namespace llvm
142 
143 namespace {
144 
145 struct RegionViewer
146  : public DOTGraphTraitsViewer<RegionInfo, false> {
147  static char ID;
148  RegionViewer() : DOTGraphTraitsViewer<RegionInfo, false>("reg", ID){
150  }
151 };
152 char RegionViewer::ID = 0;
153 
154 struct RegionOnlyViewer
155  : public DOTGraphTraitsViewer<RegionInfo, true> {
156  static char ID;
157  RegionOnlyViewer() : DOTGraphTraitsViewer<RegionInfo, true>("regonly", ID) {
159  }
160 };
161 char RegionOnlyViewer::ID = 0;
162 
163 struct RegionPrinter
164  : public DOTGraphTraitsPrinter<RegionInfo, false> {
165  static char ID;
166  RegionPrinter() :
169  }
170 };
171 char RegionPrinter::ID = 0;
172 } //end anonymous namespace
173 
174 INITIALIZE_PASS(RegionPrinter, "dot-regions",
175  "Print regions of function to 'dot' file", true, true)
176 
177 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
178  true, true)
179 
180 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
181  "View regions of function (with no function bodies)",
182  true, true)
183 
184 namespace {
185 
186 struct RegionOnlyPrinter
187  : public DOTGraphTraitsPrinter<RegionInfo, true> {
188  static char ID;
189  RegionOnlyPrinter() :
192  }
193 };
194 
195 }
196 
197 char RegionOnlyPrinter::ID = 0;
198 INITIALIZE_PASS(RegionOnlyPrinter, "dot-regions-only",
199  "Print regions of function to 'dot' file "
200  "(with no function bodies)",
201  true, true)
202 
204  return new RegionViewer();
205 }
206 
208  return new RegionOnlyViewer();
209 }
210 
212  return new RegionPrinter();
213 }
214 
216  return new RegionOnlyPrinter();
217 }
218 
unsigned getDepth() const
Get the nesting level of this Region.
Definition: RegionInfo.cpp:386
static PassRegistry * getPassRegistry()
static std::string getGraphName(RegionInfo *DT)
void initializeRegionViewerPass(PassRegistry &)
void initializeRegionPrinterPass(PassRegistry &)
block_iterator block_begin()
Definition: RegionInfo.h:534
Region * getRegionFor(BasicBlock *BB) const
Get the smallest region that contains a BasicBlock.
Definition: RegionInfo.cpp:745
RegionSet::const_iterator const_iterator
Definition: RegionInfo.h:481
std::string getNodeLabel(RegionNode *Node, RegionNode *Graph)
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:116
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
Definition: RegionInfo.h:56
iterator begin()
Definition: RegionInfo.h:483
INITIALIZE_PASS(RegionPrinter,"dot-regions","Print regions of function to 'dot' file", true, true) INITIALIZE_PASS(RegionViewer
raw_ostream & getOStream()
Definition: GraphWriter.h:304
FunctionPass * createRegionOnlyViewerPass()
RegionInfo * getRegionInfo() const
Return the RegionInfo object, that belongs to this Region.
Definition: RegionInfo.h:349
std::string getNodeLabel(RegionNode *Node, RegionInfo *G)
block_iterator block_end()
Definition: RegionInfo.h:538
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
#define false
Definition: ConvertUTF.c:64
#define G(x, y, z)
Definition: MD5.cpp:52
std::string getNodeLabel(const void *, const GraphType &)
view View regions of function
FunctionPass * createRegionOnlyPrinterPass()
Region * getParent() const
Get the parent of the Region.
Definition: RegionInfo.h:295
static void printRegionCluster(const Region *R, GraphWriter< RegionInfo * > &GW, unsigned depth=0)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:314
static void addCustomGraphFeatures(const RegionInfo *RI, GraphWriter< RegionInfo * > &GW)
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
A single entry single exit Region.
Definition: RegionInfo.h:202
bool isSimple() const
Is this a simple region?
Definition: RegionInfo.cpp:207
FunctionPass * createRegionViewerPass()
RegionNode * getBBNode(BasicBlock *BB) const
Get the BasicBlock RegionNode for a BasicBlock.
Definition: RegionInfo.cpp:313
Region * getTopLevelRegion() const
Definition: RegionInfo.h:712
FunctionPass * createRegionPrinterPass()
void initializeRegionOnlyViewerPass(PassRegistry &)
view regions
void initializeRegionOnlyPrinterPass(PassRegistry &)
bool isSubRegion() const
Is this RegionNode a subregion?
Definition: RegionInfo.h:120
view View regions of true
iterator end()
Definition: RegionInfo.h:484
static cl::opt< bool > onlySimpleRegions("only-simple-regions", cl::desc("Show only simple regions in the graphviz viewer"), cl::Hidden, cl::init(false))
onlySimpleRegion - Show only the simple regions in the RegionViewer.
T * getNodeAs() const
Get the content of this RegionNode.
std::string getEdgeAttributes(RegionNode *srcNode, GraphTraits< RegionInfo * >::ChildIteratorType CI, RegionInfo *RI)
Analysis that detects all canonical Regions.
Definition: RegionInfo.h:577