Skip to content

Commit

Permalink
[BFI]: graph viewer code refactoring
Browse files Browse the repository at this point in the history
BFI and MBFI's dot traits class share most of the
code and all future enhancement. This patch extracts
common implementation into base class BFIDOTGraphTraitsBase.

This patch also enables BFI graph to show branch probability
on edges as MBFI does before.




git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273990 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
david-xl committed Jun 28, 2016
1 parent edd67d6 commit 6fff479
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 77 deletions.
1 change: 1 addition & 0 deletions include/llvm/Analysis/BlockFrequencyInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class BlockFrequencyInfo {
BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);

const Function *getFunction() const;
const BranchProbabilityInfo *getBPI() const;
void view() const;

/// getblockFreq - Return block frequency. Return 0 if we don't have the
Expand Down
67 changes: 67 additions & 0 deletions include/llvm/Analysis/BlockFrequencyInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
#define LLVM_ANALYSIS_BLOCKFREQUENCYINFOIMPL_H

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/GraphTraits.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/Support/BlockFrequency.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/DOTGraphTraits.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ScaledNumber.h"
#include "llvm/Support/raw_ostream.h"
#include <deque>
Expand Down Expand Up @@ -1231,6 +1234,70 @@ raw_ostream &BlockFrequencyInfoImpl<BT>::print(raw_ostream &OS) const {
return OS;
}

// Graph trait base class for block frequency information graph
// viewer.

enum GVDAGType { GVDT_None, GVDT_Fraction, GVDT_Integer, GVDT_Count };

template <class BlockFrequencyInfoT, class BranchProbabilityInfoT>
struct BFIDOTGraphTraitsBase : public DefaultDOTGraphTraits {
explicit BFIDOTGraphTraitsBase(bool isSimple = false)
: DefaultDOTGraphTraits(isSimple) {}

typedef GraphTraits<BlockFrequencyInfoT *> GTraits;
typedef typename GTraits::NodeType NodeType;
typedef typename GTraits::ChildIteratorType EdgeIter;
typedef typename GTraits::nodes_iterator NodeIter;

static std::string getGraphName(const BlockFrequencyInfoT *G) {
return G->getFunction()->getName();
}

std::string getNodeLabel(const NodeType *Node,
const BlockFrequencyInfoT *Graph, GVDAGType GType) {
std::string Result;
raw_string_ostream OS(Result);

OS << Node->getName().str() << " : ";
switch (GType) {
case GVDT_Fraction:
Graph->printBlockFreq(OS, Node);
break;
case GVDT_Integer:
OS << Graph->getBlockFreq(Node).getFrequency();
break;
case GVDT_Count: {
auto Count = Graph->getBlockProfileCount(Node);
if (Count)
OS << Count.getValue();
else
OS << "Unknown";
break;
}
case GVDT_None:
llvm_unreachable("If we are not supposed to render a graph we should "
"never reach this point.");
}
return Result;
}

std::string getEdgeAttributes(const NodeType *Node, EdgeIter EI,
const BranchProbabilityInfoT *BPI) {
std::string Str;
if (!BPI)
return Str;

BranchProbability BP = BPI->getEdgeProbability(Node, EI);
uint32_t N = BP.getNumerator();
uint32_t D = BP.getDenominator();
double Percent = 100.0 * N / D;
raw_string_ostream OS(Str);
OS << format("label=\"%.1f%%\"", Percent);
OS.flush();
return Str;
}
};

} // end namespace llvm

#undef DEBUG_TYPE
Expand Down
48 changes: 18 additions & 30 deletions lib/Analysis/BlockFrequencyInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ using namespace llvm;
#define DEBUG_TYPE "block-freq"

#ifndef NDEBUG
enum GVDAGType {
GVDT_None,
GVDT_Fraction,
GVDT_Integer
};

static cl::opt<GVDAGType>
ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden,
cl::desc("Pop up a window to show a dag displaying how block "
Expand Down Expand Up @@ -71,34 +65,24 @@ struct GraphTraits<BlockFrequencyInfo *> {
}
};

template<>
struct DOTGraphTraits<BlockFrequencyInfo*> : public DefaultDOTGraphTraits {
explicit DOTGraphTraits(bool isSimple=false) :
DefaultDOTGraphTraits(isSimple) {}
typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>
BFIDOTGTraitsBase;

static std::string getGraphName(const BlockFrequencyInfo *G) {
return G->getFunction()->getName();
}
template <>
struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
explicit DOTGraphTraits(bool isSimple = false)
: BFIDOTGTraitsBase(isSimple) {}

std::string getNodeLabel(const BasicBlock *Node,
const BlockFrequencyInfo *Graph) {
std::string Result;
raw_string_ostream OS(Result);

OS << Node->getName() << ":";
switch (ViewBlockFreqPropagationDAG) {
case GVDT_Fraction:
Graph->printBlockFreq(OS, Node);
break;
case GVDT_Integer:
OS << Graph->getBlockFreq(Node).getFrequency();
break;
case GVDT_None:
llvm_unreachable("If we are not supposed to render a graph we should "
"never reach this point.");
}

return Result;

return BFIDOTGTraitsBase::getNodeLabel(Node, Graph,
ViewBlockFreqPropagationDAG);
}

std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
const BlockFrequencyInfo *BFI) {
return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI->getBPI());
}
};

Expand Down Expand Up @@ -167,6 +151,10 @@ const Function *BlockFrequencyInfo::getFunction() const {
return BFI ? BFI->getFunction() : nullptr;
}

const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
return BFI ? &BFI->getBPI() : nullptr;
}

raw_ostream &BlockFrequencyInfo::
printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
Expand Down
58 changes: 11 additions & 47 deletions lib/CodeGen/MachineBlockFrequencyInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ using namespace llvm;
#define DEBUG_TYPE "block-freq"

#ifndef NDEBUG
enum GVDAGType { GVDT_None, GVDT_Fraction, GVDT_Integer, GVDT_Count };

static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
"view-machine-block-freq-propagation-dags", cl::Hidden,
Expand Down Expand Up @@ -79,59 +78,24 @@ template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
}
};

typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
MachineBranchProbabilityInfo>
MBFIDOTGraphTraitsBase;
template <>
struct DOTGraphTraits<MachineBlockFrequencyInfo *>
: public DefaultDOTGraphTraits {
: public MBFIDOTGraphTraitsBase {
explicit DOTGraphTraits(bool isSimple = false)
: DefaultDOTGraphTraits(isSimple) {}

typedef MachineBasicBlock::const_succ_iterator EdgeIter;
static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
return G->getFunction()->getName();
}
: MBFIDOTGraphTraitsBase(isSimple) {}

std::string getNodeLabel(const MachineBasicBlock *Node,
const MachineBlockFrequencyInfo *Graph) {
std::string Result;
raw_string_ostream OS(Result);

OS << Node->getName().str() << " : ";
switch (ViewMachineBlockFreqPropagationDAG) {
case GVDT_Fraction:
Graph->printBlockFreq(OS, Node);
break;
case GVDT_Integer:
OS << Graph->getBlockFreq(Node).getFrequency();
break;
case GVDT_Count: {
auto Count = Graph->getBlockProfileCount(Node);
if (Count)
OS << Count.getValue();
else
OS << "Unknown";
break;
}
case GVDT_None:
llvm_unreachable("If we are not supposed to render a graph we should "
"never reach this point.");
}
return Result;
return MBFIDOTGraphTraitsBase::getNodeLabel(
Node, Graph, ViewMachineBlockFreqPropagationDAG);
}
static std::string getEdgeAttributes(const MachineBasicBlock *Node,
EdgeIter EI,
const MachineBlockFrequencyInfo *MBFI) {
std::string Str;
const MachineBranchProbabilityInfo *MBPI = MBFI->getMBPI();
if (!MBPI)
return Str;
BranchProbability BP = MBPI->getEdgeProbability(Node, EI);
uint32_t N = BP.getNumerator();
uint32_t D = BP.getDenominator();
double Percent = 100.0 * N / D;
raw_string_ostream OS(Str);
OS << format("label=\"%.1f%%\"", Percent);
OS.flush();
return Str;

std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
const MachineBlockFrequencyInfo *MBFI) {
return MBFIDOTGraphTraitsBase::getEdgeAttributes(Node, EI, MBFI->getMBPI());
}
};

Expand Down

0 comments on commit 6fff479

Please sign in to comment.