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"
24 #include "llvm/IR/LegacyPassManager.h"
29 //===----------------------------------------------------------------------===//
30 /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
32 onlySimpleRegions("only-simple-regions",
33 cl::desc("Show only simple regions in the graphviz viewer"),
39 struct DOTGraphTraits<RegionNode*> : public DefaultDOTGraphTraits {
41 DOTGraphTraits (bool isSimple=false)
42 : DefaultDOTGraphTraits(isSimple) {}
44 std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
46 if (!Node->isSubRegion()) {
47 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
50 return DOTGraphTraits<const Function*>
51 ::getSimpleNodeLabel(BB, BB->getParent());
53 return DOTGraphTraits<const Function*>
54 ::getCompleteNodeLabel(BB, BB->getParent());
57 return "Not implemented";
62 struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> {
64 DOTGraphTraits (bool isSimple = false)
65 : DOTGraphTraits<RegionNode*>(isSimple) {}
67 static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
69 std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
70 return DOTGraphTraits<RegionNode *>::getNodeLabel(
71 Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
74 std::string getEdgeAttributes(RegionNode *srcNode,
75 GraphTraits<RegionInfo *>::ChildIteratorType CI,
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 = G->getRegionFor(destBB);
88 while (R && R->getParent())
89 if (R->getParent()->getEntry() == destBB)
94 if (R && 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, GraphWriter<RegionInfo *> &GW,
103 unsigned depth = 0) {
104 raw_ostream &O = GW.getOStream();
105 O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
107 O.indent(2 * (depth + 1)) << "label = \"\";\n";
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";
115 O.indent(2 * (depth + 1)) << "style = solid;\n";
116 O.indent(2 * (depth + 1)) << "color = "
117 << ((R.getDepth() * 2 % 12) + 2) << "\n";
120 for (Region::const_iterator RI = R.begin(), RE = R.end(); RI != RE; ++RI)
121 printRegionCluster(**RI, GW, depth + 1);
123 const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
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))
131 O.indent(2 * depth) << "}\n";
134 static void addCustomGraphFeatures(const RegionInfo *G,
135 GraphWriter<RegionInfo *> &GW) {
136 raw_ostream &O = GW.getOStream();
137 O << "\tcolorscheme = \"paired12\"\n";
138 printRegionCluster(*G->getTopLevelRegion(), GW, 4);
141 } //end namespace llvm
145 struct RegionInfoPassGraphTraits {
146 static RegionInfo *getGraph(RegionInfoPass *RIP) {
147 return &RIP->getRegionInfo();
152 : public DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
153 RegionInfoPassGraphTraits> {
156 : DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
157 RegionInfoPassGraphTraits>("reg", ID) {
158 initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
161 char RegionPrinter::ID = 0;
163 struct RegionOnlyPrinter
164 : public DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
165 RegionInfoPassGraphTraits> {
168 : DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
169 RegionInfoPassGraphTraits>("reg", ID) {
170 initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
173 char RegionOnlyPrinter::ID = 0;
176 : public DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
177 RegionInfoPassGraphTraits> {
180 : DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
181 RegionInfoPassGraphTraits>("reg", ID) {
182 initializeRegionViewerPass(*PassRegistry::getPassRegistry());
185 char RegionViewer::ID = 0;
187 struct RegionOnlyViewer
188 : public DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
189 RegionInfoPassGraphTraits> {
192 : DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
193 RegionInfoPassGraphTraits>("regonly", ID) {
194 initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
197 char RegionOnlyViewer::ID = 0;
199 } //end anonymous namespace
201 INITIALIZE_PASS(RegionPrinter, "dot-regions",
202 "Print regions of function to 'dot' file", true, true)
205 RegionOnlyPrinter, "dot-regions-only",
206 "Print regions of function to 'dot' file (with no function bodies)", true,
209 INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
212 INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
213 "View regions of function (with no function bodies)",
216 FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
218 FunctionPass *llvm::createRegionOnlyPrinterPass() {
219 return new RegionOnlyPrinter();
222 FunctionPass* llvm::createRegionViewerPass() {
223 return new RegionViewer();
226 FunctionPass* llvm::createRegionOnlyViewerPass() {
227 return new RegionOnlyViewer();
231 static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
232 assert(RI && "Argument must be non-null");
234 llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
235 std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
237 llvm::ViewGraph(RI, "reg", ShortNames,
238 Twine(GraphName) + " for '" + F->getName() + "' function");
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");
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);
249 llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
251 FPM.doInitialization();
253 FPM.doFinalization();
256 void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
258 void llvm::viewRegion(const Function *F) {
259 invokeFunctionPass(F, createRegionViewerPass());
262 void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }
264 void llvm::viewRegionOnly(const Function *F) {
265 invokeFunctionPass(F, createRegionOnlyViewerPass());