blob: 104180ad4d2de57731f17ee03bceb8dc1d9aeb8a [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"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/None.h"
17#include "llvm/ADT/iterator.h"
Duncan P. N. Exon Smith689a5072014-04-11 23:20:58 +000018#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
Jakub Staszak27131172011-07-16 20:23:20 +000020#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000023#include "llvm/Pass.h"
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000024#include "llvm/Support/CommandLine.h"
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000025#include "llvm/Support/GraphWriter.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000026#include <string>
Jakub Staszak27131172011-07-16 20:23:20 +000027
28using namespace llvm;
29
Matthias Braun1527baa2017-05-25 21:26:32 +000030#define DEBUG_TYPE "machine-block-freq"
Chandler Carruth1b9dde02014-04-22 02:02:50 +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.")));
Eugene Zelenko5df3d892017-08-24 21:21:39 +000045
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 Li58fcc9b2017-02-02 21:29:17 +000063// Command line option to specify the name of the function for CFG dump
64// Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=
Xinliang David Li8dd5ce92016-06-28 04:07:03 +000065extern cl::opt<std::string> ViewBlockFreqFuncName;
Eugene Zelenko5df3d892017-08-24 21:21:39 +000066
Xinliang David Li58fcc9b2017-02-02 21:29:17 +000067// Command line option to specify hot frequency threshold.
68// Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc=
Simon Pilgrim2d531582016-06-28 12:34:44 +000069extern cl::opt<unsigned> ViewHotFreqPercent;
Xinliang David Li80457ce2016-06-22 02:12:54 +000070
Xinliang David Lifd3f6452017-01-29 01:57:02 +000071static GVDAGType getGVDT() {
72 if (ViewBlockLayoutWithBFI != GVDT_None)
73 return ViewBlockLayoutWithBFI;
74
75 return ViewMachineBlockFreqPropagationDAG;
76}
77
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000078namespace llvm {
79
Xinliang David Libc157082016-06-21 23:36:12 +000080template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
Eugene Zelenko5df3d892017-08-24 21:21:39 +000081 using NodeRef = const MachineBasicBlock *;
82 using ChildIteratorType = MachineBasicBlock::const_succ_iterator;
83 using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000084
Tim Shen48f814e2016-08-31 16:48:13 +000085 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
Duncan P. N. Exon Smith0ac8eb92015-10-09 19:23:20 +000086 return &G->getFunction()->front();
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000087 }
Michael Gottesman748fe482013-12-03 20:21:17 +000088
Tim Shenf2187ed2016-08-22 21:09:30 +000089 static ChildIteratorType child_begin(const NodeRef N) {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000090 return N->succ_begin();
91 }
Michael Gottesman748fe482013-12-03 20:21:17 +000092
Tim Shenf2187ed2016-08-22 21:09:30 +000093 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
Michael Gottesman748fe482013-12-03 20:21:17 +000094
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000095 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000096 return nodes_iterator(G->getFunction()->begin());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000097 }
Michael Gottesman748fe482013-12-03 20:21:17 +000098
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000099 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +0000100 return nodes_iterator(G->getFunction()->end());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000101 }
102};
103
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000104using MBFIDOTGraphTraitsBase =
105 BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
106 MachineBranchProbabilityInfo>;
107
Xinliang David Libc157082016-06-21 23:36:12 +0000108template <>
109struct DOTGraphTraits<MachineBlockFrequencyInfo *>
Xinliang David Li55415f22016-06-28 03:41:29 +0000110 : public MBFIDOTGraphTraitsBase {
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000111 const MachineFunction *CurFunc = nullptr;
Xinliang David Lifd3f6452017-01-29 01:57:02 +0000112 DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000113
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000114 explicit DOTGraphTraits(bool isSimple = false)
115 : MBFIDOTGraphTraitsBase(isSimple) {}
116
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000117 std::string getNodeLabel(const MachineBasicBlock *Node,
118 const MachineBlockFrequencyInfo *Graph) {
Xinliang David Lifd3f6452017-01-29 01:57:02 +0000119 int layout_order = -1;
120 // Attach additional ordering information if 'isSimple' is false.
121 if (!isSimple()) {
122 const MachineFunction *F = Node->getParent();
123 if (!CurFunc || F != CurFunc) {
124 if (CurFunc)
125 LayoutOrderMap.clear();
126
127 CurFunc = F;
128 int O = 0;
129 for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
130 LayoutOrderMap[&*MBI] = O;
131 }
132 }
133 layout_order = LayoutOrderMap[Node];
134 }
135 return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
136 layout_order);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000137 }
Xinliang David Li55415f22016-06-28 03:41:29 +0000138
Xinliang David Li3e176c72016-06-28 06:58:21 +0000139 std::string getNodeAttributes(const MachineBasicBlock *Node,
140 const MachineBlockFrequencyInfo *Graph) {
141 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
142 ViewHotFreqPercent);
143 }
144
Xinliang David Li55415f22016-06-28 03:41:29 +0000145 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
146 const MachineBlockFrequencyInfo *MBFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000147 return MBFIDOTGraphTraitsBase::getEdgeAttributes(
148 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
Xinliang David Li69317f22016-06-22 16:04:51 +0000149 }
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000150};
151
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000152} // end namespace llvm
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000153
Matthias Braun1527baa2017-05-25 21:26:32 +0000154INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE,
Jakub Staszak27131172011-07-16 20:23:20 +0000155 "Machine Block Frequency Analysis", true, true)
156INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000157INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Matthias Braun1527baa2017-05-25 21:26:32 +0000158INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE,
Jakub Staszak27131172011-07-16 20:23:20 +0000159 "Machine Block Frequency Analysis", true, true)
160
Jakub Staszak875ebd52011-07-25 19:25:40 +0000161char MachineBlockFrequencyInfo::ID = 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000162
Xinliang David Libc157082016-06-21 23:36:12 +0000163MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
164 : MachineFunctionPass(ID) {
Jakub Staszak875ebd52011-07-25 19:25:40 +0000165 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
Jakub Staszak27131172011-07-16 20:23:20 +0000166}
167
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000168MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default;
Jakub Staszak27131172011-07-16 20:23:20 +0000169
Jakub Staszak875ebd52011-07-25 19:25:40 +0000170void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak27131172011-07-16 20:23:20 +0000171 AU.addRequired<MachineBranchProbabilityInfo>();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000172 AU.addRequired<MachineLoopInfo>();
Jakub Staszak27131172011-07-16 20:23:20 +0000173 AU.setPreservesAll();
Jakub Staszakcb7c0a42011-07-21 22:59:09 +0000174 MachineFunctionPass::getAnalysisUsage(AU);
Jakub Staszak27131172011-07-16 20:23:20 +0000175}
176
Adam Nemetbbb141c2017-02-14 17:21:09 +0000177void MachineBlockFrequencyInfo::calculate(
178 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
179 const MachineLoopInfo &MLI) {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000180 if (!MBFI)
181 MBFI.reset(new ImplType);
Cong Hou5e67b662015-07-15 19:58:26 +0000182 MBFI->calculate(F, MBPI, MLI);
Xinliang David Li80457ce2016-06-22 02:12:54 +0000183 if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000184 (ViewBlockFreqFuncName.empty() ||
185 F.getName().equals(ViewBlockFreqFuncName))) {
Xinliang David Li538d6662017-02-15 19:21:04 +0000186 view("MachineBlockFrequencyDAGS." + F.getName());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000187 }
Adam Nemetbbb141c2017-02-14 17:21:09 +0000188}
189
190bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
191 MachineBranchProbabilityInfo &MBPI =
192 getAnalysis<MachineBranchProbabilityInfo>();
193 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
194 calculate(F, MBPI, MLI);
Jakub Staszak27131172011-07-16 20:23:20 +0000195 return false;
196}
197
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000198void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
199
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000200/// Pop up a ghostview window with the current block frequency propagation
201/// rendered using dot.
Xinliang David Li538d6662017-02-15 19:21:04 +0000202void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {
203 // This code is only for debugging.
204 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000205}
206
Xinliang David Libc157082016-06-21 23:36:12 +0000207BlockFrequency
208MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000209 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000210}
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000211
Xinliang David Li30c50f32016-06-22 19:26:44 +0000212Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
213 const MachineBasicBlock *MBB) const {
214 const Function *F = MBFI->getFunction()->getFunction();
215 return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
216}
217
Sean Silvaf8015752016-08-02 02:15:45 +0000218Optional<uint64_t>
219MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
220 const Function *F = MBFI->getFunction()->getFunction();
221 return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
222}
223
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +0000224const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000225 return MBFI ? MBFI->getFunction() : nullptr;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000226}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000227
Xinliang David Li3264fdd2016-06-28 00:15:45 +0000228const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
229 return MBFI ? &MBFI->getBPI() : nullptr;
230}
231
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000232raw_ostream &
233MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
234 const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000235 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000236}
237
238raw_ostream &
239MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
240 const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000241 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000242}
243
Michael Gottesman5e985ee2013-12-14 02:37:38 +0000244uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000245 return MBFI ? MBFI->getEntryFreq() : 0;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000246}