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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
#include "polly/ScopGraphPrinter.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
#include "polly/ScopDetection.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 {
|
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
std::string DOTGraphTraits<ScopDetection *>::getEdgeAttributes(
|
|
|
|
RegionNode *srcNode, GraphTraits<RegionInfo *>::ChildIteratorType CI,
|
|
|
|
ScopDetection *SD) {
|
|
|
|
RegionNode *destNode = *CI;
|
2020-04-07 00:53:10 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
if (srcNode->isSubRegion() || destNode->isSubRegion())
|
|
|
|
return "";
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
// 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>();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
RegionInfo *RI = SD->getRI();
|
|
|
|
Region *R = RI->getRegionFor(destBB);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
while (R && R->getParent())
|
|
|
|
if (R->getParent()->getEntry() == destBB)
|
|
|
|
R = R->getParent();
|
|
|
|
else
|
|
|
|
break;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
if (R && R->getEntry() == destBB && R->contains(srcBB))
|
|
|
|
return "constraint=false";
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
return "";
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
std::string
|
|
|
|
DOTGraphTraits<ScopDetection *>::escapeString(llvm::StringRef String) {
|
|
|
|
std::string Escaped;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
for (const auto &C : String) {
|
|
|
|
if (C == '"')
|
|
|
|
Escaped += '\\';
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
Escaped += C;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
2022-05-10 02:38:20 +08:00
|
|
|
return Escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOTGraphTraits<ScopDetection *>::printRegionCluster(ScopDetection *SD,
|
|
|
|
const Region *R,
|
|
|
|
raw_ostream &O,
|
|
|
|
unsigned depth) {
|
|
|
|
O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void *>(R)
|
|
|
|
<< " {\n";
|
|
|
|
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-04-29 14:27:02 +08:00
|
|
|
}
|
2012-09-08 16:31:55 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
std::string ErrorMessage = SD->regionIsInvalidBecause(R);
|
|
|
|
ErrorMessage = escapeString(ErrorMessage);
|
|
|
|
O.indent(2 * (depth + 1))
|
|
|
|
<< "label = \"" << Location << ErrorMessage << "\";\n";
|
2012-09-08 16:31:55 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
if (SD->isMaxRegionInScop(*R)) {
|
|
|
|
O.indent(2 * (depth + 1)) << "style = filled;\n";
|
2015-05-03 13:21:36 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
// Set color to green.
|
|
|
|
O.indent(2 * (depth + 1)) << "color = 3";
|
|
|
|
} else {
|
|
|
|
O.indent(2 * (depth + 1)) << "style = solid;\n";
|
2015-05-03 13:21:36 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
int color = (R->getDepth() * 2 % 12) + 1;
|
2015-05-03 13:21:36 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
// We do not want green again.
|
|
|
|
if (color == 3)
|
|
|
|
color = 6;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
O.indent(2 * (depth + 1)) << "color = " << color << "\n";
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
for (const auto &SubRegion : *R)
|
|
|
|
printRegionCluster(SD, SubRegion.get(), O, depth + 1);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
RegionInfo *RI = R->getRegionInfo();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
for (BasicBlock *BB : R->blocks())
|
|
|
|
if (RI->getRegionFor(BB) == R)
|
|
|
|
O.indent(2 * (depth + 1))
|
|
|
|
<< "Node"
|
|
|
|
<< static_cast<void *>(RI->getTopLevelRegion()->getBBNode(BB))
|
|
|
|
<< ";\n";
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
O.indent(2 * depth) << "}\n";
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
void DOTGraphTraits<ScopDetection *>::addCustomGraphFeatures(
|
|
|
|
ScopDetection *SD, GraphWriter<ScopDetection *> &GW) {
|
|
|
|
raw_ostream &O = GW.getOStream();
|
|
|
|
O << "\tcolorscheme = \"paired12\"\n";
|
|
|
|
printRegionCluster(SD, SD->getRI()->getTopLevelRegion(), O, 4);
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
} // namespace llvm
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
struct ScopDetectionAnalysisGraphTraits {
|
|
|
|
static ScopDetection *getGraph(ScopDetectionWrapperPass *Analysis) {
|
|
|
|
return &Analysis->getSD();
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
struct ScopViewerWrapperPass
|
|
|
|
: public DOTGraphTraitsViewerWrapperPass<ScopDetectionWrapperPass, false,
|
|
|
|
ScopDetection *,
|
|
|
|
ScopDetectionAnalysisGraphTraits> {
|
2011-04-29 14:27:02 +08:00
|
|
|
static char ID;
|
2022-05-10 02:38:20 +08:00
|
|
|
ScopViewerWrapperPass()
|
|
|
|
: DOTGraphTraitsViewerWrapperPass<ScopDetectionWrapperPass, false,
|
|
|
|
ScopDetection *,
|
|
|
|
ScopDetectionAnalysisGraphTraits>(
|
2022-05-10 02:38:16 +08:00
|
|
|
"scops", ID) {}
|
2017-05-12 22:37:29 +08:00
|
|
|
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
|
|
|
};
|
2022-05-10 02:38:20 +08:00
|
|
|
char ScopViewerWrapperPass::ID = 0;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
struct ScopOnlyViewerWrapperPass
|
|
|
|
: public DOTGraphTraitsViewerWrapperPass<ScopDetectionWrapperPass, false,
|
|
|
|
ScopDetection *,
|
|
|
|
ScopDetectionAnalysisGraphTraits> {
|
2011-04-29 14:27:02 +08:00
|
|
|
static char ID;
|
2022-05-10 02:38:20 +08:00
|
|
|
ScopOnlyViewerWrapperPass()
|
|
|
|
: DOTGraphTraitsViewerWrapperPass<ScopDetectionWrapperPass, false,
|
|
|
|
ScopDetection *,
|
|
|
|
ScopDetectionAnalysisGraphTraits>(
|
2022-05-10 02:38:16 +08:00
|
|
|
"scopsonly", ID) {}
|
2011-04-29 14:27:02 +08:00
|
|
|
};
|
2022-05-10 02:38:20 +08:00
|
|
|
char ScopOnlyViewerWrapperPass::ID = 0;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
struct ScopPrinterWrapperPass
|
|
|
|
: public DOTGraphTraitsPrinterWrapperPass<
|
|
|
|
ScopDetectionWrapperPass, false, ScopDetection *,
|
|
|
|
ScopDetectionAnalysisGraphTraits> {
|
2011-04-29 14:27:02 +08:00
|
|
|
static char ID;
|
2022-05-10 02:38:20 +08:00
|
|
|
ScopPrinterWrapperPass()
|
|
|
|
: DOTGraphTraitsPrinterWrapperPass<ScopDetectionWrapperPass, false,
|
|
|
|
ScopDetection *,
|
|
|
|
ScopDetectionAnalysisGraphTraits>(
|
2022-05-10 02:38:16 +08:00
|
|
|
"scops", ID) {}
|
2011-04-29 14:27:02 +08:00
|
|
|
};
|
2022-05-10 02:38:20 +08:00
|
|
|
char ScopPrinterWrapperPass::ID = 0;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
struct ScopOnlyPrinterWrapperPass
|
|
|
|
: public DOTGraphTraitsPrinterWrapperPass<
|
|
|
|
ScopDetectionWrapperPass, true, ScopDetection *,
|
|
|
|
ScopDetectionAnalysisGraphTraits> {
|
2011-04-29 14:27:02 +08:00
|
|
|
static char ID;
|
2022-05-10 02:38:20 +08:00
|
|
|
ScopOnlyPrinterWrapperPass()
|
|
|
|
: DOTGraphTraitsPrinterWrapperPass<ScopDetectionWrapperPass, true,
|
|
|
|
ScopDetection *,
|
|
|
|
ScopDetectionAnalysisGraphTraits>(
|
2022-05-10 02:38:16 +08:00
|
|
|
"scopsonly", ID) {}
|
2011-04-29 14:27:02 +08:00
|
|
|
};
|
2022-05-10 02:38:20 +08:00
|
|
|
char ScopOnlyPrinterWrapperPass::ID = 0;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
static RegisterPass<ScopViewerWrapperPass> X("view-scops",
|
|
|
|
"Polly - View Scops of function");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
static RegisterPass<ScopOnlyViewerWrapperPass>
|
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
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
static RegisterPass<ScopPrinterWrapperPass>
|
|
|
|
M("dot-scops", "Polly - Print Scops of function");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
static RegisterPass<ScopOnlyPrinterWrapperPass>
|
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
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
Pass *polly::createDOTViewerWrapperPass() {
|
|
|
|
return new ScopViewerWrapperPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *polly::createDOTOnlyViewerWrapperPass() {
|
|
|
|
return new ScopOnlyViewerWrapperPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *polly::createDOTPrinterWrapperPass() {
|
|
|
|
return new ScopPrinterWrapperPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *polly::createDOTOnlyPrinterWrapperPass() {
|
|
|
|
return new ScopOnlyPrinterWrapperPass();
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
bool ScopViewer::processFunction(Function &F, const ScopDetection &SD) {
|
|
|
|
if (ViewFilter != "" && !F.getName().count(ViewFilter))
|
|
|
|
return false;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
if (ViewAll)
|
|
|
|
return true;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2022-05-10 02:38:20 +08:00
|
|
|
// Check that at least one scop was detected.
|
|
|
|
return std::distance(SD.begin(), SD.end()) > 0;
|
|
|
|
}
|