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