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/Function.h"
13 #include "llvm/ADT/SmallSet.h"
14 #include "llvm/CodeGen/CalcSpillWeights.h"
15 #include "llvm/CodeGen/LiveIntervalAnalysis.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<MachineLoopInfo>();
39 MachineFunctionPass::getAnalysisUsage(au);
42 bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &MF) {
44 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
45 << "********** Function: "
46 << MF.getFunction()->getName() << '\n');
48 LiveIntervals &LIS = getAnalysis<LiveIntervals>();
49 MachineRegisterInfo &MRI = MF.getRegInfo();
50 VirtRegAuxInfo VRAI(MF, LIS, getAnalysis<MachineLoopInfo>());
51 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
52 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
53 if (MRI.reg_nodbg_empty(Reg))
55 VRAI.CalculateWeightAndHint(LIS.getInterval(Reg));
60 // Return the preferred allocation register for reg, given a COPY instruction.
61 static unsigned copyHint(const MachineInstr *mi, unsigned reg,
62 const TargetRegisterInfo &tri,
63 const MachineRegisterInfo &mri) {
64 unsigned sub, hreg, hsub;
65 if (mi->getOperand(0).getReg() == reg) {
66 sub = mi->getOperand(0).getSubReg();
67 hreg = mi->getOperand(1).getReg();
68 hsub = mi->getOperand(1).getSubReg();
70 sub = mi->getOperand(1).getSubReg();
71 hreg = mi->getOperand(0).getReg();
72 hsub = mi->getOperand(0).getSubReg();
78 if (TargetRegisterInfo::isVirtualRegister(hreg))
79 return sub == hsub ? hreg : 0;
81 const TargetRegisterClass *rc = mri.getRegClass(reg);
83 // Only allow physreg hints in rc.
85 return rc->contains(hreg) ? hreg : 0;
87 // reg:sub should match the physreg hreg.
88 return tri.getMatchingSuperReg(hreg, sub, rc);
91 // Check if all values in LI are rematerializable
92 static bool isRematerializable(const LiveInterval &LI,
93 const LiveIntervals &LIS,
94 const TargetInstrInfo &TII) {
95 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
97 const VNInfo *VNI = *I;
103 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
104 assert(MI && "Dead valno in interval");
106 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
112 void 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 unsigned loopDepth = 0;
118 bool isExiting = false;
119 float totalWeight = 0;
120 SmallPtrSet<MachineInstr*, 8> visited;
122 // Find the best physreg hist and the best virtreg hint.
123 float bestPhys = 0, bestVirt = 0;
124 unsigned hintPhys = 0, hintVirt = 0;
126 // Don't recompute a target specific hint.
127 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
129 // Don't recompute spill weight for an unspillable register.
130 bool Spillable = li.isSpillable();
132 for (MachineRegisterInfo::reg_iterator I = mri.reg_begin(li.reg);
133 MachineInstr *mi = I.skipInstruction();) {
134 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
136 if (!visited.insert(mi))
141 // Get loop info for mi.
142 if (mi->getParent() != mbb) {
143 mbb = mi->getParent();
144 loop = Loops.getLoopFor(mbb);
145 loopDepth = loop ? loop->getLoopDepth() : 0;
146 isExiting = loop ? loop->isLoopExiting(mbb) : false;
149 // Calculate instr weight.
151 tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
152 weight = LiveIntervals::getSpillWeight(writes, reads, loopDepth);
154 // Give extra weight to what looks like a loop induction variable update.
155 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
158 totalWeight += weight;
161 // Get allocation hints from copies.
162 if (noHint || !mi->isCopy())
164 unsigned hint = copyHint(mi, li.reg, tri, mri);
167 float hweight = Hint[hint] += weight;
168 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
169 if (hweight > bestPhys && LIS.isAllocatable(hint))
170 bestPhys = hweight, hintPhys = hint;
172 if (hweight > bestVirt)
173 bestVirt = hweight, hintVirt = hint;
179 // Always prefer the physreg hint.
180 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
181 mri.setRegAllocationHint(li.reg, 0, hint);
182 // Weakly boost the spill weight of hinted registers.
183 totalWeight *= 1.01F;
186 // If the live interval was already unspillable, leave it that way.
190 // Mark li as unspillable if all live ranges are tiny.
191 if (li.isZeroLength(LIS.getSlotIndexes())) {
192 li.markNotSpillable();
196 // If all of the definitions of the interval are re-materializable,
197 // it is a preferred candidate for spilling.
198 // FIXME: this gets much more complicated once we support non-trivial
199 // re-materialization.
200 if (isRematerializable(li, LIS, *MF.getTarget().getInstrInfo()))
203 li.weight = normalizeSpillWeight(totalWeight, li.getSize());