LLVM  14.0.0git
RegionPrinter.cpp
Go to the documentation of this file.
1 //===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // Print out the region tree of a function using dotty/graphviz.
9 //===----------------------------------------------------------------------===//
10 
14 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Analysis/Passes.h"
19 #include "llvm/InitializePasses.h"
21 #include "llvm/Support/Debug.h"
23 #ifndef NDEBUG
25 #endif
26 
27 using namespace llvm;
28 
29 //===----------------------------------------------------------------------===//
30 /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
31 static cl::opt<bool>
32 onlySimpleRegions("only-simple-regions",
33  cl::desc("Show only simple regions in the graphviz viewer"),
34  cl::Hidden,
35  cl::init(false));
36 
37 namespace llvm {
38 template<>
40 
41  DOTGraphTraits (bool isSimple=false)
43 
44  std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
45 
46  if (!Node->isSubRegion()) {
47  BasicBlock *BB = Node->getNodeAs<BasicBlock>();
48 
49  if (isSimple())
51  ::getSimpleNodeLabel(BB, nullptr);
52  else
54  ::getCompleteNodeLabel(BB, nullptr);
55  }
56 
57  return "Not implemented";
58  }
59 };
60 
61 template <>
63 
64  DOTGraphTraits (bool isSimple = false)
66 
67  static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
68 
69  std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
71  Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
72  }
73 
74  std::string getEdgeAttributes(RegionNode *srcNode,
76  RegionInfo *G) {
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 = G->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 && 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.
103  unsigned depth = 0) {
104  raw_ostream &O = GW.getOStream();
105  O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
106  << " {\n";
107  O.indent(2 * (depth + 1)) << "label = \"\";\n";
108 
109  if (!onlySimpleRegions || R.isSimple()) {
110  O.indent(2 * (depth + 1)) << "style = filled;\n";
111  O.indent(2 * (depth + 1)) << "color = "
112  << ((R.getDepth() * 2 % 12) + 1) << "\n";
113 
114  } else {
115  O.indent(2 * (depth + 1)) << "style = solid;\n";
116  O.indent(2 * (depth + 1)) << "color = "
117  << ((R.getDepth() * 2 % 12) + 2) << "\n";
118  }
119 
120  for (const auto &RI : R)
121  printRegionCluster(*RI, GW, depth + 1);
122 
123  const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
124 
125  for (auto *BB : R.blocks())
126  if (RI.getRegionFor(BB) == &R)
127  O.indent(2 * (depth + 1)) << "Node"
128  << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
129  << ";\n";
130 
131  O.indent(2 * depth) << "}\n";
132  }
133 
134  static void addCustomGraphFeatures(const RegionInfo *G,
136  raw_ostream &O = GW.getOStream();
137  O << "\tcolorscheme = \"paired12\"\n";
138  printRegionCluster(*G->getTopLevelRegion(), GW, 4);
139  }
140 };
141 } //end namespace llvm
142 
143 namespace {
144 
145 struct RegionInfoPassGraphTraits {
146  static RegionInfo *getGraph(RegionInfoPass *RIP) {
147  return &RIP->getRegionInfo();
148  }
149 };
150 
151 struct RegionPrinter
152  : public DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
153  RegionInfoPassGraphTraits> {
154  static char ID;
155  RegionPrinter()
157  RegionInfoPassGraphTraits>("reg", ID) {
159  }
160 };
161 char RegionPrinter::ID = 0;
162 
163 struct RegionOnlyPrinter
164  : public DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
165  RegionInfoPassGraphTraits> {
166  static char ID;
167  RegionOnlyPrinter()
169  RegionInfoPassGraphTraits>("reg", ID) {
171  }
172 };
173 char RegionOnlyPrinter::ID = 0;
174 
175 struct RegionViewer
176  : public DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
177  RegionInfoPassGraphTraits> {
178  static char ID;
179  RegionViewer()
181  RegionInfoPassGraphTraits>("reg", ID) {
183  }
184 };
185 char RegionViewer::ID = 0;
186 
187 struct RegionOnlyViewer
188  : public DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
189  RegionInfoPassGraphTraits> {
190  static char ID;
191  RegionOnlyViewer()
193  RegionInfoPassGraphTraits>("regonly", ID) {
195  }
196 };
197 char RegionOnlyViewer::ID = 0;
198 
199 } //end anonymous namespace
200 
201 INITIALIZE_PASS(RegionPrinter, "dot-regions",
202  "Print regions of function to 'dot' file", true, true)
203 
205  RegionOnlyPrinter, "dot-regions-only",
206  "Print regions of function to 'dot' file (with no function bodies)", true,
207  true)
208 
209 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
210  true, true)
211 
212 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
213  "View regions of function (with no function bodies)",
214  true, true)
215 
216 FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
217 
219  return new RegionOnlyPrinter();
220 }
221 
223  return new RegionViewer();
224 }
225 
227  return new RegionOnlyViewer();
228 }
229 
230 #ifndef NDEBUG
231 static 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 
241 static 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 
256 void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
257 
260 }
261 
263 
266 }
267 #endif
RegionInfo.h
llvm::DOTGraphTraits< RegionNode * >::DOTGraphTraits
DOTGraphTraits(bool isSimple=false)
Definition: RegionPrinter.cpp:41
llvm::initializeRegionOnlyPrinterPass
void initializeRegionOnlyPrinterPass(PassRegistry &)
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::createRegionOnlyPrinterPass
FunctionPass * createRegionOnlyPrinterPass()
Definition: RegionPrinter.cpp:218
llvm::GraphWriter
Definition: ScheduleDAG.h:34
llvm::RegionInfoBase::getTopLevelRegion
RegionT * getTopLevelRegion() const
Definition: RegionInfo.h:866
llvm::Function
Definition: Function.h:61
Statistic.h
llvm::DOTGraphTraits< RegionInfo * >::DOTGraphTraits
DOTGraphTraits(bool isSimple=false)
Definition: RegionPrinter.cpp:64
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
to
Should compile to
Definition: README.txt:449
llvm::DOTGraphTraits< RegionNode * >
Definition: RegionPrinter.cpp:39
llvm::DOTGraphTraits< RegionInfo * >::getNodeLabel
std::string getNodeLabel(RegionNode *Node, RegionInfo *G)
Definition: RegionPrinter.cpp:69
llvm::GraphWriter::getOStream
raw_ostream & getOStream()
getOStream - Get the raw output stream into the graph file.
Definition: GraphWriter.h:301
llvm::ViewGraph
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
Definition: GraphWriter.h:375
RegionPrinter.h
LegacyPassManager.h
llvm::RegionInfoPass::getRegionInfo
RegionInfo & getRegionInfo()
Definition: RegionInfo.h:951
DepthFirstIterator.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:193
llvm::DOTGraphTraitsViewer
Definition: DOTGraphTraitsPass.h:31
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::initializeRegionOnlyViewerPass
void initializeRegionOnlyViewerPass(PassRegistry &)
CommandLine.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::legacy::FunctionPassManager::doFinalization
bool doFinalization()
doFinalization - Run all of the finalizers for the function passes.
Definition: LegacyPassManager.cpp:1368
regions
regions
Definition: RegionInfo.cpp:166
llvm::DOTGraphTraitsPrinter
Definition: DOTGraphTraitsPass.h:73
viewRegionInfo
static void viewRegionInfo(RegionInfo *RI, bool ShortNames)
Definition: RegionPrinter.cpp:231
false
Definition: StackSlotColoring.cpp:142
llvm::DOTGraphTraits< RegionInfo * >::printRegionCluster
static void printRegionCluster(const Region &R, GraphWriter< RegionInfo * > &GW, unsigned depth=0)
Definition: RegionPrinter.cpp:102
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::initializeRegionViewerPass
void initializeRegionViewerPass(PassRegistry &)
only
dot regions only
Definition: RegionPrinter.cpp:205
true
dot regions Print regions of function to dot true
Definition: RegionPrinter.cpp:206
llvm::RegionNode
Definition: RegionInfo.h:879
llvm::DOTGraphTraits
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
Definition: DOTGraphTraits.h:161
llvm::RegionInfoBase::getRegionFor
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
Definition: RegionInfoImpl.h:817
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
llvm::cl::opt< bool >
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:192
file
dot regions Print regions of function to dot file(with no function bodies)"
llvm::RegionNodeBase::getNodeAs
T * getNodeAs() const
Get the content of this RegionNode.
invokeFunctionPass
static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass)
Definition: RegionPrinter.cpp:241
llvm::DefaultDOTGraphTraits::getGraphName
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
Definition: DOTGraphTraits.h:44
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
llvm::legacy::FunctionPassManager::doInitialization
bool doInitialization()
doInitialization - Run all of the initializers for the function passes.
Definition: LegacyPassManager.cpp:1362
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RegionIterator.h
llvm::legacy::FunctionPassManager::add
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: LegacyPassManager.cpp:1344
llvm::DOTGraphTraits< RegionNode * >::getNodeLabel
std::string getNodeLabel(RegionNode *Node, RegionNode *Graph)
Definition: RegionPrinter.cpp:44
llvm::RegionInfoPass
Definition: RegionInfo.h:942
llvm::DOTGraphTraits< RegionInfo * >::getGraphName
static std::string getGraphName(const RegionInfo *)
Definition: RegionPrinter.cpp:67
llvm::initializeRegionPrinterPass
void initializeRegionPrinterPass(PassRegistry &)
llvm::RegionNodeBase::isSubRegion
bool isSubRegion() const
Is this RegionNode a subregion?
Definition: RegionInfo.h:188
llvm::createRegionViewerPass
FunctionPass * createRegionViewerPass()
Definition: RegionPrinter.cpp:222
llvm::RegionInfo
Definition: RegionInfo.h:900
function
dot regions Print regions of function to dot true view regions View regions of function(with no function bodies)"
llvm::tgtok::dot
@ dot
Definition: TGLexer.h:43
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::legacy::FunctionPassManager::run
bool run(Function &F)
run - Execute all of the passes scheduled for execution.
Definition: LegacyPassManager.cpp:1352
llvm::DOTGraphTraits< DOTFuncInfo * >
Definition: CFGPrinter.h:123
DOTGraphTraitsPass.h
INITIALIZE_PASS
INITIALIZE_PASS(RegionPrinter, "dot-regions", "Print regions of function to 'dot' file", true, true) INITIALIZE_PASS(RegionOnlyPrinter
isSimple
static bool isSimple(Instruction *I)
Definition: SLPVectorizer.cpp:529
llvm::viewRegion
void viewRegion(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
Definition: RegionPrinter.cpp:256
PostOrderIterator.h
llvm::createRegionOnlyViewerPass
FunctionPass * createRegionOnlyViewerPass()
Definition: RegionPrinter.cpp:226
llvm::legacy::FunctionPassManager
FunctionPassManager manages FunctionPasses.
Definition: LegacyPassManager.h:71
llvm::DefaultDOTGraphTraits::getNodeLabel
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...
Definition: DOTGraphTraits.h:71
llvm::DOTGraphTraits< RegionInfo * >::getEdgeAttributes
std::string getEdgeAttributes(RegionNode *srcNode, GraphTraits< RegionInfo * >::ChildIteratorType CI, RegionInfo *G)
Definition: RegionPrinter.cpp:74
llvm::DefaultDOTGraphTraits
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
Definition: DOTGraphTraits.h:28
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::GraphTraits
Definition: GraphTraits.h:35
llvm::createRegionPrinterPass
FunctionPass * createRegionPrinterPass()
Definition: RegionPrinter.cpp:216
llvm::cl::desc
Definition: CommandLine.h:414
llvm::Region
Definition: RegionInfo.h:889
raw_ostream.h
onlySimpleRegions
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.
InitializePasses.h
Debug.h
llvm::viewRegionOnly
void viewRegionOnly(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
Definition: RegionPrinter.cpp:262
llvm::DOTGraphTraits< RegionInfo * >::addCustomGraphFeatures
static void addCustomGraphFeatures(const RegionInfo *G, GraphWriter< RegionInfo * > &GW)
Definition: RegionPrinter.cpp:134
of
Add support for conditional and other related patterns Instead of
Definition: README.txt:134
Passes.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37
INITIALIZE_PASS
TargetPassConfig.
Definition: TargetPassConfig.cpp:359