2010-07-22 07:46:31 +00:00
|
|
|
//===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Print out the region tree of a function using dotty/graphviz.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/DOTGraphTraitsPass.h"
|
2010-07-22 07:46:31 +00:00
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
|
|
|
#include "llvm/Analysis/RegionPrinter.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-07-22 07:46:31 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
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())
|
|
|
|
return DOTGraphTraits<const Function*>
|
|
|
|
::getSimpleNodeLabel(BB, BB->getParent());
|
|
|
|
else
|
|
|
|
return DOTGraphTraits<const Function*>
|
|
|
|
::getCompleteNodeLabel(BB, BB->getParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Not implemented";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2014-07-19 18:29:29 +00:00
|
|
|
struct DOTGraphTraits<RegionInfoPass*> : public DOTGraphTraits<RegionNode*> {
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
DOTGraphTraits (bool isSimple = false)
|
2010-07-22 07:46:31 +00:00
|
|
|
: DOTGraphTraits<RegionNode*>(isSimple) {}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
static std::string getGraphName(RegionInfoPass *DT) {
|
2010-07-22 07:46:31 +00:00
|
|
|
return "Region Graph";
|
|
|
|
}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
std::string getNodeLabel(RegionNode *Node, RegionInfoPass *G) {
|
|
|
|
RegionInfo &RI = G->getRegionInfo();
|
2010-07-22 07:46:31 +00:00
|
|
|
return DOTGraphTraits<RegionNode*>::getNodeLabel(Node,
|
2014-07-19 18:29:29 +00:00
|
|
|
reinterpret_cast<RegionNode*>(RI.getTopLevelRegion()));
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2011-02-27 04:11:07 +00:00
|
|
|
std::string getEdgeAttributes(RegionNode *srcNode,
|
2014-07-19 18:29:29 +00:00
|
|
|
GraphTraits<RegionInfo*>::ChildIteratorType CI, RegionInfoPass *G) {
|
|
|
|
RegionInfo &RI = G->getRegionInfo();
|
2011-02-27 04:11:07 +00: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>();
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
Region *R = RI.getRegionFor(destBB);
|
2011-02-27 04:11:07 +00:00
|
|
|
|
|
|
|
while (R && R->getParent())
|
|
|
|
if (R->getParent()->getEntry() == destBB)
|
|
|
|
R = R->getParent();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (R->getEntry() == destBB && R->contains(srcBB))
|
|
|
|
return "constraint=false";
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2010-07-22 07:46:31 +00:00
|
|
|
// Print the cluster of the subregions. This groups the single basic blocks
|
|
|
|
// and adds a different background color for each group.
|
2014-07-19 18:29:29 +00:00
|
|
|
static void printRegionCluster(const Region &R,
|
|
|
|
GraphWriter<RegionInfoPass*> &GW,
|
2010-07-22 07:46:31 +00:00
|
|
|
unsigned depth = 0) {
|
|
|
|
raw_ostream &O = GW.getOStream();
|
2014-04-15 18:32:43 +00:00
|
|
|
O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
|
2010-07-22 07:46:31 +00:00
|
|
|
<< " {\n";
|
|
|
|
O.indent(2 * (depth + 1)) << "label = \"\";\n";
|
|
|
|
|
2014-04-15 18:32:43 +00:00
|
|
|
if (!onlySimpleRegions || R.isSimple()) {
|
2010-07-22 07:46:31 +00:00
|
|
|
O.indent(2 * (depth + 1)) << "style = filled;\n";
|
|
|
|
O.indent(2 * (depth + 1)) << "color = "
|
2014-04-15 18:32:43 +00:00
|
|
|
<< ((R.getDepth() * 2 % 12) + 1) << "\n";
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
O.indent(2 * (depth + 1)) << "style = solid;\n";
|
|
|
|
O.indent(2 * (depth + 1)) << "color = "
|
2014-04-15 18:32:43 +00:00
|
|
|
<< ((R.getDepth() * 2 % 12) + 2) << "\n";
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:32:43 +00:00
|
|
|
for (Region::const_iterator RI = R.begin(), RE = R.end(); RI != RE; ++RI)
|
|
|
|
printRegionCluster(**RI, GW, depth + 1);
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2015-04-15 21:40:50 +00:00
|
|
|
for (auto *BB : R.blocks())
|
2014-07-19 18:29:29 +00:00
|
|
|
if (RI.getRegionFor(BB) == &R)
|
2010-07-22 07:46:31 +00:00
|
|
|
O.indent(2 * (depth + 1)) << "Node"
|
2014-07-19 18:29:29 +00:00
|
|
|
<< static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
|
2010-07-22 07:46:31 +00:00
|
|
|
<< ";\n";
|
|
|
|
|
|
|
|
O.indent(2 * depth) << "}\n";
|
|
|
|
}
|
|
|
|
|
2014-07-19 18:29:29 +00:00
|
|
|
static void addCustomGraphFeatures(const RegionInfoPass* RIP,
|
|
|
|
GraphWriter<RegionInfoPass*> &GW) {
|
|
|
|
const RegionInfo &RI = RIP->getRegionInfo();
|
2010-07-22 07:46:31 +00:00
|
|
|
raw_ostream &O = GW.getOStream();
|
|
|
|
O << "\tcolorscheme = \"paired12\"\n";
|
2014-07-19 18:29:29 +00:00
|
|
|
printRegionCluster(*RI.getTopLevelRegion(), GW, 4);
|
2010-07-22 07:46:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} //end namespace llvm
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct RegionViewer
|
2014-07-19 18:29:29 +00:00
|
|
|
: public DOTGraphTraitsViewer<RegionInfoPass, false> {
|
2010-07-22 07:46:31 +00:00
|
|
|
static char ID;
|
2014-07-19 18:29:29 +00:00
|
|
|
RegionViewer() : DOTGraphTraitsViewer<RegionInfoPass, false>("reg", ID){
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeRegionViewerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-22 07:46:31 +00:00
|
|
|
};
|
|
|
|
char RegionViewer::ID = 0;
|
|
|
|
|
|
|
|
struct RegionOnlyViewer
|
2014-07-19 18:29:29 +00:00
|
|
|
: public DOTGraphTraitsViewer<RegionInfoPass, true> {
|
2010-07-22 07:46:31 +00:00
|
|
|
static char ID;
|
2014-07-19 18:29:29 +00:00
|
|
|
RegionOnlyViewer() : DOTGraphTraitsViewer<RegionInfoPass, true>("regonly", ID) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-22 07:46:31 +00:00
|
|
|
};
|
|
|
|
char RegionOnlyViewer::ID = 0;
|
|
|
|
|
|
|
|
struct RegionPrinter
|
2014-07-19 18:29:29 +00:00
|
|
|
: public DOTGraphTraitsPrinter<RegionInfoPass, false> {
|
2010-07-22 07:46:31 +00:00
|
|
|
static char ID;
|
|
|
|
RegionPrinter() :
|
2014-07-19 18:29:29 +00:00
|
|
|
DOTGraphTraitsPrinter<RegionInfoPass, false>("reg", ID) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-22 07:46:31 +00:00
|
|
|
};
|
2010-10-07 04:13:08 +00:00
|
|
|
char RegionPrinter::ID = 0;
|
2010-07-22 07:46:31 +00:00
|
|
|
} //end anonymous namespace
|
|
|
|
|
|
|
|
INITIALIZE_PASS(RegionPrinter, "dot-regions",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Print regions of function to 'dot' file", true, true)
|
2010-07-22 07:46:31 +00:00
|
|
|
|
2010-10-07 04:13:08 +00:00
|
|
|
INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
|
2010-10-07 22:25:06 +00:00
|
|
|
true, true)
|
2014-07-19 18:29:29 +00:00
|
|
|
|
2010-10-07 04:13:08 +00:00
|
|
|
INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
|
|
|
|
"View regions of function (with no function bodies)",
|
2010-10-07 22:25:06 +00:00
|
|
|
true, true)
|
2010-10-07 04:13:08 +00:00
|
|
|
|
2010-08-02 18:50:06 +00:00
|
|
|
namespace {
|
|
|
|
|
2010-07-22 07:46:31 +00:00
|
|
|
struct RegionOnlyPrinter
|
2014-07-19 18:29:29 +00:00
|
|
|
: public DOTGraphTraitsPrinter<RegionInfoPass, true> {
|
2010-07-22 07:46:31 +00:00
|
|
|
static char ID;
|
|
|
|
RegionOnlyPrinter() :
|
2014-07-19 18:29:29 +00:00
|
|
|
DOTGraphTraitsPrinter<RegionInfoPass, true>("reg", ID) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-07-22 07:46:31 +00:00
|
|
|
};
|
|
|
|
|
2010-08-02 18:50:06 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 07:46:31 +00:00
|
|
|
char RegionOnlyPrinter::ID = 0;
|
|
|
|
INITIALIZE_PASS(RegionOnlyPrinter, "dot-regions-only",
|
|
|
|
"Print regions of function to 'dot' file "
|
|
|
|
"(with no function bodies)",
|
2010-10-07 22:25:06 +00:00
|
|
|
true, true)
|
2010-07-22 07:46:31 +00:00
|
|
|
|
|
|
|
FunctionPass* llvm::createRegionViewerPass() {
|
|
|
|
return new RegionViewer();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass* llvm::createRegionOnlyViewerPass() {
|
|
|
|
return new RegionOnlyViewer();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass* llvm::createRegionPrinterPass() {
|
|
|
|
return new RegionPrinter();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass* llvm::createRegionOnlyPrinterPass() {
|
|
|
|
return new RegionOnlyPrinter();
|
|
|
|
}
|
|
|
|
|