Use BlockFrequency instead of uint32_t in BlockFrequencyInfo.
authorJakub Staszak <jstaszak@apple.com>
Wed, 27 Jul 2011 22:05:51 +0000 (22:05 +0000)
committerJakub Staszak <jstaszak@apple.com>
Wed, 27 Jul 2011 22:05:51 +0000 (22:05 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@136278 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/BlockFrequencyImpl.h
include/llvm/Analysis/BlockFrequencyInfo.h
include/llvm/CodeGen/MachineBlockFrequencyInfo.h
lib/Analysis/BlockFrequencyInfo.cpp
lib/CodeGen/MachineBlockFrequencyInfo.cpp

index 6cb6f01d8812edb0bc41694bbdc84fe7b0a4b1af..bfe2d66d1ce205e4318135a5693bb331f4053231 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/Support/BlockFrequency.h"
 #include "llvm/Support/BranchProbability.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
@@ -40,7 +41,7 @@ class MachineBlockFrequencyInfo;
 template<class BlockT, class FunctionT, class BlockProbInfoT>
 class BlockFrequencyImpl {
 
-  DenseMap<BlockT *, uint32_t> Freqs;
+  DenseMap<BlockT *, BlockFrequency> Freqs;
 
   BlockProbInfoT *BPI;
 
@@ -64,26 +65,21 @@ class BlockFrequencyImpl {
     return ss.str();
   }
 
-  void setBlockFreq(BlockT *BB, uint32_t Freq) {
+  void setBlockFreq(BlockT *BB, BlockFrequency Freq) {
     Freqs[BB] = Freq;
     DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") = " << Freq << "\n");
   }
 
   /// getEdgeFreq - Return edge frequency based on SRC frequency and Src -> Dst
   /// edge probability.
-  uint32_t getEdgeFreq(BlockT *Src, BlockT *Dst) const {
+  BlockFrequency getEdgeFreq(BlockT *Src, BlockT *Dst) const {
     BranchProbability Prob = BPI->getEdgeProbability(Src, Dst);
-    uint64_t N = Prob.getNumerator();
-    uint64_t D = Prob.getDenominator();
-    uint64_t Res = (N * getBlockFreq(Src)) / D;
-
-    assert(Res <= UINT32_MAX);
-    return (uint32_t) Res;
+    return getBlockFreq(Src) * Prob;
   }
 
   /// incBlockFreq - Increase BB block frequency by FREQ.
   ///
-  void incBlockFreq(BlockT *BB, uint32_t Freq) {
+  void incBlockFreq(BlockT *BB, BlockFrequency Freq) {
     Freqs[BB] += Freq;
     DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") += " << Freq
                  << " --> " << Freqs[BB] << "\n");
@@ -95,13 +91,13 @@ class BlockFrequencyImpl {
     uint64_t N = Prob.getNumerator();
     assert(N && "Illegal division by zero!");
     uint64_t D = Prob.getDenominator();
-    uint64_t Freq = (Freqs[BB] * D) / N;
+    uint64_t Freq = (Freqs[BB].getFrequency() * D) / N;
 
     // Should we assert it?
     if (Freq > UINT32_MAX)
       Freq = UINT32_MAX;
 
-    Freqs[BB] = (uint32_t) Freq;
+    Freqs[BB] = BlockFrequency(Freq);
     DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") /= (" << Prob
                  << ") --> " << Freqs[BB] << "\n");
   }
@@ -136,15 +132,6 @@ class BlockFrequencyImpl {
   }
 
 
-  /// Return a probability of getting to the DST block through SRC->DST edge.
-  ///
-  BranchProbability getBackEdgeProbability(BlockT *Src, BlockT *Dst) const {
-    uint32_t N = getEdgeFreq(Src, Dst);
-    uint32_t D = getBlockFreq(Dst);
-
-    return BranchProbability(N, D);
-  }
-
   /// isReachable - Returns if BB block is reachable from the entry.
   ///
   bool isReachable(BlockT *BB) {
@@ -252,9 +239,9 @@ class BlockFrequencyImpl {
       BlockT *Pred = *PI;
       assert(Pred);
       if (isReachable(Pred) && isBackedge(Pred, Head)) {
-        BranchProbability Prob = getBackEdgeProbability(Pred, Head);
-        uint64_t N = Prob.getNumerator();
-        uint64_t D = Prob.getDenominator();
+        uint64_t N = getEdgeFreq(Pred, Head).getFrequency();
+        uint64_t D = getBlockFreq(Head).getFrequency();
+        assert(N <= 1024 && "Backedge frequency must be <= 1024!");
         uint64_t Res = (N * START_FREQ) / D;
 
         assert(Res <= UINT32_MAX);
@@ -315,8 +302,8 @@ class BlockFrequencyImpl {
 
 public:
   /// getBlockFreq - Return block frequency. Return 0 if we don't have it.
-  uint32_t getBlockFreq(BlockT *BB) const {
-    typename DenseMap<BlockT *, uint32_t>::const_iterator I = Freqs.find(BB);
+  BlockFrequency getBlockFreq(BlockT *BB) const {
+    typename DenseMap<BlockT *, BlockFrequency>::const_iterator I = Freqs.find(BB);
     if (I != Freqs.end())
       return I->second;
     return 0;
index bfc615d4537feb2804ca7e34e7d159227faf4b98..bd9807dc89658371de018d5dd6976ef0643dddf7 100644 (file)
@@ -15,6 +15,7 @@
 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
 
 #include "llvm/Pass.h"
+#include "llvm/Support/BlockFrequency.h"
 #include <climits>
 
 namespace llvm {
@@ -45,7 +46,7 @@ public:
   /// that we should not rely on the value itself, but only on the comparison to
   /// the other block frequencies. We do this to avoid using of floating points.
   ///
-  uint32_t getBlockFreq(BasicBlock *BB);
+  BlockFrequency getBlockFreq(BasicBlock *BB);
 };
 
 }
index f888e54593d6e80edac587507ccb7e6301e132c0..63ab2ef850bd810a0550ec99fa4da2bab2902846 100644 (file)
@@ -15,6 +15,7 @@
 #define LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
 
 #include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/Support/BlockFrequency.h"
 #include <climits>
 
 namespace llvm {
@@ -45,7 +46,7 @@ public:
   /// that we should not rely on the value itself, but only on the comparison to
   /// the other block frequencies. We do this to avoid using of floating points.
   ///
-  uint32_t getBlockFreq(MachineBasicBlock *MBB);
+  BlockFrequency getBlockFreq(MachineBasicBlock *MBB);
 };
 
 }
index b93e9a6aa9518bd8b7127b0fe62c02bd62013906..5bb3540a1ff8e7e1215289bef15d9cbf23e413e5 100644 (file)
@@ -54,6 +54,6 @@ bool BlockFrequencyInfo::runOnFunction(Function &F) {
 /// that we should not rely on the value itself, but only on the comparison to
 /// the other block frequencies. We do this to avoid using of floating points.
 ///
-uint32_t BlockFrequencyInfo::getBlockFreq(BasicBlock *BB) {
+BlockFrequency BlockFrequencyInfo::getBlockFreq(BasicBlock *BB) {
   return BFI->getBlockFreq(BB);
 }
index 583e9c4efd469fb968c19e8fa1ecb463d09a24d9..4e08ebe6a9430f80f0b0458e747759d078ab4892 100644 (file)
@@ -55,6 +55,6 @@ bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
 /// that we should not rely on the value itself, but only on the comparison to
 /// the other block frequencies. We do this to avoid using of floating points.
 ///
-uint32_t MachineBlockFrequencyInfo::getBlockFreq(MachineBasicBlock *MBB) {
+BlockFrequency MachineBlockFrequencyInfo::getBlockFreq(MachineBasicBlock *MBB) {
   return MBFI->getBlockFreq(MBB);
 }