LLVM  4.0.0
CalcSpillWeights.h
Go to the documentation of this file.
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"
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  ///
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.
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;
59  NormalizingFn normalize;
60 
61  public:
63  VirtRegMap *vrm, const MachineLoopInfo &loops,
64  const MachineBlockFrequencyInfo &mbfi,
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.
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,
80 }
81 
82 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H
Calculate auxiliary information for a virtual register such as its spill weight and allocation hint...
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:625
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
float(* NormalizingFn)(float, unsigned, unsigned)
VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis, VirtRegMap *vrm, const MachineLoopInfo &loops, const MachineBlockFrequencyInfo &mbfi, NormalizingFn norm=normalizeSpillWeight)
The default distance between instructions as returned by distance().
Definition: SlotIndexes.h:130
void calculateSpillWeightsAndHints(LiveIntervals &LIS, MachineFunction &MF, VirtRegMap *VRM, const MachineLoopInfo &MLI, const MachineBlockFrequencyInfo &MBFI, VirtRegAuxInfo::NormalizingFn norm=normalizeSpillWeight)
Compute spill weights and allocation hints for all virtual register live intervals.
void calculateSpillWeightAndHint(LiveInterval &li)
(re)compute li's spill weight and allocation hint.
static float normalizeSpillWeight(float UseDefFreq, unsigned Size, unsigned NumInstr)
Normalize the spill weight of a live interval.
loops
Definition: LoopInfo.cpp:709