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 char CalculateSpillWeights::ID = 0;
26 INITIALIZE_PASS_BEGIN(CalculateSpillWeights, "calcspillweights",
27 "Calculate spill weights", false, false)
28 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
29 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
30 INITIALIZE_PASS_END(CalculateSpillWeights, "calcspillweights",
31 "Calculate spill weights", false, false)
33 void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const {
34 au.addRequired<LiveIntervals>();
35 au.addRequired<MachineBlockFrequencyInfo>();
36 au.addRequired<MachineLoopInfo>();
38 MachineFunctionPass::getAnalysisUsage(au);
41 bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &MF) {
43 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
44 << "********** Function: " << MF.getName() << '\n');
46 LiveIntervals &LIS = getAnalysis<LiveIntervals>();
47 MachineRegisterInfo &MRI = MF.getRegInfo();
48 VirtRegAuxInfo VRAI(MF, LIS, getAnalysis<MachineLoopInfo>(),
49 getAnalysis<MachineBlockFrequencyInfo>());
50 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
51 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
52 if (MRI.reg_nodbg_empty(Reg))
54 VRAI.CalculateWeightAndHint(LIS.getInterval(Reg));
59 // Return the preferred allocation register for reg, given a COPY instruction.
60 static unsigned copyHint(const MachineInstr *mi, unsigned reg,
61 const TargetRegisterInfo &tri,
62 const MachineRegisterInfo &mri) {
63 unsigned sub, hreg, hsub;
64 if (mi->getOperand(0).getReg() == reg) {
65 sub = mi->getOperand(0).getSubReg();
66 hreg = mi->getOperand(1).getReg();
67 hsub = mi->getOperand(1).getSubReg();
69 sub = mi->getOperand(1).getSubReg();
70 hreg = mi->getOperand(0).getReg();
71 hsub = mi->getOperand(0).getSubReg();
77 if (TargetRegisterInfo::isVirtualRegister(hreg))
78 return sub == hsub ? hreg : 0;
80 const TargetRegisterClass *rc = mri.getRegClass(reg);
82 // Only allow physreg hints in rc.
84 return rc->contains(hreg) ? hreg : 0;
86 // reg:sub should match the physreg hreg.
87 return tri.getMatchingSuperReg(hreg, sub, rc);
90 // Check if all values in LI are rematerializable
91 static bool isRematerializable(const LiveInterval &LI,
92 const LiveIntervals &LIS,
93 const TargetInstrInfo &TII) {
94 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
96 const VNInfo *VNI = *I;
102 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
103 assert(MI && "Dead valno in interval");
105 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
112 VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
113 MachineRegisterInfo &mri = MF.getRegInfo();
114 const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo();
115 MachineBasicBlock *mbb = 0;
116 MachineLoop *loop = 0;
117 bool isExiting = false;
118 float totalWeight = 0;
119 SmallPtrSet<MachineInstr*, 8> visited;
121 // Find the best physreg hint and the best virtreg hint.
122 float bestPhys = 0, bestVirt = 0;
123 unsigned hintPhys = 0, hintVirt = 0;
125 // Don't recompute a target specific hint.
126 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
128 // Don't recompute spill weight for an unspillable register.
129 bool Spillable = li.isSpillable();
131 for (MachineRegisterInfo::reg_iterator I = mri.reg_begin(li.reg);
132 MachineInstr *mi = I.skipInstruction();) {
133 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
135 if (!visited.insert(mi))
140 // Get loop info for mi.
141 if (mi->getParent() != mbb) {
142 mbb = mi->getParent();
143 loop = Loops.getLoopFor(mbb);
144 isExiting = loop ? loop->isLoopExiting(mbb) : false;
147 // Calculate instr weight.
149 tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
150 weight = LiveIntervals::getSpillWeight(
151 writes, reads, MBFI.getBlockFreq(mi->getParent()));
153 // Give extra weight to what looks like a loop induction variable update.
154 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
157 totalWeight += weight;
160 // Get allocation hints from copies.
161 if (noHint || !mi->isCopy())
163 unsigned hint = copyHint(mi, li.reg, tri, mri);
166 float hweight = Hint[hint] += weight;
167 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
168 if (hweight > bestPhys && mri.isAllocatable(hint))
169 bestPhys = hweight, hintPhys = hint;
171 if (hweight > bestVirt)
172 bestVirt = hweight, hintVirt = hint;
178 // Always prefer the physreg hint.
179 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
180 mri.setRegAllocationHint(li.reg, 0, hint);
181 // Weakly boost the spill weight of hinted registers.
182 totalWeight *= 1.01F;
185 // If the live interval was already unspillable, leave it that way.
189 // Mark li as unspillable if all live ranges are tiny.
190 if (li.isZeroLength(LIS.getSlotIndexes())) {
191 li.markNotSpillable();
195 // If all of the definitions of the interval are re-materializable,
196 // it is a preferred candidate for spilling.
197 // FIXME: this gets much more complicated once we support non-trivial
198 // re-materialization.
199 if (isRematerializable(li, LIS, *MF.getTarget().getInstrInfo()))
202 li.weight = normalizeSpillWeight(totalWeight, li.getSize());