16122f3e6SDimitry Andric //====----- MachineBlockFrequencyInfo.cpp - Machine Block Frequency Analysis ----====//
26122f3e6SDimitry Andric //
36122f3e6SDimitry Andric //                     The LLVM Compiler Infrastructure
46122f3e6SDimitry Andric //
56122f3e6SDimitry Andric // This file is distributed under the University of Illinois Open Source
66122f3e6SDimitry Andric // License. See LICENSE.TXT for details.
76122f3e6SDimitry Andric //
86122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
96122f3e6SDimitry Andric //
106122f3e6SDimitry Andric // Loops should be simplified before this analysis.
116122f3e6SDimitry Andric //
126122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
136122f3e6SDimitry Andric 
146122f3e6SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
15*139f7f9bSDimitry Andric #include "llvm/Analysis/BlockFrequencyImpl.h"
166122f3e6SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
17*139f7f9bSDimitry Andric #include "llvm/CodeGen/Passes.h"
18*139f7f9bSDimitry Andric #include "llvm/InitializePasses.h"
196122f3e6SDimitry Andric 
206122f3e6SDimitry Andric using namespace llvm;
216122f3e6SDimitry Andric 
226122f3e6SDimitry Andric INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
236122f3e6SDimitry Andric                       "Machine Block Frequency Analysis", true, true)
246122f3e6SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
256122f3e6SDimitry Andric INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
266122f3e6SDimitry Andric                     "Machine Block Frequency Analysis", true, true)
276122f3e6SDimitry Andric 
286122f3e6SDimitry Andric char MachineBlockFrequencyInfo::ID = 0;
296122f3e6SDimitry Andric 
306122f3e6SDimitry Andric 
316122f3e6SDimitry Andric MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() : MachineFunctionPass(ID) {
326122f3e6SDimitry Andric   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
336122f3e6SDimitry Andric   MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
346122f3e6SDimitry Andric                                 MachineBranchProbabilityInfo>();
356122f3e6SDimitry Andric }
366122f3e6SDimitry Andric 
376122f3e6SDimitry Andric MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {
386122f3e6SDimitry Andric   delete MBFI;
396122f3e6SDimitry Andric }
406122f3e6SDimitry Andric 
416122f3e6SDimitry Andric void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
426122f3e6SDimitry Andric   AU.addRequired<MachineBranchProbabilityInfo>();
436122f3e6SDimitry Andric   AU.setPreservesAll();
446122f3e6SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
456122f3e6SDimitry Andric }
466122f3e6SDimitry Andric 
476122f3e6SDimitry Andric bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
486122f3e6SDimitry Andric   MachineBranchProbabilityInfo &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
496122f3e6SDimitry Andric   MBFI->doFunction(&F, &MBPI);
506122f3e6SDimitry Andric   return false;
516122f3e6SDimitry Andric }
526122f3e6SDimitry Andric 
536122f3e6SDimitry Andric /// getblockFreq - Return block frequency. Return 0 if we don't have the
546122f3e6SDimitry Andric /// information. Please note that initial frequency is equal to 1024. It means
556122f3e6SDimitry Andric /// that we should not rely on the value itself, but only on the comparison to
566122f3e6SDimitry Andric /// the other block frequencies. We do this to avoid using of floating points.
576122f3e6SDimitry Andric ///
586122f3e6SDimitry Andric BlockFrequency MachineBlockFrequencyInfo::
59dff0c46cSDimitry Andric getBlockFreq(const MachineBasicBlock *MBB) const {
606122f3e6SDimitry Andric   return MBFI->getBlockFreq(MBB);
616122f3e6SDimitry Andric }
62