LLVM API Documentation

CalcSpillWeights.cpp
Go to the documentation of this file.
00001 //===------------------------ CalcSpillWeights.cpp ------------------------===//
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 #include "llvm/CodeGen/CalcSpillWeights.h"
00011 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
00012 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
00013 #include "llvm/CodeGen/MachineFunction.h"
00014 #include "llvm/CodeGen/MachineLoopInfo.h"
00015 #include "llvm/CodeGen/MachineRegisterInfo.h"
00016 #include "llvm/Support/Debug.h"
00017 #include "llvm/Support/raw_ostream.h"
00018 #include "llvm/Target/TargetInstrInfo.h"
00019 #include "llvm/Target/TargetMachine.h"
00020 #include "llvm/Target/TargetRegisterInfo.h"
00021 #include "llvm/Target/TargetSubtargetInfo.h"
00022 using namespace llvm;
00023 
00024 #define DEBUG_TYPE "calcspillweights"
00025 
00026 void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
00027                            MachineFunction &MF,
00028                            const MachineLoopInfo &MLI,
00029                            const MachineBlockFrequencyInfo &MBFI,
00030                            VirtRegAuxInfo::NormalizingFn norm) {
00031   DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
00032                << "********** Function: " << MF.getName() << '\n');
00033 
00034   MachineRegisterInfo &MRI = MF.getRegInfo();
00035   VirtRegAuxInfo VRAI(MF, LIS, MLI, MBFI, norm);
00036   for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
00037     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
00038     if (MRI.reg_nodbg_empty(Reg))
00039       continue;
00040     VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg));
00041   }
00042 }
00043 
00044 // Return the preferred allocation register for reg, given a COPY instruction.
00045 static unsigned copyHint(const MachineInstr *mi, unsigned reg,
00046                          const TargetRegisterInfo &tri,
00047                          const MachineRegisterInfo &mri) {
00048   unsigned sub, hreg, hsub;
00049   if (mi->getOperand(0).getReg() == reg) {
00050     sub = mi->getOperand(0).getSubReg();
00051     hreg = mi->getOperand(1).getReg();
00052     hsub = mi->getOperand(1).getSubReg();
00053   } else {
00054     sub = mi->getOperand(1).getSubReg();
00055     hreg = mi->getOperand(0).getReg();
00056     hsub = mi->getOperand(0).getSubReg();
00057   }
00058 
00059   if (!hreg)
00060     return 0;
00061 
00062   if (TargetRegisterInfo::isVirtualRegister(hreg))
00063     return sub == hsub ? hreg : 0;
00064 
00065   const TargetRegisterClass *rc = mri.getRegClass(reg);
00066 
00067   // Only allow physreg hints in rc.
00068   if (sub == 0)
00069     return rc->contains(hreg) ? hreg : 0;
00070 
00071   // reg:sub should match the physreg hreg.
00072   return tri.getMatchingSuperReg(hreg, sub, rc);
00073 }
00074 
00075 // Check if all values in LI are rematerializable
00076 static bool isRematerializable(const LiveInterval &LI,
00077                                const LiveIntervals &LIS,
00078                                const TargetInstrInfo &TII) {
00079   for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
00080        I != E; ++I) {
00081     const VNInfo *VNI = *I;
00082     if (VNI->isUnused())
00083       continue;
00084     if (VNI->isPHIDef())
00085       return false;
00086 
00087     MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
00088     assert(MI && "Dead valno in interval");
00089 
00090     if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
00091       return false;
00092   }
00093   return true;
00094 }
00095 
00096 void
00097 VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
00098   MachineRegisterInfo &mri = MF.getRegInfo();
00099   const TargetRegisterInfo &tri = *MF.getSubtarget().getRegisterInfo();
00100   MachineBasicBlock *mbb = nullptr;
00101   MachineLoop *loop = nullptr;
00102   bool isExiting = false;
00103   float totalWeight = 0;
00104   SmallPtrSet<MachineInstr*, 8> visited;
00105 
00106   // Find the best physreg hint and the best virtreg hint.
00107   float bestPhys = 0, bestVirt = 0;
00108   unsigned hintPhys = 0, hintVirt = 0;
00109 
00110   // Don't recompute a target specific hint.
00111   bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
00112 
00113   // Don't recompute spill weight for an unspillable register.
00114   bool Spillable = li.isSpillable();
00115 
00116   for (MachineRegisterInfo::reg_instr_iterator
00117        I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end();
00118        I != E; ) {
00119     MachineInstr *mi = &*(I++);
00120     if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
00121       continue;
00122     if (!visited.insert(mi))
00123       continue;
00124 
00125     float weight = 1.0f;
00126     if (Spillable) {
00127       // Get loop info for mi.
00128       if (mi->getParent() != mbb) {
00129         mbb = mi->getParent();
00130         loop = Loops.getLoopFor(mbb);
00131         isExiting = loop ? loop->isLoopExiting(mbb) : false;
00132       }
00133 
00134       // Calculate instr weight.
00135       bool reads, writes;
00136       std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
00137       weight = LiveIntervals::getSpillWeight(
00138         writes, reads, &MBFI, mi);
00139 
00140       // Give extra weight to what looks like a loop induction variable update.
00141       if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
00142         weight *= 3;
00143 
00144       totalWeight += weight;
00145     }
00146 
00147     // Get allocation hints from copies.
00148     if (noHint || !mi->isCopy())
00149       continue;
00150     unsigned hint = copyHint(mi, li.reg, tri, mri);
00151     if (!hint)
00152       continue;
00153     // Force hweight onto the stack so that x86 doesn't add hidden precision,
00154     // making the comparison incorrectly pass (i.e., 1 > 1 == true??).
00155     //
00156     // FIXME: we probably shouldn't use floats at all.
00157     volatile float hweight = Hint[hint] += weight;
00158     if (TargetRegisterInfo::isPhysicalRegister(hint)) {
00159       if (hweight > bestPhys && mri.isAllocatable(hint))
00160         bestPhys = hweight, hintPhys = hint;
00161     } else {
00162       if (hweight > bestVirt)
00163         bestVirt = hweight, hintVirt = hint;
00164     }
00165   }
00166 
00167   Hint.clear();
00168 
00169   // Always prefer the physreg hint.
00170   if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
00171     mri.setRegAllocationHint(li.reg, 0, hint);
00172     // Weakly boost the spill weight of hinted registers.
00173     totalWeight *= 1.01F;
00174   }
00175 
00176   // If the live interval was already unspillable, leave it that way.
00177   if (!Spillable)
00178     return;
00179 
00180   // Mark li as unspillable if all live ranges are tiny.
00181   if (li.isZeroLength(LIS.getSlotIndexes())) {
00182     li.markNotSpillable();
00183     return;
00184   }
00185 
00186   // If all of the definitions of the interval are re-materializable,
00187   // it is a preferred candidate for spilling.
00188   // FIXME: this gets much more complicated once we support non-trivial
00189   // re-materialization.
00190   if (isRematerializable(li, LIS, *MF.getSubtarget().getInstrInfo()))
00191     totalWeight *= 0.5F;
00192 
00193   li.weight = normalize(totalWeight, li.getSize());
00194 }