LLVM  14.0.0git
DDGPrinter.cpp
Go to the documentation of this file.
1 //===- DDGPrinter.cpp - DOT printer for the data dependence graph ----------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 //===----------------------------------------------------------------------===//
10 //
11 // This file defines the `-dot-ddg` analysis pass, which emits DDG in DOT format
12 // in a file named `ddg.<graph-name>.dot` for each loop in a function.
13 //===----------------------------------------------------------------------===//
14 
18 
19 using namespace llvm;
20 
21 static cl::opt<bool> DotOnly("dot-ddg-only", cl::init(false), cl::Hidden,
22  cl::ZeroOrMore, cl::desc("simple ddg dot graph"));
24  "dot-ddg-filename-prefix", cl::init("ddg"), cl::Hidden,
25  cl::desc("The prefix used for the DDG dot file names."));
26 
27 static void writeDDGToDotFile(DataDependenceGraph &G, bool DOnly = false);
28 
29 //===--------------------------------------------------------------------===//
30 // Implementation of DDG DOT Printer for a loop
31 //===--------------------------------------------------------------------===//
34  LPMUpdater &U) {
36  return PreservedAnalyses::all();
37 }
38 
39 static void writeDDGToDotFile(DataDependenceGraph &G, bool DOnly) {
40  std::string Filename =
41  Twine(DDGDotFilenamePrefix + "." + G.getName() + ".dot").str();
42  errs() << "Writing '" << Filename << "'...";
43 
44  std::error_code EC;
45  raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
46 
47  if (!EC)
48  // We only provide the constant verson of the DOTGraphTrait specialization,
49  // hence the conversion to const pointer
50  WriteGraph(File, (const DataDependenceGraph *)&G, DOnly);
51  else
52  errs() << " error opening file for writing!";
53  errs() << "\n";
54 }
55 
56 //===--------------------------------------------------------------------===//
57 // DDG DOT Printer Implementation
58 //===--------------------------------------------------------------------===//
59 std::string DDGDotGraphTraits::getNodeLabel(const DDGNode *Node,
60  const DataDependenceGraph *Graph) {
61  if (isSimple())
62  return getSimpleNodeLabel(Node, Graph);
63  else
64  return getVerboseNodeLabel(Node, Graph);
65 }
66 
69  const DataDependenceGraph *G) {
70  const DDGEdge *E = static_cast<const DDGEdge *>(*I.getCurrent());
71  if (isSimple())
72  return getSimpleEdgeAttributes(Node, E, G);
73  else
74  return getVerboseEdgeAttributes(Node, E, G);
75 }
76 
78  const DataDependenceGraph *Graph) {
79  if (isSimple() && isa<RootDDGNode>(Node))
80  return true;
81  assert(Graph && "expected a valid graph pointer");
82  return Graph->getPiBlock(*Node) != nullptr;
83 }
84 
85 std::string
86 DDGDotGraphTraits::getSimpleNodeLabel(const DDGNode *Node,
87  const DataDependenceGraph *G) {
88  std::string Str;
89  raw_string_ostream OS(Str);
90  if (isa<SimpleDDGNode>(Node))
91  for (auto *II : static_cast<const SimpleDDGNode *>(Node)->getInstructions())
92  OS << *II << "\n";
93  else if (isa<PiBlockDDGNode>(Node))
94  OS << "pi-block\nwith\n"
95  << cast<PiBlockDDGNode>(Node)->getNodes().size() << " nodes\n";
96  else if (isa<RootDDGNode>(Node))
97  OS << "root\n";
98  else
99  llvm_unreachable("Unimplemented type of node");
100  return OS.str();
101 }
102 
103 std::string
104 DDGDotGraphTraits::getVerboseNodeLabel(const DDGNode *Node,
105  const DataDependenceGraph *G) {
106  std::string Str;
107  raw_string_ostream OS(Str);
108  OS << "<kind:" << Node->getKind() << ">\n";
109  if (isa<SimpleDDGNode>(Node))
110  for (auto *II : static_cast<const SimpleDDGNode *>(Node)->getInstructions())
111  OS << *II << "\n";
112  else if (isa<PiBlockDDGNode>(Node)) {
113  OS << "--- start of nodes in pi-block ---\n";
114  unsigned Count = 0;
115  const auto &PNodes = cast<PiBlockDDGNode>(Node)->getNodes();
116  for (auto *PN : PNodes) {
117  OS << getVerboseNodeLabel(PN, G);
118  if (++Count != PNodes.size())
119  OS << "\n";
120  }
121  OS << "--- end of nodes in pi-block ---\n";
122  } else if (isa<RootDDGNode>(Node))
123  OS << "root\n";
124  else
125  llvm_unreachable("Unimplemented type of node");
126  return OS.str();
127 }
128 
129 std::string DDGDotGraphTraits::getSimpleEdgeAttributes(
130  const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) {
131  std::string Str;
132  raw_string_ostream OS(Str);
133  DDGEdge::EdgeKind Kind = Edge->getKind();
134  OS << "label=\"[" << Kind << "]\"";
135  return OS.str();
136 }
137 
138 std::string DDGDotGraphTraits::getVerboseEdgeAttributes(
139  const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) {
140  std::string Str;
141  raw_string_ostream OS(Str);
142  DDGEdge::EdgeKind Kind = Edge->getKind();
143  OS << "label=\"[";
145  OS << G->getDependenceString(*Src, Edge->getTargetNode());
146  else
147  OS << Kind;
148  OS << "]\"";
149  return OS.str();
150 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
DotOnly
static cl::opt< bool > DotOnly("dot-ddg-only", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("simple ddg dot graph"))
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:779
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:530
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:625
llvm::DOTGraphTraits< const DataDependenceGraph * >::isNodeHidden
bool isNodeHidden(const DDGNode *Node, const DataDependenceGraph *G)
Do not print nodes that are part of a pi-block separately.
Definition: DDGPrinter.cpp:77
llvm::DDGEdge::getKind
EdgeKind getKind() const
Get the edge kind.
Definition: DDG.h:247
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::DOTGraphTraits< const DataDependenceGraph * >::getNodeLabel
std::string getNodeLabel(const DDGNode *Node, const DataDependenceGraph *Graph)
Print a DDG node either in concise form (-ddg-dot-only) or verbose mode (-ddg-dot).
Definition: DDGPrinter.cpp:59
llvm::LoopStandardAnalysisResults
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
Definition: LoopAnalysisManager.h:52
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::DDGDotPrinterPass::run
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Definition: DDGPrinter.cpp:32
DDGDotFilenamePrefix
static cl::opt< std::string > DDGDotFilenamePrefix("dot-ddg-filename-prefix", cl::init("ddg"), cl::Hidden, cl::desc("The prefix used for the DDG dot file names."))
CommandLine.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::DDGEdge
Data Dependency Graph Edge.
Definition: DDG.h:219
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::Twine::str
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
llvm::cl::ZeroOrMore
@ ZeroOrMore
Definition: CommandLine.h:120
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
llvm::cl::opt< bool >
llvm::sys::fs::OF_Text
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition: FileSystem.h:761
llvm::LPMUpdater
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Definition: LoopPassManager.h:249
llvm::SimpleDDGNode
Subclass of DDGNode representing single or multi-instruction nodes.
Definition: DDG.h:106
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
llvm::DGEdge::getTargetNode
const NodeType & getTargetNode() const
Retrieve the target node this edge connects to.
Definition: DirectedGraph.h:47
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::WriteGraph
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
Definition: GraphWriter.h:307
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::DefaultDOTGraphTraits::isSimple
bool isSimple()
Definition: DOTGraphTraits.h:33
writeDDGToDotFile
static void writeDDGToDotFile(DataDependenceGraph &G, bool DOnly=false)
Definition: DDGPrinter.cpp:39
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:443
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
GraphWriter.h
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
DDGPrinter.h
llvm::DDGNode
Data Dependence Graph Node The graph can represent the following types of nodes:
Definition: DDG.h:42
llvm::DataDependenceGraph
Data Dependency Graph.
Definition: DDG.h:316
llvm::DOTGraphTraits< const DataDependenceGraph * >::getEdgeAttributes
std::string getEdgeAttributes(const DDGNode *Node, GraphTraits< const DDGNode * >::ChildIteratorType I, const DataDependenceGraph *G)
Print attributes of an edge in the DDG graph.
Definition: DDGPrinter.cpp:67
llvm::DDGEdge::EdgeKind::MemoryDependence
@ MemoryDependence
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::GraphTraits
Definition: GraphTraits.h:35
llvm::DDGAnalysis
Analysis pass that builds the DDG for a loop.
Definition: DDG.h:425
llvm::cl::desc
Definition: CommandLine.h:414
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:205
llvm::DataDependenceGraph::getPiBlock
const PiBlockDDGNode * getPiBlock(const NodeType &N) const
If node N belongs to a pi-block return a pointer to the pi-block, otherwise return null.
Definition: DDG.cpp:244
llvm::DDGEdge::EdgeKind
EdgeKind
The kind of edge in the DDG.
Definition: DDG.h:222