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