blob: 0f7ba8e01ec0d1d569e1b8b34aaa0fc2fd008d92 [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"
Benjamin Kramer4eed7562013-06-17 19:00:36 +000024#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Stephen Hines36b56882014-04-23 16:57:46 -070025#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
26#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
David Blaikie6d9dbd52013-06-16 20:34:15 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000031#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000033#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenb9edad02011-09-15 21:06:00 +000034#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000037#include "llvm/Target/TargetInstrInfo.h"
38#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000039
40using namespace llvm;
41
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000042STATISTIC(NumSpilledRanges, "Number of spilled live ranges");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000043STATISTIC(NumSnippets, "Number of spilled snippets");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000044STATISTIC(NumSpills, "Number of spills inserted");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000045STATISTIC(NumSpillsRemoved, "Number of spills removed");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000046STATISTIC(NumReloads, "Number of reloads inserted");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000047STATISTIC(NumReloadsRemoved, "Number of reloads removed");
Jakob Stoklund Olesene9bd4ea2011-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 Olesen79c40a02011-09-15 17:54:28 +000051STATISTIC(NumOmitReloadSpill, "Number of omitted spills of reloads");
52STATISTIC(NumHoists, "Number of hoisted spills");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000053
Jakob Stoklund Olesenb9edad02011-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 Olesen914f2ff2010-06-29 23:58:39 +000057namespace {
58class InlineSpiller : public Spiller {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000059 MachineFunction &MF;
60 LiveIntervals &LIS;
61 LiveStacks &LSS;
62 AliasAnalysis *AA;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000063 MachineDominatorTree &MDT;
64 MachineLoopInfo &Loops;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000065 VirtRegMap &VRM;
66 MachineFrameInfo &MFI;
67 MachineRegisterInfo &MRI;
68 const TargetInstrInfo &TII;
69 const TargetRegisterInfo &TRI;
Benjamin Kramer4eed7562013-06-17 19:00:36 +000070 const MachineBlockFrequencyInfo &MBFI;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000071
72 // Variables that are valid during spill(), but used by multiple methods.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000073 LiveRangeEdit *Edit;
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +000074 LiveInterval *StackInt;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000075 int StackSlot;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000076 unsigned Original;
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000077
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000078 // All registers to spill to StackSlot, including the main register.
Jakob Stoklund Olesen10a43322011-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 Olesen080c3162010-10-20 22:00:51 +000085 // Values that failed to remat at some point.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000086 SmallPtrSet<VNInfo*, 8> UsedValues;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000087
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +000088public:
Jakob Stoklund Olesen13ba2522011-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 Olesen1ab7c8e2011-09-09 18:11:41 +000095 // True when value is defined by an original PHI not from splitting.
96 bool DefByOrigPHI;
97
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +000098 // True when the COPY defining this value killed its source.
99 bool KillsSource;
100
Jakob Stoklund Olesen13ba2522011-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 Olesen1ab7c8e2011-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 Olesen13ba2522011-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 Olesen1ab7c8e2011-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 Olesen13ba2522011-03-15 21:13:25 +0000122 SibValueInfo(unsigned Reg, VNInfo *VNI)
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000123 : AllDefsAreReloads(true), DefByOrigPHI(false), KillsSource(false),
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000124 SpillReg(Reg), SpillVNI(VNI), SpillMBB(0), DefMI(0) {}
125
126 // Returns true when a def has been found.
127 bool hasDef() const { return DefByOrigPHI || DefMI; }
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000128 };
129
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000130private:
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000131 // Values in RegsToSpill defined by sibling copies.
Jakob Stoklund Olesen2a72bfa2011-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 Olesen13ba2522011-03-15 21:13:25 +0000137
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000138 ~InlineSpiller() {}
139
140public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000141 InlineSpiller(MachineFunctionPass &pass,
142 MachineFunction &mf,
143 VirtRegMap &vrm)
Benjamin Kramer95a9d932012-06-06 19:47:08 +0000144 : MF(mf),
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000145 LIS(pass.getAnalysis<LiveIntervals>()),
146 LSS(pass.getAnalysis<LiveStacks>()),
147 AA(&pass.getAnalysis<AliasAnalysis>()),
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000148 MDT(pass.getAnalysis<MachineDominatorTree>()),
149 Loops(pass.getAnalysis<MachineLoopInfo>()),
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000150 VRM(vrm),
151 MFI(*mf.getFrameInfo()),
152 MRI(mf.getRegInfo()),
153 TII(*mf.getTarget().getInstrInfo()),
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000154 TRI(*mf.getTarget().getRegisterInfo()),
155 MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()) {}
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000156
Stephen Hines36b56882014-04-23 16:57:46 -0700157 void spill(LiveRangeEdit &) override;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000158
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000159private:
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000160 bool isSnippet(const LiveInterval &SnipLI);
161 void collectRegsToSpill();
162
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000163 bool isRegToSpill(unsigned Reg) {
164 return std::find(RegsToSpill.begin(),
165 RegsToSpill.end(), Reg) != RegsToSpill.end();
166 }
167
168 bool isSibling(unsigned Reg);
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000169 MachineInstr *traceSiblingValue(unsigned, VNInfo*, VNInfo*);
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000170 void propagateSiblingValue(SibValueMap::iterator, VNInfo *VNI = 0);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000171 void analyzeSiblingValues();
172
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000173 bool hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000174 void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000175
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000176 void markValueUsed(LiveInterval*, VNInfo*);
177 bool reMaterializeFor(LiveInterval&, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000178 void reMaterializeAll();
179
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000180 bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000181 bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> >,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000182 MachineInstr *LoadMI = 0);
Mark Laceye742d682013-08-14 23:50:16 +0000183 void insertReload(unsigned VReg, SlotIndex, MachineBasicBlock::iterator MI);
184 void insertSpill(unsigned VReg, bool isKill, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000185
186 void spillAroundUses(unsigned Reg);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000187 void spillAll();
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000188};
189}
190
191namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000192Spiller *createInlineSpiller(MachineFunctionPass &pass,
193 MachineFunction &mf,
194 VirtRegMap &vrm) {
195 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000196}
197}
198
Jakob Stoklund Olesen10a43322011-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 Espindolacfe52542011-06-30 21:15:52 +0000214 if (!MI->isFullCopy())
Jakob Stoklund Olesen10a43322011-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 Olesen766faf42011-03-14 19:56:43 +0000225/// Edit->getReg().
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000226bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000227 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-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 Olesen766faf42011-03-14 19:56:43 +0000236 if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000237 return false;
238
239 MachineInstr *UseMI = 0;
240
241 // Check that all uses satisfy our criteria.
Stephen Hines36b56882014-04-23 16:57:46 -0700242 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 Olesen10a43322011-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 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
Stephen Hines36b56882014-04-23 16:57:46 -0700282 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 Olesen10a43322011-03-12 04:17:20 +0000285 unsigned SnipReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000286 if (!isSibling(SnipReg))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000287 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000288 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000289 if (!isSnippet(SnipLI))
290 continue;
291 SnippetCopies.insert(MI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000292 if (isRegToSpill(SnipReg))
293 continue;
294 RegsToSpill.push_back(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000295 DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000296 ++NumSnippets;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000297 }
298}
299
Jakob Stoklund Olesen13ba2522011-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 Olesen2a72bfa2011-03-18 04:23:06 +0000313bool InlineSpiller::isSibling(unsigned Reg) {
314 return TargetRegisterInfo::isVirtualRegister(Reg) &&
315 VRM.getOriginal(Reg) == Original;
316}
317
Jakob Stoklund Olesen1ab7c8e2011-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 Olesen69cf1ca2011-09-16 00:03:33 +0000329 if (SVI.KillsSource)
330 OS << " kill";
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000331 OS << " deps[";
332 for (unsigned i = 0, e = SVI.Deps.size(); i != e; ++i)
333 OS << ' ' << SVI.Deps[i]->id << '@' << SVI.Deps[i]->def;
334 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 Kramer2db14ba2013-05-23 15:42:57 +0000346/// @param SVIIter SibValues entry to propagate.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000347/// @param VNI Dependent value, or NULL to propagate to all saved dependents.
Benjamin Kramer2db14ba2013-05-23 15:42:57 +0000348void InlineSpiller::propagateSiblingValue(SibValueMap::iterator SVIIter,
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000349 VNInfo *VNI) {
Benjamin Kramer2db14ba2013-05-23 15:42:57 +0000350 SibValueMap::value_type *SVI = &*SVIIter;
351
Jakob Stoklund Olesen1ab7c8e2011-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 Kramer2db14ba2013-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 Olesen1ab7c8e2011-09-09 18:11:41 +0000366
367 do {
368 SVI = WorkList.pop_back_val();
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000369 TinyPtrVector<VNInfo*> *Deps = VNI ? &FirstDeps : &SVI->second.Deps;
370 VNI = 0;
371
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 Olesenb9edad02011-09-15 21:06:00 +0000383 bool PropSpill = !DisableHoisting && !isRegToSpill(SV.SpillReg);
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000384 unsigned SpillDepth = ~0u;
385
386 for (TinyPtrVector<VNInfo*>::iterator DepI = Deps->begin(),
387 DepE = Deps->end(); DepI != DepE; ++DepI) {
388 SibValueMap::iterator DepSVI = SibValues.find(*DepI);
389 assert(DepSVI != SibValues.end() && "Dependent value not in SibValues");
390 SibValueInfo &DepSV = DepSVI->second;
391 if (!DepSV.SpillMBB)
392 DepSV.SpillMBB = LIS.getMBBFromIndex(DepSV.SpillVNI->def);
393
394 bool Changed = false;
395
396 // Propagate defining instruction.
397 if (!DepSV.hasDef()) {
398 Changed = true;
399 DepSV.DefMI = SV.DefMI;
400 DepSV.DefByOrigPHI = SV.DefByOrigPHI;
401 }
402
403 // Propagate AllDefsAreReloads. For PHI values, this computes an AND of
404 // all predecessors.
405 if (!SV.AllDefsAreReloads && DepSV.AllDefsAreReloads) {
406 Changed = true;
407 DepSV.AllDefsAreReloads = false;
408 }
409
410 // Propagate best spill value.
411 if (PropSpill && SV.SpillVNI != DepSV.SpillVNI) {
412 if (SV.SpillMBB == DepSV.SpillMBB) {
413 // DepSV is in the same block. Hoist when dominated.
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000414 if (DepSV.KillsSource && SV.SpillVNI->def < DepSV.SpillVNI->def) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000415 // This is an alternative def earlier in the same MBB.
416 // Hoist the spill as far as possible in SpillMBB. This can ease
417 // register pressure:
418 //
419 // x = def
420 // y = use x
421 // s = copy x
422 //
423 // Hoisting the spill of s to immediately after the def removes the
424 // interference between x and y:
425 //
426 // x = def
427 // spill x
428 // y = use x<kill>
429 //
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000430 // This hoist only helps when the DepSV copy kills its source.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000431 Changed = true;
432 DepSV.SpillReg = SV.SpillReg;
433 DepSV.SpillVNI = SV.SpillVNI;
434 DepSV.SpillMBB = SV.SpillMBB;
435 }
436 } else {
437 // DepSV is in a different block.
438 if (SpillDepth == ~0u)
439 SpillDepth = Loops.getLoopDepth(SV.SpillMBB);
440
441 // Also hoist spills to blocks with smaller loop depth, but make sure
442 // that the new value dominates. Non-phi dependents are always
443 // dominated, phis need checking.
Stephen Hines36b56882014-04-23 16:57:46 -0700444
445 const BranchProbability MarginProb(4, 5); // 80%
446 // Hoist a spill to outer loop if there are multiple dependents (it
447 // can be beneficial if more than one dependents are hoisted) or
448 // if DepSV (the hoisting source) is hotter than SV (the hoisting
449 // destination) (we add a 80% margin to bias a little towards
450 // loop depth).
451 bool HoistCondition =
452 (MBFI.getBlockFreq(DepSV.SpillMBB) >=
453 (MBFI.getBlockFreq(SV.SpillMBB) * MarginProb)) ||
454 Deps->size() > 1;
455
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000456 if ((Loops.getLoopDepth(DepSV.SpillMBB) > SpillDepth) &&
Stephen Hines36b56882014-04-23 16:57:46 -0700457 HoistCondition &&
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000458 (!DepSVI->first->isPHIDef() ||
459 MDT.dominates(SV.SpillMBB, DepSV.SpillMBB))) {
460 Changed = true;
461 DepSV.SpillReg = SV.SpillReg;
462 DepSV.SpillVNI = SV.SpillVNI;
463 DepSV.SpillMBB = SV.SpillMBB;
464 }
465 }
466 }
467
468 if (!Changed)
469 continue;
470
471 // Something changed in DepSVI. Propagate to dependents.
Benjamin Kramer2db14ba2013-05-23 15:42:57 +0000472 WorkList.insert(&*DepSVI);
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000473
474 DEBUG(dbgs() << " update " << DepSVI->first->id << '@'
475 << DepSVI->first->def << " to:\t" << DepSV);
476 }
477 } while (!WorkList.empty());
478}
479
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000480/// traceSiblingValue - Trace a value that is about to be spilled back to the
481/// real defining instructions by looking through sibling copies. Always stay
482/// within the range of OrigVNI so the registers are known to carry the same
483/// value.
484///
485/// Determine if the value is defined by all reloads, so spilling isn't
486/// necessary - the value is already in the stack slot.
487///
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000488/// Return a defining instruction that may be a candidate for rematerialization.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000489///
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000490MachineInstr *InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
491 VNInfo *OrigVNI) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000492 // Check if a cached value already exists.
493 SibValueMap::iterator SVI;
494 bool Inserted;
Stephen Hines36b56882014-04-23 16:57:46 -0700495 std::tie(SVI, Inserted) =
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000496 SibValues.insert(std::make_pair(UseVNI, SibValueInfo(UseReg, UseVNI)));
497 if (!Inserted) {
498 DEBUG(dbgs() << "Cached value " << PrintReg(UseReg) << ':'
499 << UseVNI->id << '@' << UseVNI->def << ' ' << SVI->second);
500 return SVI->second.DefMI;
501 }
502
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000503 DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
504 << UseVNI->id << '@' << UseVNI->def << '\n');
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000505
506 // List of (Reg, VNI) that have been inserted into SibValues, but need to be
507 // processed.
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000508 SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000509 WorkList.push_back(std::make_pair(UseReg, UseVNI));
510
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000511 do {
512 unsigned Reg;
513 VNInfo *VNI;
Stephen Hines36b56882014-04-23 16:57:46 -0700514 std::tie(Reg, VNI) = WorkList.pop_back_val();
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000515 DEBUG(dbgs() << " " << PrintReg(Reg) << ':' << VNI->id << '@' << VNI->def
516 << ":\t");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000517
Jakob Stoklund Olesen1ab7c8e2011-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 Olesen13ba2522011-03-15 21:13:25 +0000521
522 // Trace through PHI-defs created by live range splitting.
523 if (VNI->isPHIDef()) {
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000524 // Stop at original PHIs. We don't know the value at the predecessors.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000525 if (VNI->def == OrigVNI->def) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000526 DEBUG(dbgs() << "orig phi value\n");
527 SVI->second.DefByOrigPHI = true;
528 SVI->second.AllDefsAreReloads = false;
529 propagateSiblingValue(SVI);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000530 continue;
531 }
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000532
533 // This is a PHI inserted by live range splitting. We could trace the
534 // live-out value from predecessor blocks, but that search can be very
535 // expensive if there are many predecessors and many more PHIs as
536 // generated by tail-dup when it sees an indirectbr. Instead, look at
537 // all the non-PHI defs that have the same value as OrigVNI. They must
538 // jointly dominate VNI->def. This is not optimal since VNI may actually
539 // be jointly dominated by a smaller subset of defs, so there is a change
540 // we will miss a AllDefsAreReloads optimization.
541
542 // Separate all values dominated by OrigVNI into PHIs and non-PHIs.
543 SmallVector<VNInfo*, 8> PHIs, NonPHIs;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000544 LiveInterval &LI = LIS.getInterval(Reg);
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000545 LiveInterval &OrigLI = LIS.getInterval(Original);
546
547 for (LiveInterval::vni_iterator VI = LI.vni_begin(), VE = LI.vni_end();
548 VI != VE; ++VI) {
549 VNInfo *VNI2 = *VI;
550 if (VNI2->isUnused())
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000551 continue;
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000552 if (!OrigLI.containsOneValue() &&
553 OrigLI.getVNInfoAt(VNI2->def) != OrigVNI)
554 continue;
555 if (VNI2->isPHIDef() && VNI2->def != OrigVNI->def)
556 PHIs.push_back(VNI2);
557 else
558 NonPHIs.push_back(VNI2);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000559 }
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000560 DEBUG(dbgs() << "split phi value, checking " << PHIs.size()
561 << " phi-defs, and " << NonPHIs.size()
562 << " non-phi/orig defs\n");
563
564 // Create entries for all the PHIs. Don't add them to the worklist, we
565 // are processing all of them in one go here.
566 for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
567 SibValues.insert(std::make_pair(PHIs[i], SibValueInfo(Reg, PHIs[i])));
568
569 // Add every PHI as a dependent of all the non-PHIs.
570 for (unsigned i = 0, e = NonPHIs.size(); i != e; ++i) {
571 VNInfo *NonPHI = NonPHIs[i];
572 // Known value? Try an insertion.
Stephen Hines36b56882014-04-23 16:57:46 -0700573 std::tie(SVI, Inserted) =
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000574 SibValues.insert(std::make_pair(NonPHI, SibValueInfo(Reg, NonPHI)));
575 // Add all the PHIs as dependents of NonPHI.
576 for (unsigned pi = 0, pe = PHIs.size(); pi != pe; ++pi)
577 SVI->second.Deps.push_back(PHIs[pi]);
578 // This is the first time we see NonPHI, add it to the worklist.
579 if (Inserted)
580 WorkList.push_back(std::make_pair(Reg, NonPHI));
581 else
582 // Propagate to all inserted PHIs, not just VNI.
583 propagateSiblingValue(SVI);
584 }
585
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000586 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000587 continue;
588 }
589
590 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
591 assert(MI && "Missing def");
592
593 // Trace through sibling copies.
594 if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000595 if (isSibling(SrcReg)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000596 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
Matthias Braun5649e252013-10-10 21:28:52 +0000597 LiveQueryResult SrcQ = SrcLI.Query(VNI->def);
Jakob Stoklund Olesen92a05fa2012-05-20 02:44:33 +0000598 assert(SrcQ.valueIn() && "Copy from non-existing value");
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000599 // Check if this COPY kills its source.
Jakob Stoklund Olesen92a05fa2012-05-20 02:44:33 +0000600 SVI->second.KillsSource = SrcQ.isKill();
601 VNInfo *SrcVNI = SrcQ.valueIn();
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000602 DEBUG(dbgs() << "copy of " << PrintReg(SrcReg) << ':'
Jakob Stoklund Olesen69cf1ca2011-09-16 00:03:33 +0000603 << SrcVNI->id << '@' << SrcVNI->def
604 << " kill=" << unsigned(SVI->second.KillsSource) << '\n');
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000605 // Known sibling source value? Try an insertion.
Stephen Hines36b56882014-04-23 16:57:46 -0700606 std::tie(SVI, Inserted) = SibValues.insert(
607 std::make_pair(SrcVNI, SibValueInfo(SrcReg, SrcVNI)));
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000608 // This is the first time we see Src, add it to the worklist.
609 if (Inserted)
610 WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
611 propagateSiblingValue(SVI, VNI);
612 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000613 continue;
614 }
615 }
616
617 // Track reachable reloads.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000618 SVI->second.DefMI = MI;
619 SVI->second.SpillMBB = MI->getParent();
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000620 int FI;
621 if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000622 DEBUG(dbgs() << "reload\n");
623 propagateSiblingValue(SVI);
624 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000625 continue;
626 }
627
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000628 // Potential remat candidate.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000629 DEBUG(dbgs() << "def " << *MI);
630 SVI->second.AllDefsAreReloads = false;
631 propagateSiblingValue(SVI);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000632 } while (!WorkList.empty());
633
Logan Chiene2ac5522012-09-01 12:11:41 +0000634 // Look up the value we were looking for. We already did this lookup at the
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000635 // top of the function, but SibValues may have been invalidated.
636 SVI = SibValues.find(UseVNI);
637 assert(SVI != SibValues.end() && "Didn't compute requested info");
638 DEBUG(dbgs() << " traced to:\t" << SVI->second);
639 return SVI->second.DefMI;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000640}
641
642/// analyzeSiblingValues - Trace values defined by sibling copies back to
643/// something that isn't a sibling copy.
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000644///
645/// Keep track of values that may be rematerializable.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000646void InlineSpiller::analyzeSiblingValues() {
647 SibValues.clear();
648
649 // No siblings at all?
650 if (Edit->getReg() == Original)
651 return;
652
653 LiveInterval &OrigLI = LIS.getInterval(Original);
654 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
655 unsigned Reg = RegsToSpill[i];
656 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 Olesen2ef661b2011-03-29 03:12:02 +0000660 if (VNI->isUnused())
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000661 continue;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000662 MachineInstr *DefMI = 0;
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000663 if (!VNI->isPHIDef()) {
664 DefMI = LIS.getInstructionFromIndex(VNI->def);
665 assert(DefMI && "No defining instruction");
666 }
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000667 // Check possible sibling copies.
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000668 if (VNI->isPHIDef() || DefMI->isCopy()) {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000669 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen9693d4c2011-07-05 15:38:41 +0000670 assert(OrigVNI && "Def outside original live range");
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000671 if (OrigVNI->def != VNI->def)
672 DefMI = traceSiblingValue(Reg, VNI, OrigVNI);
673 }
Pete Cooper8a06af92012-04-02 22:22:53 +0000674 if (DefMI && Edit->checkRematerializable(VNI, DefMI, AA)) {
Jakob Stoklund Olesen3b7d9172011-04-20 22:14:20 +0000675 DEBUG(dbgs() << "Value " << PrintReg(Reg) << ':' << VNI->id << '@'
676 << VNI->def << " may remat from " << *DefMI);
677 }
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000678 }
679 }
680}
681
Jakob Stoklund Olesen2a72bfa2011-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 Olesen2debd482011-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 Olesen6ee56e62011-04-30 06:42:21 +0000688 SibValueMap::iterator I = SibValues.find(VNI);
Jakob Stoklund Olesen2a72bfa2011-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 Olesen6ee56e62011-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 Olesen2a72bfa2011-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 Olesene9c50732011-03-26 22:16:41 +0000715 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000716 LiveInterval &OrigLI = LIS.getInterval(Original);
717 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000718 StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +0000719 DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000720 << *StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000721
722 // Already spilled everywhere.
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000723 if (SVI.AllDefsAreReloads) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000724 DEBUG(dbgs() << "\tno spill needed: " << SVI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000725 ++NumOmitReloadSpill;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000726 return true;
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000727 }
Jakob Stoklund Olesen2a72bfa2011-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 Olesen6ee56e62011-04-30 06:42:21 +0000730 eliminateRedundantSpills(SibLI, SVI.SpillVNI);
Jakob Stoklund Olesen2a72bfa2011-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 Olesen6ee56e62011-04-30 06:42:21 +0000737 MachineInstr *DefMI = LIS.getInstructionFromIndex(SVI.SpillVNI->def);
738 assert(DefMI && "Defining instruction disappeared");
739 MII = DefMI;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000740 ++MII;
741 }
742 // Insert spill without kill flag immediately after def.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000743 TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot,
744 MRI.getRegClass(SVI.SpillReg), &TRI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000745 --MII; // Point to store instruction.
746 LIS.InsertMachineInstrInMaps(MII);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000747 DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000748
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +0000749 ++NumSpills;
750 ++NumHoists;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000751 return true;
752}
753
Jakob Stoklund Olesen01a46c82011-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 Olesen682eed02011-03-20 05:44:58 +0000757 assert(VNI && "Missing value");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000758 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
759 WorkList.push_back(std::make_pair(&SLI, VNI));
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000760 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000761
762 do {
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000763 LiveInterval *LI;
Stephen Hines36b56882014-04-23 16:57:46 -0700764 std::tie(LI, VNI) = WorkList.pop_back_val();
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000765 unsigned Reg = LI->reg;
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000766 DEBUG(dbgs() << "Checking redundant spills for "
767 << VNI->id << '@' << VNI->def << " in " << *LI << '\n');
Jakob Stoklund Olesen2a72bfa2011-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 Olesene9c50732011-03-26 22:16:41 +0000774 StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
775 DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000776
777 // Find all spills and copies of VNI.
Stephen Hines36b56882014-04-23 16:57:46 -0700778 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 Cheng5a96b3d2011-12-07 07:15:52 +0000782 if (!MI->isCopy() && !MI->mayStore())
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000783 continue;
784 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000785 if (LI->getVNInfoAt(Idx) != VNI)
Jakob Stoklund Olesen2a72bfa2011-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 Olesen2debd482011-11-13 20:45:27 +0000792 VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000793 assert(DstVNI && "Missing defined value");
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000794 assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000795 WorkList.push_back(std::make_pair(&DstLI, DstVNI));
Jakob Stoklund Olesen2a72bfa2011-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 Olesen79c40a02011-09-15 17:54:28 +0000807 ++NumSpillsRemoved;
808 --NumSpills;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000809 }
810 }
811 } while (!WorkList.empty());
812}
813
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000814
Jakob Stoklund Olesen2ef661b2011-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 {
Stephen Hines36b56882014-04-23 16:57:46 -0700825 std::tie(LI, VNI) = WorkList.pop_back_val();
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000826 if (!UsedValues.insert(VNI))
827 continue;
828
829 if (VNI->isPHIDef()) {
830 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
831 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
832 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen194eb712011-11-14 01:39:36 +0000833 VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI));
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000834 if (PVNI)
835 WorkList.push_back(std::make_pair(LI, PVNI));
836 }
837 continue;
838 }
839
840 // Follow snippet copies.
841 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
842 if (!SnippetCopies.count(MI))
843 continue;
844 LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
845 assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000846 VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000847 assert(SnipVNI && "Snippet undefined before copy");
848 WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
849 } while (!WorkList.empty());
850}
851
852/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
853bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg,
854 MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000855 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
Jakob Stoklund Olesen79413502011-07-18 05:31:59 +0000856 VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000857
858 if (!ParentVNI) {
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000859 DEBUG(dbgs() << "\tadding <undef> flags: ");
860 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
861 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000862 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000863 MO.setIsUndef();
864 }
865 DEBUG(dbgs() << UseIdx << '\t' << *MI);
866 return true;
867 }
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000868
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000869 if (SnippetCopies.count(MI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000870 return false;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000871
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000872 // Use an OrigVNI from traceSiblingValue when ParentVNI is a sibling copy.
873 LiveRangeEdit::Remat RM(ParentVNI);
874 SibValueMap::const_iterator SibI = SibValues.find(ParentVNI);
875 if (SibI != SibValues.end())
876 RM.OrigMI = SibI->second.DefMI;
Pete Cooper8a06af92012-04-02 22:22:53 +0000877 if (!Edit->canRematerializeAt(RM, UseIdx, false)) {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000878 markValueUsed(&VirtReg, ParentVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000879 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
880 return false;
881 }
882
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000883 // If the instruction also writes VirtReg.reg, it had better not require the
884 // same register for uses and defs.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000885 SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
James Molloyb17cf292012-09-12 10:03:31 +0000886 MIBundleOperands::VirtRegInfo RI =
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000887 MIBundleOperands(MI).analyzeVirtReg(VirtReg.reg, &Ops);
888 if (RI.Tied) {
889 markValueUsed(&VirtReg, ParentVNI);
890 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
891 return false;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000892 }
893
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000894 // Before rematerializing into a register for a single instruction, try to
895 // fold a load into the instruction. That avoids allocating a new register.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000896 if (RM.OrigMI->canFoldAsLoad() &&
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000897 foldMemoryOperand(Ops, RM.OrigMI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000898 Edit->markRematerialized(RM.ParentVNI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000899 ++NumFoldedLoads;
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000900 return true;
901 }
902
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000903 // Alocate a new register for the remat.
Mark Laceye742d682013-08-14 23:50:16 +0000904 unsigned NewVReg = Edit->createFrom(Original);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000905
906 // Finally we can rematerialize OrigMI before MI.
Mark Laceye742d682013-08-14 23:50:16 +0000907 SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewVReg, RM,
Pete Cooper8a06af92012-04-02 22:22:53 +0000908 TRI);
Mark Laceye742d682013-08-14 23:50:16 +0000909 (void)DefIdx;
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000910 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000911 << *LIS.getInstructionFromIndex(DefIdx));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000912
913 // Replace operands
914 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +0000915 MachineOperand &MO = MI->getOperand(Ops[i].second);
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000916 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
Mark Laceye742d682013-08-14 23:50:16 +0000917 MO.setReg(NewVReg);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000918 MO.setIsKill();
919 }
920 }
Mark Laceye742d682013-08-14 23:50:16 +0000921 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI << '\n');
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000922
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000923 ++NumRemats;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000924 return true;
925}
926
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000927/// reMaterializeAll - Try to rematerialize as many uses as possible,
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000928/// and trim the live ranges after.
929void InlineSpiller::reMaterializeAll() {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000930 // analyzeSiblingValues has already tested all relevant defining instructions.
Pete Cooper8a06af92012-04-02 22:22:53 +0000931 if (!Edit->anyRematerializable(AA))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000932 return;
933
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000934 UsedValues.clear();
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000935
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000936 // Try to remat before all uses of snippets.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000937 bool anyRemat = false;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000938 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
939 unsigned Reg = RegsToSpill[i];
940 LiveInterval &LI = LIS.getInterval(Reg);
Stephen Hines36b56882014-04-23 16:57:46 -0700941 for (MachineRegisterInfo::use_bundle_nodbg_iterator
942 RI = MRI.use_bundle_nodbg_begin(Reg), E = MRI.use_bundle_nodbg_end();
943 RI != E; ) {
944 MachineInstr *MI = &*(RI++);
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000945 anyRemat |= reMaterializeFor(LI, MI);
Stephen Hines36b56882014-04-23 16:57:46 -0700946 }
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000947 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000948 if (!anyRemat)
949 return;
950
951 // Remove any values that were completely rematted.
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000952 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
953 unsigned Reg = RegsToSpill[i];
954 LiveInterval &LI = LIS.getInterval(Reg);
955 for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
956 I != E; ++I) {
957 VNInfo *VNI = *I;
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000958 if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000959 continue;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000960 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
961 MI->addRegisterDead(Reg, &TRI);
962 if (!MI->allDefsAreDead())
963 continue;
964 DEBUG(dbgs() << "All defs dead: " << *MI);
965 DeadDefs.push_back(MI);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000966 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000967 }
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000968
969 // Eliminate dead code after remat. Note that some snippet copies may be
970 // deleted here.
971 if (DeadDefs.empty())
972 return;
973 DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
Pete Cooper8a06af92012-04-02 22:22:53 +0000974 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000975
976 // Get rid of deleted and empty intervals.
Benjamin Kramere210df22013-05-05 11:29:14 +0000977 unsigned ResultPos = 0;
978 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
979 unsigned Reg = RegsToSpill[i];
980 if (!LIS.hasInterval(Reg))
981 continue;
982
983 LiveInterval &LI = LIS.getInterval(Reg);
984 if (LI.empty()) {
985 Edit->eraseVirtReg(Reg);
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000986 continue;
987 }
Benjamin Kramere210df22013-05-05 11:29:14 +0000988
989 RegsToSpill[ResultPos++] = Reg;
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000990 }
Benjamin Kramere210df22013-05-05 11:29:14 +0000991 RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000992 DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n");
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000993}
994
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000995
996//===----------------------------------------------------------------------===//
997// Spilling
998//===----------------------------------------------------------------------===//
999
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001000/// If MI is a load or store of StackSlot, it can be removed.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001001bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001002 int FI = 0;
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +00001003 unsigned InstrReg = TII.isLoadFromStackSlot(MI, FI);
1004 bool IsLoad = InstrReg;
1005 if (!IsLoad)
1006 InstrReg = TII.isStoreToStackSlot(MI, FI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001007
1008 // We have a stack access. Is it the right register and slot?
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001009 if (InstrReg != Reg || FI != StackSlot)
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001010 return false;
1011
1012 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001013 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001014 MI->eraseFromParent();
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +00001015
1016 if (IsLoad) {
1017 ++NumReloadsRemoved;
1018 --NumReloads;
1019 } else {
1020 ++NumSpillsRemoved;
1021 --NumSpills;
1022 }
1023
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001024 return true;
1025}
1026
Mark Laceye742d682013-08-14 23:50:16 +00001027#if !defined(NDEBUG)
1028// Dump the range of instructions from B to E with their slot indexes.
1029static void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B,
1030 MachineBasicBlock::iterator E,
1031 LiveIntervals const &LIS,
1032 const char *const header,
1033 unsigned VReg =0) {
1034 char NextLine = '\n';
1035 char SlotIndent = '\t';
1036
Stephen Hines36b56882014-04-23 16:57:46 -07001037 if (std::next(B) == E) {
Mark Laceye742d682013-08-14 23:50:16 +00001038 NextLine = ' ';
1039 SlotIndent = ' ';
1040 }
1041
1042 dbgs() << '\t' << header << ": " << NextLine;
1043
1044 for (MachineBasicBlock::iterator I = B; I != E; ++I) {
1045 SlotIndex Idx = LIS.getInstructionIndex(I).getRegSlot();
1046
1047 // If a register was passed in and this instruction has it as a
1048 // destination that is marked as an early clobber, print the
1049 // early-clobber slot index.
1050 if (VReg) {
1051 MachineOperand *MO = I->findRegisterDefOperand(VReg);
1052 if (MO && MO->isEarlyClobber())
1053 Idx = Idx.getRegSlot(true);
1054 }
1055
1056 dbgs() << SlotIndent << Idx << '\t' << *I;
1057 }
1058}
1059#endif
1060
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001061/// foldMemoryOperand - Try folding stack slot references in Ops into their
1062/// instructions.
1063///
1064/// @param Ops Operand indices from analyzeVirtReg().
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +00001065/// @param LoadMI Load instruction to use instead of stack slot when non-null.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001066/// @return True on success.
1067bool InlineSpiller::
1068foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops,
1069 MachineInstr *LoadMI) {
1070 if (Ops.empty())
1071 return false;
1072 // Don't attempt folding in bundles.
1073 MachineInstr *MI = Ops.front().first;
1074 if (Ops.back().first != MI || MI->isBundled())
1075 return false;
1076
Jakob Stoklund Olesend205f7a2011-09-15 18:22:52 +00001077 bool WasCopy = MI->isCopy();
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001078 unsigned ImpReg = 0;
1079
Andrew Trickbb756ca2013-11-17 01:36:23 +00001080 bool SpillSubRegs = (MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1081 MI->getOpcode() == TargetOpcode::STACKMAP);
1082
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001083 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
1084 // operands.
1085 SmallVector<unsigned, 8> FoldOps;
1086 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001087 unsigned Idx = Ops[i].second;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001088 MachineOperand &MO = MI->getOperand(Idx);
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001089 if (MO.isImplicit()) {
1090 ImpReg = MO.getReg();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001091 continue;
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001092 }
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001093 // FIXME: Teach targets to deal with subregs.
Andrew Trickbb756ca2013-11-17 01:36:23 +00001094 if (!SpillSubRegs && MO.getSubReg())
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001095 return false;
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +00001096 // We cannot fold a load instruction into a def.
1097 if (LoadMI && MO.isDef())
1098 return false;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001099 // Tied use operands should not be passed to foldMemoryOperand.
1100 if (!MI->isRegTiedToDefOperand(Idx))
1101 FoldOps.push_back(Idx);
1102 }
1103
Mark Laceye742d682013-08-14 23:50:16 +00001104 MachineInstrSpan MIS(MI);
1105
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +00001106 MachineInstr *FoldMI =
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001107 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
1108 : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001109 if (!FoldMI)
1110 return false;
Andrew Trickc12c8802013-06-21 18:33:26 +00001111
1112 // Remove LIS for any dead defs in the original MI not in FoldMI.
1113 for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
1114 if (!MO->isReg())
1115 continue;
1116 unsigned Reg = MO->getReg();
1117 if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) ||
1118 MRI.isReserved(Reg)) {
1119 continue;
1120 }
Stephen Hines36b56882014-04-23 16:57:46 -07001121 // Skip non-Defs, including undef uses and internal reads.
1122 if (MO->isUse())
1123 continue;
Andrew Trickc12c8802013-06-21 18:33:26 +00001124 MIBundleOperands::PhysRegInfo RI =
1125 MIBundleOperands(FoldMI).analyzePhysReg(Reg, &TRI);
Andrew Trickc12c8802013-06-21 18:33:26 +00001126 if (RI.Defines)
1127 continue;
1128 // FoldMI does not define this physreg. Remove the LI segment.
1129 assert(MO->isDead() && "Cannot fold physreg def");
1130 for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) {
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001131 if (LiveRange *LR = LIS.getCachedRegUnit(*Units)) {
Andrew Trickc12c8802013-06-21 18:33:26 +00001132 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
Matthias Braun4f3b5e82013-10-10 21:29:02 +00001133 if (VNInfo *VNI = LR->getVNInfoAt(Idx))
1134 LR->removeValNo(VNI);
Andrew Trickc12c8802013-06-21 18:33:26 +00001135 }
1136 }
1137 }
Mark Laceye742d682013-08-14 23:50:16 +00001138
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001139 LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001140 MI->eraseFromParent();
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001141
Mark Laceye742d682013-08-14 23:50:16 +00001142 // Insert any new instructions other than FoldMI into the LIS maps.
1143 assert(!MIS.empty() && "Unexpected empty span of instructions!");
1144 for (MachineBasicBlock::iterator MII = MIS.begin(), End = MIS.end();
1145 MII != End; ++MII)
1146 if (&*MII != FoldMI)
1147 LIS.InsertMachineInstrInMaps(&*MII);
1148
Jakob Stoklund Olesen17afb062011-11-10 00:17:03 +00001149 // TII.foldMemoryOperand may have left some implicit operands on the
1150 // instruction. Strip them.
1151 if (ImpReg)
1152 for (unsigned i = FoldMI->getNumOperands(); i; --i) {
1153 MachineOperand &MO = FoldMI->getOperand(i - 1);
1154 if (!MO.isReg() || !MO.isImplicit())
1155 break;
1156 if (MO.getReg() == ImpReg)
1157 FoldMI->RemoveOperand(i - 1);
1158 }
1159
Mark Laceye742d682013-08-14 23:50:16 +00001160 DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS,
1161 "folded"));
1162
Jakob Stoklund Olesend205f7a2011-09-15 18:22:52 +00001163 if (!WasCopy)
1164 ++NumFolded;
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001165 else if (Ops.front().second == 0)
Jakob Stoklund Olesend205f7a2011-09-15 18:22:52 +00001166 ++NumSpills;
1167 else
1168 ++NumReloads;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001169 return true;
1170}
1171
Mark Laceye742d682013-08-14 23:50:16 +00001172void InlineSpiller::insertReload(unsigned NewVReg,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001173 SlotIndex Idx,
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001174 MachineBasicBlock::iterator MI) {
1175 MachineBasicBlock &MBB = *MI->getParent();
Mark Laceye742d682013-08-14 23:50:16 +00001176
1177 MachineInstrSpan MIS(MI);
1178 TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot,
1179 MRI.getRegClass(NewVReg), &TRI);
1180
1181 LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI);
1182
1183 DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload",
1184 NewVReg));
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001185 ++NumReloads;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001186}
1187
Mark Laceye742d682013-08-14 23:50:16 +00001188/// insertSpill - Insert a spill of NewVReg after MI.
1189void InlineSpiller::insertSpill(unsigned NewVReg, bool isKill,
1190 MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001191 MachineBasicBlock &MBB = *MI->getParent();
Mark Laceye742d682013-08-14 23:50:16 +00001192
1193 MachineInstrSpan MIS(MI);
Stephen Hines36b56882014-04-23 16:57:46 -07001194 TII.storeRegToStackSlot(MBB, std::next(MI), NewVReg, isKill, StackSlot,
Mark Laceye742d682013-08-14 23:50:16 +00001195 MRI.getRegClass(NewVReg), &TRI);
1196
Stephen Hines36b56882014-04-23 16:57:46 -07001197 LIS.InsertMachineInstrRangeInMaps(std::next(MI), MIS.end());
Mark Laceye742d682013-08-14 23:50:16 +00001198
Stephen Hines36b56882014-04-23 16:57:46 -07001199 DEBUG(dumpMachineInstrRangeWithSlotIndex(std::next(MI), MIS.end(), LIS,
Mark Laceye742d682013-08-14 23:50:16 +00001200 "spill"));
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001201 ++NumSpills;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001202}
1203
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001204/// spillAroundUses - insert spill code around each use of Reg.
1205void InlineSpiller::spillAroundUses(unsigned Reg) {
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001206 DEBUG(dbgs() << "spillAroundUses " << PrintReg(Reg) << '\n');
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001207 LiveInterval &OldLI = LIS.getInterval(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001208
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001209 // Iterate over instructions using Reg.
Stephen Hines36b56882014-04-23 16:57:46 -07001210 for (MachineRegisterInfo::reg_bundle_iterator
1211 RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
1212 RegI != E; ) {
1213 MachineInstr *MI = &*(RegI++);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001214
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +00001215 // Debug values are not allowed to affect codegen.
1216 if (MI->isDebugValue()) {
1217 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantl818833f2013-09-16 23:29:03 +00001218 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantl4759f262013-07-10 16:56:47 +00001219 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +00001220 const MDNode *MDPtr = MI->getOperand(2).getMetadata();
1221 DebugLoc DL = MI->getDebugLoc();
David Blaikie6d9dbd52013-06-16 20:34:15 +00001222 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
1223 MachineBasicBlock *MBB = MI->getParent();
1224 BuildMI(*MBB, MBB->erase(MI), DL, TII.get(TargetOpcode::DBG_VALUE))
1225 .addFrameIndex(StackSlot).addImm(Offset).addMetadata(MDPtr);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +00001226 continue;
1227 }
1228
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001229 // Ignore copies to/from snippets. We'll delete them.
1230 if (SnippetCopies.count(MI))
1231 continue;
1232
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001233 // Stack slot accesses may coalesce away.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001234 if (coalesceStackAccess(MI, Reg))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001235 continue;
1236
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001237 // Analyze instruction.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001238 SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
James Molloyb17cf292012-09-12 10:03:31 +00001239 MIBundleOperands::VirtRegInfo RI =
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001240 MIBundleOperands(MI).analyzeVirtReg(Reg, &Ops);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001241
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001242 // Find the slot index where this instruction reads and writes OldLI.
1243 // This is usually the def slot, except for tied early clobbers.
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +00001244 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
1245 if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001246 if (SlotIndex::isSameInstr(Idx, VNI->def))
1247 Idx = VNI->def;
1248
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001249 // Check for a sibling copy.
1250 unsigned SibReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001251 if (SibReg && isSibling(SibReg)) {
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001252 // This may actually be a copy between snippets.
1253 if (isRegToSpill(SibReg)) {
1254 DEBUG(dbgs() << "Found new snippet copy: " << *MI);
1255 SnippetCopies.insert(MI);
1256 continue;
1257 }
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001258 if (RI.Writes) {
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001259 // Hoist the spill of a sib-reg copy.
1260 if (hoistSpill(OldLI, MI)) {
1261 // This COPY is now dead, the value is already in the stack slot.
1262 MI->getOperand(0).setIsDead();
1263 DeadDefs.push_back(MI);
1264 continue;
1265 }
1266 } else {
1267 // This is a reload for a sib-reg copy. Drop spills downstream.
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001268 LiveInterval &SibLI = LIS.getInterval(SibReg);
1269 eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
1270 // The COPY will fold to a reload below.
1271 }
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001272 }
1273
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +00001274 // Attempt to fold memory ops.
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001275 if (foldMemoryOperand(Ops))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +00001276 continue;
1277
Mark Laceye742d682013-08-14 23:50:16 +00001278 // Create a new virtual register for spill/fill.
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001279 // FIXME: Infer regclass from instruction alone.
Mark Laceye742d682013-08-14 23:50:16 +00001280 unsigned NewVReg = Edit->createFrom(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001281
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001282 if (RI.Reads)
Mark Laceye742d682013-08-14 23:50:16 +00001283 insertReload(NewVReg, Idx, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001284
1285 // Rewrite instruction operands.
1286 bool hasLiveDef = false;
1287 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001288 MachineOperand &MO = Ops[i].first->getOperand(Ops[i].second);
Mark Laceye742d682013-08-14 23:50:16 +00001289 MO.setReg(NewVReg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001290 if (MO.isUse()) {
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001291 if (!Ops[i].first->isRegTiedToDefOperand(Ops[i].second))
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001292 MO.setIsKill();
1293 } else {
1294 if (!MO.isDead())
1295 hasLiveDef = true;
1296 }
1297 }
Mark Laceye742d682013-08-14 23:50:16 +00001298 DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI << '\n');
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001299
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001300 // FIXME: Use a second vreg if instruction has no tied ops.
Mark Laceye742d682013-08-14 23:50:16 +00001301 if (RI.Writes)
Jakob Stoklund Olesen66c994c2012-03-01 01:43:25 +00001302 if (hasLiveDef)
Mark Laceye742d682013-08-14 23:50:16 +00001303 insertSpill(NewVReg, true, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001304 }
1305}
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001306
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001307/// spillAll - Spill all registers remaining after rematerialization.
1308void InlineSpiller::spillAll() {
1309 // Update LiveStacks now that we are committed to spilling.
1310 if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
1311 StackSlot = VRM.assignVirt2StackSlot(Original);
1312 StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +00001313 StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001314 } else
1315 StackInt = &LSS.getInterval(StackSlot);
1316
1317 if (Original != Edit->getReg())
1318 VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
1319
1320 assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
1321 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Matthias Braun331de112013-10-10 21:28:43 +00001322 StackInt->MergeSegmentsInAsValue(LIS.getInterval(RegsToSpill[i]),
1323 StackInt->getValNumInfo(0));
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001324 DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
1325
1326 // Spill around uses of all RegsToSpill.
1327 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
1328 spillAroundUses(RegsToSpill[i]);
1329
1330 // Hoisted spills may cause dead code.
1331 if (!DeadDefs.empty()) {
1332 DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
Pete Cooper8a06af92012-04-02 22:22:53 +00001333 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001334 }
1335
1336 // Finally delete the SnippetCopies.
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001337 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
Stephen Hines36b56882014-04-23 16:57:46 -07001338 for (MachineRegisterInfo::reg_instr_iterator
1339 RI = MRI.reg_instr_begin(RegsToSpill[i]), E = MRI.reg_instr_end();
1340 RI != E; ) {
1341 MachineInstr *MI = &*(RI++);
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001342 assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
1343 // FIXME: Do this with a LiveRangeEdit callback.
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001344 LIS.RemoveMachineInstrFromMaps(MI);
1345 MI->eraseFromParent();
1346 }
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001347 }
1348
1349 // Delete all spilled registers.
1350 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Pete Cooper8a06af92012-04-02 22:22:53 +00001351 Edit->eraseVirtReg(RegsToSpill[i]);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001352}
1353
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001354void InlineSpiller::spill(LiveRangeEdit &edit) {
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001355 ++NumSpilledRanges;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001356 Edit = &edit;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001357 assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
1358 && "Trying to spill a stack slot.");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001359 // Share a stack slot among all descendants of Original.
1360 Original = VRM.getOriginal(edit.getReg());
1361 StackSlot = VRM.getStackSlot(Original);
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +00001362 StackInt = 0;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001363
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001364 DEBUG(dbgs() << "Inline spilling "
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001365 << MRI.getRegClass(edit.getReg())->getName()
Matthias Braun03d96092013-10-10 21:29:05 +00001366 << ':' << edit.getParent()
Mark Laceye742d682013-08-14 23:50:16 +00001367 << "\nFrom original " << PrintReg(Original) << '\n');
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001368 assert(edit.getParent().isSpillable() &&
1369 "Attempting to spill already spilled value.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001370 assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001371
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001372 collectRegsToSpill();
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001373 analyzeSiblingValues();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001374 reMaterializeAll();
1375
1376 // Remat may handle everything.
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001377 if (!RegsToSpill.empty())
1378 spillAll();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001379
Benjamin Kramer4eed7562013-06-17 19:00:36 +00001380 Edit->calculateRegClassAndHint(MF, Loops, MBFI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001381}