1 //===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Print out the region tree of a function using dotty/graphviz.
10 //===----------------------------------------------------------------------===//
12 #include "llvm/Analysis/Passes.h"
13 #include "llvm/ADT/DepthFirstIterator.h"
14 #include "llvm/ADT/PostOrderIterator.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Analysis/DOTGraphTraitsPass.h"
17 #include "llvm/Analysis/RegionInfo.h"
18 #include "llvm/Analysis/RegionIterator.h"
19 #include "llvm/Analysis/RegionPrinter.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
26 //===----------------------------------------------------------------------===//
27 /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
29 onlySimpleRegions("only-simple-regions",
30 cl::desc("Show only simple regions in the graphviz viewer"),
36 struct DOTGraphTraits<RegionNode*> : public DefaultDOTGraphTraits {
38 DOTGraphTraits (bool isSimple=false)
39 : DefaultDOTGraphTraits(isSimple) {}
41 std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
43 if (!Node->isSubRegion()) {
44 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
47 return DOTGraphTraits<const Function*>
48 ::getSimpleNodeLabel(BB, BB->getParent());
50 return DOTGraphTraits<const Function*>
51 ::getCompleteNodeLabel(BB, BB->getParent());
54 return "Not implemented";
59 struct DOTGraphTraits<RegionInfoPass*> : public DOTGraphTraits<RegionNode*> {
61 DOTGraphTraits (bool isSimple = false)
62 : DOTGraphTraits<RegionNode*>(isSimple) {}
64 static std::string getGraphName(RegionInfoPass *DT) {
65 return "Region Graph";
68 std::string getNodeLabel(RegionNode *Node, RegionInfoPass *G) {
69 RegionInfo &RI = G->getRegionInfo();
70 return DOTGraphTraits<RegionNode*>::getNodeLabel(Node,
71 reinterpret_cast<RegionNode*>(RI.getTopLevelRegion()));
74 std::string getEdgeAttributes(RegionNode *srcNode,
75 GraphTraits<RegionInfo*>::ChildIteratorType CI, RegionInfoPass *G) {
76 RegionInfo &RI = G->getRegionInfo();
77 RegionNode *destNode = *CI;
79 if (srcNode->isSubRegion() || destNode->isSubRegion())
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>();
86 Region *R = RI.getRegionFor(destBB);
88 while (R && R->getParent())
89 if (R->getParent()->getEntry() == destBB)
94 if (R->getEntry() == destBB && R->contains(srcBB))
95 return "constraint=false";
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,
103 GraphWriter<RegionInfoPass*> &GW,
104 unsigned depth = 0) {
105 raw_ostream &O = GW.getOStream();
106 O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
108 O.indent(2 * (depth + 1)) << "label = \"\";\n";
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";
116 O.indent(2 * (depth + 1)) << "style = solid;\n";
117 O.indent(2 * (depth + 1)) << "color = "
118 << ((R.getDepth() * 2 % 12) + 2) << "\n";
121 for (Region::const_iterator RI = R.begin(), RE = R.end(); RI != RE; ++RI)
122 printRegionCluster(**RI, GW, depth + 1);
124 const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
126 for (const 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))
132 O.indent(2 * depth) << "}\n";
135 static void addCustomGraphFeatures(const RegionInfoPass* RIP,
136 GraphWriter<RegionInfoPass*> &GW) {
137 const RegionInfo &RI = RIP->getRegionInfo();
138 raw_ostream &O = GW.getOStream();
139 O << "\tcolorscheme = \"paired12\"\n";
140 printRegionCluster(*RI.getTopLevelRegion(), GW, 4);
143 } //end namespace llvm
148 : public DOTGraphTraitsViewer<RegionInfoPass, false> {
150 RegionViewer() : DOTGraphTraitsViewer<RegionInfoPass, false>("reg", ID){
151 initializeRegionViewerPass(*PassRegistry::getPassRegistry());
154 char RegionViewer::ID = 0;
156 struct RegionOnlyViewer
157 : public DOTGraphTraitsViewer<RegionInfoPass, true> {
159 RegionOnlyViewer() : DOTGraphTraitsViewer<RegionInfoPass, true>("regonly", ID) {
160 initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
163 char RegionOnlyViewer::ID = 0;
166 : public DOTGraphTraitsPrinter<RegionInfoPass, false> {
169 DOTGraphTraitsPrinter<RegionInfoPass, false>("reg", ID) {
170 initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
173 char RegionPrinter::ID = 0;
174 } //end anonymous namespace
176 INITIALIZE_PASS(RegionPrinter, "dot-regions",
177 "Print regions of function to 'dot' file", true, true)
179 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
182 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
183 "View regions of function (with no function bodies)",
188 struct RegionOnlyPrinter
189 : public DOTGraphTraitsPrinter<RegionInfoPass, true> {
191 RegionOnlyPrinter() :
192 DOTGraphTraitsPrinter<RegionInfoPass, true>("reg", ID) {
193 initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
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)",
205 FunctionPass* llvm::createRegionViewerPass() {
206 return new RegionViewer();
209 FunctionPass* llvm::createRegionOnlyViewerPass() {
210 return new RegionOnlyViewer();
213 FunctionPass* llvm::createRegionPrinterPass() {
214 return new RegionPrinter();
217 FunctionPass* llvm::createRegionOnlyPrinterPass() {
218 return new RegionOnlyPrinter();