Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
Line
Count
Source (jump to first uncovered line)
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"
16
#include "llvm/CodeGen/MachineConstantPool.h"
17
#include "llvm/CodeGen/MachineFunction.h"
18
#include "llvm/CodeGen/SelectionDAG.h"
19
#include "llvm/CodeGen/TargetRegisterInfo.h"
20
#include "llvm/IR/Constants.h"
21
#include "llvm/Support/Debug.h"
22
#include "llvm/Support/GraphWriter.h"
23
#include "llvm/Support/raw_ostream.h"
24
#include "llvm/Target/TargetMachine.h"
25
using namespace llvm;
26
27
#define DEBUG_TYPE "dag-printer"
28
29
namespace llvm {
30
  template<>
31
  struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
32
33
    explicit DOTGraphTraits(bool isSimple=false) :
34
0
      DefaultDOTGraphTraits(isSimple) {}
35
36
0
    static bool hasEdgeDestLabels() {
37
0
      return true;
38
0
    }
39
40
0
    static unsigned numEdgeDestLabels(const void *Node) {
41
0
      return ((const SDNode *) Node)->getNumValues();
42
0
    }
43
44
0
    static std::string getEdgeDestLabel(const void *Node, unsigned i) {
45
0
      return ((const SDNode *) Node)->getValueType(i).getEVTString();
46
0
    }
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
0
    static std::string getGraphName(const SelectionDAG *G) {
73
0
      return G->getMachineFunction().getName();
74
0
    }
75
76
0
    static bool renderGraphFromBottomUp() {
77
0
      return true;
78
0
    }
79
80
    static std::string getNodeIdentifierLabel(const SDNode *Node,
81
0
                                              const SelectionDAG *Graph) {
82
0
      std::string R;
83
0
      raw_string_ostream OS(R);
84
0
#ifndef NDEBUG
85
0
      OS << 't' << Node->PersistentId;
86
0
#else
87
0
      OS << static_cast<const void *>(Node);
88
0
#endif
89
0
      return R;
90
0
    }
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
0
                                          const SelectionDAG *G) {
109
0
      std::string Result = Node->getOperationName(G);
110
0
      {
111
0
        raw_string_ostream OS(Result);
112
0
        Node->print_details(OS, G);
113
0
      }
114
0
      return Result;
115
0
    }
116
    std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
117
    static std::string getNodeAttributes(const SDNode *N,
118
0
                                         const SelectionDAG *Graph) {
119
0
#ifndef NDEBUG
120
0
      const std::string &Attrs = Graph->getGraphAttrs(N);
121
0
      if (!Attrs.empty()) {
122
0
        if (Attrs.find("shape=") == std::string::npos)
123
0
          return std::string("shape=Mrecord,") + Attrs;
124
0
        else
125
0
          return Attrs;
126
0
      }
127
0
#endif
128
0
      return "shape=Mrecord";
129
0
    }
130
131
    static void addCustomGraphFeatures(SelectionDAG *G,
132
0
                                       GraphWriter<SelectionDAG*> &GW) {
133
0
      GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
134
0
      if (G->getRoot().getNode())
135
0
        GW.emitEdge(nullptr, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
136
0
                    "color=blue,style=dashed");
137
0
    }
138
  };
139
}
140
141
std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
142
0
                                                        const SelectionDAG *G) {
143
0
  return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
144
0
}
145
146
147
/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
148
/// rendered using 'dot'.
149
///
150
0
void SelectionDAG::viewGraph(const std::string &Title) {
151
0
// 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
0
         << "systems with Graphviz or gv!\n";
158
0
#endif  // NDEBUG
159
0
}
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.
163
0
void SelectionDAG::viewGraph() {
164
0
  viewGraph("");
165
0
}
166
167
/// clearGraphAttrs - Clear all previously defined node graph attributes.
168
/// Intended to be used from a debugging tool (eg. gdb).
169
0
void SelectionDAG::clearGraphAttrs() {
170
#ifndef NDEBUG
171
  NodeGraphAttrs.clear();
172
#else
173
  errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
174
0
         << " on systems with Graphviz or gv!\n";
175
0
#endif
176
0
}
177
178
179
/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
180
///
181
0
void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
182
#ifndef NDEBUG
183
  NodeGraphAttrs[N] = Attrs;
184
#else
185
  errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
186
0
         << " on systems with Graphviz or gv!\n";
187
0
#endif
188
0
}
189
190
191
/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
192
/// Used from getNodeAttributes.
193
0
const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
194
#ifndef NDEBUG
195
  std::map<const SDNode *, std::string>::const_iterator I =
196
    NodeGraphAttrs.find(N);
197
198
  if (I != NodeGraphAttrs.end())
199
    return I->second;
200
  else
201
    return "";
202
#else
203
  errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
204
0
         << " on systems with Graphviz or gv!\n";
205
0
  return std::string();
206
0
#endif
207
0
}
208
209
/// setGraphColor - Convenience for setting node color attribute.
210
///
211
0
void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
212
#ifndef NDEBUG
213
  NodeGraphAttrs[N] = std::string("color=") + Color;
214
#else
215
  errs() << "SelectionDAG::setGraphColor is only available in debug builds"
216
0
         << " on systems with Graphviz or gv!\n";
217
0
#endif
218
0
}
219
220
/// setSubgraphColorHelper - Implement setSubgraphColor.  Return
221
/// whether we truncated the search.
222
///
223
bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
224
0
                                          int level, bool &printed) {
225
0
  bool hit_limit = false;
226
0
227
#ifndef NDEBUG
228
  if (level >= 20) {
229
    if (!printed) {
230
      printed = true;
231
      LLVM_DEBUG(dbgs() << "setSubgraphColor hit max level\n");
232
    }
233
    return true;
234
  }
235
236
  unsigned oldSize = visited.size();
237
  visited.insert(N);
238
  if (visited.size() != oldSize) {
239
    setGraphColor(N, Color);
240
    for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
241
        i != iend;
242
        ++i) {
243
      hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
244
    }
245
  }
246
#else
247
  errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
248
0
         << " on systems with Graphviz or gv!\n";
249
0
#endif
250
0
  return hit_limit;
251
0
}
252
253
/// setSubgraphColor - Convenience for setting subgraph color attribute.
254
///
255
0
void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
256
#ifndef NDEBUG
257
  DenseSet<SDNode *> visited;
258
  bool printed = false;
259
  if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
260
    // Visually mark that we hit the limit
261
    if (strcmp(Color, "red") == 0) {
262
      setSubgraphColorHelper(N, "blue", visited, 0, printed);
263
    } else if (strcmp(Color, "yellow") == 0) {
264
      setSubgraphColorHelper(N, "green", visited, 0, printed);
265
    }
266
  }
267
268
#else
269
  errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
270
0
         << " on systems with Graphviz or gv!\n";
271
0
#endif
272
0
}
273
274
0
std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
275
0
  std::string s;
276
0
  raw_string_ostream O(s);
277
0
  O << "SU(" << SU->NodeNum << "): ";
278
0
  if (SU->getNode()) {
279
0
    SmallVector<SDNode *, 4> GluedNodes;
280
0
    for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
281
0
      GluedNodes.push_back(N);
282
0
    while (!GluedNodes.empty()) {
283
0
      O << DOTGraphTraits<SelectionDAG*>
284
0
        ::getSimpleNodeLabel(GluedNodes.back(), DAG);
285
0
      GluedNodes.pop_back();
286
0
      if (!GluedNodes.empty())
287
0
        O << "\n    ";
288
0
    }
289
0
  } else {
290
0
    O << "CROSS RC COPY";
291
0
  }
292
0
  return O.str();
293
0
}
294
295
0
void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
296
0
  if (DAG) {
297
0
    // Draw a special "GraphRoot" node to indicate the root of the graph.
298
0
    GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
299
0
    const SDNode *N = DAG->getRoot().getNode();
300
0
    if (N && N->getNodeId() != -1)
301
0
      GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
302
0
                  "color=blue,style=dashed");
303
0
  }
304
0
}