]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
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 | ||
223e47cc | 14 | #include "llvm/ADT/DenseMap.h" |
970d7e83 | 15 | #include "llvm/CodeGen/SlotIndexes.h" |
223e47cc LB |
16 | |
17 | namespace llvm { | |
18 | ||
19 | class LiveInterval; | |
20 | class LiveIntervals; | |
1a4d82fc | 21 | class MachineBlockFrequencyInfo; |
223e47cc LB |
22 | class MachineLoopInfo; |
23 | ||
1a4d82fc JJ |
24 | /// \brief Normalize the spill weight of a live interval |
25 | /// | |
26 | /// The spill weight of a live interval is computed as: | |
223e47cc LB |
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() | |
85aaf69f | 33 | /// @param NumInstr Number of instructions using this live interval |
223e47cc | 34 | /// |
85aaf69f SL |
35 | static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size, |
36 | unsigned NumInstr) { | |
223e47cc LB |
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 | ||
1a4d82fc JJ |
45 | /// \brief Calculate auxiliary information for a virtual register such as its |
46 | /// spill weight and allocation hint. | |
223e47cc | 47 | class VirtRegAuxInfo { |
1a4d82fc | 48 | public: |
85aaf69f | 49 | typedef float (*NormalizingFn)(float, unsigned, unsigned); |
1a4d82fc JJ |
50 | |
51 | private: | |
223e47cc LB |
52 | MachineFunction &MF; |
53 | LiveIntervals &LIS; | |
54 | const MachineLoopInfo &Loops; | |
1a4d82fc | 55 | const MachineBlockFrequencyInfo &MBFI; |
223e47cc | 56 | DenseMap<unsigned, float> Hint; |
1a4d82fc JJ |
57 | NormalizingFn normalize; |
58 | ||
223e47cc LB |
59 | public: |
60 | VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis, | |
1a4d82fc JJ |
61 | const MachineLoopInfo &loops, |
62 | const MachineBlockFrequencyInfo &mbfi, | |
63 | NormalizingFn norm = normalizeSpillWeight) | |
64 | : MF(mf), LIS(lis), Loops(loops), MBFI(mbfi), normalize(norm) {} | |
223e47cc | 65 | |
1a4d82fc JJ |
66 | /// \brief (re)compute li's spill weight and allocation hint. |
67 | void calculateSpillWeightAndHint(LiveInterval &li); | |
223e47cc LB |
68 | }; |
69 | ||
1a4d82fc | 70 | /// \brief Compute spill weights and allocation hints for all virtual register |
223e47cc | 71 | /// live intervals. |
1a4d82fc JJ |
72 | void calculateSpillWeightsAndHints(LiveIntervals &LIS, MachineFunction &MF, |
73 | const MachineLoopInfo &MLI, | |
74 | const MachineBlockFrequencyInfo &MBFI, | |
75 | VirtRegAuxInfo::NormalizingFn norm = | |
76 | normalizeSpillWeight); | |
223e47cc LB |
77 | } |
78 | ||
79 | #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H |