1 //===---------------- lib/CodeGen/CalcSpillWeights.h ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
12 #define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/CodeGen/SlotIndexes.h"
21 class MachineBlockFrequencyInfo;
22 class MachineLoopInfo;
25 /// \brief Normalize the spill weight of a live interval
27 /// The spill weight of a live interval is computed as:
29 /// (sum(use freq) + sum(def freq)) / (K + size)
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
36 static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size,
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
43 return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
46 /// \brief Calculate auxiliary information for a virtual register such as its
47 /// spill weight and allocation hint.
48 class VirtRegAuxInfo {
50 typedef float (*NormalizingFn)(float, unsigned, unsigned);
56 const MachineLoopInfo &Loops;
57 const MachineBlockFrequencyInfo &MBFI;
58 DenseMap<unsigned, float> Hint;
59 NormalizingFn normalize;
62 VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
63 VirtRegMap *vrm, const MachineLoopInfo &loops,
64 const MachineBlockFrequencyInfo &mbfi,
65 NormalizingFn norm = normalizeSpillWeight)
66 : MF(mf), LIS(lis), VRM(vrm), Loops(loops), MBFI(mbfi), normalize(norm) {}
68 /// \brief (re)compute li's spill weight and allocation hint.
69 void calculateSpillWeightAndHint(LiveInterval &li);
72 /// \brief Compute spill weights and allocation hints for all virtual register
74 void calculateSpillWeightsAndHints(LiveIntervals &LIS, MachineFunction &MF,
76 const MachineLoopInfo &MLI,
77 const MachineBlockFrequencyInfo &MBFI,
78 VirtRegAuxInfo::NormalizingFn norm =
79 normalizeSpillWeight);
82 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H