blob: db63b5263836bad5ad8be03ad68bee67e2bfb367 [file] [log] [blame]
Jakob Stoklund Olesen914f2ff2010-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 Olesen376dcbd2010-11-03 20:39:23 +000015#define DEBUG_TYPE "regalloc"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000016#include "Spiller.h"
Benjamin Kramer2db14ba2013-05-23 15:42:57 +000017#include "llvm/ADT/SetVector.h"
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000018#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +000019#include "llvm/ADT/TinyPtrVector.h"
Jakob Stoklund Olesene93198a2010-11-10 23:55:56 +000020#include "llvm/Analysis/AliasAnalysis.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000021#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000022#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen0a12b802010-10-26 00:11:35 +000023#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000024#include "llvm/CodeGen/MachineDominators.h"
Benjamin Kramer4eed7562013-06-17 19:00:36 +000025#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
David Blaikie6d9dbd52013-06-16 20:34:15 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineInstrBundle.h"
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000030#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000032#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenb9edad02011-09-15 21:06:00 +000033#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000038
39using namespace llvm;
40
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000041STATISTIC(NumSpilledRanges, "Number of spilled live ranges");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000042STATISTIC(NumSnippets, "Number of spilled snippets");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000043STATISTIC(NumSpills, "Number of spills inserted");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000044STATISTIC(NumSpillsRemoved, "Number of spills removed");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000045STATISTIC(NumReloads, "Number of reloads inserted");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000046STATISTIC(NumReloadsRemoved, "Number of reloads removed");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000047STATISTIC(NumFolded, "Number of folded stack accesses");
48STATISTIC(NumFoldedLoads, "Number of folded loads");
49STATISTIC(NumRemats, "Number of rematerialized defs for spilling");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000050STATISTIC(NumOmitReloadSpill, "Number of omitted spills of reloads");
51STATISTIC(NumHoists, "Number of hoisted spills");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000052
Jakob Stoklund Olesenb9edad02011-09-15 21:06:00 +000053static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden,
54 cl::desc("Disable inline spill hoisting"));
55
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000056namespace {
57class InlineSpiller : public Spiller {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000058 MachineFunction &MF;
59 LiveIntervals &LIS;
60 LiveStacks &LSS;
61 AliasAnalysis *AA;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000062 MachineDominatorTree &MDT;
63 MachineLoopInfo &Loops;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000064 VirtRegMap &VRM;
65 MachineFrameInfo &MFI;
66 MachineRegisterInfo &MRI;
67 const TargetInstrInfo &TII;
68 const TargetRegisterInfo &TRI;
Benjamin Kramer4eed7562013-06-17 19:00:36 +000069 const MachineBlockFrequencyInfo &MBFI;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000070
71 // Variables that are valid during spill(), but used by multiple methods.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000072 LiveRangeEdit *Edit;
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +000073 LiveInterval *StackInt;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000074 int StackSlot;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000075 unsigned Original;
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000076
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000077 // All registers to spill to StackSlot, including the main register.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +000078 SmallVector<unsigned, 8> RegsToSpill;
79
80 // All COPY instructions to/from snippets.
81 // They are ignored since both operands refer to the same stack slot.
82 SmallPtrSet<MachineInstr*, 8> SnippetCopies;
83
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000084 // Values that failed to remat at some point.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000085 SmallPtrSet<VNInfo*, 8> UsedValues;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000086
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +000087public:
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000088 // Information about a value that was defined by a copy from a sibling
89 // register.
90 struct SibValueInfo {
91 // True when all reaching defs were reloads: No spill is necessary.
92 bool AllDefsAreReloads;
93
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +000094 // True when value is defined by an original PHI not from splitting.
95 bool DefByOrigPHI;
96
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +000097 // True when the COPY defining this value killed its source.
98 bool KillsSource;
99
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000100 // The preferred register to spill.
101 unsigned SpillReg;
102
103 // The value of SpillReg that should be spilled.
104 VNInfo *SpillVNI;
105
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000106 // The block where SpillVNI should be spilled. Currently, this must be the
107 // block containing SpillVNI->def.
108 MachineBasicBlock *SpillMBB;
109
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000110 // A defining instruction that is not a sibling copy or a reload, or NULL.
111 // This can be used as a template for rematerialization.
112 MachineInstr *DefMI;
113
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000114 // List of values that depend on this one. These values are actually the
115 // same, but live range splitting has placed them in different registers,
116 // or SSA update needed to insert PHI-defs to preserve SSA form. This is
117 // copies of the current value and phi-kills. Usually only phi-kills cause
118 // more than one dependent value.
119 TinyPtrVector<VNInfo*> Deps;
120
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000121 SibValueInfo(unsigned Reg, VNInfo *VNI)
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000122 : AllDefsAreReloads(true), DefByOrigPHI(false), KillsSource(false),
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000123 SpillReg(Reg), SpillVNI(VNI), SpillMBB(0), DefMI(0) {}
124
125 // Returns true when a def has been found.
126 bool hasDef() const { return DefByOrigPHI || DefMI; }
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000127 };
128
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000129private:
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000130 // Values in RegsToSpill defined by sibling copies.
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000131 typedef DenseMap<VNInfo*, SibValueInfo> SibValueMap;
132 SibValueMap SibValues;
133
134 // Dead defs generated during spilling.
135 SmallVector<MachineInstr*, 8> DeadDefs;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000136
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000137 ~InlineSpiller() {}
138
139public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000140 InlineSpiller(MachineFunctionPass &pass,
141 MachineFunction &mf,
142 VirtRegMap &vrm)
Benjamin Kramer95a9d932012-06-06 19:47:08 +0000143 : MF(mf),
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000144 LIS(pass.getAnalysis<LiveIntervals>()),
145 LSS(pass.getAnalysis<LiveStacks>()),
146 AA(&pass.getAnalysis<AliasAnalysis>()),
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000147 MDT(pass.getAnalysis<MachineDominatorTree>()),
148 Loops(pass.getAnalysis<MachineLoopInfo>()),
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000149 VRM(vrm),
150 MFI(*mf.getFrameInfo()),
151 MRI(mf.getRegInfo()),
152 TII(*mf.getTarget().getInstrInfo()),
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000153 TRI(*mf.getTarget().getRegisterInfo()),
154 MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()) {}
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000155
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000156 void spill(LiveRangeEdit &);
157
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000158private:
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000159 bool isSnippet(const LiveInterval &SnipLI);
160 void collectRegsToSpill();
161
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000162 bool isRegToSpill(unsigned Reg) {
163 return std::find(RegsToSpill.begin(),
164 RegsToSpill.end(), Reg) != RegsToSpill.end();
165 }
166
167 bool isSibling(unsigned Reg);
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000168 MachineInstr *traceSiblingValue(unsigned, VNInfo*, VNInfo*);
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000169 void propagateSiblingValue(SibValueMap::iterator, VNInfo *VNI = 0);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000170 void analyzeSiblingValues();
171
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000172 bool hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000173 void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000174
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000175 void markValueUsed(LiveInterval*, VNInfo*);
176 bool reMaterializeFor(LiveInterval&, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000177 void reMaterializeAll();
178
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000179 bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000180 bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> >,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000181 MachineInstr *LoadMI = 0);
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000182 void insertReload(LiveInterval &NewLI, SlotIndex,
183 MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000184 void insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000185 SlotIndex, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000186
187 void spillAroundUses(unsigned Reg);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000188 void spillAll();
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000189};
190}
191
192namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000193Spiller *createInlineSpiller(MachineFunctionPass &pass,
194 MachineFunction &mf,
195 VirtRegMap &vrm) {
196 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000197}
198}
199
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000200//===----------------------------------------------------------------------===//
201// Snippets
202//===----------------------------------------------------------------------===//
203
204// When spilling a virtual register, we also spill any snippets it is connected
205// to. The snippets are small live ranges that only have a single real use,
206// leftovers from live range splitting. Spilling them enables memory operand
207// folding or tightens the live range around the single use.
208//
209// This minimizes register pressure and maximizes the store-to-load distance for
210// spill slots which can be important in tight loops.
211
212/// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
213/// otherwise return 0.
214static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
Rafael Espindolacfe52542011-06-30 21:15:52 +0000215 if (!MI->isFullCopy())
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000216 return 0;
217 if (MI->getOperand(0).getReg() == Reg)
218 return MI->getOperand(1).getReg();
219 if (MI->getOperand(1).getReg() == Reg)
220 return MI->getOperand(0).getReg();
221 return 0;
222}
223
224/// isSnippet - Identify if a live interval is a snippet that should be spilled.
225/// It is assumed that SnipLI is a virtual register with the same original as
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000226/// Edit->getReg().
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000227bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000228 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000229
230 // A snippet is a tiny live range with only a single instruction using it
231 // besides copies to/from Reg or spills/fills. We accept:
232 //
233 // %snip = COPY %Reg / FILL fi#
234 // %snip = USE %snip
235 // %Reg = COPY %snip / SPILL %snip, fi#
236 //
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000237 if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000238 return false;
239
240 MachineInstr *UseMI = 0;
241
242 // Check that all uses satisfy our criteria.
243 for (MachineRegisterInfo::reg_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000244 RI = MRI.reg_nodbg_begin(SnipLI.reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000245 MachineInstr *MI = RI.skipInstruction();) {
246
247 // Allow copies to/from Reg.
248 if (isFullCopyOf(MI, Reg))
249 continue;
250
251 // Allow stack slot loads.
252 int FI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000253 if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000254 continue;
255
256 // Allow stack slot stores.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000257 if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-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 Olesen766faf42011-03-14 19:56:43 +0000271 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-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 Olesen13ba2522011-03-15 21:13:25 +0000279 if (Original == Reg)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000280 return;
281
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000282 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000283 MachineInstr *MI = RI.skipInstruction();) {
284 unsigned SnipReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000285 if (!isSibling(SnipReg))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000286 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000287 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000288 if (!isSnippet(SnipLI))
289 continue;
290 SnippetCopies.insert(MI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000291 if (isRegToSpill(SnipReg))
292 continue;
293 RegsToSpill.push_back(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000294 DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000295 ++NumSnippets;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000296 }
297}
298
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000299
300//===----------------------------------------------------------------------===//
301// Sibling Values
302//===----------------------------------------------------------------------===//
303
304// After live range splitting, some values to be spilled may be defined by
305// copies from sibling registers. We trace the sibling copies back to the
306// original value if it still exists. We need it for rematerialization.
307//
308// Even when the value can't be rematerialized, we still want to determine if
309// the value has already been spilled, or we may want to hoist the spill from a
310// loop.
311
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000312bool InlineSpiller::isSibling(unsigned Reg) {
313 return TargetRegisterInfo::isVirtualRegister(Reg) &&
314 VRM.getOriginal(Reg) == Original;
315}
316
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000317#ifndef NDEBUG
318static raw_ostream &operator<<(raw_ostream &OS,
319 const InlineSpiller::SibValueInfo &SVI) {
320 OS << "spill " << PrintReg(SVI.SpillReg) << ':'
321 << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def;
322 if (SVI.SpillMBB)
323 OS << " in BB#" << SVI.SpillMBB->getNumber();
324 if (SVI.AllDefsAreReloads)
325 OS << " all-reloads";
326 if (SVI.DefByOrigPHI)
327 OS << " orig-phi";
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000328 if (SVI.KillsSource)
329 OS << " kill";
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000330 OS << " deps[";
331 for (unsigned i = 0, e = SVI.Deps.size(); i != e; ++i)
332 OS << ' ' << SVI.Deps[i]->id << '@' << SVI.Deps[i]->def;
333 OS << " ]";
334 if (SVI.DefMI)
335 OS << " def: " << *SVI.DefMI;
336 else
337 OS << '\n';
338 return OS;
339}
340#endif
341
342/// propagateSiblingValue - Propagate the value in SVI to dependents if it is
343/// known. Otherwise remember the dependency for later.
344///
Benjamin Kramer2db14ba2013-05-23 15:42:57 +0000345/// @param SVIIter SibValues entry to propagate.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000346/// @param VNI Dependent value, or NULL to propagate to all saved dependents.
Benjamin Kramer2db14ba2013-05-23 15:42:57 +0000347void InlineSpiller::propagateSiblingValue(SibValueMap::iterator SVIIter,
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000348 VNInfo *VNI) {
Benjamin Kramer2db14ba2013-05-23 15:42:57 +0000349 SibValueMap::value_type *SVI = &*SVIIter;
350
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000351 // When VNI is non-NULL, add it to SVI's deps, and only propagate to that.
352 TinyPtrVector<VNInfo*> FirstDeps;
353 if (VNI) {
354 FirstDeps.push_back(VNI);
355 SVI->second.Deps.push_back(VNI);
356 }
357
358 // Has the value been completely determined yet? If not, defer propagation.
359 if (!SVI->second.hasDef())
360 return;
361
Benjamin Kramer2db14ba2013-05-23 15:42:57 +0000362 // Work list of values to propagate.
363 SmallSetVector<SibValueMap::value_type *, 8> WorkList;
364 WorkList.insert(SVI);
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000365
366 do {
367 SVI = WorkList.pop_back_val();
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000368 TinyPtrVector<VNInfo*> *Deps = VNI ? &FirstDeps : &SVI->second.Deps;
369 VNI = 0;
370
371 SibValueInfo &SV = SVI->second;
372 if (!SV.SpillMBB)
373 SV.SpillMBB = LIS.getMBBFromIndex(SV.SpillVNI->def);
374
375 DEBUG(dbgs() << " prop to " << Deps->size() << ": "
376 << SVI->first->id << '@' << SVI->first->def << ":\t" << SV);
377
378 assert(SV.hasDef() && "Propagating undefined value");
379
380 // Should this value be propagated as a preferred spill candidate? We don't
381 // propagate values of registers that are about to spill.
Jakob Stoklund Olesenb9edad02011-09-15 21:06:00 +0000382 bool PropSpill = !DisableHoisting && !isRegToSpill(SV.SpillReg);
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000383 unsigned SpillDepth = ~0u;
384
385 for (TinyPtrVector<VNInfo*>::iterator DepI = Deps->begin(),
386 DepE = Deps->end(); DepI != DepE; ++DepI) {
387 SibValueMap::iterator DepSVI = SibValues.find(*DepI);
388 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 Olesen69cf1ca2011-09-16 00:03:33 +0000413 if (DepSV.KillsSource && SV.SpillVNI->def < DepSV.SpillVNI->def) {
Jakob Stoklund Olesen1ab7c8e2011-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 Olesen69cf1ca2011-09-16 00:03:33 +0000429 // This hoist only helps when the DepSV copy kills its source.
Jakob Stoklund Olesen1ab7c8e2011-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.
443 if ((Loops.getLoopDepth(DepSV.SpillMBB) > SpillDepth) &&
444 (!DepSVI->first->isPHIDef() ||
445 MDT.dominates(SV.SpillMBB, DepSV.SpillMBB))) {
446 Changed = true;
447 DepSV.SpillReg = SV.SpillReg;
448 DepSV.SpillVNI = SV.SpillVNI;
449 DepSV.SpillMBB = SV.SpillMBB;
450 }
451 }
452 }
453
454 if (!Changed)
455 continue;
456
457 // Something changed in DepSVI. Propagate to dependents.
Benjamin Kramer2db14ba2013-05-23 15:42:57 +0000458 WorkList.insert(&*DepSVI);
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000459
460 DEBUG(dbgs() << " update " << DepSVI->first->id << '@'
461 << DepSVI->first->def << " to:\t" << DepSV);
462 }
463 } while (!WorkList.empty());
464}
465
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000466/// traceSiblingValue - Trace a value that is about to be spilled back to the
467/// real defining instructions by looking through sibling copies. Always stay
468/// within the range of OrigVNI so the registers are known to carry the same
469/// value.
470///
471/// Determine if the value is defined by all reloads, so spilling isn't
472/// necessary - the value is already in the stack slot.
473///
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000474/// Return a defining instruction that may be a candidate for rematerialization.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000475///
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000476MachineInstr *InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
477 VNInfo *OrigVNI) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000478 // Check if a cached value already exists.
479 SibValueMap::iterator SVI;
480 bool Inserted;
481 tie(SVI, Inserted) =
482 SibValues.insert(std::make_pair(UseVNI, SibValueInfo(UseReg, UseVNI)));
483 if (!Inserted) {
484 DEBUG(dbgs() << "Cached value " << PrintReg(UseReg) << ':'
485 << UseVNI->id << '@' << UseVNI->def << ' ' << SVI->second);
486 return SVI->second.DefMI;
487 }
488
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000489 DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
490 << UseVNI->id << '@' << UseVNI->def << '\n');
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000491
492 // List of (Reg, VNI) that have been inserted into SibValues, but need to be
493 // processed.
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000494 SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000495 WorkList.push_back(std::make_pair(UseReg, UseVNI));
496
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000497 do {
498 unsigned Reg;
499 VNInfo *VNI;
500 tie(Reg, VNI) = WorkList.pop_back_val();
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000501 DEBUG(dbgs() << " " << PrintReg(Reg) << ':' << VNI->id << '@' << VNI->def
502 << ":\t");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000503
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000504 // First check if this value has already been computed.
505 SVI = SibValues.find(VNI);
506 assert(SVI != SibValues.end() && "Missing SibValues entry");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000507
508 // Trace through PHI-defs created by live range splitting.
509 if (VNI->isPHIDef()) {
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000510 // Stop at original PHIs. We don't know the value at the predecessors.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000511 if (VNI->def == OrigVNI->def) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000512 DEBUG(dbgs() << "orig phi value\n");
513 SVI->second.DefByOrigPHI = true;
514 SVI->second.AllDefsAreReloads = false;
515 propagateSiblingValue(SVI);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000516 continue;
517 }
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000518
519 // This is a PHI inserted by live range splitting. We could trace the
520 // live-out value from predecessor blocks, but that search can be very
521 // expensive if there are many predecessors and many more PHIs as
522 // generated by tail-dup when it sees an indirectbr. Instead, look at
523 // all the non-PHI defs that have the same value as OrigVNI. They must
524 // jointly dominate VNI->def. This is not optimal since VNI may actually
525 // be jointly dominated by a smaller subset of defs, so there is a change
526 // we will miss a AllDefsAreReloads optimization.
527
528 // Separate all values dominated by OrigVNI into PHIs and non-PHIs.
529 SmallVector<VNInfo*, 8> PHIs, NonPHIs;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000530 LiveInterval &LI = LIS.getInterval(Reg);
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000531 LiveInterval &OrigLI = LIS.getInterval(Original);
532
533 for (LiveInterval::vni_iterator VI = LI.vni_begin(), VE = LI.vni_end();
534 VI != VE; ++VI) {
535 VNInfo *VNI2 = *VI;
536 if (VNI2->isUnused())
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000537 continue;
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000538 if (!OrigLI.containsOneValue() &&
539 OrigLI.getVNInfoAt(VNI2->def) != OrigVNI)
540 continue;
541 if (VNI2->isPHIDef() && VNI2->def != OrigVNI->def)
542 PHIs.push_back(VNI2);
543 else
544 NonPHIs.push_back(VNI2);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000545 }
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000546 DEBUG(dbgs() << "split phi value, checking " << PHIs.size()
547 << " phi-defs, and " << NonPHIs.size()
548 << " non-phi/orig defs\n");
549
550 // Create entries for all the PHIs. Don't add them to the worklist, we
551 // are processing all of them in one go here.
552 for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
553 SibValues.insert(std::make_pair(PHIs[i], SibValueInfo(Reg, PHIs[i])));
554
555 // Add every PHI as a dependent of all the non-PHIs.
556 for (unsigned i = 0, e = NonPHIs.size(); i != e; ++i) {
557 VNInfo *NonPHI = NonPHIs[i];
558 // Known value? Try an insertion.
559 tie(SVI, Inserted) =
560 SibValues.insert(std::make_pair(NonPHI, SibValueInfo(Reg, NonPHI)));
561 // Add all the PHIs as dependents of NonPHI.
562 for (unsigned pi = 0, pe = PHIs.size(); pi != pe; ++pi)
563 SVI->second.Deps.push_back(PHIs[pi]);
564 // This is the first time we see NonPHI, add it to the worklist.
565 if (Inserted)
566 WorkList.push_back(std::make_pair(Reg, NonPHI));
567 else
568 // Propagate to all inserted PHIs, not just VNI.
569 propagateSiblingValue(SVI);
570 }
571
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000572 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000573 continue;
574 }
575
576 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
577 assert(MI && "Missing def");
578
579 // Trace through sibling copies.
580 if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000581 if (isSibling(SrcReg)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000582 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
Jakob Stoklund Olesen92a05fa2012-05-20 02:44:33 +0000583 LiveRangeQuery SrcQ(SrcLI, VNI->def);
584 assert(SrcQ.valueIn() && "Copy from non-existing value");
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000585 // Check if this COPY kills its source.
Jakob Stoklund Olesen92a05fa2012-05-20 02:44:33 +0000586 SVI->second.KillsSource = SrcQ.isKill();
587 VNInfo *SrcVNI = SrcQ.valueIn();
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000588 DEBUG(dbgs() << "copy of " << PrintReg(SrcReg) << ':'
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000589 << SrcVNI->id << '@' << SrcVNI->def
590 << " kill=" << unsigned(SVI->second.KillsSource) << '\n');
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000591 // Known sibling source value? Try an insertion.
592 tie(SVI, Inserted) = SibValues.insert(std::make_pair(SrcVNI,
593 SibValueInfo(SrcReg, SrcVNI)));
594 // This is the first time we see Src, add it to the worklist.
595 if (Inserted)
596 WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
597 propagateSiblingValue(SVI, VNI);
598 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000599 continue;
600 }
601 }
602
603 // Track reachable reloads.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000604 SVI->second.DefMI = MI;
605 SVI->second.SpillMBB = MI->getParent();
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000606 int FI;
607 if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000608 DEBUG(dbgs() << "reload\n");
609 propagateSiblingValue(SVI);
610 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000611 continue;
612 }
613
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000614 // Potential remat candidate.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000615 DEBUG(dbgs() << "def " << *MI);
616 SVI->second.AllDefsAreReloads = false;
617 propagateSiblingValue(SVI);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000618 } while (!WorkList.empty());
619
Logan Chiene2ac5522012-09-01 12:11:41 +0000620 // Look up the value we were looking for. We already did this lookup at the
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000621 // top of the function, but SibValues may have been invalidated.
622 SVI = SibValues.find(UseVNI);
623 assert(SVI != SibValues.end() && "Didn't compute requested info");
624 DEBUG(dbgs() << " traced to:\t" << SVI->second);
625 return SVI->second.DefMI;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000626}
627
628/// analyzeSiblingValues - Trace values defined by sibling copies back to
629/// something that isn't a sibling copy.
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000630///
631/// Keep track of values that may be rematerializable.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000632void InlineSpiller::analyzeSiblingValues() {
633 SibValues.clear();
634
635 // No siblings at all?
636 if (Edit->getReg() == Original)
637 return;
638
639 LiveInterval &OrigLI = LIS.getInterval(Original);
640 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
641 unsigned Reg = RegsToSpill[i];
642 LiveInterval &LI = LIS.getInterval(Reg);
643 for (LiveInterval::const_vni_iterator VI = LI.vni_begin(),
644 VE = LI.vni_end(); VI != VE; ++VI) {
645 VNInfo *VNI = *VI;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000646 if (VNI->isUnused())
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000647 continue;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000648 MachineInstr *DefMI = 0;
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000649 if (!VNI->isPHIDef()) {
650 DefMI = LIS.getInstructionFromIndex(VNI->def);
651 assert(DefMI && "No defining instruction");
652 }
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000653 // Check possible sibling copies.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000654 if (VNI->isPHIDef() || DefMI->isCopy()) {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000655 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen9693d4c2011-07-05 15:38:41 +0000656 assert(OrigVNI && "Def outside original live range");
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000657 if (OrigVNI->def != VNI->def)
658 DefMI = traceSiblingValue(Reg, VNI, OrigVNI);
659 }
Pete Cooper8a06af92012-04-02 22:22:53 +0000660 if (DefMI && Edit->checkRematerializable(VNI, DefMI, AA)) {
Jakob Stoklund Olesen3b7d9172011-04-20 22:14:20 +0000661 DEBUG(dbgs() << "Value " << PrintReg(Reg) << ':' << VNI->id << '@'
662 << VNI->def << " may remat from " << *DefMI);
663 }
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000664 }
665 }
666}
667
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000668/// hoistSpill - Given a sibling copy that defines a value to be spilled, insert
669/// a spill at a better location.
670bool InlineSpiller::hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI) {
671 SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000672 VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot());
673 assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy");
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000674 SibValueMap::iterator I = SibValues.find(VNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000675 if (I == SibValues.end())
676 return false;
677
678 const SibValueInfo &SVI = I->second;
679
680 // Let the normal folding code deal with the boring case.
681 if (!SVI.AllDefsAreReloads && SVI.SpillVNI == VNI)
682 return false;
683
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000684 // SpillReg may have been deleted by remat and DCE.
685 if (!LIS.hasInterval(SVI.SpillReg)) {
686 DEBUG(dbgs() << "Stale interval: " << PrintReg(SVI.SpillReg) << '\n');
687 SibValues.erase(I);
688 return false;
689 }
690
691 LiveInterval &SibLI = LIS.getInterval(SVI.SpillReg);
692 if (!SibLI.containsValue(SVI.SpillVNI)) {
693 DEBUG(dbgs() << "Stale value: " << PrintReg(SVI.SpillReg) << '\n');
694 SibValues.erase(I);
695 return false;
696 }
697
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000698 // Conservatively extend the stack slot range to the range of the original
699 // value. We may be able to do better with stack slot coloring by being more
700 // careful here.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000701 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000702 LiveInterval &OrigLI = LIS.getInterval(Original);
703 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000704 StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +0000705 DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000706 << *StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000707
708 // Already spilled everywhere.
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000709 if (SVI.AllDefsAreReloads) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000710 DEBUG(dbgs() << "\tno spill needed: " << SVI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000711 ++NumOmitReloadSpill;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000712 return true;
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000713 }
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000714 // We are going to spill SVI.SpillVNI immediately after its def, so clear out
715 // any later spills of the same value.
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000716 eliminateRedundantSpills(SibLI, SVI.SpillVNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000717
718 MachineBasicBlock *MBB = LIS.getMBBFromIndex(SVI.SpillVNI->def);
719 MachineBasicBlock::iterator MII;
720 if (SVI.SpillVNI->isPHIDef())
721 MII = MBB->SkipPHIsAndLabels(MBB->begin());
722 else {
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000723 MachineInstr *DefMI = LIS.getInstructionFromIndex(SVI.SpillVNI->def);
724 assert(DefMI && "Defining instruction disappeared");
725 MII = DefMI;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000726 ++MII;
727 }
728 // Insert spill without kill flag immediately after def.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000729 TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot,
730 MRI.getRegClass(SVI.SpillReg), &TRI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000731 --MII; // Point to store instruction.
732 LIS.InsertMachineInstrInMaps(MII);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000733 DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000734
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +0000735 ++NumSpills;
736 ++NumHoists;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000737 return true;
738}
739
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000740/// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
741/// redundant spills of this value in SLI.reg and sibling copies.
742void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +0000743 assert(VNI && "Missing value");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000744 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
745 WorkList.push_back(std::make_pair(&SLI, VNI));
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000746 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000747
748 do {
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000749 LiveInterval *LI;
750 tie(LI, VNI) = WorkList.pop_back_val();
751 unsigned Reg = LI->reg;
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000752 DEBUG(dbgs() << "Checking redundant spills for "
753 << VNI->id << '@' << VNI->def << " in " << *LI << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000754
755 // Regs to spill are taken care of.
756 if (isRegToSpill(Reg))
757 continue;
758
759 // Add all of VNI's live range to StackInt.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000760 StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
761 DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000762
763 // Find all spills and copies of VNI.
764 for (MachineRegisterInfo::use_nodbg_iterator UI = MRI.use_nodbg_begin(Reg);
765 MachineInstr *MI = UI.skipInstruction();) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000766 if (!MI->isCopy() && !MI->mayStore())
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000767 continue;
768 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000769 if (LI->getVNInfoAt(Idx) != VNI)
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000770 continue;
771
772 // Follow sibling copies down the dominator tree.
773 if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
774 if (isSibling(DstReg)) {
775 LiveInterval &DstLI = LIS.getInterval(DstReg);
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000776 VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000777 assert(DstVNI && "Missing defined value");
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000778 assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000779 WorkList.push_back(std::make_pair(&DstLI, DstVNI));
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000780 }
781 continue;
782 }
783
784 // Erase spills.
785 int FI;
786 if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
787 DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << *MI);
788 // eliminateDeadDefs won't normally remove stores, so switch opcode.
789 MI->setDesc(TII.get(TargetOpcode::KILL));
790 DeadDefs.push_back(MI);
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +0000791 ++NumSpillsRemoved;
792 --NumSpills;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000793 }
794 }
795 } while (!WorkList.empty());
796}
797
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000798
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000799//===----------------------------------------------------------------------===//
800// Rematerialization
801//===----------------------------------------------------------------------===//
802
803/// markValueUsed - Remember that VNI failed to rematerialize, so its defining
804/// instruction cannot be eliminated. See through snippet copies
805void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
806 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
807 WorkList.push_back(std::make_pair(LI, VNI));
808 do {
809 tie(LI, VNI) = WorkList.pop_back_val();
810 if (!UsedValues.insert(VNI))
811 continue;
812
813 if (VNI->isPHIDef()) {
814 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
815 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
816 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000817 VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI));
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000818 if (PVNI)
819 WorkList.push_back(std::make_pair(LI, PVNI));
820 }
821 continue;
822 }
823
824 // Follow snippet copies.
825 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
826 if (!SnippetCopies.count(MI))
827 continue;
828 LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
829 assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000830 VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000831 assert(SnipVNI && "Snippet undefined before copy");
832 WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
833 } while (!WorkList.empty());
834}
835
836/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
837bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg,
838 MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000839 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
Jakob Stoklund Olesen79413502011-07-18 05:31:59 +0000840 VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000841
842 if (!ParentVNI) {
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000843 DEBUG(dbgs() << "\tadding <undef> flags: ");
844 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
845 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000846 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000847 MO.setIsUndef();
848 }
849 DEBUG(dbgs() << UseIdx << '\t' << *MI);
850 return true;
851 }
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000852
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000853 if (SnippetCopies.count(MI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000854 return false;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000855
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000856 // Use an OrigVNI from traceSiblingValue when ParentVNI is a sibling copy.
857 LiveRangeEdit::Remat RM(ParentVNI);
858 SibValueMap::const_iterator SibI = SibValues.find(ParentVNI);
859 if (SibI != SibValues.end())
860 RM.OrigMI = SibI->second.DefMI;
Pete Cooper8a06af92012-04-02 22:22:53 +0000861 if (!Edit->canRematerializeAt(RM, UseIdx, false)) {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000862 markValueUsed(&VirtReg, ParentVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000863 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
864 return false;
865 }
866
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000867 // If the instruction also writes VirtReg.reg, it had better not require the
868 // same register for uses and defs.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000869 SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
James Molloyb17cf292012-09-12 10:03:31 +0000870 MIBundleOperands::VirtRegInfo RI =
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000871 MIBundleOperands(MI).analyzeVirtReg(VirtReg.reg, &Ops);
872 if (RI.Tied) {
873 markValueUsed(&VirtReg, ParentVNI);
874 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
875 return false;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000876 }
877
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000878 // Before rematerializing into a register for a single instruction, try to
879 // fold a load into the instruction. That avoids allocating a new register.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000880 if (RM.OrigMI->canFoldAsLoad() &&
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000881 foldMemoryOperand(Ops, RM.OrigMI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000882 Edit->markRematerialized(RM.ParentVNI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000883 ++NumFoldedLoads;
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000884 return true;
885 }
886
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000887 // Alocate a new register for the remat.
Pete Cooper8a06af92012-04-02 22:22:53 +0000888 LiveInterval &NewLI = Edit->createFrom(Original);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000889 NewLI.markNotSpillable();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000890
891 // Finally we can rematerialize OrigMI before MI.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000892 SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
Pete Cooper8a06af92012-04-02 22:22:53 +0000893 TRI);
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000894 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000895 << *LIS.getInstructionFromIndex(DefIdx));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000896
897 // Replace operands
898 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000899 MachineOperand &MO = MI->getOperand(Ops[i].second);
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000900 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000901 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000902 MO.setIsKill();
903 }
904 }
905 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
906
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000907 VNInfo *DefVNI = NewLI.getNextValue(DefIdx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000908 NewLI.addRange(LiveRange(DefIdx, UseIdx.getRegSlot(), DefVNI));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000909 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000910 ++NumRemats;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000911 return true;
912}
913
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000914/// reMaterializeAll - Try to rematerialize as many uses as possible,
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000915/// and trim the live ranges after.
916void InlineSpiller::reMaterializeAll() {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000917 // analyzeSiblingValues has already tested all relevant defining instructions.
Pete Cooper8a06af92012-04-02 22:22:53 +0000918 if (!Edit->anyRematerializable(AA))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000919 return;
920
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000921 UsedValues.clear();
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000922
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000923 // Try to remat before all uses of snippets.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000924 bool anyRemat = false;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000925 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
926 unsigned Reg = RegsToSpill[i];
927 LiveInterval &LI = LIS.getInterval(Reg);
928 for (MachineRegisterInfo::use_nodbg_iterator
929 RI = MRI.use_nodbg_begin(Reg);
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000930 MachineInstr *MI = RI.skipBundle();)
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000931 anyRemat |= reMaterializeFor(LI, MI);
932 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000933 if (!anyRemat)
934 return;
935
936 // Remove any values that were completely rematted.
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000937 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
938 unsigned Reg = RegsToSpill[i];
939 LiveInterval &LI = LIS.getInterval(Reg);
940 for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
941 I != E; ++I) {
942 VNInfo *VNI = *I;
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000943 if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000944 continue;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000945 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
946 MI->addRegisterDead(Reg, &TRI);
947 if (!MI->allDefsAreDead())
948 continue;
949 DEBUG(dbgs() << "All defs dead: " << *MI);
950 DeadDefs.push_back(MI);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000951 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000952 }
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000953
954 // Eliminate dead code after remat. Note that some snippet copies may be
955 // deleted here.
956 if (DeadDefs.empty())
957 return;
958 DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
Pete Cooper8a06af92012-04-02 22:22:53 +0000959 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000960
961 // Get rid of deleted and empty intervals.
Benjamin Kramere210df22013-05-05 11:29:14 +0000962 unsigned ResultPos = 0;
963 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
964 unsigned Reg = RegsToSpill[i];
965 if (!LIS.hasInterval(Reg))
966 continue;
967
968 LiveInterval &LI = LIS.getInterval(Reg);
969 if (LI.empty()) {
970 Edit->eraseVirtReg(Reg);
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000971 continue;
972 }
Benjamin Kramere210df22013-05-05 11:29:14 +0000973
974 RegsToSpill[ResultPos++] = Reg;
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000975 }
Benjamin Kramere210df22013-05-05 11:29:14 +0000976 RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000977 DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n");
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000978}
979
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000980
981//===----------------------------------------------------------------------===//
982// Spilling
983//===----------------------------------------------------------------------===//
984
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000985/// If MI is a load or store of StackSlot, it can be removed.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000986bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000987 int FI = 0;
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +0000988 unsigned InstrReg = TII.isLoadFromStackSlot(MI, FI);
989 bool IsLoad = InstrReg;
990 if (!IsLoad)
991 InstrReg = TII.isStoreToStackSlot(MI, FI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000992
993 // We have a stack access. Is it the right register and slot?
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000994 if (InstrReg != Reg || FI != StackSlot)
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000995 return false;
996
997 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000998 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000999 MI->eraseFromParent();
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +00001000
1001 if (IsLoad) {
1002 ++NumReloadsRemoved;
1003 --NumReloads;
1004 } else {
1005 ++NumSpillsRemoved;
1006 --NumSpills;
1007 }
1008
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001009 return true;
1010}
1011
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001012/// foldMemoryOperand - Try folding stack slot references in Ops into their
1013/// instructions.
1014///
1015/// @param Ops Operand indices from analyzeVirtReg().
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +00001016/// @param LoadMI Load instruction to use instead of stack slot when non-null.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001017/// @return True on success.
1018bool InlineSpiller::
1019foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops,
1020 MachineInstr *LoadMI) {
1021 if (Ops.empty())
1022 return false;
1023 // Don't attempt folding in bundles.
1024 MachineInstr *MI = Ops.front().first;
1025 if (Ops.back().first != MI || MI->isBundled())
1026 return false;
1027
Jakob Stoklund Olesend205f7a2011-09-15 18:22:52 +00001028 bool WasCopy = MI->isCopy();
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001029 unsigned ImpReg = 0;
1030
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001031 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
1032 // operands.
1033 SmallVector<unsigned, 8> FoldOps;
1034 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001035 unsigned Idx = Ops[i].second;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001036 MachineOperand &MO = MI->getOperand(Idx);
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001037 if (MO.isImplicit()) {
1038 ImpReg = MO.getReg();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001039 continue;
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001040 }
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001041 // FIXME: Teach targets to deal with subregs.
1042 if (MO.getSubReg())
1043 return false;
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +00001044 // We cannot fold a load instruction into a def.
1045 if (LoadMI && MO.isDef())
1046 return false;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001047 // Tied use operands should not be passed to foldMemoryOperand.
1048 if (!MI->isRegTiedToDefOperand(Idx))
1049 FoldOps.push_back(Idx);
1050 }
1051
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +00001052 MachineInstr *FoldMI =
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001053 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
1054 : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001055 if (!FoldMI)
1056 return false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001057 LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001058 MI->eraseFromParent();
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001059
1060 // TII.foldMemoryOperand may have left some implicit operands on the
1061 // instruction. Strip them.
1062 if (ImpReg)
1063 for (unsigned i = FoldMI->getNumOperands(); i; --i) {
1064 MachineOperand &MO = FoldMI->getOperand(i - 1);
1065 if (!MO.isReg() || !MO.isImplicit())
1066 break;
1067 if (MO.getReg() == ImpReg)
1068 FoldMI->RemoveOperand(i - 1);
1069 }
1070
1071 DEBUG(dbgs() << "\tfolded: " << LIS.getInstructionIndex(FoldMI) << '\t'
1072 << *FoldMI);
Jakob Stoklund Olesend205f7a2011-09-15 18:22:52 +00001073 if (!WasCopy)
1074 ++NumFolded;
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001075 else if (Ops.front().second == 0)
Jakob Stoklund Olesend205f7a2011-09-15 18:22:52 +00001076 ++NumSpills;
1077 else
1078 ++NumReloads;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001079 return true;
1080}
1081
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001082/// insertReload - Insert a reload of NewLI.reg before MI.
1083void InlineSpiller::insertReload(LiveInterval &NewLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001084 SlotIndex Idx,
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001085 MachineBasicBlock::iterator MI) {
1086 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +00001087 TII.loadRegFromStackSlot(MBB, MI, NewLI.reg, StackSlot,
1088 MRI.getRegClass(NewLI.reg), &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001089 --MI; // Point to load instruction.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001090 SlotIndex LoadIdx = LIS.InsertMachineInstrInMaps(MI).getRegSlot();
Jakob Stoklund Olesen27982e12012-07-14 18:45:35 +00001091 // Some (out-of-tree) targets have EC reload instructions.
1092 if (MachineOperand *MO = MI->findRegisterDefOperand(NewLI.reg))
1093 if (MO->isEarlyClobber())
1094 LoadIdx = LoadIdx.getRegSlot(true);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001095 DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI);
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +00001096 VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001097 NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001098 ++NumReloads;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001099}
1100
1101/// insertSpill - Insert a spill of NewLI.reg after MI.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001102void InlineSpiller::insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001103 SlotIndex Idx, MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001104 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +00001105 TII.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, StackSlot,
1106 MRI.getRegClass(NewLI.reg), &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001107 --MI; // Point to store instruction.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001108 SlotIndex StoreIdx = LIS.InsertMachineInstrInMaps(MI).getRegSlot();
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001109 DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +00001110 VNInfo *StoreVNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001111 NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001112 ++NumSpills;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001113}
1114
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001115/// spillAroundUses - insert spill code around each use of Reg.
1116void InlineSpiller::spillAroundUses(unsigned Reg) {
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001117 DEBUG(dbgs() << "spillAroundUses " << PrintReg(Reg) << '\n');
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001118 LiveInterval &OldLI = LIS.getInterval(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001119
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001120 // Iterate over instructions using Reg.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001121 for (MachineRegisterInfo::reg_iterator RegI = MRI.reg_begin(Reg);
1122 MachineInstr *MI = RegI.skipBundle();) {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001123
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +00001124 // Debug values are not allowed to affect codegen.
1125 if (MI->isDebugValue()) {
1126 // Modify DBG_VALUE now that the value is in a spill slot.
1127 uint64_t Offset = MI->getOperand(1).getImm();
1128 const MDNode *MDPtr = MI->getOperand(2).getMetadata();
1129 DebugLoc DL = MI->getDebugLoc();
David Blaikie6d9dbd52013-06-16 20:34:15 +00001130 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
1131 MachineBasicBlock *MBB = MI->getParent();
1132 BuildMI(*MBB, MBB->erase(MI), DL, TII.get(TargetOpcode::DBG_VALUE))
1133 .addFrameIndex(StackSlot).addImm(Offset).addMetadata(MDPtr);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +00001134 continue;
1135 }
1136
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001137 // Ignore copies to/from snippets. We'll delete them.
1138 if (SnippetCopies.count(MI))
1139 continue;
1140
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001141 // Stack slot accesses may coalesce away.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001142 if (coalesceStackAccess(MI, Reg))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001143 continue;
1144
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001145 // Analyze instruction.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001146 SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
James Molloyb17cf292012-09-12 10:03:31 +00001147 MIBundleOperands::VirtRegInfo RI =
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001148 MIBundleOperands(MI).analyzeVirtReg(Reg, &Ops);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001149
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001150 // Find the slot index where this instruction reads and writes OldLI.
1151 // This is usually the def slot, except for tied early clobbers.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001152 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
1153 if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001154 if (SlotIndex::isSameInstr(Idx, VNI->def))
1155 Idx = VNI->def;
1156
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001157 // Check for a sibling copy.
1158 unsigned SibReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001159 if (SibReg && isSibling(SibReg)) {
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001160 // This may actually be a copy between snippets.
1161 if (isRegToSpill(SibReg)) {
1162 DEBUG(dbgs() << "Found new snippet copy: " << *MI);
1163 SnippetCopies.insert(MI);
1164 continue;
1165 }
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001166 if (RI.Writes) {
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001167 // Hoist the spill of a sib-reg copy.
1168 if (hoistSpill(OldLI, MI)) {
1169 // This COPY is now dead, the value is already in the stack slot.
1170 MI->getOperand(0).setIsDead();
1171 DeadDefs.push_back(MI);
1172 continue;
1173 }
1174 } else {
1175 // This is a reload for a sib-reg copy. Drop spills downstream.
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001176 LiveInterval &SibLI = LIS.getInterval(SibReg);
1177 eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
1178 // The COPY will fold to a reload below.
1179 }
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001180 }
1181
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +00001182 // Attempt to fold memory ops.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001183 if (foldMemoryOperand(Ops))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +00001184 continue;
1185
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001186 // Allocate interval around instruction.
1187 // FIXME: Infer regclass from instruction alone.
Pete Cooper8a06af92012-04-02 22:22:53 +00001188 LiveInterval &NewLI = Edit->createFrom(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001189 NewLI.markNotSpillable();
1190
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001191 if (RI.Reads)
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001192 insertReload(NewLI, Idx, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001193
1194 // Rewrite instruction operands.
1195 bool hasLiveDef = false;
1196 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001197 MachineOperand &MO = Ops[i].first->getOperand(Ops[i].second);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +00001198 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001199 if (MO.isUse()) {
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001200 if (!Ops[i].first->isRegTiedToDefOperand(Ops[i].second))
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001201 MO.setIsKill();
1202 } else {
1203 if (!MO.isDead())
1204 hasLiveDef = true;
1205 }
1206 }
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001207 DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001208
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001209 // FIXME: Use a second vreg if instruction has no tied ops.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001210 if (RI.Writes) {
1211 if (hasLiveDef)
1212 insertSpill(NewLI, OldLI, Idx, MI);
1213 else {
1214 // This instruction defines a dead value. We don't need to spill it,
1215 // but do create a live range for the dead value.
1216 VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
1217 NewLI.addRange(LiveRange(Idx, Idx.getDeadSlot(), VNI));
1218 }
Jakob Stoklund Olesena80444f2011-10-14 00:34:31 +00001219 }
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001220
1221 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001222 }
1223}
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001224
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001225/// spillAll - Spill all registers remaining after rematerialization.
1226void InlineSpiller::spillAll() {
1227 // Update LiveStacks now that we are committed to spilling.
1228 if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
1229 StackSlot = VRM.assignVirt2StackSlot(Original);
1230 StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +00001231 StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001232 } else
1233 StackInt = &LSS.getInterval(StackSlot);
1234
1235 if (Original != Edit->getReg())
1236 VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
1237
1238 assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
1239 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
1240 StackInt->MergeRangesInAsValue(LIS.getInterval(RegsToSpill[i]),
1241 StackInt->getValNumInfo(0));
1242 DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
1243
1244 // Spill around uses of all RegsToSpill.
1245 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
1246 spillAroundUses(RegsToSpill[i]);
1247
1248 // Hoisted spills may cause dead code.
1249 if (!DeadDefs.empty()) {
1250 DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
Pete Cooper8a06af92012-04-02 22:22:53 +00001251 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001252 }
1253
1254 // Finally delete the SnippetCopies.
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001255 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
1256 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(RegsToSpill[i]);
1257 MachineInstr *MI = RI.skipInstruction();) {
1258 assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
1259 // FIXME: Do this with a LiveRangeEdit callback.
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001260 LIS.RemoveMachineInstrFromMaps(MI);
1261 MI->eraseFromParent();
1262 }
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001263 }
1264
1265 // Delete all spilled registers.
1266 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Pete Cooper8a06af92012-04-02 22:22:53 +00001267 Edit->eraseVirtReg(RegsToSpill[i]);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001268}
1269
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001270void InlineSpiller::spill(LiveRangeEdit &edit) {
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001271 ++NumSpilledRanges;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001272 Edit = &edit;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001273 assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
1274 && "Trying to spill a stack slot.");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001275 // Share a stack slot among all descendants of Original.
1276 Original = VRM.getOriginal(edit.getReg());
1277 StackSlot = VRM.getStackSlot(Original);
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +00001278 StackInt = 0;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001279
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001280 DEBUG(dbgs() << "Inline spilling "
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001281 << MRI.getRegClass(edit.getReg())->getName()
Jakob Stoklund Olesen127cdba2012-06-15 23:47:09 +00001282 << ':' << PrintReg(edit.getReg()) << ' ' << edit.getParent()
1283 << "\nFrom original " << LIS.getInterval(Original) << '\n');
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001284 assert(edit.getParent().isSpillable() &&
1285 "Attempting to spill already spilled value.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001286 assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001287
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001288 collectRegsToSpill();
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001289 analyzeSiblingValues();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001290 reMaterializeAll();
1291
1292 // Remat may handle everything.
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001293 if (!RegsToSpill.empty())
1294 spillAll();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001295
Benjamin Kramer4eed7562013-06-17 19:00:36 +00001296 Edit->calculateRegClassAndHint(MF, Loops, MBFI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001297}