blob: d8ee2438ae2d4eaf39acb920b0f5b3a6ee89ac53 [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#ifndef NDEBUG
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000032
Xinliang David Libc157082016-06-21 23:36:12 +000033static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
34 "view-machine-block-freq-propagation-dags", cl::Hidden,
35 cl::desc("Pop up a window to show a dag displaying how machine block "
36 "frequencies propagate through the CFG."),
37 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
38 clEnumValN(GVDT_Fraction, "fraction",
39 "display a graph using the "
40 "fractional block frequency representation."),
41 clEnumValN(GVDT_Integer, "integer",
42 "display a graph using the raw "
43 "integer fractional block frequency representation."),
Xinliang David Li30c50f32016-06-22 19:26:44 +000044 clEnumValN(GVDT_Count, "count", "display a graph using the real "
Mehdi Amini732afdd2016-10-08 19:41:06 +000045 "profile count if available.")));
Xinliang David Lifd3f6452017-01-29 01:57:02 +000046// Similar option above, but used to control BFI display only after MBP pass
47cl::opt<GVDAGType> ViewBlockLayoutWithBFI(
48 "view-block-layout-with-bfi", cl::Hidden,
49 cl::desc(
50 "Pop up a window to show a dag displaying MBP layout and associated "
51 "block frequencies of the CFG."),
52 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
53 clEnumValN(GVDT_Fraction, "fraction",
54 "display a graph using the "
55 "fractional block frequency representation."),
56 clEnumValN(GVDT_Integer, "integer",
57 "display a graph using the raw "
58 "integer fractional block frequency representation."),
59 clEnumValN(GVDT_Count, "count",
60 "display a graph using the real "
61 "profile count if available.")));
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000062
Xinliang David Li8dd5ce92016-06-28 04:07:03 +000063extern cl::opt<std::string> ViewBlockFreqFuncName;
Simon Pilgrim2d531582016-06-28 12:34:44 +000064extern cl::opt<unsigned> ViewHotFreqPercent;
Xinliang David Li80457ce2016-06-22 02:12:54 +000065
Xinliang David Lifd3f6452017-01-29 01:57:02 +000066static GVDAGType getGVDT() {
67 if (ViewBlockLayoutWithBFI != GVDT_None)
68 return ViewBlockLayoutWithBFI;
69
70 return ViewMachineBlockFreqPropagationDAG;
71}
72
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000073namespace llvm {
74
Xinliang David Libc157082016-06-21 23:36:12 +000075template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
Tim Sheneb3958f2016-08-17 20:07:29 +000076 typedef const MachineBasicBlock *NodeRef;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000077 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
Tim Shenb5e0f5a2016-08-19 21:20:13 +000078 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000079
Tim Shen48f814e2016-08-31 16:48:13 +000080 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
Duncan P. N. Exon Smith0ac8eb92015-10-09 19:23:20 +000081 return &G->getFunction()->front();
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000082 }
Michael Gottesman748fe482013-12-03 20:21:17 +000083
Tim Shenf2187ed2016-08-22 21:09:30 +000084 static ChildIteratorType child_begin(const NodeRef N) {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000085 return N->succ_begin();
86 }
Michael Gottesman748fe482013-12-03 20:21:17 +000087
Tim Shenf2187ed2016-08-22 21:09:30 +000088 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
Michael Gottesman748fe482013-12-03 20:21:17 +000089
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000090 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000091 return nodes_iterator(G->getFunction()->begin());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000092 }
Michael Gottesman748fe482013-12-03 20:21:17 +000093
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000094 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000095 return nodes_iterator(G->getFunction()->end());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000096 }
97};
98
Xinliang David Li55415f22016-06-28 03:41:29 +000099typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
100 MachineBranchProbabilityInfo>
101 MBFIDOTGraphTraitsBase;
Xinliang David Libc157082016-06-21 23:36:12 +0000102template <>
103struct DOTGraphTraits<MachineBlockFrequencyInfo *>
Xinliang David Li55415f22016-06-28 03:41:29 +0000104 : public MBFIDOTGraphTraitsBase {
Xinliang David Libc157082016-06-21 23:36:12 +0000105 explicit DOTGraphTraits(bool isSimple = false)
Xinliang David Lifd3f6452017-01-29 01:57:02 +0000106 : MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {}
107
108 const MachineFunction *CurFunc;
109 DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000110
111 std::string getNodeLabel(const MachineBasicBlock *Node,
112 const MachineBlockFrequencyInfo *Graph) {
Xinliang David Lifd3f6452017-01-29 01:57:02 +0000113
114 int layout_order = -1;
115 // Attach additional ordering information if 'isSimple' is false.
116 if (!isSimple()) {
117 const MachineFunction *F = Node->getParent();
118 if (!CurFunc || F != CurFunc) {
119 if (CurFunc)
120 LayoutOrderMap.clear();
121
122 CurFunc = F;
123 int O = 0;
124 for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
125 LayoutOrderMap[&*MBI] = O;
126 }
127 }
128 layout_order = LayoutOrderMap[Node];
129 }
130 return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
131 layout_order);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000132 }
Xinliang David Li55415f22016-06-28 03:41:29 +0000133
Xinliang David Li3e176c72016-06-28 06:58:21 +0000134 std::string getNodeAttributes(const MachineBasicBlock *Node,
135 const MachineBlockFrequencyInfo *Graph) {
136 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
137 ViewHotFreqPercent);
138 }
139
Xinliang David Li55415f22016-06-28 03:41:29 +0000140 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
141 const MachineBlockFrequencyInfo *MBFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000142 return MBFIDOTGraphTraitsBase::getEdgeAttributes(
143 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
Xinliang David Li69317f22016-06-22 16:04:51 +0000144 }
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000145};
146
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000147} // end namespace llvm
148#endif
149
Jakub Staszak875ebd52011-07-25 19:25:40 +0000150INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak27131172011-07-16 20:23:20 +0000151 "Machine Block Frequency Analysis", true, true)
152INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000153INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Jakub Staszak875ebd52011-07-25 19:25:40 +0000154INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
Jakub Staszak27131172011-07-16 20:23:20 +0000155 "Machine Block Frequency Analysis", true, true)
156
Jakub Staszak875ebd52011-07-25 19:25:40 +0000157char MachineBlockFrequencyInfo::ID = 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000158
Xinliang David Libc157082016-06-21 23:36:12 +0000159MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
160 : MachineFunctionPass(ID) {
Jakub Staszak875ebd52011-07-25 19:25:40 +0000161 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
Jakub Staszak27131172011-07-16 20:23:20 +0000162}
163
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000164MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
Jakub Staszak27131172011-07-16 20:23:20 +0000165
Jakub Staszak875ebd52011-07-25 19:25:40 +0000166void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak27131172011-07-16 20:23:20 +0000167 AU.addRequired<MachineBranchProbabilityInfo>();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000168 AU.addRequired<MachineLoopInfo>();
Jakub Staszak27131172011-07-16 20:23:20 +0000169 AU.setPreservesAll();
Jakub Staszakcb7c0a42011-07-21 22:59:09 +0000170 MachineFunctionPass::getAnalysisUsage(AU);
Jakub Staszak27131172011-07-16 20:23:20 +0000171}
172
Jakub Staszak875ebd52011-07-25 19:25:40 +0000173bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
Michael Gottesman748fe482013-12-03 20:21:17 +0000174 MachineBranchProbabilityInfo &MBPI =
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000175 getAnalysis<MachineBranchProbabilityInfo>();
176 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000177 if (!MBFI)
178 MBFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000179 MBFI->calculate(F, MBPI, MLI);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000180#ifndef NDEBUG
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))) {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000184 view();
185 }
186#endif
Jakub Staszak27131172011-07-16 20:23:20 +0000187 return false;
188}
189
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000190void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
191
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000192/// Pop up a ghostview window with the current block frequency propagation
193/// rendered using dot.
Xinliang David Lifd3f6452017-01-29 01:57:02 +0000194void MachineBlockFrequencyInfo::view(bool isSimple) const {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000195// This code is only for debugging.
196#ifndef NDEBUG
197 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
Xinliang David Lifd3f6452017-01-29 01:57:02 +0000198 "MachineBlockFrequencyDAGs", isSimple);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000199#else
Michael Gottesman748fe482013-12-03 20:21:17 +0000200 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
Xinliang David Libc157082016-06-21 23:36:12 +0000201 "on systems with Graphviz or gv!\n";
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000202#endif // NDEBUG
203}
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}