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 #include "llvm/CodeGen/CalcSpillWeights.h"
11 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
12 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/MachineLoopInfo.h"
15 #include "llvm/CodeGen/MachineRegisterInfo.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20 #include "llvm/Target/TargetSubtargetInfo.h"
23 #define DEBUG_TYPE "calcspillweights"
25 void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
27 const MachineLoopInfo &MLI,
28 const MachineBlockFrequencyInfo &MBFI,
29 VirtRegAuxInfo::NormalizingFn norm) {
30 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
31 << "********** Function: " << MF.getName() << '\n');
33 MachineRegisterInfo &MRI = MF.getRegInfo();
34 VirtRegAuxInfo VRAI(MF, LIS, MLI, MBFI, norm);
35 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
36 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
37 if (MRI.reg_nodbg_empty(Reg))
39 VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg));
43 // Return the preferred allocation register for reg, given a COPY instruction.
44 static unsigned copyHint(const MachineInstr *mi, unsigned reg,
45 const TargetRegisterInfo &tri,
46 const MachineRegisterInfo &mri) {
47 unsigned sub, hreg, hsub;
48 if (mi->getOperand(0).getReg() == reg) {
49 sub = mi->getOperand(0).getSubReg();
50 hreg = mi->getOperand(1).getReg();
51 hsub = mi->getOperand(1).getSubReg();
53 sub = mi->getOperand(1).getSubReg();
54 hreg = mi->getOperand(0).getReg();
55 hsub = mi->getOperand(0).getSubReg();
61 if (TargetRegisterInfo::isVirtualRegister(hreg))
62 return sub == hsub ? hreg : 0;
64 const TargetRegisterClass *rc = mri.getRegClass(reg);
66 // Only allow physreg hints in rc.
68 return rc->contains(hreg) ? hreg : 0;
70 // reg:sub should match the physreg hreg.
71 return tri.getMatchingSuperReg(hreg, sub, rc);
74 // Check if all values in LI are rematerializable
75 static bool isRematerializable(const LiveInterval &LI,
76 const LiveIntervals &LIS,
77 const TargetInstrInfo &TII) {
78 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
80 const VNInfo *VNI = *I;
86 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
87 assert(MI && "Dead valno in interval");
89 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
96 VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
97 MachineRegisterInfo &mri = MF.getRegInfo();
98 const TargetRegisterInfo &tri = *MF.getSubtarget().getRegisterInfo();
99 MachineBasicBlock *mbb = nullptr;
100 MachineLoop *loop = nullptr;
101 bool isExiting = false;
102 float totalWeight = 0;
103 SmallPtrSet<MachineInstr*, 8> visited;
105 // Find the best physreg hint and the best virtreg hint.
106 float bestPhys = 0, bestVirt = 0;
107 unsigned hintPhys = 0, hintVirt = 0;
109 // Don't recompute a target specific hint.
110 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
112 // Don't recompute spill weight for an unspillable register.
113 bool Spillable = li.isSpillable();
115 for (MachineRegisterInfo::reg_instr_iterator
116 I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end();
118 MachineInstr *mi = &*(I++);
119 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
121 if (!visited.insert(mi))
126 // Get loop info for mi.
127 if (mi->getParent() != mbb) {
128 mbb = mi->getParent();
129 loop = Loops.getLoopFor(mbb);
130 isExiting = loop ? loop->isLoopExiting(mbb) : false;
133 // Calculate instr weight.
135 std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
136 weight = LiveIntervals::getSpillWeight(
137 writes, reads, &MBFI, mi);
139 // Give extra weight to what looks like a loop induction variable update.
140 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
143 totalWeight += weight;
146 // Get allocation hints from copies.
147 if (noHint || !mi->isCopy())
149 unsigned hint = copyHint(mi, li.reg, tri, mri);
152 // Force hweight onto the stack so that x86 doesn't add hidden precision,
153 // making the comparison incorrectly pass (i.e., 1 > 1 == true??).
155 // FIXME: we probably shouldn't use floats at all.
156 volatile float hweight = Hint[hint] += weight;
157 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
158 if (hweight > bestPhys && mri.isAllocatable(hint))
159 bestPhys = hweight, hintPhys = hint;
161 if (hweight > bestVirt)
162 bestVirt = hweight, hintVirt = hint;
168 // Always prefer the physreg hint.
169 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
170 mri.setRegAllocationHint(li.reg, 0, hint);
171 // Weakly boost the spill weight of hinted registers.
172 totalWeight *= 1.01F;
175 // If the live interval was already unspillable, leave it that way.
179 // Mark li as unspillable if all live ranges are tiny.
180 if (li.isZeroLength(LIS.getSlotIndexes())) {
181 li.markNotSpillable();
185 // If all of the definitions of the interval are re-materializable,
186 // it is a preferred candidate for spilling.
187 // FIXME: this gets much more complicated once we support non-trivial
188 // re-materialization.
189 if (isRematerializable(li, LIS, *MF.getSubtarget().getInstrInfo()))
192 li.weight = normalize(totalWeight, li.getSize());