blob: b03c325c895fa29e3c50d560238b8131a39333d4 [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"
Benjamin Kramer4eed7562013-06-17 19:00:36 +000015#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Lang Hamesa937f222009-12-14 06:49:42 +000016#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>();
Benjamin Kramer4eed7562013-06-17 19:00:36 +000037 au.addRequired<MachineBlockFrequencyInfo>();
Lang Hamesa937f222009-12-14 06:49:42 +000038 au.addRequired<MachineLoopInfo>();
39 au.setPreservesAll();
40 MachineFunctionPass::getAnalysisUsage(au);
41}
42
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000043bool CalculateSpillWeights::runOnMachineFunction(MachineFunction &MF) {
Lang Hamesa937f222009-12-14 06:49:42 +000044
David Greene7ed6dd62009-12-24 00:39:02 +000045 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
David Blaikie986d76d2012-08-22 17:18:53 +000046 << "********** Function: " << MF.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();
Benjamin Kramer4eed7562013-06-17 19:00:36 +000050 VirtRegAuxInfo VRAI(MF, LIS, getAnalysis<MachineLoopInfo>(),
51 getAnalysis<MachineBlockFrequencyInfo>());
Jakob Stoklund Olesend67582e2012-06-20 21:25:05 +000052 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
53 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
54 if (MRI.reg_nodbg_empty(Reg))
55 continue;
56 VRAI.CalculateWeightAndHint(LIS.getInterval(Reg));
Lang Hamesa937f222009-12-14 06:49:42 +000057 }
Lang Hamesa937f222009-12-14 06:49:42 +000058 return false;
59}
60
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +000061// Return the preferred allocation register for reg, given a COPY instruction.
62static 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();
70 } else {
71 sub = mi->getOperand(1).getSubReg();
72 hreg = mi->getOperand(0).getReg();
73 hsub = mi->getOperand(0).getSubReg();
74 }
75
76 if (!hreg)
77 return 0;
78
79 if (TargetRegisterInfo::isVirtualRegister(hreg))
80 return sub == hsub ? hreg : 0;
81
82 const TargetRegisterClass *rc = mri.getRegClass(reg);
83
84 // Only allow physreg hints in rc.
85 if (sub == 0)
86 return rc->contains(hreg) ? hreg : 0;
87
88 // reg:sub should match the physreg hreg.
89 return tri.getMatchingSuperReg(hreg, sub, rc);
Lang Hamesa937f222009-12-14 06:49:42 +000090}
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +000091
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +000092// Check if all values in LI are rematerializable
93static 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();
97 I != E; ++I) {
98 const VNInfo *VNI = *I;
99 if (VNI->isUnused())
100 continue;
101 if (VNI->isPHIDef())
102 return false;
103
104 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
105 assert(MI && "Dead valno in interval");
106
107 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
108 return false;
109 }
110 return true;
111}
112
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000113void
114VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000115 MachineRegisterInfo &mri = MF.getRegInfo();
116 const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo();
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000117 MachineBasicBlock *mbb = 0;
118 MachineLoop *loop = 0;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000119 bool isExiting = false;
120 float totalWeight = 0;
121 SmallPtrSet<MachineInstr*, 8> visited;
122
Nadav Rotem6e01dcb2013-04-06 04:24:12 +0000123 // Find the best physreg hint and the best virtreg hint.
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000124 float bestPhys = 0, bestVirt = 0;
125 unsigned hintPhys = 0, hintVirt = 0;
126
127 // Don't recompute a target specific hint.
128 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
129
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000130 // Don't recompute spill weight for an unspillable register.
131 bool Spillable = li.isSpillable();
132
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000133 for (MachineRegisterInfo::reg_iterator I = mri.reg_begin(li.reg);
134 MachineInstr *mi = I.skipInstruction();) {
135 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
136 continue;
137 if (!visited.insert(mi))
138 continue;
139
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000140 float weight = 1.0f;
141 if (Spillable) {
142 // Get loop info for mi.
143 if (mi->getParent() != mbb) {
144 mbb = mi->getParent();
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000145 loop = Loops.getLoopFor(mbb);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000146 isExiting = loop ? loop->isLoopExiting(mbb) : false;
147 }
148
149 // Calculate instr weight.
150 bool reads, writes;
151 tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000152 weight = LiveIntervals::getSpillWeight(
153 writes, reads, MBFI.getBlockFreq(mi->getParent()));
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000154
155 // Give extra weight to what looks like a loop induction variable update.
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000156 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000157 weight *= 3;
158
159 totalWeight += weight;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000160 }
161
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000162 // Get allocation hints from copies.
163 if (noHint || !mi->isCopy())
164 continue;
165 unsigned hint = copyHint(mi, li.reg, tri, mri);
166 if (!hint)
167 continue;
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000168 float hweight = Hint[hint] += weight;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000169 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
Jakob Stoklund Olesen79004762012-10-15 22:14:34 +0000170 if (hweight > bestPhys && mri.isAllocatable(hint))
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000171 bestPhys = hweight, hintPhys = hint;
172 } else {
173 if (hweight > bestVirt)
174 bestVirt = hweight, hintVirt = hint;
175 }
176 }
177
Jakob Stoklund Olesen1394e6d2011-04-26 18:52:36 +0000178 Hint.clear();
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000179
180 // Always prefer the physreg hint.
181 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
182 mri.setRegAllocationHint(li.reg, 0, hint);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000183 // Weakly boost the spill weight of hinted registers.
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000184 totalWeight *= 1.01F;
185 }
186
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000187 // If the live interval was already unspillable, leave it that way.
188 if (!Spillable)
189 return;
190
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000191 // Mark li as unspillable if all live ranges are tiny.
Jakob Stoklund Olesenf5497fb2011-05-16 23:50:05 +0000192 if (li.isZeroLength(LIS.getSlotIndexes())) {
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000193 li.markNotSpillable();
194 return;
195 }
196
197 // If all of the definitions of the interval are re-materializable,
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +0000198 // it is a preferred candidate for spilling.
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000199 // FIXME: this gets much more complicated once we support non-trivial
200 // re-materialization.
Jakob Stoklund Olesen3dfd59b2012-06-05 01:06:12 +0000201 if (isRematerializable(li, LIS, *MF.getTarget().getInstrInfo()))
202 totalWeight *= 0.5F;
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000203
Jakob Stoklund Oleseneb9f0402011-02-14 23:15:38 +0000204 li.weight = normalizeSpillWeight(totalWeight, li.getSize());
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000205}