1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG::viewGraph method.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Constants.h"
15 #include "llvm/Function.h"
16 #include "llvm/Assembly/Writer.h"
17 #include "llvm/CodeGen/SelectionDAG.h"
18 #include "llvm/CodeGen/ScheduleDAG.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/PseudoSourceValue.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/GraphWriter.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/Config/config.h"
34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
35 static bool hasEdgeDestLabels() {
39 static unsigned numEdgeDestLabels(const void *Node) {
40 return ((const SDNode *) Node)->getNumValues();
43 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
44 return ((const SDNode *) Node)->getValueType(i).getMVTString();
47 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
48 /// should actually target another edge source, not a node. If this method is
49 /// implemented, getEdgeTarget should be implemented.
50 template<typename EdgeIter>
51 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
55 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
56 /// called to determine which outgoing edge of Node is the target of this
58 template<typename EdgeIter>
59 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
60 SDNode *TargetNode = *I;
61 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
62 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
66 static std::string getGraphName(const SelectionDAG *G) {
67 return G->getMachineFunction().getFunction()->getName();
70 static bool renderGraphFromBottomUp() {
74 static bool hasNodeAddressLabel(const SDNode *Node,
75 const SelectionDAG *Graph) {
79 /// If you want to override the dot attributes printed for a particular
80 /// edge, override this method.
81 template<typename EdgeIter>
82 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
83 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
84 MVT VT = Op.getValueType();
86 return "color=red,style=bold";
87 else if (VT == MVT::Other)
88 return "color=blue,style=dashed";
93 static std::string getNodeLabel(const SDNode *Node,
94 const SelectionDAG *Graph);
95 static std::string getNodeAttributes(const SDNode *N,
96 const SelectionDAG *Graph) {
98 const std::string &Attrs = Graph->getGraphAttrs(N);
100 if (Attrs.find("shape=") == std::string::npos)
101 return std::string("shape=Mrecord,") + Attrs;
106 return "shape=Mrecord";
109 static void addCustomGraphFeatures(SelectionDAG *G,
110 GraphWriter<SelectionDAG*> &GW) {
111 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
112 if (G->getRoot().getNode())
113 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
114 "color=blue,style=dashed");
119 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
120 const SelectionDAG *G) {
121 std::string Op = Node->getOperationName(G);
123 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
124 Op += ": " + utostr(CSDN->getZExtValue());
125 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
126 Op += ": " + ftostr(CSDN->getValueAPF());
127 } else if (const GlobalAddressSDNode *GADN =
128 dyn_cast<GlobalAddressSDNode>(Node)) {
129 int offset = GADN->getOffset();
130 Op += ": " + GADN->getGlobal()->getName();
132 Op += "+" + itostr(offset);
134 Op += itostr(offset);
135 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
136 Op += " " + itostr(FIDN->getIndex());
137 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
138 Op += " " + itostr(JTDN->getIndex());
139 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
140 if (CP->isMachineConstantPoolEntry()) {
143 raw_string_ostream OSS(Op);
144 OSS << *CP->getMachineCPVal();
148 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
149 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
150 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
151 Op += "<" + utostr(CI->getZExtValue()) + ">";
155 raw_string_ostream OSS(Op);
156 WriteAsOperand(OSS, CP->getConstVal(), false);
161 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
163 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
165 Op += LBB->getName();
166 //Op += " " + (const void*)BBDN->getBasicBlock();
167 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
168 if (G && R->getReg() != 0 &&
169 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
171 G->getTarget().getRegisterInfo()->getName(R->getReg());
173 Op += " #" + utostr(R->getReg());
175 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
176 Op += ": " + D->getCompileUnit()->getFileName();
177 Op += ":" + utostr(D->getLine());
178 if (D->getColumn() != 0)
179 Op += ":" + utostr(D->getColumn());
180 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
181 Op += ": LabelID=" + utostr(L->getLabelID());
182 } else if (const CallSDNode *C = dyn_cast<CallSDNode>(Node)) {
183 Op += ": CallingConv=" + utostr(C->getCallingConv());
187 Op += ", isTailCall";
188 } else if (const SymbolSDNode *S = dyn_cast<SymbolSDNode>(Node)) {
189 Op += "'" + std::string(S->getSymbol()) + "'";
190 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
192 Op += "<" + M->getValue()->getName() + ">";
195 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
196 const Value *V = M->MO.getValue();
200 } else if (isa<PseudoSourceValue>(V)) {
201 // PseudoSourceValues don't have names, so use their print method.
203 raw_string_ostream OSS(Op);
204 OSS << *M->MO.getValue();
209 Op += '+' + itostr(M->MO.getOffset()) + '>';
210 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
211 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
212 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
213 Op = Op + " VT=" + N->getVT().getMVTString();
214 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
216 switch (LD->getExtensionType()) {
217 default: doExt = false; break;
219 Op = Op + "<anyext ";
229 Op += LD->getMemoryVT().getMVTString() + ">";
230 if (LD->isVolatile())
232 Op += LD->getIndexedModeName(LD->getAddressingMode());
233 if (LD->getAlignment() > 1)
234 Op += " A=" + utostr(LD->getAlignment());
235 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
236 if (ST->isTruncatingStore())
237 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
238 if (ST->isVolatile())
240 Op += ST->getIndexedModeName(ST->getAddressingMode());
241 if (ST->getAlignment() > 1)
242 Op += " A=" + utostr(ST->getAlignment());
246 Op += " Id=" + itostr(Node->getNodeId());
253 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
254 /// rendered using 'dot'.
256 void SelectionDAG::viewGraph(const std::string &Title) {
257 // This code is only for debugging!
259 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(),
262 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
263 << "systems with Graphviz or gv!\n";
267 // This overload is defined out-of-line here instead of just using a
268 // default parameter because this is easiest for gdb to call.
269 void SelectionDAG::viewGraph() {
273 /// clearGraphAttrs - Clear all previously defined node graph attributes.
274 /// Intended to be used from a debugging tool (eg. gdb).
275 void SelectionDAG::clearGraphAttrs() {
277 NodeGraphAttrs.clear();
279 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
280 << " on systems with Graphviz or gv!\n";
285 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
287 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
289 NodeGraphAttrs[N] = Attrs;
291 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
292 << " on systems with Graphviz or gv!\n";
297 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
298 /// Used from getNodeAttributes.
299 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
301 std::map<const SDNode *, std::string>::const_iterator I =
302 NodeGraphAttrs.find(N);
304 if (I != NodeGraphAttrs.end())
309 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
310 << " on systems with Graphviz or gv!\n";
311 return std::string("");
315 /// setGraphColor - Convenience for setting node color attribute.
317 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
319 NodeGraphAttrs[N] = std::string("color=") + Color;
321 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
322 << " on systems with Graphviz or gv!\n";
328 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
329 static std::string getGraphName(const ScheduleDAG *G) {
330 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
333 static bool renderGraphFromBottomUp() {
337 static bool hasNodeAddressLabel(const SUnit *Node,
338 const ScheduleDAG *Graph) {
342 /// If you want to override the dot attributes printed for a particular
343 /// edge, override this method.
344 template<typename EdgeIter>
345 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
346 if (EI.isSpecialDep())
347 return "color=cyan,style=dashed";
349 return "color=blue,style=dashed";
354 static std::string getNodeLabel(const SUnit *Node,
355 const ScheduleDAG *Graph);
356 static std::string getNodeAttributes(const SUnit *N,
357 const ScheduleDAG *Graph) {
358 return "shape=Mrecord";
361 static void addCustomGraphFeatures(ScheduleDAG *G,
362 GraphWriter<ScheduleDAG*> &GW) {
363 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
364 const SDNode *N = G->DAG.getRoot().getNode();
365 if (N && N->getNodeId() != -1)
366 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1,
367 "color=blue,style=dashed");
372 std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
373 const ScheduleDAG *G) {
376 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
377 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
382 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
384 Op += "<CROSS RC COPY>";
390 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
391 /// rendered using 'dot'.
393 void ScheduleDAG::viewGraph() {
394 // This code is only for debugging!
396 ViewGraph(this, "dag." + MF->getFunction()->getName(),
397 "Scheduling-Units Graph for " + MF->getFunction()->getName() + ':' +
398 BB->getBasicBlock()->getName());
400 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
401 << "systems with Graphviz or gv!\n";