2010-07-22 09:46:31 +02:00
|
|
|
//===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-07-22 09:46:31 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Print out the region tree of a function using dotty/graphviz.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/Analysis/RegionPrinter.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/DOTGraphTraitsPass.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2010-07-22 09:46:31 +02:00
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2010-07-22 09:46:31 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-07-22 09:46:31 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-08-10 15:21:59 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
|
|
|
#endif
|
2010-07-22 09:46:31 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// onlySimpleRegion - Show only the simple regions in the RegionViewer.
|
|
|
|
static cl::opt<bool>
|
|
|
|
onlySimpleRegions("only-simple-regions",
|
|
|
|
cl::desc("Show only simple regions in the graphviz viewer"),
|
|
|
|
cl::Hidden,
|
|
|
|
cl::init(false));
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<RegionNode*> : public DefaultDOTGraphTraits {
|
|
|
|
|
|
|
|
DOTGraphTraits (bool isSimple=false)
|
|
|
|
: DefaultDOTGraphTraits(isSimple) {}
|
|
|
|
|
|
|
|
std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
|
|
|
|
|
|
|
|
if (!Node->isSubRegion()) {
|
|
|
|
BasicBlock *BB = Node->getNodeAs<BasicBlock>();
|
|
|
|
|
|
|
|
if (isSimple())
|
2020-04-06 18:53:10 +02:00
|
|
|
return DOTGraphTraits<DOTFuncInfo *>
|
|
|
|
::getSimpleNodeLabel(BB, nullptr);
|
2010-07-22 09:46:31 +02:00
|
|
|
else
|
2020-04-06 18:53:10 +02:00
|
|
|
return DOTGraphTraits<DOTFuncInfo *>
|
|
|
|
::getCompleteNodeLabel(BB, nullptr);
|
2010-07-22 09:46:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "Not implemented";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
template <>
|
|
|
|
struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> {
|
2010-07-22 09:46:31 +02:00
|
|
|
|
2014-07-19 20:29:29 +02:00
|
|
|
DOTGraphTraits (bool isSimple = false)
|
2010-07-22 09:46:31 +02:00
|
|
|
: DOTGraphTraits<RegionNode*>(isSimple) {}
|
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
|
2010-07-22 09:46:31 +02:00
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
|
|
|
|
return DOTGraphTraits<RegionNode *>::getNodeLabel(
|
|
|
|
Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
|
2010-07-22 09:46:31 +02:00
|
|
|
}
|
|
|
|
|
2011-02-27 05:11:07 +01:00
|
|
|
std::string getEdgeAttributes(RegionNode *srcNode,
|
2015-08-10 14:57:23 +02:00
|
|
|
GraphTraits<RegionInfo *>::ChildIteratorType CI,
|
|
|
|
RegionInfo *G) {
|
2011-02-27 05:11:07 +01:00
|
|
|
RegionNode *destNode = *CI;
|
|
|
|
|
|
|
|
if (srcNode->isSubRegion() || destNode->isSubRegion())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// In case of a backedge, do not use it to define the layout of the nodes.
|
|
|
|
BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
|
|
|
|
BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
|
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
Region *R = G->getRegionFor(destBB);
|
2011-02-27 05:11:07 +01:00
|
|
|
|
|
|
|
while (R && R->getParent())
|
|
|
|
if (R->getParent()->getEntry() == destBB)
|
|
|
|
R = R->getParent();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
if (R && R->getEntry() == destBB && R->contains(srcBB))
|
2011-02-27 05:11:07 +01:00
|
|
|
return "constraint=false";
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2010-07-22 09:46:31 +02:00
|
|
|
// Print the cluster of the subregions. This groups the single basic blocks
|
|
|
|
// and adds a different background color for each group.
|
2015-08-10 14:57:23 +02:00
|
|
|
static void printRegionCluster(const Region &R, GraphWriter<RegionInfo *> &GW,
|
2010-07-22 09:46:31 +02:00
|
|
|
unsigned depth = 0) {
|
|
|
|
raw_ostream &O = GW.getOStream();
|
2014-04-15 20:32:43 +02:00
|
|
|
O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
|
2010-07-22 09:46:31 +02:00
|
|
|
<< " {\n";
|
|
|
|
O.indent(2 * (depth + 1)) << "label = \"\";\n";
|
|
|
|
|
2014-04-15 20:32:43 +02:00
|
|
|
if (!onlySimpleRegions || R.isSimple()) {
|
2010-07-22 09:46:31 +02:00
|
|
|
O.indent(2 * (depth + 1)) << "style = filled;\n";
|
|
|
|
O.indent(2 * (depth + 1)) << "color = "
|
2014-04-15 20:32:43 +02:00
|
|
|
<< ((R.getDepth() * 2 % 12) + 1) << "\n";
|
2010-07-22 09:46:31 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
O.indent(2 * (depth + 1)) << "style = solid;\n";
|
|
|
|
O.indent(2 * (depth + 1)) << "color = "
|
2014-04-15 20:32:43 +02:00
|
|
|
<< ((R.getDepth() * 2 % 12) + 2) << "\n";
|
2010-07-22 09:46:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-26 19:27:42 +02:00
|
|
|
for (const auto &RI : R)
|
|
|
|
printRegionCluster(*RI, GW, depth + 1);
|
2010-07-22 09:46:31 +02:00
|
|
|
|
2014-07-19 20:29:29 +02:00
|
|
|
const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
|
2010-07-22 09:46:31 +02:00
|
|
|
|
2015-04-15 23:40:50 +02:00
|
|
|
for (auto *BB : R.blocks())
|
2014-07-19 20:29:29 +02:00
|
|
|
if (RI.getRegionFor(BB) == &R)
|
2010-07-22 09:46:31 +02:00
|
|
|
O.indent(2 * (depth + 1)) << "Node"
|
2014-07-19 20:29:29 +02:00
|
|
|
<< static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
|
2010-07-22 09:46:31 +02:00
|
|
|
<< ";\n";
|
|
|
|
|
|
|
|
O.indent(2 * depth) << "}\n";
|
|
|
|
}
|
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
static void addCustomGraphFeatures(const RegionInfo *G,
|
|
|
|
GraphWriter<RegionInfo *> &GW) {
|
2010-07-22 09:46:31 +02:00
|
|
|
raw_ostream &O = GW.getOStream();
|
|
|
|
O << "\tcolorscheme = \"paired12\"\n";
|
2015-08-10 14:57:23 +02:00
|
|
|
printRegionCluster(*G->getTopLevelRegion(), GW, 4);
|
2010-07-22 09:46:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} //end namespace llvm
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
struct RegionInfoPassGraphTraits {
|
|
|
|
static RegionInfo *getGraph(RegionInfoPass *RIP) {
|
|
|
|
return &RIP->getRegionInfo();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionPrinter
|
|
|
|
: public DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
|
|
|
|
RegionInfoPassGraphTraits> {
|
|
|
|
static char ID;
|
|
|
|
RegionPrinter()
|
|
|
|
: DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
|
|
|
|
RegionInfoPassGraphTraits>("reg", ID) {
|
|
|
|
initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
char RegionPrinter::ID = 0;
|
|
|
|
|
|
|
|
struct RegionOnlyPrinter
|
|
|
|
: public DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
|
|
|
|
RegionInfoPassGraphTraits> {
|
|
|
|
static char ID;
|
|
|
|
RegionOnlyPrinter()
|
|
|
|
: DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
|
|
|
|
RegionInfoPassGraphTraits>("reg", ID) {
|
|
|
|
initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
char RegionOnlyPrinter::ID = 0;
|
|
|
|
|
2010-07-22 09:46:31 +02:00
|
|
|
struct RegionViewer
|
2015-08-10 14:57:23 +02:00
|
|
|
: public DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
|
|
|
|
RegionInfoPassGraphTraits> {
|
2010-07-22 09:46:31 +02:00
|
|
|
static char ID;
|
2015-08-10 14:57:23 +02:00
|
|
|
RegionViewer()
|
|
|
|
: DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
|
|
|
|
RegionInfoPassGraphTraits>("reg", ID) {
|
2010-10-19 19:21:58 +02:00
|
|
|
initializeRegionViewerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-22 09:46:31 +02:00
|
|
|
};
|
|
|
|
char RegionViewer::ID = 0;
|
|
|
|
|
|
|
|
struct RegionOnlyViewer
|
2015-08-10 14:57:23 +02:00
|
|
|
: public DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
|
|
|
|
RegionInfoPassGraphTraits> {
|
2010-07-22 09:46:31 +02:00
|
|
|
static char ID;
|
2015-08-10 14:57:23 +02:00
|
|
|
RegionOnlyViewer()
|
|
|
|
: DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
|
|
|
|
RegionInfoPassGraphTraits>("regonly", ID) {
|
2010-10-19 19:21:58 +02:00
|
|
|
initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-22 09:46:31 +02:00
|
|
|
};
|
|
|
|
char RegionOnlyViewer::ID = 0;
|
|
|
|
|
|
|
|
} //end anonymous namespace
|
|
|
|
|
|
|
|
INITIALIZE_PASS(RegionPrinter, "dot-regions",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Print regions of function to 'dot' file", true, true)
|
2010-07-22 09:46:31 +02:00
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
INITIALIZE_PASS(
|
|
|
|
RegionOnlyPrinter, "dot-regions-only",
|
|
|
|
"Print regions of function to 'dot' file (with no function bodies)", true,
|
|
|
|
true)
|
|
|
|
|
2010-10-07 06:13:08 +02:00
|
|
|
INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
|
2010-10-08 00:25:06 +02:00
|
|
|
true, true)
|
2014-07-19 20:29:29 +02:00
|
|
|
|
2010-10-07 06:13:08 +02:00
|
|
|
INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
|
|
|
|
"View regions of function (with no function bodies)",
|
2010-10-08 00:25:06 +02:00
|
|
|
true, true)
|
2010-10-07 06:13:08 +02:00
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
|
2010-07-22 09:46:31 +02:00
|
|
|
|
2015-08-10 14:57:23 +02:00
|
|
|
FunctionPass *llvm::createRegionOnlyPrinterPass() {
|
|
|
|
return new RegionOnlyPrinter();
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2010-08-02 20:50:06 +02:00
|
|
|
|
2010-07-22 09:46:31 +02:00
|
|
|
FunctionPass* llvm::createRegionViewerPass() {
|
|
|
|
return new RegionViewer();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass* llvm::createRegionOnlyViewerPass() {
|
|
|
|
return new RegionOnlyViewer();
|
|
|
|
}
|
|
|
|
|
2015-08-10 15:21:59 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
|
|
|
|
assert(RI && "Argument must be non-null");
|
|
|
|
|
|
|
|
llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
|
|
|
|
std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
|
|
|
|
|
|
|
|
llvm::ViewGraph(RI, "reg", ShortNames,
|
|
|
|
Twine(GraphName) + " for '" + F->getName() + "' function");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
|
|
|
|
assert(F && "Argument must be non-null");
|
|
|
|
assert(!F->isDeclaration() && "Function must have an implementation");
|
|
|
|
|
|
|
|
// The viewer and analysis passes do not modify anything, so we can safely
|
|
|
|
// remove the const qualifier
|
|
|
|
auto NonConstF = const_cast<Function *>(F);
|
|
|
|
|
|
|
|
llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
|
|
|
|
FPM.add(ViewerPass);
|
|
|
|
FPM.doInitialization();
|
|
|
|
FPM.run(*NonConstF);
|
|
|
|
FPM.doFinalization();
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
|
|
|
|
|
|
|
|
void llvm::viewRegion(const Function *F) {
|
|
|
|
invokeFunctionPass(F, createRegionViewerPass());
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }
|
|
|
|
|
|
|
|
void llvm::viewRegionOnly(const Function *F) {
|
|
|
|
invokeFunctionPass(F, createRegionOnlyViewerPass());
|
|
|
|
}
|
|
|
|
#endif
|