LLVM  16.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 
16 #include "llvm/InitializePasses.h"
19 #ifndef NDEBUG
21 #endif
22 
23 using namespace llvm;
24 
25 //===----------------------------------------------------------------------===//
26 /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
27 static cl::opt<bool>
28 onlySimpleRegions("only-simple-regions",
29  cl::desc("Show only simple regions in the graphviz viewer"),
30  cl::Hidden,
31  cl::init(false));
32 
33 namespace llvm {
34 
36  RegionNode *Graph) {
37  if (!Node->isSubRegion()) {
38  BasicBlock *BB = Node->getNodeAs<BasicBlock>();
39 
40  if (isSimple())
42  else
44  }
45 
46  return "Not implemented";
47 }
48 
49 template <>
51 
52  DOTGraphTraits (bool isSimple = false)
54 
55  static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
56 
57  std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
59  Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
60  }
61 
62  std::string getEdgeAttributes(RegionNode *srcNode,
64  RegionInfo *G) {
65  RegionNode *destNode = *CI;
66 
67  if (srcNode->isSubRegion() || destNode->isSubRegion())
68  return "";
69 
70  // In case of a backedge, do not use it to define the layout of the nodes.
71  BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
72  BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
73 
74  Region *R = G->getRegionFor(destBB);
75 
76  while (R && R->getParent())
77  if (R->getParent()->getEntry() == destBB)
78  R = R->getParent();
79  else
80  break;
81 
82  if (R && R->getEntry() == destBB && R->contains(srcBB))
83  return "constraint=false";
84 
85  return "";
86  }
87 
88  // Print the cluster of the subregions. This groups the single basic blocks
89  // and adds a different background color for each group.
91  unsigned depth = 0) {
92  raw_ostream &O = GW.getOStream();
93  O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
94  << " {\n";
95  O.indent(2 * (depth + 1)) << "label = \"\";\n";
96 
97  if (!onlySimpleRegions || R.isSimple()) {
98  O.indent(2 * (depth + 1)) << "style = filled;\n";
99  O.indent(2 * (depth + 1)) << "color = "
100  << ((R.getDepth() * 2 % 12) + 1) << "\n";
101 
102  } else {
103  O.indent(2 * (depth + 1)) << "style = solid;\n";
104  O.indent(2 * (depth + 1)) << "color = "
105  << ((R.getDepth() * 2 % 12) + 2) << "\n";
106  }
107 
108  for (const auto &RI : R)
109  printRegionCluster(*RI, GW, depth + 1);
110 
111  const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
112 
113  for (auto *BB : R.blocks())
114  if (RI.getRegionFor(BB) == &R)
115  O.indent(2 * (depth + 1)) << "Node"
116  << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
117  << ";\n";
118 
119  O.indent(2 * depth) << "}\n";
120  }
121 
122  static void addCustomGraphFeatures(const RegionInfo *G,
124  raw_ostream &O = GW.getOStream();
125  O << "\tcolorscheme = \"paired12\"\n";
126  printRegionCluster(*G->getTopLevelRegion(), GW, 4);
127  }
128 };
129 } // end namespace llvm
130 
131 namespace {
132 
133 struct RegionInfoPassGraphTraits {
134  static RegionInfo *getGraph(RegionInfoPass *RIP) {
135  return &RIP->getRegionInfo();
136  }
137 };
138 
139 struct RegionPrinter
141  RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
142  static char ID;
143  RegionPrinter()
145  RegionInfoPassGraphTraits>("reg", ID) {
146  initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
147  }
148 };
149 char RegionPrinter::ID = 0;
150 
151 struct RegionOnlyPrinter
153  RegionInfoPass, true, RegionInfo *, RegionInfoPassGraphTraits> {
154  static char ID;
155  RegionOnlyPrinter()
157  RegionInfoPassGraphTraits>("reg", ID) {
158  initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
159  }
160 };
161 char RegionOnlyPrinter::ID = 0;
162 
163 struct RegionViewer
165  RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {
166  static char ID;
167  RegionViewer()
169  RegionInfoPassGraphTraits>("reg", ID) {
170  initializeRegionViewerPass(*PassRegistry::getPassRegistry());
171  }
172 };
173 char RegionViewer::ID = 0;
174 
175 struct RegionOnlyViewer
176  : public DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
177  RegionInfoPassGraphTraits> {
178  static char ID;
179  RegionOnlyViewer()
181  RegionInfoPassGraphTraits>("regonly",
182  ID) {
183  initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
184  }
185 };
186 char RegionOnlyViewer::ID = 0;
187 
188 } //end anonymous namespace
189 
190 INITIALIZE_PASS(RegionPrinter, "dot-regions",
191  "Print regions of function to 'dot' file", true, true)
192 
194  RegionOnlyPrinter, "dot-regions-only",
195  "Print regions of function to 'dot' file (with no function bodies)", true,
196  true)
197 
198 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
199  true, true)
200 
201 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
202  "View regions of function (with no function bodies)",
203  true, true)
204 
205 FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
206 
208  return new RegionOnlyPrinter();
209 }
210 
212  return new RegionViewer();
213 }
214 
216  return new RegionOnlyViewer();
217 }
218 
219 #ifndef NDEBUG
220 static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
221  assert(RI && "Argument must be non-null");
222 
223  llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
224  std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
225 
226  llvm::ViewGraph(RI, "reg", ShortNames,
227  Twine(GraphName) + " for '" + F->getName() + "' function");
228 }
229 
230 static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
231  assert(F && "Argument must be non-null");
232  assert(!F->isDeclaration() && "Function must have an implementation");
233 
234  // The viewer and analysis passes do not modify anything, so we can safely
235  // remove the const qualifier
236  auto NonConstF = const_cast<Function *>(F);
237 
238  llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
239  FPM.add(ViewerPass);
240  FPM.doInitialization();
241  FPM.run(*NonConstF);
242  FPM.doFinalization();
243 }
244 
245 void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
246 
249 }
250 
252 
255 }
256 #endif
RegionInfo.h
llvm::initializeRegionOnlyPrinterPass
void initializeRegionOnlyPrinterPass(PassRegistry &)
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::createRegionOnlyPrinterPass
FunctionPass * createRegionOnlyPrinterPass()
Definition: RegionPrinter.cpp:207
llvm::GraphWriter
Definition: ScheduleDAG.h:34
llvm::RegionInfoBase::getTopLevelRegion
RegionT * getTopLevelRegion() const
Definition: RegionInfo.h:866
llvm::Function
Definition: Function.h:60
llvm::DOTGraphTraits< RegionInfo * >::DOTGraphTraits
DOTGraphTraits(bool isSimple=false)
Definition: RegionPrinter.cpp:52
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
to
Should compile to
Definition: README.txt:449
llvm::DOTGraphTraits< RegionNode * >
Definition: RegionPrinter.h:31
llvm::DOTGraphTraits< RegionInfo * >::getNodeLabel
std::string getNodeLabel(RegionNode *Node, RegionInfo *G)
Definition: RegionPrinter.cpp:57
llvm::GraphWriter::getOStream
raw_ostream & getOStream()
getOStream - Get the raw output stream into the graph file.
Definition: GraphWriter.h:353
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:427
RegionPrinter.h
LegacyPassManager.h
llvm::RegionInfoPass::getRegionInfo
RegionInfo & getRegionInfo()
Definition: RegionInfo.h:951
DepthFirstIterator.h
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:265
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::initializeRegionOnlyViewerPass
void initializeRegionOnlyViewerPass(PassRegistry &)
CommandLine.h
llvm::legacy::FunctionPassManager::doFinalization
bool doFinalization()
doFinalization - Run all of the finalizers for the function passes.
Definition: LegacyPassManager.cpp:1359
regions
regions
Definition: RegionInfo.cpp:167
viewRegionInfo
static void viewRegionInfo(RegionInfo *RI, bool ShortNames)
Definition: RegionPrinter.cpp:220
false
Definition: StackSlotColoring.cpp:141
llvm::DOTGraphTraits< RegionInfo * >::printRegionCluster
static void printRegionCluster(const Region &R, GraphWriter< RegionInfo * > &GW, unsigned depth=0)
Definition: RegionPrinter.cpp:90
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::initializeRegionViewerPass
void initializeRegionViewerPass(PassRegistry &)
only
dot regions only
Definition: RegionPrinter.cpp:194
true
dot regions Print regions of function to dot true
Definition: RegionPrinter.cpp:195
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
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:166
llvm::RegionInfoBase::getRegionFor
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
Definition: RegionInfoImpl.h:815
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::cl::opt< bool >
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
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:230
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:440
llvm::legacy::FunctionPassManager::doInitialization
bool doInitialization()
doInitialization - Run all of the initializers for the function passes.
Definition: LegacyPassManager.cpp:1353
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:1335
llvm::RegionInfoPass
Definition: RegionInfo.h:942
llvm::DOTGraphTraits< RegionInfo * >::getGraphName
static std::string getGraphName(const RegionInfo *)
Definition: RegionPrinter.cpp:55
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:211
llvm::DefaultDOTGraphTraits::isSimple
bool isSimple()
Definition: DOTGraphTraits.h:33
llvm::rdf::Print
Print(const T &, const DataFlowGraph &) -> Print< T >
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:81
llvm::legacy::FunctionPassManager::run
bool run(Function &F)
run - Execute all of the passes scheduled for execution.
Definition: LegacyPassManager.cpp:1343
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:717
llvm::viewRegion
void viewRegion(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
Definition: RegionPrinter.cpp:245
llvm::createRegionOnlyViewerPass
FunctionPass * createRegionOnlyViewerPass()
Definition: RegionPrinter.cpp:215
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:76
llvm::DOTGraphTraits< RegionInfo * >::getEdgeAttributes
std::string getEdgeAttributes(RegionNode *srcNode, GraphTraits< RegionInfo * >::ChildIteratorType CI, RegionInfo *G)
Definition: RegionPrinter.cpp:62
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
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:37
llvm::createRegionPrinterPass
FunctionPass * createRegionPrinterPass()
Definition: RegionPrinter.cpp:205
llvm::cl::desc
Definition: CommandLine.h:413
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.
llvm::DOTGraphTraitsViewerWrapperPass
Definition: DOTGraphTraitsPass.h:164
InitializePasses.h
llvm::viewRegionOnly
void viewRegionOnly(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
Definition: RegionPrinter.cpp:251
llvm::DOTGraphTraitsPrinterWrapperPass
Definition: DOTGraphTraitsPass.h:203
llvm::DOTGraphTraits< RegionInfo * >::addCustomGraphFeatures
static void addCustomGraphFeatures(const RegionInfo *G, GraphWriter< RegionInfo * > &GW)
Definition: RegionPrinter.cpp:122
of
Add support for conditional and other related patterns Instead of
Definition: README.txt:134
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
INITIALIZE_PASS
TargetPassConfig.
Definition: TargetPassConfig.cpp:367