| 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 | |
| 12 | #include "llvm/CodeGen/SlotIndexes.h" |
| 13 | |
| 14 | namespace llvm { |
| 15 | |
| 16 | class LiveInterval; |
| 17 | class LiveIntervals; |
| 18 | class MachineBlockFrequencyInfo; |
| 19 | class MachineFunction; |
| 20 | class MachineLoopInfo; |
| 21 | class ProfileSummaryInfo; |
| 22 | class 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. |
| 46 | class VirtRegAuxInfo { |
| 47 | MachineFunction &MF; |
| 48 | LiveIntervals &LIS; |
| 49 | const VirtRegMap &VRM; |
| 50 | const MachineLoopInfo &Loops; |
| 51 | ProfileSummaryInfo *PSI; |
| 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: |
| 59 | VirtRegAuxInfo(MachineFunction &MF, LiveIntervals &LIS, |
| 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. |
| 68 | void calculateSpillWeightAndHint(LiveInterval &LI); |
| 69 | |
| 70 | /// Compute spill weights and allocation hints for all virtual register |
| 71 | /// live intervals. |
| 72 | void calculateSpillWeightsAndHints(); |
| 73 | |
| 74 | /// Return the preferred allocation register for reg, given a COPY |
| 75 | /// instruction. |
| 76 | static Register copyHint(const MachineInstr *MI, Register Reg, |
| 77 | const TargetRegisterInfo &TRI, |
| 78 | const MachineRegisterInfo &MRI); |
| 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 |
| 111 | |