1 //===------------------------ CalcSpillWeights.cpp ------------------------===//
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 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "calcspillweights"
12 #include "llvm/CodeGen/CalcSpillWeights.h"
13 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
14 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineLoopInfo.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
25 void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
27 const MachineLoopInfo &MLI,
28 const MachineBlockFrequencyInfo &MBFI) {
29 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
30 << "********** Function: " << MF.getName() << '\n');
32 MachineRegisterInfo &MRI = MF.getRegInfo();
33 VirtRegAuxInfo VRAI(MF, LIS, MLI, MBFI);
34 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
35 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
36 if (MRI.reg_nodbg_empty(Reg))
38 VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg));
42 // Return the preferred allocation register for reg, given a COPY instruction.
43 static unsigned copyHint(const MachineInstr *mi, unsigned reg,
44 const TargetRegisterInfo &tri,
45 const MachineRegisterInfo &mri) {
46 unsigned sub, hreg, hsub;
47 if (mi->getOperand(0).getReg() == reg) {
48 sub = mi->getOperand(0).getSubReg();
49 hreg = mi->getOperand(1).getReg();
50 hsub = mi->getOperand(1).getSubReg();
52 sub = mi->getOperand(1).getSubReg();
53 hreg = mi->getOperand(0).getReg();
54 hsub = mi->getOperand(0).getSubReg();
60 if (TargetRegisterInfo::isVirtualRegister(hreg))
61 return sub == hsub ? hreg : 0;
63 const TargetRegisterClass *rc = mri.getRegClass(reg);
65 // Only allow physreg hints in rc.
67 return rc->contains(hreg) ? hreg : 0;
69 // reg:sub should match the physreg hreg.
70 return tri.getMatchingSuperReg(hreg, sub, rc);
73 // Check if all values in LI are rematerializable
74 static bool isRematerializable(const LiveInterval &LI,
75 const LiveIntervals &LIS,
76 const TargetInstrInfo &TII) {
77 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
79 const VNInfo *VNI = *I;
85 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
86 assert(MI && "Dead valno in interval");
88 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
95 VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
96 MachineRegisterInfo &mri = MF.getRegInfo();
97 const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo();
98 MachineBasicBlock *mbb = 0;
99 MachineLoop *loop = 0;
100 bool isExiting = false;
101 float totalWeight = 0;
102 SmallPtrSet<MachineInstr*, 8> visited;
104 // Find the best physreg hint and the best virtreg hint.
105 float bestPhys = 0, bestVirt = 0;
106 unsigned hintPhys = 0, hintVirt = 0;
108 // Don't recompute a target specific hint.
109 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
111 // Don't recompute spill weight for an unspillable register.
112 bool Spillable = li.isSpillable();
114 for (MachineRegisterInfo::reg_iterator I = mri.reg_begin(li.reg);
115 MachineInstr *mi = I.skipInstruction();) {
116 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
118 if (!visited.insert(mi))
123 // Get loop info for mi.
124 if (mi->getParent() != mbb) {
125 mbb = mi->getParent();
126 loop = Loops.getLoopFor(mbb);
127 isExiting = loop ? loop->isLoopExiting(mbb) : false;
130 // Calculate instr weight.
132 tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
133 weight = LiveIntervals::getSpillWeight(
134 writes, reads, MBFI.getBlockFreq(mi->getParent()));
136 // Give extra weight to what looks like a loop induction variable update.
137 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
140 totalWeight += weight;
143 // Get allocation hints from copies.
144 if (noHint || !mi->isCopy())
146 unsigned hint = copyHint(mi, li.reg, tri, mri);
149 float hweight = Hint[hint] += weight;
150 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
151 if (hweight > bestPhys && mri.isAllocatable(hint))
152 bestPhys = hweight, hintPhys = hint;
154 if (hweight > bestVirt)
155 bestVirt = hweight, hintVirt = hint;
161 // Always prefer the physreg hint.
162 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
163 mri.setRegAllocationHint(li.reg, 0, hint);
164 // Weakly boost the spill weight of hinted registers.
165 totalWeight *= 1.01F;
168 // If the live interval was already unspillable, leave it that way.
172 // Mark li as unspillable if all live ranges are tiny.
173 if (li.isZeroLength(LIS.getSlotIndexes())) {
174 li.markNotSpillable();
178 // If all of the definitions of the interval are re-materializable,
179 // it is a preferred candidate for spilling.
180 // FIXME: this gets much more complicated once we support non-trivial
181 // re-materialization.
182 if (isRematerializable(li, LIS, *MF.getTarget().getInstrInfo()))
185 li.weight = normalizeSpillWeight(totalWeight, li.getSize());