1 //===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass is responsible for finalizing the functions frame layout, saving
10 // callee saved registers, and for emitting prolog & epilog code for the
13 // This pass must be run after register allocation. After this pass is
14 // executed, it is illegal to construct MO_FrameIndex operands.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/BitVector.h"
20 #include "llvm/ADT/DepthFirstIterator.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
28 #include "llvm/CodeGen/MachineBasicBlock.h"
29 #include "llvm/CodeGen/MachineDominators.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineLoopInfo.h"
36 #include "llvm/CodeGen/MachineModuleInfo.h"
37 #include "llvm/CodeGen/MachineOperand.h"
38 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
39 #include "llvm/CodeGen/MachineRegisterInfo.h"
40 #include "llvm/CodeGen/RegisterScavenging.h"
41 #include "llvm/CodeGen/TargetFrameLowering.h"
42 #include "llvm/CodeGen/TargetInstrInfo.h"
43 #include "llvm/CodeGen/TargetOpcodes.h"
44 #include "llvm/CodeGen/TargetRegisterInfo.h"
45 #include "llvm/CodeGen/TargetSubtargetInfo.h"
46 #include "llvm/CodeGen/WinEHFuncInfo.h"
47 #include "llvm/IR/Attributes.h"
48 #include "llvm/IR/CallingConv.h"
49 #include "llvm/IR/DebugInfoMetadata.h"
50 #include "llvm/IR/DiagnosticInfo.h"
51 #include "llvm/IR/Function.h"
52 #include "llvm/IR/InlineAsm.h"
53 #include "llvm/IR/LLVMContext.h"
54 #include "llvm/InitializePasses.h"
55 #include "llvm/MC/MCRegisterInfo.h"
56 #include "llvm/Pass.h"
57 #include "llvm/Support/CodeGen.h"
58 #include "llvm/Support/CommandLine.h"
59 #include "llvm/Support/Debug.h"
60 #include "llvm/Support/ErrorHandling.h"
61 #include "llvm/Support/MathExtras.h"
62 #include "llvm/Support/raw_ostream.h"
63 #include "llvm/Target/TargetMachine.h"
64 #include "llvm/Target/TargetOptions.h"
75 #define DEBUG_TYPE "prologepilog"
77 using MBBVector = SmallVector<MachineBasicBlock *, 4>;
79 STATISTIC(NumLeafFuncWithSpills, "Number of leaf functions with CSRs");
80 STATISTIC(NumFuncSeen, "Number of functions seen in PEI");
85 class PEI : public MachineFunctionPass {
89 PEI() : MachineFunctionPass(ID) {
90 initializePEIPass(*PassRegistry::getPassRegistry());
93 void getAnalysisUsage(AnalysisUsage &AU) const override;
95 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
96 /// frame indexes with appropriate references.
97 bool runOnMachineFunction(MachineFunction &MF) override;
102 // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
103 // stack frame indexes.
104 unsigned MinCSFrameIndex = std::numeric_limits<unsigned>::max();
105 unsigned MaxCSFrameIndex = 0;
107 // Save and Restore blocks of the current function. Typically there is a
108 // single save block, unless Windows EH funclets are involved.
109 MBBVector SaveBlocks;
110 MBBVector RestoreBlocks;
112 // Flag to control whether to use the register scavenger to resolve
113 // frame index materialization registers. Set according to
114 // TRI->requiresFrameIndexScavenging() for the current function.
115 bool FrameIndexVirtualScavenging;
117 // Flag to control whether the scavenger should be passed even though
118 // FrameIndexVirtualScavenging is used.
119 bool FrameIndexEliminationScavenging;
122 MachineOptimizationRemarkEmitter *ORE = nullptr;
124 void calculateCallFrameInfo(MachineFunction &MF);
125 void calculateSaveRestoreBlocks(MachineFunction &MF);
126 void spillCalleeSavedRegs(MachineFunction &MF);
128 void calculateFrameObjectOffsets(MachineFunction &MF);
129 void replaceFrameIndices(MachineFunction &MF);
130 void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
132 void insertPrologEpilogCode(MachineFunction &MF);
135 } // end anonymous namespace
139 char &llvm::PrologEpilogCodeInserterID = PEI::ID;
141 static cl::opt<unsigned>
142 WarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1),
143 cl::desc("Warn for stack size bigger than the given"
146 INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false,
148 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
149 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
150 INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
151 INITIALIZE_PASS_END(PEI, DEBUG_TYPE,
152 "Prologue/Epilogue Insertion & Frame Finalization", false,
155 MachineFunctionPass *llvm::createPrologEpilogInserterPass() {
159 STATISTIC(NumBytesStackSpace,
160 "Number of bytes used for stack in all functions");
162 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
163 AU.setPreservesCFG();
164 AU.addPreserved<MachineLoopInfo>();
165 AU.addPreserved<MachineDominatorTree>();
166 AU.addRequired<MachineOptimizationRemarkEmitterPass>();
167 MachineFunctionPass::getAnalysisUsage(AU);
170 /// StackObjSet - A set of stack object indexes
171 using StackObjSet = SmallSetVector<int, 8>;
173 using SavedDbgValuesMap =
174 SmallDenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 4>, 4>;
176 /// Stash DBG_VALUEs that describe parameters and which are placed at the start
177 /// of the block. Later on, after the prologue code has been emitted, the
178 /// stashed DBG_VALUEs will be reinserted at the start of the block.
179 static void stashEntryDbgValues(MachineBasicBlock &MBB,
180 SavedDbgValuesMap &EntryDbgValues) {
181 SmallVector<const MachineInstr *, 4> FrameIndexValues;
183 for (auto &MI : MBB) {
184 if (!MI.isDebugInstr())
186 if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter())
188 if (MI.getDebugOperand(0).isFI()) {
189 // We can only emit valid locations for frame indices after the frame
190 // setup, so do not stash away them.
191 FrameIndexValues.push_back(&MI);
194 const DILocalVariable *Var = MI.getDebugVariable();
195 const DIExpression *Expr = MI.getDebugExpression();
196 auto Overlaps = [Var, Expr](const MachineInstr *DV) {
197 return Var == DV->getDebugVariable() &&
198 Expr->fragmentsOverlap(DV->getDebugExpression());
200 // See if the debug value overlaps with any preceding debug value that will
201 // not be stashed. If that is the case, then we can't stash this value, as
202 // we would then reorder the values at reinsertion.
203 if (llvm::none_of(FrameIndexValues, Overlaps))
204 EntryDbgValues[&MBB].push_back(&MI);
207 // Remove stashed debug values from the block.
208 if (EntryDbgValues.count(&MBB))
209 for (auto *MI : EntryDbgValues[&MBB])
210 MI->removeFromParent();
213 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
214 /// frame indexes with appropriate references.
215 bool PEI::runOnMachineFunction(MachineFunction &MF) {
217 const Function &F = MF.getFunction();
218 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
219 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
221 RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
222 FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
223 ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
225 // Calculate the MaxCallFrameSize and AdjustsStack variables for the
226 // function's frame information. Also eliminates call frame pseudo
228 calculateCallFrameInfo(MF);
230 // Determine placement of CSR spill/restore code and prolog/epilog code:
231 // place all spills in the entry block, all restores in return blocks.
232 calculateSaveRestoreBlocks(MF);
234 // Stash away DBG_VALUEs that should not be moved by insertion of prolog code.
235 SavedDbgValuesMap EntryDbgValues;
236 for (MachineBasicBlock *SaveBlock : SaveBlocks)
237 stashEntryDbgValues(*SaveBlock, EntryDbgValues);
239 // Handle CSR spilling and restoring, for targets that need it.
240 if (MF.getTarget().usesPhysRegsForValues())
241 spillCalleeSavedRegs(MF);
243 // Allow the target machine to make final modifications to the function
244 // before the frame layout is finalized.
245 TFI->processFunctionBeforeFrameFinalized(MF, RS);
247 // Calculate actual frame offsets for all abstract stack objects...
248 calculateFrameObjectOffsets(MF);
250 // Add prolog and epilog code to the function. This function is required
251 // to align the stack frame as necessary for any stack variables or
252 // called functions. Because of this, calculateCalleeSavedRegisters()
253 // must be called before this function in order to set the AdjustsStack
254 // and MaxCallFrameSize variables.
255 if (!F.hasFnAttribute(Attribute::Naked))
256 insertPrologEpilogCode(MF);
258 // Reinsert stashed debug values at the start of the entry blocks.
259 for (auto &I : EntryDbgValues)
260 I.first->insert(I.first->begin(), I.second.begin(), I.second.end());
262 // Allow the target machine to make final modifications to the function
263 // before the frame layout is finalized.
264 TFI->processFunctionBeforeFrameIndicesReplaced(MF, RS);
266 // Replace all MO_FrameIndex operands with physical register references
267 // and actual offsets.
269 replaceFrameIndices(MF);
271 // If register scavenging is needed, as we've enabled doing it as a
272 // post-pass, scavenge the virtual registers that frame index elimination
274 if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
275 scavengeFrameVirtualRegs(MF, *RS);
277 // Warn on stack size when we exceeds the given limit.
278 MachineFrameInfo &MFI = MF.getFrameInfo();
279 uint64_t StackSize = MFI.getStackSize();
280 if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
281 DiagnosticInfoStackSize DiagStackSize(F, StackSize);
282 F.getContext().diagnose(DiagStackSize);
285 return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
286 MF.getFunction().getSubprogram(),
288 << ore::NV("NumStackBytes", StackSize) << " stack bytes in function";
293 RestoreBlocks.clear();
294 MFI.setSavePoint(nullptr);
295 MFI.setRestorePoint(nullptr);
299 /// Calculate the MaxCallFrameSize and AdjustsStack
300 /// variables for the function's frame information and eliminate call frame
301 /// pseudo instructions.
302 void PEI::calculateCallFrameInfo(MachineFunction &MF) {
303 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
304 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
305 MachineFrameInfo &MFI = MF.getFrameInfo();
307 unsigned MaxCallFrameSize = 0;
308 bool AdjustsStack = MFI.adjustsStack();
310 // Get the function call frame set-up and tear-down instruction opcode
311 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
312 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
314 // Early exit for targets which have no call frame setup/destroy pseudo
316 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
319 std::vector<MachineBasicBlock::iterator> FrameSDOps;
320 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
321 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
322 if (TII.isFrameInstr(*I)) {
323 unsigned Size = TII.getFrameSize(*I);
324 if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
326 FrameSDOps.push_back(I);
327 } else if (I->isInlineAsm()) {
328 // Some inline asm's need a stack frame, as indicated by operand 1.
329 unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
330 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
334 assert(!MFI.isMaxCallFrameSizeComputed() ||
335 (MFI.getMaxCallFrameSize() == MaxCallFrameSize &&
336 MFI.adjustsStack() == AdjustsStack));
337 MFI.setAdjustsStack(AdjustsStack);
338 MFI.setMaxCallFrameSize(MaxCallFrameSize);
340 for (std::vector<MachineBasicBlock::iterator>::iterator
341 i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
342 MachineBasicBlock::iterator I = *i;
344 // If call frames are not being included as part of the stack frame, and
345 // the target doesn't indicate otherwise, remove the call frame pseudos
346 // here. The sub/add sp instruction pairs are still inserted, but we don't
347 // need to track the SP adjustment for frame index elimination.
348 if (TFI->canSimplifyCallFramePseudos(MF))
349 TFI->eliminateCallFramePseudoInstr(MF, *I->getParent(), I);
353 /// Compute the sets of entry and return blocks for saving and restoring
354 /// callee-saved registers, and placing prolog and epilog code.
355 void PEI::calculateSaveRestoreBlocks(MachineFunction &MF) {
356 const MachineFrameInfo &MFI = MF.getFrameInfo();
358 // Even when we do not change any CSR, we still want to insert the
359 // prologue and epilogue of the function.
360 // So set the save points for those.
362 // Use the points found by shrink-wrapping, if any.
363 if (MFI.getSavePoint()) {
364 SaveBlocks.push_back(MFI.getSavePoint());
365 assert(MFI.getRestorePoint() && "Both restore and save must be set");
366 MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
367 // If RestoreBlock does not have any successor and is not a return block
368 // then the end point is unreachable and we do not need to insert any
370 if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
371 RestoreBlocks.push_back(RestoreBlock);
375 // Save refs to entry and return blocks.
376 SaveBlocks.push_back(&MF.front());
377 for (MachineBasicBlock &MBB : MF) {
378 if (MBB.isEHFuncletEntry())
379 SaveBlocks.push_back(&MBB);
380 if (MBB.isReturnBlock())
381 RestoreBlocks.push_back(&MBB);
385 static void assignCalleeSavedSpillSlots(MachineFunction &F,
386 const BitVector &SavedRegs,
387 unsigned &MinCSFrameIndex,
388 unsigned &MaxCSFrameIndex) {
389 if (SavedRegs.empty())
392 const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
393 const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
395 std::vector<CalleeSavedInfo> CSI;
396 for (unsigned i = 0; CSRegs[i]; ++i) {
397 unsigned Reg = CSRegs[i];
398 if (SavedRegs.test(Reg))
399 CSI.push_back(CalleeSavedInfo(Reg));
402 const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
403 MachineFrameInfo &MFI = F.getFrameInfo();
404 if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
405 // If target doesn't implement this, use generic code.
408 return; // Early exit if no callee saved registers are modified!
410 unsigned NumFixedSpillSlots;
411 const TargetFrameLowering::SpillSlot *FixedSpillSlots =
412 TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
414 // Now that we know which registers need to be saved and restored, allocate
415 // stack slots for them.
416 for (auto &CS : CSI) {
417 // If the target has spilled this register to another register, we don't
418 // need to allocate a stack slot.
419 if (CS.isSpilledToReg())
422 unsigned Reg = CS.getReg();
423 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
426 if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
427 CS.setFrameIdx(FrameIdx);
431 // Check to see if this physreg must be spilled to a particular stack slot
433 const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
434 while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
435 FixedSlot->Reg != Reg)
438 unsigned Size = RegInfo->getSpillSize(*RC);
439 if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
440 // Nope, just spill it anywhere convenient.
441 Align Alignment(RegInfo->getSpillAlignment(*RC));
442 // We may not be able to satisfy the desired alignment specification of
443 // the TargetRegisterClass if the stack alignment is smaller. Use the
445 Alignment = std::min(Alignment, TFI->getStackAlign());
446 FrameIdx = MFI.CreateStackObject(Size, Alignment, true);
447 if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
448 if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
450 // Spill it to the stack where we must.
451 FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset);
454 CS.setFrameIdx(FrameIdx);
458 MFI.setCalleeSavedInfo(CSI);
461 /// Helper function to update the liveness information for the callee-saved
463 static void updateLiveness(MachineFunction &MF) {
464 MachineFrameInfo &MFI = MF.getFrameInfo();
465 // Visited will contain all the basic blocks that are in the region
466 // where the callee saved registers are alive:
467 // - Anything that is not Save or Restore -> LiveThrough.
469 // - Restore -> LiveOut.
470 // The live-out is not attached to the block, so no need to keep
471 // Restore in this set.
472 SmallPtrSet<MachineBasicBlock *, 8> Visited;
473 SmallVector<MachineBasicBlock *, 8> WorkList;
474 MachineBasicBlock *Entry = &MF.front();
475 MachineBasicBlock *Save = MFI.getSavePoint();
481 WorkList.push_back(Entry);
482 Visited.insert(Entry);
484 Visited.insert(Save);
486 MachineBasicBlock *Restore = MFI.getRestorePoint();
488 // By construction Restore cannot be visited, otherwise it
489 // means there exists a path to Restore that does not go
491 WorkList.push_back(Restore);
493 while (!WorkList.empty()) {
494 const MachineBasicBlock *CurBB = WorkList.pop_back_val();
495 // By construction, the region that is after the save point is
496 // dominated by the Save and post-dominated by the Restore.
497 if (CurBB == Save && Save != Restore)
499 // Enqueue all the successors not already visited.
500 // Those are by construction either before Save or after Restore.
501 for (MachineBasicBlock *SuccBB : CurBB->successors())
502 if (Visited.insert(SuccBB).second)
503 WorkList.push_back(SuccBB);
506 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
508 MachineRegisterInfo &MRI = MF.getRegInfo();
509 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
510 for (MachineBasicBlock *MBB : Visited) {
511 MCPhysReg Reg = CSI[i].getReg();
512 // Add the callee-saved register as live-in.
513 // It's killed at the spill.
514 if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg))
517 // If callee-saved register is spilled to another register rather than
518 // spilling to stack, the destination register has to be marked as live for
519 // each MBB between the prologue and epilogue so that it is not clobbered
520 // before it is reloaded in the epilogue. The Visited set contains all
521 // blocks outside of the region delimited by prologue/epilogue.
522 if (CSI[i].isSpilledToReg()) {
523 for (MachineBasicBlock &MBB : MF) {
524 if (Visited.count(&MBB))
526 MCPhysReg DstReg = CSI[i].getDstReg();
527 if (!MBB.isLiveIn(DstReg))
528 MBB.addLiveIn(DstReg);
535 /// Insert restore code for the callee-saved registers used in the function.
536 static void insertCSRSaves(MachineBasicBlock &SaveBlock,
537 ArrayRef<CalleeSavedInfo> CSI) {
538 MachineFunction &MF = *SaveBlock.getParent();
539 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
540 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
541 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
543 MachineBasicBlock::iterator I = SaveBlock.begin();
544 if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
545 for (const CalleeSavedInfo &CS : CSI) {
546 // Insert the spill to the stack frame.
547 unsigned Reg = CS.getReg();
549 if (CS.isSpilledToReg()) {
550 BuildMI(SaveBlock, I, DebugLoc(),
551 TII.get(TargetOpcode::COPY), CS.getDstReg())
552 .addReg(Reg, getKillRegState(true));
554 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
555 TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC,
562 /// Insert restore code for the callee-saved registers used in the function.
563 static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
564 std::vector<CalleeSavedInfo> &CSI) {
565 MachineFunction &MF = *RestoreBlock.getParent();
566 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
567 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
568 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
570 // Restore all registers immediately before the return and any
571 // terminators that precede it.
572 MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator();
574 if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
575 for (const CalleeSavedInfo &CI : reverse(CSI)) {
576 unsigned Reg = CI.getReg();
577 if (CI.isSpilledToReg()) {
578 BuildMI(RestoreBlock, I, DebugLoc(), TII.get(TargetOpcode::COPY), Reg)
579 .addReg(CI.getDstReg(), getKillRegState(true));
581 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
582 TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
583 assert(I != RestoreBlock.begin() &&
584 "loadRegFromStackSlot didn't insert any code!");
585 // Insert in reverse order. loadRegFromStackSlot can insert
586 // multiple instructions.
592 void PEI::spillCalleeSavedRegs(MachineFunction &MF) {
593 // We can't list this requirement in getRequiredProperties because some
594 // targets (WebAssembly) use virtual registers past this point, and the pass
595 // pipeline is set up without giving the passes a chance to look at the
597 // FIXME: Find a way to express this in getRequiredProperties.
598 assert(MF.getProperties().hasProperty(
599 MachineFunctionProperties::Property::NoVRegs));
601 const Function &F = MF.getFunction();
602 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
603 MachineFrameInfo &MFI = MF.getFrameInfo();
604 MinCSFrameIndex = std::numeric_limits<unsigned>::max();
607 // Determine which of the registers in the callee save list should be saved.
609 TFI->determineCalleeSaves(MF, SavedRegs, RS);
611 // Assign stack slots for any callee-saved registers that must be spilled.
612 assignCalleeSavedSpillSlots(MF, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex);
614 // Add the code to save and restore the callee saved registers.
615 if (!F.hasFnAttribute(Attribute::Naked)) {
616 MFI.setCalleeSavedInfoValid(true);
618 std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
621 NumLeafFuncWithSpills++;
623 for (MachineBasicBlock *SaveBlock : SaveBlocks) {
624 insertCSRSaves(*SaveBlock, CSI);
625 // Update the live-in information of all the blocks up to the save
629 for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
630 insertCSRRestores(*RestoreBlock, CSI);
635 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
636 static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
637 bool StackGrowsDown, int64_t &Offset,
638 Align &MaxAlign, unsigned Skew) {
639 // If the stack grows down, add the object size to find the lowest address.
641 Offset += MFI.getObjectSize(FrameIdx);
643 Align Alignment = MFI.getObjectAlign(FrameIdx);
645 // If the alignment of this object is greater than that of the stack, then
646 // increase the stack alignment to match.
647 MaxAlign = std::max(MaxAlign, Alignment);
649 // Adjust to alignment boundary.
650 Offset = alignTo(Offset, Alignment, Skew);
652 if (StackGrowsDown) {
653 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
655 MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
657 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
659 MFI.setObjectOffset(FrameIdx, Offset);
660 Offset += MFI.getObjectSize(FrameIdx);
664 /// Compute which bytes of fixed and callee-save stack area are unused and keep
665 /// track of them in StackBytesFree.
667 computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown,
668 unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex,
669 int64_t FixedCSEnd, BitVector &StackBytesFree) {
670 // Avoid undefined int64_t -> int conversion below in extreme case.
671 if (FixedCSEnd > std::numeric_limits<int>::max())
674 StackBytesFree.resize(FixedCSEnd, true);
676 SmallVector<int, 16> AllocatedFrameSlots;
677 // Add fixed objects.
678 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
679 // StackSlot scavenging is only implemented for the default stack.
680 if (MFI.getStackID(i) == TargetStackID::Default)
681 AllocatedFrameSlots.push_back(i);
682 // Add callee-save objects.
683 for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i)
684 if (MFI.getStackID(i) == TargetStackID::Default)
685 AllocatedFrameSlots.push_back(i);
687 for (int i : AllocatedFrameSlots) {
688 // These are converted from int64_t, but they should always fit in int
689 // because of the FixedCSEnd check above.
690 int ObjOffset = MFI.getObjectOffset(i);
691 int ObjSize = MFI.getObjectSize(i);
692 int ObjStart, ObjEnd;
693 if (StackGrowsDown) {
694 // ObjOffset is negative when StackGrowsDown is true.
695 ObjStart = -ObjOffset - ObjSize;
698 ObjStart = ObjOffset;
699 ObjEnd = ObjOffset + ObjSize;
701 // Ignore fixed holes that are in the previous stack frame.
703 StackBytesFree.reset(ObjStart, ObjEnd);
707 /// Assign frame object to an unused portion of the stack in the fixed stack
708 /// object range. Return true if the allocation was successful.
709 static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
710 bool StackGrowsDown, Align MaxAlign,
711 BitVector &StackBytesFree) {
712 if (MFI.isVariableSizedObjectIndex(FrameIdx))
715 if (StackBytesFree.none()) {
716 // clear it to speed up later scavengeStackSlot calls to
717 // StackBytesFree.none()
718 StackBytesFree.clear();
722 Align ObjAlign = MFI.getObjectAlign(FrameIdx);
723 if (ObjAlign > MaxAlign)
726 int64_t ObjSize = MFI.getObjectSize(FrameIdx);
728 for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
729 FreeStart = StackBytesFree.find_next(FreeStart)) {
731 // Check that free space has suitable alignment.
732 unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
733 if (alignTo(ObjStart, ObjAlign) != ObjStart)
736 if (FreeStart + ObjSize > StackBytesFree.size())
739 bool AllBytesFree = true;
740 for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
741 if (!StackBytesFree.test(FreeStart + Byte)) {
742 AllBytesFree = false;
752 if (StackGrowsDown) {
753 int ObjStart = -(FreeStart + ObjSize);
754 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
755 << ObjStart << "]\n");
756 MFI.setObjectOffset(FrameIdx, ObjStart);
758 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
759 << FreeStart << "]\n");
760 MFI.setObjectOffset(FrameIdx, FreeStart);
763 StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
767 /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
768 /// those required to be close to the Stack Protector) to stack offsets.
769 static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
770 SmallSet<int, 16> &ProtectedObjs,
771 MachineFrameInfo &MFI, bool StackGrowsDown,
772 int64_t &Offset, Align &MaxAlign,
775 for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
776 E = UnassignedObjs.end(); I != E; ++I) {
778 AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew);
779 ProtectedObjs.insert(i);
783 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
784 /// abstract stack objects.
785 void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
786 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
788 bool StackGrowsDown =
789 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
791 // Loop over all of the stack objects, assigning sequential addresses...
792 MachineFrameInfo &MFI = MF.getFrameInfo();
794 // Start at the beginning of the local area.
795 // The Offset is the distance from the stack top in the direction
796 // of stack growth -- so it's always nonnegative.
797 int LocalAreaOffset = TFI.getOffsetOfLocalArea();
799 LocalAreaOffset = -LocalAreaOffset;
800 assert(LocalAreaOffset >= 0
801 && "Local area offset should be in direction of stack growth");
802 int64_t Offset = LocalAreaOffset;
804 // Skew to be applied to alignment.
805 unsigned Skew = TFI.getStackAlignmentSkew(MF);
807 #ifdef EXPENSIVE_CHECKS
808 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i)
809 if (!MFI.isDeadObjectIndex(i) &&
810 MFI.getStackID(i) == TargetStackID::Default)
811 assert(MFI.getObjectAlign(i) <= MFI.getMaxAlign() &&
812 "MaxAlignment is invalid");
815 // If there are fixed sized objects that are preallocated in the local area,
816 // non-fixed objects can't be allocated right at the start of local area.
817 // Adjust 'Offset' to point to the end of last fixed sized preallocated
819 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
820 if (MFI.getStackID(i) !=
821 TargetStackID::Default) // Only allocate objects on the default stack.
825 if (StackGrowsDown) {
826 // The maximum distance from the stack pointer is at lower address of
827 // the object -- which is given by offset. For down growing stack
828 // the offset is negative, so we negate the offset to get the distance.
829 FixedOff = -MFI.getObjectOffset(i);
831 // The maximum distance from the start pointer is at the upper
832 // address of the object.
833 FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
835 if (FixedOff > Offset) Offset = FixedOff;
838 // First assign frame offsets to stack objects that are used to spill
839 // callee saved registers.
840 if (StackGrowsDown) {
841 for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
842 if (MFI.getStackID(i) !=
843 TargetStackID::Default) // Only allocate objects on the default stack.
846 // If the stack grows down, we need to add the size to find the lowest
847 // address of the object.
848 Offset += MFI.getObjectSize(i);
850 // Adjust to alignment boundary
851 Offset = alignTo(Offset, MFI.getObjectAlign(i), Skew);
853 LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << -Offset << "]\n");
854 MFI.setObjectOffset(i, -Offset); // Set the computed offset
856 } else if (MaxCSFrameIndex >= MinCSFrameIndex) {
857 // Be careful about underflow in comparisons agains MinCSFrameIndex.
858 for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) {
859 if (MFI.getStackID(i) !=
860 TargetStackID::Default) // Only allocate objects on the default stack.
863 if (MFI.isDeadObjectIndex(i))
866 // Adjust to alignment boundary
867 Offset = alignTo(Offset, MFI.getObjectAlign(i), Skew);
869 LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << Offset << "]\n");
870 MFI.setObjectOffset(i, Offset);
871 Offset += MFI.getObjectSize(i);
875 // FixedCSEnd is the stack offset to the end of the fixed and callee-save
877 int64_t FixedCSEnd = Offset;
878 Align MaxAlign = MFI.getMaxAlign();
880 // Make sure the special register scavenging spill slot is closest to the
881 // incoming stack pointer if a frame pointer is required and is closer
882 // to the incoming rather than the final stack pointer.
883 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
884 bool EarlyScavengingSlots = (TFI.hasFP(MF) &&
885 TFI.isFPCloseToIncomingSP() &&
886 RegInfo->useFPForScavengingIndex(MF) &&
887 !RegInfo->needsStackRealignment(MF));
888 if (RS && EarlyScavengingSlots) {
889 SmallVector<int, 2> SFIs;
890 RS->getScavengingFrameIndices(SFIs);
891 for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
892 IE = SFIs.end(); I != IE; ++I)
893 AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
896 // FIXME: Once this is working, then enable flag will change to a target
897 // check for whether the frame is large enough to want to use virtual
898 // frame index registers. Functions which don't want/need this optimization
899 // will continue to use the existing code path.
900 if (MFI.getUseLocalStackAllocationBlock()) {
901 Align Alignment = MFI.getLocalFrameMaxAlign();
903 // Adjust to alignment boundary.
904 Offset = alignTo(Offset, Alignment, Skew);
906 LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
908 // Resolve offsets for objects in the local block.
909 for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
910 std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
911 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
912 LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
914 MFI.setObjectOffset(Entry.first, FIOffset);
916 // Allocate the local block
917 Offset += MFI.getLocalFrameSize();
919 MaxAlign = std::max(Alignment, MaxAlign);
922 // Retrieve the Exception Handler registration node.
923 int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
924 if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo())
925 EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
927 // Make sure that the stack protector comes before the local variables on the
929 SmallSet<int, 16> ProtectedObjs;
930 if (MFI.hasStackProtectorIndex()) {
931 int StackProtectorFI = MFI.getStackProtectorIndex();
932 StackObjSet LargeArrayObjs;
933 StackObjSet SmallArrayObjs;
934 StackObjSet AddrOfObjs;
936 // If we need a stack protector, we need to make sure that
937 // LocalStackSlotPass didn't already allocate a slot for it.
938 // If we are told to use the LocalStackAllocationBlock, the stack protector
939 // is expected to be already pre-allocated.
940 if (!MFI.getUseLocalStackAllocationBlock())
941 AdjustStackOffset(MFI, StackProtectorFI, StackGrowsDown, Offset, MaxAlign,
943 else if (!MFI.isObjectPreAllocated(MFI.getStackProtectorIndex()))
945 "Stack protector not pre-allocated by LocalStackSlotPass.");
947 // Assign large stack objects first.
948 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
949 if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock())
951 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
953 if (RS && RS->isScavengingFrameIndex((int)i))
955 if (MFI.isDeadObjectIndex(i))
957 if (StackProtectorFI == (int)i || EHRegNodeFrameIndex == (int)i)
959 if (MFI.getStackID(i) !=
960 TargetStackID::Default) // Only allocate objects on the default stack.
963 switch (MFI.getObjectSSPLayout(i)) {
964 case MachineFrameInfo::SSPLK_None:
966 case MachineFrameInfo::SSPLK_SmallArray:
967 SmallArrayObjs.insert(i);
969 case MachineFrameInfo::SSPLK_AddrOf:
970 AddrOfObjs.insert(i);
972 case MachineFrameInfo::SSPLK_LargeArray:
973 LargeArrayObjs.insert(i);
976 llvm_unreachable("Unexpected SSPLayoutKind.");
979 // We expect **all** the protected stack objects to be pre-allocated by
980 // LocalStackSlotPass. If it turns out that PEI still has to allocate some
981 // of them, we may end up messing up the expected order of the objects.
982 if (MFI.getUseLocalStackAllocationBlock() &&
983 !(LargeArrayObjs.empty() && SmallArrayObjs.empty() &&
985 llvm_unreachable("Found protected stack objects not pre-allocated by "
986 "LocalStackSlotPass.");
988 AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
989 Offset, MaxAlign, Skew);
990 AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
991 Offset, MaxAlign, Skew);
992 AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
993 Offset, MaxAlign, Skew);
996 SmallVector<int, 8> ObjectsToAllocate;
998 // Then prepare to assign frame offsets to stack objects that are not used to
999 // spill callee saved registers.
1000 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
1001 if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock())
1003 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
1005 if (RS && RS->isScavengingFrameIndex((int)i))
1007 if (MFI.isDeadObjectIndex(i))
1009 if (MFI.getStackProtectorIndex() == (int)i || EHRegNodeFrameIndex == (int)i)
1011 if (ProtectedObjs.count(i))
1013 if (MFI.getStackID(i) !=
1014 TargetStackID::Default) // Only allocate objects on the default stack.
1017 // Add the objects that we need to allocate to our working set.
1018 ObjectsToAllocate.push_back(i);
1021 // Allocate the EH registration node first if one is present.
1022 if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
1023 AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
1026 // Give the targets a chance to order the objects the way they like it.
1027 if (MF.getTarget().getOptLevel() != CodeGenOpt::None &&
1028 MF.getTarget().Options.StackSymbolOrdering)
1029 TFI.orderFrameObjects(MF, ObjectsToAllocate);
1031 // Keep track of which bytes in the fixed and callee-save range are used so we
1032 // can use the holes when allocating later stack objects. Only do this if
1033 // stack protector isn't being used and the target requests it and we're
1035 BitVector StackBytesFree;
1036 if (!ObjectsToAllocate.empty() &&
1037 MF.getTarget().getOptLevel() != CodeGenOpt::None &&
1038 MFI.getStackProtectorIndex() < 0 && TFI.enableStackSlotScavenging(MF))
1039 computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex,
1040 FixedCSEnd, StackBytesFree);
1042 // Now walk the objects and actually assign base offsets to them.
1043 for (auto &Object : ObjectsToAllocate)
1044 if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
1046 AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign, Skew);
1048 // Make sure the special register scavenging spill slot is closest to the
1050 if (RS && !EarlyScavengingSlots) {
1051 SmallVector<int, 2> SFIs;
1052 RS->getScavengingFrameIndices(SFIs);
1053 for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
1054 IE = SFIs.end(); I != IE; ++I)
1055 AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
1058 if (!TFI.targetHandlesStackFrameRounding()) {
1059 // If we have reserved argument space for call sites in the function
1060 // immediately on entry to the current function, count it as part of the
1061 // overall stack size.
1062 if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF))
1063 Offset += MFI.getMaxCallFrameSize();
1065 // Round up the size to a multiple of the alignment. If the function has
1066 // any calls or alloca's, align to the target's StackAlignment value to
1067 // ensure that the callee's frame or the alloca data is suitably aligned;
1068 // otherwise, for leaf functions, align to the TransientStackAlignment
1071 if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
1072 (RegInfo->needsStackRealignment(MF) && MFI.getObjectIndexEnd() != 0))
1073 StackAlign = TFI.getStackAlign();
1075 StackAlign = TFI.getTransientStackAlign();
1077 // If the frame pointer is eliminated, all frame offsets will be relative to
1078 // SP not FP. Align to MaxAlign so this works.
1079 StackAlign = std::max(StackAlign, MaxAlign);
1080 Offset = alignTo(Offset, StackAlign, Skew);
1083 // Update frame info to pretend that this is part of the stack...
1084 int64_t StackSize = Offset - LocalAreaOffset;
1085 MFI.setStackSize(StackSize);
1086 NumBytesStackSpace += StackSize;
1089 /// insertPrologEpilogCode - Scan the function for modified callee saved
1090 /// registers, insert spill code for these callee saved registers, then add
1091 /// prolog and epilog code to the function.
1092 void PEI::insertPrologEpilogCode(MachineFunction &MF) {
1093 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1095 // Add prologue to the function...
1096 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1097 TFI.emitPrologue(MF, *SaveBlock);
1099 // Add epilogue to restore the callee-save registers in each exiting block.
1100 for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
1101 TFI.emitEpilogue(MF, *RestoreBlock);
1103 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1104 TFI.inlineStackProbe(MF, *SaveBlock);
1106 // Emit additional code that is required to support segmented stacks, if
1107 // we've been asked for it. This, when linked with a runtime with support
1108 // for segmented stacks (libgcc is one), will result in allocating stack
1109 // space in small chunks instead of one large contiguous block.
1110 if (MF.shouldSplitStack()) {
1111 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1112 TFI.adjustForSegmentedStacks(MF, *SaveBlock);
1113 // Record that there are split-stack functions, so we will emit a
1114 // special section to tell the linker.
1115 MF.getMMI().setHasSplitStack(true);
1117 MF.getMMI().setHasNosplitStack(true);
1119 // Emit additional code that is required to explicitly handle the stack in
1120 // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
1121 // approach is rather similar to that of Segmented Stacks, but it uses a
1122 // different conditional check and another BIF for allocating more stack
1124 if (MF.getFunction().getCallingConv() == CallingConv::HiPE)
1125 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1126 TFI.adjustForHiPEPrologue(MF, *SaveBlock);
1129 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1130 /// register references and actual offsets.
1131 void PEI::replaceFrameIndices(MachineFunction &MF) {
1132 const auto &ST = MF.getSubtarget();
1133 const TargetFrameLowering &TFI = *ST.getFrameLowering();
1134 if (!TFI.needsFrameIndexResolution(MF))
1137 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
1139 // Allow the target to determine this after knowing the frame size.
1140 FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
1141 TRI->requiresFrameIndexReplacementScavenging(MF);
1143 // Store SPAdj at exit of a basic block.
1144 SmallVector<int, 8> SPState;
1145 SPState.resize(MF.getNumBlockIDs());
1146 df_iterator_default_set<MachineBasicBlock*> Reachable;
1148 // Iterate over the reachable blocks in DFS order.
1149 for (auto DFI = df_ext_begin(&MF, Reachable), DFE = df_ext_end(&MF, Reachable);
1150 DFI != DFE; ++DFI) {
1152 // Check the exit state of the DFS stack predecessor.
1153 if (DFI.getPathLength() >= 2) {
1154 MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1155 assert(Reachable.count(StackPred) &&
1156 "DFS stack predecessor is already visited.\n");
1157 SPAdj = SPState[StackPred->getNumber()];
1159 MachineBasicBlock *BB = *DFI;
1160 replaceFrameIndices(BB, MF, SPAdj);
1161 SPState[BB->getNumber()] = SPAdj;
1164 // Handle the unreachable blocks.
1165 for (auto &BB : MF) {
1166 if (Reachable.count(&BB))
1167 // Already handled in DFS traversal.
1170 replaceFrameIndices(&BB, MF, SPAdj);
1174 void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1176 assert(MF.getSubtarget().getRegisterInfo() &&
1177 "getRegisterInfo() must be implemented!");
1178 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1179 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1180 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
1182 if (RS && FrameIndexEliminationScavenging)
1183 RS->enterBasicBlock(*BB);
1185 bool InsideCallSequence = false;
1187 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
1188 if (TII.isFrameInstr(*I)) {
1189 InsideCallSequence = TII.isFrameSetup(*I);
1190 SPAdj += TII.getSPAdjust(*I);
1191 I = TFI->eliminateCallFramePseudoInstr(MF, *BB, I);
1195 MachineInstr &MI = *I;
1197 bool DidFinishLoop = true;
1198 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1199 if (!MI.getOperand(i).isFI())
1202 // Frame indices in debug values are encoded in a target independent
1203 // way with simply the frame index and offset rather than any
1204 // target-specific addressing mode.
1205 if (MI.isDebugValue()) {
1206 assert(i == 0 && "Frame indices can only appear as the first "
1207 "operand of a DBG_VALUE machine instruction");
1209 unsigned FrameIdx = MI.getOperand(0).getIndex();
1210 unsigned Size = MF.getFrameInfo().getObjectSize(FrameIdx);
1213 TFI->getFrameIndexReference(MF, FrameIdx, Reg);
1214 MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
1215 MI.getOperand(0).setIsDebug();
1217 const DIExpression *DIExpr = MI.getDebugExpression();
1219 // If we have a direct DBG_VALUE, and its location expression isn't
1220 // currently complex, then adding an offset will morph it into a
1221 // complex location that is interpreted as being a memory address.
1222 // This changes a pointer-valued variable to dereference that pointer,
1223 // which is incorrect. Fix by adding DW_OP_stack_value.
1224 unsigned PrependFlags = DIExpression::ApplyOffset;
1225 if (!MI.isIndirectDebugValue() && !DIExpr->isComplex())
1226 PrependFlags |= DIExpression::StackValue;
1228 // If we have DBG_VALUE that is indirect and has a Implicit location
1229 // expression need to insert a deref before prepending a Memory
1230 // location expression. Also after doing this we change the DBG_VALUE
1232 if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) {
1233 SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size, Size};
1234 bool WithStackValue = true;
1235 DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue);
1236 // Make the DBG_VALUE direct.
1237 MI.getDebugOffset().ChangeToRegister(0, false);
1239 DIExpr = DIExpression::prepend(DIExpr, PrependFlags, Offset);
1240 MI.getDebugExpressionOp().setMetadata(DIExpr);
1244 // TODO: This code should be commoned with the code for
1245 // PATCHPOINT. There's no good reason for the difference in
1246 // implementation other than historical accident. The only
1247 // remaining difference is the unconditional use of the stack
1248 // pointer as the base register.
1249 if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
1250 assert((!MI.isDebugValue() || i == 0) &&
1251 "Frame indicies can only appear as the first operand of a "
1252 "DBG_VALUE machine instruction");
1254 MachineOperand &Offset = MI.getOperand(i + 1);
1255 int refOffset = TFI->getFrameIndexReferencePreferSP(
1256 MF, MI.getOperand(i).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
1257 Offset.setImm(Offset.getImm() + refOffset + SPAdj);
1258 MI.getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
1262 // Some instructions (e.g. inline asm instructions) can have
1263 // multiple frame indices and/or cause eliminateFrameIndex
1264 // to insert more than one instruction. We need the register
1265 // scavenger to go through all of these instructions so that
1266 // it can update its register information. We keep the
1267 // iterator at the point before insertion so that we can
1268 // revisit them in full.
1269 bool AtBeginning = (I == BB->begin());
1270 if (!AtBeginning) --I;
1272 // If this instruction has a FrameIndex operand, we need to
1273 // use that target machine register info object to eliminate
1275 TRI.eliminateFrameIndex(MI, SPAdj, i,
1276 FrameIndexEliminationScavenging ? RS : nullptr);
1278 // Reset the iterator if we were at the beginning of the BB.
1284 DidFinishLoop = false;
1288 // If we are looking at a call sequence, we need to keep track of
1289 // the SP adjustment made by each instruction in the sequence.
1290 // This includes both the frame setup/destroy pseudos (handled above),
1291 // as well as other instructions that have side effects w.r.t the SP.
1292 // Note that this must come after eliminateFrameIndex, because
1293 // if I itself referred to a frame index, we shouldn't count its own
1295 if (DidFinishLoop && InsideCallSequence)
1296 SPAdj += TII.getSPAdjust(MI);
1298 if (DoIncr && I != BB->end()) ++I;
1300 // Update register states.
1301 if (RS && FrameIndexEliminationScavenging && DidFinishLoop)