1 //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is responsible for finalizing the functions frame layout, saving
11 // callee saved registers, and for emitting prolog & epilog code for the
14 // This pass must be run after register allocation. After this pass is
15 // executed, it is illegal to construct MO_FrameIndex operands.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/MachineDominators.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineLoopInfo.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/Passes.h"
30 #include "llvm/CodeGen/RegisterScavenging.h"
31 #include "llvm/CodeGen/StackProtector.h"
32 #include "llvm/CodeGen/WinEHFuncInfo.h"
33 #include "llvm/IR/DiagnosticInfo.h"
34 #include "llvm/IR/InlineAsm.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Target/TargetFrameLowering.h"
40 #include "llvm/Target/TargetInstrInfo.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetRegisterInfo.h"
43 #include "llvm/Target/TargetSubtargetInfo.h"
48 #define DEBUG_TYPE "pei"
50 typedef SmallVector<MachineBasicBlock *, 4> MBBVector;
51 static void doSpillCalleeSavedRegs(MachineFunction &MF, RegScavenger *RS,
52 unsigned &MinCSFrameIndex,
53 unsigned &MaxCXFrameIndex,
54 const MBBVector &SaveBlocks,
55 const MBBVector &RestoreBlocks);
57 static void doScavengeFrameVirtualRegs(MachineFunction &MF, RegScavenger *RS);
60 class PEI : public MachineFunctionPass {
63 explicit PEI(const TargetMachine *TM = nullptr) : MachineFunctionPass(ID) {
64 initializePEIPass(*PassRegistry::getPassRegistry());
66 if (TM && (!TM->usesPhysRegsForPEI())) {
67 SpillCalleeSavedRegisters = [](MachineFunction &, RegScavenger *,
68 unsigned &, unsigned &, const MBBVector &,
69 const MBBVector &) {};
70 ScavengeFrameVirtualRegs = [](MachineFunction &, RegScavenger *) {};
72 SpillCalleeSavedRegisters = doSpillCalleeSavedRegs;
73 ScavengeFrameVirtualRegs = doScavengeFrameVirtualRegs;
74 UsesCalleeSaves = true;
78 void getAnalysisUsage(AnalysisUsage &AU) const override;
80 MachineFunctionProperties getRequiredProperties() const override {
81 MachineFunctionProperties MFP;
83 MFP.set(MachineFunctionProperties::Property::AllVRegsAllocated);
87 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
88 /// frame indexes with appropriate references.
90 bool runOnMachineFunction(MachineFunction &Fn) override;
93 std::function<void(MachineFunction &MF, RegScavenger *RS,
94 unsigned &MinCSFrameIndex, unsigned &MaxCSFrameIndex,
95 const MBBVector &SaveBlocks,
96 const MBBVector &RestoreBlocks)>
97 SpillCalleeSavedRegisters;
98 std::function<void(MachineFunction &MF, RegScavenger *RS)>
99 ScavengeFrameVirtualRegs;
101 bool UsesCalleeSaves = false;
105 // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
106 // stack frame indexes.
107 unsigned MinCSFrameIndex = std::numeric_limits<unsigned>::max();
108 unsigned MaxCSFrameIndex = 0;
110 // Save and Restore blocks of the current function. Typically there is a
111 // single save block, unless Windows EH funclets are involved.
112 MBBVector SaveBlocks;
113 MBBVector RestoreBlocks;
115 // Flag to control whether to use the register scavenger to resolve
116 // frame index materialization registers. Set according to
117 // TRI->requiresFrameIndexScavenging() for the current function.
118 bool FrameIndexVirtualScavenging;
120 void calculateCallFrameInfo(MachineFunction &Fn);
121 void calculateSaveRestoreBlocks(MachineFunction &Fn);
123 void calculateFrameObjectOffsets(MachineFunction &Fn);
124 void replaceFrameIndices(MachineFunction &Fn);
125 void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn,
127 void insertPrologEpilogCode(MachineFunction &Fn);
132 char &llvm::PrologEpilogCodeInserterID = PEI::ID;
134 static cl::opt<unsigned>
135 WarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1),
136 cl::desc("Warn for stack size bigger than the given"
139 INITIALIZE_TM_PASS_BEGIN(PEI, "prologepilog", "Prologue/Epilogue Insertion",
141 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
142 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
143 INITIALIZE_PASS_DEPENDENCY(StackProtector)
144 INITIALIZE_TM_PASS_END(PEI, "prologepilog",
145 "Prologue/Epilogue Insertion & Frame Finalization",
148 MachineFunctionPass *
149 llvm::createPrologEpilogInserterPass(const TargetMachine *TM) {
153 STATISTIC(NumScavengedRegs, "Number of frame index regs scavenged");
154 STATISTIC(NumBytesStackSpace,
155 "Number of bytes used for stack in all functions");
157 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
158 AU.setPreservesCFG();
159 AU.addPreserved<MachineLoopInfo>();
160 AU.addPreserved<MachineDominatorTree>();
161 AU.addRequired<StackProtector>();
162 MachineFunctionPass::getAnalysisUsage(AU);
166 /// StackObjSet - A set of stack object indexes
167 typedef SmallSetVector<int, 8> StackObjSet;
169 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
170 /// frame indexes with appropriate references.
172 bool PEI::runOnMachineFunction(MachineFunction &Fn) {
173 const Function* F = Fn.getFunction();
174 const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
175 const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
177 RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : nullptr;
178 FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn);
180 // Calculate the MaxCallFrameSize and AdjustsStack variables for the
181 // function's frame information. Also eliminates call frame pseudo
183 calculateCallFrameInfo(Fn);
185 // Determine placement of CSR spill/restore code and prolog/epilog code:
186 // place all spills in the entry block, all restores in return blocks.
187 calculateSaveRestoreBlocks(Fn);
189 // Handle CSR spilling and restoring, for targets that need it.
190 SpillCalleeSavedRegisters(Fn, RS, MinCSFrameIndex, MaxCSFrameIndex,
191 SaveBlocks, RestoreBlocks);
193 // Allow the target machine to make final modifications to the function
194 // before the frame layout is finalized.
195 TFI->processFunctionBeforeFrameFinalized(Fn, RS);
197 // Calculate actual frame offsets for all abstract stack objects...
198 calculateFrameObjectOffsets(Fn);
200 // Add prolog and epilog code to the function. This function is required
201 // to align the stack frame as necessary for any stack variables or
202 // called functions. Because of this, calculateCalleeSavedRegisters()
203 // must be called before this function in order to set the AdjustsStack
204 // and MaxCallFrameSize variables.
205 if (!F->hasFnAttribute(Attribute::Naked))
206 insertPrologEpilogCode(Fn);
208 // Replace all MO_FrameIndex operands with physical register references
209 // and actual offsets.
211 replaceFrameIndices(Fn);
213 // If register scavenging is needed, as we've enabled doing it as a
214 // post-pass, scavenge the virtual registers that frame index elimination
216 if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging) {
217 ScavengeFrameVirtualRegs(Fn, RS);
219 // Clear any vregs created by virtual scavenging.
220 Fn.getRegInfo().clearVirtRegs();
223 // Warn on stack size when we exceeds the given limit.
224 MachineFrameInfo *MFI = Fn.getFrameInfo();
225 uint64_t StackSize = MFI->getStackSize();
226 if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
227 DiagnosticInfoStackSize DiagStackSize(*F, StackSize);
228 F->getContext().diagnose(DiagStackSize);
233 RestoreBlocks.clear();
234 MFI->setSavePoint(nullptr);
235 MFI->setRestorePoint(nullptr);
239 /// Calculate the MaxCallFrameSize and AdjustsStack
240 /// variables for the function's frame information and eliminate call frame
241 /// pseudo instructions.
242 void PEI::calculateCallFrameInfo(MachineFunction &Fn) {
243 const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
244 const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
245 MachineFrameInfo *MFI = Fn.getFrameInfo();
247 unsigned MaxCallFrameSize = 0;
248 bool AdjustsStack = MFI->adjustsStack();
250 // Get the function call frame set-up and tear-down instruction opcode
251 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
252 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
254 // Early exit for targets which have no call frame setup/destroy pseudo
256 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
259 std::vector<MachineBasicBlock::iterator> FrameSDOps;
260 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
261 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
262 if (I->getOpcode() == FrameSetupOpcode ||
263 I->getOpcode() == FrameDestroyOpcode) {
264 assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
265 " instructions should have a single immediate argument!");
266 unsigned Size = I->getOperand(0).getImm();
267 if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
269 FrameSDOps.push_back(I);
270 } else if (I->isInlineAsm()) {
271 // Some inline asm's need a stack frame, as indicated by operand 1.
272 unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
273 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
277 MFI->setAdjustsStack(AdjustsStack);
278 MFI->setMaxCallFrameSize(MaxCallFrameSize);
280 for (std::vector<MachineBasicBlock::iterator>::iterator
281 i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
282 MachineBasicBlock::iterator I = *i;
284 // If call frames are not being included as part of the stack frame, and
285 // the target doesn't indicate otherwise, remove the call frame pseudos
286 // here. The sub/add sp instruction pairs are still inserted, but we don't
287 // need to track the SP adjustment for frame index elimination.
288 if (TFI->canSimplifyCallFramePseudos(Fn))
289 TFI->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
293 /// Compute the sets of entry and return blocks for saving and restoring
294 /// callee-saved registers, and placing prolog and epilog code.
295 void PEI::calculateSaveRestoreBlocks(MachineFunction &Fn) {
296 const MachineFrameInfo *MFI = Fn.getFrameInfo();
298 // Even when we do not change any CSR, we still want to insert the
299 // prologue and epilogue of the function.
300 // So set the save points for those.
302 // Use the points found by shrink-wrapping, if any.
303 if (MFI->getSavePoint()) {
304 SaveBlocks.push_back(MFI->getSavePoint());
305 assert(MFI->getRestorePoint() && "Both restore and save must be set");
306 MachineBasicBlock *RestoreBlock = MFI->getRestorePoint();
307 // If RestoreBlock does not have any successor and is not a return block
308 // then the end point is unreachable and we do not need to insert any
310 if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
311 RestoreBlocks.push_back(RestoreBlock);
315 // Save refs to entry and return blocks.
316 SaveBlocks.push_back(&Fn.front());
317 for (MachineBasicBlock &MBB : Fn) {
318 if (MBB.isEHFuncletEntry())
319 SaveBlocks.push_back(&MBB);
320 if (MBB.isReturnBlock())
321 RestoreBlocks.push_back(&MBB);
325 static void assignCalleeSavedSpillSlots(MachineFunction &F,
326 const BitVector &SavedRegs,
327 unsigned &MinCSFrameIndex,
328 unsigned &MaxCSFrameIndex) {
329 if (SavedRegs.empty())
332 const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
333 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&F);
335 std::vector<CalleeSavedInfo> CSI;
336 for (unsigned i = 0; CSRegs[i]; ++i) {
337 unsigned Reg = CSRegs[i];
338 if (SavedRegs.test(Reg))
339 CSI.push_back(CalleeSavedInfo(Reg));
342 const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
343 MachineFrameInfo *MFI = F.getFrameInfo();
344 if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
345 // If target doesn't implement this, use generic code.
348 return; // Early exit if no callee saved registers are modified!
350 unsigned NumFixedSpillSlots;
351 const TargetFrameLowering::SpillSlot *FixedSpillSlots =
352 TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
354 // Now that we know which registers need to be saved and restored, allocate
355 // stack slots for them.
356 for (auto &CS : CSI) {
357 unsigned Reg = CS.getReg();
358 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
361 if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
362 CS.setFrameIdx(FrameIdx);
366 // Check to see if this physreg must be spilled to a particular stack slot
368 const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
369 while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
370 FixedSlot->Reg != Reg)
373 if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
374 // Nope, just spill it anywhere convenient.
375 unsigned Align = RC->getAlignment();
376 unsigned StackAlign = TFI->getStackAlignment();
378 // We may not be able to satisfy the desired alignment specification of
379 // the TargetRegisterClass if the stack alignment is smaller. Use the
381 Align = std::min(Align, StackAlign);
382 FrameIdx = MFI->CreateStackObject(RC->getSize(), Align, true);
383 if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
384 if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
386 // Spill it to the stack where we must.
388 MFI->CreateFixedSpillStackObject(RC->getSize(), FixedSlot->Offset);
391 CS.setFrameIdx(FrameIdx);
395 MFI->setCalleeSavedInfo(CSI);
398 /// Helper function to update the liveness information for the callee-saved
400 static void updateLiveness(MachineFunction &MF) {
401 MachineFrameInfo *MFI = MF.getFrameInfo();
402 // Visited will contain all the basic blocks that are in the region
403 // where the callee saved registers are alive:
404 // - Anything that is not Save or Restore -> LiveThrough.
406 // - Restore -> LiveOut.
407 // The live-out is not attached to the block, so no need to keep
408 // Restore in this set.
409 SmallPtrSet<MachineBasicBlock *, 8> Visited;
410 SmallVector<MachineBasicBlock *, 8> WorkList;
411 MachineBasicBlock *Entry = &MF.front();
412 MachineBasicBlock *Save = MFI->getSavePoint();
418 WorkList.push_back(Entry);
419 Visited.insert(Entry);
421 Visited.insert(Save);
423 MachineBasicBlock *Restore = MFI->getRestorePoint();
425 // By construction Restore cannot be visited, otherwise it
426 // means there exists a path to Restore that does not go
428 WorkList.push_back(Restore);
430 while (!WorkList.empty()) {
431 const MachineBasicBlock *CurBB = WorkList.pop_back_val();
432 // By construction, the region that is after the save point is
433 // dominated by the Save and post-dominated by the Restore.
434 if (CurBB == Save && Save != Restore)
436 // Enqueue all the successors not already visited.
437 // Those are by construction either before Save or after Restore.
438 for (MachineBasicBlock *SuccBB : CurBB->successors())
439 if (Visited.insert(SuccBB).second)
440 WorkList.push_back(SuccBB);
443 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
445 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
446 for (MachineBasicBlock *MBB : Visited) {
447 MCPhysReg Reg = CSI[i].getReg();
448 // Add the callee-saved register as live-in.
449 // It's killed at the spill.
450 if (!MBB->isLiveIn(Reg))
456 /// insertCSRSpillsAndRestores - Insert spill and restore code for
457 /// callee saved registers used in the function.
459 static void insertCSRSpillsAndRestores(MachineFunction &Fn,
460 const MBBVector &SaveBlocks,
461 const MBBVector &RestoreBlocks) {
462 // Get callee saved register information.
463 MachineFrameInfo *MFI = Fn.getFrameInfo();
464 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
466 MFI->setCalleeSavedInfoValid(true);
468 // Early exit if no callee saved registers are modified!
472 const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
473 const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
474 const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
475 MachineBasicBlock::iterator I;
477 // Spill using target interface.
478 for (MachineBasicBlock *SaveBlock : SaveBlocks) {
479 I = SaveBlock->begin();
480 if (!TFI->spillCalleeSavedRegisters(*SaveBlock, I, CSI, TRI)) {
481 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
482 // Insert the spill to the stack frame.
483 unsigned Reg = CSI[i].getReg();
484 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
485 TII.storeRegToStackSlot(*SaveBlock, I, Reg, true, CSI[i].getFrameIdx(),
489 // Update the live-in information of all the blocks up to the save point.
493 // Restore using target interface.
494 for (MachineBasicBlock *MBB : RestoreBlocks) {
497 // Skip over all terminator instructions, which are part of the return
499 MachineBasicBlock::iterator I2 = I;
500 while (I2 != MBB->begin() && (--I2)->isTerminator())
503 bool AtStart = I == MBB->begin();
504 MachineBasicBlock::iterator BeforeI = I;
508 // Restore all registers immediately before the return and any
509 // terminators that precede it.
510 if (!TFI->restoreCalleeSavedRegisters(*MBB, I, CSI, TRI)) {
511 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
512 unsigned Reg = CSI[i].getReg();
513 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
514 TII.loadRegFromStackSlot(*MBB, I, Reg, CSI[i].getFrameIdx(), RC, TRI);
515 assert(I != MBB->begin() &&
516 "loadRegFromStackSlot didn't insert any code!");
517 // Insert in reverse order. loadRegFromStackSlot can insert
518 // multiple instructions.
530 static void doSpillCalleeSavedRegs(MachineFunction &Fn, RegScavenger *RS,
531 unsigned &MinCSFrameIndex,
532 unsigned &MaxCSFrameIndex,
533 const MBBVector &SaveBlocks,
534 const MBBVector &RestoreBlocks) {
535 const Function *F = Fn.getFunction();
536 const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
537 MinCSFrameIndex = std::numeric_limits<unsigned>::max();
540 // Determine which of the registers in the callee save list should be saved.
542 TFI->determineCalleeSaves(Fn, SavedRegs, RS);
544 // Assign stack slots for any callee-saved registers that must be spilled.
545 assignCalleeSavedSpillSlots(Fn, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex);
547 // Add the code to save and restore the callee saved registers.
548 if (!F->hasFnAttribute(Attribute::Naked))
549 insertCSRSpillsAndRestores(Fn, SaveBlocks, RestoreBlocks);
552 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
554 AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx,
555 bool StackGrowsDown, int64_t &Offset,
556 unsigned &MaxAlign, unsigned Skew) {
557 // If the stack grows down, add the object size to find the lowest address.
559 Offset += MFI->getObjectSize(FrameIdx);
561 unsigned Align = MFI->getObjectAlignment(FrameIdx);
563 // If the alignment of this object is greater than that of the stack, then
564 // increase the stack alignment to match.
565 MaxAlign = std::max(MaxAlign, Align);
567 // Adjust to alignment boundary.
568 Offset = alignTo(Offset, Align, Skew);
570 if (StackGrowsDown) {
571 DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n");
572 MFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
574 DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n");
575 MFI->setObjectOffset(FrameIdx, Offset);
576 Offset += MFI->getObjectSize(FrameIdx);
580 /// Compute which bytes of fixed and callee-save stack area are unused and keep
581 /// track of them in StackBytesFree.
584 computeFreeStackSlots(MachineFrameInfo *MFI, bool StackGrowsDown,
585 unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex,
586 int64_t FixedCSEnd, BitVector &StackBytesFree) {
587 // Avoid undefined int64_t -> int conversion below in extreme case.
588 if (FixedCSEnd > std::numeric_limits<int>::max())
591 StackBytesFree.resize(FixedCSEnd, true);
593 SmallVector<int, 16> AllocatedFrameSlots;
594 // Add fixed objects.
595 for (int i = MFI->getObjectIndexBegin(); i != 0; ++i)
596 AllocatedFrameSlots.push_back(i);
597 // Add callee-save objects.
598 for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i)
599 AllocatedFrameSlots.push_back(i);
601 for (int i : AllocatedFrameSlots) {
602 // These are converted from int64_t, but they should always fit in int
603 // because of the FixedCSEnd check above.
604 int ObjOffset = MFI->getObjectOffset(i);
605 int ObjSize = MFI->getObjectSize(i);
606 int ObjStart, ObjEnd;
607 if (StackGrowsDown) {
608 // ObjOffset is negative when StackGrowsDown is true.
609 ObjStart = -ObjOffset - ObjSize;
612 ObjStart = ObjOffset;
613 ObjEnd = ObjOffset + ObjSize;
615 // Ignore fixed holes that are in the previous stack frame.
617 StackBytesFree.reset(ObjStart, ObjEnd);
621 /// Assign frame object to an unused portion of the stack in the fixed stack
622 /// object range. Return true if the allocation was successful.
624 static inline bool scavengeStackSlot(MachineFrameInfo *MFI, int FrameIdx,
625 bool StackGrowsDown, unsigned MaxAlign,
626 BitVector &StackBytesFree) {
627 if (MFI->isVariableSizedObjectIndex(FrameIdx))
630 if (StackBytesFree.none()) {
631 // clear it to speed up later scavengeStackSlot calls to
632 // StackBytesFree.none()
633 StackBytesFree.clear();
637 unsigned ObjAlign = MFI->getObjectAlignment(FrameIdx);
638 if (ObjAlign > MaxAlign)
641 int64_t ObjSize = MFI->getObjectSize(FrameIdx);
643 for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
644 FreeStart = StackBytesFree.find_next(FreeStart)) {
646 // Check that free space has suitable alignment.
647 unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
648 if (alignTo(ObjStart, ObjAlign) != ObjStart)
651 if (FreeStart + ObjSize > StackBytesFree.size())
654 bool AllBytesFree = true;
655 for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
656 if (!StackBytesFree.test(FreeStart + Byte)) {
657 AllBytesFree = false;
667 if (StackGrowsDown) {
668 int ObjStart = -(FreeStart + ObjSize);
669 DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP[" << ObjStart
671 MFI->setObjectOffset(FrameIdx, ObjStart);
673 DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP[" << FreeStart
675 MFI->setObjectOffset(FrameIdx, FreeStart);
678 StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
682 /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
683 /// those required to be close to the Stack Protector) to stack offsets.
685 AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
686 SmallSet<int, 16> &ProtectedObjs,
687 MachineFrameInfo *MFI, bool StackGrowsDown,
688 int64_t &Offset, unsigned &MaxAlign, unsigned Skew) {
690 for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
691 E = UnassignedObjs.end(); I != E; ++I) {
693 AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew);
694 ProtectedObjs.insert(i);
698 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
699 /// abstract stack objects.
701 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
702 const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
703 StackProtector *SP = &getAnalysis<StackProtector>();
705 bool StackGrowsDown =
706 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
708 // Loop over all of the stack objects, assigning sequential addresses...
709 MachineFrameInfo *MFI = Fn.getFrameInfo();
711 // Start at the beginning of the local area.
712 // The Offset is the distance from the stack top in the direction
713 // of stack growth -- so it's always nonnegative.
714 int LocalAreaOffset = TFI.getOffsetOfLocalArea();
716 LocalAreaOffset = -LocalAreaOffset;
717 assert(LocalAreaOffset >= 0
718 && "Local area offset should be in direction of stack growth");
719 int64_t Offset = LocalAreaOffset;
721 // Skew to be applied to alignment.
722 unsigned Skew = TFI.getStackAlignmentSkew(Fn);
724 // If there are fixed sized objects that are preallocated in the local area,
725 // non-fixed objects can't be allocated right at the start of local area.
726 // Adjust 'Offset' to point to the end of last fixed sized preallocated
728 for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
730 if (StackGrowsDown) {
731 // The maximum distance from the stack pointer is at lower address of
732 // the object -- which is given by offset. For down growing stack
733 // the offset is negative, so we negate the offset to get the distance.
734 FixedOff = -MFI->getObjectOffset(i);
736 // The maximum distance from the start pointer is at the upper
737 // address of the object.
738 FixedOff = MFI->getObjectOffset(i) + MFI->getObjectSize(i);
740 if (FixedOff > Offset) Offset = FixedOff;
743 // First assign frame offsets to stack objects that are used to spill
744 // callee saved registers.
745 if (StackGrowsDown) {
746 for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
747 // If the stack grows down, we need to add the size to find the lowest
748 // address of the object.
749 Offset += MFI->getObjectSize(i);
751 unsigned Align = MFI->getObjectAlignment(i);
752 // Adjust to alignment boundary
753 Offset = alignTo(Offset, Align, Skew);
755 DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << -Offset << "]\n");
756 MFI->setObjectOffset(i, -Offset); // Set the computed offset
758 } else if (MaxCSFrameIndex >= MinCSFrameIndex) {
759 // Be careful about underflow in comparisons agains MinCSFrameIndex.
760 for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) {
761 unsigned Align = MFI->getObjectAlignment(i);
762 // Adjust to alignment boundary
763 Offset = alignTo(Offset, Align, Skew);
765 DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << Offset << "]\n");
766 MFI->setObjectOffset(i, Offset);
767 Offset += MFI->getObjectSize(i);
771 // FixedCSEnd is the stack offset to the end of the fixed and callee-save
773 int64_t FixedCSEnd = Offset;
774 unsigned MaxAlign = MFI->getMaxAlignment();
776 // Make sure the special register scavenging spill slot is closest to the
777 // incoming stack pointer if a frame pointer is required and is closer
778 // to the incoming rather than the final stack pointer.
779 const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
780 bool EarlyScavengingSlots = (TFI.hasFP(Fn) &&
781 TFI.isFPCloseToIncomingSP() &&
782 RegInfo->useFPForScavengingIndex(Fn) &&
783 !RegInfo->needsStackRealignment(Fn));
784 if (RS && EarlyScavengingSlots) {
785 SmallVector<int, 2> SFIs;
786 RS->getScavengingFrameIndices(SFIs);
787 for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
788 IE = SFIs.end(); I != IE; ++I)
789 AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
792 // FIXME: Once this is working, then enable flag will change to a target
793 // check for whether the frame is large enough to want to use virtual
794 // frame index registers. Functions which don't want/need this optimization
795 // will continue to use the existing code path.
796 if (MFI->getUseLocalStackAllocationBlock()) {
797 unsigned Align = MFI->getLocalFrameMaxAlign();
799 // Adjust to alignment boundary.
800 Offset = alignTo(Offset, Align, Skew);
802 DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
804 // Resolve offsets for objects in the local block.
805 for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) {
806 std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i);
807 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
808 DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
810 MFI->setObjectOffset(Entry.first, FIOffset);
812 // Allocate the local block
813 Offset += MFI->getLocalFrameSize();
815 MaxAlign = std::max(Align, MaxAlign);
818 // Retrieve the Exception Handler registration node.
819 int EHRegNodeFrameIndex = INT_MAX;
820 if (const WinEHFuncInfo *FuncInfo = Fn.getWinEHFuncInfo())
821 EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
823 // Make sure that the stack protector comes before the local variables on the
825 SmallSet<int, 16> ProtectedObjs;
826 if (MFI->getStackProtectorIndex() >= 0) {
827 StackObjSet LargeArrayObjs;
828 StackObjSet SmallArrayObjs;
829 StackObjSet AddrOfObjs;
831 AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), StackGrowsDown,
832 Offset, MaxAlign, Skew);
834 // Assign large stack objects first.
835 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
836 if (MFI->isObjectPreAllocated(i) &&
837 MFI->getUseLocalStackAllocationBlock())
839 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
841 if (RS && RS->isScavengingFrameIndex((int)i))
843 if (MFI->isDeadObjectIndex(i))
845 if (MFI->getStackProtectorIndex() == (int)i ||
846 EHRegNodeFrameIndex == (int)i)
849 switch (SP->getSSPLayout(MFI->getObjectAllocation(i))) {
850 case StackProtector::SSPLK_None:
852 case StackProtector::SSPLK_SmallArray:
853 SmallArrayObjs.insert(i);
855 case StackProtector::SSPLK_AddrOf:
856 AddrOfObjs.insert(i);
858 case StackProtector::SSPLK_LargeArray:
859 LargeArrayObjs.insert(i);
862 llvm_unreachable("Unexpected SSPLayoutKind.");
865 AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
866 Offset, MaxAlign, Skew);
867 AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
868 Offset, MaxAlign, Skew);
869 AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
870 Offset, MaxAlign, Skew);
873 SmallVector<int, 8> ObjectsToAllocate;
875 // Then prepare to assign frame offsets to stack objects that are not used to
876 // spill callee saved registers.
877 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
878 if (MFI->isObjectPreAllocated(i) &&
879 MFI->getUseLocalStackAllocationBlock())
881 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
883 if (RS && RS->isScavengingFrameIndex((int)i))
885 if (MFI->isDeadObjectIndex(i))
887 if (MFI->getStackProtectorIndex() == (int)i ||
888 EHRegNodeFrameIndex == (int)i)
890 if (ProtectedObjs.count(i))
893 // Add the objects that we need to allocate to our working set.
894 ObjectsToAllocate.push_back(i);
897 // Allocate the EH registration node first if one is present.
898 if (EHRegNodeFrameIndex != INT_MAX)
899 AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
902 // Give the targets a chance to order the objects the way they like it.
903 if (Fn.getTarget().getOptLevel() != CodeGenOpt::None &&
904 Fn.getTarget().Options.StackSymbolOrdering)
905 TFI.orderFrameObjects(Fn, ObjectsToAllocate);
907 // Keep track of which bytes in the fixed and callee-save range are used so we
908 // can use the holes when allocating later stack objects. Only do this if
909 // stack protector isn't being used and the target requests it and we're
911 BitVector StackBytesFree;
912 if (!ObjectsToAllocate.empty() &&
913 Fn.getTarget().getOptLevel() != CodeGenOpt::None &&
914 MFI->getStackProtectorIndex() < 0 && TFI.enableStackSlotScavenging(Fn))
915 computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex,
916 FixedCSEnd, StackBytesFree);
918 // Now walk the objects and actually assign base offsets to them.
919 for (auto &Object : ObjectsToAllocate)
920 if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
922 AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign, Skew);
924 // Make sure the special register scavenging spill slot is closest to the
926 if (RS && !EarlyScavengingSlots) {
927 SmallVector<int, 2> SFIs;
928 RS->getScavengingFrameIndices(SFIs);
929 for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
930 IE = SFIs.end(); I != IE; ++I)
931 AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
934 if (!TFI.targetHandlesStackFrameRounding()) {
935 // If we have reserved argument space for call sites in the function
936 // immediately on entry to the current function, count it as part of the
937 // overall stack size.
938 if (MFI->adjustsStack() && TFI.hasReservedCallFrame(Fn))
939 Offset += MFI->getMaxCallFrameSize();
941 // Round up the size to a multiple of the alignment. If the function has
942 // any calls or alloca's, align to the target's StackAlignment value to
943 // ensure that the callee's frame or the alloca data is suitably aligned;
944 // otherwise, for leaf functions, align to the TransientStackAlignment
947 if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
948 (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0))
949 StackAlign = TFI.getStackAlignment();
951 StackAlign = TFI.getTransientStackAlignment();
953 // If the frame pointer is eliminated, all frame offsets will be relative to
954 // SP not FP. Align to MaxAlign so this works.
955 StackAlign = std::max(StackAlign, MaxAlign);
956 Offset = alignTo(Offset, StackAlign, Skew);
959 // Update frame info to pretend that this is part of the stack...
960 int64_t StackSize = Offset - LocalAreaOffset;
961 MFI->setStackSize(StackSize);
962 NumBytesStackSpace += StackSize;
965 /// insertPrologEpilogCode - Scan the function for modified callee saved
966 /// registers, insert spill code for these callee saved registers, then add
967 /// prolog and epilog code to the function.
969 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
970 const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
972 // Add prologue to the function...
973 for (MachineBasicBlock *SaveBlock : SaveBlocks)
974 TFI.emitPrologue(Fn, *SaveBlock);
976 // Add epilogue to restore the callee-save registers in each exiting block.
977 for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
978 TFI.emitEpilogue(Fn, *RestoreBlock);
980 for (MachineBasicBlock *SaveBlock : SaveBlocks)
981 TFI.inlineStackProbe(Fn, *SaveBlock);
983 // Emit additional code that is required to support segmented stacks, if
984 // we've been asked for it. This, when linked with a runtime with support
985 // for segmented stacks (libgcc is one), will result in allocating stack
986 // space in small chunks instead of one large contiguous block.
987 if (Fn.shouldSplitStack()) {
988 for (MachineBasicBlock *SaveBlock : SaveBlocks)
989 TFI.adjustForSegmentedStacks(Fn, *SaveBlock);
992 // Emit additional code that is required to explicitly handle the stack in
993 // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
994 // approach is rather similar to that of Segmented Stacks, but it uses a
995 // different conditional check and another BIF for allocating more stack
997 if (Fn.getFunction()->getCallingConv() == CallingConv::HiPE)
998 for (MachineBasicBlock *SaveBlock : SaveBlocks)
999 TFI.adjustForHiPEPrologue(Fn, *SaveBlock);
1002 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1003 /// register references and actual offsets.
1005 void PEI::replaceFrameIndices(MachineFunction &Fn) {
1006 const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
1007 if (!TFI.needsFrameIndexResolution(Fn)) return;
1009 // Store SPAdj at exit of a basic block.
1010 SmallVector<int, 8> SPState;
1011 SPState.resize(Fn.getNumBlockIDs());
1012 SmallPtrSet<MachineBasicBlock*, 8> Reachable;
1014 // Iterate over the reachable blocks in DFS order.
1015 for (auto DFI = df_ext_begin(&Fn, Reachable), DFE = df_ext_end(&Fn, Reachable);
1016 DFI != DFE; ++DFI) {
1018 // Check the exit state of the DFS stack predecessor.
1019 if (DFI.getPathLength() >= 2) {
1020 MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1021 assert(Reachable.count(StackPred) &&
1022 "DFS stack predecessor is already visited.\n");
1023 SPAdj = SPState[StackPred->getNumber()];
1025 MachineBasicBlock *BB = *DFI;
1026 replaceFrameIndices(BB, Fn, SPAdj);
1027 SPState[BB->getNumber()] = SPAdj;
1030 // Handle the unreachable blocks.
1031 for (auto &BB : Fn) {
1032 if (Reachable.count(&BB))
1033 // Already handled in DFS traversal.
1036 replaceFrameIndices(&BB, Fn, SPAdj);
1040 void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn,
1042 assert(Fn.getSubtarget().getRegisterInfo() &&
1043 "getRegisterInfo() must be implemented!");
1044 const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
1045 const TargetRegisterInfo &TRI = *Fn.getSubtarget().getRegisterInfo();
1046 const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
1047 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
1048 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
1050 if (RS && !FrameIndexVirtualScavenging) RS->enterBasicBlock(*BB);
1052 bool InsideCallSequence = false;
1054 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
1056 if (I->getOpcode() == FrameSetupOpcode ||
1057 I->getOpcode() == FrameDestroyOpcode) {
1058 InsideCallSequence = (I->getOpcode() == FrameSetupOpcode);
1059 SPAdj += TII.getSPAdjust(*I);
1061 I = TFI->eliminateCallFramePseudoInstr(Fn, *BB, I);
1065 MachineInstr &MI = *I;
1067 bool DidFinishLoop = true;
1068 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1069 if (!MI.getOperand(i).isFI())
1072 // Frame indices in debug values are encoded in a target independent
1073 // way with simply the frame index and offset rather than any
1074 // target-specific addressing mode.
1075 if (MI.isDebugValue()) {
1076 assert(i == 0 && "Frame indices can only appear as the first "
1077 "operand of a DBG_VALUE machine instruction");
1079 MachineOperand &Offset = MI.getOperand(1);
1082 TFI->getFrameIndexReference(Fn, MI.getOperand(0).getIndex(), Reg));
1083 MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
1087 // TODO: This code should be commoned with the code for
1088 // PATCHPOINT. There's no good reason for the difference in
1089 // implementation other than historical accident. The only
1090 // remaining difference is the unconditional use of the stack
1091 // pointer as the base register.
1092 if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
1093 assert((!MI.isDebugValue() || i == 0) &&
1094 "Frame indicies can only appear as the first operand of a "
1095 "DBG_VALUE machine instruction");
1097 MachineOperand &Offset = MI.getOperand(i + 1);
1098 int refOffset = TFI->getFrameIndexReferencePreferSP(
1099 Fn, MI.getOperand(i).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
1100 Offset.setImm(Offset.getImm() + refOffset);
1101 MI.getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
1105 // Some instructions (e.g. inline asm instructions) can have
1106 // multiple frame indices and/or cause eliminateFrameIndex
1107 // to insert more than one instruction. We need the register
1108 // scavenger to go through all of these instructions so that
1109 // it can update its register information. We keep the
1110 // iterator at the point before insertion so that we can
1111 // revisit them in full.
1112 bool AtBeginning = (I == BB->begin());
1113 if (!AtBeginning) --I;
1115 // If this instruction has a FrameIndex operand, we need to
1116 // use that target machine register info object to eliminate
1118 TRI.eliminateFrameIndex(MI, SPAdj, i,
1119 FrameIndexVirtualScavenging ? nullptr : RS);
1121 // Reset the iterator if we were at the beginning of the BB.
1127 DidFinishLoop = false;
1131 // If we are looking at a call sequence, we need to keep track of
1132 // the SP adjustment made by each instruction in the sequence.
1133 // This includes both the frame setup/destroy pseudos (handled above),
1134 // as well as other instructions that have side effects w.r.t the SP.
1135 // Note that this must come after eliminateFrameIndex, because
1136 // if I itself referred to a frame index, we shouldn't count its own
1138 if (DidFinishLoop && InsideCallSequence)
1139 SPAdj += TII.getSPAdjust(MI);
1141 if (DoIncr && I != BB->end()) ++I;
1143 // Update register states.
1144 if (RS && !FrameIndexVirtualScavenging && DidFinishLoop)
1149 /// doScavengeFrameVirtualRegs - Replace all frame index virtual registers
1150 /// with physical registers. Use the register scavenger to find an
1151 /// appropriate register to use.
1153 /// FIXME: Iterating over the instruction stream is unnecessary. We can simply
1154 /// iterate over the vreg use list, which at this point only contains machine
1155 /// operands for which eliminateFrameIndex need a new scratch reg.
1157 doScavengeFrameVirtualRegs(MachineFunction &MF, RegScavenger *RS) {
1158 // Run through the instructions and find any virtual registers.
1159 MachineRegisterInfo &MRI = MF.getRegInfo();
1160 for (MachineBasicBlock &MBB : MF) {
1161 RS->enterBasicBlock(MBB);
1165 // The instruction stream may change in the loop, so check MBB.end()
1167 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
1168 // We might end up here again with a NULL iterator if we scavenged a
1169 // register for which we inserted spill code for definition by what was
1170 // originally the first instruction in MBB.
1171 if (I == MachineBasicBlock::iterator(nullptr))
1174 const MachineInstr &MI = *I;
1175 MachineBasicBlock::iterator J = std::next(I);
1176 MachineBasicBlock::iterator P =
1177 I == MBB.begin() ? MachineBasicBlock::iterator(nullptr)
1180 // RS should process this instruction before we might scavenge at this
1181 // location. This is because we might be replacing a virtual register
1182 // defined by this instruction, and if so, registers killed by this
1183 // instruction are available, and defined registers are not.
1186 for (const MachineOperand &MO : MI.operands()) {
1189 unsigned Reg = MO.getReg();
1190 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1193 // When we first encounter a new virtual register, it
1194 // must be a definition.
1195 assert(MO.isDef() && "frame index virtual missing def!");
1196 // Scavenge a new scratch register
1197 const TargetRegisterClass *RC = MRI.getRegClass(Reg);
1198 unsigned ScratchReg = RS->scavengeRegister(RC, J, SPAdj);
1202 // Replace this reference to the virtual register with the
1203 // scratch register.
1204 assert(ScratchReg && "Missing scratch register!");
1205 MRI.replaceRegWith(Reg, ScratchReg);
1207 // Because this instruction was processed by the RS before this
1208 // register was allocated, make sure that the RS now records the
1209 // register as being used.
1210 RS->setRegUsed(ScratchReg);
1213 // If the scavenger needed to use one of its spill slots, the
1214 // spill code will have been inserted in between I and J. This is a
1215 // problem because we need the spill code before I: Move I to just
1217 if (I != std::prev(J)) {
1218 MBB.splice(J, &MBB, I);
1220 // Before we move I, we need to prepare the RS to visit I again.
1221 // Specifically, RS will assert if it sees uses of registers that
1222 // it believes are undefined. Because we have already processed
1223 // register kills in I, when it visits I again, it will believe that
1224 // those registers are undefined. To avoid this situation, unprocess
1225 // the instruction I.
1226 assert(RS->getCurrentPosition() == I &&
1227 "The register scavenger has an unexpected position");