2010-07-12 05:26:37 +00:00
|
|
|
//===-- CFGPrinter.h - CFG printer external interface -----------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-04-26 16:27:53 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-04-26 16:27:53 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines external functions that can be called to explicitly
|
|
|
|
// instantiate the CFG printer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_CFGPRINTER_H
|
|
|
|
#define LLVM_ANALYSIS_CFGPRINTER_H
|
|
|
|
|
2011-04-11 09:35:34 +00:00
|
|
|
#include "llvm/Constants.h"
|
2009-10-18 04:09:11 +00:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
|
|
|
#include "llvm/Support/CFG.h"
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
|
2009-11-30 12:38:13 +00:00
|
|
|
|
|
|
|
DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
|
|
|
|
|
2009-10-18 04:09:11 +00:00
|
|
|
static std::string getGraphName(const Function *F) {
|
|
|
|
return "CFG for '" + F->getNameStr() + "' function";
|
|
|
|
}
|
|
|
|
|
2009-11-30 12:38:47 +00:00
|
|
|
static std::string getSimpleNodeLabel(const BasicBlock *Node,
|
|
|
|
const Function *Graph) {
|
|
|
|
if (!Node->getName().empty())
|
2009-11-30 11:55:24 +00:00
|
|
|
return Node->getNameStr();
|
2009-10-18 04:09:11 +00:00
|
|
|
|
|
|
|
std::string Str;
|
|
|
|
raw_string_ostream OS(Str);
|
|
|
|
|
2009-11-30 12:38:47 +00:00
|
|
|
WriteAsOperand(OS, Node, false);
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2010-07-12 05:26:37 +00:00
|
|
|
static std::string getCompleteNodeLabel(const BasicBlock *Node,
|
|
|
|
const Function *Graph) {
|
2009-11-30 12:38:47 +00:00
|
|
|
std::string Str;
|
|
|
|
raw_string_ostream OS(Str);
|
2009-10-18 04:09:11 +00:00
|
|
|
|
|
|
|
if (Node->getName().empty()) {
|
|
|
|
WriteAsOperand(OS, Node, false);
|
|
|
|
OS << ":";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << *Node;
|
|
|
|
std::string OutStr = OS.str();
|
|
|
|
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
|
|
|
|
|
|
|
|
// Process string output to make it nicer...
|
|
|
|
for (unsigned i = 0; i != OutStr.length(); ++i)
|
|
|
|
if (OutStr[i] == '\n') { // Left justify
|
|
|
|
OutStr[i] = '\\';
|
|
|
|
OutStr.insert(OutStr.begin()+i+1, 'l');
|
|
|
|
} else if (OutStr[i] == ';') { // Delete comments!
|
|
|
|
unsigned Idx = OutStr.find('\n', i+1); // Find end of line
|
|
|
|
OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OutStr;
|
|
|
|
}
|
|
|
|
|
2009-11-30 12:38:47 +00:00
|
|
|
std::string getNodeLabel(const BasicBlock *Node,
|
|
|
|
const Function *Graph) {
|
|
|
|
if (isSimple())
|
|
|
|
return getSimpleNodeLabel(Node, Graph);
|
|
|
|
else
|
|
|
|
return getCompleteNodeLabel(Node, Graph);
|
|
|
|
}
|
|
|
|
|
2009-10-18 04:09:11 +00:00
|
|
|
static std::string getEdgeSourceLabel(const BasicBlock *Node,
|
|
|
|
succ_const_iterator I) {
|
|
|
|
// Label source of conditional branches with "T" or "F"
|
|
|
|
if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
|
|
|
|
if (BI->isConditional())
|
|
|
|
return (I == succ_begin(Node)) ? "T" : "F";
|
2009-11-10 22:56:15 +00:00
|
|
|
|
2009-11-11 18:14:02 +00:00
|
|
|
// Label source of switch edges with the associated value.
|
2009-11-10 22:56:15 +00:00
|
|
|
if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
|
|
|
|
unsigned SuccNo = I.getSuccessorIndex();
|
|
|
|
|
|
|
|
if (SuccNo == 0) return "def";
|
|
|
|
|
|
|
|
std::string Str;
|
|
|
|
raw_string_ostream OS(Str);
|
|
|
|
OS << SI->getCaseValue(SuccNo)->getValue();
|
|
|
|
return OS.str();
|
|
|
|
}
|
2009-10-18 04:09:11 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // End llvm namespace
|
|
|
|
|
2004-04-26 16:27:53 +00:00
|
|
|
namespace llvm {
|
|
|
|
class FunctionPass;
|
|
|
|
FunctionPass *createCFGPrinterPass ();
|
|
|
|
FunctionPass *createCFGOnlyPrinterPass ();
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|