1//===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===//
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// This pass is responsible for finalizing the functions frame layout, saving
10// callee saved registers, and for emitting prolog & epilog code for the
11// function.
12//
13// This pass must be run after register allocation. After this pass is
14// executed, it is illegal to construct MO_FrameIndex operands.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/ADT/SmallSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/Statistic.h"
26#include "llvm/Analysis/OptimizationRemarkEmitter.h"
27#include "llvm/CodeGen/MachineBasicBlock.h"
28#include "llvm/CodeGen/MachineDominators.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
32#include "llvm/CodeGen/MachineInstr.h"
33#include "llvm/CodeGen/MachineModuleInfo.h"
34#include "llvm/CodeGen/MachineOperand.h"
35#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
36#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/CodeGen/PEI.h"
38#include "llvm/CodeGen/RegisterScavenging.h"
39#include "llvm/CodeGen/TargetFrameLowering.h"
40#include "llvm/CodeGen/TargetInstrInfo.h"
41#include "llvm/CodeGen/TargetOpcodes.h"
42#include "llvm/CodeGen/TargetRegisterInfo.h"
43#include "llvm/CodeGen/TargetSubtargetInfo.h"
44#include "llvm/CodeGen/WinEHFuncInfo.h"
45#include "llvm/IR/Attributes.h"
46#include "llvm/IR/CallingConv.h"
47#include "llvm/IR/DebugInfoMetadata.h"
48#include "llvm/IR/DiagnosticInfo.h"
49#include "llvm/IR/Function.h"
50#include "llvm/IR/LLVMContext.h"
51#include "llvm/InitializePasses.h"
52#include "llvm/Pass.h"
53#include "llvm/Support/CodeGen.h"
54#include "llvm/Support/Debug.h"
55#include "llvm/Support/ErrorHandling.h"
56#include "llvm/Support/FormatVariadic.h"
57#include "llvm/Support/raw_ostream.h"
58#include "llvm/Target/TargetMachine.h"
59#include "llvm/Target/TargetOptions.h"
60#include <algorithm>
61#include <cassert>
62#include <cstdint>
63#include <limits>
64#include <utility>
65#include <vector>
66
67using namespace llvm;
68
69#define DEBUG_TYPE "prolog-epilog"
70
71using MBBVector = SmallVector<MachineBasicBlock *, 4>;
72
73STATISTIC(NumLeafFuncWithSpills, "Number of leaf functions with CSRs");
74STATISTIC(NumFuncSeen, "Number of functions seen in PEI");
75
76
77namespace {
78
79class PEIImpl {
80 RegScavenger *RS = nullptr;
81
82 // Save and Restore blocks of the current function. Typically there is a
83 // single save block, unless Windows EH funclets are involved.
84 MBBVector SaveBlocks;
85 MBBVector RestoreBlocks;
86
87 // Flag to control whether to use the register scavenger to resolve
88 // frame index materialization registers. Set according to
89 // TRI->requiresFrameIndexScavenging() for the current function.
90 bool FrameIndexVirtualScavenging = false;
91
92 // Flag to control whether the scavenger should be passed even though
93 // FrameIndexVirtualScavenging is used.
94 bool FrameIndexEliminationScavenging = false;
95
96 // Emit remarks.
97 MachineOptimizationRemarkEmitter *ORE = nullptr;
98
99 void calculateCallFrameInfo(MachineFunction &MF);
100 void calculateSaveRestoreBlocks(MachineFunction &MF);
101 void spillCalleeSavedRegs(MachineFunction &MF);
102
103 void calculateFrameObjectOffsets(MachineFunction &MF);
104 void replaceFrameIndices(MachineFunction &MF);
105 void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
106 int &SPAdj);
107 // Frame indices in debug values are encoded in a target independent
108 // way with simply the frame index and offset rather than any
109 // target-specific addressing mode.
110 bool replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI,
111 unsigned OpIdx, int SPAdj = 0);
112 // Does same as replaceFrameIndices but using the backward MIR walk and
113 // backward register scavenger walk.
114 void replaceFrameIndicesBackward(MachineFunction &MF);
115 void replaceFrameIndicesBackward(MachineBasicBlock *BB, MachineFunction &MF,
116 int &SPAdj);
117
118 void insertPrologEpilogCode(MachineFunction &MF);
119 void insertZeroCallUsedRegs(MachineFunction &MF);
120
121public:
122 PEIImpl(MachineOptimizationRemarkEmitter *ORE) : ORE(ORE) {}
123 bool run(MachineFunction &MF);
124};
125
126class PEILegacy : public MachineFunctionPass {
127public:
128 static char ID;
129
130 PEILegacy() : MachineFunctionPass(ID) {}
131
132 void getAnalysisUsage(AnalysisUsage &AU) const override;
133
134 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
135 /// frame indexes with appropriate references.
136 bool runOnMachineFunction(MachineFunction &MF) override;
137};
138
139} // end anonymous namespace
140
141char PEILegacy::ID = 0;
142
143char &llvm::PrologEpilogCodeInserterID = PEILegacy::ID;
144
145INITIALIZE_PASS_BEGIN(PEILegacy, DEBUG_TYPE, "Prologue/Epilogue Insertion",
146 false, false)
147INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
148INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
149INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
150INITIALIZE_PASS_END(PEILegacy, DEBUG_TYPE,
151 "Prologue/Epilogue Insertion & Frame Finalization", false,
152 false)
153
154MachineFunctionPass *llvm::createPrologEpilogInserterPass() {
155 return new PEILegacy();
156}
157
158STATISTIC(NumBytesStackSpace,
159 "Number of bytes used for stack in all functions");
160
161void PEILegacy::getAnalysisUsage(AnalysisUsage &AU) const {
162 AU.setPreservesCFG();
163 AU.addRequired<MachineOptimizationRemarkEmitterPass>();
164 MachineFunctionPass::getAnalysisUsage(AU);
165}
166
167/// StackObjSet - A set of stack object indexes
168using StackObjSet = SmallSetVector<int, 8>;
169
170using SavedDbgValuesMap =
171 SmallDenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 4>, 4>;
172
173/// Stash DBG_VALUEs that describe parameters and which are placed at the start
174/// of the block. Later on, after the prologue code has been emitted, the
175/// stashed DBG_VALUEs will be reinserted at the start of the block.
176static void stashEntryDbgValues(MachineBasicBlock &MBB,
177 SavedDbgValuesMap &EntryDbgValues) {
178 SmallVector<const MachineInstr *, 4> FrameIndexValues;
179
180 for (auto &MI : MBB) {
181 if (!MI.isDebugInstr())
182 break;
183 if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter())
184 continue;
185 if (any_of(Range: MI.debug_operands(),
186 P: [](const MachineOperand &MO) { return MO.isFI(); })) {
187 // We can only emit valid locations for frame indices after the frame
188 // setup, so do not stash away them.
189 FrameIndexValues.push_back(Elt: &MI);
190 continue;
191 }
192 const DILocalVariable *Var = MI.getDebugVariable();
193 const DIExpression *Expr = MI.getDebugExpression();
194 auto Overlaps = [Var, Expr](const MachineInstr *DV) {
195 return Var == DV->getDebugVariable() &&
196 Expr->fragmentsOverlap(Other: DV->getDebugExpression());
197 };
198 // See if the debug value overlaps with any preceding debug value that will
199 // not be stashed. If that is the case, then we can't stash this value, as
200 // we would then reorder the values at reinsertion.
201 if (llvm::none_of(Range&: FrameIndexValues, P: Overlaps))
202 EntryDbgValues[&MBB].push_back(Elt: &MI);
203 }
204
205 // Remove stashed debug values from the block.
206 if (auto It = EntryDbgValues.find(Val: &MBB); It != EntryDbgValues.end())
207 for (auto *MI : It->second)
208 MI->removeFromParent();
209}
210
211bool PEIImpl::run(MachineFunction &MF) {
212 NumFuncSeen++;
213 const Function &F = MF.getFunction();
214 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
215 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
216
217 RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
218 FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
219
220 // Spill frame pointer and/or base pointer registers if they are clobbered.
221 // It is placed before call frame instruction elimination so it will not mess
222 // with stack arguments.
223 TFI->spillFPBP(MF);
224
225 // Calculate the MaxCallFrameSize value for the function's frame
226 // information. Also eliminates call frame pseudo instructions.
227 calculateCallFrameInfo(MF);
228
229 // Determine placement of CSR spill/restore code and prolog/epilog code:
230 // place all spills in the entry block, all restores in return blocks.
231 calculateSaveRestoreBlocks(MF);
232
233 // Stash away DBG_VALUEs that should not be moved by insertion of prolog code.
234 SavedDbgValuesMap EntryDbgValues;
235 for (MachineBasicBlock *SaveBlock : SaveBlocks)
236 stashEntryDbgValues(MBB&: *SaveBlock, EntryDbgValues);
237
238 // Handle CSR spilling and restoring, for targets that need it.
239 if (MF.getTarget().usesPhysRegsForValues())
240 spillCalleeSavedRegs(MF);
241
242 // Allow the target machine to make final modifications to the function
243 // before the frame layout is finalized.
244 TFI->processFunctionBeforeFrameFinalized(MF, RS);
245
246 // Calculate actual frame offsets for all abstract stack objects...
247 calculateFrameObjectOffsets(MF);
248
249 // Add prolog and epilog code to the function. This function is required
250 // to align the stack frame as necessary for any stack variables or
251 // called functions. Because of this, calculateCalleeSavedRegisters()
252 // must be called before this function in order to set the AdjustsStack
253 // and MaxCallFrameSize variables.
254 if (!F.hasFnAttribute(Kind: Attribute::Naked))
255 insertPrologEpilogCode(MF);
256
257 // Reinsert stashed debug values at the start of the entry blocks.
258 for (auto &I : EntryDbgValues)
259 I.first->insert(I: I.first->begin(), S: I.second.begin(), E: I.second.end());
260
261 // Allow the target machine to make final modifications to the function
262 // before the frame layout is finalized.
263 TFI->processFunctionBeforeFrameIndicesReplaced(MF, RS);
264
265 // Replace all MO_FrameIndex operands with physical register references
266 // and actual offsets.
267 if (TFI->needsFrameIndexResolution(MF)) {
268 // Allow the target to determine this after knowing the frame size.
269 FrameIndexEliminationScavenging =
270 (RS && !FrameIndexVirtualScavenging) ||
271 TRI->requiresFrameIndexReplacementScavenging(MF);
272
273 if (TRI->eliminateFrameIndicesBackwards())
274 replaceFrameIndicesBackward(MF);
275 else
276 replaceFrameIndices(MF);
277 }
278
279 // If register scavenging is needed, as we've enabled doing it as a
280 // post-pass, scavenge the virtual registers that frame index elimination
281 // inserted.
282 if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
283 scavengeFrameVirtualRegs(MF, RS&: *RS);
284
285 insertZeroCallUsedRegs(MF);
286
287 // Warn on stack size when we exceeds the given limit.
288 MachineFrameInfo &MFI = MF.getFrameInfo();
289 uint64_t StackSize = MFI.getStackSize();
290
291 uint64_t Threshold = TFI->getStackThreshold();
292 if (MF.getFunction().hasFnAttribute(Kind: "warn-stack-size")) {
293 bool Failed = MF.getFunction()
294 .getFnAttribute(Kind: "warn-stack-size")
295 .getValueAsString()
296 .getAsInteger(Radix: 10, Result&: Threshold);
297 // Verifier should have caught this.
298 assert(!Failed && "Invalid warn-stack-size fn attr value");
299 (void)Failed;
300 }
301 uint64_t UnsafeStackSize = MFI.getUnsafeStackSize();
302 if (MF.getFunction().hasFnAttribute(Kind: Attribute::SafeStack))
303 StackSize += UnsafeStackSize;
304
305 if (StackSize > Threshold) {
306 DiagnosticInfoStackSize DiagStackSize(F, StackSize, Threshold, DS_Warning);
307 F.getContext().diagnose(DI: DiagStackSize);
308 int64_t SpillSize = 0;
309 for (int Idx = MFI.getObjectIndexBegin(), End = MFI.getObjectIndexEnd();
310 Idx != End; ++Idx) {
311 if (MFI.isSpillSlotObjectIndex(ObjectIdx: Idx))
312 SpillSize += MFI.getObjectSize(ObjectIdx: Idx);
313 }
314
315 [[maybe_unused]] float SpillPct =
316 static_cast<float>(SpillSize) / static_cast<float>(StackSize);
317 LLVM_DEBUG(
318 dbgs() << formatv("{0}/{1} ({3:P}) spills, {2}/{1} ({4:P}) variables",
319 SpillSize, StackSize, StackSize - SpillSize, SpillPct,
320 1.0f - SpillPct));
321 if (UnsafeStackSize != 0) {
322 LLVM_DEBUG(dbgs() << formatv(", {0}/{2} ({1:P}) unsafe stack",
323 UnsafeStackSize,
324 static_cast<float>(UnsafeStackSize) /
325 static_cast<float>(StackSize),
326 StackSize));
327 }
328 LLVM_DEBUG(dbgs() << "\n");
329 }
330
331 ORE->emit(RemarkBuilder: [&]() {
332 return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
333 MF.getFunction().getSubprogram(),
334 &MF.front())
335 << ore::NV("NumStackBytes", StackSize)
336 << " stack bytes in function '"
337 << ore::NV("Function", MF.getFunction().getName()) << "'";
338 });
339
340 // Emit any remarks implemented for the target, based on final frame layout.
341 TFI->emitRemarks(MF, ORE);
342
343 delete RS;
344 SaveBlocks.clear();
345 RestoreBlocks.clear();
346 MFI.clearSavePoints();
347 MFI.clearRestorePoints();
348 return true;
349}
350
351/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
352/// frame indexes with appropriate references.
353bool PEILegacy::runOnMachineFunction(MachineFunction &MF) {
354 MachineOptimizationRemarkEmitter *ORE =
355 &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
356 return PEIImpl(ORE).run(MF);
357}
358
359PreservedAnalyses
360PrologEpilogInserterPass::run(MachineFunction &MF,
361 MachineFunctionAnalysisManager &MFAM) {
362 MachineOptimizationRemarkEmitter &ORE =
363 MFAM.getResult<MachineOptimizationRemarkEmitterAnalysis>(IR&: MF);
364 if (!PEIImpl(&ORE).run(MF))
365 return PreservedAnalyses::all();
366
367 // Segmented-stack and HiPE prologues insert new basic blocks, so the CFG is
368 // not preserved for such functions.
369 // TODO: We should propagate this info directly once we are done with NewPM
370 // transition.
371 if (MF.shouldSplitStack() ||
372 MF.getFunction().getCallingConv() == CallingConv::HiPE)
373 return getMachineFunctionPassPreservedAnalyses();
374
375 return getMachineFunctionPassPreservedAnalyses().preserveSet<CFGAnalyses>();
376}
377
378/// Calculate the MaxCallFrameSize variable for the function's frame
379/// information and eliminate call frame pseudo instructions.
380void PEIImpl::calculateCallFrameInfo(MachineFunction &MF) {
381 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
382 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
383 MachineFrameInfo &MFI = MF.getFrameInfo();
384
385 // Get the function call frame set-up and tear-down instruction opcode
386 unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
387 unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
388
389 // Early exit for targets which have no call frame setup/destroy pseudo
390 // instructions.
391 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
392 return;
393
394 // (Re-)Compute the MaxCallFrameSize.
395 [[maybe_unused]] uint64_t MaxCFSIn =
396 MFI.isMaxCallFrameSizeComputed() ? MFI.getMaxCallFrameSize() : UINT64_MAX;
397 std::vector<MachineBasicBlock::iterator> FrameSDOps;
398 MFI.computeMaxCallFrameSize(MF, FrameSDOps: &FrameSDOps);
399 assert(MFI.getMaxCallFrameSize() <= MaxCFSIn &&
400 "Recomputing MaxCFS gave a larger value.");
401 assert((FrameSDOps.empty() || MF.getFrameInfo().adjustsStack()) &&
402 "AdjustsStack not set in presence of a frame pseudo instruction.");
403
404 if (TFI->canSimplifyCallFramePseudos(MF)) {
405 // If call frames are not being included as part of the stack frame, and
406 // the target doesn't indicate otherwise, remove the call frame pseudos
407 // here. The sub/add sp instruction pairs are still inserted, but we don't
408 // need to track the SP adjustment for frame index elimination.
409 for (MachineBasicBlock::iterator I : FrameSDOps)
410 TFI->eliminateCallFramePseudoInstr(MF, MBB&: *I->getParent(), MI: I);
411
412 // We can't track the call frame size after call frame pseudos have been
413 // eliminated. Set it to zero everywhere to keep MachineVerifier happy.
414 for (MachineBasicBlock &MBB : MF)
415 MBB.setCallFrameSize(0);
416 }
417}
418
419/// Compute the sets of entry and return blocks for saving and restoring
420/// callee-saved registers, and placing prolog and epilog code.
421void PEIImpl::calculateSaveRestoreBlocks(MachineFunction &MF) {
422 const MachineFrameInfo &MFI = MF.getFrameInfo();
423 // Even when we do not change any CSR, we still want to insert the
424 // prologue and epilogue of the function.
425 // So set the save points for those.
426
427 // Use the points found by shrink-wrapping, if any.
428 if (!MFI.getSavePoints().empty()) {
429 assert(MFI.getSavePoints().size() == 1 &&
430 "Multiple save points are not yet supported!");
431 const auto &SavePoint = *MFI.getSavePoints().begin();
432 SaveBlocks.push_back(Elt: SavePoint.first);
433 assert(MFI.getRestorePoints().size() == 1 &&
434 "Multiple restore points are not yet supported!");
435 const auto &RestorePoint = *MFI.getRestorePoints().begin();
436 MachineBasicBlock *RestoreBlock = RestorePoint.first;
437 // If RestoreBlock does not have any successor and is not a return block
438 // then the end point is unreachable and we do not need to insert any
439 // epilogue.
440 if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
441 RestoreBlocks.push_back(Elt: RestoreBlock);
442 return;
443 }
444
445 // Save refs to entry and return blocks.
446 SaveBlocks.push_back(Elt: &MF.front());
447 for (MachineBasicBlock &MBB : MF) {
448 if (MBB.isEHFuncletEntry())
449 SaveBlocks.push_back(Elt: &MBB);
450 if (MBB.isReturnBlock())
451 RestoreBlocks.push_back(Elt: &MBB);
452 }
453}
454
455static void assignCalleeSavedSpillSlots(MachineFunction &F,
456 const BitVector &SavedRegs) {
457 if (SavedRegs.empty())
458 return;
459
460 const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
461 const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
462 BitVector CSMask(SavedRegs.size());
463
464 for (unsigned i = 0; CSRegs[i]; ++i)
465 CSMask.set(CSRegs[i]);
466
467 std::vector<CalleeSavedInfo> CSI;
468 for (unsigned i = 0; CSRegs[i]; ++i) {
469 unsigned Reg = CSRegs[i];
470 if (SavedRegs.test(Idx: Reg)) {
471 bool SavedSuper = false;
472 for (const MCPhysReg &SuperReg : RegInfo->superregs(Reg)) {
473 // Some backends set all aliases for some registers as saved, such as
474 // Mips's $fp, so they appear in SavedRegs but not CSRegs.
475 if (SavedRegs.test(Idx: SuperReg) && CSMask.test(Idx: SuperReg)) {
476 SavedSuper = true;
477 break;
478 }
479 }
480
481 if (!SavedSuper)
482 CSI.push_back(x: CalleeSavedInfo(Reg));
483 }
484 }
485
486 const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
487 MachineFrameInfo &MFI = F.getFrameInfo();
488 if (!TFI->assignCalleeSavedSpillSlots(MF&: F, TRI: RegInfo, CSI)) {
489 // If target doesn't implement this, use generic code.
490
491 if (CSI.empty())
492 return; // Early exit if no callee saved registers are modified!
493
494 unsigned NumFixedSpillSlots;
495 const TargetFrameLowering::SpillSlot *FixedSpillSlots =
496 TFI->getCalleeSavedSpillSlots(NumEntries&: NumFixedSpillSlots);
497
498 // Now that we know which registers need to be saved and restored, allocate
499 // stack slots for them.
500 for (auto &CS : CSI) {
501 // If the target has spilled this register to another register or already
502 // handled it , we don't need to allocate a stack slot.
503 if (CS.isSpilledToReg())
504 continue;
505
506 MCRegister Reg = CS.getReg();
507 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
508
509 int FrameIdx;
510 if (RegInfo->hasReservedSpillSlot(MF: F, Reg, FrameIdx)) {
511 CS.setFrameIdx(FrameIdx);
512 continue;
513 }
514
515 // Check to see if this physreg must be spilled to a particular stack slot
516 // on this target.
517 const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
518 while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
519 FixedSlot->Reg != Reg)
520 ++FixedSlot;
521
522 unsigned Size = RegInfo->getSpillSize(RC: *RC);
523 if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
524 // Nope, just spill it anywhere convenient.
525 Align Alignment = RegInfo->getSpillAlign(RC: *RC);
526 // We may not be able to satisfy the desired alignment specification of
527 // the TargetRegisterClass if the stack alignment is smaller. Use the
528 // min.
529 Alignment = std::min(a: Alignment, b: TFI->getStackAlign());
530 FrameIdx = MFI.CreateStackObject(Size, Alignment, isSpillSlot: true, Alloca: nullptr,
531 ID: RegInfo->getSpillStackID(RC: *RC));
532 MFI.setIsCalleeSavedObjectIndex(ObjectIdx: FrameIdx, IsCalleeSaved: true);
533 } else {
534 // Spill it to the stack where we must.
535 FrameIdx = MFI.CreateFixedSpillStackObject(Size, SPOffset: FixedSlot->Offset);
536 }
537
538 CS.setFrameIdx(FrameIdx);
539 }
540 }
541
542 MFI.setCalleeSavedInfo(CSI);
543}
544
545/// Helper function to update the liveness information for the callee-saved
546/// registers.
547static void updateLiveness(MachineFunction &MF) {
548 MachineFrameInfo &MFI = MF.getFrameInfo();
549 // Visited will contain all the basic blocks that are in the region
550 // where the callee saved registers are alive:
551 // - Anything that is not Save or Restore -> LiveThrough.
552 // - Save -> LiveIn.
553 // - Restore -> LiveOut.
554 // The live-out is not attached to the block, so no need to keep
555 // Restore in this set.
556 SmallPtrSet<MachineBasicBlock *, 8> Visited;
557 SmallVector<MachineBasicBlock *, 8> WorkList;
558 MachineBasicBlock *Entry = &MF.front();
559
560 assert(MFI.getSavePoints().size() < 2 &&
561 "Multiple save points not yet supported!");
562 MachineBasicBlock *Save = MFI.getSavePoints().empty()
563 ? nullptr
564 : (*MFI.getSavePoints().begin()).first;
565
566 if (!Save)
567 Save = Entry;
568
569 if (Entry != Save) {
570 WorkList.push_back(Elt: Entry);
571 Visited.insert(Ptr: Entry);
572 }
573 Visited.insert(Ptr: Save);
574
575 assert(MFI.getRestorePoints().size() < 2 &&
576 "Multiple restore points not yet supported!");
577 MachineBasicBlock *Restore = MFI.getRestorePoints().empty()
578 ? nullptr
579 : (*MFI.getRestorePoints().begin()).first;
580 if (Restore)
581 // By construction Restore cannot be visited, otherwise it
582 // means there exists a path to Restore that does not go
583 // through Save.
584 WorkList.push_back(Elt: Restore);
585
586 while (!WorkList.empty()) {
587 const MachineBasicBlock *CurBB = WorkList.pop_back_val();
588 // By construction, the region that is after the save point is
589 // dominated by the Save and post-dominated by the Restore.
590 if (CurBB == Save && Save != Restore)
591 continue;
592 // Enqueue all the successors not already visited.
593 // Those are by construction either before Save or after Restore.
594 for (MachineBasicBlock *SuccBB : CurBB->successors())
595 if (Visited.insert(Ptr: SuccBB).second)
596 WorkList.push_back(Elt: SuccBB);
597 }
598
599 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
600
601 MachineRegisterInfo &MRI = MF.getRegInfo();
602 for (const CalleeSavedInfo &I : CSI) {
603 for (MachineBasicBlock *MBB : Visited) {
604 MCRegister Reg = I.getReg();
605 // Add the callee-saved register as live-in.
606 // It's killed at the spill.
607 if (!MRI.isReserved(PhysReg: Reg) && !MBB->isLiveIn(Reg))
608 MBB->addLiveIn(PhysReg: Reg);
609 }
610 // If callee-saved register is spilled to another register rather than
611 // spilling to stack, the destination register has to be marked as live for
612 // each MBB between the prologue and epilogue so that it is not clobbered
613 // before it is reloaded in the epilogue. The Visited set contains all
614 // blocks outside of the region delimited by prologue/epilogue.
615 if (I.isSpilledToReg()) {
616 for (MachineBasicBlock &MBB : MF) {
617 if (Visited.count(Ptr: &MBB))
618 continue;
619 MCRegister DstReg = I.getDstReg();
620 if (!MBB.isLiveIn(Reg: DstReg))
621 MBB.addLiveIn(PhysReg: DstReg);
622 }
623 }
624 }
625}
626
627/// Insert spill code for the callee-saved registers used in the function.
628static void insertCSRSaves(MachineBasicBlock &SaveBlock,
629 ArrayRef<CalleeSavedInfo> CSI) {
630 MachineFunction &MF = *SaveBlock.getParent();
631 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
632 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
633 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
634
635 MachineBasicBlock::iterator I = SaveBlock.begin();
636 if (!TFI->spillCalleeSavedRegisters(MBB&: SaveBlock, MI: I, CSI, TRI)) {
637 for (const CalleeSavedInfo &CS : CSI) {
638 TFI->spillCalleeSavedRegister(SaveBlock, MI: I, CS, TII, TRI);
639 }
640 }
641}
642
643/// Insert restore code for the callee-saved registers used in the function.
644static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
645 std::vector<CalleeSavedInfo> &CSI) {
646 MachineFunction &MF = *RestoreBlock.getParent();
647 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
648 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
649 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
650
651 // Restore all registers immediately before the return and any
652 // terminators that precede it.
653 MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator();
654
655 if (!TFI->restoreCalleeSavedRegisters(MBB&: RestoreBlock, MI: I, CSI, TRI)) {
656 for (const CalleeSavedInfo &CI : reverse(C&: CSI)) {
657 TFI->restoreCalleeSavedRegister(MBB&: RestoreBlock, MI: I, CS: CI, TII, TRI);
658 }
659 }
660}
661
662void PEIImpl::spillCalleeSavedRegs(MachineFunction &MF) {
663 // We can't list this requirement in getRequiredProperties because some
664 // targets (WebAssembly) use virtual registers past this point, and the pass
665 // pipeline is set up without giving the passes a chance to look at the
666 // TargetMachine.
667 // FIXME: Find a way to express this in getRequiredProperties.
668 assert(MF.getProperties().hasNoVRegs());
669
670 const Function &F = MF.getFunction();
671 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
672 MachineFrameInfo &MFI = MF.getFrameInfo();
673
674 // Determine which of the registers in the callee save list should be saved.
675 BitVector SavedRegs;
676 TFI->determineCalleeSaves(MF, SavedRegs, RS);
677
678 // Assign stack slots for any callee-saved registers that must be spilled.
679 assignCalleeSavedSpillSlots(F&: MF, SavedRegs);
680
681 // Add the code to save and restore the callee saved registers.
682 if (!F.hasFnAttribute(Kind: Attribute::Naked)) {
683 MFI.setCalleeSavedInfoValid(true);
684
685 std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
686
687 // Fill SavePoints and RestorePoints with CalleeSavedRegisters
688 if (!MFI.getSavePoints().empty()) {
689 SaveRestorePoints SaveRestorePts;
690 for (const auto &SavePoint : MFI.getSavePoints())
691 SaveRestorePts.insert(KV: {SavePoint.first, CSI});
692 MFI.setSavePoints(std::move(SaveRestorePts));
693
694 SaveRestorePts.clear();
695 for (const auto &RestorePoint : MFI.getRestorePoints())
696 SaveRestorePts.insert(KV: {RestorePoint.first, CSI});
697 MFI.setRestorePoints(std::move(SaveRestorePts));
698 }
699
700 if (!CSI.empty()) {
701 if (!MFI.hasCalls())
702 NumLeafFuncWithSpills++;
703
704 for (MachineBasicBlock *SaveBlock : SaveBlocks)
705 insertCSRSaves(SaveBlock&: *SaveBlock, CSI);
706
707 // Update the live-in information of all the blocks up to the save point.
708 updateLiveness(MF);
709
710 for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
711 insertCSRRestores(RestoreBlock&: *RestoreBlock, CSI);
712 }
713 }
714}
715
716/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
717static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
718 bool StackGrowsDown, int64_t &Offset,
719 Align &MaxAlign) {
720 // If the stack grows down, add the object size to find the lowest address.
721 if (StackGrowsDown)
722 Offset += MFI.getObjectSize(ObjectIdx: FrameIdx);
723
724 Align Alignment = MFI.getObjectAlign(ObjectIdx: FrameIdx);
725
726 // If the alignment of this object is greater than that of the stack, then
727 // increase the stack alignment to match.
728 MaxAlign = std::max(a: MaxAlign, b: Alignment);
729
730 // Adjust to alignment boundary.
731 Offset = alignTo(Size: Offset, A: Alignment);
732
733 if (StackGrowsDown) {
734 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
735 << "]\n");
736 MFI.setObjectOffset(ObjectIdx: FrameIdx, SPOffset: -Offset); // Set the computed offset
737 } else {
738 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
739 << "]\n");
740 MFI.setObjectOffset(ObjectIdx: FrameIdx, SPOffset: Offset);
741 Offset += MFI.getObjectSize(ObjectIdx: FrameIdx);
742 }
743}
744
745/// Compute which bytes of fixed and callee-save stack area are unused and keep
746/// track of them in StackBytesFree.
747static inline void computeFreeStackSlots(MachineFrameInfo &MFI,
748 bool StackGrowsDown,
749 int64_t FixedCSEnd,
750 BitVector &StackBytesFree) {
751 // Avoid undefined int64_t -> int conversion below in extreme case.
752 if (FixedCSEnd > std::numeric_limits<int>::max())
753 return;
754
755 StackBytesFree.resize(N: FixedCSEnd, t: true);
756
757 SmallVector<int, 16> AllocatedFrameSlots;
758 // Add fixed objects.
759 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
760 // StackSlot scavenging is only implemented for the default stack.
761 if (MFI.getStackID(ObjectIdx: i) == TargetStackID::Default)
762 AllocatedFrameSlots.push_back(Elt: i);
763 // Add callee-save objects if there are any.
764 for (int i = MFI.getObjectIndexBegin(); i < MFI.getObjectIndexEnd(); i++)
765 if (MFI.isCalleeSavedObjectIndex(ObjectIdx: i) &&
766 MFI.getStackID(ObjectIdx: i) == TargetStackID::Default)
767 AllocatedFrameSlots.push_back(Elt: i);
768
769 for (int i : AllocatedFrameSlots) {
770 // These are converted from int64_t, but they should always fit in int
771 // because of the FixedCSEnd check above.
772 int ObjOffset = MFI.getObjectOffset(ObjectIdx: i);
773 int ObjSize = MFI.getObjectSize(ObjectIdx: i);
774 int ObjStart, ObjEnd;
775 if (StackGrowsDown) {
776 // ObjOffset is negative when StackGrowsDown is true.
777 ObjStart = -ObjOffset - ObjSize;
778 ObjEnd = -ObjOffset;
779 } else {
780 ObjStart = ObjOffset;
781 ObjEnd = ObjOffset + ObjSize;
782 }
783 // Ignore fixed holes that are in the previous stack frame.
784 if (ObjEnd > 0)
785 StackBytesFree.reset(I: ObjStart, E: ObjEnd);
786 }
787}
788
789/// Assign frame object to an unused portion of the stack in the fixed stack
790/// object range. Return true if the allocation was successful.
791static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
792 bool StackGrowsDown, Align MaxAlign,
793 BitVector &StackBytesFree) {
794 if (MFI.isVariableSizedObjectIndex(ObjectIdx: FrameIdx))
795 return false;
796
797 if (StackBytesFree.none()) {
798 // clear it to speed up later scavengeStackSlot calls to
799 // StackBytesFree.none()
800 StackBytesFree.clear();
801 return false;
802 }
803
804 Align ObjAlign = MFI.getObjectAlign(ObjectIdx: FrameIdx);
805 if (ObjAlign > MaxAlign)
806 return false;
807
808 int64_t ObjSize = MFI.getObjectSize(ObjectIdx: FrameIdx);
809 int FreeStart;
810 for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
811 FreeStart = StackBytesFree.find_next(Prev: FreeStart)) {
812
813 // Check that free space has suitable alignment.
814 unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
815 if (alignTo(Size: ObjStart, A: ObjAlign) != ObjStart)
816 continue;
817
818 if (FreeStart + ObjSize > StackBytesFree.size())
819 return false;
820
821 bool AllBytesFree = true;
822 for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
823 if (!StackBytesFree.test(Idx: FreeStart + Byte)) {
824 AllBytesFree = false;
825 break;
826 }
827 if (AllBytesFree)
828 break;
829 }
830
831 if (FreeStart == -1)
832 return false;
833
834 if (StackGrowsDown) {
835 int ObjStart = -(FreeStart + ObjSize);
836 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
837 << ObjStart << "]\n");
838 MFI.setObjectOffset(ObjectIdx: FrameIdx, SPOffset: ObjStart);
839 } else {
840 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
841 << FreeStart << "]\n");
842 MFI.setObjectOffset(ObjectIdx: FrameIdx, SPOffset: FreeStart);
843 }
844
845 StackBytesFree.reset(I: FreeStart, E: FreeStart + ObjSize);
846 return true;
847}
848
849/// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
850/// those required to be close to the Stack Protector) to stack offsets.
851static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
852 SmallSet<int, 16> &ProtectedObjs,
853 MachineFrameInfo &MFI, bool StackGrowsDown,
854 int64_t &Offset, Align &MaxAlign) {
855
856 for (int i : UnassignedObjs) {
857 AdjustStackOffset(MFI, FrameIdx: i, StackGrowsDown, Offset, MaxAlign);
858 ProtectedObjs.insert(V: i);
859 }
860}
861
862/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
863/// abstract stack objects.
864void PEIImpl::calculateFrameObjectOffsets(MachineFunction &MF) {
865 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
866
867 bool StackGrowsDown =
868 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
869
870 // Loop over all of the stack objects, assigning sequential addresses...
871 MachineFrameInfo &MFI = MF.getFrameInfo();
872
873 // Start at the beginning of the local area.
874 // The Offset is the distance from the stack top in the direction
875 // of stack growth -- so it's always nonnegative.
876 int LocalAreaOffset = TFI.getOffsetOfLocalArea();
877 if (StackGrowsDown)
878 LocalAreaOffset = -LocalAreaOffset;
879 assert(LocalAreaOffset >= 0
880 && "Local area offset should be in direction of stack growth");
881 int64_t Offset = LocalAreaOffset;
882
883#ifdef EXPENSIVE_CHECKS
884 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i)
885 if (!MFI.isDeadObjectIndex(i) &&
886 MFI.getStackID(i) == TargetStackID::Default)
887 assert(MFI.getObjectAlign(i) <= MFI.getMaxAlign() &&
888 "MaxAlignment is invalid");
889#endif
890
891 // If there are fixed sized objects that are preallocated in the local area,
892 // non-fixed objects can't be allocated right at the start of local area.
893 // Adjust 'Offset' to point to the end of last fixed sized preallocated
894 // object.
895 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
896 // Only allocate objects on the default stack.
897 if (MFI.getStackID(ObjectIdx: i) != TargetStackID::Default)
898 continue;
899
900 int64_t FixedOff;
901 if (StackGrowsDown) {
902 // The maximum distance from the stack pointer is at lower address of
903 // the object -- which is given by offset. For down growing stack
904 // the offset is negative, so we negate the offset to get the distance.
905 FixedOff = -MFI.getObjectOffset(ObjectIdx: i);
906 } else {
907 // The maximum distance from the start pointer is at the upper
908 // address of the object.
909 FixedOff = MFI.getObjectOffset(ObjectIdx: i) + MFI.getObjectSize(ObjectIdx: i);
910 }
911 if (FixedOff > Offset) Offset = FixedOff;
912 }
913
914 Align MaxAlign = MFI.getMaxAlign();
915 // First assign frame offsets to stack objects that are used to spill
916 // callee saved registers.
917 auto AllFIs = seq(Begin: MFI.getObjectIndexBegin(), End: MFI.getObjectIndexEnd());
918 for (int FI : reverse_conditionally(C&: AllFIs, /*Reverse=*/ShouldReverse: !StackGrowsDown)) {
919 // Only allocate objects on the default stack.
920 if (!MFI.isCalleeSavedObjectIndex(ObjectIdx: FI) ||
921 MFI.getStackID(ObjectIdx: FI) != TargetStackID::Default)
922 continue;
923
924 // TODO: should this just be if (MFI.isDeadObjectIndex(FI))
925 if (!StackGrowsDown && MFI.isDeadObjectIndex(ObjectIdx: FI))
926 continue;
927
928 AdjustStackOffset(MFI, FrameIdx: FI, StackGrowsDown, Offset, MaxAlign);
929 }
930
931 assert(MaxAlign == MFI.getMaxAlign() &&
932 "MFI.getMaxAlign should already account for all callee-saved "
933 "registers without a fixed stack slot");
934
935 // FixedCSEnd is the stack offset to the end of the fixed and callee-save
936 // stack area.
937 int64_t FixedCSEnd = Offset;
938
939 // Make sure the special register scavenging spill slot is closest to the
940 // incoming stack pointer if a frame pointer is required and is closer
941 // to the incoming rather than the final stack pointer.
942 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
943 bool EarlyScavengingSlots = TFI.allocateScavengingFrameIndexesNearIncomingSP(MF);
944 if (RS && EarlyScavengingSlots) {
945 SmallVector<int, 2> SFIs;
946 RS->getScavengingFrameIndices(A&: SFIs);
947 for (int SFI : SFIs)
948 AdjustStackOffset(MFI, FrameIdx: SFI, StackGrowsDown, Offset, MaxAlign);
949 }
950
951 // FIXME: Once this is working, then enable flag will change to a target
952 // check for whether the frame is large enough to want to use virtual
953 // frame index registers. Functions which don't want/need this optimization
954 // will continue to use the existing code path.
955 if (MFI.getUseLocalStackAllocationBlock()) {
956 Align Alignment = MFI.getLocalFrameMaxAlign();
957
958 // Adjust to alignment boundary.
959 Offset = alignTo(Size: Offset, A: Alignment);
960
961 LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
962
963 // Resolve offsets for objects in the local block.
964 for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
965 std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
966 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
967 LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
968 << "]\n");
969 MFI.setObjectOffset(ObjectIdx: Entry.first, SPOffset: FIOffset);
970 }
971 // Allocate the local block
972 Offset += MFI.getLocalFrameSize();
973
974 MaxAlign = std::max(a: Alignment, b: MaxAlign);
975 }
976
977 // Retrieve the Exception Handler registration node.
978 int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
979 if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo())
980 EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
981
982 // Make sure that the stack protector comes before the local variables on the
983 // stack.
984 SmallSet<int, 16> ProtectedObjs;
985 if (MFI.hasStackProtectorIndex()) {
986 int StackProtectorFI = MFI.getStackProtectorIndex();
987 StackObjSet LargeArrayObjs;
988 StackObjSet SmallArrayObjs;
989 StackObjSet AddrOfObjs;
990
991 // If we need a stack protector, we need to make sure that
992 // LocalStackSlotPass didn't already allocate a slot for it.
993 // If we are told to use the LocalStackAllocationBlock, the stack protector
994 // is expected to be already pre-allocated.
995 if (MFI.getStackID(ObjectIdx: StackProtectorFI) != TargetStackID::Default) {
996 // If the stack protector isn't on the default stack then it's up to the
997 // target to set the stack offset.
998 assert(MFI.getObjectOffset(StackProtectorFI) != 0 &&
999 "Offset of stack protector on non-default stack expected to be "
1000 "already set.");
1001 assert(!MFI.isObjectPreAllocated(MFI.getStackProtectorIndex()) &&
1002 "Stack protector on non-default stack expected to not be "
1003 "pre-allocated by LocalStackSlotPass.");
1004 } else if (!MFI.getUseLocalStackAllocationBlock()) {
1005 AdjustStackOffset(MFI, FrameIdx: StackProtectorFI, StackGrowsDown, Offset,
1006 MaxAlign);
1007 } else if (!MFI.isObjectPreAllocated(ObjectIdx: MFI.getStackProtectorIndex())) {
1008 llvm_unreachable(
1009 "Stack protector not pre-allocated by LocalStackSlotPass.");
1010 }
1011
1012 // Assign large stack objects first.
1013 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
1014 if (MFI.isObjectPreAllocated(ObjectIdx: i) && MFI.getUseLocalStackAllocationBlock())
1015 continue;
1016 if (MFI.isCalleeSavedObjectIndex(ObjectIdx: i))
1017 continue;
1018 if (RS && RS->isScavengingFrameIndex(FI: (int)i))
1019 continue;
1020 if (MFI.isDeadObjectIndex(ObjectIdx: i))
1021 continue;
1022 if (StackProtectorFI == (int)i || EHRegNodeFrameIndex == (int)i)
1023 continue;
1024 // Only allocate objects on the default stack.
1025 if (MFI.getStackID(ObjectIdx: i) != TargetStackID::Default)
1026 continue;
1027
1028 switch (MFI.getObjectSSPLayout(ObjectIdx: i)) {
1029 case MachineFrameInfo::SSPLK_None:
1030 continue;
1031 case MachineFrameInfo::SSPLK_SmallArray:
1032 SmallArrayObjs.insert(X: i);
1033 continue;
1034 case MachineFrameInfo::SSPLK_AddrOf:
1035 AddrOfObjs.insert(X: i);
1036 continue;
1037 case MachineFrameInfo::SSPLK_LargeArray:
1038 LargeArrayObjs.insert(X: i);
1039 continue;
1040 }
1041 llvm_unreachable("Unexpected SSPLayoutKind.");
1042 }
1043
1044 // We expect **all** the protected stack objects to be pre-allocated by
1045 // LocalStackSlotPass. If it turns out that PEI still has to allocate some
1046 // of them, we may end up messing up the expected order of the objects.
1047 if (MFI.getUseLocalStackAllocationBlock() &&
1048 !(LargeArrayObjs.empty() && SmallArrayObjs.empty() &&
1049 AddrOfObjs.empty()))
1050 llvm_unreachable("Found protected stack objects not pre-allocated by "
1051 "LocalStackSlotPass.");
1052
1053 AssignProtectedObjSet(UnassignedObjs: LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
1054 Offset, MaxAlign);
1055 AssignProtectedObjSet(UnassignedObjs: SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
1056 Offset, MaxAlign);
1057 AssignProtectedObjSet(UnassignedObjs: AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
1058 Offset, MaxAlign);
1059 }
1060
1061 SmallVector<int, 8> ObjectsToAllocate;
1062
1063 // Then prepare to assign frame offsets to stack objects that are not used to
1064 // spill callee saved registers.
1065 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
1066 if (MFI.isObjectPreAllocated(ObjectIdx: i) && MFI.getUseLocalStackAllocationBlock())
1067 continue;
1068 if (MFI.isCalleeSavedObjectIndex(ObjectIdx: i))
1069 continue;
1070 if (RS && RS->isScavengingFrameIndex(FI: (int)i))
1071 continue;
1072 if (MFI.isDeadObjectIndex(ObjectIdx: i))
1073 continue;
1074 if (MFI.getStackProtectorIndex() == (int)i || EHRegNodeFrameIndex == (int)i)
1075 continue;
1076 if (ProtectedObjs.count(V: i))
1077 continue;
1078 // Only allocate objects on the default stack.
1079 if (MFI.getStackID(ObjectIdx: i) != TargetStackID::Default)
1080 continue;
1081
1082 // Add the objects that we need to allocate to our working set.
1083 ObjectsToAllocate.push_back(Elt: i);
1084 }
1085
1086 // Allocate the EH registration node first if one is present.
1087 if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
1088 AdjustStackOffset(MFI, FrameIdx: EHRegNodeFrameIndex, StackGrowsDown, Offset,
1089 MaxAlign);
1090
1091 // Give the targets a chance to order the objects the way they like it.
1092 if (MF.getTarget().getOptLevel() != CodeGenOptLevel::None &&
1093 MF.getTarget().Options.StackSymbolOrdering)
1094 TFI.orderFrameObjects(MF, objectsToAllocate&: ObjectsToAllocate);
1095
1096 // Keep track of which bytes in the fixed and callee-save range are used so we
1097 // can use the holes when allocating later stack objects. Only do this if
1098 // stack protector isn't being used and the target requests it and we're
1099 // optimizing.
1100 BitVector StackBytesFree;
1101 if (!ObjectsToAllocate.empty() &&
1102 MF.getTarget().getOptLevel() != CodeGenOptLevel::None &&
1103 MFI.getStackProtectorIndex() < 0 && TFI.enableStackSlotScavenging(MF))
1104 computeFreeStackSlots(MFI, StackGrowsDown, FixedCSEnd, StackBytesFree);
1105
1106 // Now walk the objects and actually assign base offsets to them.
1107 for (auto &Object : ObjectsToAllocate)
1108 if (!scavengeStackSlot(MFI, FrameIdx: Object, StackGrowsDown, MaxAlign,
1109 StackBytesFree))
1110 AdjustStackOffset(MFI, FrameIdx: Object, StackGrowsDown, Offset, MaxAlign);
1111
1112 // Make sure the special register scavenging spill slot is closest to the
1113 // stack pointer.
1114 if (RS && !EarlyScavengingSlots) {
1115 SmallVector<int, 2> SFIs;
1116 RS->getScavengingFrameIndices(A&: SFIs);
1117 for (int SFI : SFIs)
1118 AdjustStackOffset(MFI, FrameIdx: SFI, StackGrowsDown, Offset, MaxAlign);
1119 }
1120
1121 if (!TFI.targetHandlesStackFrameRounding()) {
1122 // If we have reserved argument space for call sites in the function
1123 // immediately on entry to the current function, count it as part of the
1124 // overall stack size.
1125 if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF))
1126 Offset += MFI.getMaxCallFrameSize();
1127
1128 // Round up the size to a multiple of the alignment. If the function has
1129 // any calls or alloca's, align to the target's StackAlignment value to
1130 // ensure that the callee's frame or the alloca data is suitably aligned;
1131 // otherwise, for leaf functions, align to the TransientStackAlignment
1132 // value.
1133 Align StackAlign;
1134 if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
1135 (RegInfo->hasStackRealignment(MF) && MFI.getObjectIndexEnd() != 0))
1136 StackAlign = TFI.getStackAlign();
1137 else
1138 StackAlign = TFI.getTransientStackAlign();
1139
1140 // If the frame pointer is eliminated, all frame offsets will be relative to
1141 // SP not FP. Align to MaxAlign so this works.
1142 StackAlign = std::max(a: StackAlign, b: MaxAlign);
1143 int64_t OffsetBeforeAlignment = Offset;
1144 Offset = alignTo(Size: Offset, A: StackAlign);
1145
1146 // If we have increased the offset to fulfill the alignment constrants,
1147 // then the scavenging spill slots may become harder to reach from the
1148 // stack pointer, float them so they stay close.
1149 if (StackGrowsDown && OffsetBeforeAlignment != Offset && RS &&
1150 !EarlyScavengingSlots) {
1151 SmallVector<int, 2> SFIs;
1152 RS->getScavengingFrameIndices(A&: SFIs);
1153 LLVM_DEBUG(if (!SFIs.empty()) llvm::dbgs()
1154 << "Adjusting emergency spill slots!\n";);
1155 int64_t Delta = Offset - OffsetBeforeAlignment;
1156 for (int SFI : SFIs) {
1157 LLVM_DEBUG(llvm::dbgs()
1158 << "Adjusting offset of emergency spill slot #" << SFI
1159 << " from " << MFI.getObjectOffset(SFI););
1160 MFI.setObjectOffset(ObjectIdx: SFI, SPOffset: MFI.getObjectOffset(ObjectIdx: SFI) - Delta);
1161 LLVM_DEBUG(llvm::dbgs() << " to " << MFI.getObjectOffset(SFI) << "\n";);
1162 }
1163 }
1164 }
1165
1166 // Update frame info to pretend that this is part of the stack...
1167 int64_t StackSize = Offset - LocalAreaOffset;
1168 MFI.setStackSize(StackSize);
1169 NumBytesStackSpace += StackSize;
1170}
1171
1172/// insertPrologEpilogCode - Scan the function for modified callee saved
1173/// registers, insert spill code for these callee saved registers, then add
1174/// prolog and epilog code to the function.
1175void PEIImpl::insertPrologEpilogCode(MachineFunction &MF) {
1176 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1177
1178 // Add prologue to the function...
1179 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1180 TFI.emitPrologue(MF, MBB&: *SaveBlock);
1181
1182 // Add epilogue to restore the callee-save registers in each exiting block.
1183 for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
1184 TFI.emitEpilogue(MF, MBB&: *RestoreBlock);
1185
1186 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1187 TFI.inlineStackProbe(MF, PrologueMBB&: *SaveBlock);
1188
1189 // Emit additional code that is required to support segmented stacks, if
1190 // we've been asked for it. This, when linked with a runtime with support
1191 // for segmented stacks (libgcc is one), will result in allocating stack
1192 // space in small chunks instead of one large contiguous block.
1193 if (MF.shouldSplitStack()) {
1194 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1195 TFI.adjustForSegmentedStacks(MF, PrologueMBB&: *SaveBlock);
1196 }
1197
1198 // Emit additional code that is required to explicitly handle the stack in
1199 // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
1200 // approach is rather similar to that of Segmented Stacks, but it uses a
1201 // different conditional check and another BIF for allocating more stack
1202 // space.
1203 if (MF.getFunction().getCallingConv() == CallingConv::HiPE)
1204 for (MachineBasicBlock *SaveBlock : SaveBlocks)
1205 TFI.adjustForHiPEPrologue(MF, PrologueMBB&: *SaveBlock);
1206}
1207
1208/// insertZeroCallUsedRegs - Zero out call used registers.
1209void PEIImpl::insertZeroCallUsedRegs(MachineFunction &MF) {
1210 const Function &F = MF.getFunction();
1211
1212 if (!F.hasFnAttribute(Kind: "zero-call-used-regs"))
1213 return;
1214
1215 using namespace ZeroCallUsedRegs;
1216
1217 ZeroCallUsedRegsKind ZeroRegsKind =
1218 StringSwitch<ZeroCallUsedRegsKind>(
1219 F.getFnAttribute(Kind: "zero-call-used-regs").getValueAsString())
1220 .Case(S: "skip", Value: ZeroCallUsedRegsKind::Skip)
1221 .Case(S: "used-gpr-arg", Value: ZeroCallUsedRegsKind::UsedGPRArg)
1222 .Case(S: "used-gpr", Value: ZeroCallUsedRegsKind::UsedGPR)
1223 .Case(S: "used-arg", Value: ZeroCallUsedRegsKind::UsedArg)
1224 .Case(S: "used", Value: ZeroCallUsedRegsKind::Used)
1225 .Case(S: "all-gpr-arg", Value: ZeroCallUsedRegsKind::AllGPRArg)
1226 .Case(S: "all-gpr", Value: ZeroCallUsedRegsKind::AllGPR)
1227 .Case(S: "all-arg", Value: ZeroCallUsedRegsKind::AllArg)
1228 .Case(S: "all", Value: ZeroCallUsedRegsKind::All);
1229
1230 if (ZeroRegsKind == ZeroCallUsedRegsKind::Skip)
1231 return;
1232
1233 const bool OnlyGPR = static_cast<unsigned>(ZeroRegsKind) & ONLY_GPR;
1234 const bool OnlyUsed = static_cast<unsigned>(ZeroRegsKind) & ONLY_USED;
1235 const bool OnlyArg = static_cast<unsigned>(ZeroRegsKind) & ONLY_ARG;
1236
1237 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1238 const BitVector AllocatableSet(TRI.getAllocatableSet(MF));
1239
1240 // Mark all used registers.
1241 BitVector UsedRegs(TRI.getNumRegs());
1242 if (OnlyUsed)
1243 for (const MachineBasicBlock &MBB : MF)
1244 for (const MachineInstr &MI : MBB) {
1245 // skip debug instructions
1246 if (MI.isDebugInstr())
1247 continue;
1248
1249 for (const MachineOperand &MO : MI.operands()) {
1250 if (!MO.isReg())
1251 continue;
1252
1253 MCRegister Reg = MO.getReg();
1254 if (AllocatableSet[Reg.id()] && !MO.isImplicit() &&
1255 (MO.isDef() || MO.isUse()))
1256 UsedRegs.set(Reg.id());
1257 }
1258 }
1259
1260 // Get a list of registers that are used.
1261 BitVector LiveIns(TRI.getNumRegs());
1262 for (const MachineBasicBlock::RegisterMaskPair &LI : MF.front().liveins())
1263 LiveIns.set(LI.PhysReg);
1264
1265 BitVector RegsToZero(TRI.getNumRegs());
1266 for (MCRegister Reg : AllocatableSet.set_bits()) {
1267 // Skip over fixed registers.
1268 if (TRI.isFixedRegister(MF, PhysReg: Reg))
1269 continue;
1270
1271 // Want only general purpose registers.
1272 if (OnlyGPR && !TRI.isGeneralPurposeRegister(MF, PhysReg: Reg))
1273 continue;
1274
1275 // Want only used registers.
1276 if (OnlyUsed && !UsedRegs[Reg.id()])
1277 continue;
1278
1279 // Want only registers used for arguments.
1280 if (OnlyArg) {
1281 if (OnlyUsed) {
1282 for (MCRegister LiveReg : LiveIns.set_bits()) {
1283 if (TRI.regsOverlap(RegA: Reg, RegB: LiveReg))
1284 RegsToZero.set(LiveReg);
1285 }
1286 continue;
1287 } else if (!TRI.isArgumentRegister(MF, PhysReg: Reg)) {
1288 continue;
1289 }
1290 }
1291
1292 RegsToZero.set(Reg.id());
1293 }
1294
1295 // Don't clear registers that are live when leaving the function.
1296 for (const MachineBasicBlock &MBB : MF)
1297 for (const MachineInstr &MI : MBB.terminators()) {
1298 if (!MI.isReturn())
1299 continue;
1300
1301 for (const auto &MO : MI.operands()) {
1302 if (!MO.isReg())
1303 continue;
1304
1305 MCRegister Reg = MO.getReg();
1306 if (!Reg)
1307 continue;
1308
1309 // This picks up sibling registers (e.q. %al -> %ah).
1310 // FIXME: Mixing physical registers and register units is likely a bug.
1311 for (MCRegUnit Unit : TRI.regunits(Reg))
1312 RegsToZero.reset(Idx: static_cast<unsigned>(Unit));
1313
1314 for (MCPhysReg SReg : TRI.sub_and_superregs_inclusive(Reg))
1315 RegsToZero.reset(Idx: SReg);
1316 }
1317 }
1318
1319 // Don't need to clear registers that are used/clobbered by terminating
1320 // instructions.
1321 for (const MachineBasicBlock &MBB : MF) {
1322 if (!MBB.isReturnBlock())
1323 continue;
1324
1325 MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator();
1326 for (MachineBasicBlock::const_iterator I = MBBI, E = MBB.end(); I != E;
1327 ++I) {
1328 for (const MachineOperand &MO : I->operands()) {
1329 if (!MO.isReg())
1330 continue;
1331
1332 MCRegister Reg = MO.getReg();
1333 if (!Reg)
1334 continue;
1335
1336 for (const MCPhysReg Reg : TRI.sub_and_superregs_inclusive(Reg))
1337 RegsToZero.reset(Idx: Reg);
1338 }
1339 }
1340 }
1341
1342 // Don't clear registers that must be preserved.
1343 for (const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(MF: &MF);
1344 MCPhysReg CSReg = *CSRegs; ++CSRegs)
1345 for (MCRegister Reg : TRI.sub_and_superregs_inclusive(Reg: CSReg))
1346 RegsToZero.reset(Idx: Reg.id());
1347
1348 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1349 for (MachineBasicBlock &MBB : MF)
1350 if (MBB.isReturnBlock())
1351 TFI.emitZeroCallUsedRegs(RegsToZero, MBB, RS);
1352}
1353
1354/// Replace all FrameIndex operands with physical register references and actual
1355/// offsets.
1356void PEIImpl::replaceFrameIndicesBackward(MachineFunction &MF) {
1357 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1358
1359 for (auto &MBB : MF) {
1360 int SPAdj = 0;
1361 if (!MBB.succ_empty()) {
1362 // Get the SP adjustment for the end of MBB from the start of any of its
1363 // successors. They should all be the same.
1364 assert(all_of(MBB.successors(), [&MBB](const MachineBasicBlock *Succ) {
1365 return Succ->getCallFrameSize() ==
1366 (*MBB.succ_begin())->getCallFrameSize();
1367 }));
1368 const MachineBasicBlock &FirstSucc = **MBB.succ_begin();
1369 SPAdj = TFI.alignSPAdjust(SPAdj: FirstSucc.getCallFrameSize());
1370 if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp)
1371 SPAdj = -SPAdj;
1372 }
1373
1374 replaceFrameIndicesBackward(BB: &MBB, MF, SPAdj);
1375
1376 // We can't track the call frame size after call frame pseudos have been
1377 // eliminated. Set it to zero everywhere to keep MachineVerifier happy.
1378 MBB.setCallFrameSize(0);
1379 }
1380}
1381
1382/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1383/// register references and actual offsets.
1384void PEIImpl::replaceFrameIndices(MachineFunction &MF) {
1385 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1386
1387 for (auto &MBB : MF) {
1388 int SPAdj = TFI.alignSPAdjust(SPAdj: MBB.getCallFrameSize());
1389 if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp)
1390 SPAdj = -SPAdj;
1391
1392 replaceFrameIndices(BB: &MBB, MF, SPAdj);
1393
1394 // We can't track the call frame size after call frame pseudos have been
1395 // eliminated. Set it to zero everywhere to keep MachineVerifier happy.
1396 MBB.setCallFrameSize(0);
1397 }
1398}
1399
1400bool PEIImpl::replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI,
1401 unsigned OpIdx, int SPAdj) {
1402 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
1403 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1404 if (MI.isDebugValue()) {
1405
1406 MachineOperand &Op = MI.getOperand(i: OpIdx);
1407 assert(MI.isDebugOperand(&Op) &&
1408 "Frame indices can only appear as a debug operand in a DBG_VALUE*"
1409 " machine instruction");
1410 Register Reg;
1411 unsigned FrameIdx = Op.getIndex();
1412 unsigned Size = MF.getFrameInfo().getObjectSize(ObjectIdx: FrameIdx);
1413
1414 StackOffset Offset = TFI->getFrameIndexReference(MF, FI: FrameIdx, FrameReg&: Reg);
1415 Op.ChangeToRegister(Reg, isDef: false /*isDef*/);
1416
1417 const DIExpression *DIExpr = MI.getDebugExpression();
1418
1419 // If we have a direct DBG_VALUE, and its location expression isn't
1420 // currently complex, then adding an offset will morph it into a
1421 // complex location that is interpreted as being a memory address.
1422 // This changes a pointer-valued variable to dereference that pointer,
1423 // which is incorrect. Fix by adding DW_OP_stack_value.
1424
1425 if (MI.isNonListDebugValue()) {
1426 unsigned PrependFlags = DIExpression::ApplyOffset;
1427 if (!MI.isIndirectDebugValue() && !DIExpr->isComplex())
1428 PrependFlags |= DIExpression::StackValue;
1429
1430 // If we have DBG_VALUE that is indirect and has a Implicit location
1431 // expression need to insert a deref before prepending a Memory
1432 // location expression. Also after doing this we change the DBG_VALUE
1433 // to be direct.
1434 if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) {
1435 // DW_OP_deref_size may not read more than the size of an address.
1436 if (Size > MF.getDataLayout().getPointerSize()) {
1437 LLVM_DEBUG(dbgs() << "Dropping location of object larger than an "
1438 "address: "
1439 << MI);
1440 MI.setDebugValueUndef();
1441 return true;
1442 }
1443 assert(isUInt<8>(Size) &&
1444 "DW_OP_deref_size operand must fit in a byte");
1445 SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size, Size};
1446 bool WithStackValue = true;
1447 DIExpr = DIExpression::prependOpcodes(Expr: DIExpr, Ops, StackValue: WithStackValue);
1448 // Make the DBG_VALUE direct.
1449 MI.getDebugOffset().ChangeToRegister(Reg: 0, isDef: false);
1450 }
1451 DIExpr = TRI.prependOffsetExpression(Expr: DIExpr, PrependFlags, Offset);
1452 } else {
1453 // The debug operand at DebugOpIndex was a frame index at offset
1454 // `Offset`; now the operand has been replaced with the frame
1455 // register, we must add Offset with `register x, plus Offset`.
1456 unsigned DebugOpIndex = MI.getDebugOperandIndex(Op: &Op);
1457 SmallVector<uint64_t, 3> Ops;
1458 TRI.getOffsetOpcodes(Offset, Ops);
1459 DIExpr = DIExpression::appendOpsToArg(Expr: DIExpr, Ops, ArgNo: DebugOpIndex);
1460 }
1461 MI.getDebugExpressionOp().setMetadata(DIExpr);
1462 return true;
1463 }
1464
1465 if (MI.isDebugPHI()) {
1466 // Allow stack ref to continue onwards.
1467 return true;
1468 }
1469
1470 // TODO: This code should be commoned with the code for
1471 // PATCHPOINT. There's no good reason for the difference in
1472 // implementation other than historical accident. The only
1473 // remaining difference is the unconditional use of the stack
1474 // pointer as the base register.
1475 if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
1476 assert((!MI.isDebugValue() || OpIdx == 0) &&
1477 "Frame indices can only appear as the first operand of a "
1478 "DBG_VALUE machine instruction");
1479 Register Reg;
1480 MachineOperand &Offset = MI.getOperand(i: OpIdx + 1);
1481 StackOffset refOffset = TFI->getFrameIndexReferencePreferSP(
1482 MF, FI: MI.getOperand(i: OpIdx).getIndex(), FrameReg&: Reg, /*IgnoreSPUpdates*/ false);
1483 assert(!refOffset.getScalable() &&
1484 "Frame offsets with a scalable component are not supported");
1485 Offset.setImm(Offset.getImm() + refOffset.getFixed() + SPAdj);
1486 MI.getOperand(i: OpIdx).ChangeToRegister(Reg, isDef: false /*isDef*/);
1487 return true;
1488 }
1489 return false;
1490}
1491
1492void PEIImpl::replaceFrameIndicesBackward(MachineBasicBlock *BB,
1493 MachineFunction &MF, int &SPAdj) {
1494 assert(MF.getSubtarget().getRegisterInfo() &&
1495 "getRegisterInfo() must be implemented!");
1496
1497 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1498 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1499 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1500
1501 RegScavenger *LocalRS = FrameIndexEliminationScavenging ? RS : nullptr;
1502 if (LocalRS)
1503 LocalRS->enterBasicBlockEnd(MBB&: *BB);
1504
1505 for (MachineBasicBlock::iterator I = BB->end(); I != BB->begin();) {
1506 MachineInstr &MI = *std::prev(x: I);
1507
1508 if (TII.isFrameInstr(I: MI)) {
1509 SPAdj -= TII.getSPAdjust(MI);
1510 TFI.eliminateCallFramePseudoInstr(MF, MBB&: *BB, MI: &MI);
1511 continue;
1512 }
1513
1514 // Step backwards to get the liveness state at (immedately after) MI.
1515 if (LocalRS)
1516 LocalRS->backward(I);
1517
1518 bool RemovedMI = false;
1519 for (const auto &[Idx, Op] : enumerate(First: MI.operands())) {
1520 if (!Op.isFI())
1521 continue;
1522
1523 if (replaceFrameIndexDebugInstr(MF, MI, OpIdx: Idx, SPAdj))
1524 continue;
1525
1526 // Eliminate this FrameIndex operand.
1527 RemovedMI = TRI.eliminateFrameIndex(MI, SPAdj, FIOperandNum: Idx, RS: LocalRS);
1528 if (RemovedMI)
1529 break;
1530 }
1531
1532 if (!RemovedMI)
1533 --I;
1534 }
1535}
1536
1537void PEIImpl::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1538 int &SPAdj) {
1539 assert(MF.getSubtarget().getRegisterInfo() &&
1540 "getRegisterInfo() must be implemented!");
1541 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1542 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1543 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
1544
1545 bool InsideCallSequence = false;
1546
1547 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
1548 if (TII.isFrameInstr(I: *I)) {
1549 InsideCallSequence = TII.isFrameSetup(I: *I);
1550 SPAdj += TII.getSPAdjust(MI: *I);
1551 I = TFI->eliminateCallFramePseudoInstr(MF, MBB&: *BB, MI: I);
1552 continue;
1553 }
1554
1555 MachineInstr &MI = *I;
1556 bool DoIncr = true;
1557 bool DidFinishLoop = true;
1558 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1559 if (!MI.getOperand(i).isFI())
1560 continue;
1561
1562 if (replaceFrameIndexDebugInstr(MF, MI, OpIdx: i, SPAdj))
1563 continue;
1564
1565 // Some instructions (e.g. inline asm instructions) can have
1566 // multiple frame indices and/or cause eliminateFrameIndex
1567 // to insert more than one instruction. We need the register
1568 // scavenger to go through all of these instructions so that
1569 // it can update its register information. We keep the
1570 // iterator at the point before insertion so that we can
1571 // revisit them in full.
1572 bool AtBeginning = (I == BB->begin());
1573 if (!AtBeginning) --I;
1574
1575 // If this instruction has a FrameIndex operand, we need to
1576 // use that target machine register info object to eliminate
1577 // it.
1578 TRI.eliminateFrameIndex(MI, SPAdj, FIOperandNum: i, RS);
1579
1580 // Reset the iterator if we were at the beginning of the BB.
1581 if (AtBeginning) {
1582 I = BB->begin();
1583 DoIncr = false;
1584 }
1585
1586 DidFinishLoop = false;
1587 break;
1588 }
1589
1590 // If we are looking at a call sequence, we need to keep track of
1591 // the SP adjustment made by each instruction in the sequence.
1592 // This includes both the frame setup/destroy pseudos (handled above),
1593 // as well as other instructions that have side effects w.r.t the SP.
1594 // Note that this must come after eliminateFrameIndex, because
1595 // if I itself referred to a frame index, we shouldn't count its own
1596 // adjustment.
1597 if (DidFinishLoop && InsideCallSequence)
1598 SPAdj += TII.getSPAdjust(MI);
1599
1600 if (DoIncr && I != BB->end())
1601 ++I;
1602 }
1603}
1604