1 //===- InlineSpiller.cpp - Insert spills and restores inline --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The inline spiller modifies the machine function directly instead of
10 // inserting spills and restores in VirtRegMap.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "SplitKit.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/MapVector.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/CodeGen/LiveInterval.h"
26 #include "llvm/CodeGen/LiveIntervalCalc.h"
27 #include "llvm/CodeGen/LiveIntervals.h"
28 #include "llvm/CodeGen/LiveRangeEdit.h"
29 #include "llvm/CodeGen/LiveStacks.h"
30 #include "llvm/CodeGen/MachineBasicBlock.h"
31 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
32 #include "llvm/CodeGen/MachineDominators.h"
33 #include "llvm/CodeGen/MachineFunction.h"
34 #include "llvm/CodeGen/MachineFunctionPass.h"
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/MachineInstrBuilder.h"
37 #include "llvm/CodeGen/MachineInstrBundle.h"
38 #include "llvm/CodeGen/MachineLoopInfo.h"
39 #include "llvm/CodeGen/MachineOperand.h"
40 #include "llvm/CodeGen/MachineRegisterInfo.h"
41 #include "llvm/CodeGen/SlotIndexes.h"
42 #include "llvm/CodeGen/Spiller.h"
43 #include "llvm/CodeGen/StackMaps.h"
44 #include "llvm/CodeGen/TargetInstrInfo.h"
45 #include "llvm/CodeGen/TargetOpcodes.h"
46 #include "llvm/CodeGen/TargetRegisterInfo.h"
47 #include "llvm/CodeGen/TargetSubtargetInfo.h"
48 #include "llvm/CodeGen/VirtRegMap.h"
49 #include "llvm/Config/llvm-config.h"
50 #include "llvm/Support/BlockFrequency.h"
51 #include "llvm/Support/BranchProbability.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/ErrorHandling.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include <cassert>
58 #include <iterator>
59 #include <tuple>
60 #include <utility>
61 #include <vector>
62 
63 using namespace llvm;
64 
65 #define DEBUG_TYPE "regalloc"
66 
67 STATISTIC(NumSpilledRanges,   "Number of spilled live ranges");
68 STATISTIC(NumSnippets,        "Number of spilled snippets");
69 STATISTIC(NumSpills,          "Number of spills inserted");
70 STATISTIC(NumSpillsRemoved,   "Number of spills removed");
71 STATISTIC(NumReloads,         "Number of reloads inserted");
72 STATISTIC(NumReloadsRemoved,  "Number of reloads removed");
73 STATISTIC(NumFolded,          "Number of folded stack accesses");
74 STATISTIC(NumFoldedLoads,     "Number of folded loads");
75 STATISTIC(NumRemats,          "Number of rematerialized defs for spilling");
76 
77 static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden,
78                                      cl::desc("Disable inline spill hoisting"));
79 static cl::opt<bool>
80 RestrictStatepointRemat("restrict-statepoint-remat",
81                        cl::init(false), cl::Hidden,
82                        cl::desc("Restrict remat for statepoint operands"));
83 
84 namespace {
85 
86 class HoistSpillHelper : private LiveRangeEdit::Delegate {
87   MachineFunction &MF;
88   LiveIntervals &LIS;
89   LiveStacks &LSS;
90   AliasAnalysis *AA;
91   MachineDominatorTree &MDT;
92   MachineLoopInfo &Loops;
93   VirtRegMap &VRM;
94   MachineRegisterInfo &MRI;
95   const TargetInstrInfo &TII;
96   const TargetRegisterInfo &TRI;
97   const MachineBlockFrequencyInfo &MBFI;
98 
99   InsertPointAnalysis IPA;
100 
101   // Map from StackSlot to the LiveInterval of the original register.
102   // Note the LiveInterval of the original register may have been deleted
103   // after it is spilled. We keep a copy here to track the range where
104   // spills can be moved.
105   DenseMap<int, std::unique_ptr<LiveInterval>> StackSlotToOrigLI;
106 
107   // Map from pair of (StackSlot and Original VNI) to a set of spills which
108   // have the same stackslot and have equal values defined by Original VNI.
109   // These spills are mergeable and are hoist candiates.
110   using MergeableSpillsMap =
111       MapVector<std::pair<int, VNInfo *>, SmallPtrSet<MachineInstr *, 16>>;
112   MergeableSpillsMap MergeableSpills;
113 
114   /// This is the map from original register to a set containing all its
115   /// siblings. To hoist a spill to another BB, we need to find out a live
116   /// sibling there and use it as the source of the new spill.
117   DenseMap<unsigned, SmallSetVector<unsigned, 16>> Virt2SiblingsMap;
118 
119   bool isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI,
120                      MachineBasicBlock &BB, unsigned &LiveReg);
121 
122   void rmRedundantSpills(
123       SmallPtrSet<MachineInstr *, 16> &Spills,
124       SmallVectorImpl<MachineInstr *> &SpillsToRm,
125       DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill);
126 
127   void getVisitOrders(
128       MachineBasicBlock *Root, SmallPtrSet<MachineInstr *, 16> &Spills,
129       SmallVectorImpl<MachineDomTreeNode *> &Orders,
130       SmallVectorImpl<MachineInstr *> &SpillsToRm,
131       DenseMap<MachineDomTreeNode *, unsigned> &SpillsToKeep,
132       DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill);
133 
134   void runHoistSpills(LiveInterval &OrigLI, VNInfo &OrigVNI,
135                       SmallPtrSet<MachineInstr *, 16> &Spills,
136                       SmallVectorImpl<MachineInstr *> &SpillsToRm,
137                       DenseMap<MachineBasicBlock *, unsigned> &SpillsToIns);
138 
139 public:
140   HoistSpillHelper(MachineFunctionPass &pass, MachineFunction &mf,
141                    VirtRegMap &vrm)
142       : MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
143         LSS(pass.getAnalysis<LiveStacks>()),
144         AA(&pass.getAnalysis<AAResultsWrapperPass>().getAAResults()),
145         MDT(pass.getAnalysis<MachineDominatorTree>()),
146         Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm),
147         MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()),
148         TRI(*mf.getSubtarget().getRegisterInfo()),
149         MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()),
150         IPA(LIS, mf.getNumBlockIDs()) {}
151 
152   void addToMergeableSpills(MachineInstr &Spill, int StackSlot,
153                             unsigned Original);
154   bool rmFromMergeableSpills(MachineInstr &Spill, int StackSlot);
155   void hoistAllSpills();
156   void LRE_DidCloneVirtReg(unsigned, unsigned) override;
157 };
158 
159 class InlineSpiller : public Spiller {
160   MachineFunction &MF;
161   LiveIntervals &LIS;
162   LiveStacks &LSS;
163   AliasAnalysis *AA;
164   MachineDominatorTree &MDT;
165   MachineLoopInfo &Loops;
166   VirtRegMap &VRM;
167   MachineRegisterInfo &MRI;
168   const TargetInstrInfo &TII;
169   const TargetRegisterInfo &TRI;
170   const MachineBlockFrequencyInfo &MBFI;
171 
172   // Variables that are valid during spill(), but used by multiple methods.
173   LiveRangeEdit *Edit;
174   LiveInterval *StackInt;
175   int StackSlot;
176   unsigned Original;
177 
178   // All registers to spill to StackSlot, including the main register.
179   SmallVector<unsigned, 8> RegsToSpill;
180 
181   // All COPY instructions to/from snippets.
182   // They are ignored since both operands refer to the same stack slot.
183   SmallPtrSet<MachineInstr*, 8> SnippetCopies;
184 
185   // Values that failed to remat at some point.
186   SmallPtrSet<VNInfo*, 8> UsedValues;
187 
188   // Dead defs generated during spilling.
189   SmallVector<MachineInstr*, 8> DeadDefs;
190 
191   // Object records spills information and does the hoisting.
192   HoistSpillHelper HSpiller;
193 
194   ~InlineSpiller() override = default;
195 
196 public:
197   InlineSpiller(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
198       : MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
199         LSS(pass.getAnalysis<LiveStacks>()),
200         AA(&pass.getAnalysis<AAResultsWrapperPass>().getAAResults()),
201         MDT(pass.getAnalysis<MachineDominatorTree>()),
202         Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm),
203         MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()),
204         TRI(*mf.getSubtarget().getRegisterInfo()),
205         MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()),
206         HSpiller(pass, mf, vrm) {}
207 
208   void spill(LiveRangeEdit &) override;
209   void postOptimization() override;
210 
211 private:
212   bool isSnippet(const LiveInterval &SnipLI);
213   void collectRegsToSpill();
214 
215   bool isRegToSpill(unsigned Reg) { return is_contained(RegsToSpill, Reg); }
216 
217   bool isSibling(unsigned Reg);
218   bool hoistSpillInsideBB(LiveInterval &SpillLI, MachineInstr &CopyMI);
219   void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
220 
221   void markValueUsed(LiveInterval*, VNInfo*);
222   bool canGuaranteeAssignmentAfterRemat(unsigned VReg, MachineInstr &MI);
223   bool reMaterializeFor(LiveInterval &, MachineInstr &MI);
224   void reMaterializeAll();
225 
226   bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
227   bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>>,
228                          MachineInstr *LoadMI = nullptr);
229   void insertReload(unsigned VReg, SlotIndex, MachineBasicBlock::iterator MI);
230   void insertSpill(unsigned VReg, bool isKill, MachineBasicBlock::iterator MI);
231 
232   void spillAroundUses(unsigned Reg);
233   void spillAll();
234 };
235 
236 } // end anonymous namespace
237 
238 Spiller::~Spiller() = default;
239 
240 void Spiller::anchor() {}
241 
242 Spiller *llvm::createInlineSpiller(MachineFunctionPass &pass,
243                                    MachineFunction &mf,
244                                    VirtRegMap &vrm) {
245   return new InlineSpiller(pass, mf, vrm);
246 }
247 
248 //===----------------------------------------------------------------------===//
249 //                                Snippets
250 //===----------------------------------------------------------------------===//
251 
252 // When spilling a virtual register, we also spill any snippets it is connected
253 // to. The snippets are small live ranges that only have a single real use,
254 // leftovers from live range splitting. Spilling them enables memory operand
255 // folding or tightens the live range around the single use.
256 //
257 // This minimizes register pressure and maximizes the store-to-load distance for
258 // spill slots which can be important in tight loops.
259 
260 /// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
261 /// otherwise return 0.
262 static unsigned isFullCopyOf(const MachineInstr &MI, unsigned Reg) {
263   if (!MI.isFullCopy())
264     return 0;
265   if (MI.getOperand(0).getReg() == Reg)
266     return MI.getOperand(1).getReg();
267   if (MI.getOperand(1).getReg() == Reg)
268     return MI.getOperand(0).getReg();
269   return 0;
270 }
271 
272 /// isSnippet - Identify if a live interval is a snippet that should be spilled.
273 /// It is assumed that SnipLI is a virtual register with the same original as
274 /// Edit->getReg().
275 bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
276   unsigned Reg = Edit->getReg();
277 
278   // A snippet is a tiny live range with only a single instruction using it
279   // besides copies to/from Reg or spills/fills. We accept:
280   //
281   //   %snip = COPY %Reg / FILL fi#
282   //   %snip = USE %snip
283   //   %Reg = COPY %snip / SPILL %snip, fi#
284   //
285   if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
286     return false;
287 
288   MachineInstr *UseMI = nullptr;
289 
290   // Check that all uses satisfy our criteria.
291   for (MachineRegisterInfo::reg_instr_nodbg_iterator
292        RI = MRI.reg_instr_nodbg_begin(SnipLI.reg),
293        E = MRI.reg_instr_nodbg_end(); RI != E; ) {
294     MachineInstr &MI = *RI++;
295 
296     // Allow copies to/from Reg.
297     if (isFullCopyOf(MI, Reg))
298       continue;
299 
300     // Allow stack slot loads.
301     int FI;
302     if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
303       continue;
304 
305     // Allow stack slot stores.
306     if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
307       continue;
308 
309     // Allow a single additional instruction.
310     if (UseMI && &MI != UseMI)
311       return false;
312     UseMI = &MI;
313   }
314   return true;
315 }
316 
317 /// collectRegsToSpill - Collect live range snippets that only have a single
318 /// real use.
319 void InlineSpiller::collectRegsToSpill() {
320   unsigned Reg = Edit->getReg();
321 
322   // Main register always spills.
323   RegsToSpill.assign(1, Reg);
324   SnippetCopies.clear();
325 
326   // Snippets all have the same original, so there can't be any for an original
327   // register.
328   if (Original == Reg)
329     return;
330 
331   for (MachineRegisterInfo::reg_instr_iterator
332        RI = MRI.reg_instr_begin(Reg), E = MRI.reg_instr_end(); RI != E; ) {
333     MachineInstr &MI = *RI++;
334     unsigned SnipReg = isFullCopyOf(MI, Reg);
335     if (!isSibling(SnipReg))
336       continue;
337     LiveInterval &SnipLI = LIS.getInterval(SnipReg);
338     if (!isSnippet(SnipLI))
339       continue;
340     SnippetCopies.insert(&MI);
341     if (isRegToSpill(SnipReg))
342       continue;
343     RegsToSpill.push_back(SnipReg);
344     LLVM_DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
345     ++NumSnippets;
346   }
347 }
348 
349 bool InlineSpiller::isSibling(unsigned Reg) {
350   return Register::isVirtualRegister(Reg) && VRM.getOriginal(Reg) == Original;
351 }
352 
353 /// It is beneficial to spill to earlier place in the same BB in case
354 /// as follows:
355 /// There is an alternative def earlier in the same MBB.
356 /// Hoist the spill as far as possible in SpillMBB. This can ease
357 /// register pressure:
358 ///
359 ///   x = def
360 ///   y = use x
361 ///   s = copy x
362 ///
363 /// Hoisting the spill of s to immediately after the def removes the
364 /// interference between x and y:
365 ///
366 ///   x = def
367 ///   spill x
368 ///   y = use killed x
369 ///
370 /// This hoist only helps when the copy kills its source.
371 ///
372 bool InlineSpiller::hoistSpillInsideBB(LiveInterval &SpillLI,
373                                        MachineInstr &CopyMI) {
374   SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
375 #ifndef NDEBUG
376   VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot());
377   assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy");
378 #endif
379 
380   Register SrcReg = CopyMI.getOperand(1).getReg();
381   LiveInterval &SrcLI = LIS.getInterval(SrcReg);
382   VNInfo *SrcVNI = SrcLI.getVNInfoAt(Idx);
383   LiveQueryResult SrcQ = SrcLI.Query(Idx);
384   MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(SrcVNI->def);
385   if (DefMBB != CopyMI.getParent() || !SrcQ.isKill())
386     return false;
387 
388   // Conservatively extend the stack slot range to the range of the original
389   // value. We may be able to do better with stack slot coloring by being more
390   // careful here.
391   assert(StackInt && "No stack slot assigned yet.");
392   LiveInterval &OrigLI = LIS.getInterval(Original);
393   VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
394   StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
395   LLVM_DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
396                     << *StackInt << '\n');
397 
398   // We are going to spill SrcVNI immediately after its def, so clear out
399   // any later spills of the same value.
400   eliminateRedundantSpills(SrcLI, SrcVNI);
401 
402   MachineBasicBlock *MBB = LIS.getMBBFromIndex(SrcVNI->def);
403   MachineBasicBlock::iterator MII;
404   if (SrcVNI->isPHIDef())
405     MII = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
406   else {
407     MachineInstr *DefMI = LIS.getInstructionFromIndex(SrcVNI->def);
408     assert(DefMI && "Defining instruction disappeared");
409     MII = DefMI;
410     ++MII;
411   }
412   // Insert spill without kill flag immediately after def.
413   TII.storeRegToStackSlot(*MBB, MII, SrcReg, false, StackSlot,
414                           MRI.getRegClass(SrcReg), &TRI);
415   --MII; // Point to store instruction.
416   LIS.InsertMachineInstrInMaps(*MII);
417   LLVM_DEBUG(dbgs() << "\thoisted: " << SrcVNI->def << '\t' << *MII);
418 
419   HSpiller.addToMergeableSpills(*MII, StackSlot, Original);
420   ++NumSpills;
421   return true;
422 }
423 
424 /// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
425 /// redundant spills of this value in SLI.reg and sibling copies.
426 void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
427   assert(VNI && "Missing value");
428   SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
429   WorkList.push_back(std::make_pair(&SLI, VNI));
430   assert(StackInt && "No stack slot assigned yet.");
431 
432   do {
433     LiveInterval *LI;
434     std::tie(LI, VNI) = WorkList.pop_back_val();
435     unsigned Reg = LI->reg;
436     LLVM_DEBUG(dbgs() << "Checking redundant spills for " << VNI->id << '@'
437                       << VNI->def << " in " << *LI << '\n');
438 
439     // Regs to spill are taken care of.
440     if (isRegToSpill(Reg))
441       continue;
442 
443     // Add all of VNI's live range to StackInt.
444     StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
445     LLVM_DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
446 
447     // Find all spills and copies of VNI.
448     for (MachineRegisterInfo::use_instr_nodbg_iterator
449          UI = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end();
450          UI != E; ) {
451       MachineInstr &MI = *UI++;
452       if (!MI.isCopy() && !MI.mayStore())
453         continue;
454       SlotIndex Idx = LIS.getInstructionIndex(MI);
455       if (LI->getVNInfoAt(Idx) != VNI)
456         continue;
457 
458       // Follow sibling copies down the dominator tree.
459       if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
460         if (isSibling(DstReg)) {
461            LiveInterval &DstLI = LIS.getInterval(DstReg);
462            VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
463            assert(DstVNI && "Missing defined value");
464            assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
465            WorkList.push_back(std::make_pair(&DstLI, DstVNI));
466         }
467         continue;
468       }
469 
470       // Erase spills.
471       int FI;
472       if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
473         LLVM_DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << MI);
474         // eliminateDeadDefs won't normally remove stores, so switch opcode.
475         MI.setDesc(TII.get(TargetOpcode::KILL));
476         DeadDefs.push_back(&MI);
477         ++NumSpillsRemoved;
478         if (HSpiller.rmFromMergeableSpills(MI, StackSlot))
479           --NumSpills;
480       }
481     }
482   } while (!WorkList.empty());
483 }
484 
485 //===----------------------------------------------------------------------===//
486 //                            Rematerialization
487 //===----------------------------------------------------------------------===//
488 
489 /// markValueUsed - Remember that VNI failed to rematerialize, so its defining
490 /// instruction cannot be eliminated. See through snippet copies
491 void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
492   SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
493   WorkList.push_back(std::make_pair(LI, VNI));
494   do {
495     std::tie(LI, VNI) = WorkList.pop_back_val();
496     if (!UsedValues.insert(VNI).second)
497       continue;
498 
499     if (VNI->isPHIDef()) {
500       MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
501       for (MachineBasicBlock *P : MBB->predecessors()) {
502         VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(P));
503         if (PVNI)
504           WorkList.push_back(std::make_pair(LI, PVNI));
505       }
506       continue;
507     }
508 
509     // Follow snippet copies.
510     MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
511     if (!SnippetCopies.count(MI))
512       continue;
513     LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
514     assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
515     VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
516     assert(SnipVNI && "Snippet undefined before copy");
517     WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
518   } while (!WorkList.empty());
519 }
520 
521 bool InlineSpiller::canGuaranteeAssignmentAfterRemat(unsigned VReg,
522                                                      MachineInstr &MI) {
523   if (!RestrictStatepointRemat)
524     return true;
525   // Here's a quick explanation of the problem we're trying to handle here:
526   // * There are some pseudo instructions with more vreg uses than there are
527   //   physical registers on the machine.
528   // * This is normally handled by spilling the vreg, and folding the reload
529   //   into the user instruction.  (Thus decreasing the number of used vregs
530   //   until the remainder can be assigned to physregs.)
531   // * However, since we may try to spill vregs in any order, we can end up
532   //   trying to spill each operand to the instruction, and then rematting it
533   //   instead.  When that happens, the new live intervals (for the remats) are
534   //   expected to be trivially assignable (i.e. RS_Done).  However, since we
535   //   may have more remats than physregs, we're guaranteed to fail to assign
536   //   one.
537   // At the moment, we only handle this for STATEPOINTs since they're the only
538   // pseudo op where we've seen this.  If we start seeing other instructions
539   // with the same problem, we need to revisit this.
540   if (MI.getOpcode() != TargetOpcode::STATEPOINT)
541     return true;
542   // For STATEPOINTs we allow re-materialization for fixed arguments only hoping
543   // that number of physical registers is enough to cover all fixed arguments.
544   // If it is not true we need to revisit it.
545   for (unsigned Idx = StatepointOpers(&MI).getVarIdx(),
546                 EndIdx = MI.getNumOperands();
547        Idx < EndIdx; ++Idx) {
548     MachineOperand &MO = MI.getOperand(Idx);
549     if (MO.isReg() && MO.getReg() == VReg)
550       return false;
551   }
552   return true;
553 }
554 
555 /// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
556 bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg, MachineInstr &MI) {
557   // Analyze instruction
558   SmallVector<std::pair<MachineInstr *, unsigned>, 8> Ops;
559   VirtRegInfo RI = AnalyzeVirtRegInBundle(MI, VirtReg.reg, &Ops);
560 
561   if (!RI.Reads)
562     return false;
563 
564   SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
565   VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
566 
567   if (!ParentVNI) {
568     LLVM_DEBUG(dbgs() << "\tadding <undef> flags: ");
569     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
570       MachineOperand &MO = MI.getOperand(i);
571       if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
572         MO.setIsUndef();
573     }
574     LLVM_DEBUG(dbgs() << UseIdx << '\t' << MI);
575     return true;
576   }
577 
578   if (SnippetCopies.count(&MI))
579     return false;
580 
581   LiveInterval &OrigLI = LIS.getInterval(Original);
582   VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
583   LiveRangeEdit::Remat RM(ParentVNI);
584   RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
585 
586   if (!Edit->canRematerializeAt(RM, OrigVNI, UseIdx, false)) {
587     markValueUsed(&VirtReg, ParentVNI);
588     LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
589     return false;
590   }
591 
592   // If the instruction also writes VirtReg.reg, it had better not require the
593   // same register for uses and defs.
594   if (RI.Tied) {
595     markValueUsed(&VirtReg, ParentVNI);
596     LLVM_DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << MI);
597     return false;
598   }
599 
600   // Before rematerializing into a register for a single instruction, try to
601   // fold a load into the instruction. That avoids allocating a new register.
602   if (RM.OrigMI->canFoldAsLoad() &&
603       foldMemoryOperand(Ops, RM.OrigMI)) {
604     Edit->markRematerialized(RM.ParentVNI);
605     ++NumFoldedLoads;
606     return true;
607   }
608 
609   // If we can't guarantee that we'll be able to actually assign the new vreg,
610   // we can't remat.
611   if (!canGuaranteeAssignmentAfterRemat(VirtReg.reg, MI)) {
612     markValueUsed(&VirtReg, ParentVNI);
613     LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
614     return false;
615   }
616 
617   // Allocate a new register for the remat.
618   unsigned NewVReg = Edit->createFrom(Original);
619 
620   // Finally we can rematerialize OrigMI before MI.
621   SlotIndex DefIdx =
622       Edit->rematerializeAt(*MI.getParent(), MI, NewVReg, RM, TRI);
623 
624   // We take the DebugLoc from MI, since OrigMI may be attributed to a
625   // different source location.
626   auto *NewMI = LIS.getInstructionFromIndex(DefIdx);
627   NewMI->setDebugLoc(MI.getDebugLoc());
628 
629   (void)DefIdx;
630   LLVM_DEBUG(dbgs() << "\tremat:  " << DefIdx << '\t'
631                     << *LIS.getInstructionFromIndex(DefIdx));
632 
633   // Replace operands
634   for (const auto &OpPair : Ops) {
635     MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
636     if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
637       MO.setReg(NewVReg);
638       MO.setIsKill();
639     }
640   }
641   LLVM_DEBUG(dbgs() << "\t        " << UseIdx << '\t' << MI << '\n');
642 
643   ++NumRemats;
644   return true;
645 }
646 
647 /// reMaterializeAll - Try to rematerialize as many uses as possible,
648 /// and trim the live ranges after.
649 void InlineSpiller::reMaterializeAll() {
650   if (!Edit->anyRematerializable(AA))
651     return;
652 
653   UsedValues.clear();
654 
655   // Try to remat before all uses of snippets.
656   bool anyRemat = false;
657   for (unsigned Reg : RegsToSpill) {
658     LiveInterval &LI = LIS.getInterval(Reg);
659     for (MachineRegisterInfo::reg_bundle_iterator
660            RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
661          RegI != E; ) {
662       MachineInstr &MI = *RegI++;
663 
664       // Debug values are not allowed to affect codegen.
665       if (MI.isDebugValue())
666         continue;
667 
668       assert(!MI.isDebugInstr() && "Did not expect to find a use in debug "
669              "instruction that isn't a DBG_VALUE");
670 
671       anyRemat |= reMaterializeFor(LI, MI);
672     }
673   }
674   if (!anyRemat)
675     return;
676 
677   // Remove any values that were completely rematted.
678   for (unsigned Reg : RegsToSpill) {
679     LiveInterval &LI = LIS.getInterval(Reg);
680     for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
681          I != E; ++I) {
682       VNInfo *VNI = *I;
683       if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
684         continue;
685       MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
686       MI->addRegisterDead(Reg, &TRI);
687       if (!MI->allDefsAreDead())
688         continue;
689       LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
690       DeadDefs.push_back(MI);
691     }
692   }
693 
694   // Eliminate dead code after remat. Note that some snippet copies may be
695   // deleted here.
696   if (DeadDefs.empty())
697     return;
698   LLVM_DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
699   Edit->eliminateDeadDefs(DeadDefs, RegsToSpill, AA);
700 
701   // LiveRangeEdit::eliminateDeadDef is used to remove dead define instructions
702   // after rematerialization.  To remove a VNI for a vreg from its LiveInterval,
703   // LiveIntervals::removeVRegDefAt is used. However, after non-PHI VNIs are all
704   // removed, PHI VNI are still left in the LiveInterval.
705   // So to get rid of unused reg, we need to check whether it has non-dbg
706   // reference instead of whether it has non-empty interval.
707   unsigned ResultPos = 0;
708   for (unsigned Reg : RegsToSpill) {
709     if (MRI.reg_nodbg_empty(Reg)) {
710       Edit->eraseVirtReg(Reg);
711       continue;
712     }
713 
714     assert(LIS.hasInterval(Reg) &&
715            (!LIS.getInterval(Reg).empty() || !MRI.reg_nodbg_empty(Reg)) &&
716            "Empty and not used live-range?!");
717 
718     RegsToSpill[ResultPos++] = Reg;
719   }
720   RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
721   LLVM_DEBUG(dbgs() << RegsToSpill.size()
722                     << " registers to spill after remat.\n");
723 }
724 
725 //===----------------------------------------------------------------------===//
726 //                                 Spilling
727 //===----------------------------------------------------------------------===//
728 
729 /// If MI is a load or store of StackSlot, it can be removed.
730 bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
731   int FI = 0;
732   unsigned InstrReg = TII.isLoadFromStackSlot(*MI, FI);
733   bool IsLoad = InstrReg;
734   if (!IsLoad)
735     InstrReg = TII.isStoreToStackSlot(*MI, FI);
736 
737   // We have a stack access. Is it the right register and slot?
738   if (InstrReg != Reg || FI != StackSlot)
739     return false;
740 
741   if (!IsLoad)
742     HSpiller.rmFromMergeableSpills(*MI, StackSlot);
743 
744   LLVM_DEBUG(dbgs() << "Coalescing stack access: " << *MI);
745   LIS.RemoveMachineInstrFromMaps(*MI);
746   MI->eraseFromParent();
747 
748   if (IsLoad) {
749     ++NumReloadsRemoved;
750     --NumReloads;
751   } else {
752     ++NumSpillsRemoved;
753     --NumSpills;
754   }
755 
756   return true;
757 }
758 
759 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
760 LLVM_DUMP_METHOD
761 // Dump the range of instructions from B to E with their slot indexes.
762 static void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B,
763                                                MachineBasicBlock::iterator E,
764                                                LiveIntervals const &LIS,
765                                                const char *const header,
766                                                unsigned VReg =0) {
767   char NextLine = '\n';
768   char SlotIndent = '\t';
769 
770   if (std::next(B) == E) {
771     NextLine = ' ';
772     SlotIndent = ' ';
773   }
774 
775   dbgs() << '\t' << header << ": " << NextLine;
776 
777   for (MachineBasicBlock::iterator I = B; I != E; ++I) {
778     SlotIndex Idx = LIS.getInstructionIndex(*I).getRegSlot();
779 
780     // If a register was passed in and this instruction has it as a
781     // destination that is marked as an early clobber, print the
782     // early-clobber slot index.
783     if (VReg) {
784       MachineOperand *MO = I->findRegisterDefOperand(VReg);
785       if (MO && MO->isEarlyClobber())
786         Idx = Idx.getRegSlot(true);
787     }
788 
789     dbgs() << SlotIndent << Idx << '\t' << *I;
790   }
791 }
792 #endif
793 
794 /// foldMemoryOperand - Try folding stack slot references in Ops into their
795 /// instructions.
796 ///
797 /// @param Ops    Operand indices from AnalyzeVirtRegInBundle().
798 /// @param LoadMI Load instruction to use instead of stack slot when non-null.
799 /// @return       True on success.
800 bool InlineSpiller::
801 foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
802                   MachineInstr *LoadMI) {
803   if (Ops.empty())
804     return false;
805   // Don't attempt folding in bundles.
806   MachineInstr *MI = Ops.front().first;
807   if (Ops.back().first != MI || MI->isBundled())
808     return false;
809 
810   bool WasCopy = MI->isCopy();
811   unsigned ImpReg = 0;
812 
813   // Spill subregs if the target allows it.
814   // We always want to spill subregs for stackmap/patchpoint pseudos.
815   bool SpillSubRegs = TII.isSubregFoldable() ||
816                       MI->getOpcode() == TargetOpcode::STATEPOINT ||
817                       MI->getOpcode() == TargetOpcode::PATCHPOINT ||
818                       MI->getOpcode() == TargetOpcode::STACKMAP;
819 
820   // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
821   // operands.
822   SmallVector<unsigned, 8> FoldOps;
823   for (const auto &OpPair : Ops) {
824     unsigned Idx = OpPair.second;
825     assert(MI == OpPair.first && "Instruction conflict during operand folding");
826     MachineOperand &MO = MI->getOperand(Idx);
827     if (MO.isImplicit()) {
828       ImpReg = MO.getReg();
829       continue;
830     }
831 
832     if (!SpillSubRegs && MO.getSubReg())
833       return false;
834     // We cannot fold a load instruction into a def.
835     if (LoadMI && MO.isDef())
836       return false;
837     // Tied use operands should not be passed to foldMemoryOperand.
838     if (!MI->isRegTiedToDefOperand(Idx))
839       FoldOps.push_back(Idx);
840   }
841 
842   // If we only have implicit uses, we won't be able to fold that.
843   // Moreover, TargetInstrInfo::foldMemoryOperand will assert if we try!
844   if (FoldOps.empty())
845     return false;
846 
847   MachineInstrSpan MIS(MI, MI->getParent());
848 
849   MachineInstr *FoldMI =
850       LoadMI ? TII.foldMemoryOperand(*MI, FoldOps, *LoadMI, &LIS)
851              : TII.foldMemoryOperand(*MI, FoldOps, StackSlot, &LIS, &VRM);
852   if (!FoldMI)
853     return false;
854 
855   // Remove LIS for any dead defs in the original MI not in FoldMI.
856   for (MIBundleOperands MO(*MI); MO.isValid(); ++MO) {
857     if (!MO->isReg())
858       continue;
859     Register Reg = MO->getReg();
860     if (!Reg || Register::isVirtualRegister(Reg) || MRI.isReserved(Reg)) {
861       continue;
862     }
863     // Skip non-Defs, including undef uses and internal reads.
864     if (MO->isUse())
865       continue;
866     PhysRegInfo RI = AnalyzePhysRegInBundle(*FoldMI, Reg, &TRI);
867     if (RI.FullyDefined)
868       continue;
869     // FoldMI does not define this physreg. Remove the LI segment.
870     assert(MO->isDead() && "Cannot fold physreg def");
871     SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
872     LIS.removePhysRegDefAt(Reg, Idx);
873   }
874 
875   int FI;
876   if (TII.isStoreToStackSlot(*MI, FI) &&
877       HSpiller.rmFromMergeableSpills(*MI, FI))
878     --NumSpills;
879   LIS.ReplaceMachineInstrInMaps(*MI, *FoldMI);
880   // Update the call site info.
881   if (MI->isCandidateForCallSiteEntry())
882     MI->getMF()->moveCallSiteInfo(MI, FoldMI);
883   MI->eraseFromParent();
884 
885   // Insert any new instructions other than FoldMI into the LIS maps.
886   assert(!MIS.empty() && "Unexpected empty span of instructions!");
887   for (MachineInstr &MI : MIS)
888     if (&MI != FoldMI)
889       LIS.InsertMachineInstrInMaps(MI);
890 
891   // TII.foldMemoryOperand may have left some implicit operands on the
892   // instruction.  Strip them.
893   if (ImpReg)
894     for (unsigned i = FoldMI->getNumOperands(); i; --i) {
895       MachineOperand &MO = FoldMI->getOperand(i - 1);
896       if (!MO.isReg() || !MO.isImplicit())
897         break;
898       if (MO.getReg() == ImpReg)
899         FoldMI->RemoveOperand(i - 1);
900     }
901 
902   LLVM_DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS,
903                                                 "folded"));
904 
905   if (!WasCopy)
906     ++NumFolded;
907   else if (Ops.front().second == 0) {
908     ++NumSpills;
909     HSpiller.addToMergeableSpills(*FoldMI, StackSlot, Original);
910   } else
911     ++NumReloads;
912   return true;
913 }
914 
915 void InlineSpiller::insertReload(unsigned NewVReg,
916                                  SlotIndex Idx,
917                                  MachineBasicBlock::iterator MI) {
918   MachineBasicBlock &MBB = *MI->getParent();
919 
920   MachineInstrSpan MIS(MI, &MBB);
921   TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot,
922                            MRI.getRegClass(NewVReg), &TRI);
923 
924   LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI);
925 
926   LLVM_DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload",
927                                                 NewVReg));
928   ++NumReloads;
929 }
930 
931 /// Check if \p Def fully defines a VReg with an undefined value.
932 /// If that's the case, that means the value of VReg is actually
933 /// not relevant.
934 static bool isRealSpill(const MachineInstr &Def) {
935   if (!Def.isImplicitDef())
936     return true;
937   assert(Def.getNumOperands() == 1 &&
938          "Implicit def with more than one definition");
939   // We can say that the VReg defined by Def is undef, only if it is
940   // fully defined by Def. Otherwise, some of the lanes may not be
941   // undef and the value of the VReg matters.
942   return Def.getOperand(0).getSubReg();
943 }
944 
945 /// insertSpill - Insert a spill of NewVReg after MI.
946 void InlineSpiller::insertSpill(unsigned NewVReg, bool isKill,
947                                  MachineBasicBlock::iterator MI) {
948   MachineBasicBlock &MBB = *MI->getParent();
949 
950   MachineInstrSpan MIS(MI, &MBB);
951   MachineBasicBlock::iterator SpillBefore = std::next(MI);
952   bool IsRealSpill = isRealSpill(*MI);
953   if (IsRealSpill)
954     TII.storeRegToStackSlot(MBB, SpillBefore, NewVReg, isKill, StackSlot,
955                             MRI.getRegClass(NewVReg), &TRI);
956   else
957     // Don't spill undef value.
958     // Anything works for undef, in particular keeping the memory
959     // uninitialized is a viable option and it saves code size and
960     // run time.
961     BuildMI(MBB, SpillBefore, MI->getDebugLoc(), TII.get(TargetOpcode::KILL))
962         .addReg(NewVReg, getKillRegState(isKill));
963 
964   MachineBasicBlock::iterator Spill = std::next(MI);
965   LIS.InsertMachineInstrRangeInMaps(Spill, MIS.end());
966 
967   LLVM_DEBUG(
968       dumpMachineInstrRangeWithSlotIndex(Spill, MIS.end(), LIS, "spill"));
969   ++NumSpills;
970   if (IsRealSpill)
971     HSpiller.addToMergeableSpills(*Spill, StackSlot, Original);
972 }
973 
974 /// spillAroundUses - insert spill code around each use of Reg.
975 void InlineSpiller::spillAroundUses(unsigned Reg) {
976   LLVM_DEBUG(dbgs() << "spillAroundUses " << printReg(Reg) << '\n');
977   LiveInterval &OldLI = LIS.getInterval(Reg);
978 
979   // Iterate over instructions using Reg.
980   for (MachineRegisterInfo::reg_bundle_iterator
981        RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
982        RegI != E; ) {
983     MachineInstr *MI = &*(RegI++);
984 
985     // Debug values are not allowed to affect codegen.
986     if (MI->isDebugValue()) {
987       // Modify DBG_VALUE now that the value is in a spill slot.
988       MachineBasicBlock *MBB = MI->getParent();
989       LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:\t" << *MI);
990       buildDbgValueForSpill(*MBB, MI, *MI, StackSlot);
991       MBB->erase(MI);
992       continue;
993     }
994 
995     assert(!MI->isDebugInstr() && "Did not expect to find a use in debug "
996            "instruction that isn't a DBG_VALUE");
997 
998     // Ignore copies to/from snippets. We'll delete them.
999     if (SnippetCopies.count(MI))
1000       continue;
1001 
1002     // Stack slot accesses may coalesce away.
1003     if (coalesceStackAccess(MI, Reg))
1004       continue;
1005 
1006     // Analyze instruction.
1007     SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
1008     VirtRegInfo RI = AnalyzeVirtRegInBundle(*MI, Reg, &Ops);
1009 
1010     // Find the slot index where this instruction reads and writes OldLI.
1011     // This is usually the def slot, except for tied early clobbers.
1012     SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
1013     if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
1014       if (SlotIndex::isSameInstr(Idx, VNI->def))
1015         Idx = VNI->def;
1016 
1017     // Check for a sibling copy.
1018     unsigned SibReg = isFullCopyOf(*MI, Reg);
1019     if (SibReg && isSibling(SibReg)) {
1020       // This may actually be a copy between snippets.
1021       if (isRegToSpill(SibReg)) {
1022         LLVM_DEBUG(dbgs() << "Found new snippet copy: " << *MI);
1023         SnippetCopies.insert(MI);
1024         continue;
1025       }
1026       if (RI.Writes) {
1027         if (hoistSpillInsideBB(OldLI, *MI)) {
1028           // This COPY is now dead, the value is already in the stack slot.
1029           MI->getOperand(0).setIsDead();
1030           DeadDefs.push_back(MI);
1031           continue;
1032         }
1033       } else {
1034         // This is a reload for a sib-reg copy. Drop spills downstream.
1035         LiveInterval &SibLI = LIS.getInterval(SibReg);
1036         eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
1037         // The COPY will fold to a reload below.
1038       }
1039     }
1040 
1041     // Attempt to fold memory ops.
1042     if (foldMemoryOperand(Ops))
1043       continue;
1044 
1045     // Create a new virtual register for spill/fill.
1046     // FIXME: Infer regclass from instruction alone.
1047     unsigned NewVReg = Edit->createFrom(Reg);
1048 
1049     if (RI.Reads)
1050       insertReload(NewVReg, Idx, MI);
1051 
1052     // Rewrite instruction operands.
1053     bool hasLiveDef = false;
1054     for (const auto &OpPair : Ops) {
1055       MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
1056       MO.setReg(NewVReg);
1057       if (MO.isUse()) {
1058         if (!OpPair.first->isRegTiedToDefOperand(OpPair.second))
1059           MO.setIsKill();
1060       } else {
1061         if (!MO.isDead())
1062           hasLiveDef = true;
1063       }
1064     }
1065     LLVM_DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI << '\n');
1066 
1067     // FIXME: Use a second vreg if instruction has no tied ops.
1068     if (RI.Writes)
1069       if (hasLiveDef)
1070         insertSpill(NewVReg, true, MI);
1071   }
1072 }
1073 
1074 /// spillAll - Spill all registers remaining after rematerialization.
1075 void InlineSpiller::spillAll() {
1076   // Update LiveStacks now that we are committed to spilling.
1077   if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
1078     StackSlot = VRM.assignVirt2StackSlot(Original);
1079     StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
1080     StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
1081   } else
1082     StackInt = &LSS.getInterval(StackSlot);
1083 
1084   if (Original != Edit->getReg())
1085     VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
1086 
1087   assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
1088   for (unsigned Reg : RegsToSpill)
1089     StackInt->MergeSegmentsInAsValue(LIS.getInterval(Reg),
1090                                      StackInt->getValNumInfo(0));
1091   LLVM_DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
1092 
1093   // Spill around uses of all RegsToSpill.
1094   for (unsigned Reg : RegsToSpill)
1095     spillAroundUses(Reg);
1096 
1097   // Hoisted spills may cause dead code.
1098   if (!DeadDefs.empty()) {
1099     LLVM_DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
1100     Edit->eliminateDeadDefs(DeadDefs, RegsToSpill, AA);
1101   }
1102 
1103   // Finally delete the SnippetCopies.
1104   for (unsigned Reg : RegsToSpill) {
1105     for (MachineRegisterInfo::reg_instr_iterator
1106          RI = MRI.reg_instr_begin(Reg), E = MRI.reg_instr_end();
1107          RI != E; ) {
1108       MachineInstr &MI = *(RI++);
1109       assert(SnippetCopies.count(&MI) && "Remaining use wasn't a snippet copy");
1110       // FIXME: Do this with a LiveRangeEdit callback.
1111       LIS.RemoveMachineInstrFromMaps(MI);
1112       MI.eraseFromParent();
1113     }
1114   }
1115 
1116   // Delete all spilled registers.
1117   for (unsigned Reg : RegsToSpill)
1118     Edit->eraseVirtReg(Reg);
1119 }
1120 
1121 void InlineSpiller::spill(LiveRangeEdit &edit) {
1122   ++NumSpilledRanges;
1123   Edit = &edit;
1124   assert(!Register::isStackSlot(edit.getReg()) &&
1125          "Trying to spill a stack slot.");
1126   // Share a stack slot among all descendants of Original.
1127   Original = VRM.getOriginal(edit.getReg());
1128   StackSlot = VRM.getStackSlot(Original);
1129   StackInt = nullptr;
1130 
1131   LLVM_DEBUG(dbgs() << "Inline spilling "
1132                     << TRI.getRegClassName(MRI.getRegClass(edit.getReg()))
1133                     << ':' << edit.getParent() << "\nFrom original "
1134                     << printReg(Original) << '\n');
1135   assert(edit.getParent().isSpillable() &&
1136          "Attempting to spill already spilled value.");
1137   assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
1138 
1139   collectRegsToSpill();
1140   reMaterializeAll();
1141 
1142   // Remat may handle everything.
1143   if (!RegsToSpill.empty())
1144     spillAll();
1145 
1146   Edit->calculateRegClassAndHint(MF, Loops, MBFI);
1147 }
1148 
1149 /// Optimizations after all the reg selections and spills are done.
1150 void InlineSpiller::postOptimization() { HSpiller.hoistAllSpills(); }
1151 
1152 /// When a spill is inserted, add the spill to MergeableSpills map.
1153 void HoistSpillHelper::addToMergeableSpills(MachineInstr &Spill, int StackSlot,
1154                                             unsigned Original) {
1155   BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
1156   LiveInterval &OrigLI = LIS.getInterval(Original);
1157   // save a copy of LiveInterval in StackSlotToOrigLI because the original
1158   // LiveInterval may be cleared after all its references are spilled.
1159   if (StackSlotToOrigLI.find(StackSlot) == StackSlotToOrigLI.end()) {
1160     auto LI = std::make_unique<LiveInterval>(OrigLI.reg, OrigLI.weight);
1161     LI->assign(OrigLI, Allocator);
1162     StackSlotToOrigLI[StackSlot] = std::move(LI);
1163   }
1164   SlotIndex Idx = LIS.getInstructionIndex(Spill);
1165   VNInfo *OrigVNI = StackSlotToOrigLI[StackSlot]->getVNInfoAt(Idx.getRegSlot());
1166   std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1167   MergeableSpills[MIdx].insert(&Spill);
1168 }
1169 
1170 /// When a spill is removed, remove the spill from MergeableSpills map.
1171 /// Return true if the spill is removed successfully.
1172 bool HoistSpillHelper::rmFromMergeableSpills(MachineInstr &Spill,
1173                                              int StackSlot) {
1174   auto It = StackSlotToOrigLI.find(StackSlot);
1175   if (It == StackSlotToOrigLI.end())
1176     return false;
1177   SlotIndex Idx = LIS.getInstructionIndex(Spill);
1178   VNInfo *OrigVNI = It->second->getVNInfoAt(Idx.getRegSlot());
1179   std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1180   return MergeableSpills[MIdx].erase(&Spill);
1181 }
1182 
1183 /// Check BB to see if it is a possible target BB to place a hoisted spill,
1184 /// i.e., there should be a living sibling of OrigReg at the insert point.
1185 bool HoistSpillHelper::isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI,
1186                                      MachineBasicBlock &BB, unsigned &LiveReg) {
1187   SlotIndex Idx;
1188   unsigned OrigReg = OrigLI.reg;
1189   MachineBasicBlock::iterator MI = IPA.getLastInsertPointIter(OrigLI, BB);
1190   if (MI != BB.end())
1191     Idx = LIS.getInstructionIndex(*MI);
1192   else
1193     Idx = LIS.getMBBEndIdx(&BB).getPrevSlot();
1194   SmallSetVector<unsigned, 16> &Siblings = Virt2SiblingsMap[OrigReg];
1195   assert(OrigLI.getVNInfoAt(Idx) == &OrigVNI && "Unexpected VNI");
1196 
1197   for (auto const SibReg : Siblings) {
1198     LiveInterval &LI = LIS.getInterval(SibReg);
1199     VNInfo *VNI = LI.getVNInfoAt(Idx);
1200     if (VNI) {
1201       LiveReg = SibReg;
1202       return true;
1203     }
1204   }
1205   return false;
1206 }
1207 
1208 /// Remove redundant spills in the same BB. Save those redundant spills in
1209 /// SpillsToRm, and save the spill to keep and its BB in SpillBBToSpill map.
1210 void HoistSpillHelper::rmRedundantSpills(
1211     SmallPtrSet<MachineInstr *, 16> &Spills,
1212     SmallVectorImpl<MachineInstr *> &SpillsToRm,
1213     DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill) {
1214   // For each spill saw, check SpillBBToSpill[] and see if its BB already has
1215   // another spill inside. If a BB contains more than one spill, only keep the
1216   // earlier spill with smaller SlotIndex.
1217   for (const auto CurrentSpill : Spills) {
1218     MachineBasicBlock *Block = CurrentSpill->getParent();
1219     MachineDomTreeNode *Node = MDT.getBase().getNode(Block);
1220     MachineInstr *PrevSpill = SpillBBToSpill[Node];
1221     if (PrevSpill) {
1222       SlotIndex PIdx = LIS.getInstructionIndex(*PrevSpill);
1223       SlotIndex CIdx = LIS.getInstructionIndex(*CurrentSpill);
1224       MachineInstr *SpillToRm = (CIdx > PIdx) ? CurrentSpill : PrevSpill;
1225       MachineInstr *SpillToKeep = (CIdx > PIdx) ? PrevSpill : CurrentSpill;
1226       SpillsToRm.push_back(SpillToRm);
1227       SpillBBToSpill[MDT.getBase().getNode(Block)] = SpillToKeep;
1228     } else {
1229       SpillBBToSpill[MDT.getBase().getNode(Block)] = CurrentSpill;
1230     }
1231   }
1232   for (const auto SpillToRm : SpillsToRm)
1233     Spills.erase(SpillToRm);
1234 }
1235 
1236 /// Starting from \p Root find a top-down traversal order of the dominator
1237 /// tree to visit all basic blocks containing the elements of \p Spills.
1238 /// Redundant spills will be found and put into \p SpillsToRm at the same
1239 /// time. \p SpillBBToSpill will be populated as part of the process and
1240 /// maps a basic block to the first store occurring in the basic block.
1241 /// \post SpillsToRm.union(Spills\@post) == Spills\@pre
1242 void HoistSpillHelper::getVisitOrders(
1243     MachineBasicBlock *Root, SmallPtrSet<MachineInstr *, 16> &Spills,
1244     SmallVectorImpl<MachineDomTreeNode *> &Orders,
1245     SmallVectorImpl<MachineInstr *> &SpillsToRm,
1246     DenseMap<MachineDomTreeNode *, unsigned> &SpillsToKeep,
1247     DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill) {
1248   // The set contains all the possible BB nodes to which we may hoist
1249   // original spills.
1250   SmallPtrSet<MachineDomTreeNode *, 8> WorkSet;
1251   // Save the BB nodes on the path from the first BB node containing
1252   // non-redundant spill to the Root node.
1253   SmallPtrSet<MachineDomTreeNode *, 8> NodesOnPath;
1254   // All the spills to be hoisted must originate from a single def instruction
1255   // to the OrigReg. It means the def instruction should dominate all the spills
1256   // to be hoisted. We choose the BB where the def instruction is located as
1257   // the Root.
1258   MachineDomTreeNode *RootIDomNode = MDT[Root]->getIDom();
1259   // For every node on the dominator tree with spill, walk up on the dominator
1260   // tree towards the Root node until it is reached. If there is other node
1261   // containing spill in the middle of the path, the previous spill saw will
1262   // be redundant and the node containing it will be removed. All the nodes on
1263   // the path starting from the first node with non-redundant spill to the Root
1264   // node will be added to the WorkSet, which will contain all the possible
1265   // locations where spills may be hoisted to after the loop below is done.
1266   for (const auto Spill : Spills) {
1267     MachineBasicBlock *Block = Spill->getParent();
1268     MachineDomTreeNode *Node = MDT[Block];
1269     MachineInstr *SpillToRm = nullptr;
1270     while (Node != RootIDomNode) {
1271       // If Node dominates Block, and it already contains a spill, the spill in
1272       // Block will be redundant.
1273       if (Node != MDT[Block] && SpillBBToSpill[Node]) {
1274         SpillToRm = SpillBBToSpill[MDT[Block]];
1275         break;
1276         /// If we see the Node already in WorkSet, the path from the Node to
1277         /// the Root node must already be traversed by another spill.
1278         /// Then no need to repeat.
1279       } else if (WorkSet.count(Node)) {
1280         break;
1281       } else {
1282         NodesOnPath.insert(Node);
1283       }
1284       Node = Node->getIDom();
1285     }
1286     if (SpillToRm) {
1287       SpillsToRm.push_back(SpillToRm);
1288     } else {
1289       // Add a BB containing the original spills to SpillsToKeep -- i.e.,
1290       // set the initial status before hoisting start. The value of BBs
1291       // containing original spills is set to 0, in order to descriminate
1292       // with BBs containing hoisted spills which will be inserted to
1293       // SpillsToKeep later during hoisting.
1294       SpillsToKeep[MDT[Block]] = 0;
1295       WorkSet.insert(NodesOnPath.begin(), NodesOnPath.end());
1296     }
1297     NodesOnPath.clear();
1298   }
1299 
1300   // Sort the nodes in WorkSet in top-down order and save the nodes
1301   // in Orders. Orders will be used for hoisting in runHoistSpills.
1302   unsigned idx = 0;
1303   Orders.push_back(MDT.getBase().getNode(Root));
1304   do {
1305     MachineDomTreeNode *Node = Orders[idx++];
1306     const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
1307     unsigned NumChildren = Children.size();
1308     for (unsigned i = 0; i != NumChildren; ++i) {
1309       MachineDomTreeNode *Child = Children[i];
1310       if (WorkSet.count(Child))
1311         Orders.push_back(Child);
1312     }
1313   } while (idx != Orders.size());
1314   assert(Orders.size() == WorkSet.size() &&
1315          "Orders have different size with WorkSet");
1316 
1317 #ifndef NDEBUG
1318   LLVM_DEBUG(dbgs() << "Orders size is " << Orders.size() << "\n");
1319   SmallVector<MachineDomTreeNode *, 32>::reverse_iterator RIt = Orders.rbegin();
1320   for (; RIt != Orders.rend(); RIt++)
1321     LLVM_DEBUG(dbgs() << "BB" << (*RIt)->getBlock()->getNumber() << ",");
1322   LLVM_DEBUG(dbgs() << "\n");
1323 #endif
1324 }
1325 
1326 /// Try to hoist spills according to BB hotness. The spills to removed will
1327 /// be saved in \p SpillsToRm. The spills to be inserted will be saved in
1328 /// \p SpillsToIns.
1329 void HoistSpillHelper::runHoistSpills(
1330     LiveInterval &OrigLI, VNInfo &OrigVNI,
1331     SmallPtrSet<MachineInstr *, 16> &Spills,
1332     SmallVectorImpl<MachineInstr *> &SpillsToRm,
1333     DenseMap<MachineBasicBlock *, unsigned> &SpillsToIns) {
1334   // Visit order of dominator tree nodes.
1335   SmallVector<MachineDomTreeNode *, 32> Orders;
1336   // SpillsToKeep contains all the nodes where spills are to be inserted
1337   // during hoisting. If the spill to be inserted is an original spill
1338   // (not a hoisted one), the value of the map entry is 0. If the spill
1339   // is a hoisted spill, the value of the map entry is the VReg to be used
1340   // as the source of the spill.
1341   DenseMap<MachineDomTreeNode *, unsigned> SpillsToKeep;
1342   // Map from BB to the first spill inside of it.
1343   DenseMap<MachineDomTreeNode *, MachineInstr *> SpillBBToSpill;
1344 
1345   rmRedundantSpills(Spills, SpillsToRm, SpillBBToSpill);
1346 
1347   MachineBasicBlock *Root = LIS.getMBBFromIndex(OrigVNI.def);
1348   getVisitOrders(Root, Spills, Orders, SpillsToRm, SpillsToKeep,
1349                  SpillBBToSpill);
1350 
1351   // SpillsInSubTreeMap keeps the map from a dom tree node to a pair of
1352   // nodes set and the cost of all the spills inside those nodes.
1353   // The nodes set are the locations where spills are to be inserted
1354   // in the subtree of current node.
1355   using NodesCostPair =
1356       std::pair<SmallPtrSet<MachineDomTreeNode *, 16>, BlockFrequency>;
1357   DenseMap<MachineDomTreeNode *, NodesCostPair> SpillsInSubTreeMap;
1358 
1359   // Iterate Orders set in reverse order, which will be a bottom-up order
1360   // in the dominator tree. Once we visit a dom tree node, we know its
1361   // children have already been visited and the spill locations in the
1362   // subtrees of all the children have been determined.
1363   SmallVector<MachineDomTreeNode *, 32>::reverse_iterator RIt = Orders.rbegin();
1364   for (; RIt != Orders.rend(); RIt++) {
1365     MachineBasicBlock *Block = (*RIt)->getBlock();
1366 
1367     // If Block contains an original spill, simply continue.
1368     if (SpillsToKeep.find(*RIt) != SpillsToKeep.end() && !SpillsToKeep[*RIt]) {
1369       SpillsInSubTreeMap[*RIt].first.insert(*RIt);
1370       // SpillsInSubTreeMap[*RIt].second contains the cost of spill.
1371       SpillsInSubTreeMap[*RIt].second = MBFI.getBlockFreq(Block);
1372       continue;
1373     }
1374 
1375     // Collect spills in subtree of current node (*RIt) to
1376     // SpillsInSubTreeMap[*RIt].first.
1377     const std::vector<MachineDomTreeNode *> &Children = (*RIt)->getChildren();
1378     unsigned NumChildren = Children.size();
1379     for (unsigned i = 0; i != NumChildren; ++i) {
1380       MachineDomTreeNode *Child = Children[i];
1381       if (SpillsInSubTreeMap.find(Child) == SpillsInSubTreeMap.end())
1382         continue;
1383       // The stmt "SpillsInSubTree = SpillsInSubTreeMap[*RIt].first" below
1384       // should be placed before getting the begin and end iterators of
1385       // SpillsInSubTreeMap[Child].first, or else the iterators may be
1386       // invalidated when SpillsInSubTreeMap[*RIt] is seen the first time
1387       // and the map grows and then the original buckets in the map are moved.
1388       SmallPtrSet<MachineDomTreeNode *, 16> &SpillsInSubTree =
1389           SpillsInSubTreeMap[*RIt].first;
1390       BlockFrequency &SubTreeCost = SpillsInSubTreeMap[*RIt].second;
1391       SubTreeCost += SpillsInSubTreeMap[Child].second;
1392       auto BI = SpillsInSubTreeMap[Child].first.begin();
1393       auto EI = SpillsInSubTreeMap[Child].first.end();
1394       SpillsInSubTree.insert(BI, EI);
1395       SpillsInSubTreeMap.erase(Child);
1396     }
1397 
1398     SmallPtrSet<MachineDomTreeNode *, 16> &SpillsInSubTree =
1399           SpillsInSubTreeMap[*RIt].first;
1400     BlockFrequency &SubTreeCost = SpillsInSubTreeMap[*RIt].second;
1401     // No spills in subtree, simply continue.
1402     if (SpillsInSubTree.empty())
1403       continue;
1404 
1405     // Check whether Block is a possible candidate to insert spill.
1406     unsigned LiveReg = 0;
1407     if (!isSpillCandBB(OrigLI, OrigVNI, *Block, LiveReg))
1408       continue;
1409 
1410     // If there are multiple spills that could be merged, bias a little
1411     // to hoist the spill.
1412     BranchProbability MarginProb = (SpillsInSubTree.size() > 1)
1413                                        ? BranchProbability(9, 10)
1414                                        : BranchProbability(1, 1);
1415     if (SubTreeCost > MBFI.getBlockFreq(Block) * MarginProb) {
1416       // Hoist: Move spills to current Block.
1417       for (const auto SpillBB : SpillsInSubTree) {
1418         // When SpillBB is a BB contains original spill, insert the spill
1419         // to SpillsToRm.
1420         if (SpillsToKeep.find(SpillBB) != SpillsToKeep.end() &&
1421             !SpillsToKeep[SpillBB]) {
1422           MachineInstr *SpillToRm = SpillBBToSpill[SpillBB];
1423           SpillsToRm.push_back(SpillToRm);
1424         }
1425         // SpillBB will not contain spill anymore, remove it from SpillsToKeep.
1426         SpillsToKeep.erase(SpillBB);
1427       }
1428       // Current Block is the BB containing the new hoisted spill. Add it to
1429       // SpillsToKeep. LiveReg is the source of the new spill.
1430       SpillsToKeep[*RIt] = LiveReg;
1431       LLVM_DEBUG({
1432         dbgs() << "spills in BB: ";
1433         for (const auto Rspill : SpillsInSubTree)
1434           dbgs() << Rspill->getBlock()->getNumber() << " ";
1435         dbgs() << "were promoted to BB" << (*RIt)->getBlock()->getNumber()
1436                << "\n";
1437       });
1438       SpillsInSubTree.clear();
1439       SpillsInSubTree.insert(*RIt);
1440       SubTreeCost = MBFI.getBlockFreq(Block);
1441     }
1442   }
1443   // For spills in SpillsToKeep with LiveReg set (i.e., not original spill),
1444   // save them to SpillsToIns.
1445   for (const auto &Ent : SpillsToKeep) {
1446     if (Ent.second)
1447       SpillsToIns[Ent.first->getBlock()] = Ent.second;
1448   }
1449 }
1450 
1451 /// For spills with equal values, remove redundant spills and hoist those left
1452 /// to less hot spots.
1453 ///
1454 /// Spills with equal values will be collected into the same set in
1455 /// MergeableSpills when spill is inserted. These equal spills are originated
1456 /// from the same defining instruction and are dominated by the instruction.
1457 /// Before hoisting all the equal spills, redundant spills inside in the same
1458 /// BB are first marked to be deleted. Then starting from the spills left, walk
1459 /// up on the dominator tree towards the Root node where the define instruction
1460 /// is located, mark the dominated spills to be deleted along the way and
1461 /// collect the BB nodes on the path from non-dominated spills to the define
1462 /// instruction into a WorkSet. The nodes in WorkSet are the candidate places
1463 /// where we are considering to hoist the spills. We iterate the WorkSet in
1464 /// bottom-up order, and for each node, we will decide whether to hoist spills
1465 /// inside its subtree to that node. In this way, we can get benefit locally
1466 /// even if hoisting all the equal spills to one cold place is impossible.
1467 void HoistSpillHelper::hoistAllSpills() {
1468   SmallVector<unsigned, 4> NewVRegs;
1469   LiveRangeEdit Edit(nullptr, NewVRegs, MF, LIS, &VRM, this);
1470 
1471   for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
1472     unsigned Reg = Register::index2VirtReg(i);
1473     unsigned Original = VRM.getPreSplitReg(Reg);
1474     if (!MRI.def_empty(Reg))
1475       Virt2SiblingsMap[Original].insert(Reg);
1476   }
1477 
1478   // Each entry in MergeableSpills contains a spill set with equal values.
1479   for (auto &Ent : MergeableSpills) {
1480     int Slot = Ent.first.first;
1481     LiveInterval &OrigLI = *StackSlotToOrigLI[Slot];
1482     VNInfo *OrigVNI = Ent.first.second;
1483     SmallPtrSet<MachineInstr *, 16> &EqValSpills = Ent.second;
1484     if (Ent.second.empty())
1485       continue;
1486 
1487     LLVM_DEBUG({
1488       dbgs() << "\nFor Slot" << Slot << " and VN" << OrigVNI->id << ":\n"
1489              << "Equal spills in BB: ";
1490       for (const auto spill : EqValSpills)
1491         dbgs() << spill->getParent()->getNumber() << " ";
1492       dbgs() << "\n";
1493     });
1494 
1495     // SpillsToRm is the spill set to be removed from EqValSpills.
1496     SmallVector<MachineInstr *, 16> SpillsToRm;
1497     // SpillsToIns is the spill set to be newly inserted after hoisting.
1498     DenseMap<MachineBasicBlock *, unsigned> SpillsToIns;
1499 
1500     runHoistSpills(OrigLI, *OrigVNI, EqValSpills, SpillsToRm, SpillsToIns);
1501 
1502     LLVM_DEBUG({
1503       dbgs() << "Finally inserted spills in BB: ";
1504       for (const auto &Ispill : SpillsToIns)
1505         dbgs() << Ispill.first->getNumber() << " ";
1506       dbgs() << "\nFinally removed spills in BB: ";
1507       for (const auto Rspill : SpillsToRm)
1508         dbgs() << Rspill->getParent()->getNumber() << " ";
1509       dbgs() << "\n";
1510     });
1511 
1512     // Stack live range update.
1513     LiveInterval &StackIntvl = LSS.getInterval(Slot);
1514     if (!SpillsToIns.empty() || !SpillsToRm.empty())
1515       StackIntvl.MergeValueInAsValue(OrigLI, OrigVNI,
1516                                      StackIntvl.getValNumInfo(0));
1517 
1518     // Insert hoisted spills.
1519     for (auto const &Insert : SpillsToIns) {
1520       MachineBasicBlock *BB = Insert.first;
1521       unsigned LiveReg = Insert.second;
1522       MachineBasicBlock::iterator MI = IPA.getLastInsertPointIter(OrigLI, *BB);
1523       TII.storeRegToStackSlot(*BB, MI, LiveReg, false, Slot,
1524                               MRI.getRegClass(LiveReg), &TRI);
1525       LIS.InsertMachineInstrRangeInMaps(std::prev(MI), MI);
1526       ++NumSpills;
1527     }
1528 
1529     // Remove redundant spills or change them to dead instructions.
1530     NumSpills -= SpillsToRm.size();
1531     for (auto const RMEnt : SpillsToRm) {
1532       RMEnt->setDesc(TII.get(TargetOpcode::KILL));
1533       for (unsigned i = RMEnt->getNumOperands(); i; --i) {
1534         MachineOperand &MO = RMEnt->getOperand(i - 1);
1535         if (MO.isReg() && MO.isImplicit() && MO.isDef() && !MO.isDead())
1536           RMEnt->RemoveOperand(i - 1);
1537       }
1538     }
1539     Edit.eliminateDeadDefs(SpillsToRm, None, AA);
1540   }
1541 }
1542 
1543 /// For VirtReg clone, the \p New register should have the same physreg or
1544 /// stackslot as the \p old register.
1545 void HoistSpillHelper::LRE_DidCloneVirtReg(unsigned New, unsigned Old) {
1546   if (VRM.hasPhys(Old))
1547     VRM.assignVirt2Phys(New, VRM.getPhys(Old));
1548   else if (VRM.getStackSlot(Old) != VirtRegMap::NO_STACK_SLOT)
1549     VRM.assignVirt2StackSlot(New, VRM.getStackSlot(Old));
1550   else
1551     llvm_unreachable("VReg should be assigned either physreg or stackslot");
1552 }
1553