LLVM API Documentation

CalcSpillWeights.h
Go to the documentation of this file.
00001 //===---------------- lib/CodeGen/CalcSpillWeights.h ------------*- 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 
00011 #ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
00012 #define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
00013 
00014 #include "llvm/ADT/DenseMap.h"
00015 #include "llvm/CodeGen/SlotIndexes.h"
00016 
00017 namespace llvm {
00018 
00019   class LiveInterval;
00020   class LiveIntervals;
00021   class MachineBlockFrequencyInfo;
00022   class MachineLoopInfo;
00023 
00024   /// \brief Normalize the spill weight of a live interval
00025   ///
00026   /// The spill weight of a live interval is computed as:
00027   ///
00028   ///   (sum(use freq) + sum(def freq)) / (K + size)
00029   ///
00030   /// @param UseDefFreq Expected number of executed use and def instructions
00031   ///                   per function call. Derived from block frequencies.
00032   /// @param Size       Size of live interval as returnexd by getSize()
00033   ///
00034   static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size) {
00035     // The constant 25 instructions is added to avoid depending too much on
00036     // accidental SlotIndex gaps for small intervals. The effect is that small
00037     // intervals have a spill weight that is mostly proportional to the number
00038     // of uses, while large intervals get a spill weight that is closer to a use
00039     // density.
00040     return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
00041   }
00042 
00043   /// \brief Calculate auxiliary information for a virtual register such as its
00044   /// spill weight and allocation hint.
00045   class VirtRegAuxInfo {
00046   public:
00047     typedef float (*NormalizingFn)(float, unsigned);
00048 
00049   private:
00050     MachineFunction &MF;
00051     LiveIntervals &LIS;
00052     const MachineLoopInfo &Loops;
00053     const MachineBlockFrequencyInfo &MBFI;
00054     DenseMap<unsigned, float> Hint;
00055     NormalizingFn normalize;
00056 
00057   public:
00058     VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
00059                    const MachineLoopInfo &loops,
00060                    const MachineBlockFrequencyInfo &mbfi,
00061                    NormalizingFn norm = normalizeSpillWeight)
00062         : MF(mf), LIS(lis), Loops(loops), MBFI(mbfi), normalize(norm) {}
00063 
00064     /// \brief (re)compute li's spill weight and allocation hint.
00065     void calculateSpillWeightAndHint(LiveInterval &li);
00066   };
00067 
00068   /// \brief Compute spill weights and allocation hints for all virtual register
00069   /// live intervals.
00070   void calculateSpillWeightsAndHints(LiveIntervals &LIS, MachineFunction &MF,
00071                                      const MachineLoopInfo &MLI,
00072                                      const MachineBlockFrequencyInfo &MBFI,
00073                                      VirtRegAuxInfo::NormalizingFn norm =
00074                                          normalizeSpillWeight);
00075 }
00076 
00077 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H