blob: 5986b8c4e0c30aa3181cf11e678d92edb5fbc8ca [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
Chandler Carruthe3e43d92017-06-06 11:49:48 +000012#include "llvm/Analysis/RegionPrinter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000013#include "llvm/ADT/DepthFirstIterator.h"
14#include "llvm/ADT/PostOrderIterator.h"
15#include "llvm/ADT/Statistic.h"
16#include "llvm/Analysis/DOTGraphTraitsPass.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000017#include "llvm/Analysis/Passes.h"
Tobias Grosserf96b0062010-07-22 07:46:31 +000018#include "llvm/Analysis/RegionInfo.h"
19#include "llvm/Analysis/RegionIterator.h"
Tobias Grosserf96b0062010-07-22 07:46:31 +000020#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/Support/Debug.h"
Tobias Grosserf96b0062010-07-22 07:46:31 +000022#include "llvm/Support/raw_ostream.h"
Michael Kruse5864ac72015-08-10 13:21:59 +000023#ifndef NDEBUG
24#include "llvm/IR/LegacyPassManager.h"
25#endif
Tobias Grosserf96b0062010-07-22 07:46:31 +000026
27using namespace llvm;
28
29//===----------------------------------------------------------------------===//
30/// onlySimpleRegion - Show only the simple regions in the RegionViewer.
31static cl::opt<bool>
32onlySimpleRegions("only-simple-regions",
33 cl::desc("Show only simple regions in the graphviz viewer"),
34 cl::Hidden,
35 cl::init(false));
36
37namespace llvm {
38template<>
39struct DOTGraphTraits<RegionNode*> : public DefaultDOTGraphTraits {
40
41 DOTGraphTraits (bool isSimple=false)
42 : DefaultDOTGraphTraits(isSimple) {}
43
44 std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
45
46 if (!Node->isSubRegion()) {
47 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
48
49 if (isSimple())
Sean Fertile551913f2018-06-29 17:48:58 +000050 return DOTGraphTraits<const Function*>
51 ::getSimpleNodeLabel(BB, BB->getParent());
Tobias Grosserf96b0062010-07-22 07:46:31 +000052 else
Sean Fertile551913f2018-06-29 17:48:58 +000053 return DOTGraphTraits<const Function*>
54 ::getCompleteNodeLabel(BB, BB->getParent());
Tobias Grosserf96b0062010-07-22 07:46:31 +000055 }
56
57 return "Not implemented";
58 }
59};
60
Michael Kruse6b236512015-08-10 12:57:23 +000061template <>
62struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> {
Tobias Grosserf96b0062010-07-22 07:46:31 +000063
Matt Arsenault5e1c96a2014-07-19 18:29:29 +000064 DOTGraphTraits (bool isSimple = false)
Tobias Grosserf96b0062010-07-22 07:46:31 +000065 : DOTGraphTraits<RegionNode*>(isSimple) {}
66
Michael Kruse6b236512015-08-10 12:57:23 +000067 static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
Tobias Grosserf96b0062010-07-22 07:46:31 +000068
Michael Kruse6b236512015-08-10 12:57:23 +000069 std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
70 return DOTGraphTraits<RegionNode *>::getNodeLabel(
71 Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
Tobias Grosserf96b0062010-07-22 07:46:31 +000072 }
73
Tobias Grosser3091c922011-02-27 04:11:07 +000074 std::string getEdgeAttributes(RegionNode *srcNode,
Michael Kruse6b236512015-08-10 12:57:23 +000075 GraphTraits<RegionInfo *>::ChildIteratorType CI,
76 RegionInfo *G) {
Tobias Grosser3091c922011-02-27 04:11:07 +000077 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
Michael Kruse6b236512015-08-10 12:57:23 +000086 Region *R = G->getRegionFor(destBB);
Tobias Grosser3091c922011-02-27 04:11:07 +000087
88 while (R && R->getParent())
89 if (R->getParent()->getEntry() == destBB)
90 R = R->getParent();
91 else
92 break;
93
Michael Kruse6b236512015-08-10 12:57:23 +000094 if (R && R->getEntry() == destBB && R->contains(srcBB))
Tobias Grosser3091c922011-02-27 04:11:07 +000095 return "constraint=false";
96
97 return "";
98 }
99
Tobias Grosserf96b0062010-07-22 07:46:31 +0000100 // Print the cluster of the subregions. This groups the single basic blocks
101 // and adds a different background color for each group.
Michael Kruse6b236512015-08-10 12:57:23 +0000102 static void printRegionCluster(const Region &R, GraphWriter<RegionInfo *> &GW,
Tobias Grosserf96b0062010-07-22 07:46:31 +0000103 unsigned depth = 0) {
104 raw_ostream &O = GW.getOStream();
David Blaikie2bbc5a72014-04-15 18:32:43 +0000105 O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000106 << " {\n";
107 O.indent(2 * (depth + 1)) << "label = \"\";\n";
108
David Blaikie2bbc5a72014-04-15 18:32:43 +0000109 if (!onlySimpleRegions || R.isSimple()) {
Tobias Grosserf96b0062010-07-22 07:46:31 +0000110 O.indent(2 * (depth + 1)) << "style = filled;\n";
111 O.indent(2 * (depth + 1)) << "color = "
David Blaikie2bbc5a72014-04-15 18:32:43 +0000112 << ((R.getDepth() * 2 % 12) + 1) << "\n";
Tobias Grosserf96b0062010-07-22 07:46:31 +0000113
114 } else {
115 O.indent(2 * (depth + 1)) << "style = solid;\n";
116 O.indent(2 * (depth + 1)) << "color = "
David Blaikie2bbc5a72014-04-15 18:32:43 +0000117 << ((R.getDepth() * 2 % 12) + 2) << "\n";
Tobias Grosserf96b0062010-07-22 07:46:31 +0000118 }
119
Benjamin Kramer8d0d2b62016-06-26 17:27:42 +0000120 for (const auto &RI : R)
121 printRegionCluster(*RI, GW, depth + 1);
Tobias Grosserf96b0062010-07-22 07:46:31 +0000122
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000123 const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
Tobias Grosserf96b0062010-07-22 07:46:31 +0000124
Richard Trieuf4222622015-04-15 21:40:50 +0000125 for (auto *BB : R.blocks())
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000126 if (RI.getRegionFor(BB) == &R)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000127 O.indent(2 * (depth + 1)) << "Node"
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000128 << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
Tobias Grosserf96b0062010-07-22 07:46:31 +0000129 << ";\n";
Tobias Grosserf96b0062010-07-22 07:46:31 +0000130
131 O.indent(2 * depth) << "}\n";
132 }
133
Michael Kruse6b236512015-08-10 12:57:23 +0000134 static void addCustomGraphFeatures(const RegionInfo *G,
135 GraphWriter<RegionInfo *> &GW) {
Tobias Grosserf96b0062010-07-22 07:46:31 +0000136 raw_ostream &O = GW.getOStream();
137 O << "\tcolorscheme = \"paired12\"\n";
Michael Kruse6b236512015-08-10 12:57:23 +0000138 printRegionCluster(*G->getTopLevelRegion(), GW, 4);
Tobias Grosserf96b0062010-07-22 07:46:31 +0000139 }
140};
141} //end namespace llvm
142
143namespace {
144
Michael Kruse6b236512015-08-10 12:57:23 +0000145struct RegionInfoPassGraphTraits {
146 static RegionInfo *getGraph(RegionInfoPass *RIP) {
147 return &RIP->getRegionInfo();
148 }
149};
150
151struct RegionPrinter
152 : public DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
153 RegionInfoPassGraphTraits> {
Tobias Grosserf96b0062010-07-22 07:46:31 +0000154 static char ID;
Michael Kruse6b236512015-08-10 12:57:23 +0000155 RegionPrinter()
156 : DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
157 RegionInfoPassGraphTraits>("reg", ID) {
158 initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
159 }
160};
161char RegionPrinter::ID = 0;
162
163struct RegionOnlyPrinter
164 : public DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
165 RegionInfoPassGraphTraits> {
166 static char ID;
167 RegionOnlyPrinter()
168 : DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
169 RegionInfoPassGraphTraits>("reg", ID) {
170 initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
171 }
172};
173char RegionOnlyPrinter::ID = 0;
174
175struct RegionViewer
176 : public DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
177 RegionInfoPassGraphTraits> {
178 static char ID;
179 RegionViewer()
180 : DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
181 RegionInfoPassGraphTraits>("reg", ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000182 initializeRegionViewerPass(*PassRegistry::getPassRegistry());
183 }
Tobias Grosserf96b0062010-07-22 07:46:31 +0000184};
Tobias Grosserf96b0062010-07-22 07:46:31 +0000185char RegionViewer::ID = 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000186
187struct RegionOnlyViewer
Michael Kruse6b236512015-08-10 12:57:23 +0000188 : public DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
189 RegionInfoPassGraphTraits> {
Tobias Grosserf96b0062010-07-22 07:46:31 +0000190 static char ID;
Michael Kruse6b236512015-08-10 12:57:23 +0000191 RegionOnlyViewer()
192 : DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
193 RegionInfoPassGraphTraits>("regonly", ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000194 initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
195 }
Tobias Grosserf96b0062010-07-22 07:46:31 +0000196};
Tobias Grosserf96b0062010-07-22 07:46:31 +0000197char RegionOnlyViewer::ID = 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000198
Tobias Grosserf96b0062010-07-22 07:46:31 +0000199} //end anonymous namespace
200
Tobias Grosserf96b0062010-07-22 07:46:31 +0000201INITIALIZE_PASS(RegionPrinter, "dot-regions",
Owen Andersonce665bd2010-10-07 22:25:06 +0000202 "Print regions of function to 'dot' file", true, true)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000203
Michael Kruse6b236512015-08-10 12:57:23 +0000204INITIALIZE_PASS(
205 RegionOnlyPrinter, "dot-regions-only",
206 "Print regions of function to 'dot' file (with no function bodies)", true,
207 true)
208
Owen Anderson71802342010-10-07 04:13:08 +0000209INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
Owen Andersonce665bd2010-10-07 22:25:06 +0000210 true, true)
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000211
Owen Anderson71802342010-10-07 04:13:08 +0000212INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
213 "View regions of function (with no function bodies)",
Owen Andersonce665bd2010-10-07 22:25:06 +0000214 true, true)
Owen Anderson71802342010-10-07 04:13:08 +0000215
Michael Kruse6b236512015-08-10 12:57:23 +0000216FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
Dan Gohman811edc12010-08-02 18:50:06 +0000217
Michael Kruse6b236512015-08-10 12:57:23 +0000218FunctionPass *llvm::createRegionOnlyPrinterPass() {
219 return new RegionOnlyPrinter();
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +0000220}
Dan Gohman811edc12010-08-02 18:50:06 +0000221
Tobias Grosserf96b0062010-07-22 07:46:31 +0000222FunctionPass* llvm::createRegionViewerPass() {
223 return new RegionViewer();
224}
225
226FunctionPass* llvm::createRegionOnlyViewerPass() {
227 return new RegionOnlyViewer();
228}
229
Michael Kruse5864ac72015-08-10 13:21:59 +0000230#ifndef NDEBUG
231static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
232 assert(RI && "Argument must be non-null");
233
234 llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
235 std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
236
237 llvm::ViewGraph(RI, "reg", ShortNames,
238 Twine(GraphName) + " for '" + F->getName() + "' function");
239}
240
241static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
242 assert(F && "Argument must be non-null");
243 assert(!F->isDeclaration() && "Function must have an implementation");
244
245 // The viewer and analysis passes do not modify anything, so we can safely
246 // remove the const qualifier
247 auto NonConstF = const_cast<Function *>(F);
248
249 llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
250 FPM.add(ViewerPass);
251 FPM.doInitialization();
252 FPM.run(*NonConstF);
253 FPM.doFinalization();
254}
255
256void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
257
258void llvm::viewRegion(const Function *F) {
259 invokeFunctionPass(F, createRegionViewerPass());
260}
261
262void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }
263
264void llvm::viewRegionOnly(const Function *F) {
265 invokeFunctionPass(F, createRegionOnlyViewerPass());
266}
267#endif