Duncan P. N. Exon Smith | 689a507 | 2014-04-11 23:20:58 +0000 | [diff] [blame] | 1 | //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===// |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 2 | // |
| 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 Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Duncan P. N. Exon Smith | 689a507 | 2014-04-11 23:20:58 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/BlockFrequencyInfoImpl.h" |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Passes.h" |
| 20 | #include "llvm/InitializePasses.h" |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
| 22 | #include "llvm/Support/Debug.h" |
Xinliang David Li | 69317f2 | 2016-06-22 16:04:51 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Format.h" |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/GraphWriter.h" |
Xinliang David Li | 69317f2 | 2016-06-22 16:04:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "block-freq" |
| 30 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 31 | #ifndef NDEBUG |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 32 | |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 33 | static 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 Li | 30c50f3 | 2016-06-22 19:26:44 +0000 | [diff] [blame] | 44 | clEnumValN(GVDT_Count, "count", "display a graph using the real " |
| 45 | "profile count if available."), |
| 46 | |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 47 | clEnumValEnd)); |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 48 | |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame] | 49 | extern cl::opt<std::string> ViewBlockFreqFuncName; |
Simon Pilgrim | 2d53158 | 2016-06-28 12:34:44 +0000 | [diff] [blame] | 50 | extern cl::opt<unsigned> ViewHotFreqPercent; |
Xinliang David Li | 80457ce | 2016-06-22 02:12:54 +0000 | [diff] [blame] | 51 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 52 | namespace llvm { |
| 53 | |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 54 | template <> struct GraphTraits<MachineBlockFrequencyInfo *> { |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 55 | typedef const MachineBasicBlock NodeType; |
Tim Shen | eb3958f | 2016-08-17 20:07:29 +0000 | [diff] [blame] | 56 | typedef const MachineBasicBlock *NodeRef; |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 57 | typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; |
Tim Shen | b5e0f5a | 2016-08-19 21:20:13 +0000 | [diff] [blame^] | 58 | typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator; |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 59 | |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 60 | static inline const NodeType * |
| 61 | getEntryNode(const MachineBlockFrequencyInfo *G) { |
Duncan P. N. Exon Smith | 0ac8eb9 | 2015-10-09 19:23:20 +0000 | [diff] [blame] | 62 | return &G->getFunction()->front(); |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 63 | } |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 64 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 65 | static ChildIteratorType child_begin(const NodeType *N) { |
| 66 | return N->succ_begin(); |
| 67 | } |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 68 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 69 | static ChildIteratorType child_end(const NodeType *N) { |
| 70 | return N->succ_end(); |
| 71 | } |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 72 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 73 | static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) { |
Tim Shen | b5e0f5a | 2016-08-19 21:20:13 +0000 | [diff] [blame^] | 74 | return nodes_iterator(G->getFunction()->begin()); |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 75 | } |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 76 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 77 | static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) { |
Tim Shen | b5e0f5a | 2016-08-19 21:20:13 +0000 | [diff] [blame^] | 78 | return nodes_iterator(G->getFunction()->end()); |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 82 | typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo, |
| 83 | MachineBranchProbabilityInfo> |
| 84 | MBFIDOTGraphTraitsBase; |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 85 | template <> |
| 86 | struct DOTGraphTraits<MachineBlockFrequencyInfo *> |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 87 | : public MBFIDOTGraphTraitsBase { |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 88 | explicit DOTGraphTraits(bool isSimple = false) |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 89 | : MBFIDOTGraphTraitsBase(isSimple) {} |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 90 | |
| 91 | std::string getNodeLabel(const MachineBasicBlock *Node, |
| 92 | const MachineBlockFrequencyInfo *Graph) { |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 93 | return MBFIDOTGraphTraitsBase::getNodeLabel( |
| 94 | Node, Graph, ViewMachineBlockFreqPropagationDAG); |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 95 | } |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 96 | |
Xinliang David Li | 3e176c7 | 2016-06-28 06:58:21 +0000 | [diff] [blame] | 97 | std::string getNodeAttributes(const MachineBasicBlock *Node, |
| 98 | const MachineBlockFrequencyInfo *Graph) { |
| 99 | return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph, |
| 100 | ViewHotFreqPercent); |
| 101 | } |
| 102 | |
Xinliang David Li | 55415f2 | 2016-06-28 03:41:29 +0000 | [diff] [blame] | 103 | std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, |
| 104 | const MachineBlockFrequencyInfo *MBFI) { |
Xinliang David Li | 3e176c7 | 2016-06-28 06:58:21 +0000 | [diff] [blame] | 105 | return MBFIDOTGraphTraitsBase::getEdgeAttributes( |
| 106 | Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent); |
Xinliang David Li | 69317f2 | 2016-06-22 16:04:51 +0000 | [diff] [blame] | 107 | } |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 110 | } // end namespace llvm |
| 111 | #endif |
| 112 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 113 | INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq", |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 114 | "Machine Block Frequency Analysis", true, true) |
| 115 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 116 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 117 | INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq", |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 118 | "Machine Block Frequency Analysis", true, true) |
| 119 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 120 | char MachineBlockFrequencyInfo::ID = 0; |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 121 | |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 122 | MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() |
| 123 | : MachineFunctionPass(ID) { |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 124 | initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry()); |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 127 | MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {} |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 128 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 129 | void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 130 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 131 | AU.addRequired<MachineLoopInfo>(); |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 132 | AU.setPreservesAll(); |
Jakub Staszak | cb7c0a4 | 2011-07-21 22:59:09 +0000 | [diff] [blame] | 133 | MachineFunctionPass::getAnalysisUsage(AU); |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Jakub Staszak | 875ebd5 | 2011-07-25 19:25:40 +0000 | [diff] [blame] | 136 | bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 137 | MachineBranchProbabilityInfo &MBPI = |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 138 | getAnalysis<MachineBranchProbabilityInfo>(); |
| 139 | MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 140 | if (!MBFI) |
| 141 | MBFI.reset(new ImplType); |
Cong Hou | 5e67b66 | 2015-07-15 19:58:26 +0000 | [diff] [blame] | 142 | MBFI->calculate(F, MBPI, MLI); |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 143 | #ifndef NDEBUG |
Xinliang David Li | 80457ce | 2016-06-22 02:12:54 +0000 | [diff] [blame] | 144 | if (ViewMachineBlockFreqPropagationDAG != GVDT_None && |
Xinliang David Li | 8dd5ce9 | 2016-06-28 04:07:03 +0000 | [diff] [blame] | 145 | (ViewBlockFreqFuncName.empty() || |
| 146 | F.getName().equals(ViewBlockFreqFuncName))) { |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 147 | view(); |
| 148 | } |
| 149 | #endif |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 153 | void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); } |
| 154 | |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 155 | /// Pop up a ghostview window with the current block frequency propagation |
| 156 | /// rendered using dot. |
| 157 | void MachineBlockFrequencyInfo::view() const { |
| 158 | // This code is only for debugging. |
| 159 | #ifndef NDEBUG |
| 160 | ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), |
| 161 | "MachineBlockFrequencyDAGs"); |
| 162 | #else |
Michael Gottesman | 748fe48 | 2013-12-03 20:21:17 +0000 | [diff] [blame] | 163 | errs() << "MachineBlockFrequencyInfo::view is only available in debug builds " |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 164 | "on systems with Graphviz or gv!\n"; |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 165 | #endif // NDEBUG |
| 166 | } |
| 167 | |
Xinliang David Li | bc15708 | 2016-06-21 23:36:12 +0000 | [diff] [blame] | 168 | BlockFrequency |
| 169 | MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 170 | return MBFI ? MBFI->getBlockFreq(MBB) : 0; |
Jakub Staszak | 2713117 | 2011-07-16 20:23:20 +0000 | [diff] [blame] | 171 | } |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 172 | |
Xinliang David Li | 30c50f3 | 2016-06-22 19:26:44 +0000 | [diff] [blame] | 173 | Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount( |
| 174 | const MachineBasicBlock *MBB) const { |
| 175 | const Function *F = MBFI->getFunction()->getFunction(); |
| 176 | return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None; |
| 177 | } |
| 178 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 179 | Optional<uint64_t> |
| 180 | MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const { |
| 181 | const Function *F = MBFI->getFunction()->getFunction(); |
| 182 | return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None; |
| 183 | } |
| 184 | |
Duncan P. N. Exon Smith | 936aef9 | 2014-03-25 18:01:32 +0000 | [diff] [blame] | 185 | const MachineFunction *MachineBlockFrequencyInfo::getFunction() const { |
Duncan P. N. Exon Smith | 10be9a8 | 2014-04-21 17:57:07 +0000 | [diff] [blame] | 186 | return MBFI ? MBFI->getFunction() : nullptr; |
Michael Gottesman | 65bbcdf | 2013-12-03 00:49:33 +0000 | [diff] [blame] | 187 | } |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 188 | |
Xinliang David Li | 3264fdd | 2016-06-28 00:15:45 +0000 | [diff] [blame] | 189 | const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const { |
| 190 | return MBFI ? &MBFI->getBPI() : nullptr; |
| 191 | } |
| 192 | |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 193 | raw_ostream & |
| 194 | MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, |
| 195 | const BlockFrequency Freq) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 196 | return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | raw_ostream & |
| 200 | MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS, |
| 201 | const MachineBasicBlock *MBB) const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 202 | return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Michael Gottesman | 5e985ee | 2013-12-14 02:37:38 +0000 | [diff] [blame] | 205 | uint64_t MachineBlockFrequencyInfo::getEntryFreq() const { |
Duncan P. N. Exon Smith | 3dbe105 | 2014-03-25 18:01:38 +0000 | [diff] [blame] | 206 | return MBFI ? MBFI->getEntryFreq() : 0; |
Michael Gottesman | fd5c4b2 | 2013-12-14 00:06:03 +0000 | [diff] [blame] | 207 | } |