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