LLVM API Documentation

BlockFrequencyInfo.cpp
Go to the documentation of this file.
00001 //=======-------- BlockFrequencyInfo.cpp - Block Frequency Analysis -------=======//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Loops should be simplified before this analysis.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Analysis/BlockFrequencyInfo.h"
00015 #include "llvm/Analysis/BlockFrequencyImpl.h"
00016 #include "llvm/Analysis/BranchProbabilityInfo.h"
00017 #include "llvm/Analysis/LoopInfo.h"
00018 #include "llvm/Analysis/Passes.h"
00019 #include "llvm/InitializePasses.h"
00020 
00021 using namespace llvm;
00022 
00023 INITIALIZE_PASS_BEGIN(BlockFrequencyInfo, "block-freq", "Block Frequency Analysis",
00024                       true, true)
00025 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfo)
00026 INITIALIZE_PASS_END(BlockFrequencyInfo, "block-freq", "Block Frequency Analysis",
00027                     true, true)
00028 
00029 char BlockFrequencyInfo::ID = 0;
00030 
00031 
00032 BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
00033   initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
00034   BFI = new BlockFrequencyImpl<BasicBlock, Function, BranchProbabilityInfo>();
00035 }
00036 
00037 BlockFrequencyInfo::~BlockFrequencyInfo() {
00038   delete BFI;
00039 }
00040 
00041 void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
00042   AU.addRequired<BranchProbabilityInfo>();
00043   AU.setPreservesAll();
00044 }
00045 
00046 bool BlockFrequencyInfo::runOnFunction(Function &F) {
00047   BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
00048   BFI->doFunction(&F, &BPI);
00049   return false;
00050 }
00051 
00052 void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const {
00053   if (BFI) BFI->print(O);
00054 }
00055 
00056 /// getblockFreq - Return block frequency. Return 0 if we don't have the
00057 /// information. Please note that initial frequency is equal to 1024. It means
00058 /// that we should not rely on the value itself, but only on the comparison to
00059 /// the other block frequencies. We do this to avoid using of floating points.
00060 ///
00061 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
00062   return BFI->getBlockFreq(BB);
00063 }