LLVM  9.0.0svn
MachineBlockFrequencyInfo.cpp
Go to the documentation of this file.
1 //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Loops should be simplified before this analysis.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/iterator.h"
22 #include "llvm/Pass.h"
25 #include <string>
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "machine-block-freq"
30 
32  "view-machine-block-freq-propagation-dags", cl::Hidden,
33  cl::desc("Pop up a window to show a dag displaying how machine block "
34  "frequencies propagate through the CFG."),
35  cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
36  clEnumValN(GVDT_Fraction, "fraction",
37  "display a graph using the "
38  "fractional block frequency representation."),
39  clEnumValN(GVDT_Integer, "integer",
40  "display a graph using the raw "
41  "integer fractional block frequency representation."),
42  clEnumValN(GVDT_Count, "count", "display a graph using the real "
43  "profile count if available.")));
44 
45 // Similar option above, but used to control BFI display only after MBP pass
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.")));
61 
62 // Command line option to specify the name of the function for CFG dump
63 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=
65 
66 // Command line option to specify hot frequency threshold.
67 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc=
69 
71  "print-machine-bfi", cl::init(false), cl::Hidden,
72  cl::desc("Print the machine block frequency info."));
73 
74 // Command line option to specify the name of the function for block frequency
75 // dump. Defined in Analysis/BlockFrequencyInfo.cpp.
77 
78 static GVDAGType getGVDT() {
81 
83 }
84 
85 namespace llvm {
86 
87 template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
88  using NodeRef = const MachineBasicBlock *;
91 
93  return &G->getFunction()->front();
94  }
95 
97  return N->succ_begin();
98  }
99 
100  static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
101 
103  return nodes_iterator(G->getFunction()->begin());
104  }
105 
107  return nodes_iterator(G->getFunction()->end());
108  }
109 };
110 
114 
115 template <>
116 struct DOTGraphTraits<MachineBlockFrequencyInfo *>
117  : public MBFIDOTGraphTraitsBase {
118  const MachineFunction *CurFunc = nullptr;
120 
121  explicit DOTGraphTraits(bool isSimple = false)
123 
124  std::string getNodeLabel(const MachineBasicBlock *Node,
125  const MachineBlockFrequencyInfo *Graph) {
126  int layout_order = -1;
127  // Attach additional ordering information if 'isSimple' is false.
128  if (!isSimple()) {
129  const MachineFunction *F = Node->getParent();
130  if (!CurFunc || F != CurFunc) {
131  if (CurFunc)
132  LayoutOrderMap.clear();
133 
134  CurFunc = F;
135  int O = 0;
136  for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
137  LayoutOrderMap[&*MBI] = O;
138  }
139  }
140  layout_order = LayoutOrderMap[Node];
141  }
142  return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
143  layout_order);
144  }
145 
146  std::string getNodeAttributes(const MachineBasicBlock *Node,
147  const MachineBlockFrequencyInfo *Graph) {
149  ViewHotFreqPercent);
150  }
151 
152  std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
153  const MachineBlockFrequencyInfo *MBFI) {
155  Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
156  }
157 };
158 
159 } // end namespace llvm
160 
162  "Machine Block Frequency Analysis", true, true)
166  "Machine Block Frequency Analysis", true, true)
167 
168 char MachineBlockFrequencyInfo::ID = 0;
169 
170 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
171  : MachineFunctionPass(ID) {
173 }
174 
176 
180  AU.setPreservesAll();
182 }
183 
185  const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
186  const MachineLoopInfo &MLI) {
187  if (!MBFI)
188  MBFI.reset(new ImplType);
189  MBFI->calculate(F, MBPI, MLI);
191  (ViewBlockFreqFuncName.empty() ||
192  F.getName().equals(ViewBlockFreqFuncName))) {
193  view("MachineBlockFrequencyDAGS." + F.getName());
194  }
195  if (PrintMachineBlockFreq &&
196  (PrintBlockFreqFuncName.empty() ||
198  MBFI->print(dbgs());
199  }
200 }
201 
204  getAnalysis<MachineBranchProbabilityInfo>();
205  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
206  calculate(F, MBPI, MLI);
207  return false;
208 }
209 
211 
212 /// Pop up a ghostview window with the current block frequency propagation
213 /// rendered using dot.
215  // This code is only for debugging.
216  ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
217 }
218 
221  return MBFI ? MBFI->getBlockFreq(MBB) : 0;
222 }
223 
225  const MachineBasicBlock *MBB) const {
226  const Function &F = MBFI->getFunction()->getFunction();
227  return MBFI ? MBFI->getBlockProfileCount(F, MBB) : None;
228 }
229 
232  const Function &F = MBFI->getFunction()->getFunction();
233  return MBFI ? MBFI->getProfileCountFromFreq(F, Freq) : None;
234 }
235 
236 bool
238  assert(MBFI && "Expected analysis to be available");
239  return MBFI->isIrrLoopHeader(MBB);
240 }
241 
243  return MBFI ? MBFI->getFunction() : nullptr;
244 }
245 
247  return MBFI ? &MBFI->getBPI() : nullptr;
248 }
249 
250 raw_ostream &
252  const BlockFrequency Freq) const {
253  return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
254 }
255 
256 raw_ostream &
258  const MachineBasicBlock *MBB) const {
259  return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
260 }
261 
263  return MBFI ? MBFI->getEntryFreq() : 0;
264 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
std::string getNodeAttributes(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
cl::opt< unsigned > ViewHotFreqPercent
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static ChildIteratorType child_end(const NodeRef N)
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
F(f)
static GVDAGType getGVDT()
static ChildIteratorType child_begin(const NodeRef N)
*ViewGraph Emit a dot run run gv on the postscript *then cleanup For use from the debugger *void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
Definition: GraphWriter.h:366
Machine Block Frequency Analysis
AnalysisUsage & addRequired()
INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE, "Machine Block Frequency Analysis", true, true) INITIALIZE_PASS_END(MachineBlockFrequencyInfo
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
void view(const Twine &Name, bool isSimple=true) const
Pop up a ghostview window with the current block frequency propagation rendered using dot...
std::string getNodeLabel(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)
std::string getNodeLabel(NodeRef Node, const BlockFrequencyInfoT *Graph, GVDAGType GType, int layout_order=-1)
cl::opt< GVDAGType > ViewBlockLayoutWithBFI("view-block-layout-with-bfi", cl::Hidden, cl::desc("Pop up a window to show a dag displaying MBP layout and associated " "block frequencies of the CFG."), cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), clEnumValN(GVDT_Fraction, "fraction", "display a graph using the " "fractional block frequency representation."), clEnumValN(GVDT_Integer, "integer", "display a graph using the raw " "integer fractional block frequency representation."), clEnumValN(GVDT_Count, "count", "display a graph using the real " "profile count if available.")))
static bool isSimple(Instruction *I)
Optional< uint64_t > getBlockProfileCount(const MachineBasicBlock *MBB) const
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
static cl::opt< bool > PrintMachineBlockFreq("print-machine-bfi", cl::init(false), cl::Hidden, cl::desc("Print the machine block frequency info."))
std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, const MachineBlockFrequencyInfo *MBFI)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G)
static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
#define DEBUG_TYPE
cl::opt< std::string > PrintBlockFreqFuncName
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:652
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
raw_ostream & printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const
Represent the analysis usage information of a pass.
MachineBasicBlock::const_succ_iterator ChildIteratorType
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
DenseMap< const MachineBasicBlock *, int > LayoutOrderMap
const MachineBranchProbabilityInfo * getMBPI() const
const MachineBasicBlock & front() const
const MachineFunction * getFunction() const
std::string getEdgeAttributes(NodeRef Node, EdgeIter EI, const BlockFrequencyInfoT *BFI, const BranchProbabilityInfoT *BPI, unsigned HotPercentThreshold=0)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
void initializeMachineBlockFrequencyInfoPass(PassRegistry &)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to &#39;dot...
std::string getNodeAttributes(NodeRef Node, const BlockFrequencyInfoT *Graph, unsigned HotPercentThreshold=0)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void setPreservesAll()
Set by analyses that do not transform their input at all.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
static cl::opt< GVDAGType > ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags", cl::Hidden, cl::desc("Pop up a window to show a dag displaying how machine block " "frequencies propagate through the CFG."), cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."), clEnumValN(GVDT_Fraction, "fraction", "display a graph using the " "fractional block frequency representation."), clEnumValN(GVDT_Integer, "integer", "display a graph using the raw " "integer fractional block frequency representation."), clEnumValN(GVDT_Count, "count", "display a graph using the real " "profile count if available.")))
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:627
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:160
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
#define N
typename GTraits::ChildIteratorType EdgeIter
static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
cl::opt< std::string > ViewBlockFreqFuncName
Optional< uint64_t > getProfileCountFromFreq(uint64_t Freq) const
bool isIrrLoopHeader(const MachineBasicBlock *MBB)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
void calculate(const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, const MachineLoopInfo &MLI)
calculate - compute block frequency info for the given function.
Shared implementation for block frequency analysis.
Machine Block Frequency true