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