LLVM 20.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
14namespace llvm {
15
16class LiveInterval;
17class LiveIntervals;
18class MachineBlockFrequencyInfo;
19class MachineFunction;
20class MachineLoopInfo;
21class ProfileSummaryInfo;
22class VirtRegMap;
23
24 /// Normalize the spill weight of a live interval
25 ///
26 /// The spill weight of a live interval is computed as:
27 ///
28 /// (sum(use freq) + sum(def freq)) / (K + size)
29 ///
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()
33 /// @param NumInstr Number of instructions using this live interval
34 static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size,
35 unsigned NumInstr) {
36 // The constant 25 instructions is added to avoid depending too much on
37 // accidental SlotIndex gaps for small intervals. The effect is that small
38 // intervals have a spill weight that is mostly proportional to the number
39 // of uses, while large intervals get a spill weight that is closer to a use
40 // density.
41 return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
42 }
43
44 /// Calculate auxiliary information for a virtual register such as its
45 /// spill weight and allocation hint.
48 LiveIntervals &LIS;
49 const VirtRegMap &VRM;
50 const MachineLoopInfo &Loops;
52 const MachineBlockFrequencyInfo &MBFI;
53
54 /// Returns true if Reg of live interval LI is used in instruction with many
55 /// operands like STATEPOINT.
56 bool isLiveAtStatepointVarArg(LiveInterval &LI);
57
58 public:
60 const VirtRegMap &VRM, const MachineLoopInfo &Loops,
61 const MachineBlockFrequencyInfo &MBFI,
62 ProfileSummaryInfo *PSI = nullptr)
63 : MF(MF), LIS(LIS), VRM(VRM), Loops(Loops), PSI(PSI), MBFI(MBFI) {}
64
65 virtual ~VirtRegAuxInfo() = default;
66
67 /// (re)compute li's spill weight and allocation hint.
69
70 /// Compute spill weights and allocation hints for all virtual register
71 /// live intervals.
73
74 /// Return the preferred allocation register for reg, given a COPY
75 /// instruction.
76 static Register copyHint(const MachineInstr *MI, unsigned Reg,
79
80 /// Determine if all values in LI are rematerializable.
81 static bool isRematerializable(const LiveInterval &LI,
82 const LiveIntervals &LIS,
83 const VirtRegMap &VRM,
84 const TargetInstrInfo &TII);
85
86 protected:
87 /// Helper function for weight calculations.
88 /// (Re)compute LI's spill weight and allocation hint, or, for non null
89 /// start and end - compute future expected spill weight of a split
90 /// artifact of LI that will span between start and end slot indexes.
91 /// \param LI The live interval for which to compute the weight.
92 /// \param Start The expected beginning of the split artifact. Instructions
93 /// before start will not affect the weight. Relevant for
94 /// weight calculation of future split artifact.
95 /// \param End The expected end of the split artifact. Instructions
96 /// after end will not affect the weight. Relevant for
97 /// weight calculation of future split artifact.
98 /// \return The spill weight. Returns negative weight for unspillable LI.
99 float weightCalcHelper(LiveInterval &LI, SlotIndex *Start = nullptr,
100 SlotIndex *End = nullptr);
101
102 /// Weight normalization function.
103 virtual float normalize(float UseDefFreq, unsigned Size,
104 unsigned NumInstr) {
105 return normalizeSpillWeight(UseDefFreq, Size, NumInstr);
106 }
107 };
108} // end namespace llvm
109
110#endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H
unsigned const MachineRegisterInfo * MRI
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
const HexagonInstrInfo * TII
Hexagon Hardware Loops
IRTranslator LLVM IR MI
unsigned const TargetRegisterInfo * TRI
unsigned Reg
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:687
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Analysis providing profile information.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:65
@ InstrDist
The default distance between instructions as returned by distance().
Definition: SlotIndexes.h:112
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Calculate auxiliary information for a virtual register such as its spill weight and allocation hint.
static Register copyHint(const MachineInstr *MI, unsigned Reg, const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI)
Return the preferred allocation register for reg, given a COPY instruction.
float weightCalcHelper(LiveInterval &LI, SlotIndex *Start=nullptr, SlotIndex *End=nullptr)
Helper function for weight calculations.
virtual ~VirtRegAuxInfo()=default
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)
virtual float normalize(float UseDefFreq, unsigned Size, unsigned NumInstr)
Weight normalization function.
static bool isRematerializable(const LiveInterval &LI, const LiveIntervals &LIS, const VirtRegMap &VRM, const TargetInstrInfo &TII)
Determine if all values in LI are rematerializable.
void calculateSpillWeightAndHint(LiveInterval &LI)
(re)compute li's spill weight and allocation hint.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static float normalizeSpillWeight(float UseDefFreq, unsigned Size, unsigned NumInstr)
Normalize the spill weight of a live interval.