From 6fff479973360629e0dc361ff91db21590d2978a Mon Sep 17 00:00:00 2001 From: Xinliang David Li Date: Tue, 28 Jun 2016 03:41:29 +0000 Subject: [PATCH] [BFI]: graph viewer code refactoring 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 --- include/llvm/Analysis/BlockFrequencyInfo.h | 1 + .../llvm/Analysis/BlockFrequencyInfoImpl.h | 67 +++++++++++++++++++ lib/Analysis/BlockFrequencyInfo.cpp | 48 +++++-------- lib/CodeGen/MachineBlockFrequencyInfo.cpp | 58 +++------------- 4 files changed, 97 insertions(+), 77 deletions(-) diff --git a/include/llvm/Analysis/BlockFrequencyInfo.h b/include/llvm/Analysis/BlockFrequencyInfo.h index ca9b129feb78..12d2e6876e39 100644 --- a/include/llvm/Analysis/BlockFrequencyInfo.h +++ b/include/llvm/Analysis/BlockFrequencyInfo.h @@ -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 diff --git a/include/llvm/Analysis/BlockFrequencyInfoImpl.h b/include/llvm/Analysis/BlockFrequencyInfoImpl.h index 6315e7efb7df..de3102411378 100644 --- a/include/llvm/Analysis/BlockFrequencyInfoImpl.h +++ b/include/llvm/Analysis/BlockFrequencyInfoImpl.h @@ -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 @@ -1231,6 +1234,70 @@ raw_ostream &BlockFrequencyInfoImpl::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 +struct BFIDOTGraphTraitsBase : public DefaultDOTGraphTraits { + explicit BFIDOTGraphTraitsBase(bool isSimple = false) + : DefaultDOTGraphTraits(isSimple) {} + + typedef GraphTraits 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 diff --git a/lib/Analysis/BlockFrequencyInfo.cpp b/lib/Analysis/BlockFrequencyInfo.cpp index ac7f6e28b3d0..1370ace759b0 100644 --- a/lib/Analysis/BlockFrequencyInfo.cpp +++ b/lib/Analysis/BlockFrequencyInfo.cpp @@ -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 ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden, cl::desc("Pop up a window to show a dag displaying how block " @@ -71,34 +65,24 @@ struct GraphTraits { } }; -template<> -struct DOTGraphTraits : public DefaultDOTGraphTraits { - explicit DOTGraphTraits(bool isSimple=false) : - DefaultDOTGraphTraits(isSimple) {} +typedef BFIDOTGraphTraitsBase + BFIDOTGTraitsBase; - static std::string getGraphName(const BlockFrequencyInfo *G) { - return G->getFunction()->getName(); - } +template <> +struct DOTGraphTraits : 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()); } }; @@ -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; diff --git a/lib/CodeGen/MachineBlockFrequencyInfo.cpp b/lib/CodeGen/MachineBlockFrequencyInfo.cpp index e994aec4e1ef..f02356b92c51 100644 --- a/lib/CodeGen/MachineBlockFrequencyInfo.cpp +++ b/lib/CodeGen/MachineBlockFrequencyInfo.cpp @@ -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 ViewMachineBlockFreqPropagationDAG( "view-machine-block-freq-propagation-dags", cl::Hidden, @@ -79,59 +78,24 @@ template <> struct GraphTraits { } }; +typedef BFIDOTGraphTraitsBase + MBFIDOTGraphTraitsBase; template <> struct DOTGraphTraits - : 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()); } };