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