blob: 10b0e929f2bba04ef3c193a7fd9eec221587bea2 [file] [log] [blame]
Duncan P. N. Exon Smith689a5072014-04-11 23:20:58 +00001//===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
Jakub Staszak27131172011-07-16 20:23:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Loops should be simplified before this analysis.
11//
12//===----------------------------------------------------------------------===//
13
Jakub Staszak875ebd52011-07-25 19:25:40 +000014#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Duncan P. N. Exon Smith689a5072014-04-11 23:20:58 +000015#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Jakub Staszak27131172011-07-16 20:23:20 +000016#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineLoopInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/Passes.h"
20#include "llvm/InitializePasses.h"
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/GraphWriter.h"
Jakub Staszak27131172011-07-16 20:23:20 +000024
25using namespace llvm;
26
Chandler Carruth1b9dde02014-04-22 02:02:50 +000027#define DEBUG_TYPE "block-freq"
28
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000029#ifndef NDEBUG
30enum GVDAGType {
31 GVDT_None,
32 GVDT_Fraction,
33 GVDT_Integer
34};
35
36static cl::opt<GVDAGType>
37ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
38 cl::Hidden,
39 cl::desc("Pop up a window to show a dag displaying how machine block "
Michael Gottesman748fe482013-12-03 20:21:17 +000040 "frequencies propagate through the CFG."),
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000041 cl::values(
42 clEnumValN(GVDT_None, "none",
43 "do not display graphs."),
44 clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
45 "fractional block frequency representation."),
46 clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
47 "integer fractional block frequency representation."),
48 clEnumValEnd));
49
50namespace llvm {
51
52template <>
53struct GraphTraits<MachineBlockFrequencyInfo *> {
54 typedef const MachineBasicBlock NodeType;
55 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
56 typedef MachineFunction::const_iterator nodes_iterator;
57
Michael Gottesman748fe482013-12-03 20:21:17 +000058 static inline
59 const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000060 return G->getFunction()->begin();
61 }
Michael Gottesman748fe482013-12-03 20:21:17 +000062
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000063 static ChildIteratorType child_begin(const NodeType *N) {
64 return N->succ_begin();
65 }
Michael Gottesman748fe482013-12-03 20:21:17 +000066
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000067 static ChildIteratorType child_end(const NodeType *N) {
68 return N->succ_end();
69 }
Michael Gottesman748fe482013-12-03 20:21:17 +000070
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000071 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
72 return G->getFunction()->begin();
73 }
Michael Gottesman748fe482013-12-03 20:21:17 +000074
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000075 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
76 return G->getFunction()->end();
77 }
78};
79
80template<>
Michael Gottesman748fe482013-12-03 20:21:17 +000081struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
82 public DefaultDOTGraphTraits {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000083 explicit DOTGraphTraits(bool isSimple=false) :
84 DefaultDOTGraphTraits(isSimple) {}
85
86 static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
87 return G->getFunction()->getName();
88 }
89
90 std::string getNodeLabel(const MachineBasicBlock *Node,
91 const MachineBlockFrequencyInfo *Graph) {
Alp Tokere69170a2014-06-26 22:52:05 +000092 std::string Result;
93 raw_string_ostream OS(Result);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000094
Alp Tokere69170a2014-06-26 22:52:05 +000095 OS << Node->getName().str() << ":";
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000096 switch (ViewMachineBlockFreqPropagationDAG) {
97 case GVDT_Fraction:
Michael Gottesmanb0c1ed82013-12-14 00:25:42 +000098 Graph->printBlockFreq(OS, Node);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000099 break;
100 case GVDT_Integer:
101 OS << Graph->getBlockFreq(Node).getFrequency();
102 break;
103 case GVDT_None:
104 llvm_unreachable("If we are not supposed to render a graph we should "
105 "never reach this point.");
106 }
107
Alp Tokere69170a2014-06-26 22:52:05 +0000108 return Result;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000109 }
110};
111
112
113} // end namespace llvm
114#endif
115
Jakub Staszak875ebd52011-07-25 19:25:40 +0000116INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak27131172011-07-16 20:23:20 +0000117 "Machine Block Frequency Analysis", true, true)
118INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000119INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Jakub Staszak875ebd52011-07-25 19:25:40 +0000120INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak27131172011-07-16 20:23:20 +0000121 "Machine Block Frequency Analysis", true, true)
122
Jakub Staszak875ebd52011-07-25 19:25:40 +0000123char MachineBlockFrequencyInfo::ID = 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000124
125
Michael Gottesman748fe482013-12-03 20:21:17 +0000126MachineBlockFrequencyInfo::
127MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
Jakub Staszak875ebd52011-07-25 19:25:40 +0000128 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
Jakub Staszak27131172011-07-16 20:23:20 +0000129}
130
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000131MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
Jakub Staszak27131172011-07-16 20:23:20 +0000132
Jakub Staszak875ebd52011-07-25 19:25:40 +0000133void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak27131172011-07-16 20:23:20 +0000134 AU.addRequired<MachineBranchProbabilityInfo>();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000135 AU.addRequired<MachineLoopInfo>();
Jakub Staszak27131172011-07-16 20:23:20 +0000136 AU.setPreservesAll();
Jakub Staszakcb7c0a42011-07-21 22:59:09 +0000137 MachineFunctionPass::getAnalysisUsage(AU);
Jakub Staszak27131172011-07-16 20:23:20 +0000138}
139
Jakub Staszak875ebd52011-07-25 19:25:40 +0000140bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
Michael Gottesman748fe482013-12-03 20:21:17 +0000141 MachineBranchProbabilityInfo &MBPI =
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000142 getAnalysis<MachineBranchProbabilityInfo>();
143 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000144 if (!MBFI)
145 MBFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000146 MBFI->calculate(F, MBPI, MLI);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000147#ifndef NDEBUG
148 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
149 view();
150 }
151#endif
Jakub Staszak27131172011-07-16 20:23:20 +0000152 return false;
153}
154
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000155void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
156
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000157/// Pop up a ghostview window with the current block frequency propagation
158/// rendered using dot.
159void MachineBlockFrequencyInfo::view() const {
160// This code is only for debugging.
161#ifndef NDEBUG
162 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
163 "MachineBlockFrequencyDAGs");
164#else
Michael Gottesman748fe482013-12-03 20:21:17 +0000165 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
166 "on systems with Graphviz or gv!\n";
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000167#endif // NDEBUG
168}
169
Jakub Staszaka60d1302011-08-03 21:30:57 +0000170BlockFrequency MachineBlockFrequencyInfo::
Jakub Staszak96f8c552011-12-20 20:03:10 +0000171getBlockFreq(const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000172 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000173}
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000174
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +0000175const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000176 return MBFI ? MBFI->getFunction() : nullptr;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000177}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000178
179raw_ostream &
180MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
181 const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000182 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000183}
184
185raw_ostream &
186MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
187 const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000188 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000189}
190
Michael Gottesman5e985ee2013-12-14 02:37:38 +0000191uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000192 return MBFI ? MBFI->getEntryFreq() : 0;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000193}