]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/commitdiff
Move more fragments of spill weight calculation into CalcSpillWeights.h
authorJakob Stoklund Olesen <stoklund@2pi.dk>
Mon, 14 Feb 2011 23:15:38 +0000 (23:15 +0000)
committerJakob Stoklund Olesen <stoklund@2pi.dk>
Mon, 14 Feb 2011 23:15:38 +0000 (23:15 +0000)
Simplify the spill weight calculation a bit by bypassing
getApproximateInstructionCount() and using LiveInterval::getSize() directly.
This changes the computed spill weights, but only by a constant factor in each
function. It should not affect how spill weights compare against each other, and
so it shouldn't affect code generation.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125530 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/CalcSpillWeights.h
include/llvm/CodeGen/LiveIntervalAnalysis.h
lib/CodeGen/CalcSpillWeights.cpp
lib/CodeGen/LiveIntervalAnalysis.cpp

index 24264d7f97f0e7177df61c911f3924e106c0ceb6..853ebf99a87bdf657ad9c422b0afbaf101f9fda1 100644 (file)
@@ -20,6 +20,26 @@ namespace llvm {
   class LiveIntervals;
   class MachineLoopInfo;
 
+  /// normalizeSpillWeight - The spill weight of a live interval is computed as:
+  ///
+  ///   (sum(use freq) + sum(def freq)) / (K + size)
+  ///
+  /// @param UseDefFreq Expected number of executed use and def instructions
+  ///                   per function call. Derived from block frequencies.
+  /// @param Size       Size of live interval as returnexd by getSize()
+  ///
+  static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size) {
+    // The magic constant 200 corresponds to approx. 25 instructions since
+    // SlotIndexes allocate 8 slots per instruction.
+    //
+    // The constant is added to avoid depending too much on accidental SlotIndex
+    // gaps for small intervals. The effect is that small intervals have a spill
+    // weight that is mostly proportional to the number of uses, while large
+    // intervals get a spill weight that is closer to a use density.
+    //
+    return UseDefFreq / (Size + 200);
+  }
+
   /// VirtRegAuxInfo - Calculate auxiliary information for a virtual
   /// register such as its spill weight and allocation hint.
   class VirtRegAuxInfo {
index 3d1862076e977b72cb0dc0349a2bf7c88b339e97..b09f8d11106671f5491a1d157adf67a1f7b9b8a0 100644 (file)
@@ -75,14 +75,6 @@ namespace llvm {
     // Calculate the spill weight to assign to a single instruction.
     static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth);
 
-    // After summing the spill weights of all defs and uses, the final weight
-    // should be normalized, dividing the weight of the interval by its size.
-    // This encourages spilling of intervals that are large and have few uses,
-    // and discourages spilling of small intervals with many uses.
-    void normalizeSpillWeight(LiveInterval &li) {
-      li.weight /= getApproximateInstructionCount(li) + 25;
-    }
-
     typedef Reg2IntervalMap::iterator iterator;
     typedef Reg2IntervalMap::const_iterator const_iterator;
     const_iterator begin() const { return r2iMap_.begin(); }
@@ -461,9 +453,6 @@ namespace llvm {
         DenseMap<unsigned,unsigned> &MBBVRegsMap,
         std::vector<LiveInterval*> &NewLIs);
 
-    // Normalize the spill weight of all the intervals in NewLIs.
-    void normalizeSpillWeights(std::vector<LiveInterval*> &NewLIs);
-
     static LiveInterval* createInterval(unsigned Reg);
 
     void printInstrs(raw_ostream &O) const;
index 6b57a07500c7fbd7e6bae35eb6f0eddc1ef20d24..76bb3d148b0b0ef27ea72a6b5275cec7b0c069ec 100644 (file)
@@ -174,8 +174,7 @@ void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
       totalWeight *= 0.5F;
   }
 
-  li.weight = totalWeight;
-  lis_.normalizeSpillWeight(li);
+  li.weight = normalizeSpillWeight(totalWeight, li.getSize());
 }
 
 void VirtRegAuxInfo::CalculateRegClass(unsigned reg) {
index e8210244ca7c543fee03bb9c26cad5379590b6bf..aef5b5f77e78457b02d2e2891b1f5112ec534761 100644 (file)
@@ -20,6 +20,7 @@
 #include "VirtRegMap.h"
 #include "llvm/Value.h"
 #include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/CodeGen/CalcSpillWeights.h"
 #include "llvm/CodeGen/LiveVariables.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/MachineInstr.h"
@@ -1706,10 +1707,10 @@ LiveIntervals::getSpillWeight(bool isDef, bool isUse, unsigned loopDepth) {
   return (isDef + isUse) * lc;
 }
 
-void
-LiveIntervals::normalizeSpillWeights(std::vector<LiveInterval*> &NewLIs) {
+static void normalizeSpillWeights(std::vector<LiveInterval*> &NewLIs) {
   for (unsigned i = 0, e = NewLIs.size(); i != e; ++i)
-    normalizeSpillWeight(*NewLIs[i]);
+    NewLIs[i]->weight =
+      normalizeSpillWeight(NewLIs[i]->weight, NewLIs[i]->getSize());
 }
 
 std::vector<LiveInterval*> LiveIntervals::