blob: 939af3f0ccc2d2307ae8eae4331368c712a15cd1 [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
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"
Evan Cheng8112b532010-02-10 01:21:02 +000023#include "llvm/Target/TargetMachine.h"
Lang Hamesa937f222009-12-14 06:49:42 +000024#include "llvm/Target/TargetRegisterInfo.h"
Lang Hamesa937f222009-12-14 06:49:42 +000025using namespace llvm;
26
27char CalculateSpillWeights::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000028INITIALIZE_PASS_BEGIN(CalculateSpillWeights, "calcspillweights",
29 "Calculate spill weights", false, false)
30INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
31INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
32INITIALIZE_PASS_END(CalculateSpillWeights, "calcspillweights",
Owen Andersonce665bd2010-10-07 22:25:06 +000033 "Calculate spill weights", false, false)
Lang Hamesa937f222009-12-14 06:49:42 +000034
35void CalculateSpillWeights::getAnalysisUsage(AnalysisUsage &au) const {
36 au.addRequired<LiveIntervals>();
37 au.addRequired<MachineLoopInfo>();
38 au.setPreservesAll();
39 MachineFunctionPass::getAnalysisUsage(au);
40}
41
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000042bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &MF) {
Lang Hamesa937f222009-12-14 06:49:42 +000043
David Greene7ed6dd62009-12-24 00:39:02 +000044 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
Lang Hamesa937f222009-12-14 06:49:42 +000045 << "********** Function: "
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000046 << MF.getFunction()->getName() << '\n');
Lang Hamesa937f222009-12-14 06:49:42 +000047
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000048 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))
54 continue;
55 VRAI.CalculateWeightAndHint(LIS.getInterval(Reg));
Lang Hamesa937f222009-12-14 06:49:42 +000056 }
Lang Hamesa937f222009-12-14 06:49:42 +000057 return false;
58}
59
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +000060// Return the preferred allocation register for reg, given a COPY instruction.
61static 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();
69 } else {
70 sub = mi->getOperand(1).getSubReg();
71 hreg = mi->getOperand(0).getReg();
72 hsub = mi->getOperand(0).getSubReg();
73 }
74
75 if (!hreg)
76 return 0;
77
78 if (TargetRegisterInfo::isVirtualRegister(hreg))
79 return sub == hsub ? hreg : 0;
80
81 const TargetRegisterClass *rc = mri.getRegClass(reg);
82
83 // Only allow physreg hints in rc.
84 if (sub == 0)
85 return rc->contains(hreg) ? hreg : 0;
86
87 // reg:sub should match the physreg hreg.
88 return tri.getMatchingSuperReg(hreg, sub, rc);
Lang Hamesa937f222009-12-14 06:49:42 +000089}
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +000090
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +000091// Check if all values in LI are rematerializable
92static 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();
96 I != E; ++I) {
97 const VNInfo *VNI = *I;
98 if (VNI->isUnused())
99 continue;
100 if (VNI->isPHIDef())
101 return false;
102
103 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
104 assert(MI && "Dead valno in interval");
105
106 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
107 return false;
108 }
109 return true;
110}
111
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000112void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000113 MachineRegisterInfo &mri = MF.getRegInfo();
114 const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo();
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000115 MachineBasicBlock *mbb = 0;
116 MachineLoop *loop = 0;
117 unsigned loopDepth = 0;
118 bool isExiting = false;
119 float totalWeight = 0;
120 SmallPtrSet<MachineInstr*, 8> visited;
121
122 // Find the best physreg hist and the best virtreg hint.
123 float bestPhys = 0, bestVirt = 0;
124 unsigned hintPhys = 0, hintVirt = 0;
125
126 // Don't recompute a target specific hint.
127 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
128
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000129 // Don't recompute spill weight for an unspillable register.
130 bool Spillable = li.isSpillable();
131
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000132 for (MachineRegisterInfo::reg_iterator I = mri.reg_begin(li.reg);
133 MachineInstr *mi = I.skipInstruction();) {
134 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
135 continue;
136 if (!visited.insert(mi))
137 continue;
138
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000139 float weight = 1.0f;
140 if (Spillable) {
141 // Get loop info for mi.
142 if (mi->getParent() != mbb) {
143 mbb = mi->getParent();
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000144 loop = Loops.getLoopFor(mbb);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000145 loopDepth = loop ? loop->getLoopDepth() : 0;
146 isExiting = loop ? loop->isLoopExiting(mbb) : false;
147 }
148
149 // Calculate instr weight.
150 bool reads, writes;
151 tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
152 weight = LiveIntervals::getSpillWeight(writes, reads, loopDepth);
153
154 // Give extra weight to what looks like a loop induction variable update.
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000155 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000156 weight *= 3;
157
158 totalWeight += weight;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000159 }
160
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000161 // Get allocation hints from copies.
162 if (noHint || !mi->isCopy())
163 continue;
164 unsigned hint = copyHint(mi, li.reg, tri, mri);
165 if (!hint)
166 continue;
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000167 float hweight = Hint[hint] += weight;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000168 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000169 if (hweight > bestPhys && LIS.isAllocatable(hint))
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000170 bestPhys = hweight, hintPhys = hint;
171 } else {
172 if (hweight > bestVirt)
173 bestVirt = hweight, hintVirt = hint;
174 }
175 }
176
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000177 Hint.clear();
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000178
179 // Always prefer the physreg hint.
180 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
181 mri.setRegAllocationHint(li.reg, 0, hint);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000182 // Weakly boost the spill weight of hinted registers.
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000183 totalWeight *= 1.01F;
184 }
185
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000186 // If the live interval was already unspillable, leave it that way.
187 if (!Spillable)
188 return;
189
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000190 // Mark li as unspillable if all live ranges are tiny.
Jakob Stoklund Olesenf5497fb2011-05-16 23:50:05 +0000191 if (li.isZeroLength(LIS.getSlotIndexes())) {
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000192 li.markNotSpillable();
193 return;
194 }
195
196 // If all of the definitions of the interval are re-materializable,
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +0000197 // it is a preferred candidate for spilling.
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000198 // FIXME: this gets much more complicated once we support non-trivial
199 // re-materialization.
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +0000200 if (isRematerializable(li, LIS, *MF.getTarget().getInstrInfo()))
201 totalWeight *= 0.5F;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000202
Jakob Stoklund Oleseneb9f0402011-02-14 23:15:38 +0000203 li.weight = normalizeSpillWeight(totalWeight, li.getSize());
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000204}