LLVM 24.0.0git
CalcSpillWeights.h
Go to the documentation of this file.
1//===- lib/CodeGen/CalcSpillWeights.h ---------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
10#define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
11
13#include <optional>
14
15namespace llvm {
16
17class LiveInterval;
18class LiveIntervals;
20class MachineFunction;
21class MachineLoopInfo;
23class 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.
49 LiveIntervals &LIS;
50 const VirtRegMap &VRM;
51 const MachineLoopInfo &Loops;
53 const MachineBlockFrequencyInfo &MBFI;
54
55 /// Memoized llvm::shouldOptimizeForSize(&MF, PSI, &MBFI).
56 std::optional<bool> CachedOptForSize;
57
58 /// Lazily computes and caches the above.
59 bool getCachedOptimizeForSize();
60
61 /// Returns true if Reg of live interval LI is used in instruction with many
62 /// operands like STATEPOINT.
63 bool isLiveAtStatepointVarArg(LiveInterval &LI);
64
65 public:
67 const VirtRegMap &VRM, const MachineLoopInfo &Loops,
68 const MachineBlockFrequencyInfo &MBFI,
69 ProfileSummaryInfo *PSI = nullptr)
70 : MF(MF), LIS(LIS), VRM(VRM), Loops(Loops), PSI(PSI), MBFI(MBFI) {}
71
72 virtual ~VirtRegAuxInfo() = default;
73
74 /// (re)compute li's spill weight and allocation hint.
76
77 /// Compute spill weights and allocation hints for all virtual register
78 /// live intervals.
80
81 /// Return the preferred allocation register for reg, given a COPY
82 /// instruction.
85 const MachineRegisterInfo &MRI);
86
87 /// Determine if all values in LI are rematerializable.
88 LLVM_ABI static bool isRematerializable(const LiveInterval &LI,
89 const LiveIntervals &LIS,
90 const VirtRegMap &VRM,
91 const MachineRegisterInfo &MRI,
92 const TargetInstrInfo &TII);
93
94 /// \returns true if all registers used by \p MI are also available with the
95 /// same value at \p UseIdx.
96 LLVM_ABI static bool allUsesAvailableAt(const MachineInstr *MI,
97 SlotIndex UseIdx,
98 const LiveIntervals &LIS,
99 const MachineRegisterInfo &MRI,
100 const TargetInstrInfo &TII);
101
102 protected:
103 /// Helper function for weight calculations.
104 /// (Re)compute LI's spill weight and allocation hint, or, for non null
105 /// start and end - compute future expected spill weight of a split
106 /// artifact of LI that will span between start and end slot indexes.
107 /// \param LI The live interval for which to compute the weight.
108 /// \return The spill weight. Returns negative weight for unspillable LI.
110
111 /// Weight normalization function.
112 virtual float normalize(float UseDefFreq, unsigned Size,
113 unsigned NumInstr) {
114 return normalizeSpillWeight(UseDefFreq, Size, NumInstr);
115 }
116 };
117} // end namespace llvm
118
119#endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H
#define LLVM_ABI
Definition Compiler.h:215
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Register Reg
Register const TargetRegisterInfo * TRI
LiveInterval - This class represents the liveness of a register, or stack slot.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Analysis providing profile information.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
@ InstrDist
The default distance between instructions as returned by distance().
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM_ABI float weightCalcHelper(LiveInterval &LI)
Helper function for weight calculations.
static LLVM_ABI bool allUsesAvailableAt(const MachineInstr *MI, SlotIndex UseIdx, const LiveIntervals &LIS, const MachineRegisterInfo &MRI, const TargetInstrInfo &TII)
virtual ~VirtRegAuxInfo()=default
LLVM_ABI void calculateSpillWeightsAndHints()
Compute spill weights and allocation hints for all virtual register live intervals.
VirtRegAuxInfo(MachineFunction &MF, LiveIntervals &LIS, const VirtRegMap &VRM, const MachineLoopInfo &Loops, const MachineBlockFrequencyInfo &MBFI, ProfileSummaryInfo *PSI=nullptr)
static LLVM_ABI bool isRematerializable(const LiveInterval &LI, const LiveIntervals &LIS, const VirtRegMap &VRM, const MachineRegisterInfo &MRI, const TargetInstrInfo &TII)
Determine if all values in LI are rematerializable.
virtual float normalize(float UseDefFreq, unsigned Size, unsigned NumInstr)
Weight normalization function.
static LLVM_ABI Register copyHint(const MachineInstr *MI, Register Reg, const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI)
Return the preferred allocation register for reg, given a COPY instruction.
LLVM_ABI void calculateSpillWeightAndHint(LiveInterval &LI)
(re)compute li's spill weight and allocation hint.
This is an optimization pass for GlobalISel generic memory operations.
static float normalizeSpillWeight(float UseDefFreq, unsigned Size, unsigned NumInstr)
Normalize the spill weight of a live interval.