1f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===- lib/CodeGen/CalcSpillWeights.h ---------------------------*- C++ -*-===//
2f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//
3f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//                     The LLVM Compiler Infrastructure
4f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//
5f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// This file is distributed under the University of Illinois Open Source
6f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot// License. See LICENSE.TXT for details.
7f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//
8f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot//===----------------------------------------------------------------------===//
9f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
10f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
11f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
12f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
13f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/ADT/DenseMap.h"
14f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#include "llvm/CodeGen/SlotIndexes.h"
15f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
16f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotnamespace llvm {
17f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
18f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LiveInterval;
19f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass LiveIntervals;
20f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass MachineBlockFrequencyInfo;
21f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass MachineFunction;
22f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass MachineLoopInfo;
23f3014761c955345d6e05491608e73228d014afbandroid-build-team Robotclass VirtRegMap;
24f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
25f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// \brief Normalize the spill weight of a live interval
26f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  ///
27f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// The spill weight of a live interval is computed as:
28f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  ///
29f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  ///   (sum(use freq) + sum(def freq)) / (K + size)
30f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  ///
31f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// @param UseDefFreq Expected number of executed use and def instructions
32f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  ///                   per function call. Derived from block frequencies.
33f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// @param Size       Size of live interval as returnexd by getSize()
34f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// @param NumInstr   Number of instructions using this live interval
35f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size,
36f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                                           unsigned NumInstr) {
37f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    // The constant 25 instructions is added to avoid depending too much on
38f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    // accidental SlotIndex gaps for small intervals. The effect is that small
39f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    // intervals have a spill weight that is mostly proportional to the number
40f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    // of uses, while large intervals get a spill weight that is closer to a use
41f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    // density.
42f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
43f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  }
44f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
45f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// \brief Calculate auxiliary information for a virtual register such as its
46f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// spill weight and allocation hint.
47f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  class VirtRegAuxInfo {
48f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  public:
49f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    using NormalizingFn = float (*)(float, unsigned, unsigned);
50f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
51f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  private:
52f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    MachineFunction &MF;
53f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    LiveIntervals &LIS;
54f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    VirtRegMap *VRM;
55f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    const MachineLoopInfo &Loops;
56f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    const MachineBlockFrequencyInfo &MBFI;
57f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    DenseMap<unsigned, float> Hint;
58f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    NormalizingFn normalize;
59f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
60f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  public:
61f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
62f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                   VirtRegMap *vrm, const MachineLoopInfo &loops,
63f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                   const MachineBlockFrequencyInfo &mbfi,
64f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                   NormalizingFn norm = normalizeSpillWeight)
65f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot        : MF(mf), LIS(lis), VRM(vrm), Loops(loops), MBFI(mbfi), normalize(norm) {}
66f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
67f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    /// \brief (re)compute li's spill weight and allocation hint.
68f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot    void calculateSpillWeightAndHint(LiveInterval &li);
69f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  };
70f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
71f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// \brief Compute spill weights and allocation hints for all virtual register
72f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  /// live intervals.
73f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot  void calculateSpillWeightsAndHints(LiveIntervals &LIS, MachineFunction &MF,
74f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                                     VirtRegMap *VRM,
75f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                                     const MachineLoopInfo &MLI,
76f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                                     const MachineBlockFrequencyInfo &MBFI,
77f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                                     VirtRegAuxInfo::NormalizingFn norm =
78f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot                                         normalizeSpillWeight);
79f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
80f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot} // end namespace llvm
81f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot
82f3014761c955345d6e05491608e73228d014afbandroid-build-team Robot#endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H
83