blob: 1db96c9f1eb807f75d5013b88f2614c5598c0f5c [file] [log] [blame]
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +00001//===- CalcSpillWeights.cpp -----------------------------------------------===//
Lang Hamesd17e2962009-12-14 06:49:42 +00002//
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
Lang Hamesd17e2962009-12-14 06:49:42 +000010#include "llvm/CodeGen/CalcSpillWeights.h"
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000011#include "llvm/ADT/SmallPtrSet.h"
12#include "llvm/CodeGen/LiveInterval.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000013#include "llvm/CodeGen/LiveIntervalAnalysis.h"
14#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000015#include "llvm/CodeGen/MachineInstr.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000016#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000017#include "llvm/CodeGen/MachineOperand.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/CodeGen/VirtRegMap.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000024#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenko4f81cdd2017-09-29 21:55:49 +000025#include <cassert>
26#include <tuple>
27
Lang Hamesd17e2962009-12-14 06:49:42 +000028using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "calcspillweights"
31
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +000032void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +000033 MachineFunction &MF,
Robert Lougher11a44b72015-08-10 11:59:44 +000034 VirtRegMap *VRM,
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +000035 const MachineLoopInfo &MLI,
Arnaud A. de Grandmaisonf5f040f2013-11-11 19:56:14 +000036 const MachineBlockFrequencyInfo &MBFI,
37 VirtRegAuxInfo::NormalizingFn norm) {
David Greenee40730d2009-12-24 00:39:02 +000038 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +000039 << "********** Function: " << MF.getName() << '\n');
Lang Hamesd17e2962009-12-14 06:49:42 +000040
Jakob Stoklund Olesena1f43dc2012-06-20 21:25:05 +000041 MachineRegisterInfo &MRI = MF.getRegInfo();
Robert Lougher11a44b72015-08-10 11:59:44 +000042 VirtRegAuxInfo VRAI(MF, LIS, VRM, MLI, MBFI, norm);
Jakob Stoklund Olesena1f43dc2012-06-20 21:25:05 +000043 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
44 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
45 if (MRI.reg_nodbg_empty(Reg))
46 continue;
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +000047 VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg));
Lang Hamesd17e2962009-12-14 06:49:42 +000048 }
Lang Hamesd17e2962009-12-14 06:49:42 +000049}
50
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +000051// Return the preferred allocation register for reg, given a COPY instruction.
52static unsigned copyHint(const MachineInstr *mi, unsigned reg,
53 const TargetRegisterInfo &tri,
54 const MachineRegisterInfo &mri) {
55 unsigned sub, hreg, hsub;
56 if (mi->getOperand(0).getReg() == reg) {
57 sub = mi->getOperand(0).getSubReg();
58 hreg = mi->getOperand(1).getReg();
59 hsub = mi->getOperand(1).getSubReg();
60 } else {
61 sub = mi->getOperand(1).getSubReg();
62 hreg = mi->getOperand(0).getReg();
63 hsub = mi->getOperand(0).getSubReg();
64 }
65
66 if (!hreg)
67 return 0;
68
69 if (TargetRegisterInfo::isVirtualRegister(hreg))
70 return sub == hsub ? hreg : 0;
71
72 const TargetRegisterClass *rc = mri.getRegClass(reg);
73
74 // Only allow physreg hints in rc.
75 if (sub == 0)
76 return rc->contains(hreg) ? hreg : 0;
77
78 // reg:sub should match the physreg hreg.
79 return tri.getMatchingSuperReg(hreg, sub, rc);
Lang Hamesd17e2962009-12-14 06:49:42 +000080}
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +000081
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +000082// Check if all values in LI are rematerializable
83static bool isRematerializable(const LiveInterval &LI,
84 const LiveIntervals &LIS,
Robert Lougher11a44b72015-08-10 11:59:44 +000085 VirtRegMap *VRM,
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +000086 const TargetInstrInfo &TII) {
Robert Lougher11a44b72015-08-10 11:59:44 +000087 unsigned Reg = LI.reg;
88 unsigned Original = VRM ? VRM->getOriginal(Reg) : 0;
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +000089 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
90 I != E; ++I) {
91 const VNInfo *VNI = *I;
92 if (VNI->isUnused())
93 continue;
94 if (VNI->isPHIDef())
95 return false;
96
97 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
98 assert(MI && "Dead valno in interval");
99
Robert Lougher11a44b72015-08-10 11:59:44 +0000100 // Trace copies introduced by live range splitting. The inline
101 // spiller can rematerialize through these copies, so the spill
102 // weight must reflect this.
103 if (VRM) {
104 while (MI->isFullCopy()) {
105 // The copy destination must match the interval register.
106 if (MI->getOperand(0).getReg() != Reg)
107 return false;
108
109 // Get the source register.
110 Reg = MI->getOperand(1).getReg();
111
112 // If the original (pre-splitting) registers match this
113 // copy came from a split.
114 if (!TargetRegisterInfo::isVirtualRegister(Reg) ||
115 VRM->getOriginal(Reg) != Original)
116 return false;
117
118 // Follow the copy live-in value.
119 const LiveInterval &SrcLI = LIS.getInterval(Reg);
120 LiveQueryResult SrcQ = SrcLI.Query(VNI->def);
121 VNI = SrcQ.valueIn();
122 assert(VNI && "Copy from non-existing value");
123 if (VNI->isPHIDef())
124 return false;
125 MI = LIS.getInstructionFromIndex(VNI->def);
126 assert(MI && "Dead valno in interval");
127 }
128 }
129
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000130 if (!TII.isTriviallyReMaterializable(*MI, LIS.getAliasAnalysis()))
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +0000131 return false;
132 }
133 return true;
134}
135
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000136void
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000137VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000138 MachineRegisterInfo &mri = MF.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000139 const TargetRegisterInfo &tri = *MF.getSubtarget().getRegisterInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000140 MachineBasicBlock *mbb = nullptr;
141 MachineLoop *loop = nullptr;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000142 bool isExiting = false;
143 float totalWeight = 0;
Arnaud A. de Grandmaison829dd812014-11-04 20:51:24 +0000144 unsigned numInstr = 0; // Number of instructions using li
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000145 SmallPtrSet<MachineInstr*, 8> visited;
146
Nadav Rotemc4bd84c2013-04-06 04:24:12 +0000147 // Find the best physreg hint and the best virtreg hint.
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000148 float bestPhys = 0, bestVirt = 0;
149 unsigned hintPhys = 0, hintVirt = 0;
150
151 // Don't recompute a target specific hint.
152 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
153
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000154 // Don't recompute spill weight for an unspillable register.
155 bool Spillable = li.isSpillable();
156
Owen Andersonabb90c92014-03-13 06:02:25 +0000157 for (MachineRegisterInfo::reg_instr_iterator
158 I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end();
159 I != E; ) {
160 MachineInstr *mi = &*(I++);
Arnaud A. de Grandmaison829dd812014-11-04 20:51:24 +0000161 numInstr++;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000162 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
163 continue;
David Blaikie70573dc2014-11-19 07:49:26 +0000164 if (!visited.insert(mi).second)
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000165 continue;
166
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000167 float weight = 1.0f;
168 if (Spillable) {
169 // Get loop info for mi.
170 if (mi->getParent() != mbb) {
171 mbb = mi->getParent();
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000172 loop = Loops.getLoopFor(mbb);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000173 isExiting = loop ? loop->isLoopExiting(mbb) : false;
174 }
175
176 // Calculate instr weight.
177 bool reads, writes;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000178 std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
Duncan P. N. Exon Smithbe8f8c42016-02-27 20:14:29 +0000179 weight = LiveIntervals::getSpillWeight(writes, reads, &MBFI, *mi);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000180
181 // Give extra weight to what looks like a loop induction variable update.
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000182 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000183 weight *= 3;
184
185 totalWeight += weight;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000186 }
187
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000188 // Get allocation hints from copies.
189 if (noHint || !mi->isCopy())
190 continue;
191 unsigned hint = copyHint(mi, li.reg, tri, mri);
192 if (!hint)
193 continue;
Duncan P. N. Exon Smith7af34322014-04-21 17:57:01 +0000194 // Force hweight onto the stack so that x86 doesn't add hidden precision,
195 // making the comparison incorrectly pass (i.e., 1 > 1 == true??).
196 //
197 // FIXME: we probably shouldn't use floats at all.
198 volatile float hweight = Hint[hint] += weight;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000199 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000200 if (hweight > bestPhys && mri.isAllocatable(hint)) {
201 bestPhys = hweight;
202 hintPhys = hint;
203 }
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000204 } else {
Richard Trieu7a083812016-02-18 22:09:30 +0000205 if (hweight > bestVirt) {
206 bestVirt = hweight;
207 hintVirt = hint;
208 }
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000209 }
210 }
211
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000212 Hint.clear();
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000213
214 // Always prefer the physreg hint.
215 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
216 mri.setRegAllocationHint(li.reg, 0, hint);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000217 // Weakly boost the spill weight of hinted registers.
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000218 totalWeight *= 1.01F;
219 }
220
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000221 // If the live interval was already unspillable, leave it that way.
222 if (!Spillable)
223 return;
224
Andrew Kaylor12244882016-02-08 22:52:51 +0000225 // Mark li as unspillable if all live ranges are tiny and the interval
226 // is not live at any reg mask. If the interval is live at a reg mask
227 // spilling may be required.
228 if (li.isZeroLength(LIS.getSlotIndexes()) &&
229 !li.isLiveAtIndexes(LIS.getRegMaskSlots())) {
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000230 li.markNotSpillable();
231 return;
232 }
233
234 // If all of the definitions of the interval are re-materializable,
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +0000235 // it is a preferred candidate for spilling.
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000236 // FIXME: this gets much more complicated once we support non-trivial
237 // re-materialization.
Robert Lougher11a44b72015-08-10 11:59:44 +0000238 if (isRematerializable(li, LIS, VRM, *MF.getSubtarget().getInstrInfo()))
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +0000239 totalWeight *= 0.5F;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000240
Arnaud A. de Grandmaison829dd812014-11-04 20:51:24 +0000241 li.weight = normalize(totalWeight, li.getSize(), numInstr);
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000242}