CalcSpillWeights.h revision 218893
1200581Srdivacky//===---------------- lib/CodeGen/CalcSpillWeights.h ------------*- C++ -*-===// 2200581Srdivacky// 3200581Srdivacky// The LLVM Compiler Infrastructure 4200581Srdivacky// 5200581Srdivacky// This file is distributed under the University of Illinois Open Source 6200581Srdivacky// License. See LICENSE.TXT for details. 7200581Srdivacky// 8200581Srdivacky//===----------------------------------------------------------------------===// 9200581Srdivacky 10200581Srdivacky 11200581Srdivacky#ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H 12200581Srdivacky#define LLVM_CODEGEN_CALCSPILLWEIGHTS_H 13200581Srdivacky 14200581Srdivacky#include "llvm/CodeGen/MachineFunctionPass.h" 15212904Sdim#include "llvm/ADT/DenseMap.h" 16200581Srdivacky 17200581Srdivackynamespace llvm { 18200581Srdivacky 19200581Srdivacky class LiveInterval; 20212904Sdim class LiveIntervals; 21212904Sdim class MachineLoopInfo; 22200581Srdivacky 23218893Sdim /// normalizeSpillWeight - The spill weight of a live interval is computed as: 24218893Sdim /// 25218893Sdim /// (sum(use freq) + sum(def freq)) / (K + size) 26218893Sdim /// 27218893Sdim /// @param UseDefFreq Expected number of executed use and def instructions 28218893Sdim /// per function call. Derived from block frequencies. 29218893Sdim /// @param Size Size of live interval as returnexd by getSize() 30218893Sdim /// 31218893Sdim static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size) { 32218893Sdim // The magic constant 200 corresponds to approx. 25 instructions since 33218893Sdim // SlotIndexes allocate 8 slots per instruction. 34218893Sdim // 35218893Sdim // The constant is added to avoid depending too much on accidental SlotIndex 36218893Sdim // gaps for small intervals. The effect is that small intervals have a spill 37218893Sdim // weight that is mostly proportional to the number of uses, while large 38218893Sdim // intervals get a spill weight that is closer to a use density. 39218893Sdim // 40218893Sdim return UseDefFreq / (Size + 200); 41218893Sdim } 42218893Sdim 43212904Sdim /// VirtRegAuxInfo - Calculate auxiliary information for a virtual 44212904Sdim /// register such as its spill weight and allocation hint. 45212904Sdim class VirtRegAuxInfo { 46212904Sdim MachineFunction &mf_; 47212904Sdim LiveIntervals &lis_; 48212904Sdim const MachineLoopInfo &loops_; 49212904Sdim DenseMap<unsigned, float> hint_; 50212904Sdim public: 51212904Sdim VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis, 52212904Sdim const MachineLoopInfo &loops) : 53212904Sdim mf_(mf), lis_(lis), loops_(loops) {} 54212904Sdim 55212904Sdim /// CalculateRegClass - recompute the register class for reg from its uses. 56212904Sdim /// Since the register class can affect the allocation hint, this function 57212904Sdim /// should be called before CalculateWeightAndHint if both are called. 58212904Sdim void CalculateRegClass(unsigned reg); 59212904Sdim 60212904Sdim /// CalculateWeightAndHint - (re)compute li's spill weight and allocation 61212904Sdim /// hint. 62212904Sdim void CalculateWeightAndHint(LiveInterval &li); 63212904Sdim }; 64212904Sdim 65200581Srdivacky /// CalculateSpillWeights - Compute spill weights for all virtual register 66200581Srdivacky /// live intervals. 67200581Srdivacky class CalculateSpillWeights : public MachineFunctionPass { 68200581Srdivacky public: 69200581Srdivacky static char ID; 70200581Srdivacky 71218893Sdim CalculateSpillWeights() : MachineFunctionPass(ID) { 72218893Sdim initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); 73218893Sdim } 74200581Srdivacky 75200581Srdivacky virtual void getAnalysisUsage(AnalysisUsage &au) const; 76200581Srdivacky 77212904Sdim virtual bool runOnMachineFunction(MachineFunction &fn); 78200581Srdivacky 79200581Srdivacky private: 80200581Srdivacky /// Returns true if the given live interval is zero length. 81200581Srdivacky bool isZeroLengthInterval(LiveInterval *li) const; 82200581Srdivacky }; 83200581Srdivacky 84200581Srdivacky} 85200581Srdivacky 86200581Srdivacky#endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H 87