LLVM  3.7.0
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(RegionInfoPass *DT) {
65  return "Region Graph";
66  }
67 
68  std::string getNodeLabel(RegionNode *Node, RegionInfoPass *G) {
69  RegionInfo &RI = G->getRegionInfo();
71  reinterpret_cast<RegionNode*>(RI.getTopLevelRegion()));
72  }
73 
74  std::string getEdgeAttributes(RegionNode *srcNode,
76  RegionInfo &RI = G->getRegionInfo();
77  RegionNode *destNode = *CI;
78 
79  if (srcNode->isSubRegion() || destNode->isSubRegion())
80  return "";
81 
82  // In case of a backedge, do not use it to define the layout of the nodes.
83  BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
84  BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
85 
86  Region *R = RI.getRegionFor(destBB);
87 
88  while (R && R->getParent())
89  if (R->getParent()->getEntry() == destBB)
90  R = R->getParent();
91  else
92  break;
93 
94  if (R->getEntry() == destBB && R->contains(srcBB))
95  return "constraint=false";
96 
97  return "";
98  }
99 
100  // Print the cluster of the subregions. This groups the single basic blocks
101  // and adds a different background color for each group.
102  static void printRegionCluster(const Region &R,
104  unsigned depth = 0) {
105  raw_ostream &O = GW.getOStream();
106  O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
107  << " {\n";
108  O.indent(2 * (depth + 1)) << "label = \"\";\n";
109 
110  if (!onlySimpleRegions || R.isSimple()) {
111  O.indent(2 * (depth + 1)) << "style = filled;\n";
112  O.indent(2 * (depth + 1)) << "color = "
113  << ((R.getDepth() * 2 % 12) + 1) << "\n";
114 
115  } else {
116  O.indent(2 * (depth + 1)) << "style = solid;\n";
117  O.indent(2 * (depth + 1)) << "color = "
118  << ((R.getDepth() * 2 % 12) + 2) << "\n";
119  }
120 
121  for (Region::const_iterator RI = R.begin(), RE = R.end(); RI != RE; ++RI)
122  printRegionCluster(**RI, GW, depth + 1);
123 
124  const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
125 
126  for (auto *BB : R.blocks())
127  if (RI.getRegionFor(BB) == &R)
128  O.indent(2 * (depth + 1)) << "Node"
129  << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
130  << ";\n";
131 
132  O.indent(2 * depth) << "}\n";
133  }
134 
135  static void addCustomGraphFeatures(const RegionInfoPass* RIP,
137  const RegionInfo &RI = RIP->getRegionInfo();
138  raw_ostream &O = GW.getOStream();
139  O << "\tcolorscheme = \"paired12\"\n";
140  printRegionCluster(*RI.getTopLevelRegion(), GW, 4);
141  }
142 };
143 } //end namespace llvm
144 
145 namespace {
146 
147 struct RegionViewer
148  : public DOTGraphTraitsViewer<RegionInfoPass, false> {
149  static char ID;
150  RegionViewer() : DOTGraphTraitsViewer<RegionInfoPass, false>("reg", ID){
152  }
153 };
154 char RegionViewer::ID = 0;
155 
156 struct RegionOnlyViewer
157  : public DOTGraphTraitsViewer<RegionInfoPass, true> {
158  static char ID;
159  RegionOnlyViewer() : DOTGraphTraitsViewer<RegionInfoPass, true>("regonly", ID) {
161  }
162 };
163 char RegionOnlyViewer::ID = 0;
164 
165 struct RegionPrinter
166  : public DOTGraphTraitsPrinter<RegionInfoPass, false> {
167  static char ID;
168  RegionPrinter() :
171  }
172 };
173 char RegionPrinter::ID = 0;
174 } //end anonymous namespace
175 
176 INITIALIZE_PASS(RegionPrinter, "dot-regions",
177  "Print regions of function to 'dot' file", true, true)
178 
179 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
180  true, true)
181 
182 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
183  "View regions of function (with no function bodies)",
184  true, true)
185 
186 namespace {
187 
188 struct RegionOnlyPrinter
189  : public DOTGraphTraitsPrinter<RegionInfoPass, true> {
190  static char ID;
191  RegionOnlyPrinter() :
194  }
195 };
196 
197 }
198 
199 char RegionOnlyPrinter::ID = 0;
200 INITIALIZE_PASS(RegionOnlyPrinter, "dot-regions-only",
201  "Print regions of function to 'dot' file "
202  "(with no function bodies)",
203  true, true)
204 
206  return new RegionViewer();
207 }
208 
210  return new RegionOnlyViewer();
211 }
212 
214  return new RegionPrinter();
215 }
216 
218  return new RegionOnlyPrinter();
219 }
220 
iterator begin()
Definition: RegionInfo.h:548
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool isSimple() const
Is this a simple region?
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: GraphTraits.h:27
void initializeRegionViewerPass(PassRegistry &)
void initializeRegionPrinterPass(PassRegistry &)
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:111
bool isSubRegion() const
Is this RegionNode a subregion?
Definition: RegionInfo.h:181
RegionT * getTopLevelRegion() const
Definition: RegionInfo.h:797
T * getNodeAs() const
Get the content of this RegionNode.
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
INITIALIZE_PASS(RegionPrinter,"dot-regions","Print regions of function to 'dot' file", true, true) INITIALIZE_PASS(RegionViewer
raw_ostream & getOStream()
getOStream - Get the raw output stream into the graph file.
Definition: GraphWriter.h:303
RegionInfoT * getRegionInfo() const
Return the RegionInfo object, that belongs to this Region.
Definition: RegionInfo.h:414
FunctionPass * createRegionOnlyViewerPass()
unsigned getDepth() const
Get the nesting level of this Region.
RegionInfo & getRegionInfo()
Definition: RegionInfo.h:861
block_range blocks()
Returns a range view of the basic blocks in the region.
Definition: RegionInfo.h:610
#define false
Definition: ConvertUTF.c:65
#define G(x, y, z)
Definition: MD5.cpp:52
std::string getNodeLabel(const void *, const GraphType &)
getNodeLabel - Given a node and a pointer to the top level graph, return the label to print in the no...
view View regions of function
FunctionPass * createRegionOnlyPrinterPass()
iterator end()
Definition: RegionInfo.h:549
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
static void printRegionCluster(const Region &R, GraphWriter< RegionInfoPass * > &GW, unsigned depth=0)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
FunctionPass * createRegionViewerPass()
std::string getNodeLabel(RegionNode *Node, RegionInfoPass *G)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
FunctionPass * createRegionPrinterPass()
void initializeRegionOnlyViewerPass(PassRegistry &)
view regions
std::string getEdgeAttributes(RegionNode *srcNode, GraphTraits< RegionInfo * >::ChildIteratorType CI, RegionInfoPass *G)
static void addCustomGraphFeatures(const RegionInfoPass *RIP, GraphWriter< RegionInfoPass * > &GW)
void initializeRegionOnlyPrinterPass(PassRegistry &)
view View regions of true
RegionSet::const_iterator const_iterator
Definition: RegionInfo.h:546
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.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
static std::string getGraphName(RegionInfoPass *DT)