LLVM  8.0.1
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 #ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
11 #define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
12 
13 #include "llvm/ADT/DenseMap.h"
15 
16 namespace llvm {
17 
18 class LiveInterval;
19 class LiveIntervals;
20 class MachineBlockFrequencyInfo;
21 class MachineFunction;
22 class MachineLoopInfo;
23 class VirtRegMap;
24 
25  /// 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  static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size,
36  unsigned NumInstr) {
37  // The constant 25 instructions is added to avoid depending too much on
38  // accidental SlotIndex gaps for small intervals. The effect is that small
39  // intervals have a spill weight that is mostly proportional to the number
40  // of uses, while large intervals get a spill weight that is closer to a use
41  // density.
42  return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
43  }
44 
45  /// Calculate auxiliary information for a virtual register such as its
46  /// spill weight and allocation hint.
48  public:
49  using NormalizingFn = float (*)(float, unsigned, unsigned);
50 
51  private:
52  MachineFunction &MF;
53  LiveIntervals &LIS;
54  VirtRegMap *VRM;
55  const MachineLoopInfo &Loops;
56  const MachineBlockFrequencyInfo &MBFI;
58  NormalizingFn normalize;
59 
60  public:
62  VirtRegMap *vrm, const MachineLoopInfo &loops,
63  const MachineBlockFrequencyInfo &mbfi,
65  : MF(mf), LIS(lis), VRM(vrm), Loops(loops), MBFI(mbfi), normalize(norm) {}
66 
67  /// (re)compute li's spill weight and allocation hint.
69 
70  /// Compute future expected spill weight of a split artifact of li
71  /// that will span between start and end slot indexes.
72  /// \param li The live interval to be split.
73  /// \param start The expected begining of the split artifact. Instructions
74  /// before start will not affect the weight.
75  /// \param end The expected end of the split artifact. Instructions
76  /// after end will not affect the weight.
77  /// \return The expected spill weight of the split artifact. Returns
78  /// negative weight for unspillable li.
79  float futureWeight(LiveInterval &li, SlotIndex start, SlotIndex end);
80 
81  /// Helper function for weight calculations.
82  /// (Re)compute li's spill weight and allocation hint, or, for non null
83  /// start and end - compute future expected spill weight of a split
84  /// artifact of li that will span between start and end slot indexes.
85  /// \param li The live interval for which to compute the weight.
86  /// \param start The expected begining of the split artifact. Instructions
87  /// before start will not affect the weight. Relevant for
88  /// weight calculation of future split artifact.
89  /// \param end The expected end of the split artifact. Instructions
90  /// after end will not affect the weight. Relevant for
91  /// weight calculation of future split artifact.
92  /// \return The spill weight. Returns negative weight for unspillable li.
93  float weightCalcHelper(LiveInterval &li, SlotIndex *start = nullptr,
94  SlotIndex *end = nullptr);
95  };
96 
97  /// Compute spill weights and allocation hints for all virtual register
98  /// live intervals.
100  VirtRegMap *VRM,
101  const MachineLoopInfo &MLI,
102  const MachineBlockFrequencyInfo &MBFI,
105 
106 } // end namespace llvm
107 
108 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:259
Calculate auxiliary information for a virtual register such as its spill weight and allocation hint...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:638
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
float futureWeight(LiveInterval &li, SlotIndex start, SlotIndex end)
Compute future expected spill weight of a split artifact of li that will span between start and end s...
VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis, VirtRegMap *vrm, const MachineLoopInfo &loops, const MachineBlockFrequencyInfo &mbfi, NormalizingFn norm=normalizeSpillWeight)
float weightCalcHelper(LiveInterval &li, SlotIndex *start=nullptr, SlotIndex *end=nullptr)
Helper function for weight calculations.
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.
uint32_t Size
Definition: Profile.cpp:47
static float normalizeSpillWeight(float UseDefFreq, unsigned Size, unsigned NumInstr)
Normalize the spill weight of a live interval.
The default distance between instructions as returned by distance().
Definition: SlotIndexes.h:138
float(*)(float, unsigned, unsigned) NormalizingFn
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:84
loops
Definition: LoopInfo.cpp:772