blob: d0c6261a478a8885893afe3ef9510bf1a258e308 [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
Xinliang David Libc157082016-06-21 23:36:12 +000030enum GVDAGType { GVDT_None, GVDT_Fraction, GVDT_Integer };
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000031
Xinliang David Libc157082016-06-21 23:36:12 +000032static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
33 "view-machine-block-freq-propagation-dags", cl::Hidden,
34 cl::desc("Pop up a window to show a dag displaying how machine block "
35 "frequencies propagate through the CFG."),
36 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
37 clEnumValN(GVDT_Fraction, "fraction",
38 "display a graph using the "
39 "fractional block frequency representation."),
40 clEnumValN(GVDT_Integer, "integer",
41 "display a graph using the raw "
42 "integer fractional block frequency representation."),
43 clEnumValEnd));
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000044
45namespace llvm {
46
Xinliang David Libc157082016-06-21 23:36:12 +000047template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000048 typedef const MachineBasicBlock NodeType;
49 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
50 typedef MachineFunction::const_iterator nodes_iterator;
51
Xinliang David Libc157082016-06-21 23:36:12 +000052 static inline const NodeType *
53 getEntryNode(const MachineBlockFrequencyInfo *G) {
Duncan P. N. Exon Smith0ac8eb92015-10-09 19:23:20 +000054 return &G->getFunction()->front();
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000055 }
Michael Gottesman748fe482013-12-03 20:21:17 +000056
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000057 static ChildIteratorType child_begin(const NodeType *N) {
58 return N->succ_begin();
59 }
Michael Gottesman748fe482013-12-03 20:21:17 +000060
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000061 static ChildIteratorType child_end(const NodeType *N) {
62 return N->succ_end();
63 }
Michael Gottesman748fe482013-12-03 20:21:17 +000064
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000065 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
66 return G->getFunction()->begin();
67 }
Michael Gottesman748fe482013-12-03 20:21:17 +000068
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000069 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
70 return G->getFunction()->end();
71 }
72};
73
Xinliang David Libc157082016-06-21 23:36:12 +000074template <>
75struct DOTGraphTraits<MachineBlockFrequencyInfo *>
76 : public DefaultDOTGraphTraits {
77 explicit DOTGraphTraits(bool isSimple = false)
78 : DefaultDOTGraphTraits(isSimple) {}
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000079
80 static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
81 return G->getFunction()->getName();
82 }
83
84 std::string getNodeLabel(const MachineBasicBlock *Node,
85 const MachineBlockFrequencyInfo *Graph) {
Alp Tokere69170a2014-06-26 22:52:05 +000086 std::string Result;
87 raw_string_ostream OS(Result);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000088
Alp Tokere69170a2014-06-26 22:52:05 +000089 OS << Node->getName().str() << ":";
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000090 switch (ViewMachineBlockFreqPropagationDAG) {
91 case GVDT_Fraction:
Michael Gottesmanb0c1ed82013-12-14 00:25:42 +000092 Graph->printBlockFreq(OS, Node);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000093 break;
94 case GVDT_Integer:
95 OS << Graph->getBlockFreq(Node).getFrequency();
96 break;
97 case GVDT_None:
98 llvm_unreachable("If we are not supposed to render a graph we should "
99 "never reach this point.");
100 }
101
Alp Tokere69170a2014-06-26 22:52:05 +0000102 return Result;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000103 }
104};
105
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000106} // end namespace llvm
107#endif
108
Jakub Staszak875ebd52011-07-25 19:25:40 +0000109INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak27131172011-07-16 20:23:20 +0000110 "Machine Block Frequency Analysis", true, true)
111INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000112INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Jakub Staszak875ebd52011-07-25 19:25:40 +0000113INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak27131172011-07-16 20:23:20 +0000114 "Machine Block Frequency Analysis", true, true)
115
Jakub Staszak875ebd52011-07-25 19:25:40 +0000116char MachineBlockFrequencyInfo::ID = 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000117
Xinliang David Libc157082016-06-21 23:36:12 +0000118MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
119 : MachineFunctionPass(ID) {
Jakub Staszak875ebd52011-07-25 19:25:40 +0000120 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
Jakub Staszak27131172011-07-16 20:23:20 +0000121}
122
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000123MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
Jakub Staszak27131172011-07-16 20:23:20 +0000124
Jakub Staszak875ebd52011-07-25 19:25:40 +0000125void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak27131172011-07-16 20:23:20 +0000126 AU.addRequired<MachineBranchProbabilityInfo>();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000127 AU.addRequired<MachineLoopInfo>();
Jakub Staszak27131172011-07-16 20:23:20 +0000128 AU.setPreservesAll();
Jakub Staszakcb7c0a42011-07-21 22:59:09 +0000129 MachineFunctionPass::getAnalysisUsage(AU);
Jakub Staszak27131172011-07-16 20:23:20 +0000130}
131
Jakub Staszak875ebd52011-07-25 19:25:40 +0000132bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
Michael Gottesman748fe482013-12-03 20:21:17 +0000133 MachineBranchProbabilityInfo &MBPI =
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000134 getAnalysis<MachineBranchProbabilityInfo>();
135 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000136 if (!MBFI)
137 MBFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000138 MBFI->calculate(F, MBPI, MLI);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000139#ifndef NDEBUG
140 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
141 view();
142 }
143#endif
Jakub Staszak27131172011-07-16 20:23:20 +0000144 return false;
145}
146
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000147void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
148
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000149/// Pop up a ghostview window with the current block frequency propagation
150/// rendered using dot.
151void MachineBlockFrequencyInfo::view() const {
152// This code is only for debugging.
153#ifndef NDEBUG
154 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
155 "MachineBlockFrequencyDAGs");
156#else
Michael Gottesman748fe482013-12-03 20:21:17 +0000157 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
Xinliang David Libc157082016-06-21 23:36:12 +0000158 "on systems with Graphviz or gv!\n";
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000159#endif // NDEBUG
160}
161
Xinliang David Libc157082016-06-21 23:36:12 +0000162BlockFrequency
163MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000164 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000165}
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000166
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +0000167const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000168 return MBFI ? MBFI->getFunction() : nullptr;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000169}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000170
171raw_ostream &
172MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
173 const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000174 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000175}
176
177raw_ostream &
178MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
179 const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000180 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000181}
182
Michael Gottesman5e985ee2013-12-14 02:37:38 +0000183uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000184 return MBFI ? MBFI->getEntryFreq() : 0;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000185}