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/TargetMachine.h"
20 #include "llvm/Target/TargetRegisterInfo.h"
21 #include "llvm/Target/TargetSubtargetInfo.h"
24 #define DEBUG_TYPE "calcspillweights"
26 void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
28 const MachineLoopInfo &MLI,
29 const MachineBlockFrequencyInfo &MBFI,
30 VirtRegAuxInfo::NormalizingFn norm) {
31 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
32 << "********** Function: " << MF.getName() << '\n');
34 MachineRegisterInfo &MRI = MF.getRegInfo();
35 VirtRegAuxInfo VRAI(MF, LIS, MLI, MBFI, norm);
36 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
37 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
38 if (MRI.reg_nodbg_empty(Reg))
40 VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg));
44 // Return the preferred allocation register for reg, given a COPY instruction.
45 static unsigned copyHint(const MachineInstr *mi, unsigned reg,
46 const TargetRegisterInfo &tri,
47 const MachineRegisterInfo &mri) {
48 unsigned sub, hreg, hsub;
49 if (mi->getOperand(0).getReg() == reg) {
50 sub = mi->getOperand(0).getSubReg();
51 hreg = mi->getOperand(1).getReg();
52 hsub = mi->getOperand(1).getSubReg();
54 sub = mi->getOperand(1).getSubReg();
55 hreg = mi->getOperand(0).getReg();
56 hsub = mi->getOperand(0).getSubReg();
62 if (TargetRegisterInfo::isVirtualRegister(hreg))
63 return sub == hsub ? hreg : 0;
65 const TargetRegisterClass *rc = mri.getRegClass(reg);
67 // Only allow physreg hints in rc.
69 return rc->contains(hreg) ? hreg : 0;
71 // reg:sub should match the physreg hreg.
72 return tri.getMatchingSuperReg(hreg, sub, rc);
75 // Check if all values in LI are rematerializable
76 static bool isRematerializable(const LiveInterval &LI,
77 const LiveIntervals &LIS,
78 const TargetInstrInfo &TII) {
79 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
81 const VNInfo *VNI = *I;
87 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
88 assert(MI && "Dead valno in interval");
90 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
97 VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
98 MachineRegisterInfo &mri = MF.getRegInfo();
99 const TargetRegisterInfo &tri =
100 *MF.getTarget().getSubtargetImpl()->getRegisterInfo();
101 MachineBasicBlock *mbb = nullptr;
102 MachineLoop *loop = nullptr;
103 bool isExiting = false;
104 float totalWeight = 0;
105 SmallPtrSet<MachineInstr*, 8> visited;
107 // Find the best physreg hint and the best virtreg hint.
108 float bestPhys = 0, bestVirt = 0;
109 unsigned hintPhys = 0, hintVirt = 0;
111 // Don't recompute a target specific hint.
112 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
114 // Don't recompute spill weight for an unspillable register.
115 bool Spillable = li.isSpillable();
117 for (MachineRegisterInfo::reg_instr_iterator
118 I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end();
120 MachineInstr *mi = &*(I++);
121 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
123 if (!visited.insert(mi))
128 // Get loop info for mi.
129 if (mi->getParent() != mbb) {
130 mbb = mi->getParent();
131 loop = Loops.getLoopFor(mbb);
132 isExiting = loop ? loop->isLoopExiting(mbb) : false;
135 // Calculate instr weight.
137 std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
138 weight = LiveIntervals::getSpillWeight(
139 writes, reads, &MBFI, mi);
141 // Give extra weight to what looks like a loop induction variable update.
142 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
145 totalWeight += weight;
148 // Get allocation hints from copies.
149 if (noHint || !mi->isCopy())
151 unsigned hint = copyHint(mi, li.reg, tri, mri);
154 // Force hweight onto the stack so that x86 doesn't add hidden precision,
155 // making the comparison incorrectly pass (i.e., 1 > 1 == true??).
157 // FIXME: we probably shouldn't use floats at all.
158 volatile float hweight = Hint[hint] += weight;
159 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
160 if (hweight > bestPhys && mri.isAllocatable(hint))
161 bestPhys = hweight, hintPhys = hint;
163 if (hweight > bestVirt)
164 bestVirt = hweight, hintVirt = hint;
170 // Always prefer the physreg hint.
171 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
172 mri.setRegAllocationHint(li.reg, 0, hint);
173 // Weakly boost the spill weight of hinted registers.
174 totalWeight *= 1.01F;
177 // If the live interval was already unspillable, leave it that way.
181 // Mark li as unspillable if all live ranges are tiny.
182 if (li.isZeroLength(LIS.getSlotIndexes())) {
183 li.markNotSpillable();
187 // If all of the definitions of the interval are re-materializable,
188 // it is a preferred candidate for spilling.
189 // FIXME: this gets much more complicated once we support non-trivial
190 // re-materialization.
191 if (isRematerializable(li, LIS,
192 *MF.getTarget().getSubtargetImpl()->getInstrInfo()))
195 li.weight = normalize(totalWeight, li.getSize());