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/ADT/SmallSet.h"
13 #include "llvm/CodeGen/CalcSpillWeights.h"
14 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
15 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineLoopInfo.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/SlotIndexes.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
27 char CalculateSpillWeights::ID = 0;
28 INITIALIZE_PASS_BEGIN(CalculateSpillWeights, "calcspillweights",
29 "Calculate spill weights", false, false)
30 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
31 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
32 INITIALIZE_PASS_END(CalculateSpillWeights, "calcspillweights",
33 "Calculate spill weights", false, false)
35 void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const {
36 au.addRequired<LiveIntervals>();
37 au.addRequired<MachineBlockFrequencyInfo>();
38 au.addRequired<MachineLoopInfo>();
40 MachineFunctionPass::getAnalysisUsage(au);
43 bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &MF) {
45 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
46 << "********** Function: " << MF.getName() << '\n');
48 LiveIntervals &LIS = getAnalysis<LiveIntervals>();
49 MachineRegisterInfo &MRI = MF.getRegInfo();
50 VirtRegAuxInfo VRAI(MF, LIS, getAnalysis<MachineLoopInfo>(),
51 getAnalysis<MachineBlockFrequencyInfo>());
52 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
53 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
54 if (MRI.reg_nodbg_empty(Reg))
56 VRAI.CalculateWeightAndHint(LIS.getInterval(Reg));
61 // Return the preferred allocation register for reg, given a COPY instruction.
62 static unsigned copyHint(const MachineInstr *mi, unsigned reg,
63 const TargetRegisterInfo &tri,
64 const MachineRegisterInfo &mri) {
65 unsigned sub, hreg, hsub;
66 if (mi->getOperand(0).getReg() == reg) {
67 sub = mi->getOperand(0).getSubReg();
68 hreg = mi->getOperand(1).getReg();
69 hsub = mi->getOperand(1).getSubReg();
71 sub = mi->getOperand(1).getSubReg();
72 hreg = mi->getOperand(0).getReg();
73 hsub = mi->getOperand(0).getSubReg();
79 if (TargetRegisterInfo::isVirtualRegister(hreg))
80 return sub == hsub ? hreg : 0;
82 const TargetRegisterClass *rc = mri.getRegClass(reg);
84 // Only allow physreg hints in rc.
86 return rc->contains(hreg) ? hreg : 0;
88 // reg:sub should match the physreg hreg.
89 return tri.getMatchingSuperReg(hreg, sub, rc);
92 // Check if all values in LI are rematerializable
93 static bool isRematerializable(const LiveInterval &LI,
94 const LiveIntervals &LIS,
95 const TargetInstrInfo &TII) {
96 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
98 const VNInfo *VNI = *I;
104 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
105 assert(MI && "Dead valno in interval");
107 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
114 VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
115 MachineRegisterInfo &mri = MF.getRegInfo();
116 const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo();
117 MachineBasicBlock *mbb = 0;
118 MachineLoop *loop = 0;
119 bool isExiting = false;
120 float totalWeight = 0;
121 SmallPtrSet<MachineInstr*, 8> visited;
123 // Find the best physreg hint and the best virtreg hint.
124 float bestPhys = 0, bestVirt = 0;
125 unsigned hintPhys = 0, hintVirt = 0;
127 // Don't recompute a target specific hint.
128 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
130 // Don't recompute spill weight for an unspillable register.
131 bool Spillable = li.isSpillable();
133 for (MachineRegisterInfo::reg_iterator I = mri.reg_begin(li.reg);
134 MachineInstr *mi = I.skipInstruction();) {
135 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
137 if (!visited.insert(mi))
142 // Get loop info for mi.
143 if (mi->getParent() != mbb) {
144 mbb = mi->getParent();
145 loop = Loops.getLoopFor(mbb);
146 isExiting = loop ? loop->isLoopExiting(mbb) : false;
149 // Calculate instr weight.
151 tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
152 weight = LiveIntervals::getSpillWeight(
153 writes, reads, MBFI.getBlockFreq(mi->getParent()));
155 // Give extra weight to what looks like a loop induction variable update.
156 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
159 totalWeight += weight;
162 // Get allocation hints from copies.
163 if (noHint || !mi->isCopy())
165 unsigned hint = copyHint(mi, li.reg, tri, mri);
168 float hweight = Hint[hint] += weight;
169 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
170 if (hweight > bestPhys && mri.isAllocatable(hint))
171 bestPhys = hweight, hintPhys = hint;
173 if (hweight > bestVirt)
174 bestVirt = hweight, hintVirt = hint;
180 // Always prefer the physreg hint.
181 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
182 mri.setRegAllocationHint(li.reg, 0, hint);
183 // Weakly boost the spill weight of hinted registers.
184 totalWeight *= 1.01F;
187 // If the live interval was already unspillable, leave it that way.
191 // Mark li as unspillable if all live ranges are tiny.
192 if (li.isZeroLength(LIS.getSlotIndexes())) {
193 li.markNotSpillable();
197 // If all of the definitions of the interval are re-materializable,
198 // it is a preferred candidate for spilling.
199 // FIXME: this gets much more complicated once we support non-trivial
200 // re-materialization.
201 if (isRematerializable(li, LIS, *MF.getTarget().getInstrInfo()))
204 li.weight = normalizeSpillWeight(totalWeight, li.getSize());