blob: 5045675104f3f83a064f2ab3cfbb422f7a23cf41 [file] [log] [blame]
Lang Hamesa937f222009-12-14 06:49:42 +00001//===------------------------ CalcSpillWeights.cpp ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#define DEBUG_TYPE "calcspillweights"
11
Lang Hamesa937f222009-12-14 06:49:42 +000012#include "llvm/ADT/SmallSet.h"
13#include "llvm/CodeGen/CalcSpillWeights.h"
14#include "llvm/CodeGen/LiveIntervalAnalysis.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineLoopInfo.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/CodeGen/SlotIndexes.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
21#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng8112b532010-02-10 01:21:02 +000022#include "llvm/Target/TargetMachine.h"
Lang Hamesa937f222009-12-14 06:49:42 +000023#include "llvm/Target/TargetRegisterInfo.h"
Lang Hamesa937f222009-12-14 06:49:42 +000024using namespace llvm;
25
26char CalculateSpillWeights::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000027INITIALIZE_PASS_BEGIN(CalculateSpillWeights, "calcspillweights",
28 "Calculate spill weights", false, false)
29INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
30INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
31INITIALIZE_PASS_END(CalculateSpillWeights, "calcspillweights",
Owen Andersonce665bd2010-10-07 22:25:06 +000032 "Calculate spill weights", false, false)
Lang Hamesa937f222009-12-14 06:49:42 +000033
34void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const {
35 au.addRequired<LiveIntervals>();
36 au.addRequired<MachineLoopInfo>();
37 au.setPreservesAll();
38 MachineFunctionPass::getAnalysisUsage(au);
39}
40
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000041bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &MF) {
Lang Hamesa937f222009-12-14 06:49:42 +000042
David Greene7ed6dd62009-12-24 00:39:02 +000043 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
Lang Hamesa937f222009-12-14 06:49:42 +000044 << "********** Function: "
Craig Topper96601ca2012-08-22 06:07:19 +000045 << MF.getName() << '\n');
Lang Hamesa937f222009-12-14 06:49:42 +000046
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000047 LiveIntervals &LIS = getAnalysis<LiveIntervals>();
48 MachineRegisterInfo &MRI = MF.getRegInfo();
49 VirtRegAuxInfo VRAI(MF, LIS, getAnalysis<MachineLoopInfo>());
50 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
51 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
52 if (MRI.reg_nodbg_empty(Reg))
53 continue;
54 VRAI.CalculateWeightAndHint(LIS.getInterval(Reg));
Lang Hamesa937f222009-12-14 06:49:42 +000055 }
Lang Hamesa937f222009-12-14 06:49:42 +000056 return false;
57}
58
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +000059// Return the preferred allocation register for reg, given a COPY instruction.
60static 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();
68 } else {
69 sub = mi->getOperand(1).getSubReg();
70 hreg = mi->getOperand(0).getReg();
71 hsub = mi->getOperand(0).getSubReg();
72 }
73
74 if (!hreg)
75 return 0;
76
77 if (TargetRegisterInfo::isVirtualRegister(hreg))
78 return sub == hsub ? hreg : 0;
79
80 const TargetRegisterClass *rc = mri.getRegClass(reg);
81
82 // Only allow physreg hints in rc.
83 if (sub == 0)
84 return rc->contains(hreg) ? hreg : 0;
85
86 // reg:sub should match the physreg hreg.
87 return tri.getMatchingSuperReg(hreg, sub, rc);
Lang Hamesa937f222009-12-14 06:49:42 +000088}
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +000089
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +000090// Check if all values in LI are rematerializable
91static 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();
95 I != E; ++I) {
96 const VNInfo *VNI = *I;
97 if (VNI->isUnused())
98 continue;
99 if (VNI->isPHIDef())
100 return false;
101
102 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
103 assert(MI && "Dead valno in interval");
104
105 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
106 return false;
107 }
108 return true;
109}
110
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000111void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000112 MachineRegisterInfo &mri = MF.getRegInfo();
113 const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo();
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000114 MachineBasicBlock *mbb = 0;
115 MachineLoop *loop = 0;
116 unsigned loopDepth = 0;
117 bool isExiting = false;
118 float totalWeight = 0;
119 SmallPtrSet<MachineInstr*, 8> visited;
120
121 // Find the best physreg hist and the best virtreg hint.
122 float bestPhys = 0, bestVirt = 0;
123 unsigned hintPhys = 0, hintVirt = 0;
124
125 // Don't recompute a target specific hint.
126 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
127
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000128 // Don't recompute spill weight for an unspillable register.
129 bool Spillable = li.isSpillable();
130
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000131 for (MachineRegisterInfo::reg_iterator I = mri.reg_begin(li.reg);
132 MachineInstr *mi = I.skipInstruction();) {
133 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
134 continue;
135 if (!visited.insert(mi))
136 continue;
137
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000138 float weight = 1.0f;
139 if (Spillable) {
140 // Get loop info for mi.
141 if (mi->getParent() != mbb) {
142 mbb = mi->getParent();
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000143 loop = Loops.getLoopFor(mbb);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000144 loopDepth = loop ? loop->getLoopDepth() : 0;
145 isExiting = loop ? loop->isLoopExiting(mbb) : false;
146 }
147
148 // Calculate instr weight.
149 bool reads, writes;
150 tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
151 weight = LiveIntervals::getSpillWeight(writes, reads, loopDepth);
152
153 // Give extra weight to what looks like a loop induction variable update.
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000154 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000155 weight *= 3;
156
157 totalWeight += weight;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000158 }
159
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000160 // Get allocation hints from copies.
161 if (noHint || !mi->isCopy())
162 continue;
163 unsigned hint = copyHint(mi, li.reg, tri, mri);
164 if (!hint)
165 continue;
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000166 float hweight = Hint[hint] += weight;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000167 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000168 if (hweight > bestPhys && LIS.isAllocatable(hint))
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000169 bestPhys = hweight, hintPhys = hint;
170 } else {
171 if (hweight > bestVirt)
172 bestVirt = hweight, hintVirt = hint;
173 }
174 }
175
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000176 Hint.clear();
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000177
178 // Always prefer the physreg hint.
179 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
180 mri.setRegAllocationHint(li.reg, 0, hint);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000181 // Weakly boost the spill weight of hinted registers.
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000182 totalWeight *= 1.01F;
183 }
184
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000185 // If the live interval was already unspillable, leave it that way.
186 if (!Spillable)
187 return;
188
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000189 // Mark li as unspillable if all live ranges are tiny.
Jakob Stoklund Olesenf5497fb2011-05-16 23:50:05 +0000190 if (li.isZeroLength(LIS.getSlotIndexes())) {
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000191 li.markNotSpillable();
192 return;
193 }
194
195 // If all of the definitions of the interval are re-materializable,
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +0000196 // it is a preferred candidate for spilling.
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000197 // FIXME: this gets much more complicated once we support non-trivial
198 // re-materialization.
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +0000199 if (isRematerializable(li, LIS, *MF.getTarget().getInstrInfo()))
200 totalWeight *= 0.5F;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000201
Jakob Stoklund Oleseneb9f0402011-02-14 23:15:38 +0000202 li.weight = normalizeSpillWeight(totalWeight, li.getSize());
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000203}