LLVM API Documentation

SelectionDAGPrinter.cpp

Go to the documentation of this file.
00001 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This implements the SelectionDAG::viewGraph method.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "ScheduleDAGSDNodes.h"
00015 #include "llvm/Constants.h"
00016 #include "llvm/Function.h"
00017 #include "llvm/Assembly/Writer.h"
00018 #include "llvm/CodeGen/SelectionDAG.h"
00019 #include "llvm/CodeGen/MachineConstantPool.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineModuleInfo.h"
00022 #include "llvm/CodeGen/PseudoSourceValue.h"
00023 #include "llvm/Analysis/DebugInfo.h"
00024 #include "llvm/Target/TargetRegisterInfo.h"
00025 #include "llvm/Target/TargetMachine.h"
00026 #include "llvm/Support/Debug.h"
00027 #include "llvm/Support/GraphWriter.h"
00028 #include "llvm/Support/raw_ostream.h"
00029 #include "llvm/ADT/DenseSet.h"
00030 #include "llvm/ADT/StringExtras.h"
00031 #include "llvm/Config/config.h"
00032 #include <fstream>
00033 using namespace llvm;
00034 
00035 namespace llvm {
00036   template<>
00037   struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
00038     static bool hasEdgeDestLabels() {
00039       return true;
00040     }
00041 
00042     static unsigned numEdgeDestLabels(const void *Node) {
00043       return ((const SDNode *) Node)->getNumValues();
00044     }
00045 
00046     static std::string getEdgeDestLabel(const void *Node, unsigned i) {
00047       return ((const SDNode *) Node)->getValueType(i).getEVTString();
00048     }
00049 
00050     /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
00051     /// should actually target another edge source, not a node.  If this method is
00052     /// implemented, getEdgeTarget should be implemented.
00053     template<typename EdgeIter>
00054     static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
00055       return true;
00056     }
00057 
00058     /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
00059     /// called to determine which outgoing edge of Node is the target of this
00060     /// edge.
00061     template<typename EdgeIter>
00062     static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
00063       SDNode *TargetNode = *I;
00064       SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
00065       std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
00066       return NI;
00067     }
00068 
00069     static std::string getGraphName(const SelectionDAG *G) {
00070       return G->getMachineFunction().getFunction()->getName();
00071     }
00072 
00073     static bool renderGraphFromBottomUp() {
00074       return true;
00075     }
00076     
00077     static bool hasNodeAddressLabel(const SDNode *Node,
00078                                     const SelectionDAG *Graph) {
00079       return true;
00080     }
00081     
00082     /// If you want to override the dot attributes printed for a particular
00083     /// edge, override this method.
00084     template<typename EdgeIter>
00085     static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
00086       SDValue Op = EI.getNode()->getOperand(EI.getOperand());
00087       EVT VT = Op.getValueType();
00088       if (VT == MVT::Flag)
00089         return "color=red,style=bold";
00090       else if (VT == MVT::Other)
00091         return "color=blue,style=dashed";
00092       return "";
00093     }
00094     
00095 
00096     static std::string getNodeLabel(const SDNode *Node,
00097                                     const SelectionDAG *Graph,
00098                                     bool ShortNames);
00099     static std::string getNodeAttributes(const SDNode *N,
00100                                          const SelectionDAG *Graph) {
00101 #ifndef NDEBUG
00102       const std::string &Attrs = Graph->getGraphAttrs(N);
00103       if (!Attrs.empty()) {
00104         if (Attrs.find("shape=") == std::string::npos)
00105           return std::string("shape=Mrecord,") + Attrs;
00106         else
00107           return Attrs;
00108       }
00109 #endif
00110       return "shape=Mrecord";
00111     }
00112 
00113     static void addCustomGraphFeatures(SelectionDAG *G,
00114                                        GraphWriter<SelectionDAG*> &GW) {
00115       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
00116       if (G->getRoot().getNode())
00117         GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
00118                     "color=blue,style=dashed");
00119     }
00120   };
00121 }
00122 
00123 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
00124                                                         const SelectionDAG *G,
00125                                                         bool ShortNames) {
00126   std::string Result = Node->getOperationName(G);
00127   {
00128     raw_string_ostream OS(Result);
00129     Node->print_details(OS, G);
00130   }
00131   return Result;
00132 }
00133 
00134 
00135 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
00136 /// rendered using 'dot'.
00137 ///
00138 void SelectionDAG::viewGraph(const std::string &Title) {
00139 // This code is only for debugging!
00140 #ifndef NDEBUG
00141   ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(), 
00142             false, Title);
00143 #else
00144   cerr << "SelectionDAG::viewGraph is only available in debug builds on "
00145        << "systems with Graphviz or gv!\n";
00146 #endif  // NDEBUG
00147 }
00148 
00149 // This overload is defined out-of-line here instead of just using a
00150 // default parameter because this is easiest for gdb to call.
00151 void SelectionDAG::viewGraph() {
00152   viewGraph("");
00153 }
00154 
00155 /// clearGraphAttrs - Clear all previously defined node graph attributes.
00156 /// Intended to be used from a debugging tool (eg. gdb).
00157 void SelectionDAG::clearGraphAttrs() {
00158 #ifndef NDEBUG
00159   NodeGraphAttrs.clear();
00160 #else
00161   cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
00162        << " on systems with Graphviz or gv!\n";
00163 #endif
00164 }
00165 
00166 
00167 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
00168 ///
00169 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
00170 #ifndef NDEBUG
00171   NodeGraphAttrs[N] = Attrs;
00172 #else
00173   cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
00174        << " on systems with Graphviz or gv!\n";
00175 #endif
00176 }
00177 
00178 
00179 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
00180 /// Used from getNodeAttributes.
00181 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
00182 #ifndef NDEBUG
00183   std::map<const SDNode *, std::string>::const_iterator I =
00184     NodeGraphAttrs.find(N);
00185     
00186   if (I != NodeGraphAttrs.end())
00187     return I->second;
00188   else
00189     return "";
00190 #else
00191   cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
00192        << " on systems with Graphviz or gv!\n";
00193   return std::string("");
00194 #endif
00195 }
00196 
00197 /// setGraphColor - Convenience for setting node color attribute.
00198 ///
00199 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
00200 #ifndef NDEBUG
00201   NodeGraphAttrs[N] = std::string("color=") + Color;
00202 #else
00203   cerr << "SelectionDAG::setGraphColor is only available in debug builds"
00204        << " on systems with Graphviz or gv!\n";
00205 #endif
00206 }
00207 
00208 /// setSubgraphColorHelper - Implement setSubgraphColor.  Return
00209 /// whether we truncated the search.
00210 ///
00211 bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
00212                                           int level, bool &printed) {
00213   bool hit_limit = false;
00214 
00215 #ifndef NDEBUG
00216   if (level >= 20) {
00217     if (!printed) {
00218       printed = true;
00219       DOUT << "setSubgraphColor hit max level\n";
00220     }
00221     return true;
00222   }
00223 
00224   unsigned oldSize = visited.size();
00225   visited.insert(N);
00226   if (visited.size() != oldSize) {
00227     setGraphColor(N, Color);
00228     for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
00229         i != iend;
00230         ++i) {
00231       hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
00232     }
00233   }
00234 #else
00235   cerr << "SelectionDAG::setSubgraphColor is only available in debug builds"
00236        << " on systems with Graphviz or gv!\n";
00237 #endif
00238   return hit_limit;
00239 }
00240 
00241 /// setSubgraphColor - Convenience for setting subgraph color attribute.
00242 ///
00243 void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
00244 #ifndef NDEBUG
00245   DenseSet<SDNode *> visited;
00246   bool printed = false;
00247   if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
00248     // Visually mark that we hit the limit
00249     if (strcmp(Color, "red") == 0) {
00250       setSubgraphColorHelper(N, "blue", visited, 0, printed);
00251     }
00252     else if (strcmp(Color, "yellow") == 0) {
00253       setSubgraphColorHelper(N, "green", visited, 0, printed);
00254     }
00255   }
00256 
00257 #else
00258   cerr << "SelectionDAG::setSubgraphColor is only available in debug builds"
00259        << " on systems with Graphviz or gv!\n";
00260 #endif
00261 }
00262 
00263 std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
00264   std::string s;
00265   raw_string_ostream O(s);
00266   O << "SU(" << SU->NodeNum << "): ";
00267   if (SU->getNode()) {
00268     SmallVector<SDNode *, 4> FlaggedNodes;
00269     for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
00270       FlaggedNodes.push_back(N);
00271     while (!FlaggedNodes.empty()) {
00272       O << DOTGraphTraits<SelectionDAG*>::getNodeLabel(FlaggedNodes.back(),
00273                                                        DAG, false);
00274       FlaggedNodes.pop_back();
00275       if (!FlaggedNodes.empty())
00276         O << "\n    ";
00277     }
00278   } else {
00279     O << "CROSS RC COPY";
00280   }
00281   return O.str();
00282 }
00283 
00284 void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
00285   if (DAG) {
00286     // Draw a special "GraphRoot" node to indicate the root of the graph.
00287     GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
00288     const SDNode *N = DAG->getRoot().getNode();
00289     if (N && N->getNodeId() != -1)
00290       GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
00291                   "color=blue,style=dashed");
00292   }
00293 }