Home | History | Annotate | Download | only in Analysis
      1 //=======-------- BlockFrequencyInfo.cpp - 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/Analysis/BlockFrequencyInfo.h"
     17 #include "llvm/Analysis/LoopInfo.h"
     18 #include "llvm/Analysis/Passes.h"
     19 #include "llvm/Analysis/BranchProbabilityInfo.h"
     20 
     21 using namespace llvm;
     22 
     23 INITIALIZE_PASS_BEGIN(BlockFrequencyInfo, "block-freq", "Block Frequency Analysis",
     24                       true, true)
     25 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfo)
     26 INITIALIZE_PASS_END(BlockFrequencyInfo, "block-freq", "Block Frequency Analysis",
     27                     true, true)
     28 
     29 char BlockFrequencyInfo::ID = 0;
     30 
     31 
     32 BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
     33   initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
     34   BFI = new BlockFrequencyImpl<BasicBlock, Function, BranchProbabilityInfo>();
     35 }
     36 
     37 BlockFrequencyInfo::~BlockFrequencyInfo() {
     38   delete BFI;
     39 }
     40 
     41 void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
     42   AU.addRequired<BranchProbabilityInfo>();
     43   AU.setPreservesAll();
     44 }
     45 
     46 bool BlockFrequencyInfo::runOnFunction(Function &F) {
     47   BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
     48   BFI->doFunction(&F, &BPI);
     49   return false;
     50 }
     51 
     52 void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const {
     53   if (BFI) BFI->print(O);
     54 }
     55 
     56 /// getblockFreq - Return block frequency. Return 0 if we don't have the
     57 /// information. Please note that initial frequency is equal to 1024. It means
     58 /// that we should not rely on the value itself, but only on the comparison to
     59 /// the other block frequencies. We do this to avoid using of floating points.
     60 ///
     61 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
     62   return BFI->getBlockFreq(BB);
     63 }
     64