LLVM  12.0.0git
SelectionDAGPrinter.cpp
Go to the documentation of this file.
1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
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 // This implements the SelectionDAG::viewGraph method.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ScheduleDAGSDNodes.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/Support/Debug.h"
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "dag-printer"
28 
29 namespace llvm {
30  template<>
32 
33  explicit DOTGraphTraits(bool isSimple=false) :
35 
36  static bool hasEdgeDestLabels() {
37  return true;
38  }
39 
40  static unsigned numEdgeDestLabels(const void *Node) {
41  return ((const SDNode *) Node)->getNumValues();
42  }
43 
44  static std::string getEdgeDestLabel(const void *Node, unsigned i) {
45  return ((const SDNode *) Node)->getValueType(i).getEVTString();
46  }
47 
48  template<typename EdgeIter>
49  static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
50  return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
51  }
52 
53  /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
54  /// should actually target another edge source, not a node. If this method
55  /// is implemented, getEdgeTarget should be implemented.
56  template<typename EdgeIter>
57  static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
58  return true;
59  }
60 
61  /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
62  /// called to determine which outgoing edge of Node is the target of this
63  /// edge.
64  template<typename EdgeIter>
65  static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
66  SDNode *TargetNode = *I;
67  SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
68  std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
69  return NI;
70  }
71 
72  static std::string getGraphName(const SelectionDAG *G) {
73  return std::string(G->getMachineFunction().getName());
74  }
75 
76  static bool renderGraphFromBottomUp() {
77  return true;
78  }
79 
80  static std::string getNodeIdentifierLabel(const SDNode *Node,
81  const SelectionDAG *Graph) {
82  std::string R;
83  raw_string_ostream OS(R);
84 #ifndef NDEBUG
85  OS << 't' << Node->PersistentId;
86 #else
87  OS << static_cast<const void *>(Node);
88 #endif
89  return R;
90  }
91 
92  /// If you want to override the dot attributes printed for a particular
93  /// edge, override this method.
94  template<typename EdgeIter>
95  static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
96  const SelectionDAG *Graph) {
97  SDValue Op = EI.getNode()->getOperand(EI.getOperand());
98  EVT VT = Op.getValueType();
99  if (VT == MVT::Glue)
100  return "color=red,style=bold";
101  else if (VT == MVT::Other)
102  return "color=blue,style=dashed";
103  return "";
104  }
105 
106 
107  static std::string getSimpleNodeLabel(const SDNode *Node,
108  const SelectionDAG *G) {
109  std::string Result = Node->getOperationName(G);
110  {
111  raw_string_ostream OS(Result);
112  Node->print_details(OS, G);
113  }
114  return Result;
115  }
116  std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
117  static std::string getNodeAttributes(const SDNode *N,
118  const SelectionDAG *Graph) {
119 #ifndef NDEBUG
120  const std::string &Attrs = Graph->getGraphAttrs(N);
121  if (!Attrs.empty()) {
122  if (Attrs.find("shape=") == std::string::npos)
123  return std::string("shape=Mrecord,") + Attrs;
124  else
125  return Attrs;
126  }
127 #endif
128  return "shape=Mrecord";
129  }
130 
133  GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
134  if (G->getRoot().getNode())
135  GW.emitEdge(nullptr, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
136  "color=blue,style=dashed");
137  }
138  };
139 }
140 
142  const SelectionDAG *G) {
144 }
145 
146 
147 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
148 /// rendered using 'dot'.
149 ///
150 void SelectionDAG::viewGraph(const std::string &Title) {
151 // This code is only for debugging!
152 #ifndef NDEBUG
153  ViewGraph(this, "dag." + getMachineFunction().getName(),
154  false, Title);
155 #else
156  errs() << "SelectionDAG::viewGraph is only available in debug builds on "
157  << "systems with Graphviz or gv!\n";
158 #endif // NDEBUG
159 }
160 
161 // This overload is defined out-of-line here instead of just using a
162 // default parameter because this is easiest for gdb to call.
164  viewGraph("");
165 }
166 
167 /// Just dump dot graph to a user-provided path and title.
168 /// This doesn't open the dot viewer program and
169 /// helps visualization when outside debugging session.
170 /// FileName expects absolute path. If provided
171 /// without any path separators then the file
172 /// will be created in the current directory.
173 /// Error will be emitted if the path is insane.
174 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
176  const Twine &Title) {
177  dumpDotGraphToFile(this, FileName, Title);
178 }
179 #endif
180 
181 /// clearGraphAttrs - Clear all previously defined node graph attributes.
182 /// Intended to be used from a debugging tool (eg. gdb).
184 #ifndef NDEBUG
185  NodeGraphAttrs.clear();
186 #else
187  errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
188  << " on systems with Graphviz or gv!\n";
189 #endif
190 }
191 
192 
193 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
194 ///
195 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
196 #ifndef NDEBUG
197  NodeGraphAttrs[N] = Attrs;
198 #else
199  errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
200  << " on systems with Graphviz or gv!\n";
201 #endif
202 }
203 
204 
205 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
206 /// Used from getNodeAttributes.
207 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
208 #ifndef NDEBUG
209  std::map<const SDNode *, std::string>::const_iterator I =
210  NodeGraphAttrs.find(N);
211 
212  if (I != NodeGraphAttrs.end())
213  return I->second;
214  else
215  return "";
216 #else
217  errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
218  << " on systems with Graphviz or gv!\n";
219  return std::string();
220 #endif
221 }
222 
223 /// setGraphColor - Convenience for setting node color attribute.
224 ///
225 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
226 #ifndef NDEBUG
227  NodeGraphAttrs[N] = std::string("color=") + Color;
228 #else
229  errs() << "SelectionDAG::setGraphColor is only available in debug builds"
230  << " on systems with Graphviz or gv!\n";
231 #endif
232 }
233 
234 /// setSubgraphColorHelper - Implement setSubgraphColor. Return
235 /// whether we truncated the search.
236 ///
237 bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
238  int level, bool &printed) {
239  bool hit_limit = false;
240 
241 #ifndef NDEBUG
242  if (level >= 20) {
243  if (!printed) {
244  printed = true;
245  LLVM_DEBUG(dbgs() << "setSubgraphColor hit max level\n");
246  }
247  return true;
248  }
249 
250  unsigned oldSize = visited.size();
251  visited.insert(N);
252  if (visited.size() != oldSize) {
253  setGraphColor(N, Color);
255  i != iend;
256  ++i) {
257  hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
258  }
259  }
260 #else
261  errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
262  << " on systems with Graphviz or gv!\n";
263 #endif
264  return hit_limit;
265 }
266 
267 /// setSubgraphColor - Convenience for setting subgraph color attribute.
268 ///
269 void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
270 #ifndef NDEBUG
271  DenseSet<SDNode *> visited;
272  bool printed = false;
273  if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
274  // Visually mark that we hit the limit
275  if (strcmp(Color, "red") == 0) {
276  setSubgraphColorHelper(N, "blue", visited, 0, printed);
277  } else if (strcmp(Color, "yellow") == 0) {
278  setSubgraphColorHelper(N, "green", visited, 0, printed);
279  }
280  }
281 
282 #else
283  errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
284  << " on systems with Graphviz or gv!\n";
285 #endif
286 }
287 
288 std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
289  std::string s;
291  O << "SU(" << SU->NodeNum << "): ";
292  if (SU->getNode()) {
293  SmallVector<SDNode *, 4> GluedNodes;
294  for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
295  GluedNodes.push_back(N);
296  while (!GluedNodes.empty()) {
297  O << DOTGraphTraits<SelectionDAG*>
298  ::getSimpleNodeLabel(GluedNodes.back(), DAG);
299  GluedNodes.pop_back();
300  if (!GluedNodes.empty())
301  O << "\n ";
302  }
303  } else {
304  O << "CROSS RC COPY";
305  }
306  return O.str();
307 }
308 
310  if (DAG) {
311  // Draw a special "GraphRoot" node to indicate the root of the graph.
312  GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
313  const SDNode *N = DAG->getRoot().getNode();
314  if (N && N->getNodeId() != -1)
315  GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
316  "color=blue,style=dashed");
317  }
318 }
EVT getValueType() const
Return the ValueType of the referenced return value.
static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I)
edgeTargetsEdgeSource - This method returns true if this outgoing edge should actually target another...
void emitSimpleNode(const void *ID, const std::string &Attr, const std::string &Label, unsigned NumEdgeSources=0, const std::vector< std::string > *EdgeSourceLabels=nullptr)
emitSimpleNode - Outputs a simple (non-record) node
Definition: GraphWriter.h:260
static std::string getSimpleNodeLabel(const SDNode *Node, const SelectionDAG *G)
std::string getOperationName(const SelectionDAG *G=nullptr) const
Return the opcode of this operation for printing.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:508
SDNode * getNode() const
Returns the representative SDNode for this SUnit.
Definition: ScheduleDAG.h:355
Implements a dense probed hash-table based set.
Definition: DenseSet.h:255
const std::string getGraphAttrs(const SDNode *N) const
Get graph attributes for a node.
const SDNode * getNode() const
void print_details(raw_ostream &OS, const SelectionDAG *G) const
virtual void getCustomGraphFeatures(GraphWriter< ScheduleDAG *> &GW) const
void setSubgraphColor(SDNode *N, const char *Color)
Convenience for setting subgraph color attribute.
static std::string getGraphName(const SelectionDAG *G)
SDNode * getNode() const
get the SDNode which holds the desired result
static std::string getEdgeDestLabel(const void *Node, unsigned i)
uint16_t PersistentId
Unique and persistent id per SDNode in the DAG.
*ViewGraph Emit a dot run run gv on the postscript *then cleanup For use from the debugger *void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
Definition: GraphWriter.h:376
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
static unsigned numEdgeDestLabels(const void *Node)
static StringRef getName(Value *V)
static std::string getEdgeSourceLabel(const void *Node, EdgeIter I)
std::string getNodeLabel(const void *, const GraphType &)
getNodeLabel - Given a node and a pointer to the top level graph, return the label to print in the no...
static bool isSimple(Instruction *I)
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:421
static SDNodeIterator begin(const SDNode *N)
std::string itostr(int64_t X)
Definition: StringExtras.h:246
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void setGraphColor(const SDNode *N, const char *Color)
Convenience for setting node color attribute.
static std::string getNodeIdentifierLabel(const SDNode *Node, const SelectionDAG *Graph)
const SDValue & getOperand(unsigned Num) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void clearGraphAttrs()
Clear all previously defined node graph attributes.
static std::string getEdgeAttributes(const void *Node, EdgeIter EI, const SelectionDAG *Graph)
If you want to override the dot attributes printed for a particular edge, override this method...
SDNode * getGluedNode() const
If this node has a glue operand, return the node to which the glue operand points.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
*DumpDotGraph Just dump a dot graph to the user provided file name LLVM_DUMP_METHOD void dumpDotGraphToFile(const GraphType &G, const Twine &FileName, const Twine &Title, bool ShortNames=false, const Twine &Name="")
Definition: GraphWriter.h:365
Extended Value Type.
Definition: ValueTypes.h:35
static std::string getNodeAttributes(const SDNode *N, const SelectionDAG *Graph)
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:539
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to &#39;dot...
Color
A "color", which is either even or odd.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:223
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
static EdgeIter getEdgeTarget(const void *Node, EdgeIter I)
getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is called to determine which outgo...
Represents one node in the SelectionDAG.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static void addCustomGraphFeatures(SelectionDAG *G, GraphWriter< SelectionDAG *> &GW)
std::string getGraphNodeLabel(const SUnit *SU) const override
Returns a label for an SUnit node in a visualization of the ScheduleDAG.
int getNodeId() const
Return the unique node id.
static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS)
#define I(x, y, z)
Definition: MD5.cpp:59
#define N
void emitEdge(const void *SrcNodeID, int SrcNodePort, const void *DestNodeID, int DestNodePort, const std::string &Attrs)
emitEdge - Output an edge from a simple node into the graph...
Definition: GraphWriter.h:283
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
Definition: SelectionDAG.h:494
unsigned NodeNum
Entry # of node in the node vector.
Definition: ScheduleDAG.h:264
void setGraphAttrs(const SDNode *N, const char *Attrs)
Set graph attributes for a node. (eg. "color=red".)
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:521
unsigned getResNo() const
get the index which selects a specific result in the SDNode
static SDNodeIterator end(const SDNode *N)
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
#define LLVM_DEBUG(X)
Definition: Debug.h:122
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
LLVM_DUMP_METHOD void dumpDotGraph(const Twine &FileName, const Twine &Title)
Just dump dot graph to a user-provided path and title.
Scheduling unit. This is a node in the scheduling DAG.
Definition: ScheduleDAG.h:242