1 //===---------------- lib/CodeGen/CalcSpillWeights.h ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
12 #define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/CodeGen/SlotIndexes.h"
21 class MachineLoopInfo;
23 /// normalizeSpillWeight - The spill weight of a live interval is computed as:
25 /// (sum(use freq) + sum(def freq)) / (K + size)
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()
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
37 return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
40 /// VirtRegAuxInfo - Calculate auxiliary information for a virtual
41 /// register such as its spill weight and allocation hint.
42 class VirtRegAuxInfo {
45 const MachineLoopInfo &Loops;
46 DenseMap<unsigned, float> Hint;
48 VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
49 const MachineLoopInfo &loops) :
50 MF(mf), LIS(lis), Loops(loops) {}
52 /// CalculateWeightAndHint - (re)compute li's spill weight and allocation
54 void CalculateWeightAndHint(LiveInterval &li);
57 /// CalculateSpillWeights - Compute spill weights for all virtual register
59 class CalculateSpillWeights : public MachineFunctionPass {
63 CalculateSpillWeights() : MachineFunctionPass(ID) {
64 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
67 virtual void getAnalysisUsage(AnalysisUsage &au) const;
69 virtual bool runOnMachineFunction(MachineFunction &fn);
72 /// Returns true if the given live interval is zero length.
73 bool isZeroLengthInterval(LiveInterval *li) const;
78 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H