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 class VirtRegMap; 24 25 /// \brief Normalize the spill weight of a live interval 26 /// 27 /// The spill weight of a live interval is computed as: 28 /// 29 /// (sum(use freq) + sum(def freq)) / (K + size) 30 /// 31 /// @param UseDefFreq Expected number of executed use and def instructions 32 /// per function call. Derived from block frequencies. 33 /// @param Size Size of live interval as returnexd by getSize() 34 /// @param NumInstr Number of instructions using this live interval 35 /// normalizeSpillWeight(float UseDefFreq,unsigned Size,unsigned NumInstr)36 static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size, 37 unsigned NumInstr) { 38 // The constant 25 instructions is added to avoid depending too much on 39 // accidental SlotIndex gaps for small intervals. The effect is that small 40 // intervals have a spill weight that is mostly proportional to the number 41 // of uses, while large intervals get a spill weight that is closer to a use 42 // density. 43 return UseDefFreq / (Size + 25*SlotIndex::InstrDist); 44 } 45 46 /// \brief Calculate auxiliary information for a virtual register such as its 47 /// spill weight and allocation hint. 48 class VirtRegAuxInfo { 49 public: 50 typedef float (*NormalizingFn)(float, unsigned, unsigned); 51 52 private: 53 MachineFunction &MF; 54 LiveIntervals &LIS; 55 VirtRegMap *VRM; 56 const MachineLoopInfo &Loops; 57 const MachineBlockFrequencyInfo &MBFI; 58 DenseMap<unsigned, float> Hint; 59 NormalizingFn normalize; 60 61 public: 62 VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis, 63 VirtRegMap *vrm, const MachineLoopInfo &loops, 64 const MachineBlockFrequencyInfo &mbfi, 65 NormalizingFn norm = normalizeSpillWeight) MF(mf)66 : MF(mf), LIS(lis), VRM(vrm), Loops(loops), MBFI(mbfi), normalize(norm) {} 67 68 /// \brief (re)compute li's spill weight and allocation hint. 69 void calculateSpillWeightAndHint(LiveInterval &li); 70 }; 71 72 /// \brief Compute spill weights and allocation hints for all virtual register 73 /// live intervals. 74 void calculateSpillWeightsAndHints(LiveIntervals &LIS, MachineFunction &MF, 75 VirtRegMap *VRM, 76 const MachineLoopInfo &MLI, 77 const MachineBlockFrequencyInfo &MBFI, 78 VirtRegAuxInfo::NormalizingFn norm = 79 normalizeSpillWeight); 80 } 81 82 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H 83