LLVM API Documentation

CalcSpillWeights.h
Go to the documentation of this file.
00001 //===---------------- lib/CodeGen/CalcSpillWeights.h ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 
00011 #ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
00012 #define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
00013 
00014 #include "llvm/ADT/DenseMap.h"
00015 #include "llvm/CodeGen/SlotIndexes.h"
00016 
00017 namespace llvm {
00018 
00019   class LiveInterval;
00020   class LiveIntervals;
00021   class MachineLoopInfo;
00022 
00023   /// normalizeSpillWeight - The spill weight of a live interval is computed as:
00024   ///
00025   ///   (sum(use freq) + sum(def freq)) / (K + size)
00026   ///
00027   /// @param UseDefFreq Expected number of executed use and def instructions
00028   ///                   per function call. Derived from block frequencies.
00029   /// @param Size       Size of live interval as returnexd by getSize()
00030   ///
00031   static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size) {
00032     // The constant 25 instructions is added to avoid depending too much on
00033     // accidental SlotIndex gaps for small intervals. The effect is that small
00034     // intervals have a spill weight that is mostly proportional to the number
00035     // of uses, while large intervals get a spill weight that is closer to a use
00036     // density.
00037     return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
00038   }
00039 
00040   /// VirtRegAuxInfo - Calculate auxiliary information for a virtual
00041   /// register such as its spill weight and allocation hint.
00042   class VirtRegAuxInfo {
00043     MachineFunction &MF;
00044     LiveIntervals &LIS;
00045     const MachineLoopInfo &Loops;
00046     DenseMap<unsigned, float> Hint;
00047   public:
00048     VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
00049                    const MachineLoopInfo &loops) :
00050       MF(mf), LIS(lis), Loops(loops) {}
00051 
00052     /// CalculateWeightAndHint - (re)compute li's spill weight and allocation
00053     /// hint.
00054     void CalculateWeightAndHint(LiveInterval &li);
00055   };
00056 
00057   /// CalculateSpillWeights - Compute spill weights for all virtual register
00058   /// live intervals.
00059   class CalculateSpillWeights : public MachineFunctionPass {
00060   public:
00061     static char ID;
00062 
00063     CalculateSpillWeights() : MachineFunctionPass(ID) {
00064       initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
00065     }
00066 
00067     virtual void getAnalysisUsage(AnalysisUsage &au) const;
00068 
00069     virtual bool runOnMachineFunction(MachineFunction &fn);
00070 
00071   private:
00072     /// Returns true if the given live interval is zero length.
00073     bool isZeroLengthInterval(LiveInterval *li) const;
00074   };
00075 
00076 }
00077 
00078 #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H