1 //====----- MachineBlockFrequency.cpp - Machine Block Frequency Analysis ----====//
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
14 #include "llvm/InitializePasses.h"
15 #include "llvm/Analysis/BlockFrequencyImpl.h"
16 #include "llvm/CodeGen/MachineBlockFrequency.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
19
20 using namespace llvm;
21
22 INITIALIZE_PASS_BEGIN(MachineBlockFrequency, "machine-block-freq",
23 "Machine Block Frequency Analysis", true, true)
24 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
25 INITIALIZE_PASS_END(MachineBlockFrequency, "machine-block-freq",
26 "Machine Block Frequency Analysis", true, true)
27
28 char MachineBlockFrequency::ID = 0;
29
30
MachineBlockFrequency()31 MachineBlockFrequency::MachineBlockFrequency() : MachineFunctionPass(ID) {
32 initializeMachineBlockFrequencyPass(*PassRegistry::getPassRegistry());
33 MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
34 MachineBranchProbabilityInfo>();
35 }
36
~MachineBlockFrequency()37 MachineBlockFrequency::~MachineBlockFrequency() {
38 delete MBFI;
39 }
40
getAnalysisUsage(AnalysisUsage & AU) const41 void MachineBlockFrequency::getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<MachineBranchProbabilityInfo>();
43 AU.setPreservesAll();
44 }
45
runOnMachineFunction(MachineFunction & F)46 bool MachineBlockFrequency::runOnMachineFunction(MachineFunction &F) {
47 MachineBranchProbabilityInfo &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
48 MBFI->doFunction(&F, &MBPI);
49 return false;
50 }
51
52 /// getblockFreq - Return block frequency. Never return 0, value must be
53 /// positive. Please note that initial frequency is equal to 1024. It means that
54 /// we should not rely on the value itself, but only on the comparison to the
55 /// other block frequencies. We do this to avoid using of floating points.
56 ///
getBlockFreq(MachineBasicBlock * MBB)57 uint32_t MachineBlockFrequency::getBlockFreq(MachineBasicBlock *MBB) {
58 return MBFI->getBlockFreq(MBB);
59 }
60