2011-04-29 14:27:02 +08:00
|
|
|
//===- GraphPrinter.cpp - Create a DOT output describing the Scop. --------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2011-04-29 14:27:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Create a DOT output describing the Scop.
|
|
|
|
//
|
|
|
|
// For each function a dot file is created that shows the control flow graph of
|
|
|
|
// the function and highlights the detected Scops.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
#include "polly/ScopDetection.h"
|
2015-05-03 13:21:36 +08:00
|
|
|
#include "polly/Support/ScopLocation.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Analysis/DOTGraphTraitsPass.h"
|
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
2015-08-04 00:39:56 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
using namespace polly;
|
|
|
|
using namespace llvm;
|
2015-08-04 00:39:56 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
ViewFilter("polly-view-only",
|
|
|
|
cl::desc("Only view functions that match this pattern"),
|
|
|
|
cl::Hidden, cl::init(""), cl::ZeroOrMore);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2015-12-17 21:04:30 +08:00
|
|
|
static cl::opt<bool> ViewAll("polly-view-all",
|
|
|
|
cl::desc("Also show functions without any scops"),
|
|
|
|
cl::Hidden, cl::init(false), cl::ZeroOrMore);
|
2015-12-17 20:55:26 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
namespace llvm {
|
2013-03-23 09:05:07 +08:00
|
|
|
template <>
|
|
|
|
struct GraphTraits<ScopDetection *> : public GraphTraits<RegionInfo *> {
|
2016-08-23 06:30:27 +08:00
|
|
|
static NodeRef getEntryNode(ScopDetection *SD) {
|
2013-03-23 09:05:07 +08:00
|
|
|
return GraphTraits<RegionInfo *>::getEntryNode(SD->getRI());
|
|
|
|
}
|
|
|
|
static nodes_iterator nodes_begin(ScopDetection *SD) {
|
|
|
|
return nodes_iterator::begin(getEntryNode(SD));
|
|
|
|
}
|
|
|
|
static nodes_iterator nodes_end(ScopDetection *SD) {
|
|
|
|
return nodes_iterator::end(getEntryNode(SD));
|
|
|
|
}
|
|
|
|
};
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2017-05-12 22:37:29 +08:00
|
|
|
template <>
|
|
|
|
struct GraphTraits<ScopDetectionWrapperPass *>
|
|
|
|
: public GraphTraits<ScopDetection *> {
|
|
|
|
static NodeRef getEntryNode(ScopDetectionWrapperPass *P) {
|
|
|
|
return GraphTraits<ScopDetection *>::getEntryNode(&P->getSD());
|
|
|
|
}
|
|
|
|
static nodes_iterator nodes_begin(ScopDetectionWrapperPass *P) {
|
|
|
|
return nodes_iterator::begin(getEntryNode(P));
|
|
|
|
}
|
|
|
|
static nodes_iterator nodes_end(ScopDetectionWrapperPass *P) {
|
|
|
|
return nodes_iterator::end(getEntryNode(P));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
template <> struct DOTGraphTraits<RegionNode *> : public DefaultDOTGraphTraits {
|
|
|
|
DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
|
|
|
|
if (!Node->isSubRegion()) {
|
|
|
|
BasicBlock *BB = Node->getNodeAs<BasicBlock>();
|
|
|
|
|
|
|
|
if (isSimple())
|
2013-03-23 09:05:07 +08:00
|
|
|
return DOTGraphTraits<const Function *>::getSimpleNodeLabel(
|
|
|
|
BB, BB->getParent());
|
2011-04-29 14:27:02 +08:00
|
|
|
else
|
2013-03-23 09:05:07 +08:00
|
|
|
return DOTGraphTraits<const Function *>::getCompleteNodeLabel(
|
|
|
|
BB, BB->getParent());
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return "Not implemented";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
template <>
|
2017-05-12 22:37:29 +08:00
|
|
|
struct DOTGraphTraits<ScopDetectionWrapperPass *>
|
|
|
|
: public DOTGraphTraits<RegionNode *> {
|
2013-03-23 09:05:07 +08:00
|
|
|
DOTGraphTraits(bool isSimple = false)
|
|
|
|
: DOTGraphTraits<RegionNode *>(isSimple) {}
|
2017-05-12 22:37:29 +08:00
|
|
|
static std::string getGraphName(ScopDetectionWrapperPass *SD) {
|
|
|
|
return "Scop Graph";
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
std::string getEdgeAttributes(RegionNode *srcNode,
|
2013-03-23 09:05:07 +08:00
|
|
|
GraphTraits<RegionInfo *>::ChildIteratorType CI,
|
2017-05-12 22:37:29 +08:00
|
|
|
ScopDetectionWrapperPass *P) {
|
2011-04-29 14:27:02 +08:00
|
|
|
RegionNode *destNode = *CI;
|
2017-05-12 22:37:29 +08:00
|
|
|
auto *SD = &P->getSD();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
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>();
|
|
|
|
|
|
|
|
RegionInfo *RI = SD->getRI();
|
|
|
|
Region *R = RI->getRegionFor(destBB);
|
|
|
|
|
|
|
|
while (R && R->getParent())
|
|
|
|
if (R->getParent()->getEntry() == destBB)
|
|
|
|
R = R->getParent();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2015-03-30 06:00:54 +08:00
|
|
|
if (R && R->getEntry() == destBB && R->contains(srcBB))
|
2011-04-29 14:27:02 +08:00
|
|
|
return "constraint=false";
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-05-12 22:37:29 +08:00
|
|
|
std::string getNodeLabel(RegionNode *Node, ScopDetectionWrapperPass *P) {
|
2013-03-23 09:05:07 +08:00
|
|
|
return DOTGraphTraits<RegionNode *>::getNodeLabel(
|
2017-05-12 22:37:29 +08:00
|
|
|
Node, reinterpret_cast<RegionNode *>(
|
|
|
|
P->getSD().getRI()->getTopLevelRegion()));
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
2012-09-08 16:31:55 +08:00
|
|
|
|
|
|
|
static std::string escapeString(std::string String) {
|
|
|
|
std::string Escaped;
|
|
|
|
|
2014-06-28 16:59:45 +08:00
|
|
|
for (const auto &C : String) {
|
|
|
|
if (C == '"')
|
2012-09-08 16:31:55 +08:00
|
|
|
Escaped += '\\';
|
|
|
|
|
2014-06-28 16:59:45 +08:00
|
|
|
Escaped += C;
|
2012-09-08 16:31:55 +08:00
|
|
|
}
|
|
|
|
return Escaped;
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
// Print the cluster of the subregions. This groups the single basic blocks
|
|
|
|
// and adds a different background color for each group.
|
|
|
|
static void printRegionCluster(const ScopDetection *SD, const Region *R,
|
|
|
|
raw_ostream &O, unsigned depth = 0) {
|
2013-03-23 09:05:07 +08:00
|
|
|
O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void *>(R)
|
|
|
|
<< " {\n";
|
2015-05-03 13:21:36 +08:00
|
|
|
unsigned LineBegin, LineEnd;
|
|
|
|
std::string FileName;
|
|
|
|
|
|
|
|
getDebugLocation(R, LineBegin, LineEnd, FileName);
|
|
|
|
|
|
|
|
std::string Location;
|
|
|
|
if (LineBegin != (unsigned)-1) {
|
|
|
|
Location = escapeString(FileName + ":" + std::to_string(LineBegin) + "-" +
|
|
|
|
std::to_string(LineEnd) + "\n");
|
|
|
|
}
|
|
|
|
|
2011-10-08 08:30:55 +08:00
|
|
|
std::string ErrorMessage = SD->regionIsInvalidBecause(R);
|
2012-09-08 16:31:55 +08:00
|
|
|
ErrorMessage = escapeString(ErrorMessage);
|
2016-12-19 22:06:40 +08:00
|
|
|
O.indent(2 * (depth + 1))
|
|
|
|
<< "label = \"" << Location << ErrorMessage << "\";\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
if (SD->isMaxRegionInScop(*R)) {
|
|
|
|
O.indent(2 * (depth + 1)) << "style = filled;\n";
|
|
|
|
|
|
|
|
// Set color to green.
|
|
|
|
O.indent(2 * (depth + 1)) << "color = 3";
|
|
|
|
} else {
|
|
|
|
O.indent(2 * (depth + 1)) << "style = solid;\n";
|
|
|
|
|
|
|
|
int color = (R->getDepth() * 2 % 12) + 1;
|
|
|
|
|
|
|
|
// We do not want green again.
|
|
|
|
if (color == 3)
|
|
|
|
color = 6;
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
O.indent(2 * (depth + 1)) << "color = " << color << "\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2014-03-03 21:13:55 +08:00
|
|
|
for (const auto &SubRegion : *R)
|
2014-04-16 05:48:34 +08:00
|
|
|
printRegionCluster(SD, SubRegion.get(), O, depth + 1);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
RegionInfo *RI = R->getRegionInfo();
|
|
|
|
|
2014-03-03 21:13:55 +08:00
|
|
|
for (const auto &BB : R->blocks())
|
|
|
|
if (RI->getRegionFor(BB) == R)
|
2013-03-23 09:05:07 +08:00
|
|
|
O.indent(2 * (depth + 1))
|
2013-07-14 00:58:07 +08:00
|
|
|
<< "Node"
|
2014-03-03 21:13:55 +08:00
|
|
|
<< static_cast<void *>(RI->getTopLevelRegion()->getBBNode(BB))
|
2013-07-14 00:58:07 +08:00
|
|
|
<< ";\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
O.indent(2 * depth) << "}\n";
|
|
|
|
}
|
2017-05-12 22:37:29 +08:00
|
|
|
static void
|
|
|
|
addCustomGraphFeatures(const ScopDetectionWrapperPass *SD,
|
|
|
|
GraphWriter<ScopDetectionWrapperPass *> &GW) {
|
2011-04-29 14:27:02 +08:00
|
|
|
raw_ostream &O = GW.getOStream();
|
|
|
|
O << "\tcolorscheme = \"paired12\"\n";
|
2017-05-12 22:37:29 +08:00
|
|
|
printRegionCluster(&SD->getSD(), SD->getSD().getRI()->getTopLevelRegion(),
|
|
|
|
O, 4);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
};
|
2013-06-23 09:29:29 +08:00
|
|
|
} // end namespace llvm
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2017-05-12 22:37:29 +08:00
|
|
|
struct ScopViewer
|
|
|
|
: public DOTGraphTraitsViewer<ScopDetectionWrapperPass, false> {
|
2011-04-29 14:27:02 +08:00
|
|
|
static char ID;
|
2017-05-12 22:37:29 +08:00
|
|
|
ScopViewer()
|
|
|
|
: DOTGraphTraitsViewer<ScopDetectionWrapperPass, false>("scops", ID) {}
|
|
|
|
bool processFunction(Function &F, ScopDetectionWrapperPass &SD) override {
|
2015-12-17 20:55:26 +08:00
|
|
|
if (ViewFilter != "" && !F.getName().count(ViewFilter))
|
|
|
|
return false;
|
2015-08-04 00:39:56 +08:00
|
|
|
|
2015-12-17 20:55:26 +08:00
|
|
|
if (ViewAll)
|
2015-08-04 00:39:56 +08:00
|
|
|
return true;
|
|
|
|
|
2015-12-17 20:55:26 +08:00
|
|
|
// Check that at least one scop was detected.
|
2017-05-12 22:37:29 +08:00
|
|
|
return std::distance(SD.getSD().begin(), SD.getSD().end()) > 0;
|
2015-08-04 00:39:56 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
};
|
|
|
|
char ScopViewer::ID = 0;
|
|
|
|
|
2017-05-12 22:37:29 +08:00
|
|
|
struct ScopOnlyViewer
|
|
|
|
: public DOTGraphTraitsViewer<ScopDetectionWrapperPass, true> {
|
2011-04-29 14:27:02 +08:00
|
|
|
static char ID;
|
|
|
|
ScopOnlyViewer()
|
2017-05-12 22:37:29 +08:00
|
|
|
: DOTGraphTraitsViewer<ScopDetectionWrapperPass, true>("scopsonly", ID) {}
|
2011-04-29 14:27:02 +08:00
|
|
|
};
|
|
|
|
char ScopOnlyViewer::ID = 0;
|
|
|
|
|
2017-05-12 22:37:29 +08:00
|
|
|
struct ScopPrinter
|
|
|
|
: public DOTGraphTraitsPrinter<ScopDetectionWrapperPass, false> {
|
2011-04-29 14:27:02 +08:00
|
|
|
static char ID;
|
2017-05-12 22:37:29 +08:00
|
|
|
ScopPrinter()
|
|
|
|
: DOTGraphTraitsPrinter<ScopDetectionWrapperPass, false>("scops", ID) {}
|
2011-04-29 14:27:02 +08:00
|
|
|
};
|
|
|
|
char ScopPrinter::ID = 0;
|
|
|
|
|
2017-05-12 22:37:29 +08:00
|
|
|
struct ScopOnlyPrinter
|
|
|
|
: public DOTGraphTraitsPrinter<ScopDetectionWrapperPass, true> {
|
2011-04-29 14:27:02 +08:00
|
|
|
static char ID;
|
2013-03-23 09:05:07 +08:00
|
|
|
ScopOnlyPrinter()
|
2017-05-12 22:37:29 +08:00
|
|
|
: DOTGraphTraitsPrinter<ScopDetectionWrapperPass, true>("scopsonly", ID) {
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
};
|
|
|
|
char ScopOnlyPrinter::ID = 0;
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
static RegisterPass<ScopViewer> X("view-scops",
|
|
|
|
"Polly - View Scops of function");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
static RegisterPass<ScopOnlyViewer>
|
2014-07-09 18:50:10 +08:00
|
|
|
Y("view-scops-only",
|
|
|
|
"Polly - View Scops of function (with no function bodies)");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
static RegisterPass<ScopPrinter> M("dot-scops",
|
|
|
|
"Polly - Print Scops of function");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
static RegisterPass<ScopOnlyPrinter>
|
2014-07-09 18:50:10 +08:00
|
|
|
N("dot-scops-only",
|
|
|
|
"Polly - Print Scops of function (with no function bodies)");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
Pass *polly::createDOTViewerPass() { return new ScopViewer(); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
Pass *polly::createDOTOnlyViewerPass() { return new ScopOnlyViewer(); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
Pass *polly::createDOTPrinterPass() { return new ScopPrinter(); }
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
Pass *polly::createDOTOnlyPrinterPass() { return new ScopOnlyPrinter(); }
|