blob: 9c7367b4c78020ecb2b6260658c8a67f46d00750 [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"
Xinliang David Li69317f22016-06-22 16:04:51 +000023#include "llvm/Support/Format.h"
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000024#include "llvm/Support/GraphWriter.h"
Xinliang David Li69317f22016-06-22 16:04:51 +000025#include "llvm/Support/raw_ostream.h"
Jakub Staszak27131172011-07-16 20:23:20 +000026
27using namespace llvm;
28
Chandler Carruth1b9dde02014-04-22 02:02:50 +000029#define DEBUG_TYPE "block-freq"
30
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."),
Xinliang David Li30c50f32016-06-22 19:26:44 +000043 clEnumValN(GVDT_Count, "count", "display a graph using the real "
Mehdi Amini732afdd2016-10-08 19:41:06 +000044 "profile count if available.")));
Xinliang David Lifd3f6452017-01-29 01:57:02 +000045// Similar option above, but used to control BFI display only after MBP pass
46cl::opt<GVDAGType> ViewBlockLayoutWithBFI(
47 "view-block-layout-with-bfi", cl::Hidden,
48 cl::desc(
49 "Pop up a window to show a dag displaying MBP layout and associated "
50 "block frequencies of the CFG."),
51 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
52 clEnumValN(GVDT_Fraction, "fraction",
53 "display a graph using the "
54 "fractional block frequency representation."),
55 clEnumValN(GVDT_Integer, "integer",
56 "display a graph using the raw "
57 "integer fractional block frequency representation."),
58 clEnumValN(GVDT_Count, "count",
59 "display a graph using the real "
60 "profile count if available.")));
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000061
Xinliang David Li58fcc9b2017-02-02 21:29:17 +000062// Command line option to specify the name of the function for CFG dump
63// Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=
Xinliang David Li8dd5ce92016-06-28 04:07:03 +000064extern cl::opt<std::string> ViewBlockFreqFuncName;
Xinliang David Li58fcc9b2017-02-02 21:29:17 +000065// Command line option to specify hot frequency threshold.
66// Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc=
Simon Pilgrim2d531582016-06-28 12:34:44 +000067extern cl::opt<unsigned> ViewHotFreqPercent;
Xinliang David Li80457ce2016-06-22 02:12:54 +000068
Xinliang David Lifd3f6452017-01-29 01:57:02 +000069static GVDAGType getGVDT() {
70 if (ViewBlockLayoutWithBFI != GVDT_None)
71 return ViewBlockLayoutWithBFI;
72
73 return ViewMachineBlockFreqPropagationDAG;
74}
75
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000076namespace llvm {
77
Xinliang David Libc157082016-06-21 23:36:12 +000078template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
Tim Sheneb3958f2016-08-17 20:07:29 +000079 typedef const MachineBasicBlock *NodeRef;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000080 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
Tim Shenb5e0f5a2016-08-19 21:20:13 +000081 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000082
Tim Shen48f814e2016-08-31 16:48:13 +000083 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
Duncan P. N. Exon Smith0ac8eb92015-10-09 19:23:20 +000084 return &G->getFunction()->front();
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000085 }
Michael Gottesman748fe482013-12-03 20:21:17 +000086
Tim Shenf2187ed2016-08-22 21:09:30 +000087 static ChildIteratorType child_begin(const NodeRef N) {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000088 return N->succ_begin();
89 }
Michael Gottesman748fe482013-12-03 20:21:17 +000090
Tim Shenf2187ed2016-08-22 21:09:30 +000091 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
Michael Gottesman748fe482013-12-03 20:21:17 +000092
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000093 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000094 return nodes_iterator(G->getFunction()->begin());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000095 }
Michael Gottesman748fe482013-12-03 20:21:17 +000096
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000097 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000098 return nodes_iterator(G->getFunction()->end());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000099 }
100};
101
Xinliang David Li55415f22016-06-28 03:41:29 +0000102typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
103 MachineBranchProbabilityInfo>
104 MBFIDOTGraphTraitsBase;
Xinliang David Libc157082016-06-21 23:36:12 +0000105template <>
106struct DOTGraphTraits<MachineBlockFrequencyInfo *>
Xinliang David Li55415f22016-06-28 03:41:29 +0000107 : public MBFIDOTGraphTraitsBase {
Xinliang David Libc157082016-06-21 23:36:12 +0000108 explicit DOTGraphTraits(bool isSimple = false)
Xinliang David Lifd3f6452017-01-29 01:57:02 +0000109 : MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {}
110
111 const MachineFunction *CurFunc;
112 DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000113
114 std::string getNodeLabel(const MachineBasicBlock *Node,
115 const MachineBlockFrequencyInfo *Graph) {
Xinliang David Lifd3f6452017-01-29 01:57:02 +0000116
117 int layout_order = -1;
118 // Attach additional ordering information if 'isSimple' is false.
119 if (!isSimple()) {
120 const MachineFunction *F = Node->getParent();
121 if (!CurFunc || F != CurFunc) {
122 if (CurFunc)
123 LayoutOrderMap.clear();
124
125 CurFunc = F;
126 int O = 0;
127 for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
128 LayoutOrderMap[&*MBI] = O;
129 }
130 }
131 layout_order = LayoutOrderMap[Node];
132 }
133 return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
134 layout_order);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000135 }
Xinliang David Li55415f22016-06-28 03:41:29 +0000136
Xinliang David Li3e176c72016-06-28 06:58:21 +0000137 std::string getNodeAttributes(const MachineBasicBlock *Node,
138 const MachineBlockFrequencyInfo *Graph) {
139 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
140 ViewHotFreqPercent);
141 }
142
Xinliang David Li55415f22016-06-28 03:41:29 +0000143 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
144 const MachineBlockFrequencyInfo *MBFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000145 return MBFIDOTGraphTraitsBase::getEdgeAttributes(
146 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
Xinliang David Li69317f22016-06-22 16:04:51 +0000147 }
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000148};
149
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000150} // end namespace llvm
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000151
Jakub Staszak875ebd52011-07-25 19:25:40 +0000152INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak27131172011-07-16 20:23:20 +0000153 "Machine Block Frequency Analysis", true, true)
154INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000155INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Jakub Staszak875ebd52011-07-25 19:25:40 +0000156INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak27131172011-07-16 20:23:20 +0000157 "Machine Block Frequency Analysis", true, true)
158
Jakub Staszak875ebd52011-07-25 19:25:40 +0000159char MachineBlockFrequencyInfo::ID = 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000160
Xinliang David Libc157082016-06-21 23:36:12 +0000161MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
162 : MachineFunctionPass(ID) {
Jakub Staszak875ebd52011-07-25 19:25:40 +0000163 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
Jakub Staszak27131172011-07-16 20:23:20 +0000164}
165
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000166MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
Jakub Staszak27131172011-07-16 20:23:20 +0000167
Jakub Staszak875ebd52011-07-25 19:25:40 +0000168void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak27131172011-07-16 20:23:20 +0000169 AU.addRequired<MachineBranchProbabilityInfo>();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000170 AU.addRequired<MachineLoopInfo>();
Jakub Staszak27131172011-07-16 20:23:20 +0000171 AU.setPreservesAll();
Jakub Staszakcb7c0a42011-07-21 22:59:09 +0000172 MachineFunctionPass::getAnalysisUsage(AU);
Jakub Staszak27131172011-07-16 20:23:20 +0000173}
174
Adam Nemetbbb141c2017-02-14 17:21:09 +0000175void MachineBlockFrequencyInfo::calculate(
176 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
177 const MachineLoopInfo &MLI) {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000178 if (!MBFI)
179 MBFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000180 MBFI->calculate(F, MBPI, MLI);
Xinliang David Li80457ce2016-06-22 02:12:54 +0000181 if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000182 (ViewBlockFreqFuncName.empty() ||
183 F.getName().equals(ViewBlockFreqFuncName))) {
Xinliang David Li538d6662017-02-15 19:21:04 +0000184 view("MachineBlockFrequencyDAGS." + F.getName());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000185 }
Adam Nemetbbb141c2017-02-14 17:21:09 +0000186}
187
188bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
189 MachineBranchProbabilityInfo &MBPI =
190 getAnalysis<MachineBranchProbabilityInfo>();
191 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
192 calculate(F, MBPI, MLI);
Jakub Staszak27131172011-07-16 20:23:20 +0000193 return false;
194}
195
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000196void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
197
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000198/// Pop up a ghostview window with the current block frequency propagation
199/// rendered using dot.
Xinliang David Li538d6662017-02-15 19:21:04 +0000200void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {
201 // This code is only for debugging.
202 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000203}
204
Xinliang David Libc157082016-06-21 23:36:12 +0000205BlockFrequency
206MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000207 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000208}
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000209
Xinliang David Li30c50f32016-06-22 19:26:44 +0000210Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
211 const MachineBasicBlock *MBB) const {
212 const Function *F = MBFI->getFunction()->getFunction();
213 return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
214}
215
Sean Silvaf8015752016-08-02 02:15:45 +0000216Optional<uint64_t>
217MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
218 const Function *F = MBFI->getFunction()->getFunction();
219 return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
220}
221
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +0000222const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000223 return MBFI ? MBFI->getFunction() : nullptr;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000224}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000225
Xinliang David Li3264fdd2016-06-28 00:15:45 +0000226const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
227 return MBFI ? &MBFI->getBPI() : nullptr;
228}
229
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000230raw_ostream &
231MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
232 const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000233 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000234}
235
236raw_ostream &
237MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
238 const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000239 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000240}
241
Michael Gottesman5e985ee2013-12-14 02:37:38 +0000242uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000243 return MBFI ? MBFI->getEntryFreq() : 0;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000244}