Home | History | Annotate | Download | only in CodeGen
      1 //===---------------- lib/CodeGen/CalcSpillWeights.h ------------*- C++ -*-===//
      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 
     11 #ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
     12 #define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
     13 
     14 #include "llvm/ADT/DenseMap.h"
     15 #include "llvm/CodeGen/SlotIndexes.h"
     16 
     17 namespace llvm {
     18 
     19   class LiveInterval;
     20   class LiveIntervals;
     21   class MachineBlockFrequencyInfo;
     22   class MachineLoopInfo;
     23 
     24   /// normalizeSpillWeight - The spill weight of a live interval is computed as:
     25   ///
     26   ///   (sum(use freq) + sum(def freq)) / (K + size)
     27   ///
     28   /// @param UseDefFreq Expected number of executed use and def instructions
     29   ///                   per function call. Derived from block frequencies.
     30   /// @param Size       Size of live interval as returnexd by getSize()
     31   ///
     32   static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size) {
     33     // The constant 25 instructions is added to avoid depending too much on
     34     // accidental SlotIndex gaps for small intervals. The effect is that small
     35     // intervals have a spill weight that is mostly proportional to the number
     36     // of uses, while large intervals get a spill weight that is closer to a use
     37     // density.
     38     return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
     39   }
     40 
     41   /// VirtRegAuxInfo - Calculate auxiliary information for a virtual
     42   /// register such as its spill weight and allocation hint.
     43   class VirtRegAuxInfo {
     44     MachineFunction &MF;
     45     LiveIntervals &LIS;
     46     const MachineLoopInfo &Loops;
     47     const MachineBlockFrequencyInfo &MBFI;
     48     DenseMap<unsigned, float> Hint;
     49   public:
     50     VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
     51                    const MachineLoopInfo &loops,
     52                    const MachineBlockFrequencyInfo &mbfi)
     53         : MF(mf), LIS(lis), Loops(loops), MBFI(mbfi) {}
     54 
     55     /// CalculateWeightAndHint - (re)compute li's spill weight and allocation
     56     /// hint.
     57     void CalculateWeightAndHint(LiveInterval &li);
     58   };
     59 
     60   /// CalculateSpillWeights - Compute spill weights for all virtual register
     61   /// live intervals.
     62   class CalculateSpillWeights : public MachineFunctionPass {
     63   public:
     64     static char ID;
     65 
     66     CalculateSpillWeights() : MachineFunctionPass(ID) {
     67       initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
     68     }
     69 
     70     virtual void getAnalysisUsage(AnalysisUsage &au) const;
     71 
     72     virtual bool runOnMachineFunction(MachineFunction &fn);
     73 
     74   private:
     75     /// Returns true if the given live interval is zero length.
     76     bool isZeroLengthInterval(LiveInterval *li) const;
     77   };
     78 
     79 }
     80 
     81 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H
     82