blob: e31013266bc7b39209de7fefc3add8671a8b8445 [file] [log] [blame]
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001//===-------- InlineSpiller.cpp - Insert spills and restores inline -------===//
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// The inline spiller modifies the machine function directly instead of
11// inserting spills and restores in VirtRegMap.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +000015#include "Spiller.h"
Benjamin Kramerbc6666b2013-05-23 15:42:57 +000016#include "llvm/ADT/SetVector.h"
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000017#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +000018#include "llvm/ADT/TinyPtrVector.h"
Jakob Stoklund Olesen868dd4e2010-11-10 23:55:56 +000019#include "llvm/Analysis/AliasAnalysis.h"
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000021#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesene2c340c2010-10-26 00:11:35 +000022#include "llvm/CodeGen/LiveStackAnalysis.h"
Benjamin Kramere2a1d892013-06-17 19:00:36 +000023#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Manman Renc9355602014-03-21 21:46:24 +000024#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
David Blaikie0252265b2013-06-16 20:34:15 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineInstrBundle.h"
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +000030#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000032#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenbceb9e52011-09-15 21:06:00 +000033#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +000037
38using namespace llvm;
39
Chandler Carruth1b9dde02014-04-22 02:02:50 +000040#define DEBUG_TYPE "regalloc"
41
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000042STATISTIC(NumSpilledRanges, "Number of spilled live ranges");
Jakob Stoklund Olesen37eb6962011-09-15 17:54:28 +000043STATISTIC(NumSnippets, "Number of spilled snippets");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000044STATISTIC(NumSpills, "Number of spills inserted");
Jakob Stoklund Olesen37eb6962011-09-15 17:54:28 +000045STATISTIC(NumSpillsRemoved, "Number of spills removed");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000046STATISTIC(NumReloads, "Number of reloads inserted");
Jakob Stoklund Olesen37eb6962011-09-15 17:54:28 +000047STATISTIC(NumReloadsRemoved, "Number of reloads removed");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000048STATISTIC(NumFolded, "Number of folded stack accesses");
49STATISTIC(NumFoldedLoads, "Number of folded loads");
50STATISTIC(NumRemats, "Number of rematerialized defs for spilling");
Jakob Stoklund Olesen37eb6962011-09-15 17:54:28 +000051STATISTIC(NumOmitReloadSpill, "Number of omitted spills of reloads");
52STATISTIC(NumHoists, "Number of hoisted spills");
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000053
Jakob Stoklund Olesenbceb9e52011-09-15 21:06:00 +000054static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden,
55 cl::desc("Disable inline spill hoisting"));
56
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +000057namespace {
58class InlineSpiller : public Spiller {
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +000059 MachineFunction &MF;
60 LiveIntervals &LIS;
61 LiveStacks &LSS;
62 AliasAnalysis *AA;
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +000063 MachineDominatorTree &MDT;
64 MachineLoopInfo &Loops;
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +000065 VirtRegMap &VRM;
66 MachineFrameInfo &MFI;
67 MachineRegisterInfo &MRI;
68 const TargetInstrInfo &TII;
69 const TargetRegisterInfo &TRI;
Benjamin Kramere2a1d892013-06-17 19:00:36 +000070 const MachineBlockFrequencyInfo &MBFI;
Jakob Stoklund Olesenbde96ad2010-06-30 23:03:52 +000071
72 // Variables that are valid during spill(), but used by multiple methods.
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +000073 LiveRangeEdit *Edit;
Jakob Stoklund Olesene4663452011-03-26 22:16:41 +000074 LiveInterval *StackInt;
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +000075 int StackSlot;
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +000076 unsigned Original;
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +000077
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +000078 // All registers to spill to StackSlot, including the main register.
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +000079 SmallVector<unsigned, 8> RegsToSpill;
80
81 // All COPY instructions to/from snippets.
82 // They are ignored since both operands refer to the same stack slot.
83 SmallPtrSet<MachineInstr*, 8> SnippetCopies;
84
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000085 // Values that failed to remat at some point.
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +000086 SmallPtrSet<VNInfo*, 8> UsedValues;
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +000087
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +000088public:
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +000089 // Information about a value that was defined by a copy from a sibling
90 // register.
91 struct SibValueInfo {
92 // True when all reaching defs were reloads: No spill is necessary.
93 bool AllDefsAreReloads;
94
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +000095 // True when value is defined by an original PHI not from splitting.
96 bool DefByOrigPHI;
97
Jakob Stoklund Olesene8339b22011-09-16 00:03:33 +000098 // True when the COPY defining this value killed its source.
99 bool KillsSource;
100
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000101 // The preferred register to spill.
102 unsigned SpillReg;
103
104 // The value of SpillReg that should be spilled.
105 VNInfo *SpillVNI;
106
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000107 // The block where SpillVNI should be spilled. Currently, this must be the
108 // block containing SpillVNI->def.
109 MachineBasicBlock *SpillMBB;
110
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000111 // A defining instruction that is not a sibling copy or a reload, or NULL.
112 // This can be used as a template for rematerialization.
113 MachineInstr *DefMI;
114
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000115 // List of values that depend on this one. These values are actually the
116 // same, but live range splitting has placed them in different registers,
117 // or SSA update needed to insert PHI-defs to preserve SSA form. This is
118 // copies of the current value and phi-kills. Usually only phi-kills cause
119 // more than one dependent value.
120 TinyPtrVector<VNInfo*> Deps;
121
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000122 SibValueInfo(unsigned Reg, VNInfo *VNI)
Jakob Stoklund Olesene8339b22011-09-16 00:03:33 +0000123 : AllDefsAreReloads(true), DefByOrigPHI(false), KillsSource(false),
Craig Topperc0196b12014-04-14 00:51:57 +0000124 SpillReg(Reg), SpillVNI(VNI), SpillMBB(nullptr), DefMI(nullptr) {}
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000125
126 // Returns true when a def has been found.
127 bool hasDef() const { return DefByOrigPHI || DefMI; }
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000128 };
129
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000130private:
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000131 // Values in RegsToSpill defined by sibling copies.
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000132 typedef DenseMap<VNInfo*, SibValueInfo> SibValueMap;
133 SibValueMap SibValues;
134
135 // Dead defs generated during spilling.
136 SmallVector<MachineInstr*, 8> DeadDefs;
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000137
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000138 ~InlineSpiller() override {}
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +0000139
140public:
Eric Christopherd9134482014-08-04 21:25:23 +0000141 InlineSpiller(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
142 : MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
143 LSS(pass.getAnalysis<LiveStacks>()),
Chandler Carruth7b560d42015-09-09 17:55:00 +0000144 AA(&pass.getAnalysis<AAResultsWrapperPass>().getAAResults()),
Eric Christopherd9134482014-08-04 21:25:23 +0000145 MDT(pass.getAnalysis<MachineDominatorTree>()),
146 Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm),
147 MFI(*mf.getFrameInfo()), MRI(mf.getRegInfo()),
Eric Christopherfc6de422014-08-05 02:39:49 +0000148 TII(*mf.getSubtarget().getInstrInfo()),
149 TRI(*mf.getSubtarget().getRegisterInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000150 MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()) {}
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +0000151
Craig Topper4584cd52014-03-07 09:26:03 +0000152 void spill(LiveRangeEdit &) override;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000153
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000154private:
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000155 bool isSnippet(const LiveInterval &SnipLI);
156 void collectRegsToSpill();
157
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000158 bool isRegToSpill(unsigned Reg) {
159 return std::find(RegsToSpill.begin(),
160 RegsToSpill.end(), Reg) != RegsToSpill.end();
161 }
162
163 bool isSibling(unsigned Reg);
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000164 MachineInstr *traceSiblingValue(unsigned, VNInfo*, VNInfo*);
Craig Topperc0196b12014-04-14 00:51:57 +0000165 void propagateSiblingValue(SibValueMap::iterator, VNInfo *VNI = nullptr);
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000166 void analyzeSiblingValues();
167
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000168 bool hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI);
Jakob Stoklund Olesen39488642011-03-20 05:44:55 +0000169 void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000170
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000171 void markValueUsed(LiveInterval*, VNInfo*);
172 bool reMaterializeFor(LiveInterval&, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000173 void reMaterializeAll();
174
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000175 bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +0000176 bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> >,
Craig Topperc0196b12014-04-14 00:51:57 +0000177 MachineInstr *LoadMI = nullptr);
Mark Lacey9d8103d2013-08-14 23:50:16 +0000178 void insertReload(unsigned VReg, SlotIndex, MachineBasicBlock::iterator MI);
179 void insertSpill(unsigned VReg, bool isKill, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000180
181 void spillAroundUses(unsigned Reg);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000182 void spillAll();
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +0000183};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000184}
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +0000185
186namespace llvm {
Lang Hamescdd90772014-11-06 19:12:38 +0000187
188Spiller::~Spiller() { }
189void Spiller::anchor() { }
190
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000191Spiller *createInlineSpiller(MachineFunctionPass &pass,
192 MachineFunction &mf,
193 VirtRegMap &vrm) {
194 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +0000195}
Lang Hamescdd90772014-11-06 19:12:38 +0000196
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000197}
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +0000198
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000199//===----------------------------------------------------------------------===//
200// Snippets
201//===----------------------------------------------------------------------===//
202
203// When spilling a virtual register, we also spill any snippets it is connected
204// to. The snippets are small live ranges that only have a single real use,
205// leftovers from live range splitting. Spilling them enables memory operand
206// folding or tightens the live range around the single use.
207//
208// This minimizes register pressure and maximizes the store-to-load distance for
209// spill slots which can be important in tight loops.
210
211/// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
212/// otherwise return 0.
213static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
Rafael Espindola070f96c2011-06-30 21:15:52 +0000214 if (!MI->isFullCopy())
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000215 return 0;
216 if (MI->getOperand(0).getReg() == Reg)
217 return MI->getOperand(1).getReg();
218 if (MI->getOperand(1).getReg() == Reg)
219 return MI->getOperand(0).getReg();
220 return 0;
221}
222
223/// isSnippet - Identify if a live interval is a snippet that should be spilled.
224/// It is assumed that SnipLI is a virtual register with the same original as
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000225/// Edit->getReg().
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000226bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000227 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000228
229 // A snippet is a tiny live range with only a single instruction using it
230 // besides copies to/from Reg or spills/fills. We accept:
231 //
232 // %snip = COPY %Reg / FILL fi#
233 // %snip = USE %snip
234 // %Reg = COPY %snip / SPILL %snip, fi#
235 //
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000236 if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000237 return false;
238
Craig Topperc0196b12014-04-14 00:51:57 +0000239 MachineInstr *UseMI = nullptr;
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000240
241 // Check that all uses satisfy our criteria.
Owen Andersonabb90c92014-03-13 06:02:25 +0000242 for (MachineRegisterInfo::reg_instr_nodbg_iterator
243 RI = MRI.reg_instr_nodbg_begin(SnipLI.reg),
244 E = MRI.reg_instr_nodbg_end(); RI != E; ) {
245 MachineInstr *MI = &*(RI++);
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000246
247 // Allow copies to/from Reg.
248 if (isFullCopyOf(MI, Reg))
249 continue;
250
251 // Allow stack slot loads.
252 int FI;
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000253 if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000254 continue;
255
256 // Allow stack slot stores.
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000257 if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000258 continue;
259
260 // Allow a single additional instruction.
261 if (UseMI && MI != UseMI)
262 return false;
263 UseMI = MI;
264 }
265 return true;
266}
267
268/// collectRegsToSpill - Collect live range snippets that only have a single
269/// real use.
270void InlineSpiller::collectRegsToSpill() {
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000271 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000272
273 // Main register always spills.
274 RegsToSpill.assign(1, Reg);
275 SnippetCopies.clear();
276
277 // Snippets all have the same original, so there can't be any for an original
278 // register.
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000279 if (Original == Reg)
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000280 return;
281
Owen Andersonabb90c92014-03-13 06:02:25 +0000282 for (MachineRegisterInfo::reg_instr_iterator
283 RI = MRI.reg_instr_begin(Reg), E = MRI.reg_instr_end(); RI != E; ) {
284 MachineInstr *MI = &*(RI++);
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000285 unsigned SnipReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000286 if (!isSibling(SnipReg))
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000287 continue;
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000288 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000289 if (!isSnippet(SnipLI))
290 continue;
291 SnippetCopies.insert(MI);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000292 if (isRegToSpill(SnipReg))
293 continue;
294 RegsToSpill.push_back(SnipReg);
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000295 DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000296 ++NumSnippets;
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000297 }
298}
299
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000300
301//===----------------------------------------------------------------------===//
302// Sibling Values
303//===----------------------------------------------------------------------===//
304
305// After live range splitting, some values to be spilled may be defined by
306// copies from sibling registers. We trace the sibling copies back to the
307// original value if it still exists. We need it for rematerialization.
308//
309// Even when the value can't be rematerialized, we still want to determine if
310// the value has already been spilled, or we may want to hoist the spill from a
311// loop.
312
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000313bool InlineSpiller::isSibling(unsigned Reg) {
314 return TargetRegisterInfo::isVirtualRegister(Reg) &&
315 VRM.getOriginal(Reg) == Original;
316}
317
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000318#ifndef NDEBUG
319static raw_ostream &operator<<(raw_ostream &OS,
320 const InlineSpiller::SibValueInfo &SVI) {
321 OS << "spill " << PrintReg(SVI.SpillReg) << ':'
322 << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def;
323 if (SVI.SpillMBB)
324 OS << " in BB#" << SVI.SpillMBB->getNumber();
325 if (SVI.AllDefsAreReloads)
326 OS << " all-reloads";
327 if (SVI.DefByOrigPHI)
328 OS << " orig-phi";
Jakob Stoklund Olesene8339b22011-09-16 00:03:33 +0000329 if (SVI.KillsSource)
330 OS << " kill";
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000331 OS << " deps[";
Craig Topper73275a22015-12-24 05:20:40 +0000332 for (VNInfo *Dep : SVI.Deps)
333 OS << ' ' << Dep->id << '@' << Dep->def;
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000334 OS << " ]";
335 if (SVI.DefMI)
336 OS << " def: " << *SVI.DefMI;
337 else
338 OS << '\n';
339 return OS;
340}
341#endif
342
343/// propagateSiblingValue - Propagate the value in SVI to dependents if it is
344/// known. Otherwise remember the dependency for later.
345///
Benjamin Kramerbc6666b2013-05-23 15:42:57 +0000346/// @param SVIIter SibValues entry to propagate.
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000347/// @param VNI Dependent value, or NULL to propagate to all saved dependents.
Benjamin Kramerbc6666b2013-05-23 15:42:57 +0000348void InlineSpiller::propagateSiblingValue(SibValueMap::iterator SVIIter,
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000349 VNInfo *VNI) {
Benjamin Kramerbc6666b2013-05-23 15:42:57 +0000350 SibValueMap::value_type *SVI = &*SVIIter;
351
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000352 // When VNI is non-NULL, add it to SVI's deps, and only propagate to that.
353 TinyPtrVector<VNInfo*> FirstDeps;
354 if (VNI) {
355 FirstDeps.push_back(VNI);
356 SVI->second.Deps.push_back(VNI);
357 }
358
359 // Has the value been completely determined yet? If not, defer propagation.
360 if (!SVI->second.hasDef())
361 return;
362
Benjamin Kramerbc6666b2013-05-23 15:42:57 +0000363 // Work list of values to propagate.
364 SmallSetVector<SibValueMap::value_type *, 8> WorkList;
365 WorkList.insert(SVI);
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000366
367 do {
368 SVI = WorkList.pop_back_val();
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000369 TinyPtrVector<VNInfo*> *Deps = VNI ? &FirstDeps : &SVI->second.Deps;
Craig Topperc0196b12014-04-14 00:51:57 +0000370 VNI = nullptr;
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000371
372 SibValueInfo &SV = SVI->second;
373 if (!SV.SpillMBB)
374 SV.SpillMBB = LIS.getMBBFromIndex(SV.SpillVNI->def);
375
376 DEBUG(dbgs() << " prop to " << Deps->size() << ": "
377 << SVI->first->id << '@' << SVI->first->def << ":\t" << SV);
378
379 assert(SV.hasDef() && "Propagating undefined value");
380
381 // Should this value be propagated as a preferred spill candidate? We don't
382 // propagate values of registers that are about to spill.
Jakob Stoklund Olesenbceb9e52011-09-15 21:06:00 +0000383 bool PropSpill = !DisableHoisting && !isRegToSpill(SV.SpillReg);
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000384 unsigned SpillDepth = ~0u;
385
Craig Topper73275a22015-12-24 05:20:40 +0000386 for (VNInfo *Dep : *Deps) {
387 SibValueMap::iterator DepSVI = SibValues.find(Dep);
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000388 assert(DepSVI != SibValues.end() && "Dependent value not in SibValues");
389 SibValueInfo &DepSV = DepSVI->second;
390 if (!DepSV.SpillMBB)
391 DepSV.SpillMBB = LIS.getMBBFromIndex(DepSV.SpillVNI->def);
392
393 bool Changed = false;
394
395 // Propagate defining instruction.
396 if (!DepSV.hasDef()) {
397 Changed = true;
398 DepSV.DefMI = SV.DefMI;
399 DepSV.DefByOrigPHI = SV.DefByOrigPHI;
400 }
401
402 // Propagate AllDefsAreReloads. For PHI values, this computes an AND of
403 // all predecessors.
404 if (!SV.AllDefsAreReloads && DepSV.AllDefsAreReloads) {
405 Changed = true;
406 DepSV.AllDefsAreReloads = false;
407 }
408
409 // Propagate best spill value.
410 if (PropSpill && SV.SpillVNI != DepSV.SpillVNI) {
411 if (SV.SpillMBB == DepSV.SpillMBB) {
412 // DepSV is in the same block. Hoist when dominated.
Jakob Stoklund Olesene8339b22011-09-16 00:03:33 +0000413 if (DepSV.KillsSource && SV.SpillVNI->def < DepSV.SpillVNI->def) {
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000414 // This is an alternative def earlier in the same MBB.
415 // Hoist the spill as far as possible in SpillMBB. This can ease
416 // register pressure:
417 //
418 // x = def
419 // y = use x
420 // s = copy x
421 //
422 // Hoisting the spill of s to immediately after the def removes the
423 // interference between x and y:
424 //
425 // x = def
426 // spill x
427 // y = use x<kill>
428 //
Jakob Stoklund Olesene8339b22011-09-16 00:03:33 +0000429 // This hoist only helps when the DepSV copy kills its source.
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000430 Changed = true;
431 DepSV.SpillReg = SV.SpillReg;
432 DepSV.SpillVNI = SV.SpillVNI;
433 DepSV.SpillMBB = SV.SpillMBB;
434 }
435 } else {
436 // DepSV is in a different block.
437 if (SpillDepth == ~0u)
438 SpillDepth = Loops.getLoopDepth(SV.SpillMBB);
439
440 // Also hoist spills to blocks with smaller loop depth, but make sure
441 // that the new value dominates. Non-phi dependents are always
442 // dominated, phis need checking.
Manman Renc9355602014-03-21 21:46:24 +0000443
444 const BranchProbability MarginProb(4, 5); // 80%
445 // Hoist a spill to outer loop if there are multiple dependents (it
446 // can be beneficial if more than one dependents are hoisted) or
447 // if DepSV (the hoisting source) is hotter than SV (the hoisting
448 // destination) (we add a 80% margin to bias a little towards
449 // loop depth).
450 bool HoistCondition =
451 (MBFI.getBlockFreq(DepSV.SpillMBB) >=
452 (MBFI.getBlockFreq(SV.SpillMBB) * MarginProb)) ||
453 Deps->size() > 1;
454
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000455 if ((Loops.getLoopDepth(DepSV.SpillMBB) > SpillDepth) &&
Manman Renc9355602014-03-21 21:46:24 +0000456 HoistCondition &&
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000457 (!DepSVI->first->isPHIDef() ||
458 MDT.dominates(SV.SpillMBB, DepSV.SpillMBB))) {
459 Changed = true;
460 DepSV.SpillReg = SV.SpillReg;
461 DepSV.SpillVNI = SV.SpillVNI;
462 DepSV.SpillMBB = SV.SpillMBB;
463 }
464 }
465 }
466
467 if (!Changed)
468 continue;
469
470 // Something changed in DepSVI. Propagate to dependents.
Benjamin Kramerbc6666b2013-05-23 15:42:57 +0000471 WorkList.insert(&*DepSVI);
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000472
473 DEBUG(dbgs() << " update " << DepSVI->first->id << '@'
474 << DepSVI->first->def << " to:\t" << DepSV);
475 }
476 } while (!WorkList.empty());
477}
478
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000479/// traceSiblingValue - Trace a value that is about to be spilled back to the
480/// real defining instructions by looking through sibling copies. Always stay
481/// within the range of OrigVNI so the registers are known to carry the same
482/// value.
483///
484/// Determine if the value is defined by all reloads, so spilling isn't
485/// necessary - the value is already in the stack slot.
486///
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000487/// Return a defining instruction that may be a candidate for rematerialization.
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000488///
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000489MachineInstr *InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
490 VNInfo *OrigVNI) {
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000491 // Check if a cached value already exists.
492 SibValueMap::iterator SVI;
493 bool Inserted;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000494 std::tie(SVI, Inserted) =
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000495 SibValues.insert(std::make_pair(UseVNI, SibValueInfo(UseReg, UseVNI)));
496 if (!Inserted) {
497 DEBUG(dbgs() << "Cached value " << PrintReg(UseReg) << ':'
498 << UseVNI->id << '@' << UseVNI->def << ' ' << SVI->second);
499 return SVI->second.DefMI;
500 }
501
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000502 DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
503 << UseVNI->id << '@' << UseVNI->def << '\n');
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000504
505 // List of (Reg, VNI) that have been inserted into SibValues, but need to be
506 // processed.
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000507 SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList;
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000508 WorkList.push_back(std::make_pair(UseReg, UseVNI));
509
Patrik Hagglundcb06a362014-12-11 10:40:17 +0000510 LiveInterval &OrigLI = LIS.getInterval(Original);
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000511 do {
512 unsigned Reg;
513 VNInfo *VNI;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000514 std::tie(Reg, VNI) = WorkList.pop_back_val();
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000515 DEBUG(dbgs() << " " << PrintReg(Reg) << ':' << VNI->id << '@' << VNI->def
516 << ":\t");
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000517
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000518 // First check if this value has already been computed.
519 SVI = SibValues.find(VNI);
520 assert(SVI != SibValues.end() && "Missing SibValues entry");
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000521
522 // Trace through PHI-defs created by live range splitting.
523 if (VNI->isPHIDef()) {
Patrik Hagglundcb06a362014-12-11 10:40:17 +0000524 // Stop at original PHIs. We don't know the value at the
525 // predecessors. Look up the VNInfo for the current definition
526 // in OrigLI, to properly determine whether or not this phi was
527 // added by splitting.
528 if (VNI->def == OrigLI.getVNInfoAt(VNI->def)->def) {
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000529 DEBUG(dbgs() << "orig phi value\n");
530 SVI->second.DefByOrigPHI = true;
531 SVI->second.AllDefsAreReloads = false;
532 propagateSiblingValue(SVI);
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000533 continue;
534 }
Jakob Stoklund Olesen07b35032011-09-15 16:41:12 +0000535
536 // This is a PHI inserted by live range splitting. We could trace the
537 // live-out value from predecessor blocks, but that search can be very
538 // expensive if there are many predecessors and many more PHIs as
539 // generated by tail-dup when it sees an indirectbr. Instead, look at
540 // all the non-PHI defs that have the same value as OrigVNI. They must
541 // jointly dominate VNI->def. This is not optimal since VNI may actually
542 // be jointly dominated by a smaller subset of defs, so there is a change
543 // we will miss a AllDefsAreReloads optimization.
544
545 // Separate all values dominated by OrigVNI into PHIs and non-PHIs.
546 SmallVector<VNInfo*, 8> PHIs, NonPHIs;
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000547 LiveInterval &LI = LIS.getInterval(Reg);
Jakob Stoklund Olesen07b35032011-09-15 16:41:12 +0000548
549 for (LiveInterval::vni_iterator VI = LI.vni_begin(), VE = LI.vni_end();
550 VI != VE; ++VI) {
551 VNInfo *VNI2 = *VI;
552 if (VNI2->isUnused())
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000553 continue;
Jakob Stoklund Olesen07b35032011-09-15 16:41:12 +0000554 if (!OrigLI.containsOneValue() &&
555 OrigLI.getVNInfoAt(VNI2->def) != OrigVNI)
556 continue;
557 if (VNI2->isPHIDef() && VNI2->def != OrigVNI->def)
558 PHIs.push_back(VNI2);
559 else
560 NonPHIs.push_back(VNI2);
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000561 }
Jakob Stoklund Olesen07b35032011-09-15 16:41:12 +0000562 DEBUG(dbgs() << "split phi value, checking " << PHIs.size()
563 << " phi-defs, and " << NonPHIs.size()
564 << " non-phi/orig defs\n");
565
566 // Create entries for all the PHIs. Don't add them to the worklist, we
567 // are processing all of them in one go here.
Craig Topper73275a22015-12-24 05:20:40 +0000568 for (VNInfo *PHI : PHIs)
569 SibValues.insert(std::make_pair(PHI, SibValueInfo(Reg, PHI)));
Jakob Stoklund Olesen07b35032011-09-15 16:41:12 +0000570
571 // Add every PHI as a dependent of all the non-PHIs.
Craig Topper73275a22015-12-24 05:20:40 +0000572 for (VNInfo *NonPHI : NonPHIs) {
Jakob Stoklund Olesen07b35032011-09-15 16:41:12 +0000573 // Known value? Try an insertion.
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000574 std::tie(SVI, Inserted) =
Jakob Stoklund Olesen07b35032011-09-15 16:41:12 +0000575 SibValues.insert(std::make_pair(NonPHI, SibValueInfo(Reg, NonPHI)));
576 // Add all the PHIs as dependents of NonPHI.
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000577 SVI->second.Deps.insert(SVI->second.Deps.end(), PHIs.begin(),
578 PHIs.end());
Jakob Stoklund Olesen07b35032011-09-15 16:41:12 +0000579 // This is the first time we see NonPHI, add it to the worklist.
580 if (Inserted)
581 WorkList.push_back(std::make_pair(Reg, NonPHI));
582 else
583 // Propagate to all inserted PHIs, not just VNI.
584 propagateSiblingValue(SVI);
585 }
586
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000587 // Next work list item.
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000588 continue;
589 }
590
591 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
592 assert(MI && "Missing def");
593
594 // Trace through sibling copies.
595 if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000596 if (isSibling(SrcReg)) {
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000597 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
Matthias Braun88dd0ab2013-10-10 21:28:52 +0000598 LiveQueryResult SrcQ = SrcLI.Query(VNI->def);
Jakob Stoklund Olesen2aeead42012-05-20 02:44:33 +0000599 assert(SrcQ.valueIn() && "Copy from non-existing value");
Jakob Stoklund Olesene8339b22011-09-16 00:03:33 +0000600 // Check if this COPY kills its source.
Jakob Stoklund Olesen2aeead42012-05-20 02:44:33 +0000601 SVI->second.KillsSource = SrcQ.isKill();
602 VNInfo *SrcVNI = SrcQ.valueIn();
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000603 DEBUG(dbgs() << "copy of " << PrintReg(SrcReg) << ':'
Jakob Stoklund Olesene8339b22011-09-16 00:03:33 +0000604 << SrcVNI->id << '@' << SrcVNI->def
605 << " kill=" << unsigned(SVI->second.KillsSource) << '\n');
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000606 // Known sibling source value? Try an insertion.
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000607 std::tie(SVI, Inserted) = SibValues.insert(
608 std::make_pair(SrcVNI, SibValueInfo(SrcReg, SrcVNI)));
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000609 // This is the first time we see Src, add it to the worklist.
610 if (Inserted)
611 WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
612 propagateSiblingValue(SVI, VNI);
613 // Next work list item.
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000614 continue;
615 }
616 }
617
618 // Track reachable reloads.
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000619 SVI->second.DefMI = MI;
620 SVI->second.SpillMBB = MI->getParent();
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000621 int FI;
622 if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000623 DEBUG(dbgs() << "reload\n");
624 propagateSiblingValue(SVI);
625 // Next work list item.
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000626 continue;
627 }
628
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000629 // Potential remat candidate.
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000630 DEBUG(dbgs() << "def " << *MI);
631 SVI->second.AllDefsAreReloads = false;
632 propagateSiblingValue(SVI);
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000633 } while (!WorkList.empty());
634
Logan Chien64f361e2012-09-01 12:11:41 +0000635 // Look up the value we were looking for. We already did this lookup at the
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000636 // top of the function, but SibValues may have been invalidated.
637 SVI = SibValues.find(UseVNI);
638 assert(SVI != SibValues.end() && "Didn't compute requested info");
639 DEBUG(dbgs() << " traced to:\t" << SVI->second);
640 return SVI->second.DefMI;
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000641}
642
643/// analyzeSiblingValues - Trace values defined by sibling copies back to
644/// something that isn't a sibling copy.
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000645///
646/// Keep track of values that may be rematerializable.
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000647void InlineSpiller::analyzeSiblingValues() {
648 SibValues.clear();
649
650 // No siblings at all?
651 if (Edit->getReg() == Original)
652 return;
653
654 LiveInterval &OrigLI = LIS.getInterval(Original);
Craig Topper73275a22015-12-24 05:20:40 +0000655 for (unsigned Reg : RegsToSpill) {
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000656 LiveInterval &LI = LIS.getInterval(Reg);
657 for (LiveInterval::const_vni_iterator VI = LI.vni_begin(),
658 VE = LI.vni_end(); VI != VE; ++VI) {
659 VNInfo *VNI = *VI;
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000660 if (VNI->isUnused())
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000661 continue;
Craig Topperc0196b12014-04-14 00:51:57 +0000662 MachineInstr *DefMI = nullptr;
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000663 if (!VNI->isPHIDef()) {
664 DefMI = LIS.getInstructionFromIndex(VNI->def);
665 assert(DefMI && "No defining instruction");
666 }
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000667 // Check possible sibling copies.
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +0000668 if (VNI->isPHIDef() || DefMI->isCopy()) {
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000669 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesenbbad3bc2011-07-05 15:38:41 +0000670 assert(OrigVNI && "Def outside original live range");
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000671 if (OrigVNI->def != VNI->def)
672 DefMI = traceSiblingValue(Reg, VNI, OrigVNI);
673 }
Pete Cooper2bde2f42012-04-02 22:22:53 +0000674 if (DefMI && Edit->checkRematerializable(VNI, DefMI, AA)) {
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +0000675 DEBUG(dbgs() << "Value " << PrintReg(Reg) << ':' << VNI->id << '@'
676 << VNI->def << " may remat from " << *DefMI);
677 }
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +0000678 }
679 }
680}
681
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000682/// hoistSpill - Given a sibling copy that defines a value to be spilled, insert
683/// a spill at a better location.
684bool InlineSpiller::hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI) {
685 SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000686 VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot());
687 assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy");
Jakob Stoklund Olesenec9b4a62011-04-30 06:42:21 +0000688 SibValueMap::iterator I = SibValues.find(VNI);
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000689 if (I == SibValues.end())
690 return false;
691
692 const SibValueInfo &SVI = I->second;
693
694 // Let the normal folding code deal with the boring case.
695 if (!SVI.AllDefsAreReloads && SVI.SpillVNI == VNI)
696 return false;
697
Jakob Stoklund Olesenec9b4a62011-04-30 06:42:21 +0000698 // SpillReg may have been deleted by remat and DCE.
699 if (!LIS.hasInterval(SVI.SpillReg)) {
700 DEBUG(dbgs() << "Stale interval: " << PrintReg(SVI.SpillReg) << '\n');
701 SibValues.erase(I);
702 return false;
703 }
704
705 LiveInterval &SibLI = LIS.getInterval(SVI.SpillReg);
706 if (!SibLI.containsValue(SVI.SpillVNI)) {
707 DEBUG(dbgs() << "Stale value: " << PrintReg(SVI.SpillReg) << '\n');
708 SibValues.erase(I);
709 return false;
710 }
711
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000712 // Conservatively extend the stack slot range to the range of the original
713 // value. We may be able to do better with stack slot coloring by being more
714 // careful here.
Jakob Stoklund Olesene4663452011-03-26 22:16:41 +0000715 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000716 LiveInterval &OrigLI = LIS.getInterval(Original);
717 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Jakob Stoklund Olesene4663452011-03-26 22:16:41 +0000718 StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
Jakob Stoklund Olesen86985072011-03-19 23:02:47 +0000719 DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
Jakob Stoklund Olesene4663452011-03-26 22:16:41 +0000720 << *StackInt << '\n');
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000721
722 // Already spilled everywhere.
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000723 if (SVI.AllDefsAreReloads) {
Jakob Stoklund Olesen278bf022011-09-09 18:11:41 +0000724 DEBUG(dbgs() << "\tno spill needed: " << SVI);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000725 ++NumOmitReloadSpill;
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000726 return true;
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000727 }
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000728 // We are going to spill SVI.SpillVNI immediately after its def, so clear out
729 // any later spills of the same value.
Jakob Stoklund Olesenec9b4a62011-04-30 06:42:21 +0000730 eliminateRedundantSpills(SibLI, SVI.SpillVNI);
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000731
732 MachineBasicBlock *MBB = LIS.getMBBFromIndex(SVI.SpillVNI->def);
733 MachineBasicBlock::iterator MII;
734 if (SVI.SpillVNI->isPHIDef())
735 MII = MBB->SkipPHIsAndLabels(MBB->begin());
736 else {
Jakob Stoklund Olesenec9b4a62011-04-30 06:42:21 +0000737 MachineInstr *DefMI = LIS.getInstructionFromIndex(SVI.SpillVNI->def);
738 assert(DefMI && "Defining instruction disappeared");
739 MII = DefMI;
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000740 ++MII;
741 }
742 // Insert spill without kill flag immediately after def.
Jakob Stoklund Olesene4663452011-03-26 22:16:41 +0000743 TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot,
744 MRI.getRegClass(SVI.SpillReg), &TRI);
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000745 --MII; // Point to store instruction.
746 LIS.InsertMachineInstrInMaps(MII);
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000747 DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000748
Jakob Stoklund Olesen37eb6962011-09-15 17:54:28 +0000749 ++NumSpills;
750 ++NumHoists;
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000751 return true;
752}
753
Jakob Stoklund Olesen39488642011-03-20 05:44:55 +0000754/// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
755/// redundant spills of this value in SLI.reg and sibling copies.
756void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
Jakob Stoklund Olesene55003f2011-03-20 05:44:58 +0000757 assert(VNI && "Missing value");
Jakob Stoklund Olesen39488642011-03-20 05:44:55 +0000758 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
759 WorkList.push_back(std::make_pair(&SLI, VNI));
Jakob Stoklund Olesene4663452011-03-26 22:16:41 +0000760 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000761
762 do {
Jakob Stoklund Olesen39488642011-03-20 05:44:55 +0000763 LiveInterval *LI;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000764 std::tie(LI, VNI) = WorkList.pop_back_val();
Jakob Stoklund Olesen39488642011-03-20 05:44:55 +0000765 unsigned Reg = LI->reg;
Jakob Stoklund Olesenec9b4a62011-04-30 06:42:21 +0000766 DEBUG(dbgs() << "Checking redundant spills for "
767 << VNI->id << '@' << VNI->def << " in " << *LI << '\n');
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000768
769 // Regs to spill are taken care of.
770 if (isRegToSpill(Reg))
771 continue;
772
773 // Add all of VNI's live range to StackInt.
Jakob Stoklund Olesene4663452011-03-26 22:16:41 +0000774 StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
775 DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000776
777 // Find all spills and copies of VNI.
Owen Andersonabb90c92014-03-13 06:02:25 +0000778 for (MachineRegisterInfo::use_instr_nodbg_iterator
779 UI = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end();
780 UI != E; ) {
781 MachineInstr *MI = &*(UI++);
Evan Cheng7f8e5632011-12-07 07:15:52 +0000782 if (!MI->isCopy() && !MI->mayStore())
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000783 continue;
784 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen39488642011-03-20 05:44:55 +0000785 if (LI->getVNInfoAt(Idx) != VNI)
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000786 continue;
787
788 // Follow sibling copies down the dominator tree.
789 if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
790 if (isSibling(DstReg)) {
791 LiveInterval &DstLI = LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000792 VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000793 assert(DstVNI && "Missing defined value");
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000794 assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
Jakob Stoklund Olesen39488642011-03-20 05:44:55 +0000795 WorkList.push_back(std::make_pair(&DstLI, DstVNI));
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000796 }
797 continue;
798 }
799
800 // Erase spills.
801 int FI;
802 if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
803 DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << *MI);
804 // eliminateDeadDefs won't normally remove stores, so switch opcode.
805 MI->setDesc(TII.get(TargetOpcode::KILL));
806 DeadDefs.push_back(MI);
Jakob Stoklund Olesen37eb6962011-09-15 17:54:28 +0000807 ++NumSpillsRemoved;
808 --NumSpills;
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +0000809 }
810 }
811 } while (!WorkList.empty());
812}
813
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000814
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000815//===----------------------------------------------------------------------===//
816// Rematerialization
817//===----------------------------------------------------------------------===//
818
819/// markValueUsed - Remember that VNI failed to rematerialize, so its defining
820/// instruction cannot be eliminated. See through snippet copies
821void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
822 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
823 WorkList.push_back(std::make_pair(LI, VNI));
824 do {
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000825 std::tie(LI, VNI) = WorkList.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +0000826 if (!UsedValues.insert(VNI).second)
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000827 continue;
828
829 if (VNI->isPHIDef()) {
830 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
Craig Topper73275a22015-12-24 05:20:40 +0000831 for (MachineBasicBlock *P : MBB->predecessors()) {
832 VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(P));
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000833 if (PVNI)
834 WorkList.push_back(std::make_pair(LI, PVNI));
835 }
836 continue;
837 }
838
839 // Follow snippet copies.
840 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
841 if (!SnippetCopies.count(MI))
842 continue;
843 LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
844 assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000845 VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000846 assert(SnipVNI && "Snippet undefined before copy");
847 WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
848 } while (!WorkList.empty());
849}
850
851/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
852bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg,
853 MachineBasicBlock::iterator MI) {
Patrik Hagglund296acbf2014-09-01 11:04:07 +0000854
855 // Analyze instruction
856 SmallVector<std::pair<MachineInstr *, unsigned>, 8> Ops;
857 MIBundleOperands::VirtRegInfo RI =
858 MIBundleOperands(MI).analyzeVirtReg(VirtReg.reg, &Ops);
859
860 if (!RI.Reads)
861 return false;
862
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000863 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
Jakob Stoklund Olesenc0dd3da2011-07-18 05:31:59 +0000864 VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000865
866 if (!ParentVNI) {
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000867 DEBUG(dbgs() << "\tadding <undef> flags: ");
868 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
869 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen0ed9ebc2011-03-29 17:47:02 +0000870 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000871 MO.setIsUndef();
872 }
873 DEBUG(dbgs() << UseIdx << '\t' << *MI);
874 return true;
875 }
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000876
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000877 if (SnippetCopies.count(MI))
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000878 return false;
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +0000879
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000880 // Use an OrigVNI from traceSiblingValue when ParentVNI is a sibling copy.
881 LiveRangeEdit::Remat RM(ParentVNI);
882 SibValueMap::const_iterator SibI = SibValues.find(ParentVNI);
883 if (SibI != SibValues.end())
884 RM.OrigMI = SibI->second.DefMI;
Pete Cooper2bde2f42012-04-02 22:22:53 +0000885 if (!Edit->canRematerializeAt(RM, UseIdx, false)) {
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000886 markValueUsed(&VirtReg, ParentVNI);
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000887 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
888 return false;
889 }
890
Jakob Stoklund Olesen0ed9ebc2011-03-29 17:47:02 +0000891 // If the instruction also writes VirtReg.reg, it had better not require the
892 // same register for uses and defs.
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +0000893 if (RI.Tied) {
894 markValueUsed(&VirtReg, ParentVNI);
895 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
896 return false;
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000897 }
898
Jakob Stoklund Olesen3b2966d2010-12-18 03:04:14 +0000899 // Before rematerializing into a register for a single instruction, try to
900 // fold a load into the instruction. That avoids allocating a new register.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000901 if (RM.OrigMI->canFoldAsLoad() &&
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +0000902 foldMemoryOperand(Ops, RM.OrigMI)) {
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000903 Edit->markRematerialized(RM.ParentVNI);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000904 ++NumFoldedLoads;
Jakob Stoklund Olesen3b2966d2010-12-18 03:04:14 +0000905 return true;
906 }
907
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000908 // Alocate a new register for the remat.
Mark Lacey9d8103d2013-08-14 23:50:16 +0000909 unsigned NewVReg = Edit->createFrom(Original);
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000910
911 // Finally we can rematerialize OrigMI before MI.
Mark Lacey9d8103d2013-08-14 23:50:16 +0000912 SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewVReg, RM,
Pete Cooper2bde2f42012-04-02 22:22:53 +0000913 TRI);
Mark Lacey9d8103d2013-08-14 23:50:16 +0000914 (void)DefIdx;
Jakob Stoklund Olesenc6a20412011-02-08 19:33:55 +0000915 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000916 << *LIS.getInstructionFromIndex(DefIdx));
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000917
918 // Replace operands
Craig Topper73275a22015-12-24 05:20:40 +0000919 for (const auto &OpPair : Ops) {
920 MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
Jakob Stoklund Olesen0ed9ebc2011-03-29 17:47:02 +0000921 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
Mark Lacey9d8103d2013-08-14 23:50:16 +0000922 MO.setReg(NewVReg);
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000923 MO.setIsKill();
924 }
925 }
Mark Lacey9d8103d2013-08-14 23:50:16 +0000926 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI << '\n');
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000927
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000928 ++NumRemats;
Jakob Stoklund Olesenbde96ad2010-06-30 23:03:52 +0000929 return true;
930}
931
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000932/// reMaterializeAll - Try to rematerialize as many uses as possible,
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000933/// and trim the live ranges after.
934void InlineSpiller::reMaterializeAll() {
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000935 // analyzeSiblingValues has already tested all relevant defining instructions.
Pete Cooper2bde2f42012-04-02 22:22:53 +0000936 if (!Edit->anyRematerializable(AA))
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000937 return;
938
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +0000939 UsedValues.clear();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000940
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000941 // Try to remat before all uses of snippets.
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000942 bool anyRemat = false;
Craig Topper73275a22015-12-24 05:20:40 +0000943 for (unsigned Reg : RegsToSpill) {
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000944 LiveInterval &LI = LIS.getInterval(Reg);
Patrik Hagglund296acbf2014-09-01 11:04:07 +0000945 for (MachineRegisterInfo::reg_bundle_iterator
946 RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
947 RegI != E; ) {
948 MachineInstr *MI = &*(RegI++);
949
950 // Debug values are not allowed to affect codegen.
951 if (MI->isDebugValue())
952 continue;
953
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000954 anyRemat |= reMaterializeFor(LI, MI);
Owen Andersonabb90c92014-03-13 06:02:25 +0000955 }
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000956 }
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000957 if (!anyRemat)
958 return;
959
960 // Remove any values that were completely rematted.
Craig Topper73275a22015-12-24 05:20:40 +0000961 for (unsigned Reg : RegsToSpill) {
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000962 LiveInterval &LI = LIS.getInterval(Reg);
963 for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
964 I != E; ++I) {
965 VNInfo *VNI = *I;
Jakob Stoklund Olesenadd79c62011-03-29 17:47:00 +0000966 if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
Jakob Stoklund Olesencf6c5c92010-07-02 19:54:40 +0000967 continue;
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000968 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
969 MI->addRegisterDead(Reg, &TRI);
970 if (!MI->allDefsAreDead())
971 continue;
972 DEBUG(dbgs() << "All defs dead: " << *MI);
973 DeadDefs.push_back(MI);
Jakob Stoklund Olesencf6c5c92010-07-02 19:54:40 +0000974 }
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +0000975 }
Jakob Stoklund Olesenadd79c62011-03-29 17:47:00 +0000976
977 // Eliminate dead code after remat. Note that some snippet copies may be
978 // deleted here.
979 if (DeadDefs.empty())
980 return;
981 DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
Pete Cooper2bde2f42012-04-02 22:22:53 +0000982 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
Jakob Stoklund Olesenadd79c62011-03-29 17:47:00 +0000983
984 // Get rid of deleted and empty intervals.
Benjamin Kramer391f5a62013-05-05 11:29:14 +0000985 unsigned ResultPos = 0;
Craig Topper73275a22015-12-24 05:20:40 +0000986 for (unsigned Reg : RegsToSpill) {
Benjamin Kramer391f5a62013-05-05 11:29:14 +0000987 if (!LIS.hasInterval(Reg))
988 continue;
989
990 LiveInterval &LI = LIS.getInterval(Reg);
991 if (LI.empty()) {
992 Edit->eraseVirtReg(Reg);
Jakob Stoklund Olesenadd79c62011-03-29 17:47:00 +0000993 continue;
994 }
Benjamin Kramer391f5a62013-05-05 11:29:14 +0000995
996 RegsToSpill[ResultPos++] = Reg;
Jakob Stoklund Olesenadd79c62011-03-29 17:47:00 +0000997 }
Benjamin Kramer391f5a62013-05-05 11:29:14 +0000998 RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
Jakob Stoklund Olesenadd79c62011-03-29 17:47:00 +0000999 DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n");
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +00001000}
1001
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001002
1003//===----------------------------------------------------------------------===//
1004// Spilling
1005//===----------------------------------------------------------------------===//
1006
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +00001007/// If MI is a load or store of StackSlot, it can be removed.
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001008bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
Jakob Stoklund Olesen7fd49052010-08-04 22:35:11 +00001009 int FI = 0;
Jakob Stoklund Olesen37eb6962011-09-15 17:54:28 +00001010 unsigned InstrReg = TII.isLoadFromStackSlot(MI, FI);
1011 bool IsLoad = InstrReg;
1012 if (!IsLoad)
1013 InstrReg = TII.isStoreToStackSlot(MI, FI);
Jakob Stoklund Olesen7fd49052010-08-04 22:35:11 +00001014
1015 // We have a stack access. Is it the right register and slot?
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +00001016 if (InstrReg != Reg || FI != StackSlot)
Jakob Stoklund Olesen7fd49052010-08-04 22:35:11 +00001017 return false;
1018
1019 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +00001020 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen7fd49052010-08-04 22:35:11 +00001021 MI->eraseFromParent();
Jakob Stoklund Olesen37eb6962011-09-15 17:54:28 +00001022
1023 if (IsLoad) {
1024 ++NumReloadsRemoved;
1025 --NumReloads;
1026 } else {
1027 ++NumSpillsRemoved;
1028 --NumSpills;
1029 }
1030
Jakob Stoklund Olesen7fd49052010-08-04 22:35:11 +00001031 return true;
1032}
1033
Mark Lacey9d8103d2013-08-14 23:50:16 +00001034#if !defined(NDEBUG)
1035// Dump the range of instructions from B to E with their slot indexes.
1036static void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B,
1037 MachineBasicBlock::iterator E,
1038 LiveIntervals const &LIS,
1039 const char *const header,
1040 unsigned VReg =0) {
1041 char NextLine = '\n';
1042 char SlotIndent = '\t';
1043
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001044 if (std::next(B) == E) {
Mark Lacey9d8103d2013-08-14 23:50:16 +00001045 NextLine = ' ';
1046 SlotIndent = ' ';
1047 }
1048
1049 dbgs() << '\t' << header << ": " << NextLine;
1050
1051 for (MachineBasicBlock::iterator I = B; I != E; ++I) {
1052 SlotIndex Idx = LIS.getInstructionIndex(I).getRegSlot();
1053
1054 // If a register was passed in and this instruction has it as a
1055 // destination that is marked as an early clobber, print the
1056 // early-clobber slot index.
1057 if (VReg) {
1058 MachineOperand *MO = I->findRegisterDefOperand(VReg);
1059 if (MO && MO->isEarlyClobber())
1060 Idx = Idx.getRegSlot(true);
1061 }
1062
1063 dbgs() << SlotIndent << Idx << '\t' << *I;
1064 }
1065}
1066#endif
1067
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001068/// foldMemoryOperand - Try folding stack slot references in Ops into their
1069/// instructions.
1070///
1071/// @param Ops Operand indices from analyzeVirtReg().
Jakob Stoklund Olesen3b2966d2010-12-18 03:04:14 +00001072/// @param LoadMI Load instruction to use instead of stack slot when non-null.
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001073/// @return True on success.
1074bool InlineSpiller::
1075foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops,
1076 MachineInstr *LoadMI) {
1077 if (Ops.empty())
1078 return false;
1079 // Don't attempt folding in bundles.
1080 MachineInstr *MI = Ops.front().first;
1081 if (Ops.back().first != MI || MI->isBundled())
1082 return false;
1083
Jakob Stoklund Olesenc94c9672011-09-15 18:22:52 +00001084 bool WasCopy = MI->isCopy();
Jakob Stoklund Oleseneef48b62011-11-10 00:17:03 +00001085 unsigned ImpReg = 0;
1086
Philip Reames0365f1a2014-12-01 22:52:56 +00001087 bool SpillSubRegs = (MI->getOpcode() == TargetOpcode::STATEPOINT ||
1088 MI->getOpcode() == TargetOpcode::PATCHPOINT ||
Andrew Trick10d5be42013-11-17 01:36:23 +00001089 MI->getOpcode() == TargetOpcode::STACKMAP);
1090
Jakob Stoklund Olesen8656a452010-07-01 00:13:04 +00001091 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
1092 // operands.
1093 SmallVector<unsigned, 8> FoldOps;
Craig Topper73275a22015-12-24 05:20:40 +00001094 for (const auto &OpPair : Ops) {
1095 unsigned Idx = OpPair.second;
1096 assert(MI == OpPair.first && "Instruction conflict during operand folding");
Jakob Stoklund Olesen8656a452010-07-01 00:13:04 +00001097 MachineOperand &MO = MI->getOperand(Idx);
Jakob Stoklund Oleseneef48b62011-11-10 00:17:03 +00001098 if (MO.isImplicit()) {
1099 ImpReg = MO.getReg();
Jakob Stoklund Olesen8656a452010-07-01 00:13:04 +00001100 continue;
Jakob Stoklund Oleseneef48b62011-11-10 00:17:03 +00001101 }
Jakob Stoklund Olesen8656a452010-07-01 00:13:04 +00001102 // FIXME: Teach targets to deal with subregs.
Andrew Trick10d5be42013-11-17 01:36:23 +00001103 if (!SpillSubRegs && MO.getSubReg())
Jakob Stoklund Olesen8656a452010-07-01 00:13:04 +00001104 return false;
Jakob Stoklund Olesenc6a20412011-02-08 19:33:55 +00001105 // We cannot fold a load instruction into a def.
1106 if (LoadMI && MO.isDef())
1107 return false;
Jakob Stoklund Olesen8656a452010-07-01 00:13:04 +00001108 // Tied use operands should not be passed to foldMemoryOperand.
1109 if (!MI->isRegTiedToDefOperand(Idx))
1110 FoldOps.push_back(Idx);
1111 }
1112
Mark Lacey9d8103d2013-08-14 23:50:16 +00001113 MachineInstrSpan MIS(MI);
1114
Jakob Stoklund Olesen3b2966d2010-12-18 03:04:14 +00001115 MachineInstr *FoldMI =
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +00001116 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
1117 : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
Jakob Stoklund Olesen8656a452010-07-01 00:13:04 +00001118 if (!FoldMI)
1119 return false;
Andrew Trick5749b8b2013-06-21 18:33:26 +00001120
1121 // Remove LIS for any dead defs in the original MI not in FoldMI.
1122 for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
1123 if (!MO->isReg())
1124 continue;
1125 unsigned Reg = MO->getReg();
1126 if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) ||
1127 MRI.isReserved(Reg)) {
1128 continue;
1129 }
Andrew Trickdfacda32014-01-07 07:31:10 +00001130 // Skip non-Defs, including undef uses and internal reads.
1131 if (MO->isUse())
1132 continue;
Andrew Trick5749b8b2013-06-21 18:33:26 +00001133 MIBundleOperands::PhysRegInfo RI =
1134 MIBundleOperands(FoldMI).analyzePhysReg(Reg, &TRI);
Matthias Braun60d69e22015-12-11 19:42:09 +00001135 if (RI.FullyDefined)
Andrew Trick5749b8b2013-06-21 18:33:26 +00001136 continue;
1137 // FoldMI does not define this physreg. Remove the LI segment.
1138 assert(MO->isDead() && "Cannot fold physreg def");
Matthias Brauncfb8ad22015-01-21 18:50:21 +00001139 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
1140 LIS.removePhysRegDefAt(Reg, Idx);
Andrew Trick5749b8b2013-06-21 18:33:26 +00001141 }
Mark Lacey9d8103d2013-08-14 23:50:16 +00001142
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +00001143 LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
Jakob Stoklund Olesenbd953d12010-07-09 17:29:08 +00001144 MI->eraseFromParent();
Jakob Stoklund Oleseneef48b62011-11-10 00:17:03 +00001145
Mark Lacey9d8103d2013-08-14 23:50:16 +00001146 // Insert any new instructions other than FoldMI into the LIS maps.
1147 assert(!MIS.empty() && "Unexpected empty span of instructions!");
Craig Topper73275a22015-12-24 05:20:40 +00001148 for (MachineInstr &MI : MIS)
1149 if (&MI != FoldMI)
1150 LIS.InsertMachineInstrInMaps(&MI);
Mark Lacey9d8103d2013-08-14 23:50:16 +00001151
Jakob Stoklund Oleseneef48b62011-11-10 00:17:03 +00001152 // TII.foldMemoryOperand may have left some implicit operands on the
1153 // instruction. Strip them.
1154 if (ImpReg)
1155 for (unsigned i = FoldMI->getNumOperands(); i; --i) {
1156 MachineOperand &MO = FoldMI->getOperand(i - 1);
1157 if (!MO.isReg() || !MO.isImplicit())
1158 break;
1159 if (MO.getReg() == ImpReg)
1160 FoldMI->RemoveOperand(i - 1);
1161 }
1162
Mark Lacey9d8103d2013-08-14 23:50:16 +00001163 DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS,
1164 "folded"));
1165
Jakob Stoklund Olesenc94c9672011-09-15 18:22:52 +00001166 if (!WasCopy)
1167 ++NumFolded;
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001168 else if (Ops.front().second == 0)
Jakob Stoklund Olesenc94c9672011-09-15 18:22:52 +00001169 ++NumSpills;
1170 else
1171 ++NumReloads;
Jakob Stoklund Olesen8656a452010-07-01 00:13:04 +00001172 return true;
1173}
1174
Mark Lacey9d8103d2013-08-14 23:50:16 +00001175void InlineSpiller::insertReload(unsigned NewVReg,
Jakob Stoklund Olesen9f294a92011-04-18 20:23:27 +00001176 SlotIndex Idx,
Jakob Stoklund Olesenbde96ad2010-06-30 23:03:52 +00001177 MachineBasicBlock::iterator MI) {
1178 MachineBasicBlock &MBB = *MI->getParent();
Mark Lacey9d8103d2013-08-14 23:50:16 +00001179
1180 MachineInstrSpan MIS(MI);
1181 TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot,
1182 MRI.getRegClass(NewVReg), &TRI);
1183
1184 LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI);
1185
1186 DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload",
1187 NewVReg));
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +00001188 ++NumReloads;
Jakob Stoklund Olesenbde96ad2010-06-30 23:03:52 +00001189}
1190
Mark Lacey9d8103d2013-08-14 23:50:16 +00001191/// insertSpill - Insert a spill of NewVReg after MI.
1192void InlineSpiller::insertSpill(unsigned NewVReg, bool isKill,
1193 MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenbde96ad2010-06-30 23:03:52 +00001194 MachineBasicBlock &MBB = *MI->getParent();
Mark Lacey9d8103d2013-08-14 23:50:16 +00001195
1196 MachineInstrSpan MIS(MI);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001197 TII.storeRegToStackSlot(MBB, std::next(MI), NewVReg, isKill, StackSlot,
Mark Lacey9d8103d2013-08-14 23:50:16 +00001198 MRI.getRegClass(NewVReg), &TRI);
1199
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001200 LIS.InsertMachineInstrRangeInMaps(std::next(MI), MIS.end());
Mark Lacey9d8103d2013-08-14 23:50:16 +00001201
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001202 DEBUG(dumpMachineInstrRangeWithSlotIndex(std::next(MI), MIS.end(), LIS,
Mark Lacey9d8103d2013-08-14 23:50:16 +00001203 "spill"));
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +00001204 ++NumSpills;
Jakob Stoklund Olesenbde96ad2010-06-30 23:03:52 +00001205}
1206
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001207/// spillAroundUses - insert spill code around each use of Reg.
1208void InlineSpiller::spillAroundUses(unsigned Reg) {
Jakob Stoklund Olesen31a0b5e2011-05-11 18:25:10 +00001209 DEBUG(dbgs() << "spillAroundUses " << PrintReg(Reg) << '\n');
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +00001210 LiveInterval &OldLI = LIS.getInterval(Reg);
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001211
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001212 // Iterate over instructions using Reg.
Owen Andersonabb90c92014-03-13 06:02:25 +00001213 for (MachineRegisterInfo::reg_bundle_iterator
1214 RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
1215 RegI != E; ) {
Owen Andersonec5d4802014-03-14 05:02:18 +00001216 MachineInstr *MI = &*(RegI++);
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001217
Jakob Stoklund Olesencf6c5c92010-07-02 19:54:40 +00001218 // Debug values are not allowed to affect codegen.
1219 if (MI->isDebugValue()) {
1220 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +00001221 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantlc31ec1c2013-07-10 16:56:47 +00001222 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001223 const MDNode *Var = MI->getDebugVariable();
1224 const MDNode *Expr = MI->getDebugExpression();
Jakob Stoklund Olesencf6c5c92010-07-02 19:54:40 +00001225 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +00001226 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
1227 MachineBasicBlock *MBB = MI->getParent();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001228 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00001229 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +00001230 BuildMI(*MBB, MBB->erase(MI), DL, TII.get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001231 .addFrameIndex(StackSlot)
1232 .addImm(Offset)
1233 .addMetadata(Var)
1234 .addMetadata(Expr);
Jakob Stoklund Olesencf6c5c92010-07-02 19:54:40 +00001235 continue;
1236 }
1237
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001238 // Ignore copies to/from snippets. We'll delete them.
1239 if (SnippetCopies.count(MI))
1240 continue;
1241
Jakob Stoklund Olesen7fd49052010-08-04 22:35:11 +00001242 // Stack slot accesses may coalesce away.
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001243 if (coalesceStackAccess(MI, Reg))
Jakob Stoklund Olesen7fd49052010-08-04 22:35:11 +00001244 continue;
1245
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001246 // Analyze instruction.
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001247 SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
James Molloy381fab92012-09-12 10:03:31 +00001248 MIBundleOperands::VirtRegInfo RI =
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001249 MIBundleOperands(MI).analyzeVirtReg(Reg, &Ops);
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001250
Jakob Stoklund Olesen9f294a92011-04-18 20:23:27 +00001251 // Find the slot index where this instruction reads and writes OldLI.
1252 // This is usually the def slot, except for tied early clobbers.
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +00001253 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
1254 if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
Jakob Stoklund Olesen9f294a92011-04-18 20:23:27 +00001255 if (SlotIndex::isSameInstr(Idx, VNI->def))
1256 Idx = VNI->def;
1257
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +00001258 // Check for a sibling copy.
1259 unsigned SibReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesene55003f2011-03-20 05:44:58 +00001260 if (SibReg && isSibling(SibReg)) {
Jakob Stoklund Olesen31a0b5e2011-05-11 18:25:10 +00001261 // This may actually be a copy between snippets.
1262 if (isRegToSpill(SibReg)) {
1263 DEBUG(dbgs() << "Found new snippet copy: " << *MI);
1264 SnippetCopies.insert(MI);
1265 continue;
1266 }
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001267 if (RI.Writes) {
Jakob Stoklund Olesene55003f2011-03-20 05:44:58 +00001268 // Hoist the spill of a sib-reg copy.
1269 if (hoistSpill(OldLI, MI)) {
1270 // This COPY is now dead, the value is already in the stack slot.
1271 MI->getOperand(0).setIsDead();
1272 DeadDefs.push_back(MI);
1273 continue;
1274 }
1275 } else {
1276 // This is a reload for a sib-reg copy. Drop spills downstream.
Jakob Stoklund Olesene55003f2011-03-20 05:44:58 +00001277 LiveInterval &SibLI = LIS.getInterval(SibReg);
1278 eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
1279 // The COPY will fold to a reload below.
1280 }
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +00001281 }
1282
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +00001283 // Attempt to fold memory ops.
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001284 if (foldMemoryOperand(Ops))
Jakob Stoklund Olesen96037182010-07-02 17:44:57 +00001285 continue;
1286
Mark Lacey9d8103d2013-08-14 23:50:16 +00001287 // Create a new virtual register for spill/fill.
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001288 // FIXME: Infer regclass from instruction alone.
Mark Lacey9d8103d2013-08-14 23:50:16 +00001289 unsigned NewVReg = Edit->createFrom(Reg);
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001290
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001291 if (RI.Reads)
Mark Lacey9d8103d2013-08-14 23:50:16 +00001292 insertReload(NewVReg, Idx, MI);
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001293
1294 // Rewrite instruction operands.
1295 bool hasLiveDef = false;
Craig Topper73275a22015-12-24 05:20:40 +00001296 for (const auto &OpPair : Ops) {
1297 MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
Mark Lacey9d8103d2013-08-14 23:50:16 +00001298 MO.setReg(NewVReg);
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001299 if (MO.isUse()) {
Craig Topper73275a22015-12-24 05:20:40 +00001300 if (!OpPair.first->isRegTiedToDefOperand(OpPair.second))
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001301 MO.setIsKill();
1302 } else {
1303 if (!MO.isDead())
1304 hasLiveDef = true;
1305 }
1306 }
Mark Lacey9d8103d2013-08-14 23:50:16 +00001307 DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI << '\n');
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001308
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001309 // FIXME: Use a second vreg if instruction has no tied ops.
Mark Lacey9d8103d2013-08-14 23:50:16 +00001310 if (RI.Writes)
Jakob Stoklund Olesenabe8c092012-03-01 01:43:25 +00001311 if (hasLiveDef)
Mark Lacey9d8103d2013-08-14 23:50:16 +00001312 insertSpill(NewVReg, true, MI);
Jakob Stoklund Olesenf8889112010-06-29 23:58:39 +00001313 }
1314}
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001315
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001316/// spillAll - Spill all registers remaining after rematerialization.
1317void InlineSpiller::spillAll() {
1318 // Update LiveStacks now that we are committed to spilling.
1319 if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
1320 StackSlot = VRM.assignVirt2StackSlot(Original);
1321 StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
Jakob Stoklund Olesenad6b22e2012-02-04 05:20:49 +00001322 StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001323 } else
1324 StackInt = &LSS.getInterval(StackSlot);
1325
1326 if (Original != Edit->getReg())
1327 VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
1328
1329 assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
Craig Topper73275a22015-12-24 05:20:40 +00001330 for (unsigned Reg : RegsToSpill)
1331 StackInt->MergeSegmentsInAsValue(LIS.getInterval(Reg),
Matthias Braun13ddb7c2013-10-10 21:28:43 +00001332 StackInt->getValNumInfo(0));
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001333 DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
1334
1335 // Spill around uses of all RegsToSpill.
Craig Topper73275a22015-12-24 05:20:40 +00001336 for (unsigned Reg : RegsToSpill)
1337 spillAroundUses(Reg);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001338
1339 // Hoisted spills may cause dead code.
1340 if (!DeadDefs.empty()) {
1341 DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
Pete Cooper2bde2f42012-04-02 22:22:53 +00001342 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001343 }
1344
1345 // Finally delete the SnippetCopies.
Craig Topper73275a22015-12-24 05:20:40 +00001346 for (unsigned Reg : RegsToSpill) {
Owen Andersonabb90c92014-03-13 06:02:25 +00001347 for (MachineRegisterInfo::reg_instr_iterator
Craig Topper73275a22015-12-24 05:20:40 +00001348 RI = MRI.reg_instr_begin(Reg), E = MRI.reg_instr_end();
Owen Andersonabb90c92014-03-13 06:02:25 +00001349 RI != E; ) {
1350 MachineInstr *MI = &*(RI++);
Jakob Stoklund Olesen31a0b5e2011-05-11 18:25:10 +00001351 assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
1352 // FIXME: Do this with a LiveRangeEdit callback.
Jakob Stoklund Olesen31a0b5e2011-05-11 18:25:10 +00001353 LIS.RemoveMachineInstrFromMaps(MI);
1354 MI->eraseFromParent();
1355 }
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001356 }
1357
1358 // Delete all spilled registers.
Craig Topper73275a22015-12-24 05:20:40 +00001359 for (unsigned Reg : RegsToSpill)
1360 Edit->eraseVirtReg(Reg);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001361}
1362
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001363void InlineSpiller::spill(LiveRangeEdit &edit) {
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +00001364 ++NumSpilledRanges;
Jakob Stoklund Olesena00bab22011-03-14 19:56:43 +00001365 Edit = &edit;
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001366 assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
1367 && "Trying to spill a stack slot.");
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +00001368 // Share a stack slot among all descendants of Original.
1369 Original = VRM.getOriginal(edit.getReg());
1370 StackSlot = VRM.getStackSlot(Original);
Craig Topperc0196b12014-04-14 00:51:57 +00001371 StackInt = nullptr;
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +00001372
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001373 DEBUG(dbgs() << "Inline spilling "
Craig Toppercf0444b2014-11-17 05:50:14 +00001374 << TRI.getRegClassName(MRI.getRegClass(edit.getReg()))
Matthias Braunf6fe6bf2013-10-10 21:29:05 +00001375 << ':' << edit.getParent()
Mark Lacey9d8103d2013-08-14 23:50:16 +00001376 << "\nFrom original " << PrintReg(Original) << '\n');
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001377 assert(edit.getParent().isSpillable() &&
1378 "Attempting to spill already spilled value.");
Jakob Stoklund Olesen27320cb2011-03-18 04:23:06 +00001379 assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001380
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001381 collectRegsToSpill();
Jakob Stoklund Olesena0d5ec12011-03-15 21:13:25 +00001382 analyzeSiblingValues();
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001383 reMaterializeAll();
1384
1385 // Remat may handle everything.
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +00001386 if (!RegsToSpill.empty())
1387 spillAll();
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001388
Benjamin Kramere2a1d892013-06-17 19:00:36 +00001389 Edit->calculateRegClassAndHint(MF, Loops, MBFI);
Jakob Stoklund Olesena86595e2011-03-12 04:17:20 +00001390}