Symbolize the default instruction distance.
[oota-llvm.git] / include / llvm / CodeGen / CalcSpillWeights.h
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
14 #include "llvm/CodeGen/SlotIndexes.h"
15 #include "llvm/ADT/DenseMap.h"
16
17 namespace llvm {
18
19   class LiveInterval;
20   class LiveIntervals;
21   class MachineLoopInfo;
22
23   /// normalizeSpillWeight - The spill weight of a live interval is computed as:
24   ///
25   ///   (sum(use freq) + sum(def freq)) / (K + size)
26   ///
27   /// @param UseDefFreq Expected number of executed use and def instructions
28   ///                   per function call. Derived from block frequencies.
29   /// @param Size       Size of live interval as returnexd by getSize()
30   ///
31   static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size) {
32     // The constant 25 instructions is added to avoid depending too much on
33     // accidental SlotIndex gaps for small intervals. The effect is that small
34     // intervals have a spill weight that is mostly proportional to the number
35     // of uses, while large intervals get a spill weight that is closer to a use
36     // density.
37     return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
38   }
39
40   /// VirtRegAuxInfo - Calculate auxiliary information for a virtual
41   /// register such as its spill weight and allocation hint.
42   class VirtRegAuxInfo {
43     MachineFunction &mf_;
44     LiveIntervals &lis_;
45     const MachineLoopInfo &loops_;
46     DenseMap<unsigned, float> hint_;
47   public:
48     VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
49                    const MachineLoopInfo &loops) :
50       mf_(mf), lis_(lis), loops_(loops) {}
51
52     /// CalculateRegClass - recompute the register class for reg from its uses.
53     /// Since the register class can affect the allocation hint, this function
54     /// should be called before CalculateWeightAndHint if both are called.
55     void CalculateRegClass(unsigned reg);
56
57     /// CalculateWeightAndHint - (re)compute li's spill weight and allocation
58     /// hint.
59     void CalculateWeightAndHint(LiveInterval &li);
60   };
61
62   /// CalculateSpillWeights - Compute spill weights for all virtual register
63   /// live intervals.
64   class CalculateSpillWeights : public MachineFunctionPass {
65   public:
66     static char ID;
67
68     CalculateSpillWeights() : MachineFunctionPass(ID) {
69       initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
70     }
71
72     virtual void getAnalysisUsage(AnalysisUsage &au) const;
73
74     virtual bool runOnMachineFunction(MachineFunction &fn);
75
76   private:
77     /// Returns true if the given live interval is zero length.
78     bool isZeroLengthInterval(LiveInterval *li) const;
79   };
80
81 }
82
83 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H