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/ADT/StringExtras.h"
27 #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()).ResNo);
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 SDOperand 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().Val)
113 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
118 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
119 const SelectionDAG *G) {
120 std::string Op = Node->getOperationName(G);
122 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
123 Op += ": " + utostr(CSDN->getValue());
124 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
125 Op += ": " + ftostr(CSDN->getValueAPF());
126 } else if (const GlobalAddressSDNode *GADN =
127 dyn_cast<GlobalAddressSDNode>(Node)) {
128 int offset = GADN->getOffset();
129 Op += ": " + GADN->getGlobal()->getName();
131 Op += "+" + itostr(offset);
133 Op += itostr(offset);
134 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
135 Op += " " + itostr(FIDN->getIndex());
136 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
137 Op += " " + itostr(JTDN->getIndex());
138 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
139 if (CP->isMachineConstantPoolEntry()) {
140 std::ostringstream SS;
141 CP->getMachineCPVal()->print(SS);
142 Op += "<" + SS.str() + ">";
144 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
145 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
146 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
147 Op += "<" + utostr(CI->getZExtValue()) + ">";
149 std::ostringstream SS;
150 WriteAsOperand(SS, CP->getConstVal(), false);
151 Op += "<" + SS.str() + ">";
154 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
156 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
158 Op += LBB->getName();
159 //Op += " " + (const void*)BBDN->getBasicBlock();
160 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
161 if (G && R->getReg() != 0 &&
162 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
164 G->getTarget().getRegisterInfo()->getName(R->getReg());
166 Op += " #" + utostr(R->getReg());
168 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
169 Op += ": " + D->getCompileUnit()->getFileName();
170 Op += ":" + utostr(D->getLine());
171 if (D->getColumn() != 0)
172 Op += ":" + utostr(D->getColumn());
173 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
174 Op += ": LabelID=" + utostr(L->getLabelID());
175 } else if (const ExternalSymbolSDNode *ES =
176 dyn_cast<ExternalSymbolSDNode>(Node)) {
177 Op += "'" + std::string(ES->getSymbol()) + "'";
178 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
180 Op += "<" + M->getValue()->getName() + ">";
183 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
184 const Value *V = M->MO.getValue();
188 } else if (isa<PseudoSourceValue>(V)) {
189 // PseudoSourceValues don't have names, so use their print method.
190 std::ostringstream SS;
191 M->MO.getValue()->print(SS);
196 Op += '+' + itostr(M->MO.getOffset()) + '>';
197 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
198 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
199 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
200 Op = Op + " VT=" + N->getVT().getMVTString();
201 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
203 switch (LD->getExtensionType()) {
204 default: doExt = false; break;
206 Op = Op + "<anyext ";
216 Op += LD->getMemoryVT().getMVTString() + ">";
217 if (LD->isVolatile())
219 Op += LD->getIndexedModeName(LD->getAddressingMode());
220 if (LD->getAlignment() > 1)
221 Op += " A=" + utostr(LD->getAlignment());
222 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
223 if (ST->isTruncatingStore())
224 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
225 if (ST->isVolatile())
227 Op += ST->getIndexedModeName(ST->getAddressingMode());
228 if (ST->getAlignment() > 1)
229 Op += " A=" + utostr(ST->getAlignment());
233 Op += " Id=" + itostr(Node->getNodeId());
240 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
241 /// rendered using 'dot'.
243 void SelectionDAG::viewGraph(const std::string &Title) {
244 // This code is only for debugging!
246 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(),
249 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
250 << "systems with Graphviz or gv!\n";
255 /// clearGraphAttrs - Clear all previously defined node graph attributes.
256 /// Intended to be used from a debugging tool (eg. gdb).
257 void SelectionDAG::clearGraphAttrs() {
259 NodeGraphAttrs.clear();
261 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
262 << " on systems with Graphviz or gv!\n";
267 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
269 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
271 NodeGraphAttrs[N] = Attrs;
273 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
274 << " on systems with Graphviz or gv!\n";
279 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
280 /// Used from getNodeAttributes.
281 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
283 std::map<const SDNode *, std::string>::const_iterator I =
284 NodeGraphAttrs.find(N);
286 if (I != NodeGraphAttrs.end())
291 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
292 << " on systems with Graphviz or gv!\n";
293 return std::string("");
297 /// setGraphColor - Convenience for setting node color attribute.
299 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
301 NodeGraphAttrs[N] = std::string("color=") + Color;
303 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
304 << " on systems with Graphviz or gv!\n";
310 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
311 static std::string getGraphName(const ScheduleDAG *G) {
312 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
315 static bool renderGraphFromBottomUp() {
319 static bool hasNodeAddressLabel(const SUnit *Node,
320 const ScheduleDAG *Graph) {
324 /// If you want to override the dot attributes printed for a particular
325 /// edge, override this method.
326 template<typename EdgeIter>
327 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
328 if (EI.isSpecialDep())
329 return "color=cyan,style=dashed";
331 return "color=blue,style=dashed";
336 static std::string getNodeLabel(const SUnit *Node,
337 const ScheduleDAG *Graph);
338 static std::string getNodeAttributes(const SUnit *N,
339 const ScheduleDAG *Graph) {
340 return "shape=Mrecord";
343 static void addCustomGraphFeatures(ScheduleDAG *G,
344 GraphWriter<ScheduleDAG*> &GW) {
345 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
346 const SDNode *N = G->DAG.getRoot().Val;
347 if (N && N->getNodeId() != -1)
348 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1, "");
353 std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
354 const ScheduleDAG *G) {
357 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
358 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
363 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
365 Op += "<CROSS RC COPY>";
371 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
372 /// rendered using 'dot'.
374 void ScheduleDAG::viewGraph() {
375 // This code is only for debugging!
377 ViewGraph(this, "dag." + MF->getFunction()->getName(),
378 "Scheduling-Units Graph for " + MF->getFunction()->getName() + ':' +
379 BB->getBasicBlock()->getName());
381 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
382 << "systems with Graphviz or gv!\n";