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 // Try to find the rematerializable definition by tracing through COPY
692 // chains.
693 LiveInterval &LI = LIS.getInterval(VirtReg.reg());
694 VNInfo *CurVNI = LI.getVNInfoAt(UseIdx);
695 MachineInstr *CurDef = nullptr;
696
697 LLVM_DEBUG(dbgs() << "\ttracing COPY chain from "
698 << printReg(VirtReg.reg(), &TRI) << "\n");
699
700 // Trace backwards through COPY chain using VNInfo
701 while (CurVNI) {
702 CurDef = LIS.getInstructionFromIndex(CurVNI->def);
703
704 LLVM_DEBUG(dbgs() << "\t -> def at " << CurVNI->def << ": "
705 << (CurDef ? TII.getName(CurDef->getOpcode()) : "null")
706 << "\n");
707
708 if (!CurDef || !CurDef->isFullCopy())
709 break;
710
711 Register SrcReg = CurDef->getOperand(1).getReg();
712 if (!SrcReg.isVirtual())
713 break;
714 LLVM_DEBUG(dbgs() << "\t -> tracing through COPY to "
715 << printReg(SrcReg, &TRI) << "\n");
716 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
717 CurVNI = SrcLI.getVNInfoBefore(CurVNI->def);
718 }
719 if (CurDef && TII.isReMaterializable(*CurDef)) {
720 DefMI = CurDef;
721 LLVM_DEBUG(dbgs() << "\tFound remat possibility through COPY chain: "
722 << *DefMI);
723 }
724 if (!DefMI) {
725 markValueUsed(&VirtReg, ParentVNI);
726 LLVM_DEBUG(dbgs() << "\tcannot remat missing def for " << UseIdx << '\t'
727 << MI);
728 return false;
729 }
730 }
731
732 LiveRangeEdit::Remat RM(ParentVNI);
733 RM.OrigMI = DefMI;
734 if (!Edit->canRematerializeAt(RM, UseIdx)) {
735 markValueUsed(&VirtReg, ParentVNI);
736 LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
737 return false;
738 }
739
740 // If the instruction also writes VirtReg.reg, it had better not require the
741 // same register for uses and defs.
742 if (RI.Tied) {
743 markValueUsed(&VirtReg, ParentVNI);
744 LLVM_DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << MI);
745 return false;
746 }
747
748 // Before rematerializing into a register for a single instruction, try to
749 // fold a load into the instruction. That avoids allocating a new register.
750 if (RM.OrigMI->canFoldAsLoad() &&
751 (RM.OrigMI->mayLoad() || !hasPhysRegAvailable(MI)) &&
752 foldMemoryOperand(Ops, RM.OrigMI)) {
753 Edit->markRematerialized(RM.ParentVNI);
754 ++NumFoldedLoads;
755 return true;
756 }
757
758 // If we can't guarantee that we'll be able to actually assign the new vreg,
759 // we can't remat.
760 if (!canGuaranteeAssignmentAfterRemat(VirtReg.reg(), MI)) {
761 markValueUsed(&VirtReg, ParentVNI);
762 LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
763 return false;
764 }
765
766 // Allocate a new register for the remat.
767 Register NewVReg = Edit->createFrom(Original);
768
769 // Constrain it to the register class of MI.
770 MRI.constrainRegClass(NewVReg, MRI.getRegClass(VirtReg.reg()));
771
772 // Compute which lanes of the virtual register are live at the use point.
773 LaneBitmask UsedLanes = LaneBitmask::getAll();
774 if (VirtReg.hasSubRanges()) {
775 UsedLanes = LaneBitmask::getNone();
776 for (const LiveInterval::SubRange &SR : VirtReg.subranges())
777 if (SR.liveAt(UseIdx))
778 UsedLanes |= SR.LaneMask;
779 }
780
781 // Finally we can rematerialize OrigMI before MI.
782 SlotIndex DefIdx = Edit->rematerializeAt(*MI.getParent(), MI, NewVReg, RM,
783 TRI, false, 0, nullptr, UsedLanes);
784
785 // We take the DebugLoc from MI, since OrigMI may be attributed to a
786 // different source location.
787 auto *NewMI = LIS.getInstructionFromIndex(DefIdx);
788 NewMI->setDebugLoc(MI.getDebugLoc());
789
790 (void)DefIdx;
791 LLVM_DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
792 << *LIS.getInstructionFromIndex(DefIdx));
793
794 // Replace operands
795 for (const auto &OpPair : Ops) {
796 MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
797 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg()) {
798 MO.setReg(NewVReg);
799 MO.setIsKill();
800 }
801 }
802 LLVM_DEBUG(dbgs() << "\t " << UseIdx << '\t' << MI << '\n');
803
804 ++NumRemats;
805 return true;
806}
807
808/// reMaterializeAll - Try to rematerialize as many uses as possible,
809/// and trim the live ranges after.
810void InlineSpiller::reMaterializeAll() {
811 UsedValues.clear();
812
813 // Try to remat before all uses of snippets.
814 bool anyRemat = false;
815 for (Register Reg : RegsToSpill) {
816 LiveInterval &LI = LIS.getInterval(Reg);
817 for (MachineInstr &MI : llvm::make_early_inc_range(MRI.reg_bundles(Reg))) {
818 // Debug values are not allowed to affect codegen.
819 if (MI.isDebugValue())
820 continue;
821
822 assert(!MI.isDebugInstr() && "Did not expect to find a use in debug "
823 "instruction that isn't a DBG_VALUE");
824
825 anyRemat |= reMaterializeFor(LI, MI);
826 }
827 }
828 if (!anyRemat)
829 return;
830
831 // Remove any values that were completely rematted.
832 for (Register Reg : RegsToSpill) {
833 LiveInterval &LI = LIS.getInterval(Reg);
834 for (VNInfo *VNI : LI.vnis()) {
835 if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
836 continue;
837 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
838 MI->addRegisterDead(Reg, &TRI);
839 if (!MI->allDefsAreDead())
840 continue;
841 LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
842 DeadDefs.push_back(MI);
843 // If MI is a bundle header, also try removing copies inside the bundle,
844 // otherwise the verifier would complain "live range continues after dead
845 // def flag".
846 if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) {
847 MachineBasicBlock::instr_iterator BeginIt = MI->getIterator(),
848 EndIt = MI->getParent()->instr_end();
849 ++BeginIt; // Skip MI that was already handled.
850
851 bool OnlyDeadCopies = true;
852 for (MachineBasicBlock::instr_iterator It = BeginIt;
853 It != EndIt && It->isBundledWithPred(); ++It) {
854
855 auto DestSrc = TII.isCopyInstr(*It);
856 bool IsCopyToDeadReg =
857 DestSrc && DestSrc->Destination->getReg() == Reg;
858 if (!IsCopyToDeadReg) {
859 OnlyDeadCopies = false;
860 break;
861 }
862 }
863 if (OnlyDeadCopies) {
864 for (MachineBasicBlock::instr_iterator It = BeginIt;
865 It != EndIt && It->isBundledWithPred(); ++It) {
866 It->addRegisterDead(Reg, &TRI);
867 LLVM_DEBUG(dbgs() << "All defs dead: " << *It);
868 DeadDefs.push_back(&*It);
869 }
870 }
871 }
872 }
873 }
874
875 // Eliminate dead code after remat. Note that some snippet copies may be
876 // deleted here.
877 if (DeadDefs.empty())
878 return;
879 LLVM_DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
880 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
881
882 // LiveRangeEdit::eliminateDeadDef is used to remove dead define instructions
883 // after rematerialization. To remove a VNI for a vreg from its LiveInterval,
884 // LiveIntervals::removeVRegDefAt is used. However, after non-PHI VNIs are all
885 // removed, PHI VNI are still left in the LiveInterval.
886 // So to get rid of unused reg, we need to check whether it has non-dbg
887 // reference instead of whether it has non-empty interval.
888 unsigned ResultPos = 0;
889 for (Register Reg : RegsToSpill) {
890 if (MRI.reg_nodbg_empty(Reg)) {
891 Edit->eraseVirtReg(Reg);
892 RegsReplaced.push_back(Reg);
893 continue;
894 }
895
896 assert(LIS.hasInterval(Reg) &&
897 (!LIS.getInterval(Reg).empty() || !MRI.reg_nodbg_empty(Reg)) &&
898 "Empty and not used live-range?!");
899
900 RegsToSpill[ResultPos++] = Reg;
901 }
902 RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
903 LLVM_DEBUG(dbgs() << RegsToSpill.size()
904 << " registers to spill after remat.\n");
905}
906
907//===----------------------------------------------------------------------===//
908// Spilling
909//===----------------------------------------------------------------------===//
910
911/// If MI is a load or store of StackSlot, it can be removed.
912bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, Register Reg) {
913 int FI = 0;
914 Register InstrReg = TII.isLoadFromStackSlot(*MI, FI);
915 bool IsLoad = InstrReg.isValid();
916 if (!IsLoad)
917 InstrReg = TII.isStoreToStackSlot(*MI, FI);
918
919 // We have a stack access. Is it the right register and slot?
920 if (InstrReg != Reg || FI != StackSlot)
921 return false;
922
923 if (!IsLoad)
924 HSpiller.rmFromMergeableSpills(*MI, StackSlot);
925
926 LLVM_DEBUG(dbgs() << "Coalescing stack access: " << *MI);
927 LIS.RemoveMachineInstrFromMaps(*MI);
928 MI->eraseFromParent();
929
930 if (IsLoad) {
931 ++NumReloadsRemoved;
932 --NumReloads;
933 } else {
934 ++NumSpillsRemoved;
935 --NumSpills;
936 }
937
938 return true;
939}
940
941#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
943// Dump the range of instructions from B to E with their slot indexes.
946 LiveIntervals const &LIS,
947 const char *const header,
948 Register VReg = Register()) {
949 char NextLine = '\n';
950 char SlotIndent = '\t';
951
952 if (std::next(B) == E) {
953 NextLine = ' ';
954 SlotIndent = ' ';
955 }
956
957 dbgs() << '\t' << header << ": " << NextLine;
958
959 for (MachineBasicBlock::iterator I = B; I != E; ++I) {
961
962 // If a register was passed in and this instruction has it as a
963 // destination that is marked as an early clobber, print the
964 // early-clobber slot index.
965 if (VReg) {
966 MachineOperand *MO = I->findRegisterDefOperand(VReg, /*TRI=*/nullptr);
967 if (MO && MO->isEarlyClobber())
968 Idx = Idx.getRegSlot(true);
969 }
970
971 dbgs() << SlotIndent << Idx << '\t' << *I;
972 }
973}
974#endif
975
976/// foldMemoryOperand - Try folding stack slot references in Ops into their
977/// instructions.
978///
979/// @param Ops Operand indices from AnalyzeVirtRegInBundle().
980/// @param LoadMI Load instruction to use instead of stack slot when non-null.
981/// @return True on success.
982bool InlineSpiller::
983foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
984 MachineInstr *LoadMI) {
985 if (Ops.empty())
986 return false;
987 // Don't attempt folding in bundles.
988 MachineInstr *MI = Ops.front().first;
989 if (Ops.back().first != MI || MI->isBundled())
990 return false;
991
992 bool WasCopy = TII.isCopyInstr(*MI).has_value();
993 Register ImpReg;
994
995 // TII::foldMemoryOperand will do what we need here for statepoint
996 // (fold load into use and remove corresponding def). We will replace
997 // uses of removed def with loads (spillAroundUses).
998 // For that to work we need to untie def and use to pass it through
999 // foldMemoryOperand and signal foldPatchpoint that it is allowed to
1000 // fold them.
1001 bool UntieRegs = MI->getOpcode() == TargetOpcode::STATEPOINT;
1002
1003 // Spill subregs if the target allows it.
1004 // We always want to spill subregs for stackmap/patchpoint pseudos.
1005 bool SpillSubRegs = TII.isSubregFoldable() ||
1006 MI->getOpcode() == TargetOpcode::STATEPOINT ||
1007 MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1008 MI->getOpcode() == TargetOpcode::STACKMAP;
1009
1010 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
1011 // operands.
1013 for (const auto &OpPair : Ops) {
1014 unsigned Idx = OpPair.second;
1015 assert(MI == OpPair.first && "Instruction conflict during operand folding");
1016 MachineOperand &MO = MI->getOperand(Idx);
1017
1018 // No point restoring an undef read, and we'll produce an invalid live
1019 // interval.
1020 // TODO: Is this really the correct way to handle undef tied uses?
1021 if (MO.isUse() && !MO.readsReg() && !MO.isTied())
1022 continue;
1023
1024 if (MO.isImplicit()) {
1025 ImpReg = MO.getReg();
1026 continue;
1027 }
1028
1029 if (!SpillSubRegs && MO.getSubReg())
1030 return false;
1031 // We cannot fold a load instruction into a def.
1032 if (LoadMI && MO.isDef())
1033 return false;
1034 // Tied use operands should not be passed to foldMemoryOperand.
1035 if (UntieRegs || !MI->isRegTiedToDefOperand(Idx))
1036 FoldOps.push_back(Idx);
1037 }
1038
1039 // If we only have implicit uses, we won't be able to fold that.
1040 // Moreover, TargetInstrInfo::foldMemoryOperand will assert if we try!
1041 if (FoldOps.empty())
1042 return false;
1043
1044 MachineInstrSpan MIS(MI, MI->getParent());
1045
1047 if (UntieRegs)
1048 for (unsigned Idx : FoldOps) {
1049 MachineOperand &MO = MI->getOperand(Idx);
1050 if (!MO.isTied())
1051 continue;
1052 unsigned Tied = MI->findTiedOperandIdx(Idx);
1053 if (MO.isUse())
1054 TiedOps.emplace_back(Tied, Idx);
1055 else {
1056 assert(MO.isDef() && "Tied to not use and def?");
1057 TiedOps.emplace_back(Idx, Tied);
1058 }
1059 MI->untieRegOperand(Idx);
1060 }
1061
1062 MachineInstr *CopyMI = nullptr;
1063 MachineInstr *FoldMI =
1064 LoadMI
1065 ? TII.foldMemoryOperand(*MI, FoldOps, *LoadMI, CopyMI, &LIS, &VRM)
1066 : TII.foldMemoryOperand(*MI, FoldOps, StackSlot, CopyMI, &LIS, &VRM);
1067 if (!FoldMI) {
1068 // Re-tie operands.
1069 for (auto Tied : TiedOps)
1070 MI->tieOperands(Tied.first, Tied.second);
1071 return false;
1072 }
1073
1074 // Remove LIS for any dead defs in the original MI not in FoldMI.
1075 for (MIBundleOperands MO(*MI); MO.isValid(); ++MO) {
1076 if (!MO->isReg())
1077 continue;
1078 Register Reg = MO->getReg();
1079 if (!Reg || Reg.isVirtual() || MRI.isReserved(Reg)) {
1080 continue;
1081 }
1082 // Skip non-Defs, including undef uses and internal reads.
1083 if (MO->isUse())
1084 continue;
1085 PhysRegInfo RI = AnalyzePhysRegInBundle(*FoldMI, Reg, &TRI);
1086 if (RI.FullyDefined)
1087 continue;
1088 // FoldMI does not define this physreg. Remove the LI segment.
1089 assert(MO->isDead() && "Cannot fold physreg def");
1090 SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
1091 LIS.removePhysRegDefAt(Reg.asMCReg(), Idx);
1092 }
1093
1094 int FI;
1095 if (TII.isStoreToStackSlot(*MI, FI) &&
1096 HSpiller.rmFromMergeableSpills(*MI, FI))
1097 --NumSpills;
1098 SlotIndex FoldIdx = LIS.ReplaceMachineInstrInMaps(*MI, *FoldMI);
1099 if (CopyMI) {
1100 SlotIndex CopyIdx = LIS.InsertMachineInstrInMaps(*CopyMI).getRegSlot();
1101 if (!MRI.isSSA()) {
1102 LiveInterval &LI = LIS.getInterval(CopyMI->getOperand(0).getReg());
1103 VNInfo *VNI = LI.getNextValue(CopyIdx, LIS.getVNInfoAllocator());
1104 LI.addSegment(LiveRange::Segment(CopyIdx, FoldIdx.getRegSlot(), VNI));
1105 }
1106 }
1107 // Update the call info.
1108 if (MI->isCandidateForAdditionalCallInfo())
1109 MI->getMF()->moveAdditionalCallInfo(MI, FoldMI);
1110
1111 // If we've folded a store into an instruction labelled with debug-info,
1112 // record a substitution from the old operand to the memory operand. Handle
1113 // the simple common case where operand 0 is the one being folded, plus when
1114 // the destination operand is also a tied def. More values could be
1115 // substituted / preserved with more analysis.
1116 if (MI->peekDebugInstrNum() && Ops[0].second == 0) {
1117 // Helper lambda.
1118 auto MakeSubstitution = [this,FoldMI,MI,&Ops]() {
1119 // Substitute old operand zero to the new instructions memory operand.
1120 unsigned OldOperandNum = Ops[0].second;
1121 unsigned NewNum = FoldMI->getDebugInstrNum();
1122 unsigned OldNum = MI->getDebugInstrNum();
1123 MF.makeDebugValueSubstitution({OldNum, OldOperandNum},
1125 };
1126
1127 const MachineOperand &Op0 = MI->getOperand(Ops[0].second);
1128 if (Ops.size() == 1 && Op0.isDef()) {
1129 MakeSubstitution();
1130 } else if (Ops.size() == 2 && Op0.isDef() && MI->getOperand(1).isTied() &&
1131 Op0.getReg() == MI->getOperand(1).getReg()) {
1132 MakeSubstitution();
1133 }
1134 } else if (MI->peekDebugInstrNum()) {
1135 // This is a debug-labelled instruction, but the operand being folded isn't
1136 // at operand zero. Most likely this means it's a load being folded in.
1137 // Substitute any register defs from operand zero up to the one being
1138 // folded -- past that point, we don't know what the new operand indexes
1139 // will be.
1140 MF.substituteDebugValuesForInst(*MI, *FoldMI, Ops[0].second);
1141 }
1142
1143 MI->eraseFromParent();
1144
1145 // Insert any new instructions other than FoldMI into the LIS maps.
1146 assert(!MIS.empty() && "Unexpected empty span of instructions!");
1147 for (MachineInstr &MI : MIS)
1148 if (&MI != FoldMI && &MI != CopyMI)
1150
1151 if (CopyMI) {
1152 Register R = CopyMI->getOperand(1).getReg();
1153 if (R.isVirtual()) {
1154 LiveInterval &LI = LIS.getInterval(R);
1155 LIS.shrinkToUses(&LI);
1156 } else {
1157 assert(MRI.isReserved(R) && "Unexpected PhysReg in source operand!");
1158 }
1159 }
1160
1161 // TII.foldMemoryOperand may have left some implicit operands on the
1162 // instruction. Strip them.
1163 if (ImpReg)
1164 for (unsigned i = FoldMI->getNumOperands(); i; --i) {
1165 MachineOperand &MO = FoldMI->getOperand(i - 1);
1166 if (!MO.isReg() || !MO.isImplicit())
1167 break;
1168 if (MO.getReg() == ImpReg)
1169 FoldMI->removeOperand(i - 1);
1170 }
1171
1172 LLVM_DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS,
1173 "folded"));
1174
1175 if (!WasCopy)
1176 ++NumFolded;
1177 else if (Ops.front().second == 0) {
1178 ++NumSpills;
1179 // If there is only 1 store instruction is required for spill, add it
1180 // to mergeable list. In X86 AMX, 2 intructions are required to store.
1181 // We disable the merge for this case.
1182 if (std::distance(MIS.begin(), MIS.end()) <= 1)
1183 HSpiller.addToMergeableSpills(*FoldMI, StackSlot, Original);
1184 } else
1185 ++NumReloads;
1186 return true;
1187}
1188
1189void InlineSpiller::insertReload(Register NewVReg,
1190 SlotIndex Idx,
1192 MachineBasicBlock &MBB = *MI->getParent();
1193
1194 MachineInstrSpan MIS(MI, &MBB);
1195 TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot,
1196 MRI.getRegClass(NewVReg), Register());
1197
1198 LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI);
1199
1200 LLVM_DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload",
1201 NewVReg));
1202 ++NumReloads;
1203}
1204
1205/// Check if \p Def fully defines a VReg with an undefined value.
1206/// If that's the case, that means the value of VReg is actually
1207/// not relevant.
1208static bool isRealSpill(const MachineInstr &Def) {
1209 if (!Def.isImplicitDef())
1210 return true;
1211
1212 // We can say that the VReg defined by Def is undef, only if it is
1213 // fully defined by Def. Otherwise, some of the lanes may not be
1214 // undef and the value of the VReg matters.
1215 return Def.getOperand(0).getSubReg();
1216}
1217
1218/// insertSpill - Insert a spill of NewVReg after MI.
1219void InlineSpiller::insertSpill(Register NewVReg, bool isKill,
1221 // Spill are not terminators, so inserting spills after terminators will
1222 // violate invariants in MachineVerifier.
1223 assert(!MI->isTerminator() && "Inserting a spill after a terminator");
1224 MachineBasicBlock &MBB = *MI->getParent();
1225
1226 MachineInstrSpan MIS(MI, &MBB);
1227 MachineBasicBlock::iterator SpillBefore = std::next(MI);
1228 bool IsRealSpill = isRealSpill(*MI);
1229
1230 if (IsRealSpill)
1231 TII.storeRegToStackSlot(MBB, SpillBefore, NewVReg, isKill, StackSlot,
1232 MRI.getRegClass(NewVReg), Register());
1233 else
1234 // Don't spill undef value.
1235 // Anything works for undef, in particular keeping the memory
1236 // uninitialized is a viable option and it saves code size and
1237 // run time.
1238 BuildMI(MBB, SpillBefore, MI->getDebugLoc(), TII.get(TargetOpcode::KILL))
1239 .addReg(NewVReg, getKillRegState(isKill));
1240
1242 LIS.InsertMachineInstrRangeInMaps(Spill, MIS.end());
1243 for (const MachineInstr &MI : make_range(Spill, MIS.end()))
1244 getVDefInterval(MI, LIS);
1245
1246 LLVM_DEBUG(
1247 dumpMachineInstrRangeWithSlotIndex(Spill, MIS.end(), LIS, "spill"));
1248 ++NumSpills;
1249 // If there is only 1 store instruction is required for spill, add it
1250 // to mergeable list. In X86 AMX, 2 intructions are required to store.
1251 // We disable the merge for this case.
1252 if (IsRealSpill && std::distance(Spill, MIS.end()) <= 1)
1253 HSpiller.addToMergeableSpills(*Spill, StackSlot, Original);
1254}
1255
1256/// spillAroundUses - insert spill code around each use of Reg.
1257void InlineSpiller::spillAroundUses(Register Reg) {
1258 LLVM_DEBUG(dbgs() << "spillAroundUses " << printReg(Reg) << '\n');
1259 LiveInterval &OldLI = LIS.getInterval(Reg);
1260
1261 // Iterate over instructions using Reg.
1262 for (MachineInstr &MI : llvm::make_early_inc_range(MRI.reg_bundles(Reg))) {
1263 // Debug values are not allowed to affect codegen.
1264 if (MI.isDebugValue()) {
1265 // Modify DBG_VALUE now that the value is in a spill slot.
1266 MachineBasicBlock *MBB = MI.getParent();
1267 LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:\t" << MI);
1268 buildDbgValueForSpill(*MBB, &MI, MI, StackSlot, Reg);
1269 MBB->erase(MI);
1270 continue;
1271 }
1272
1273 assert(!MI.isDebugInstr() && "Did not expect to find a use in debug "
1274 "instruction that isn't a DBG_VALUE");
1275
1276 // Ignore copies to/from snippets. We'll delete them.
1277 if (SnippetCopies.count(&MI))
1278 continue;
1279
1280 // Stack slot accesses may coalesce away.
1281 if (coalesceStackAccess(&MI, Reg))
1282 continue;
1283
1284 // Analyze instruction.
1287
1288 // Find the slot index where this instruction reads and writes OldLI.
1289 // This is usually the def slot, except for tied early clobbers.
1291 if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
1292 if (SlotIndex::isSameInstr(Idx, VNI->def))
1293 Idx = VNI->def;
1294
1295 // Check for a sibling copy.
1296 Register SibReg = isCopyOfBundle(MI, Reg, TII);
1297 if (SibReg && isSibling(SibReg)) {
1298 // This may actually be a copy between snippets.
1299 if (isRegToSpill(SibReg)) {
1300 LLVM_DEBUG(dbgs() << "Found new snippet copy: " << MI);
1301 SnippetCopies.insert(&MI);
1302 continue;
1303 }
1304 if (RI.Writes) {
1305 if (hoistSpillInsideBB(OldLI, MI)) {
1306 // This COPY is now dead, the value is already in the stack slot.
1307 MI.getOperand(0).setIsDead();
1308 DeadDefs.push_back(&MI);
1309 continue;
1310 }
1311 } else {
1312 // This is a reload for a sib-reg copy. Drop spills downstream.
1313 LiveInterval &SibLI = LIS.getInterval(SibReg);
1314 eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
1315 // The COPY will fold to a reload below.
1316 }
1317 }
1318
1319 // Attempt to fold memory ops.
1320 if (foldMemoryOperand(Ops))
1321 continue;
1322
1323 // Create a new virtual register for spill/fill.
1324 // FIXME: Infer regclass from instruction alone.
1325 Register NewVReg = Edit->createFrom(Reg);
1326
1327 if (RI.Reads)
1328 insertReload(NewVReg, Idx, &MI);
1329
1330 // Rewrite instruction operands.
1331 bool hasLiveDef = false;
1332 for (const auto &OpPair : Ops) {
1333 MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
1334 MO.setReg(NewVReg);
1335 if (MO.isUse()) {
1336 if (!OpPair.first->isRegTiedToDefOperand(OpPair.second))
1337 MO.setIsKill();
1338 } else {
1339 if (!MO.isDead())
1340 hasLiveDef = true;
1341 }
1342 }
1343 LLVM_DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << MI << '\n');
1344
1345 // FIXME: Use a second vreg if instruction has no tied ops.
1346 if (RI.Writes)
1347 if (hasLiveDef)
1348 insertSpill(NewVReg, true, &MI);
1349 }
1350}
1351
1352/// spillAll - Spill all registers remaining after rematerialization.
1353void InlineSpiller::spillAll() {
1354 // Update LiveStacks now that we are committed to spilling.
1355 if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
1356 StackSlot = VRM.assignVirt2StackSlot(Original);
1357 StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
1358 StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
1359 } else
1360 StackInt = &LSS.getInterval(StackSlot);
1361
1362 if (Original != Edit->getReg())
1363 VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
1364
1365 assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
1366 for (Register Reg : RegsToSpill)
1367 StackInt->MergeSegmentsInAsValue(LIS.getInterval(Reg),
1368 StackInt->getValNumInfo(0));
1369 LLVM_DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
1370
1371 // Spill around uses of all RegsToSpill.
1372 for (Register Reg : RegsToSpill) {
1373 spillAroundUses(Reg);
1374 // Assign all of the spilled registers to the slot so that
1375 // LiveDebugVariables knows about these locations later on.
1376 if (VRM.getStackSlot(Reg) == VirtRegMap::NO_STACK_SLOT)
1377 VRM.assignVirt2StackSlot(Reg, StackSlot);
1378 }
1379
1380 // Hoisted spills may cause dead code.
1381 if (!DeadDefs.empty()) {
1382 LLVM_DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
1383 Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
1384 }
1385
1386 // Finally delete the SnippetCopies.
1387 for (Register Reg : RegsToSpill) {
1388 for (MachineInstr &MI :
1389 llvm::make_early_inc_range(MRI.reg_instructions(Reg))) {
1390 assert(SnippetCopies.count(&MI) && "Remaining use wasn't a snippet copy");
1391 // FIXME: Do this with a LiveRangeEdit callback.
1393 MI.eraseFromBundle();
1394 }
1395 }
1396
1397 // Delete all spilled registers.
1398 for (Register Reg : RegsToSpill)
1399 Edit->eraseVirtReg(Reg);
1400}
1401
1402void InlineSpiller::spill(LiveRangeEdit &edit, AllocationOrder *order) {
1403 ++NumSpilledRanges;
1404 Edit = &edit;
1405 Order = order;
1406 assert(!edit.getReg().isStack() && "Trying to spill a stack slot.");
1407 // Share a stack slot among all descendants of Original.
1408 Original = VRM.getOriginal(edit.getReg());
1409 StackSlot = VRM.getStackSlot(Original);
1410 StackInt = nullptr;
1411
1412 LLVM_DEBUG(dbgs() << "Inline spilling "
1413 << TRI.getRegClassName(MRI.getRegClass(edit.getReg()))
1414 << ':' << edit.getParent() << "\nFrom original "
1415 << printReg(Original) << '\n');
1416 assert(edit.getParent().isSpillable() &&
1417 "Attempting to spill already spilled value.");
1418 assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
1419
1420 collectRegsToSpill();
1421 reMaterializeAll();
1422
1423 // Remat may handle everything.
1424 if (!RegsToSpill.empty())
1425 spillAll();
1426
1427 Edit->calculateRegClassAndHint(MF, VRAI);
1428}
1429
1430/// Optimizations after all the reg selections and spills are done.
1431void InlineSpiller::postOptimization() { HSpiller.hoistAllSpills(); }
1432
1433/// When a spill is inserted, add the spill to MergeableSpills map.
1434void HoistSpillHelper::addToMergeableSpills(MachineInstr &Spill, int StackSlot,
1435 Register Original) {
1437 LiveInterval &OrigLI = LIS.getInterval(Original);
1438 // save a copy of LiveInterval in StackSlotToOrigLI because the original
1439 // LiveInterval may be cleared after all its references are spilled.
1440
1441 auto [Place, Inserted] = StackSlotToOrigLI.try_emplace(StackSlot);
1442 if (Inserted) {
1443 auto LI = std::make_unique<LiveInterval>(OrigLI.reg(), OrigLI.weight());
1444 LI->assign(OrigLI, Allocator);
1445 Place->second = std::move(LI);
1446 }
1447
1448 SlotIndex Idx = LIS.getInstructionIndex(Spill);
1449 VNInfo *OrigVNI = Place->second->getVNInfoAt(Idx.getRegSlot());
1450 std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1451 MergeableSpills[MIdx].insert(&Spill);
1452}
1453
1454/// When a spill is removed, remove the spill from MergeableSpills map.
1455/// Return true if the spill is removed successfully.
1456bool HoistSpillHelper::rmFromMergeableSpills(MachineInstr &Spill,
1457 int StackSlot) {
1458 auto It = StackSlotToOrigLI.find(StackSlot);
1459 if (It == StackSlotToOrigLI.end())
1460 return false;
1461 SlotIndex Idx = LIS.getInstructionIndex(Spill);
1462 VNInfo *OrigVNI = It->second->getVNInfoAt(Idx.getRegSlot());
1463 std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1464 return MergeableSpills[MIdx].erase(&Spill);
1465}
1466
1467/// Check BB to see if it is a possible target BB to place a hoisted spill,
1468/// i.e., there should be a living sibling of OrigReg at the insert point.
1469bool HoistSpillHelper::isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI,
1470 MachineBasicBlock &BB, Register &LiveReg) {
1471 SlotIndex Idx = IPA.getLastInsertPoint(OrigLI, BB);
1472 // The original def could be after the last insert point in the root block,
1473 // we can't hoist to here.
1474 if (Idx < OrigVNI.def) {
1475 // TODO: We could be better here. If LI is not alive in landing pad
1476 // we could hoist spill after LIP.
1477 LLVM_DEBUG(dbgs() << "can't spill in root block - def after LIP\n");
1478 return false;
1479 }
1480 Register OrigReg = OrigLI.reg();
1481 SmallSetVector<Register, 16> &Siblings = Virt2SiblingsMap[OrigReg];
1482 assert(OrigLI.getVNInfoAt(Idx) == &OrigVNI && "Unexpected VNI");
1483
1484 for (const Register &SibReg : Siblings) {
1485 LiveInterval &LI = LIS.getInterval(SibReg);
1486 if (!LI.getVNInfoAt(Idx))
1487 continue;
1488 // All of the sub-ranges should be alive at the prospective slot index.
1489 // Otherwise, we might risk storing unrelated / compromised values from some
1490 // sub-registers to the spill slot.
1491 if (all_of(LI.subranges(), [&](const LiveInterval::SubRange &SR) {
1492 return SR.getVNInfoAt(Idx) != nullptr;
1493 })) {
1494 LiveReg = SibReg;
1495 return true;
1496 }
1497 }
1498 return false;
1499}
1500
1501/// Remove redundant spills in the same BB. Save those redundant spills in
1502/// SpillsToRm, and save the spill to keep and its BB in SpillBBToSpill map.
1503void HoistSpillHelper::rmRedundantSpills(
1507 // For each spill saw, check SpillBBToSpill[] and see if its BB already has
1508 // another spill inside. If a BB contains more than one spill, only keep the
1509 // earlier spill with smaller SlotIndex.
1510 for (auto *const CurrentSpill : Spills) {
1511 MachineBasicBlock *Block = CurrentSpill->getParent();
1512 MachineDomTreeNode *Node = MDT.getNode(Block);
1513 MachineInstr *PrevSpill = SpillBBToSpill[Node];
1514 if (PrevSpill) {
1515 SlotIndex PIdx = LIS.getInstructionIndex(*PrevSpill);
1516 SlotIndex CIdx = LIS.getInstructionIndex(*CurrentSpill);
1517 MachineInstr *SpillToRm = (CIdx > PIdx) ? CurrentSpill : PrevSpill;
1518 MachineInstr *SpillToKeep = (CIdx > PIdx) ? PrevSpill : CurrentSpill;
1519 SpillsToRm.push_back(SpillToRm);
1520 SpillBBToSpill[MDT.getNode(Block)] = SpillToKeep;
1521 } else {
1522 SpillBBToSpill[MDT.getNode(Block)] = CurrentSpill;
1523 }
1524 }
1525 for (auto *const SpillToRm : SpillsToRm)
1526 Spills.erase(SpillToRm);
1527}
1528
1529/// Starting from \p Root find a top-down traversal order of the dominator
1530/// tree to visit all basic blocks containing the elements of \p Spills.
1531/// Redundant spills will be found and put into \p SpillsToRm at the same
1532/// time. \p SpillBBToSpill will be populated as part of the process and
1533/// maps a basic block to the first store occurring in the basic block.
1534/// \post SpillsToRm.union(Spills\@post) == Spills\@pre
1535void HoistSpillHelper::getVisitOrders(
1541 // The set contains all the possible BB nodes to which we may hoist
1542 // original spills.
1544 // Save the BB nodes on the path from the first BB node containing
1545 // non-redundant spill to the Root node.
1547 // All the spills to be hoisted must originate from a single def instruction
1548 // to the OrigReg. It means the def instruction should dominate all the spills
1549 // to be hoisted. We choose the BB where the def instruction is located as
1550 // the Root.
1551 MachineDomTreeNode *RootIDomNode = MDT[Root]->getIDom();
1552 // For every node on the dominator tree with spill, walk up on the dominator
1553 // tree towards the Root node until it is reached. If there is other node
1554 // containing spill in the middle of the path, the previous spill saw will
1555 // be redundant and the node containing it will be removed. All the nodes on
1556 // the path starting from the first node with non-redundant spill to the Root
1557 // node will be added to the WorkSet, which will contain all the possible
1558 // locations where spills may be hoisted to after the loop below is done.
1559 for (auto *const Spill : Spills) {
1560 MachineBasicBlock *Block = Spill->getParent();
1562 MachineInstr *SpillToRm = nullptr;
1563 while (Node != RootIDomNode) {
1564 // If Node dominates Block, and it already contains a spill, the spill in
1565 // Block will be redundant.
1566 if (Node != MDT[Block] && SpillBBToSpill[Node]) {
1567 SpillToRm = SpillBBToSpill[MDT[Block]];
1568 break;
1569 /// If we see the Node already in WorkSet, the path from the Node to
1570 /// the Root node must already be traversed by another spill.
1571 /// Then no need to repeat.
1572 } else if (WorkSet.count(Node)) {
1573 break;
1574 } else {
1575 NodesOnPath.insert(Node);
1576 }
1577 Node = Node->getIDom();
1578 }
1579 if (SpillToRm) {
1580 SpillsToRm.push_back(SpillToRm);
1581 } else {
1582 // Add a BB containing the original spills to SpillsToKeep -- i.e.,
1583 // set the initial status before hoisting start. The value of BBs
1584 // containing original spills is set to 0, in order to descriminate
1585 // with BBs containing hoisted spills which will be inserted to
1586 // SpillsToKeep later during hoisting.
1587 SpillsToKeep[MDT[Block]] = Register();
1588 WorkSet.insert_range(NodesOnPath);
1589 }
1590 NodesOnPath.clear();
1591 }
1592
1593 // Sort the nodes in WorkSet in top-down order and save the nodes
1594 // in Orders. Orders will be used for hoisting in runHoistSpills.
1595 unsigned idx = 0;
1596 Orders.push_back(MDT.getNode(Root));
1597 do {
1598 MachineDomTreeNode *Node = Orders[idx++];
1599 for (MachineDomTreeNode *Child : Node->children()) {
1600 if (WorkSet.count(Child))
1601 Orders.push_back(Child);
1602 }
1603 } while (idx != Orders.size());
1604 assert(Orders.size() == WorkSet.size() &&
1605 "Orders have different size with WorkSet");
1606
1607#ifndef NDEBUG
1608 LLVM_DEBUG(dbgs() << "Orders size is " << Orders.size() << "\n");
1610 for (; RIt != Orders.rend(); RIt++)
1611 LLVM_DEBUG(dbgs() << "BB" << (*RIt)->getBlock()->getNumber() << ",");
1612 LLVM_DEBUG(dbgs() << "\n");
1613#endif
1614}
1615
1616/// Try to hoist spills according to BB hotness. The spills to removed will
1617/// be saved in \p SpillsToRm. The spills to be inserted will be saved in
1618/// \p SpillsToIns.
1619void HoistSpillHelper::runHoistSpills(
1620 LiveInterval &OrigLI, VNInfo &OrigVNI,
1624 // Visit order of dominator tree nodes.
1626 // SpillsToKeep contains all the nodes where spills are to be inserted
1627 // during hoisting. If the spill to be inserted is an original spill
1628 // (not a hoisted one), the value of the map entry is 0. If the spill
1629 // is a hoisted spill, the value of the map entry is the VReg to be used
1630 // as the source of the spill.
1632 // Map from BB to the first spill inside of it.
1634
1635 rmRedundantSpills(Spills, SpillsToRm, SpillBBToSpill);
1636
1637 MachineBasicBlock *Root = LIS.getMBBFromIndex(OrigVNI.def);
1638 getVisitOrders(Root, Spills, Orders, SpillsToRm, SpillsToKeep,
1639 SpillBBToSpill);
1640
1641 // SpillsInSubTreeMap keeps the map from a dom tree node to a pair of
1642 // nodes set and the cost of all the spills inside those nodes.
1643 // The nodes set are the locations where spills are to be inserted
1644 // in the subtree of current node.
1645 using NodesCostPair =
1646 std::pair<SmallPtrSet<MachineDomTreeNode *, 16>, BlockFrequency>;
1648
1649 // Iterate Orders set in reverse order, which will be a bottom-up order
1650 // in the dominator tree. Once we visit a dom tree node, we know its
1651 // children have already been visited and the spill locations in the
1652 // subtrees of all the children have been determined.
1654 for (; RIt != Orders.rend(); RIt++) {
1655 MachineBasicBlock *Block = (*RIt)->getBlock();
1656
1657 // If Block contains an original spill, simply continue.
1658 if (auto It = SpillsToKeep.find(*RIt);
1659 It != SpillsToKeep.end() && !It->second) {
1660 auto &SIt = SpillsInSubTreeMap[*RIt];
1661 SIt.first.insert(*RIt);
1662 // Sit.second contains the cost of spill.
1663 SIt.second = MBFI.getBlockFreq(Block);
1664 continue;
1665 }
1666
1667 // Collect spills in subtree of current node (*RIt) to
1668 // SpillsInSubTreeMap[*RIt].first.
1669 for (MachineDomTreeNode *Child : (*RIt)->children()) {
1670 if (!SpillsInSubTreeMap.contains(Child))
1671 continue;
1672 // The stmt:
1673 // "auto &[SpillsInSubTree, SubTreeCost] = SpillsInSubTreeMap[*RIt]"
1674 // below should be placed before getting the begin and end iterators of
1675 // SpillsInSubTreeMap[Child].first, or else the iterators may be
1676 // invalidated when SpillsInSubTreeMap[*RIt] is seen the first time
1677 // and the map grows and then the original buckets in the map are moved.
1678 auto &[SpillsInSubTree, SubTreeCost] = SpillsInSubTreeMap[*RIt];
1679 auto ChildIt = SpillsInSubTreeMap.find(Child);
1680 SubTreeCost += ChildIt->second.second;
1681 auto BI = ChildIt->second.first.begin();
1682 auto EI = ChildIt->second.first.end();
1683 SpillsInSubTree.insert(BI, EI);
1684 SpillsInSubTreeMap.erase(ChildIt);
1685 }
1686
1687 auto &[SpillsInSubTree, SubTreeCost] = SpillsInSubTreeMap[*RIt];
1688 // No spills in subtree, simply continue.
1689 if (SpillsInSubTree.empty())
1690 continue;
1691
1692 // Check whether Block is a possible candidate to insert spill.
1693 Register LiveReg;
1694 if (!isSpillCandBB(OrigLI, OrigVNI, *Block, LiveReg))
1695 continue;
1696
1697 // If there are multiple spills that could be merged, bias a little
1698 // to hoist the spill.
1699 BranchProbability MarginProb = (SpillsInSubTree.size() > 1)
1700 ? BranchProbability(9, 10)
1701 : BranchProbability(1, 1);
1702 if (SubTreeCost > MBFI.getBlockFreq(Block) * MarginProb) {
1703 // Hoist: Move spills to current Block.
1704 for (auto *const SpillBB : SpillsInSubTree) {
1705 // When SpillBB is a BB contains original spill, insert the spill
1706 // to SpillsToRm.
1707 if (auto It = SpillsToKeep.find(SpillBB);
1708 It != SpillsToKeep.end() && !It->second) {
1709 MachineInstr *SpillToRm = SpillBBToSpill[SpillBB];
1710 SpillsToRm.push_back(SpillToRm);
1711 }
1712 // SpillBB will not contain spill anymore, remove it from SpillsToKeep.
1713 SpillsToKeep.erase(SpillBB);
1714 }
1715 // Current Block is the BB containing the new hoisted spill. Add it to
1716 // SpillsToKeep. LiveReg is the source of the new spill.
1717 SpillsToKeep[*RIt] = LiveReg;
1718 LLVM_DEBUG({
1719 dbgs() << "spills in BB: ";
1720 for (const auto Rspill : SpillsInSubTree)
1721 dbgs() << Rspill->getBlock()->getNumber() << " ";
1722 dbgs() << "were promoted to BB" << (*RIt)->getBlock()->getNumber()
1723 << "\n";
1724 });
1725 SpillsInSubTree.clear();
1726 SpillsInSubTree.insert(*RIt);
1727 SubTreeCost = MBFI.getBlockFreq(Block);
1728 }
1729 }
1730 // For spills in SpillsToKeep with LiveReg set (i.e., not original spill),
1731 // save them to SpillsToIns.
1732 for (const auto &Ent : SpillsToKeep) {
1733 if (Ent.second)
1734 SpillsToIns[Ent.first->getBlock()] = Ent.second;
1735 }
1736}
1737
1738/// For spills with equal values, remove redundant spills and hoist those left
1739/// to less hot spots.
1740///
1741/// Spills with equal values will be collected into the same set in
1742/// MergeableSpills when spill is inserted. These equal spills are originated
1743/// from the same defining instruction and are dominated by the instruction.
1744/// Before hoisting all the equal spills, redundant spills inside in the same
1745/// BB are first marked to be deleted. Then starting from the spills left, walk
1746/// up on the dominator tree towards the Root node where the define instruction
1747/// is located, mark the dominated spills to be deleted along the way and
1748/// collect the BB nodes on the path from non-dominated spills to the define
1749/// instruction into a WorkSet. The nodes in WorkSet are the candidate places
1750/// where we are considering to hoist the spills. We iterate the WorkSet in
1751/// bottom-up order, and for each node, we will decide whether to hoist spills
1752/// inside its subtree to that node. In this way, we can get benefit locally
1753/// even if hoisting all the equal spills to one cold place is impossible.
1754void HoistSpillHelper::hoistAllSpills() {
1755 SmallVector<Register, 4> NewVRegs;
1756 LiveRangeEdit Edit(nullptr, NewVRegs, MF, LIS, &VRM, this);
1757
1758 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
1760 Register Original = VRM.getPreSplitReg(Reg);
1761 if (!MRI.def_empty(Reg) && Original.isValid())
1762 Virt2SiblingsMap[Original].insert(Reg);
1763 }
1764
1765 // Each entry in MergeableSpills contains a spill set with equal values.
1766 for (auto &Ent : MergeableSpills) {
1767 int Slot = Ent.first.first;
1768 LiveInterval &OrigLI = *StackSlotToOrigLI[Slot];
1769 VNInfo *OrigVNI = Ent.first.second;
1770 SmallPtrSet<MachineInstr *, 16> &EqValSpills = Ent.second;
1771 if (Ent.second.empty())
1772 continue;
1773
1774 LLVM_DEBUG({
1775 dbgs() << "\nFor Slot" << Slot << " and VN" << OrigVNI->id << ":\n"
1776 << "Equal spills in BB: ";
1777 for (const auto spill : EqValSpills)
1778 dbgs() << spill->getParent()->getNumber() << " ";
1779 dbgs() << "\n";
1780 });
1781
1782 // SpillsToRm is the spill set to be removed from EqValSpills.
1784 // SpillsToIns is the spill set to be newly inserted after hoisting.
1786
1787 runHoistSpills(OrigLI, *OrigVNI, EqValSpills, SpillsToRm, SpillsToIns);
1788
1789 LLVM_DEBUG({
1790 dbgs() << "Finally inserted spills in BB: ";
1791 for (const auto &Ispill : SpillsToIns)
1792 dbgs() << Ispill.first->getNumber() << " ";
1793 dbgs() << "\nFinally removed spills in BB: ";
1794 for (const auto Rspill : SpillsToRm)
1795 dbgs() << Rspill->getParent()->getNumber() << " ";
1796 dbgs() << "\n";
1797 });
1798
1799 // Stack live range update.
1800 LiveInterval &StackIntvl = LSS.getInterval(Slot);
1801 if (!SpillsToIns.empty() || !SpillsToRm.empty())
1802 StackIntvl.MergeValueInAsValue(OrigLI, OrigVNI,
1803 StackIntvl.getValNumInfo(0));
1804
1805 // Insert hoisted spills.
1806 for (auto const &Insert : SpillsToIns) {
1807 MachineBasicBlock *BB = Insert.first;
1808 Register LiveReg = Insert.second;
1809 MachineBasicBlock::iterator MII = IPA.getLastInsertPointIter(OrigLI, *BB);
1810 MachineInstrSpan MIS(MII, BB);
1811 TII.storeRegToStackSlot(*BB, MII, LiveReg, false, Slot,
1812 MRI.getRegClass(LiveReg), Register());
1813 LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MII);
1814 for (const MachineInstr &MI : make_range(MIS.begin(), MII))
1815 getVDefInterval(MI, LIS);
1816 ++NumSpills;
1817 }
1818
1819 // Remove redundant spills or change them to dead instructions.
1820 NumSpills -= SpillsToRm.size();
1821 for (auto *const RMEnt : SpillsToRm) {
1822 RMEnt->setDesc(TII.get(TargetOpcode::KILL));
1823 for (unsigned i = RMEnt->getNumOperands(); i; --i) {
1824 MachineOperand &MO = RMEnt->getOperand(i - 1);
1825 if (MO.isReg() && MO.isImplicit() && MO.isDef() && !MO.isDead())
1826 RMEnt->removeOperand(i - 1);
1827 }
1828 }
1829 Edit.eliminateDeadDefs(SpillsToRm, {});
1830 }
1831}
1832
1833/// Called before a virtual register is erased from LiveIntervals.
1834/// Forcibly remove the register from LiveRegMatrix before it's deleted,
1835/// preventing dangling pointers.
1836bool HoistSpillHelper::LRE_CanEraseVirtReg(Register VirtReg) {
1837 if (Matrix && VRM.hasPhys(VirtReg)) {
1838 const LiveInterval &LI = LIS.getInterval(VirtReg);
1839 Matrix->unassign(LI, /*ClearAllReferencingSegments=*/true);
1840 }
1841 return true; // Allow deletion to proceed
1842}
1843
1844/// For VirtReg clone, the \p New register should have the same physreg or
1845/// stackslot as the \p old register.
1846void HoistSpillHelper::LRE_DidCloneVirtReg(Register New, Register Old) {
1847 if (VRM.hasPhys(Old))
1848 VRM.assignVirt2Phys(New, VRM.getPhys(Old));
1849 else if (VRM.getStackSlot(Old) != VirtRegMap::NO_STACK_SLOT)
1850 VRM.assignVirt2StackSlot(New, VRM.getStackSlot(Old));
1851 else
1852 llvm_unreachable("VReg should be assigned either physreg or stackslot");
1853 if (VRM.hasShape(Old))
1854 VRM.assignVirt2Shape(New, VRM.getShape(Old));
1855}
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:119
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:328
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:239
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.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
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...
bool isFullCopy() const
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:38
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:209
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.