LLVM 23.0.0git
InlineSpiller.cpp
Go to the documentation of this file.
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 "AllocationOrder.h"
15#include "SplitKit.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/MapVector.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/Statistic.h"
46#include "llvm/Config/llvm-config.h"
51#include "llvm/Support/Debug.h"
54#include <cassert>
55#include <iterator>
56#include <tuple>
57#include <utility>
58
59using namespace llvm;
60
61#define DEBUG_TYPE "regalloc"
62
63STATISTIC(NumSpilledRanges, "Number of spilled live ranges");
64STATISTIC(NumSnippets, "Number of spilled snippets");
65STATISTIC(NumSpills, "Number of spills inserted");
66STATISTIC(NumSpillsRemoved, "Number of spills removed");
67STATISTIC(NumReloads, "Number of reloads inserted");
68STATISTIC(NumReloadsRemoved, "Number of reloads removed");
69STATISTIC(NumFolded, "Number of folded stack accesses");
70STATISTIC(NumFoldedLoads, "Number of folded loads");
71STATISTIC(NumRemats, "Number of rematerialized defs for spilling");
72
73static cl::opt<bool>
74RestrictStatepointRemat("restrict-statepoint-remat",
75 cl::init(false), cl::Hidden,
76 cl::desc("Restrict remat for statepoint operands"));
77
78namespace {
79class HoistSpillHelper : private LiveRangeEdit::Delegate {
81 LiveIntervals &LIS;
82 LiveStacks &LSS;
84 VirtRegMap &VRM;
86 const TargetInstrInfo &TII;
88 const MachineBlockFrequencyInfo &MBFI;
90
92
93 // Map from StackSlot to the LiveInterval of the original register.
94 // Note the LiveInterval of the original register may have been deleted
95 // after it is spilled. We keep a copy here to track the range where
96 // spills can be moved.
98
99 // Map from pair of (StackSlot and Original VNI) to a set of spills which
100 // have the same stackslot and have equal values defined by Original VNI.
101 // These spills are mergeable and are hoist candidates.
102 using MergeableSpillsMap =
104 MergeableSpillsMap MergeableSpills;
105
106 /// This is the map from original register to a set containing all its
107 /// siblings. To hoist a spill to another BB, we need to find out a live
108 /// sibling there and use it as the source of the new spill.
110
111 bool isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI,
112 MachineBasicBlock &BB, Register &LiveReg);
113
114 void rmRedundantSpills(
118
119 void getVisitOrders(
125
126 void runHoistSpills(LiveInterval &OrigLI, VNInfo &OrigVNI,
130
131public:
132 HoistSpillHelper(const Spiller::RequiredAnalyses &Analyses,
133 MachineFunction &mf, VirtRegMap &vrm, LiveRegMatrix *matrix)
134 : MF(mf), LIS(Analyses.LIS), LSS(Analyses.LSS), MDT(Analyses.MDT),
135 VRM(vrm), MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()),
136 TRI(*mf.getSubtarget().getRegisterInfo()), MBFI(Analyses.MBFI),
137 Matrix(matrix), IPA(LIS, mf.getNumBlockIDs()) {}
138
139 void addToMergeableSpills(MachineInstr &Spill, int StackSlot,
140 Register Original);
141 bool rmFromMergeableSpills(MachineInstr &Spill, int StackSlot);
142 void hoistAllSpills();
143 bool LRE_CanEraseVirtReg(Register) override;
144 void LRE_DidCloneVirtReg(Register, Register) override;
145};
146
147class InlineSpiller : public Spiller {
148 MachineFunction &MF;
149 LiveIntervals &LIS;
150 LiveStacks &LSS;
151 VirtRegMap &VRM;
152 MachineRegisterInfo &MRI;
153 const TargetInstrInfo &TII;
154 const TargetRegisterInfo &TRI;
155 LiveRegMatrix *Matrix = nullptr;
156
157 // Variables that are valid during spill(), but used by multiple methods.
158 LiveRangeEdit *Edit = nullptr;
159 LiveInterval *StackInt = nullptr;
160 int StackSlot;
161 Register Original;
162 AllocationOrder *Order = nullptr;
163
164 // All registers to spill to StackSlot, including the main register.
165 SmallVector<Register, 8> RegsToSpill;
166
167 // All registers that were replaced by the spiller through some other method,
168 // e.g. rematerialization.
169 SmallVector<Register, 8> RegsReplaced;
170
171 // All COPY instructions to/from snippets.
172 // They are ignored since both operands refer to the same stack slot.
173 // For bundled copies, this will only include the first header copy.
174 SmallPtrSet<MachineInstr*, 8> SnippetCopies;
175
176 // Values that failed to remat at some point.
177 SmallPtrSet<VNInfo*, 8> UsedValues;
178
179 // Dead defs generated during spilling.
180 SmallVector<MachineInstr*, 8> DeadDefs;
181
182 // Object records spills information and does the hoisting.
183 HoistSpillHelper HSpiller;
184
185 // Live range weight calculator.
186 VirtRegAuxInfo &VRAI;
187
188 ~InlineSpiller() override = default;
189
190public:
191 InlineSpiller(const Spiller::RequiredAnalyses &Analyses, MachineFunction &MF,
192 VirtRegMap &VRM, VirtRegAuxInfo &VRAI, LiveRegMatrix *Matrix)
193 : MF(MF), LIS(Analyses.LIS), LSS(Analyses.LSS), VRM(VRM),
194 MRI(MF.getRegInfo()), TII(*MF.getSubtarget().getInstrInfo()),
195 TRI(*MF.getSubtarget().getRegisterInfo()), Matrix(Matrix),
196 HSpiller(Analyses, MF, VRM, Matrix), VRAI(VRAI) {}
197
198 void spill(LiveRangeEdit &, AllocationOrder *Order = nullptr) override;
199 ArrayRef<Register> getSpilledRegs() override { return RegsToSpill; }
200 ArrayRef<Register> getReplacedRegs() override { return RegsReplaced; }
201 void postOptimization() override;
202
203private:
204 bool isSnippet(const LiveInterval &SnipLI);
205 void collectRegsToSpill();
206
207 bool isRegToSpill(Register Reg) { return is_contained(RegsToSpill, Reg); }
208
209 bool isSibling(Register Reg);
210 bool hoistSpillInsideBB(LiveInterval &SpillLI, MachineInstr &CopyMI);
211 void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
212
213 void markValueUsed(LiveInterval*, VNInfo*);
214 bool canGuaranteeAssignmentAfterRemat(Register VReg, MachineInstr &MI);
215 bool hasPhysRegAvailable(const MachineInstr &MI);
216 bool reMaterializeFor(LiveInterval &, MachineInstr &MI);
217 void reMaterializeAll();
218
219 bool coalesceStackAccess(MachineInstr *MI, Register Reg);
220 bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>>,
221 MachineInstr *LoadMI = nullptr);
222 void insertReload(Register VReg, SlotIndex, MachineBasicBlock::iterator MI);
223 void insertSpill(Register VReg, bool isKill, MachineBasicBlock::iterator MI);
224
225 void spillAroundUses(Register Reg);
226 void spillAll();
227};
228
229} // end anonymous namespace
230
231Spiller::~Spiller() = default;
232
233void Spiller::anchor() {}
234
235Spiller *
236llvm::createInlineSpiller(const InlineSpiller::RequiredAnalyses &Analyses,
237 MachineFunction &MF, VirtRegMap &VRM,
239 return new InlineSpiller(Analyses, MF, VRM, VRAI, Matrix);
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.
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.
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;
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
311static 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().
320bool 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.
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.
383void InlineSpiller::collectRegsToSpill() {
384 Register Reg = Edit->getReg();
385
386 // Main register always spills.
387 RegsToSpill.assign(1, Reg);
388 SnippetCopies.clear();
389 RegsReplaced.clear();
390
391 // Snippets all have the same original, so there can't be any for an original
392 // register.
393 if (Original == Reg)
394 return;
395
396 for (MachineInstr &MI : llvm::make_early_inc_range(MRI.reg_bundles(Reg))) {
397 Register SnipReg = isCopyOfBundle(MI, Reg, TII);
398 if (!isSibling(SnipReg))
399 continue;
400 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
401 if (!isSnippet(SnipLI))
402 continue;
403 SnippetCopies.insert(&MI);
404 if (isRegToSpill(SnipReg))
405 continue;
406 RegsToSpill.push_back(SnipReg);
407 LLVM_DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
408 ++NumSnippets;
409 }
410}
411
412bool InlineSpiller::isSibling(Register Reg) {
413 return Reg.isVirtual() && VRM.getOriginal(Reg) == Original;
414}
415
416/// It is beneficial to spill to earlier place in the same BB in case
417/// as follows:
418/// There is an alternative def earlier in the same MBB.
419/// Hoist the spill as far as possible in SpillMBB. This can ease
420/// register pressure:
421///
422/// x = def
423/// y = use x
424/// s = copy x
425///
426/// Hoisting the spill of s to immediately after the def removes the
427/// interference between x and y:
428///
429/// x = def
430/// spill x
431/// y = use killed x
432///
433/// This hoist only helps when the copy kills its source.
434///
435bool InlineSpiller::hoistSpillInsideBB(LiveInterval &SpillLI,
436 MachineInstr &CopyMI) {
437 SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
438#ifndef NDEBUG
439 VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot());
440 assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy");
441#endif
442
443 Register SrcReg = CopyMI.getOperand(1).getReg();
444 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
445 VNInfo *SrcVNI = SrcLI.getVNInfoAt(Idx);
446 LiveQueryResult SrcQ = SrcLI.Query(Idx);
447 MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(SrcVNI->def);
448 if (DefMBB != CopyMI.getParent() || !SrcQ.isKill())
449 return false;
450
451 // Conservatively extend the stack slot range to the range of the original
452 // value. We may be able to do better with stack slot coloring by being more
453 // careful here.
454 assert(StackInt && "No stack slot assigned yet.");
455 LiveInterval &OrigLI = LIS.getInterval(Original);
456 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
457 StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
458 LLVM_DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
459 << *StackInt << '\n');
460
461 // We are going to spill SrcVNI immediately after its def, so clear out
462 // any later spills of the same value.
463 eliminateRedundantSpills(SrcLI, SrcVNI);
464
465 MachineBasicBlock *MBB = LIS.getMBBFromIndex(SrcVNI->def);
467 if (SrcVNI->isPHIDef())
468 MII = MBB->SkipPHIsLabelsAndDebug(MBB->begin(), SrcReg);
469 else {
470 MachineInstr *DefMI = LIS.getInstructionFromIndex(SrcVNI->def);
471 assert(DefMI && "Defining instruction disappeared");
472 MII = DefMI;
473 ++MII;
474 }
475 MachineInstrSpan MIS(MII, MBB);
476 // Insert spill without kill flag immediately after def.
477 TII.storeRegToStackSlot(*MBB, MII, SrcReg, false, StackSlot,
478 MRI.getRegClass(SrcReg), Register());
479 LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MII);
480 for (const MachineInstr &MI : make_range(MIS.begin(), MII))
481 getVDefInterval(MI, LIS);
482 --MII; // Point to store instruction.
483 LLVM_DEBUG(dbgs() << "\thoisted: " << SrcVNI->def << '\t' << *MII);
484
485 // When the def is a PHI, SkipPHIsLabelsAndDebug may place the store past
486 // prologue instructions. Therefore if that copy was the end of a segment
487 // we need to extend it to the store.
488 if (SrcVNI->isPHIDef()) {
489 SlotIndex StoreUseIdx = LIS.getInstructionIndex(*MII).getRegSlot(true);
490 SrcLI.extendInBlock(LIS.getMBBStartIdx(MBB), StoreUseIdx);
491 }
492
493 // If there is only 1 store instruction is required for spill, add it
494 // to mergeable list. In X86 AMX, 2 intructions are required to store.
495 // We disable the merge for this case.
496 if (MIS.begin() == MII)
497 HSpiller.addToMergeableSpills(*MII, StackSlot, Original);
498 ++NumSpills;
499 return true;
500}
501
502/// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
503/// redundant spills of this value in SLI.reg and sibling copies.
504void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
505 assert(VNI && "Missing value");
507 WorkList.push_back(std::make_pair(&SLI, VNI));
508 assert(StackInt && "No stack slot assigned yet.");
509
510 do {
511 LiveInterval *LI;
512 std::tie(LI, VNI) = WorkList.pop_back_val();
513 Register Reg = LI->reg();
514 LLVM_DEBUG(dbgs() << "Checking redundant spills for " << VNI->id << '@'
515 << VNI->def << " in " << *LI << '\n');
516
517 // Regs to spill are taken care of.
518 if (isRegToSpill(Reg))
519 continue;
520
521 // Add all of VNI's live range to StackInt.
522 StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
523 LLVM_DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
524
525 // Find all spills and copies of VNI.
526 for (MachineInstr &MI :
527 llvm::make_early_inc_range(MRI.use_nodbg_bundles(Reg))) {
528 if (!MI.mayStore() && !TII.isCopyInstr(MI))
529 continue;
530 SlotIndex Idx = LIS.getInstructionIndex(MI);
531 if (LI->getVNInfoAt(Idx) != VNI)
532 continue;
533
534 // Follow sibling copies down the dominator tree.
535 if (Register DstReg = isCopyOfBundle(MI, Reg, TII)) {
536 if (isSibling(DstReg)) {
537 LiveInterval &DstLI = LIS.getInterval(DstReg);
538 VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
539 assert(DstVNI && "Missing defined value");
540 assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
541
542 WorkList.push_back(std::make_pair(&DstLI, DstVNI));
543 }
544 continue;
545 }
546
547 // Erase spills.
548 int FI;
549 if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
550 LLVM_DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << MI);
551 // eliminateDeadDefs won't normally remove stores, so switch opcode.
552 MI.setDesc(TII.get(TargetOpcode::KILL));
553 DeadDefs.push_back(&MI);
554 ++NumSpillsRemoved;
555 if (HSpiller.rmFromMergeableSpills(MI, StackSlot))
556 --NumSpills;
557 }
558 }
559 } while (!WorkList.empty());
560}
561
562//===----------------------------------------------------------------------===//
563// Rematerialization
564//===----------------------------------------------------------------------===//
565
566/// markValueUsed - Remember that VNI failed to rematerialize, so its defining
567/// instruction cannot be eliminated. See through snippet copies
568void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
570 WorkList.push_back(std::make_pair(LI, VNI));
571 do {
572 std::tie(LI, VNI) = WorkList.pop_back_val();
573 if (!UsedValues.insert(VNI).second)
574 continue;
575
576 if (VNI->isPHIDef()) {
577 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
578 for (MachineBasicBlock *P : MBB->predecessors()) {
579 VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(P));
580 if (PVNI)
581 WorkList.push_back(std::make_pair(LI, PVNI));
582 }
583 continue;
584 }
585
586 // Follow snippet copies.
587 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
588 if (!SnippetCopies.count(MI))
589 continue;
590 LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
591 assert(isRegToSpill(SnipLI.reg()) && "Unexpected register in copy");
592 VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
593 assert(SnipVNI && "Snippet undefined before copy");
594 WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
595 } while (!WorkList.empty());
596}
597
598bool InlineSpiller::canGuaranteeAssignmentAfterRemat(Register VReg,
599 MachineInstr &MI) {
601 return true;
602 // Here's a quick explanation of the problem we're trying to handle here:
603 // * There are some pseudo instructions with more vreg uses than there are
604 // physical registers on the machine.
605 // * This is normally handled by spilling the vreg, and folding the reload
606 // into the user instruction. (Thus decreasing the number of used vregs
607 // until the remainder can be assigned to physregs.)
608 // * However, since we may try to spill vregs in any order, we can end up
609 // trying to spill each operand to the instruction, and then rematting it
610 // instead. When that happens, the new live intervals (for the remats) are
611 // expected to be trivially assignable (i.e. RS_Done). However, since we
612 // may have more remats than physregs, we're guaranteed to fail to assign
613 // one.
614 // At the moment, we only handle this for STATEPOINTs since they're the only
615 // pseudo op where we've seen this. If we start seeing other instructions
616 // with the same problem, we need to revisit this.
617 if (MI.getOpcode() != TargetOpcode::STATEPOINT)
618 return true;
619 // For STATEPOINTs we allow re-materialization for fixed arguments only hoping
620 // that number of physical registers is enough to cover all fixed arguments.
621 // If it is not true we need to revisit it.
622 for (unsigned Idx = StatepointOpers(&MI).getVarIdx(),
623 EndIdx = MI.getNumOperands();
624 Idx < EndIdx; ++Idx) {
625 MachineOperand &MO = MI.getOperand(Idx);
626 if (MO.isReg() && MO.getReg() == VReg)
627 return false;
628 }
629 return true;
630}
631
632/// hasPhysRegAvailable - Check if there is an available physical register for
633/// rematerialization.
634bool InlineSpiller::hasPhysRegAvailable(const MachineInstr &MI) {
635 if (!Order || !Matrix)
636 return false;
637
638 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
639 SlotIndex PrevIdx = UseIdx.getPrevSlot();
640
641 for (MCPhysReg PhysReg : *Order) {
642 if (!Matrix->checkInterference(PrevIdx, UseIdx, PhysReg))
643 return true;
644 }
645
646 return false;
647}
648
649/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
650bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg, MachineInstr &MI) {
651 // Analyze instruction
653 VirtRegInfo RI = AnalyzeVirtRegInBundle(MI, VirtReg.reg(), &Ops);
654
655 // Defs without reads will be deleted if unused after remat is
656 // completed for other users of the virtual register.
657 if (!RI.Reads) {
658 LLVM_DEBUG(dbgs() << "\tskipping remat of def " << MI);
659 return false;
660 }
661
662 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
663 VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
664
665 if (!ParentVNI) {
666 LLVM_DEBUG(dbgs() << "\tadding <undef> flags: ");
667 for (MachineOperand &MO : MI.all_uses())
668 if (MO.getReg() == VirtReg.reg())
669 MO.setIsUndef();
670 LLVM_DEBUG(dbgs() << UseIdx << '\t' << MI);
671 return true;
672 }
673
674 // Snippets copies are ignored for remat, and will be deleted if they
675 // don't feed a live user after rematerialization completes.
676 if (SnippetCopies.count(&MI)) {
677 LLVM_DEBUG(dbgs() << "\tskipping remat snippet copy for " << UseIdx << '\t'
678 << MI);
679 return false;
680 }
681
682 LiveInterval &OrigLI = LIS.getInterval(Original);
683 VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
684 assert(OrigVNI && "corrupted sub-interval");
685 MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
686 // This can happen if for two reasons: 1) This could be a phi valno,
687 // or 2) the remat def has already been removed from the original
688 // live interval; this happens if we rematted to all uses, and
689 // then further split one of those live ranges.
690 if (!DefMI) {
691 markValueUsed(&VirtReg, ParentVNI);
692 LLVM_DEBUG(dbgs() << "\tcannot remat missing def for " << UseIdx << '\t'
693 << MI);
694 return false;
695 }
696
697 LiveRangeEdit::Remat RM(ParentVNI);
698 RM.OrigMI = DefMI;
699 if (!Edit->canRematerializeAt(RM, UseIdx)) {
700 markValueUsed(&VirtReg, ParentVNI);
701 LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
702 return false;
703 }
704
705 // If the instruction also writes VirtReg.reg, it had better not require the
706 // same register for uses and defs.
707 if (RI.Tied) {
708 markValueUsed(&VirtReg, ParentVNI);
709 LLVM_DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << MI);
710 return false;
711 }
712
713 // Before rematerializing into a register for a single instruction, try to
714 // fold a load into the instruction. That avoids allocating a new register.
715 if (RM.OrigMI->canFoldAsLoad() &&
716 (RM.OrigMI->mayLoad() || !hasPhysRegAvailable(MI)) &&
717 foldMemoryOperand(Ops, RM.OrigMI)) {
718 Edit->markRematerialized(RM.ParentVNI);
719 ++NumFoldedLoads;
720 return true;
721 }
722
723 // If we can't guarantee that we'll be able to actually assign the new vreg,
724 // we can't remat.
725 if (!canGuaranteeAssignmentAfterRemat(VirtReg.reg(), MI)) {
726 markValueUsed(&VirtReg, ParentVNI);
727 LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
728 return false;
729 }
730
731 // Allocate a new register for the remat.
732 Register NewVReg = Edit->createFrom(Original);
733
734 // Constrain it to the register class of MI.
735 MRI.constrainRegClass(NewVReg, MRI.getRegClass(VirtReg.reg()));
736
737 // Compute which lanes of the virtual register are live at the use point.
738 LaneBitmask UsedLanes = LaneBitmask::getAll();
739 if (VirtReg.hasSubRanges()) {
740 UsedLanes = LaneBitmask::getNone();
741 for (const LiveInterval::SubRange &SR : VirtReg.subranges())
742 if (SR.liveAt(UseIdx))
743 UsedLanes |= SR.LaneMask;
744 }
745
746 // Finally we can rematerialize OrigMI before MI.
747 SlotIndex DefIdx = Edit->rematerializeAt(*MI.getParent(), MI, NewVReg, RM,
748 TRI, false, 0, nullptr, UsedLanes);
749
750 // We take the DebugLoc from MI, since OrigMI may be attributed to a
751 // different source location.
752 auto *NewMI = LIS.getInstructionFromIndex(DefIdx);
753 NewMI->setDebugLoc(MI.getDebugLoc());
754
755 (void)DefIdx;
756 LLVM_DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
757 << *LIS.getInstructionFromIndex(DefIdx));
758
759 // Replace operands
760 for (const auto &OpPair : Ops) {
761 MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
762 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg()) {
763 MO.setReg(NewVReg);
764 MO.setIsKill();
765 }
766 }
767 LLVM_DEBUG(dbgs() << "\t " << UseIdx << '\t' << MI << '\n');
768
769 ++NumRemats;
770 return true;
771}
772
773/// reMaterializeAll - Try to rematerialize as many uses as possible,
774/// and trim the live ranges after.
775void InlineSpiller::reMaterializeAll() {
776 UsedValues.clear();
777
778 // Try to remat before all uses of snippets.
779 bool anyRemat = false;
780 for (Register Reg : RegsToSpill) {
781 LiveInterval &LI = LIS.getInterval(Reg);
782 for (MachineInstr &MI : llvm::make_early_inc_range(MRI.reg_bundles(Reg))) {
783 // Debug values are not allowed to affect codegen.
784 if (MI.isDebugValue())
785 continue;
786
787 assert(!MI.isDebugInstr() && "Did not expect to find a use in debug "
788 "instruction that isn't a DBG_VALUE");
789
790 anyRemat |= reMaterializeFor(LI, MI);
791 }
792 }
793 if (!anyRemat)
794 return;
795
796 // Remove any values that were completely rematted.
797 for (Register Reg : RegsToSpill) {
798 LiveInterval &LI = LIS.getInterval(Reg);
799 for (VNInfo *VNI : LI.vnis()) {
800 if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
801 continue;
802 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
803 MI->addRegisterDead(Reg, &TRI);
804 if (!MI->allDefsAreDead())
805 continue;
806 LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
807 DeadDefs.push_back(MI);
808 // If MI is a bundle header, also try removing copies inside the bundle,
809 // otherwise the verifier would complain "live range continues after dead
810 // def flag".
811 if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) {
812 MachineBasicBlock::instr_iterator BeginIt = MI->getIterator(),
813 EndIt = MI->getParent()->instr_end();
814 ++BeginIt; // Skip MI that was already handled.
815
816 bool OnlyDeadCopies = true;
817 for (MachineBasicBlock::instr_iterator It = BeginIt;
818 It != EndIt && It->isBundledWithPred(); ++It) {
819
820 auto DestSrc = TII.isCopyInstr(*It);
821 bool IsCopyToDeadReg =
822 DestSrc && DestSrc->Destination->getReg() == Reg;
823 if (!IsCopyToDeadReg) {
824 OnlyDeadCopies = false;
825 break;
826 }
827 }
828 if (OnlyDeadCopies) {
829 for (MachineBasicBlock::instr_iterator It = BeginIt;
830 It != EndIt && It->isBundledWithPred(); ++It) {
831 It->addRegisterDead(Reg, &TRI);
832 LLVM_DEBUG(dbgs() << "All defs dead: " << *It);
833 DeadDefs.push_back(&*It);
834 }
835 }
836 }
837 }
838 }
839
840 // Eliminate dead code after remat. Note that some snippet copies may be
841 // deleted here.
842 if (DeadDefs.empty())
843 return;
844 LLVM_DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
845 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
846
847 // LiveRangeEdit::eliminateDeadDef is used to remove dead define instructions
848 // after rematerialization. To remove a VNI for a vreg from its LiveInterval,
849 // LiveIntervals::removeVRegDefAt is used. However, after non-PHI VNIs are all
850 // removed, PHI VNI are still left in the LiveInterval.
851 // So to get rid of unused reg, we need to check whether it has non-dbg
852 // reference instead of whether it has non-empty interval.
853 unsigned ResultPos = 0;
854 for (Register Reg : RegsToSpill) {
855 if (MRI.reg_nodbg_empty(Reg)) {
856 Edit->eraseVirtReg(Reg);
857 RegsReplaced.push_back(Reg);
858 continue;
859 }
860
861 assert(LIS.hasInterval(Reg) &&
862 (!LIS.getInterval(Reg).empty() || !MRI.reg_nodbg_empty(Reg)) &&
863 "Empty and not used live-range?!");
864
865 RegsToSpill[ResultPos++] = Reg;
866 }
867 RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
868 LLVM_DEBUG(dbgs() << RegsToSpill.size()
869 << " registers to spill after remat.\n");
870}
871
872//===----------------------------------------------------------------------===//
873// Spilling
874//===----------------------------------------------------------------------===//
875
876/// If MI is a load or store of StackSlot, it can be removed.
877bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, Register Reg) {
878 int FI = 0;
879 Register InstrReg = TII.isLoadFromStackSlot(*MI, FI);
880 bool IsLoad = InstrReg.isValid();
881 if (!IsLoad)
882 InstrReg = TII.isStoreToStackSlot(*MI, FI);
883
884 // We have a stack access. Is it the right register and slot?
885 if (InstrReg != Reg || FI != StackSlot)
886 return false;
887
888 if (!IsLoad)
889 HSpiller.rmFromMergeableSpills(*MI, StackSlot);
890
891 LLVM_DEBUG(dbgs() << "Coalescing stack access: " << *MI);
892 LIS.RemoveMachineInstrFromMaps(*MI);
893 MI->eraseFromParent();
894
895 if (IsLoad) {
896 ++NumReloadsRemoved;
897 --NumReloads;
898 } else {
899 ++NumSpillsRemoved;
900 --NumSpills;
901 }
902
903 return true;
904}
905
906#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
908// Dump the range of instructions from B to E with their slot indexes.
911 LiveIntervals const &LIS,
912 const char *const header,
913 Register VReg = Register()) {
914 char NextLine = '\n';
915 char SlotIndent = '\t';
916
917 if (std::next(B) == E) {
918 NextLine = ' ';
919 SlotIndent = ' ';
920 }
921
922 dbgs() << '\t' << header << ": " << NextLine;
923
924 for (MachineBasicBlock::iterator I = B; I != E; ++I) {
926
927 // If a register was passed in and this instruction has it as a
928 // destination that is marked as an early clobber, print the
929 // early-clobber slot index.
930 if (VReg) {
931 MachineOperand *MO = I->findRegisterDefOperand(VReg, /*TRI=*/nullptr);
932 if (MO && MO->isEarlyClobber())
933 Idx = Idx.getRegSlot(true);
934 }
935
936 dbgs() << SlotIndent << Idx << '\t' << *I;
937 }
938}
939#endif
940
941/// foldMemoryOperand - Try folding stack slot references in Ops into their
942/// instructions.
943///
944/// @param Ops Operand indices from AnalyzeVirtRegInBundle().
945/// @param LoadMI Load instruction to use instead of stack slot when non-null.
946/// @return True on success.
947bool InlineSpiller::
948foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
949 MachineInstr *LoadMI) {
950 if (Ops.empty())
951 return false;
952 // Don't attempt folding in bundles.
953 MachineInstr *MI = Ops.front().first;
954 if (Ops.back().first != MI || MI->isBundled())
955 return false;
956
957 bool WasCopy = TII.isCopyInstr(*MI).has_value();
958 Register ImpReg;
959
960 // TII::foldMemoryOperand will do what we need here for statepoint
961 // (fold load into use and remove corresponding def). We will replace
962 // uses of removed def with loads (spillAroundUses).
963 // For that to work we need to untie def and use to pass it through
964 // foldMemoryOperand and signal foldPatchpoint that it is allowed to
965 // fold them.
966 bool UntieRegs = MI->getOpcode() == TargetOpcode::STATEPOINT;
967
968 // Spill subregs if the target allows it.
969 // We always want to spill subregs for stackmap/patchpoint pseudos.
970 bool SpillSubRegs = TII.isSubregFoldable() ||
971 MI->getOpcode() == TargetOpcode::STATEPOINT ||
972 MI->getOpcode() == TargetOpcode::PATCHPOINT ||
973 MI->getOpcode() == TargetOpcode::STACKMAP;
974
975 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
976 // operands.
978 for (const auto &OpPair : Ops) {
979 unsigned Idx = OpPair.second;
980 assert(MI == OpPair.first && "Instruction conflict during operand folding");
981 MachineOperand &MO = MI->getOperand(Idx);
982
983 // No point restoring an undef read, and we'll produce an invalid live
984 // interval.
985 // TODO: Is this really the correct way to handle undef tied uses?
986 if (MO.isUse() && !MO.readsReg() && !MO.isTied())
987 continue;
988
989 if (MO.isImplicit()) {
990 ImpReg = MO.getReg();
991 continue;
992 }
993
994 if (!SpillSubRegs && MO.getSubReg())
995 return false;
996 // We cannot fold a load instruction into a def.
997 if (LoadMI && MO.isDef())
998 return false;
999 // Tied use operands should not be passed to foldMemoryOperand.
1000 if (UntieRegs || !MI->isRegTiedToDefOperand(Idx))
1001 FoldOps.push_back(Idx);
1002 }
1003
1004 // If we only have implicit uses, we won't be able to fold that.
1005 // Moreover, TargetInstrInfo::foldMemoryOperand will assert if we try!
1006 if (FoldOps.empty())
1007 return false;
1008
1009 MachineInstrSpan MIS(MI, MI->getParent());
1010
1012 if (UntieRegs)
1013 for (unsigned Idx : FoldOps) {
1014 MachineOperand &MO = MI->getOperand(Idx);
1015 if (!MO.isTied())
1016 continue;
1017 unsigned Tied = MI->findTiedOperandIdx(Idx);
1018 if (MO.isUse())
1019 TiedOps.emplace_back(Tied, Idx);
1020 else {
1021 assert(MO.isDef() && "Tied to not use and def?");
1022 TiedOps.emplace_back(Idx, Tied);
1023 }
1024 MI->untieRegOperand(Idx);
1025 }
1026
1027 MachineInstr *CopyMI = nullptr;
1028 MachineInstr *FoldMI =
1029 LoadMI
1030 ? TII.foldMemoryOperand(*MI, FoldOps, *LoadMI, CopyMI, &LIS)
1031 : TII.foldMemoryOperand(*MI, FoldOps, StackSlot, CopyMI, &LIS, &VRM);
1032 if (!FoldMI) {
1033 // Re-tie operands.
1034 for (auto Tied : TiedOps)
1035 MI->tieOperands(Tied.first, Tied.second);
1036 return false;
1037 }
1038
1039 // Remove LIS for any dead defs in the original MI not in FoldMI.
1040 for (MIBundleOperands MO(*MI); MO.isValid(); ++MO) {
1041 if (!MO->isReg())
1042 continue;
1043 Register Reg = MO->getReg();
1044 if (!Reg || Reg.isVirtual() || MRI.isReserved(Reg)) {
1045 continue;
1046 }
1047 // Skip non-Defs, including undef uses and internal reads.
1048 if (MO->isUse())
1049 continue;
1050 PhysRegInfo RI = AnalyzePhysRegInBundle(*FoldMI, Reg, &TRI);
1051 if (RI.FullyDefined)
1052 continue;
1053 // FoldMI does not define this physreg. Remove the LI segment.
1054 assert(MO->isDead() && "Cannot fold physreg def");
1055 SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
1056 LIS.removePhysRegDefAt(Reg.asMCReg(), Idx);
1057 }
1058
1059 int FI;
1060 if (TII.isStoreToStackSlot(*MI, FI) &&
1061 HSpiller.rmFromMergeableSpills(*MI, FI))
1062 --NumSpills;
1063 SlotIndex FoldIdx = LIS.ReplaceMachineInstrInMaps(*MI, *FoldMI);
1064 if (CopyMI) {
1065 SlotIndex CopyIdx = LIS.InsertMachineInstrInMaps(*CopyMI).getRegSlot();
1066 if (!MRI.isSSA()) {
1067 LiveInterval &LI = LIS.getInterval(CopyMI->getOperand(0).getReg());
1068 VNInfo *VNI = LI.getNextValue(CopyIdx, LIS.getVNInfoAllocator());
1069 LI.addSegment(LiveRange::Segment(CopyIdx, FoldIdx.getRegSlot(), VNI));
1070 }
1071 }
1072 // Update the call info.
1073 if (MI->isCandidateForAdditionalCallInfo())
1074 MI->getMF()->moveAdditionalCallInfo(MI, FoldMI);
1075
1076 // If we've folded a store into an instruction labelled with debug-info,
1077 // record a substitution from the old operand to the memory operand. Handle
1078 // the simple common case where operand 0 is the one being folded, plus when
1079 // the destination operand is also a tied def. More values could be
1080 // substituted / preserved with more analysis.
1081 if (MI->peekDebugInstrNum() && Ops[0].second == 0) {
1082 // Helper lambda.
1083 auto MakeSubstitution = [this,FoldMI,MI,&Ops]() {
1084 // Substitute old operand zero to the new instructions memory operand.
1085 unsigned OldOperandNum = Ops[0].second;
1086 unsigned NewNum = FoldMI->getDebugInstrNum();
1087 unsigned OldNum = MI->getDebugInstrNum();
1088 MF.makeDebugValueSubstitution({OldNum, OldOperandNum},
1090 };
1091
1092 const MachineOperand &Op0 = MI->getOperand(Ops[0].second);
1093 if (Ops.size() == 1 && Op0.isDef()) {
1094 MakeSubstitution();
1095 } else if (Ops.size() == 2 && Op0.isDef() && MI->getOperand(1).isTied() &&
1096 Op0.getReg() == MI->getOperand(1).getReg()) {
1097 MakeSubstitution();
1098 }
1099 } else if (MI->peekDebugInstrNum()) {
1100 // This is a debug-labelled instruction, but the operand being folded isn't
1101 // at operand zero. Most likely this means it's a load being folded in.
1102 // Substitute any register defs from operand zero up to the one being
1103 // folded -- past that point, we don't know what the new operand indexes
1104 // will be.
1105 MF.substituteDebugValuesForInst(*MI, *FoldMI, Ops[0].second);
1106 }
1107
1108 MI->eraseFromParent();
1109
1110 // Insert any new instructions other than FoldMI into the LIS maps.
1111 assert(!MIS.empty() && "Unexpected empty span of instructions!");
1112 for (MachineInstr &MI : MIS)
1113 if (&MI != FoldMI && &MI != CopyMI)
1115
1116 if (CopyMI) {
1117 Register R = CopyMI->getOperand(1).getReg();
1118 if (R.isVirtual()) {
1119 LiveInterval &LI = LIS.getInterval(R);
1120 LIS.shrinkToUses(&LI);
1121 } else {
1122 assert(MRI.isReserved(R) && "Unexpected PhysReg in source operand!");
1123 }
1124 }
1125
1126 // TII.foldMemoryOperand may have left some implicit operands on the
1127 // instruction. Strip them.
1128 if (ImpReg)
1129 for (unsigned i = FoldMI->getNumOperands(); i; --i) {
1130 MachineOperand &MO = FoldMI->getOperand(i - 1);
1131 if (!MO.isReg() || !MO.isImplicit())
1132 break;
1133 if (MO.getReg() == ImpReg)
1134 FoldMI->removeOperand(i - 1);
1135 }
1136
1137 LLVM_DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS,
1138 "folded"));
1139
1140 if (!WasCopy)
1141 ++NumFolded;
1142 else if (Ops.front().second == 0) {
1143 ++NumSpills;
1144 // If there is only 1 store instruction is required for spill, add it
1145 // to mergeable list. In X86 AMX, 2 intructions are required to store.
1146 // We disable the merge for this case.
1147 if (std::distance(MIS.begin(), MIS.end()) <= 1)
1148 HSpiller.addToMergeableSpills(*FoldMI, StackSlot, Original);
1149 } else
1150 ++NumReloads;
1151 return true;
1152}
1153
1154void InlineSpiller::insertReload(Register NewVReg,
1155 SlotIndex Idx,
1157 MachineBasicBlock &MBB = *MI->getParent();
1158
1159 MachineInstrSpan MIS(MI, &MBB);
1160 TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot,
1161 MRI.getRegClass(NewVReg), Register());
1162
1163 LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI);
1164
1165 LLVM_DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload",
1166 NewVReg));
1167 ++NumReloads;
1168}
1169
1170/// Check if \p Def fully defines a VReg with an undefined value.
1171/// If that's the case, that means the value of VReg is actually
1172/// not relevant.
1173static bool isRealSpill(const MachineInstr &Def) {
1174 if (!Def.isImplicitDef())
1175 return true;
1176
1177 // We can say that the VReg defined by Def is undef, only if it is
1178 // fully defined by Def. Otherwise, some of the lanes may not be
1179 // undef and the value of the VReg matters.
1180 return Def.getOperand(0).getSubReg();
1181}
1182
1183/// insertSpill - Insert a spill of NewVReg after MI.
1184void InlineSpiller::insertSpill(Register NewVReg, bool isKill,
1186 // Spill are not terminators, so inserting spills after terminators will
1187 // violate invariants in MachineVerifier.
1188 assert(!MI->isTerminator() && "Inserting a spill after a terminator");
1189 MachineBasicBlock &MBB = *MI->getParent();
1190
1191 MachineInstrSpan MIS(MI, &MBB);
1192 MachineBasicBlock::iterator SpillBefore = std::next(MI);
1193 bool IsRealSpill = isRealSpill(*MI);
1194
1195 if (IsRealSpill)
1196 TII.storeRegToStackSlot(MBB, SpillBefore, NewVReg, isKill, StackSlot,
1197 MRI.getRegClass(NewVReg), Register());
1198 else
1199 // Don't spill undef value.
1200 // Anything works for undef, in particular keeping the memory
1201 // uninitialized is a viable option and it saves code size and
1202 // run time.
1203 BuildMI(MBB, SpillBefore, MI->getDebugLoc(), TII.get(TargetOpcode::KILL))
1204 .addReg(NewVReg, getKillRegState(isKill));
1205
1207 LIS.InsertMachineInstrRangeInMaps(Spill, MIS.end());
1208 for (const MachineInstr &MI : make_range(Spill, MIS.end()))
1209 getVDefInterval(MI, LIS);
1210
1211 LLVM_DEBUG(
1212 dumpMachineInstrRangeWithSlotIndex(Spill, MIS.end(), LIS, "spill"));
1213 ++NumSpills;
1214 // If there is only 1 store instruction is required for spill, add it
1215 // to mergeable list. In X86 AMX, 2 intructions are required to store.
1216 // We disable the merge for this case.
1217 if (IsRealSpill && std::distance(Spill, MIS.end()) <= 1)
1218 HSpiller.addToMergeableSpills(*Spill, StackSlot, Original);
1219}
1220
1221/// spillAroundUses - insert spill code around each use of Reg.
1222void InlineSpiller::spillAroundUses(Register Reg) {
1223 LLVM_DEBUG(dbgs() << "spillAroundUses " << printReg(Reg) << '\n');
1224 LiveInterval &OldLI = LIS.getInterval(Reg);
1225
1226 // Iterate over instructions using Reg.
1227 for (MachineInstr &MI : llvm::make_early_inc_range(MRI.reg_bundles(Reg))) {
1228 // Debug values are not allowed to affect codegen.
1229 if (MI.isDebugValue()) {
1230 // Modify DBG_VALUE now that the value is in a spill slot.
1231 MachineBasicBlock *MBB = MI.getParent();
1232 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:\t" << MI);
1233 buildDbgValueForSpill(*MBB, &MI, MI, StackSlot, Reg);
1234 MBB->erase(MI);
1235 continue;
1236 }
1237
1238 assert(!MI.isDebugInstr() && "Did not expect to find a use in debug "
1239 "instruction that isn't a DBG_VALUE");
1240
1241 // Ignore copies to/from snippets. We'll delete them.
1242 if (SnippetCopies.count(&MI))
1243 continue;
1244
1245 // Stack slot accesses may coalesce away.
1246 if (coalesceStackAccess(&MI, Reg))
1247 continue;
1248
1249 // Analyze instruction.
1252
1253 // Find the slot index where this instruction reads and writes OldLI.
1254 // This is usually the def slot, except for tied early clobbers.
1256 if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
1257 if (SlotIndex::isSameInstr(Idx, VNI->def))
1258 Idx = VNI->def;
1259
1260 // Check for a sibling copy.
1261 Register SibReg = isCopyOfBundle(MI, Reg, TII);
1262 if (SibReg && isSibling(SibReg)) {
1263 // This may actually be a copy between snippets.
1264 if (isRegToSpill(SibReg)) {
1265 LLVM_DEBUG(dbgs() << "Found new snippet copy: " << MI);
1266 SnippetCopies.insert(&MI);
1267 continue;
1268 }
1269 if (RI.Writes) {
1270 if (hoistSpillInsideBB(OldLI, MI)) {
1271 // This COPY is now dead, the value is already in the stack slot.
1272 MI.getOperand(0).setIsDead();
1273 DeadDefs.push_back(&MI);
1274 continue;
1275 }
1276 } else {
1277 // This is a reload for a sib-reg copy. Drop spills downstream.
1278 LiveInterval &SibLI = LIS.getInterval(SibReg);
1279 eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
1280 // The COPY will fold to a reload below.
1281 }
1282 }
1283
1284 // Attempt to fold memory ops.
1285 if (foldMemoryOperand(Ops))
1286 continue;
1287
1288 // Create a new virtual register for spill/fill.
1289 // FIXME: Infer regclass from instruction alone.
1290 Register NewVReg = Edit->createFrom(Reg);
1291
1292 if (RI.Reads)
1293 insertReload(NewVReg, Idx, &MI);
1294
1295 // Rewrite instruction operands.
1296 bool hasLiveDef = false;
1297 for (const auto &OpPair : Ops) {
1298 MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
1299 MO.setReg(NewVReg);
1300 if (MO.isUse()) {
1301 if (!OpPair.first->isRegTiedToDefOperand(OpPair.second))
1302 MO.setIsKill();
1303 } else {
1304 if (!MO.isDead())
1305 hasLiveDef = true;
1306 }
1307 }
1308 LLVM_DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << MI << '\n');
1309
1310 // FIXME: Use a second vreg if instruction has no tied ops.
1311 if (RI.Writes)
1312 if (hasLiveDef)
1313 insertSpill(NewVReg, true, &MI);
1314 }
1315}
1316
1317/// spillAll - Spill all registers remaining after rematerialization.
1318void InlineSpiller::spillAll() {
1319 // Update LiveStacks now that we are committed to spilling.
1320 if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
1321 StackSlot = VRM.assignVirt2StackSlot(Original);
1322 StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
1323 StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
1324 } else
1325 StackInt = &LSS.getInterval(StackSlot);
1326
1327 if (Original != Edit->getReg())
1328 VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
1329
1330 assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
1331 for (Register Reg : RegsToSpill)
1332 StackInt->MergeSegmentsInAsValue(LIS.getInterval(Reg),
1333 StackInt->getValNumInfo(0));
1334 LLVM_DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
1335
1336 // Spill around uses of all RegsToSpill.
1337 for (Register Reg : RegsToSpill) {
1338 spillAroundUses(Reg);
1339 // Assign all of the spilled registers to the slot so that
1340 // LiveDebugVariables knows about these locations later on.
1341 if (VRM.getStackSlot(Reg) == VirtRegMap::NO_STACK_SLOT)
1342 VRM.assignVirt2StackSlot(Reg, StackSlot);
1343 }
1344
1345 // Hoisted spills may cause dead code.
1346 if (!DeadDefs.empty()) {
1347 LLVM_DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
1348 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
1349 }
1350
1351 // Finally delete the SnippetCopies.
1352 for (Register Reg : RegsToSpill) {
1353 for (MachineInstr &MI :
1354 llvm::make_early_inc_range(MRI.reg_instructions(Reg))) {
1355 assert(SnippetCopies.count(&MI) && "Remaining use wasn't a snippet copy");
1356 // FIXME: Do this with a LiveRangeEdit callback.
1358 MI.eraseFromBundle();
1359 }
1360 }
1361
1362 // Delete all spilled registers.
1363 for (Register Reg : RegsToSpill)
1364 Edit->eraseVirtReg(Reg);
1365}
1366
1367void InlineSpiller::spill(LiveRangeEdit &edit, AllocationOrder *order) {
1368 ++NumSpilledRanges;
1369 Edit = &edit;
1370 Order = order;
1371 assert(!edit.getReg().isStack() && "Trying to spill a stack slot.");
1372 // Share a stack slot among all descendants of Original.
1373 Original = VRM.getOriginal(edit.getReg());
1374 StackSlot = VRM.getStackSlot(Original);
1375 StackInt = nullptr;
1376
1377 LLVM_DEBUG(dbgs() << "Inline spilling "
1378 << TRI.getRegClassName(MRI.getRegClass(edit.getReg()))
1379 << ':' << edit.getParent() << "\nFrom original "
1380 << printReg(Original) << '\n');
1381 assert(edit.getParent().isSpillable() &&
1382 "Attempting to spill already spilled value.");
1383 assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
1384
1385 collectRegsToSpill();
1386 reMaterializeAll();
1387
1388 // Remat may handle everything.
1389 if (!RegsToSpill.empty())
1390 spillAll();
1391
1392 Edit->calculateRegClassAndHint(MF, VRAI);
1393}
1394
1395/// Optimizations after all the reg selections and spills are done.
1396void InlineSpiller::postOptimization() { HSpiller.hoistAllSpills(); }
1397
1398/// When a spill is inserted, add the spill to MergeableSpills map.
1399void HoistSpillHelper::addToMergeableSpills(MachineInstr &Spill, int StackSlot,
1400 Register Original) {
1402 LiveInterval &OrigLI = LIS.getInterval(Original);
1403 // save a copy of LiveInterval in StackSlotToOrigLI because the original
1404 // LiveInterval may be cleared after all its references are spilled.
1405
1406 auto [Place, Inserted] = StackSlotToOrigLI.try_emplace(StackSlot);
1407 if (Inserted) {
1408 auto LI = std::make_unique<LiveInterval>(OrigLI.reg(), OrigLI.weight());
1409 LI->assign(OrigLI, Allocator);
1410 Place->second = std::move(LI);
1411 }
1412
1413 SlotIndex Idx = LIS.getInstructionIndex(Spill);
1414 VNInfo *OrigVNI = Place->second->getVNInfoAt(Idx.getRegSlot());
1415 std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1416 MergeableSpills[MIdx].insert(&Spill);
1417}
1418
1419/// When a spill is removed, remove the spill from MergeableSpills map.
1420/// Return true if the spill is removed successfully.
1421bool HoistSpillHelper::rmFromMergeableSpills(MachineInstr &Spill,
1422 int StackSlot) {
1423 auto It = StackSlotToOrigLI.find(StackSlot);
1424 if (It == StackSlotToOrigLI.end())
1425 return false;
1426 SlotIndex Idx = LIS.getInstructionIndex(Spill);
1427 VNInfo *OrigVNI = It->second->getVNInfoAt(Idx.getRegSlot());
1428 std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1429 return MergeableSpills[MIdx].erase(&Spill);
1430}
1431
1432/// Check BB to see if it is a possible target BB to place a hoisted spill,
1433/// i.e., there should be a living sibling of OrigReg at the insert point.
1434bool HoistSpillHelper::isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI,
1435 MachineBasicBlock &BB, Register &LiveReg) {
1436 SlotIndex Idx = IPA.getLastInsertPoint(OrigLI, BB);
1437 // The original def could be after the last insert point in the root block,
1438 // we can't hoist to here.
1439 if (Idx < OrigVNI.def) {
1440 // TODO: We could be better here. If LI is not alive in landing pad
1441 // we could hoist spill after LIP.
1442 LLVM_DEBUG(dbgs() << "can't spill in root block - def after LIP\n");
1443 return false;
1444 }
1445 Register OrigReg = OrigLI.reg();
1446 SmallSetVector<Register, 16> &Siblings = Virt2SiblingsMap[OrigReg];
1447 assert(OrigLI.getVNInfoAt(Idx) == &OrigVNI && "Unexpected VNI");
1448
1449 for (const Register &SibReg : Siblings) {
1450 LiveInterval &LI = LIS.getInterval(SibReg);
1451 if (!LI.getVNInfoAt(Idx))
1452 continue;
1453 // All of the sub-ranges should be alive at the prospective slot index.
1454 // Otherwise, we might risk storing unrelated / compromised values from some
1455 // sub-registers to the spill slot.
1456 if (all_of(LI.subranges(), [&](const LiveInterval::SubRange &SR) {
1457 return SR.getVNInfoAt(Idx) != nullptr;
1458 })) {
1459 LiveReg = SibReg;
1460 return true;
1461 }
1462 }
1463 return false;
1464}
1465
1466/// Remove redundant spills in the same BB. Save those redundant spills in
1467/// SpillsToRm, and save the spill to keep and its BB in SpillBBToSpill map.
1468void HoistSpillHelper::rmRedundantSpills(
1472 // For each spill saw, check SpillBBToSpill[] and see if its BB already has
1473 // another spill inside. If a BB contains more than one spill, only keep the
1474 // earlier spill with smaller SlotIndex.
1475 for (auto *const CurrentSpill : Spills) {
1476 MachineBasicBlock *Block = CurrentSpill->getParent();
1477 MachineDomTreeNode *Node = MDT.getNode(Block);
1478 MachineInstr *PrevSpill = SpillBBToSpill[Node];
1479 if (PrevSpill) {
1480 SlotIndex PIdx = LIS.getInstructionIndex(*PrevSpill);
1481 SlotIndex CIdx = LIS.getInstructionIndex(*CurrentSpill);
1482 MachineInstr *SpillToRm = (CIdx > PIdx) ? CurrentSpill : PrevSpill;
1483 MachineInstr *SpillToKeep = (CIdx > PIdx) ? PrevSpill : CurrentSpill;
1484 SpillsToRm.push_back(SpillToRm);
1485 SpillBBToSpill[MDT.getNode(Block)] = SpillToKeep;
1486 } else {
1487 SpillBBToSpill[MDT.getNode(Block)] = CurrentSpill;
1488 }
1489 }
1490 for (auto *const SpillToRm : SpillsToRm)
1491 Spills.erase(SpillToRm);
1492}
1493
1494/// Starting from \p Root find a top-down traversal order of the dominator
1495/// tree to visit all basic blocks containing the elements of \p Spills.
1496/// Redundant spills will be found and put into \p SpillsToRm at the same
1497/// time. \p SpillBBToSpill will be populated as part of the process and
1498/// maps a basic block to the first store occurring in the basic block.
1499/// \post SpillsToRm.union(Spills\@post) == Spills\@pre
1500void HoistSpillHelper::getVisitOrders(
1506 // The set contains all the possible BB nodes to which we may hoist
1507 // original spills.
1509 // Save the BB nodes on the path from the first BB node containing
1510 // non-redundant spill to the Root node.
1512 // All the spills to be hoisted must originate from a single def instruction
1513 // to the OrigReg. It means the def instruction should dominate all the spills
1514 // to be hoisted. We choose the BB where the def instruction is located as
1515 // the Root.
1516 MachineDomTreeNode *RootIDomNode = MDT[Root]->getIDom();
1517 // For every node on the dominator tree with spill, walk up on the dominator
1518 // tree towards the Root node until it is reached. If there is other node
1519 // containing spill in the middle of the path, the previous spill saw will
1520 // be redundant and the node containing it will be removed. All the nodes on
1521 // the path starting from the first node with non-redundant spill to the Root
1522 // node will be added to the WorkSet, which will contain all the possible
1523 // locations where spills may be hoisted to after the loop below is done.
1524 for (auto *const Spill : Spills) {
1525 MachineBasicBlock *Block = Spill->getParent();
1527 MachineInstr *SpillToRm = nullptr;
1528 while (Node != RootIDomNode) {
1529 // If Node dominates Block, and it already contains a spill, the spill in
1530 // Block will be redundant.
1531 if (Node != MDT[Block] && SpillBBToSpill[Node]) {
1532 SpillToRm = SpillBBToSpill[MDT[Block]];
1533 break;
1534 /// If we see the Node already in WorkSet, the path from the Node to
1535 /// the Root node must already be traversed by another spill.
1536 /// Then no need to repeat.
1537 } else if (WorkSet.count(Node)) {
1538 break;
1539 } else {
1540 NodesOnPath.insert(Node);
1541 }
1542 Node = Node->getIDom();
1543 }
1544 if (SpillToRm) {
1545 SpillsToRm.push_back(SpillToRm);
1546 } else {
1547 // Add a BB containing the original spills to SpillsToKeep -- i.e.,
1548 // set the initial status before hoisting start. The value of BBs
1549 // containing original spills is set to 0, in order to descriminate
1550 // with BBs containing hoisted spills which will be inserted to
1551 // SpillsToKeep later during hoisting.
1552 SpillsToKeep[MDT[Block]] = Register();
1553 WorkSet.insert_range(NodesOnPath);
1554 }
1555 NodesOnPath.clear();
1556 }
1557
1558 // Sort the nodes in WorkSet in top-down order and save the nodes
1559 // in Orders. Orders will be used for hoisting in runHoistSpills.
1560 unsigned idx = 0;
1561 Orders.push_back(MDT.getNode(Root));
1562 do {
1563 MachineDomTreeNode *Node = Orders[idx++];
1564 for (MachineDomTreeNode *Child : Node->children()) {
1565 if (WorkSet.count(Child))
1566 Orders.push_back(Child);
1567 }
1568 } while (idx != Orders.size());
1569 assert(Orders.size() == WorkSet.size() &&
1570 "Orders have different size with WorkSet");
1571
1572#ifndef NDEBUG
1573 LLVM_DEBUG(dbgs() << "Orders size is " << Orders.size() << "\n");
1575 for (; RIt != Orders.rend(); RIt++)
1576 LLVM_DEBUG(dbgs() << "BB" << (*RIt)->getBlock()->getNumber() << ",");
1577 LLVM_DEBUG(dbgs() << "\n");
1578#endif
1579}
1580
1581/// Try to hoist spills according to BB hotness. The spills to removed will
1582/// be saved in \p SpillsToRm. The spills to be inserted will be saved in
1583/// \p SpillsToIns.
1584void HoistSpillHelper::runHoistSpills(
1585 LiveInterval &OrigLI, VNInfo &OrigVNI,
1589 // Visit order of dominator tree nodes.
1591 // SpillsToKeep contains all the nodes where spills are to be inserted
1592 // during hoisting. If the spill to be inserted is an original spill
1593 // (not a hoisted one), the value of the map entry is 0. If the spill
1594 // is a hoisted spill, the value of the map entry is the VReg to be used
1595 // as the source of the spill.
1597 // Map from BB to the first spill inside of it.
1599
1600 rmRedundantSpills(Spills, SpillsToRm, SpillBBToSpill);
1601
1602 MachineBasicBlock *Root = LIS.getMBBFromIndex(OrigVNI.def);
1603 getVisitOrders(Root, Spills, Orders, SpillsToRm, SpillsToKeep,
1604 SpillBBToSpill);
1605
1606 // SpillsInSubTreeMap keeps the map from a dom tree node to a pair of
1607 // nodes set and the cost of all the spills inside those nodes.
1608 // The nodes set are the locations where spills are to be inserted
1609 // in the subtree of current node.
1610 using NodesCostPair =
1611 std::pair<SmallPtrSet<MachineDomTreeNode *, 16>, BlockFrequency>;
1613
1614 // Iterate Orders set in reverse order, which will be a bottom-up order
1615 // in the dominator tree. Once we visit a dom tree node, we know its
1616 // children have already been visited and the spill locations in the
1617 // subtrees of all the children have been determined.
1619 for (; RIt != Orders.rend(); RIt++) {
1620 MachineBasicBlock *Block = (*RIt)->getBlock();
1621
1622 // If Block contains an original spill, simply continue.
1623 if (auto It = SpillsToKeep.find(*RIt);
1624 It != SpillsToKeep.end() && !It->second) {
1625 auto &SIt = SpillsInSubTreeMap[*RIt];
1626 SIt.first.insert(*RIt);
1627 // Sit.second contains the cost of spill.
1628 SIt.second = MBFI.getBlockFreq(Block);
1629 continue;
1630 }
1631
1632 // Collect spills in subtree of current node (*RIt) to
1633 // SpillsInSubTreeMap[*RIt].first.
1634 for (MachineDomTreeNode *Child : (*RIt)->children()) {
1635 if (!SpillsInSubTreeMap.contains(Child))
1636 continue;
1637 // The stmt:
1638 // "auto &[SpillsInSubTree, SubTreeCost] = SpillsInSubTreeMap[*RIt]"
1639 // below should be placed before getting the begin and end iterators of
1640 // SpillsInSubTreeMap[Child].first, or else the iterators may be
1641 // invalidated when SpillsInSubTreeMap[*RIt] is seen the first time
1642 // and the map grows and then the original buckets in the map are moved.
1643 auto &[SpillsInSubTree, SubTreeCost] = SpillsInSubTreeMap[*RIt];
1644 auto ChildIt = SpillsInSubTreeMap.find(Child);
1645 SubTreeCost += ChildIt->second.second;
1646 auto BI = ChildIt->second.first.begin();
1647 auto EI = ChildIt->second.first.end();
1648 SpillsInSubTree.insert(BI, EI);
1649 SpillsInSubTreeMap.erase(ChildIt);
1650 }
1651
1652 auto &[SpillsInSubTree, SubTreeCost] = SpillsInSubTreeMap[*RIt];
1653 // No spills in subtree, simply continue.
1654 if (SpillsInSubTree.empty())
1655 continue;
1656
1657 // Check whether Block is a possible candidate to insert spill.
1658 Register LiveReg;
1659 if (!isSpillCandBB(OrigLI, OrigVNI, *Block, LiveReg))
1660 continue;
1661
1662 // If there are multiple spills that could be merged, bias a little
1663 // to hoist the spill.
1664 BranchProbability MarginProb = (SpillsInSubTree.size() > 1)
1665 ? BranchProbability(9, 10)
1666 : BranchProbability(1, 1);
1667 if (SubTreeCost > MBFI.getBlockFreq(Block) * MarginProb) {
1668 // Hoist: Move spills to current Block.
1669 for (auto *const SpillBB : SpillsInSubTree) {
1670 // When SpillBB is a BB contains original spill, insert the spill
1671 // to SpillsToRm.
1672 if (auto It = SpillsToKeep.find(SpillBB);
1673 It != SpillsToKeep.end() && !It->second) {
1674 MachineInstr *SpillToRm = SpillBBToSpill[SpillBB];
1675 SpillsToRm.push_back(SpillToRm);
1676 }
1677 // SpillBB will not contain spill anymore, remove it from SpillsToKeep.
1678 SpillsToKeep.erase(SpillBB);
1679 }
1680 // Current Block is the BB containing the new hoisted spill. Add it to
1681 // SpillsToKeep. LiveReg is the source of the new spill.
1682 SpillsToKeep[*RIt] = LiveReg;
1683 LLVM_DEBUG({
1684 dbgs() << "spills in BB: ";
1685 for (const auto Rspill : SpillsInSubTree)
1686 dbgs() << Rspill->getBlock()->getNumber() << " ";
1687 dbgs() << "were promoted to BB" << (*RIt)->getBlock()->getNumber()
1688 << "\n";
1689 });
1690 SpillsInSubTree.clear();
1691 SpillsInSubTree.insert(*RIt);
1692 SubTreeCost = MBFI.getBlockFreq(Block);
1693 }
1694 }
1695 // For spills in SpillsToKeep with LiveReg set (i.e., not original spill),
1696 // save them to SpillsToIns.
1697 for (const auto &Ent : SpillsToKeep) {
1698 if (Ent.second)
1699 SpillsToIns[Ent.first->getBlock()] = Ent.second;
1700 }
1701}
1702
1703/// For spills with equal values, remove redundant spills and hoist those left
1704/// to less hot spots.
1705///
1706/// Spills with equal values will be collected into the same set in
1707/// MergeableSpills when spill is inserted. These equal spills are originated
1708/// from the same defining instruction and are dominated by the instruction.
1709/// Before hoisting all the equal spills, redundant spills inside in the same
1710/// BB are first marked to be deleted. Then starting from the spills left, walk
1711/// up on the dominator tree towards the Root node where the define instruction
1712/// is located, mark the dominated spills to be deleted along the way and
1713/// collect the BB nodes on the path from non-dominated spills to the define
1714/// instruction into a WorkSet. The nodes in WorkSet are the candidate places
1715/// where we are considering to hoist the spills. We iterate the WorkSet in
1716/// bottom-up order, and for each node, we will decide whether to hoist spills
1717/// inside its subtree to that node. In this way, we can get benefit locally
1718/// even if hoisting all the equal spills to one cold place is impossible.
1719void HoistSpillHelper::hoistAllSpills() {
1720 SmallVector<Register, 4> NewVRegs;
1721 LiveRangeEdit Edit(nullptr, NewVRegs, MF, LIS, &VRM, this);
1722
1723 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
1725 Register Original = VRM.getPreSplitReg(Reg);
1726 if (!MRI.def_empty(Reg) && Original.isValid())
1727 Virt2SiblingsMap[Original].insert(Reg);
1728 }
1729
1730 // Each entry in MergeableSpills contains a spill set with equal values.
1731 for (auto &Ent : MergeableSpills) {
1732 int Slot = Ent.first.first;
1733 LiveInterval &OrigLI = *StackSlotToOrigLI[Slot];
1734 VNInfo *OrigVNI = Ent.first.second;
1735 SmallPtrSet<MachineInstr *, 16> &EqValSpills = Ent.second;
1736 if (Ent.second.empty())
1737 continue;
1738
1739 LLVM_DEBUG({
1740 dbgs() << "\nFor Slot" << Slot << " and VN" << OrigVNI->id << ":\n"
1741 << "Equal spills in BB: ";
1742 for (const auto spill : EqValSpills)
1743 dbgs() << spill->getParent()->getNumber() << " ";
1744 dbgs() << "\n";
1745 });
1746
1747 // SpillsToRm is the spill set to be removed from EqValSpills.
1749 // SpillsToIns is the spill set to be newly inserted after hoisting.
1751
1752 runHoistSpills(OrigLI, *OrigVNI, EqValSpills, SpillsToRm, SpillsToIns);
1753
1754 LLVM_DEBUG({
1755 dbgs() << "Finally inserted spills in BB: ";
1756 for (const auto &Ispill : SpillsToIns)
1757 dbgs() << Ispill.first->getNumber() << " ";
1758 dbgs() << "\nFinally removed spills in BB: ";
1759 for (const auto Rspill : SpillsToRm)
1760 dbgs() << Rspill->getParent()->getNumber() << " ";
1761 dbgs() << "\n";
1762 });
1763
1764 // Stack live range update.
1765 LiveInterval &StackIntvl = LSS.getInterval(Slot);
1766 if (!SpillsToIns.empty() || !SpillsToRm.empty())
1767 StackIntvl.MergeValueInAsValue(OrigLI, OrigVNI,
1768 StackIntvl.getValNumInfo(0));
1769
1770 // Insert hoisted spills.
1771 for (auto const &Insert : SpillsToIns) {
1772 MachineBasicBlock *BB = Insert.first;
1773 Register LiveReg = Insert.second;
1774 MachineBasicBlock::iterator MII = IPA.getLastInsertPointIter(OrigLI, *BB);
1775 MachineInstrSpan MIS(MII, BB);
1776 TII.storeRegToStackSlot(*BB, MII, LiveReg, false, Slot,
1777 MRI.getRegClass(LiveReg), Register());
1778 LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MII);
1779 for (const MachineInstr &MI : make_range(MIS.begin(), MII))
1780 getVDefInterval(MI, LIS);
1781 ++NumSpills;
1782 }
1783
1784 // Remove redundant spills or change them to dead instructions.
1785 NumSpills -= SpillsToRm.size();
1786 for (auto *const RMEnt : SpillsToRm) {
1787 RMEnt->setDesc(TII.get(TargetOpcode::KILL));
1788 for (unsigned i = RMEnt->getNumOperands(); i; --i) {
1789 MachineOperand &MO = RMEnt->getOperand(i - 1);
1790 if (MO.isReg() && MO.isImplicit() && MO.isDef() && !MO.isDead())
1791 RMEnt->removeOperand(i - 1);
1792 }
1793 }
1794 Edit.eliminateDeadDefs(SpillsToRm, {});
1795 }
1796}
1797
1798/// Called before a virtual register is erased from LiveIntervals.
1799/// Forcibly remove the register from LiveRegMatrix before it's deleted,
1800/// preventing dangling pointers.
1801bool HoistSpillHelper::LRE_CanEraseVirtReg(Register VirtReg) {
1802 if (Matrix && VRM.hasPhys(VirtReg)) {
1803 const LiveInterval &LI = LIS.getInterval(VirtReg);
1804 Matrix->unassign(LI, /*ClearAllReferencingSegments=*/true);
1805 }
1806 return true; // Allow deletion to proceed
1807}
1808
1809/// For VirtReg clone, the \p New register should have the same physreg or
1810/// stackslot as the \p old register.
1811void HoistSpillHelper::LRE_DidCloneVirtReg(Register New, Register Old) {
1812 if (VRM.hasPhys(Old))
1813 VRM.assignVirt2Phys(New, VRM.getPhys(Old));
1814 else if (VRM.getStackSlot(Old) != VirtRegMap::NO_STACK_SLOT)
1815 VRM.assignVirt2StackSlot(New, VRM.getStackSlot(Old));
1816 else
1817 llvm_unreachable("VReg should be assigned either physreg or stackslot");
1818 if (VRM.hasShape(Old))
1819 VRM.assignVirt2Shape(New, VRM.getShape(Old));
1820}
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
This file defines the DenseMap class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
static LLVM_DUMP_METHOD void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B, MachineBasicBlock::iterator E, LiveIntervals const &LIS, const char *const header, Register VReg=Register())
static Register isCopyOfBundle(const MachineInstr &FirstMI, Register Reg, const TargetInstrInfo &TII)
Check for a copy bundle as formed by SplitKit.
static bool isRealSpill(const MachineInstr &Def)
Check if Def fully defines a VReg with an undefined value.
static cl::opt< bool > RestrictStatepointRemat("restrict-statepoint-remat", cl::init(false), cl::Hidden, cl::desc("Restrict remat for statepoint operands"))
static Register isCopyOf(const MachineInstr &MI, Register Reg, const TargetInstrInfo &TII)
isFullCopyOf - If MI is a COPY to or from Reg, return the other register, otherwise return 0.
static void getVDefInterval(const MachineInstr &MI, LiveIntervals &LIS)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Live Register Matrix
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
This file implements a map that provides insertion order iteration.
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define P(N)
Basic Register Allocator
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
bool erase(const KeyT &Val)
Definition DenseMap.h:330
iterator end()
Definition DenseMap.h:81
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:169
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
Register getReg() const
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Store the specified register of the given register class to the specified stack frame index.
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
TargetInstrInfo overrides.
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Load the specified register of the given register class from the specified stack frame index.
Register isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex) const override
If the specified machine instruction is a direct store to a stack slot, return the virtual or physica...
Determines the latest safe point in a block in which we can insert a split, spill or other instructio...
Definition SplitKit.h:50
A live range for subregisters.
LiveInterval - This class represents the liveness of a register, or stack slot.
float weight() const
Register reg() const
bool isSpillable() const
isSpillable - Can this interval be spilled?
bool hasSubRanges() const
Returns true if subregister liveness information is available.
iterator_range< subrange_iterator > subranges()
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
SlotIndexes * getSlotIndexes() const
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
VNInfo::Allocator & getVNInfoAllocator()
LiveInterval & getInterval(Register Reg)
void InsertMachineInstrRangeInMaps(MachineBasicBlock::iterator B, MachineBasicBlock::iterator E)
LLVM_ABI bool shrinkToUses(LiveInterval *li, SmallVectorImpl< MachineInstr * > *dead=nullptr)
After removing some uses of a register, shrink its live range to just the remaining uses.
LLVM_ABI void removePhysRegDefAt(MCRegister Reg, SlotIndex Pos)
Remove value numbers and related live segments starting at position Pos that are part of any liverang...
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
Result of a LiveRange query.
bool isKill() const
Return true if the live-in value is killed by this instruction.
Callback methods for LiveRangeEdit owners.
const LiveInterval & getParent() const
Register getReg() const
VNInfo * getValNumInfo(unsigned ValNo)
getValNumInfo - Returns pointer to the specified val#.
LLVM_ABI iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
LLVM_ABI void MergeValueInAsValue(const LiveRange &RHS, const VNInfo *RHSValNo, VNInfo *LHSValNo)
MergeValueInAsValue - Merge all of the segments of a specific val# in RHS into this live range as the...
iterator_range< vni_iterator > vnis()
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
VNInfo * getVNInfoBefore(SlotIndex Idx) const
getVNInfoBefore - Return the VNInfo that is live up to but not necessarily including Idx,...
LLVM_ABI std::pair< VNInfo *, bool > extendInBlock(ArrayRef< SlotIndex > Undefs, SlotIndex StartIdx, SlotIndex Kill)
Attempt to extend a value defined after StartIdx to include Use.
unsigned getNumValNums() const
VNInfo * getNextValue(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator)
getNextValue - Create a new value number and return it.
void assign(const LiveRange &Other, BumpPtrAllocator &Allocator)
Copies values numbers and live segments from Other into this range.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
MIBundleOperands - Iterate over all operands in a bundle of machine instructions.
LLVM_ABI iterator SkipPHIsLabelsAndDebug(iterator I, Register Reg=Register(), bool SkipPseudoOp=true)
Return the first instruction in MBB after I that is not a PHI, label or debug.
Instructions::iterator instr_iterator
Instructions::const_iterator const_instr_iterator
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
static const unsigned int DebugOperandMemNumber
A reserved operand number representing the instructions memory operand, for instructions that have a ...
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
MachineInstrSpan provides an interface to get an iteration range containing the instruction it was in...
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
unsigned getNumOperands() const
Retuns the total number of operands.
bool isBundledWithPred() const
Return true if this instruction is part of a bundle, and it is not the first instruction in the bundl...
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
bool isBundledWithSucc() const
Return true if this instruction is part of a bundle, and it is not the last instruction in the bundle...
LLVM_ABI unsigned getDebugInstrNum()
Fetch the instruction number of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
bool isBundled() const
Return true if this instruction part of a bundle.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
void setIsUndef(bool Val=true)
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
defusechain_instr_iterator< true, true, true, false > reg_bundle_nodbg_iterator
reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk all defs and uses of the...
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isStack() const
Return true if this is a stack slot.
Definition Register.h:46
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
constexpr bool isValid() const
Definition Register.h:112
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
LLVM_ABI void removeSingleMachineInstrFromMaps(MachineInstr &MI)
Removes a single machine instruction MI from the mapping.
size_type size() const
Definition SmallPtrSet.h:99
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
std::reverse_iterator< iterator > reverse_iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Spiller interface.
Definition Spiller.h:33
virtual ~Spiller()=0
Register getReg() const
MI-level Statepoint operands.
Definition StackMaps.h:159
LLVM_ABI bool isFoldableReg(Register Reg) const
Return true if Reg is used only in operands which can be folded to stack usage.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
VNInfo - Value Number Information.
bool isUnused() const
Returns true if this value is unused.
unsigned id
The ID number of this value.
SlotIndex def
The index of the defining instruction.
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
Calculate auxiliary information for a virtual register such as its spill weight and allocation hint.
static constexpr int NO_STACK_SLOT
Definition VirtRegMap.h:66
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr RegState getKillRegState(bool B)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg, const TargetRegisterInfo *TRI)
AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses a physical register.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI VirtRegInfo AnalyzeVirtRegInBundle(MachineInstr &MI, Register Reg, SmallVectorImpl< std::pair< MachineInstr *, unsigned > > *Ops=nullptr)
AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses a virtual register.
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
Spiller * createInlineSpiller(const Spiller::RequiredAnalyses &Analyses, MachineFunction &MF, VirtRegMap &VRM, VirtRegAuxInfo &VRAI, LiveRegMatrix *Matrix=nullptr)
Create and return a spiller that will insert spill code directly instead of deferring though VirtRegM...
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI MachineInstr * buildDbgValueForSpill(MachineBasicBlock &BB, MachineBasicBlock::iterator I, const MachineInstr &Orig, int FrameIndex, Register SpillReg)
Clone a DBG_VALUE whose value has been spilled to FrameIndex.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
static constexpr LaneBitmask getAll()
Definition LaneBitmask.h:82
static constexpr LaneBitmask getNone()
Definition LaneBitmask.h:81
Remat - Information needed to rematerialize at a specific location.
This represents a simple continuous liveness interval for a value.
Information about how a physical register Reg is used by a set of operands.
bool FullyDefined
Reg or a super-register is defined.
VirtRegInfo - Information about a virtual register used by a set of operands.
bool Reads
Reads - One of the operands read the virtual register.
bool Tied
Tied - Uses and defs must use the same register.
bool Writes
Writes - One of the operands writes the virtual register.