2005-01-10 23:08:40 +00:00
|
|
|
//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2005-01-10 23:08:40 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2005-01-10 23:08:40 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements the SelectionDAG::viewGraph method.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-08-26 17:15:30 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
2005-01-10 23:08:40 +00:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2005-08-19 21:21:16 +00:00
|
|
|
#include "llvm/Target/MRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2005-01-10 23:08:40 +00:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2005-11-19 07:44:09 +00:00
|
|
|
#include "llvm/System/Path.h"
|
2005-01-11 00:34:33 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2005-07-15 22:48:31 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2005-01-10 23:08:40 +00:00
|
|
|
#include <fstream>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2005-01-10 23:26:00 +00:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(const SelectionDAG *G) {
|
|
|
|
return G->getMachineFunction().getFunction()->getName();
|
|
|
|
}
|
2005-01-11 00:34:33 +00:00
|
|
|
|
|
|
|
static bool renderGraphFromBottomUp() {
|
|
|
|
return true;
|
2005-01-10 23:26:00 +00:00
|
|
|
}
|
2005-10-01 00:17:07 +00:00
|
|
|
|
|
|
|
static bool hasNodeAddressLabel(const SDNode *Node,
|
|
|
|
const SelectionDAG *Graph) {
|
|
|
|
return true;
|
|
|
|
}
|
2005-01-10 23:26:00 +00:00
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
static std::string getNodeLabel(const SDNode *Node,
|
|
|
|
const SelectionDAG *Graph);
|
2005-01-10 23:26:00 +00:00
|
|
|
static std::string getNodeAttributes(const SDNode *N) {
|
|
|
|
return "shape=Mrecord";
|
|
|
|
}
|
2005-01-10 23:52:04 +00:00
|
|
|
|
|
|
|
static void addCustomGraphFeatures(SelectionDAG *G,
|
|
|
|
GraphWriter<SelectionDAG*> &GW) {
|
|
|
|
GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
|
|
|
|
GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
|
|
|
|
}
|
2005-01-10 23:26:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
|
|
|
|
const SelectionDAG *G) {
|
2005-08-16 18:31:23 +00:00
|
|
|
std::string Op = Node->getOperationName(G);
|
2005-01-11 22:21:04 +00:00
|
|
|
|
2005-08-16 18:31:23 +00:00
|
|
|
for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
|
|
|
|
if (Node->getValueType(i) == MVT::Other)
|
|
|
|
Op += ":ch";
|
|
|
|
else
|
|
|
|
Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
|
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
|
|
|
|
Op += ": " + utostr(CSDN->getValue());
|
|
|
|
} else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
|
|
|
|
Op += ": " + ftostr(CSDN->getValue());
|
2005-04-21 22:36:52 +00:00
|
|
|
} else if (const GlobalAddressSDNode *GADN =
|
2005-01-11 00:34:33 +00:00
|
|
|
dyn_cast<GlobalAddressSDNode>(Node)) {
|
|
|
|
Op += ": " + GADN->getGlobal()->getName();
|
2005-04-22 04:01:18 +00:00
|
|
|
} else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
|
2005-01-11 00:34:33 +00:00
|
|
|
Op += " " + itostr(FIDN->getIndex());
|
|
|
|
} else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
|
2005-08-26 17:15:30 +00:00
|
|
|
if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
|
|
|
|
Op += "<" + ftostr(CFP->getValue()) + ">";
|
2005-04-22 04:01:18 +00:00
|
|
|
} else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
|
2005-01-11 00:34:33 +00:00
|
|
|
Op = "BB: ";
|
|
|
|
const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
|
|
|
|
if (LBB)
|
|
|
|
Op += LBB->getName();
|
|
|
|
//Op += " " + (const void*)BBDN->getBasicBlock();
|
2005-08-16 21:55:35 +00:00
|
|
|
} else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
|
2005-11-19 06:58:46 +00:00
|
|
|
if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
|
2005-08-19 21:21:16 +00:00
|
|
|
Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
|
|
|
|
} else {
|
|
|
|
Op += " #" + utostr(R->getReg());
|
|
|
|
}
|
2005-01-11 00:34:33 +00:00
|
|
|
} else if (const ExternalSymbolSDNode *ES =
|
|
|
|
dyn_cast<ExternalSymbolSDNode>(Node)) {
|
|
|
|
Op += "'" + std::string(ES->getSymbol()) + "'";
|
2005-05-09 04:08:27 +00:00
|
|
|
} else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
|
|
|
|
if (M->getValue())
|
|
|
|
Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
|
|
|
|
else
|
|
|
|
Op += "<null:" + itostr(M->getOffset()) + ">";
|
2005-08-18 03:31:02 +00:00
|
|
|
} else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
|
2005-08-24 18:30:00 +00:00
|
|
|
Op = Op + " VT=" + getValueTypeString(N->getVT());
|
2005-01-11 00:34:33 +00:00
|
|
|
}
|
|
|
|
return Op;
|
|
|
|
}
|
2005-04-21 22:36:52 +00:00
|
|
|
|
2005-01-11 00:34:33 +00:00
|
|
|
|
2005-01-10 23:08:40 +00:00
|
|
|
/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
|
|
|
|
/// rendered using 'dot'.
|
|
|
|
///
|
|
|
|
void SelectionDAG::viewGraph() {
|
2005-07-14 05:17:43 +00:00
|
|
|
// This code is only for debugging!
|
2005-07-14 05:33:13 +00:00
|
|
|
#ifndef NDEBUG
|
2005-11-20 03:45:52 +00:00
|
|
|
sys::Path TempDir = sys::Path::GetTemporaryDirectory();
|
|
|
|
sys::Path Filename = TempDir;
|
|
|
|
Filename.appendComponent("dag." + getMachineFunction().getFunction()->getName() + ".dot");
|
|
|
|
std::cerr << "Writing '" << Filename.toString() << "'... ";
|
|
|
|
std::ofstream F(Filename.toString().c_str());
|
2005-01-10 23:08:40 +00:00
|
|
|
|
|
|
|
if (!F) {
|
|
|
|
std::cerr << " error opening file for writing!\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteGraph(F, this);
|
|
|
|
F.close();
|
|
|
|
std::cerr << "\n";
|
|
|
|
|
2005-07-14 01:10:55 +00:00
|
|
|
#ifdef HAVE_GRAPHVIZ
|
|
|
|
std::cerr << "Running 'Graphviz' program... " << std::flush;
|
2005-11-20 03:45:52 +00:00
|
|
|
if (system((LLVM_PATH_GRAPHVIZ " " + Filename.toString()).c_str())) {
|
2005-07-14 01:10:55 +00:00
|
|
|
std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
|
|
|
|
} else {
|
2005-11-20 03:45:52 +00:00
|
|
|
Filename.eraseFromDisk();
|
2005-07-14 01:10:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-08-04 14:22:41 +00:00
|
|
|
#endif // HAVE_GRAPHVIZ
|
2005-07-14 01:10:55 +00:00
|
|
|
|
2005-07-14 05:33:13 +00:00
|
|
|
#ifdef HAVE_GV
|
2005-01-10 23:08:40 +00:00
|
|
|
std::cerr << "Running 'dot' program... " << std::flush;
|
2005-11-20 03:45:52 +00:00
|
|
|
sys::Path PSFilename = TempDir;
|
|
|
|
PSFilename.appendComponent("dag.tempgraph.ps");
|
|
|
|
if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename.toString()
|
|
|
|
+ " > " + PSFilename.toString()).c_str())) {
|
2005-07-14 01:10:55 +00:00
|
|
|
std::cerr << "Error viewing graph: 'dot' not in path?\n";
|
2005-01-10 23:08:40 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "\n";
|
2005-11-20 03:45:52 +00:00
|
|
|
system((LLVM_PATH_GV " " + PSFilename.toString()).c_str());
|
|
|
|
system((LLVM_PATH_GV " "+TempDir.toString()+ "dag.tempgraph.ps").c_str());
|
2005-01-10 23:08:40 +00:00
|
|
|
}
|
2005-11-20 03:45:52 +00:00
|
|
|
Filename.eraseFromDisk();
|
|
|
|
PSFilename.eraseFromDisk();
|
2005-07-14 05:33:13 +00:00
|
|
|
return;
|
2005-08-04 14:22:41 +00:00
|
|
|
#endif // HAVE_GV
|
|
|
|
#endif // NDEBUG
|
2005-07-14 05:33:13 +00:00
|
|
|
std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
2005-08-04 14:22:41 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2005-11-20 03:45:52 +00:00
|
|
|
Filename.eraseFromDisk();
|
|
|
|
TempDir.eraseFromDisk(true);
|
2005-08-04 14:22:41 +00:00
|
|
|
#endif
|
2005-01-10 23:08:40 +00:00
|
|
|
}
|