LLVM API Documentation

MachineBranchProbabilityInfo.h
Go to the documentation of this file.
00001 //=- MachineBranchProbabilityInfo.h - Branch Probability Analysis -*- C++ -*-=//
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 // This pass is used to evaluate branch probabilties on machine basic blocks.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
00015 #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
00016 
00017 #include "llvm/CodeGen/MachineBasicBlock.h"
00018 #include "llvm/Pass.h"
00019 #include "llvm/Support/BranchProbability.h"
00020 #include <climits>
00021 
00022 namespace llvm {
00023 
00024 class MachineBranchProbabilityInfo : public ImmutablePass {
00025   virtual void anchor();
00026 
00027   // Default weight value. Used when we don't have information about the edge.
00028   // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
00029   // the successors have a weight yet. But it doesn't make sense when providing
00030   // weight to an edge that may have siblings with non-zero weights. This can
00031   // be handled various ways, but it's probably fine for an edge with unknown
00032   // weight to just "inherit" the non-zero weight of an adjacent successor.
00033   static const uint32_t DEFAULT_WEIGHT = 16;
00034 
00035 public:
00036   static char ID;
00037 
00038   MachineBranchProbabilityInfo() : ImmutablePass(ID) {
00039     PassRegistry &Registry = *PassRegistry::getPassRegistry();
00040     initializeMachineBranchProbabilityInfoPass(Registry);
00041   }
00042 
00043   void getAnalysisUsage(AnalysisUsage &AU) const override {
00044     AU.setPreservesAll();
00045   }
00046 
00047   // Return edge weight. If we don't have any informations about it - return
00048   // DEFAULT_WEIGHT.
00049   uint32_t getEdgeWeight(const MachineBasicBlock *Src,
00050                          const MachineBasicBlock *Dst) const;
00051 
00052   // Same thing, but using a const_succ_iterator from Src. This is faster when
00053   // the iterator is already available.
00054   uint32_t getEdgeWeight(const MachineBasicBlock *Src,
00055                          MachineBasicBlock::const_succ_iterator Dst) const;
00056 
00057   // Get sum of the block successors' weights, potentially scaling them to fit
00058   // within 32-bits. If scaling is required, sets Scale based on the necessary
00059   // adjustment. Any edge weights used with the sum should be divided by Scale.
00060   uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const;
00061 
00062   // A 'Hot' edge is an edge which probability is >= 80%.
00063   bool isEdgeHot(const MachineBasicBlock *Src,
00064                  const MachineBasicBlock *Dst) const;
00065 
00066   // Return a hot successor for the block BB or null if there isn't one.
00067   // NB: This routine's complexity is linear on the number of successors.
00068   MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
00069 
00070   // Return a probability as a fraction between 0 (0% probability) and
00071   // 1 (100% probability), however the value is never equal to 0, and can be 1
00072   // only iff SRC block has only one successor.
00073   // NB: This routine's complexity is linear on the number of successors of
00074   // Src. Querying sequentially for each successor's probability is a quadratic
00075   // query pattern.
00076   BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
00077                                        const MachineBasicBlock *Dst) const;
00078 
00079   // Print value between 0 (0% probability) and 1 (100% probability),
00080   // however the value is never equal to 0, and can be 1 only iff SRC block
00081   // has only one successor.
00082   raw_ostream &printEdgeProbability(raw_ostream &OS,
00083                                     const MachineBasicBlock *Src,
00084                                     const MachineBasicBlock *Dst) const;
00085 };
00086 
00087 }
00088 
00089 
00090 #endif