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/CodeGen/MachineFunctionPass.h"
15 #include "llvm/ADT/DenseMap.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 magic constant 200 corresponds to approx. 25 instructions since
33 // SlotIndexes allocate 8 slots per instruction.
35 // The constant is added to avoid depending too much on accidental SlotIndex
36 // gaps for small intervals. The effect is that small intervals have a spill
37 // weight that is mostly proportional to the number of uses, while large
38 // intervals get a spill weight that is closer to a use density.
40 return UseDefFreq / (Size + 200);
43 /// VirtRegAuxInfo - Calculate auxiliary information for a virtual
44 /// register such as its spill weight and allocation hint.
45 class VirtRegAuxInfo {
48 const MachineLoopInfo &loops_;
49 DenseMap<unsigned, float> hint_;
51 VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
52 const MachineLoopInfo &loops) :
53 mf_(mf), lis_(lis), loops_(loops) {}
55 /// CalculateRegClass - recompute the register class for reg from its uses.
56 /// Since the register class can affect the allocation hint, this function
57 /// should be called before CalculateWeightAndHint if both are called.
58 void CalculateRegClass(unsigned reg);
60 /// CalculateWeightAndHint - (re)compute li's spill weight and allocation
62 void CalculateWeightAndHint(LiveInterval &li);
65 /// CalculateSpillWeights - Compute spill weights for all virtual register
67 class CalculateSpillWeights : public MachineFunctionPass {
71 CalculateSpillWeights() : MachineFunctionPass(ID) {
72 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
75 virtual void getAnalysisUsage(AnalysisUsage &au) const;
77 virtual bool runOnMachineFunction(MachineFunction &fn);
80 /// Returns true if the given live interval is zero length.
81 bool isZeroLengthInterval(LiveInterval *li) const;
86 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H