blob: bda6aa4a2bfcaa178b61e2300eaf3ef3556f9f7f [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 "
45 "profile count if available."),
46
Xinliang David Libc157082016-06-21 23:36:12 +000047 clEnumValEnd));
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000048
Xinliang David Li8dd5ce92016-06-28 04:07:03 +000049extern cl::opt<std::string> ViewBlockFreqFuncName;
Simon Pilgrim2d531582016-06-28 12:34:44 +000050extern cl::opt<unsigned> ViewHotFreqPercent;
Xinliang David Li80457ce2016-06-22 02:12:54 +000051
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000052namespace llvm {
53
Xinliang David Libc157082016-06-21 23:36:12 +000054template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
Tim Sheneb3958f2016-08-17 20:07:29 +000055 typedef const MachineBasicBlock *NodeRef;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000056 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
Tim Shenb5e0f5a2016-08-19 21:20:13 +000057 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000058
Tim Shenf2187ed2016-08-22 21:09:30 +000059 static inline NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
Duncan P. N. Exon Smith0ac8eb92015-10-09 19:23:20 +000060 return &G->getFunction()->front();
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000061 }
Michael Gottesman748fe482013-12-03 20:21:17 +000062
Tim Shenf2187ed2016-08-22 21:09:30 +000063 static ChildIteratorType child_begin(const NodeRef N) {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000064 return N->succ_begin();
65 }
Michael Gottesman748fe482013-12-03 20:21:17 +000066
Tim Shenf2187ed2016-08-22 21:09:30 +000067 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
Michael Gottesman748fe482013-12-03 20:21:17 +000068
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000069 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000070 return nodes_iterator(G->getFunction()->begin());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000071 }
Michael Gottesman748fe482013-12-03 20:21:17 +000072
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000073 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
Tim Shenb5e0f5a2016-08-19 21:20:13 +000074 return nodes_iterator(G->getFunction()->end());
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000075 }
76};
77
Xinliang David Li55415f22016-06-28 03:41:29 +000078typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
79 MachineBranchProbabilityInfo>
80 MBFIDOTGraphTraitsBase;
Xinliang David Libc157082016-06-21 23:36:12 +000081template <>
82struct DOTGraphTraits<MachineBlockFrequencyInfo *>
Xinliang David Li55415f22016-06-28 03:41:29 +000083 : public MBFIDOTGraphTraitsBase {
Xinliang David Libc157082016-06-21 23:36:12 +000084 explicit DOTGraphTraits(bool isSimple = false)
Xinliang David Li55415f22016-06-28 03:41:29 +000085 : MBFIDOTGraphTraitsBase(isSimple) {}
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000086
87 std::string getNodeLabel(const MachineBasicBlock *Node,
88 const MachineBlockFrequencyInfo *Graph) {
Xinliang David Li55415f22016-06-28 03:41:29 +000089 return MBFIDOTGraphTraitsBase::getNodeLabel(
90 Node, Graph, ViewMachineBlockFreqPropagationDAG);
Michael Gottesman65bbcdf2013-12-03 00:49:33 +000091 }
Xinliang David Li55415f22016-06-28 03:41:29 +000092
Xinliang David Li3e176c72016-06-28 06:58:21 +000093 std::string getNodeAttributes(const MachineBasicBlock *Node,
94 const MachineBlockFrequencyInfo *Graph) {
95 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
96 ViewHotFreqPercent);
97 }
98
Xinliang David Li55415f22016-06-28 03:41:29 +000099 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
100 const MachineBlockFrequencyInfo *MBFI) {
Xinliang David Li3e176c72016-06-28 06:58:21 +0000101 return MBFIDOTGraphTraitsBase::getEdgeAttributes(
102 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
Xinliang David Li69317f22016-06-22 16:04:51 +0000103 }
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000104};
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
Xinliang David Li80457ce2016-06-22 02:12:54 +0000140 if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
Xinliang David Li8dd5ce92016-06-28 04:07:03 +0000141 (ViewBlockFreqFuncName.empty() ||
142 F.getName().equals(ViewBlockFreqFuncName))) {
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000143 view();
144 }
145#endif
Jakub Staszak27131172011-07-16 20:23:20 +0000146 return false;
147}
148
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000149void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
150
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000151/// Pop up a ghostview window with the current block frequency propagation
152/// rendered using dot.
153void MachineBlockFrequencyInfo::view() const {
154// This code is only for debugging.
155#ifndef NDEBUG
156 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
157 "MachineBlockFrequencyDAGs");
158#else
Michael Gottesman748fe482013-12-03 20:21:17 +0000159 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
Xinliang David Libc157082016-06-21 23:36:12 +0000160 "on systems with Graphviz or gv!\n";
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000161#endif // NDEBUG
162}
163
Xinliang David Libc157082016-06-21 23:36:12 +0000164BlockFrequency
165MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000166 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
Jakub Staszak27131172011-07-16 20:23:20 +0000167}
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000168
Xinliang David Li30c50f32016-06-22 19:26:44 +0000169Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
170 const MachineBasicBlock *MBB) const {
171 const Function *F = MBFI->getFunction()->getFunction();
172 return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
173}
174
Sean Silvaf8015752016-08-02 02:15:45 +0000175Optional<uint64_t>
176MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
177 const Function *F = MBFI->getFunction()->getFunction();
178 return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
179}
180
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +0000181const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000182 return MBFI ? MBFI->getFunction() : nullptr;
Michael Gottesman65bbcdf2013-12-03 00:49:33 +0000183}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000184
Xinliang David Li3264fdd2016-06-28 00:15:45 +0000185const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
186 return MBFI ? &MBFI->getBPI() : nullptr;
187}
188
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000189raw_ostream &
190MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
191 const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000192 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000193}
194
195raw_ostream &
196MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
197 const MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000198 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000199}
200
Michael Gottesman5e985ee2013-12-14 02:37:38 +0000201uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000202 return MBFI ? MBFI->getEntryFreq() : 0;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000203}