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/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/MachineLoopInfo.h"
35 #include "llvm/CodeGen/MachineModuleInfo.h"
36 #include "llvm/CodeGen/MachineOperand.h"
37 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/CodeGen/RegisterScavenging.h"
40 #include "llvm/CodeGen/TargetFrameLowering.h"
41 #include "llvm/CodeGen/TargetInstrInfo.h"
42 #include "llvm/CodeGen/TargetOpcodes.h"
43 #include "llvm/CodeGen/TargetRegisterInfo.h"
44 #include "llvm/CodeGen/TargetSubtargetInfo.h"
45 #include "llvm/CodeGen/WinEHFuncInfo.h"
46 #include "llvm/IR/Attributes.h"
47 #include "llvm/IR/CallingConv.h"
48 #include "llvm/IR/DebugInfoMetadata.h"
49 #include "llvm/IR/DiagnosticInfo.h"
50 #include "llvm/IR/Function.h"
51 #include "llvm/IR/InlineAsm.h"
52 #include "llvm/IR/LLVMContext.h"
53 #include "llvm/MC/MCRegisterInfo.h"
54 #include "llvm/Pass.h"
55 #include "llvm/Support/CodeGen.h"
56 #include "llvm/Support/CommandLine.h"
57 #include "llvm/Support/Debug.h"
58 #include "llvm/Support/ErrorHandling.h"
59 #include "llvm/Support/MathExtras.h"
60 #include "llvm/Support/raw_ostream.h"
61 #include "llvm/Target/TargetMachine.h"
62 #include "llvm/Target/TargetOptions.h"
63 #include <algorithm>
64 #include <cassert>
65 #include <cstdint>
66 #include <functional>
67 #include <limits>
68 #include <utility>
69 #include <vector>
70 
71 using namespace llvm;
72 
73 #define DEBUG_TYPE "prologepilog"
74 
75 using MBBVector = SmallVector<MachineBasicBlock *, 4>;
76 
77 STATISTIC(NumLeafFuncWithSpills, "Number of leaf functions with CSRs");
78 STATISTIC(NumFuncSeen, "Number of functions seen in PEI");
79 
80 
81 namespace {
82 
83 class PEI : public MachineFunctionPass {
84 public:
85   static char ID;
86 
87   PEI() : MachineFunctionPass(ID) {
88     initializePEIPass(*PassRegistry::getPassRegistry());
89   }
90 
91   void getAnalysisUsage(AnalysisUsage &AU) const override;
92 
93   /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
94   /// frame indexes with appropriate references.
95   bool runOnMachineFunction(MachineFunction &MF) override;
96 
97 private:
98   RegScavenger *RS;
99 
100   // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
101   // stack frame indexes.
102   unsigned MinCSFrameIndex = std::numeric_limits<unsigned>::max();
103   unsigned MaxCSFrameIndex = 0;
104 
105   // Save and Restore blocks of the current function. Typically there is a
106   // single save block, unless Windows EH funclets are involved.
107   MBBVector SaveBlocks;
108   MBBVector RestoreBlocks;
109 
110   // Flag to control whether to use the register scavenger to resolve
111   // frame index materialization registers. Set according to
112   // TRI->requiresFrameIndexScavenging() for the current function.
113   bool FrameIndexVirtualScavenging;
114 
115   // Flag to control whether the scavenger should be passed even though
116   // FrameIndexVirtualScavenging is used.
117   bool FrameIndexEliminationScavenging;
118 
119   // Emit remarks.
120   MachineOptimizationRemarkEmitter *ORE = nullptr;
121 
122   void calculateCallFrameInfo(MachineFunction &MF);
123   void calculateSaveRestoreBlocks(MachineFunction &MF);
124   void spillCalleeSavedRegs(MachineFunction &MF);
125 
126   void calculateFrameObjectOffsets(MachineFunction &MF);
127   void replaceFrameIndices(MachineFunction &MF);
128   void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
129                            int &SPAdj);
130   void insertPrologEpilogCode(MachineFunction &MF);
131 };
132 
133 } // end anonymous namespace
134 
135 char PEI::ID = 0;
136 
137 char &llvm::PrologEpilogCodeInserterID = PEI::ID;
138 
139 static cl::opt<unsigned>
140 WarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1),
141               cl::desc("Warn for stack size bigger than the given"
142                        " number"));
143 
144 INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false,
145                       false)
146 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
147 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
148 INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
149 INITIALIZE_PASS_END(PEI, DEBUG_TYPE,
150                     "Prologue/Epilogue Insertion & Frame Finalization", false,
151                     false)
152 
153 MachineFunctionPass *llvm::createPrologEpilogInserterPass() {
154   return new PEI();
155 }
156 
157 STATISTIC(NumBytesStackSpace,
158           "Number of bytes used for stack in all functions");
159 
160 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
161   AU.setPreservesCFG();
162   AU.addPreserved<MachineLoopInfo>();
163   AU.addPreserved<MachineDominatorTree>();
164   AU.addRequired<MachineOptimizationRemarkEmitterPass>();
165   MachineFunctionPass::getAnalysisUsage(AU);
166 }
167 
168 /// StackObjSet - A set of stack object indexes
169 using StackObjSet = SmallSetVector<int, 8>;
170 
171 using SavedDbgValuesMap =
172     SmallDenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 4>, 4>;
173 
174 /// Stash DBG_VALUEs that describe parameters and which are placed at the start
175 /// of the block. Later on, after the prologue code has been emitted, the
176 /// stashed DBG_VALUEs will be reinserted at the start of the block.
177 static void stashEntryDbgValues(MachineBasicBlock &MBB,
178                                 SavedDbgValuesMap &EntryDbgValues) {
179   SmallVector<const MachineInstr *, 4> FrameIndexValues;
180 
181   for (auto &MI : MBB) {
182     if (!MI.isDebugInstr())
183       break;
184     if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter())
185       continue;
186     if (MI.getOperand(0).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(&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(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(FrameIndexValues, Overlaps))
202       EntryDbgValues[&MBB].push_back(&MI);
203   }
204 
205   // Remove stashed debug values from the block.
206   if (EntryDbgValues.count(&MBB))
207     for (auto *MI : EntryDbgValues[&MBB])
208       MI->removeFromParent();
209 }
210 
211 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
212 /// frame indexes with appropriate references.
213 bool PEI::runOnMachineFunction(MachineFunction &MF) {
214   NumFuncSeen++;
215   const Function &F = MF.getFunction();
216   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
217   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
218 
219   RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
220   FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
221   FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
222     TRI->requiresFrameIndexReplacementScavenging(MF);
223   ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
224 
225   // Calculate the MaxCallFrameSize and AdjustsStack variables for the
226   // function's frame information. Also eliminates call frame pseudo
227   // instructions.
228   calculateCallFrameInfo(MF);
229 
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);
233 
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);
238 
239   // Handle CSR spilling and restoring, for targets that need it.
240   if (MF.getTarget().usesPhysRegsForPEI())
241     spillCalleeSavedRegs(MF);
242 
243   // Allow the target machine to make final modifications to the function
244   // before the frame layout is finalized.
245   TFI->processFunctionBeforeFrameFinalized(MF, RS);
246 
247   // Calculate actual frame offsets for all abstract stack objects...
248   calculateFrameObjectOffsets(MF);
249 
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);
257 
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());
261 
262   // Replace all MO_FrameIndex operands with physical register references
263   // and actual offsets.
264   //
265   replaceFrameIndices(MF);
266 
267   // If register scavenging is needed, as we've enabled doing it as a
268   // post-pass, scavenge the virtual registers that frame index elimination
269   // inserted.
270   if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
271     scavengeFrameVirtualRegs(MF, *RS);
272 
273   // Warn on stack size when we exceeds the given limit.
274   MachineFrameInfo &MFI = MF.getFrameInfo();
275   uint64_t StackSize = MFI.getStackSize();
276   if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
277     DiagnosticInfoStackSize DiagStackSize(F, StackSize);
278     F.getContext().diagnose(DiagStackSize);
279   }
280   ORE->emit([&]() {
281     return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
282                                              MF.getFunction().getSubprogram(),
283                                              &MF.front())
284            << ore::NV("NumStackBytes", StackSize) << " stack bytes in function";
285   });
286 
287   delete RS;
288   SaveBlocks.clear();
289   RestoreBlocks.clear();
290   MFI.setSavePoint(nullptr);
291   MFI.setRestorePoint(nullptr);
292   return true;
293 }
294 
295 /// Calculate the MaxCallFrameSize and AdjustsStack
296 /// variables for the function's frame information and eliminate call frame
297 /// pseudo instructions.
298 void PEI::calculateCallFrameInfo(MachineFunction &MF) {
299   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
300   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
301   MachineFrameInfo &MFI = MF.getFrameInfo();
302 
303   unsigned MaxCallFrameSize = 0;
304   bool AdjustsStack = MFI.adjustsStack();
305 
306   // Get the function call frame set-up and tear-down instruction opcode
307   unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
308   unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
309 
310   // Early exit for targets which have no call frame setup/destroy pseudo
311   // instructions.
312   if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
313     return;
314 
315   std::vector<MachineBasicBlock::iterator> FrameSDOps;
316   for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
317     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
318       if (TII.isFrameInstr(*I)) {
319         unsigned Size = TII.getFrameSize(*I);
320         if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
321         AdjustsStack = true;
322         FrameSDOps.push_back(I);
323       } else if (I->isInlineAsm()) {
324         // Some inline asm's need a stack frame, as indicated by operand 1.
325         unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
326         if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
327           AdjustsStack = true;
328       }
329 
330   assert(!MFI.isMaxCallFrameSizeComputed() ||
331          (MFI.getMaxCallFrameSize() == MaxCallFrameSize &&
332           MFI.adjustsStack() == AdjustsStack));
333   MFI.setAdjustsStack(AdjustsStack);
334   MFI.setMaxCallFrameSize(MaxCallFrameSize);
335 
336   for (std::vector<MachineBasicBlock::iterator>::iterator
337          i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
338     MachineBasicBlock::iterator I = *i;
339 
340     // If call frames are not being included as part of the stack frame, and
341     // the target doesn't indicate otherwise, remove the call frame pseudos
342     // here. The sub/add sp instruction pairs are still inserted, but we don't
343     // need to track the SP adjustment for frame index elimination.
344     if (TFI->canSimplifyCallFramePseudos(MF))
345       TFI->eliminateCallFramePseudoInstr(MF, *I->getParent(), I);
346   }
347 }
348 
349 /// Compute the sets of entry and return blocks for saving and restoring
350 /// callee-saved registers, and placing prolog and epilog code.
351 void PEI::calculateSaveRestoreBlocks(MachineFunction &MF) {
352   const MachineFrameInfo &MFI = MF.getFrameInfo();
353 
354   // Even when we do not change any CSR, we still want to insert the
355   // prologue and epilogue of the function.
356   // So set the save points for those.
357 
358   // Use the points found by shrink-wrapping, if any.
359   if (MFI.getSavePoint()) {
360     SaveBlocks.push_back(MFI.getSavePoint());
361     assert(MFI.getRestorePoint() && "Both restore and save must be set");
362     MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
363     // If RestoreBlock does not have any successor and is not a return block
364     // then the end point is unreachable and we do not need to insert any
365     // epilogue.
366     if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
367       RestoreBlocks.push_back(RestoreBlock);
368     return;
369   }
370 
371   // Save refs to entry and return blocks.
372   SaveBlocks.push_back(&MF.front());
373   for (MachineBasicBlock &MBB : MF) {
374     if (MBB.isEHFuncletEntry())
375       SaveBlocks.push_back(&MBB);
376     if (MBB.isReturnBlock())
377       RestoreBlocks.push_back(&MBB);
378   }
379 }
380 
381 static void assignCalleeSavedSpillSlots(MachineFunction &F,
382                                         const BitVector &SavedRegs,
383                                         unsigned &MinCSFrameIndex,
384                                         unsigned &MaxCSFrameIndex) {
385   if (SavedRegs.empty())
386     return;
387 
388   const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
389   const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
390 
391   std::vector<CalleeSavedInfo> CSI;
392   for (unsigned i = 0; CSRegs[i]; ++i) {
393     unsigned Reg = CSRegs[i];
394     if (SavedRegs.test(Reg))
395       CSI.push_back(CalleeSavedInfo(Reg));
396   }
397 
398   const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
399   MachineFrameInfo &MFI = F.getFrameInfo();
400   if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
401     // If target doesn't implement this, use generic code.
402 
403     if (CSI.empty())
404       return; // Early exit if no callee saved registers are modified!
405 
406     unsigned NumFixedSpillSlots;
407     const TargetFrameLowering::SpillSlot *FixedSpillSlots =
408         TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
409 
410     // Now that we know which registers need to be saved and restored, allocate
411     // stack slots for them.
412     for (auto &CS : CSI) {
413       // If the target has spilled this register to another register, we don't
414       // need to allocate a stack slot.
415       if (CS.isSpilledToReg())
416         continue;
417 
418       unsigned Reg = CS.getReg();
419       const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
420 
421       int FrameIdx;
422       if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
423         CS.setFrameIdx(FrameIdx);
424         continue;
425       }
426 
427       // Check to see if this physreg must be spilled to a particular stack slot
428       // on this target.
429       const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
430       while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
431              FixedSlot->Reg != Reg)
432         ++FixedSlot;
433 
434       unsigned Size = RegInfo->getSpillSize(*RC);
435       if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
436         // Nope, just spill it anywhere convenient.
437         unsigned Align = RegInfo->getSpillAlignment(*RC);
438         unsigned StackAlign = TFI->getStackAlignment();
439 
440         // We may not be able to satisfy the desired alignment specification of
441         // the TargetRegisterClass if the stack alignment is smaller. Use the
442         // min.
443         Align = std::min(Align, StackAlign);
444         FrameIdx = MFI.CreateStackObject(Size, Align, true);
445         if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
446         if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
447       } else {
448         // Spill it to the stack where we must.
449         FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset);
450       }
451 
452       CS.setFrameIdx(FrameIdx);
453     }
454   }
455 
456   MFI.setCalleeSavedInfo(CSI);
457 }
458 
459 /// Helper function to update the liveness information for the callee-saved
460 /// registers.
461 static void updateLiveness(MachineFunction &MF) {
462   MachineFrameInfo &MFI = MF.getFrameInfo();
463   // Visited will contain all the basic blocks that are in the region
464   // where the callee saved registers are alive:
465   // - Anything that is not Save or Restore -> LiveThrough.
466   // - Save -> LiveIn.
467   // - Restore -> LiveOut.
468   // The live-out is not attached to the block, so no need to keep
469   // Restore in this set.
470   SmallPtrSet<MachineBasicBlock *, 8> Visited;
471   SmallVector<MachineBasicBlock *, 8> WorkList;
472   MachineBasicBlock *Entry = &MF.front();
473   MachineBasicBlock *Save = MFI.getSavePoint();
474 
475   if (!Save)
476     Save = Entry;
477 
478   if (Entry != Save) {
479     WorkList.push_back(Entry);
480     Visited.insert(Entry);
481   }
482   Visited.insert(Save);
483 
484   MachineBasicBlock *Restore = MFI.getRestorePoint();
485   if (Restore)
486     // By construction Restore cannot be visited, otherwise it
487     // means there exists a path to Restore that does not go
488     // through Save.
489     WorkList.push_back(Restore);
490 
491   while (!WorkList.empty()) {
492     const MachineBasicBlock *CurBB = WorkList.pop_back_val();
493     // By construction, the region that is after the save point is
494     // dominated by the Save and post-dominated by the Restore.
495     if (CurBB == Save && Save != Restore)
496       continue;
497     // Enqueue all the successors not already visited.
498     // Those are by construction either before Save or after Restore.
499     for (MachineBasicBlock *SuccBB : CurBB->successors())
500       if (Visited.insert(SuccBB).second)
501         WorkList.push_back(SuccBB);
502   }
503 
504   const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
505 
506   MachineRegisterInfo &MRI = MF.getRegInfo();
507   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
508     for (MachineBasicBlock *MBB : Visited) {
509       MCPhysReg Reg = CSI[i].getReg();
510       // Add the callee-saved register as live-in.
511       // It's killed at the spill.
512       if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg))
513         MBB->addLiveIn(Reg);
514     }
515     // If callee-saved register is spilled to another register rather than
516     // spilling to stack, the destination register has to be marked as live for
517     // each MBB between the prologue and epilogue so that it is not clobbered
518     // before it is reloaded in the epilogue. The Visited set contains all
519     // blocks outside of the region delimited by prologue/epilogue.
520     if (CSI[i].isSpilledToReg()) {
521       for (MachineBasicBlock &MBB : MF) {
522         if (Visited.count(&MBB))
523           continue;
524         MCPhysReg DstReg = CSI[i].getDstReg();
525         if (!MBB.isLiveIn(DstReg))
526           MBB.addLiveIn(DstReg);
527       }
528     }
529   }
530 
531 }
532 
533 /// Insert restore code for the callee-saved registers used in the function.
534 static void insertCSRSaves(MachineBasicBlock &SaveBlock,
535                            ArrayRef<CalleeSavedInfo> CSI) {
536   MachineFunction &MF = *SaveBlock.getParent();
537   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
538   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
539   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
540 
541   MachineBasicBlock::iterator I = SaveBlock.begin();
542   if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
543     for (const CalleeSavedInfo &CS : CSI) {
544       // Insert the spill to the stack frame.
545       unsigned Reg = CS.getReg();
546       const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
547       TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC,
548                               TRI);
549     }
550   }
551 }
552 
553 /// Insert restore code for the callee-saved registers used in the function.
554 static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
555                               std::vector<CalleeSavedInfo> &CSI) {
556   MachineFunction &MF = *RestoreBlock.getParent();
557   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
558   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
559   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
560 
561   // Restore all registers immediately before the return and any
562   // terminators that precede it.
563   MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator();
564 
565   if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
566     for (const CalleeSavedInfo &CI : reverse(CSI)) {
567       unsigned Reg = CI.getReg();
568       const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
569       TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
570       assert(I != RestoreBlock.begin() &&
571              "loadRegFromStackSlot didn't insert any code!");
572       // Insert in reverse order.  loadRegFromStackSlot can insert
573       // multiple instructions.
574     }
575   }
576 }
577 
578 void PEI::spillCalleeSavedRegs(MachineFunction &MF) {
579   // We can't list this requirement in getRequiredProperties because some
580   // targets (WebAssembly) use virtual registers past this point, and the pass
581   // pipeline is set up without giving the passes a chance to look at the
582   // TargetMachine.
583   // FIXME: Find a way to express this in getRequiredProperties.
584   assert(MF.getProperties().hasProperty(
585       MachineFunctionProperties::Property::NoVRegs));
586 
587   const Function &F = MF.getFunction();
588   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
589   MachineFrameInfo &MFI = MF.getFrameInfo();
590   MinCSFrameIndex = std::numeric_limits<unsigned>::max();
591   MaxCSFrameIndex = 0;
592 
593   // Determine which of the registers in the callee save list should be saved.
594   BitVector SavedRegs;
595   TFI->determineCalleeSaves(MF, SavedRegs, RS);
596 
597   // Assign stack slots for any callee-saved registers that must be spilled.
598   assignCalleeSavedSpillSlots(MF, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex);
599 
600   // Add the code to save and restore the callee saved registers.
601   if (!F.hasFnAttribute(Attribute::Naked)) {
602     MFI.setCalleeSavedInfoValid(true);
603 
604     std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
605     if (!CSI.empty()) {
606       if (!MFI.hasCalls())
607         NumLeafFuncWithSpills++;
608 
609       for (MachineBasicBlock *SaveBlock : SaveBlocks) {
610         insertCSRSaves(*SaveBlock, CSI);
611         // Update the live-in information of all the blocks up to the save
612         // point.
613         updateLiveness(MF);
614       }
615       for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
616         insertCSRRestores(*RestoreBlock, CSI);
617     }
618   }
619 }
620 
621 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
622 static inline void
623 AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
624                   bool StackGrowsDown, int64_t &Offset,
625                   unsigned &MaxAlign, unsigned Skew) {
626   // If the stack grows down, add the object size to find the lowest address.
627   if (StackGrowsDown)
628     Offset += MFI.getObjectSize(FrameIdx);
629 
630   unsigned Align = MFI.getObjectAlignment(FrameIdx);
631 
632   // If the alignment of this object is greater than that of the stack, then
633   // increase the stack alignment to match.
634   MaxAlign = std::max(MaxAlign, Align);
635 
636   // Adjust to alignment boundary.
637   Offset = alignTo(Offset, Align, Skew);
638 
639   if (StackGrowsDown) {
640     LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
641                       << "]\n");
642     MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
643   } else {
644     LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
645                       << "]\n");
646     MFI.setObjectOffset(FrameIdx, Offset);
647     Offset += MFI.getObjectSize(FrameIdx);
648   }
649 }
650 
651 /// Compute which bytes of fixed and callee-save stack area are unused and keep
652 /// track of them in StackBytesFree.
653 static inline void
654 computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown,
655                       unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex,
656                       int64_t FixedCSEnd, BitVector &StackBytesFree) {
657   // Avoid undefined int64_t -> int conversion below in extreme case.
658   if (FixedCSEnd > std::numeric_limits<int>::max())
659     return;
660 
661   StackBytesFree.resize(FixedCSEnd, true);
662 
663   SmallVector<int, 16> AllocatedFrameSlots;
664   // Add fixed objects.
665   for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
666     AllocatedFrameSlots.push_back(i);
667   // Add callee-save objects.
668   for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i)
669     AllocatedFrameSlots.push_back(i);
670 
671   for (int i : AllocatedFrameSlots) {
672     // These are converted from int64_t, but they should always fit in int
673     // because of the FixedCSEnd check above.
674     int ObjOffset = MFI.getObjectOffset(i);
675     int ObjSize = MFI.getObjectSize(i);
676     int ObjStart, ObjEnd;
677     if (StackGrowsDown) {
678       // ObjOffset is negative when StackGrowsDown is true.
679       ObjStart = -ObjOffset - ObjSize;
680       ObjEnd = -ObjOffset;
681     } else {
682       ObjStart = ObjOffset;
683       ObjEnd = ObjOffset + ObjSize;
684     }
685     // Ignore fixed holes that are in the previous stack frame.
686     if (ObjEnd > 0)
687       StackBytesFree.reset(ObjStart, ObjEnd);
688   }
689 }
690 
691 /// Assign frame object to an unused portion of the stack in the fixed stack
692 /// object range.  Return true if the allocation was successful.
693 static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
694                                      bool StackGrowsDown, unsigned MaxAlign,
695                                      BitVector &StackBytesFree) {
696   if (MFI.isVariableSizedObjectIndex(FrameIdx))
697     return false;
698 
699   if (StackBytesFree.none()) {
700     // clear it to speed up later scavengeStackSlot calls to
701     // StackBytesFree.none()
702     StackBytesFree.clear();
703     return false;
704   }
705 
706   unsigned ObjAlign = MFI.getObjectAlignment(FrameIdx);
707   if (ObjAlign > MaxAlign)
708     return false;
709 
710   int64_t ObjSize = MFI.getObjectSize(FrameIdx);
711   int FreeStart;
712   for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
713        FreeStart = StackBytesFree.find_next(FreeStart)) {
714 
715     // Check that free space has suitable alignment.
716     unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
717     if (alignTo(ObjStart, ObjAlign) != ObjStart)
718       continue;
719 
720     if (FreeStart + ObjSize > StackBytesFree.size())
721       return false;
722 
723     bool AllBytesFree = true;
724     for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
725       if (!StackBytesFree.test(FreeStart + Byte)) {
726         AllBytesFree = false;
727         break;
728       }
729     if (AllBytesFree)
730       break;
731   }
732 
733   if (FreeStart == -1)
734     return false;
735 
736   if (StackGrowsDown) {
737     int ObjStart = -(FreeStart + ObjSize);
738     LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
739                       << ObjStart << "]\n");
740     MFI.setObjectOffset(FrameIdx, ObjStart);
741   } else {
742     LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
743                       << FreeStart << "]\n");
744     MFI.setObjectOffset(FrameIdx, FreeStart);
745   }
746 
747   StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
748   return true;
749 }
750 
751 /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
752 /// those required to be close to the Stack Protector) to stack offsets.
753 static void
754 AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
755                       SmallSet<int, 16> &ProtectedObjs,
756                       MachineFrameInfo &MFI, bool StackGrowsDown,
757                       int64_t &Offset, unsigned &MaxAlign, unsigned Skew) {
758 
759   for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
760         E = UnassignedObjs.end(); I != E; ++I) {
761     int i = *I;
762     AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew);
763     ProtectedObjs.insert(i);
764   }
765 }
766 
767 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
768 /// abstract stack objects.
769 void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
770   const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
771 
772   bool StackGrowsDown =
773     TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
774 
775   // Loop over all of the stack objects, assigning sequential addresses...
776   MachineFrameInfo &MFI = MF.getFrameInfo();
777 
778   // Start at the beginning of the local area.
779   // The Offset is the distance from the stack top in the direction
780   // of stack growth -- so it's always nonnegative.
781   int LocalAreaOffset = TFI.getOffsetOfLocalArea();
782   if (StackGrowsDown)
783     LocalAreaOffset = -LocalAreaOffset;
784   assert(LocalAreaOffset >= 0
785          && "Local area offset should be in direction of stack growth");
786   int64_t Offset = LocalAreaOffset;
787 
788   // Skew to be applied to alignment.
789   unsigned Skew = TFI.getStackAlignmentSkew(MF);
790 
791   // If there are fixed sized objects that are preallocated in the local area,
792   // non-fixed objects can't be allocated right at the start of local area.
793   // Adjust 'Offset' to point to the end of last fixed sized preallocated
794   // object.
795   for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
796     int64_t FixedOff;
797     if (StackGrowsDown) {
798       // The maximum distance from the stack pointer is at lower address of
799       // the object -- which is given by offset. For down growing stack
800       // the offset is negative, so we negate the offset to get the distance.
801       FixedOff = -MFI.getObjectOffset(i);
802     } else {
803       // The maximum distance from the start pointer is at the upper
804       // address of the object.
805       FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
806     }
807     if (FixedOff > Offset) Offset = FixedOff;
808   }
809 
810   // First assign frame offsets to stack objects that are used to spill
811   // callee saved registers.
812   if (StackGrowsDown) {
813     for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
814       // If the stack grows down, we need to add the size to find the lowest
815       // address of the object.
816       Offset += MFI.getObjectSize(i);
817 
818       unsigned Align = MFI.getObjectAlignment(i);
819       // Adjust to alignment boundary
820       Offset = alignTo(Offset, Align, Skew);
821 
822       LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << -Offset << "]\n");
823       MFI.setObjectOffset(i, -Offset);        // Set the computed offset
824     }
825   } else if (MaxCSFrameIndex >= MinCSFrameIndex) {
826     // Be careful about underflow in comparisons agains MinCSFrameIndex.
827     for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) {
828       if (MFI.isDeadObjectIndex(i))
829         continue;
830 
831       unsigned Align = MFI.getObjectAlignment(i);
832       // Adjust to alignment boundary
833       Offset = alignTo(Offset, Align, Skew);
834 
835       LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << Offset << "]\n");
836       MFI.setObjectOffset(i, Offset);
837       Offset += MFI.getObjectSize(i);
838     }
839   }
840 
841   // FixedCSEnd is the stack offset to the end of the fixed and callee-save
842   // stack area.
843   int64_t FixedCSEnd = Offset;
844   unsigned MaxAlign = MFI.getMaxAlignment();
845 
846   // Make sure the special register scavenging spill slot is closest to the
847   // incoming stack pointer if a frame pointer is required and is closer
848   // to the incoming rather than the final stack pointer.
849   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
850   bool EarlyScavengingSlots = (TFI.hasFP(MF) &&
851                                TFI.isFPCloseToIncomingSP() &&
852                                RegInfo->useFPForScavengingIndex(MF) &&
853                                !RegInfo->needsStackRealignment(MF));
854   if (RS && EarlyScavengingSlots) {
855     SmallVector<int, 2> SFIs;
856     RS->getScavengingFrameIndices(SFIs);
857     for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
858            IE = SFIs.end(); I != IE; ++I)
859       AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
860   }
861 
862   // FIXME: Once this is working, then enable flag will change to a target
863   // check for whether the frame is large enough to want to use virtual
864   // frame index registers. Functions which don't want/need this optimization
865   // will continue to use the existing code path.
866   if (MFI.getUseLocalStackAllocationBlock()) {
867     unsigned Align = MFI.getLocalFrameMaxAlign();
868 
869     // Adjust to alignment boundary.
870     Offset = alignTo(Offset, Align, Skew);
871 
872     LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
873 
874     // Resolve offsets for objects in the local block.
875     for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
876       std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
877       int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
878       LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
879                         << "]\n");
880       MFI.setObjectOffset(Entry.first, FIOffset);
881     }
882     // Allocate the local block
883     Offset += MFI.getLocalFrameSize();
884 
885     MaxAlign = std::max(Align, MaxAlign);
886   }
887 
888   // Retrieve the Exception Handler registration node.
889   int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
890   if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo())
891     EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
892 
893   // Make sure that the stack protector comes before the local variables on the
894   // stack.
895   SmallSet<int, 16> ProtectedObjs;
896   if (MFI.getStackProtectorIndex() >= 0) {
897     StackObjSet LargeArrayObjs;
898     StackObjSet SmallArrayObjs;
899     StackObjSet AddrOfObjs;
900 
901     AdjustStackOffset(MFI, MFI.getStackProtectorIndex(), StackGrowsDown,
902                       Offset, MaxAlign, Skew);
903 
904     // Assign large stack objects first.
905     for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
906       if (MFI.isObjectPreAllocated(i) &&
907           MFI.getUseLocalStackAllocationBlock())
908         continue;
909       if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
910         continue;
911       if (RS && RS->isScavengingFrameIndex((int)i))
912         continue;
913       if (MFI.isDeadObjectIndex(i))
914         continue;
915       if (MFI.getStackProtectorIndex() == (int)i ||
916           EHRegNodeFrameIndex == (int)i)
917         continue;
918 
919       switch (MFI.getObjectSSPLayout(i)) {
920       case MachineFrameInfo::SSPLK_None:
921         continue;
922       case MachineFrameInfo::SSPLK_SmallArray:
923         SmallArrayObjs.insert(i);
924         continue;
925       case MachineFrameInfo::SSPLK_AddrOf:
926         AddrOfObjs.insert(i);
927         continue;
928       case MachineFrameInfo::SSPLK_LargeArray:
929         LargeArrayObjs.insert(i);
930         continue;
931       }
932       llvm_unreachable("Unexpected SSPLayoutKind.");
933     }
934 
935     AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
936                           Offset, MaxAlign, Skew);
937     AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
938                           Offset, MaxAlign, Skew);
939     AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
940                           Offset, MaxAlign, Skew);
941   }
942 
943   SmallVector<int, 8> ObjectsToAllocate;
944 
945   // Then prepare to assign frame offsets to stack objects that are not used to
946   // spill callee saved registers.
947   for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
948     if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock())
949       continue;
950     if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
951       continue;
952     if (RS && RS->isScavengingFrameIndex((int)i))
953       continue;
954     if (MFI.isDeadObjectIndex(i))
955       continue;
956     if (MFI.getStackProtectorIndex() == (int)i ||
957         EHRegNodeFrameIndex == (int)i)
958       continue;
959     if (ProtectedObjs.count(i))
960       continue;
961 
962     // Add the objects that we need to allocate to our working set.
963     ObjectsToAllocate.push_back(i);
964   }
965 
966   // Allocate the EH registration node first if one is present.
967   if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
968     AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
969                       MaxAlign, Skew);
970 
971   // Give the targets a chance to order the objects the way they like it.
972   if (MF.getTarget().getOptLevel() != CodeGenOpt::None &&
973       MF.getTarget().Options.StackSymbolOrdering)
974     TFI.orderFrameObjects(MF, ObjectsToAllocate);
975 
976   // Keep track of which bytes in the fixed and callee-save range are used so we
977   // can use the holes when allocating later stack objects.  Only do this if
978   // stack protector isn't being used and the target requests it and we're
979   // optimizing.
980   BitVector StackBytesFree;
981   if (!ObjectsToAllocate.empty() &&
982       MF.getTarget().getOptLevel() != CodeGenOpt::None &&
983       MFI.getStackProtectorIndex() < 0 && TFI.enableStackSlotScavenging(MF))
984     computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex,
985                           FixedCSEnd, StackBytesFree);
986 
987   // Now walk the objects and actually assign base offsets to them.
988   for (auto &Object : ObjectsToAllocate)
989     if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
990                            StackBytesFree))
991       AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign, Skew);
992 
993   // Make sure the special register scavenging spill slot is closest to the
994   // stack pointer.
995   if (RS && !EarlyScavengingSlots) {
996     SmallVector<int, 2> SFIs;
997     RS->getScavengingFrameIndices(SFIs);
998     for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
999            IE = SFIs.end(); I != IE; ++I)
1000       AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
1001   }
1002 
1003   if (!TFI.targetHandlesStackFrameRounding()) {
1004     // If we have reserved argument space for call sites in the function
1005     // immediately on entry to the current function, count it as part of the
1006     // overall stack size.
1007     if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF))
1008       Offset += MFI.getMaxCallFrameSize();
1009 
1010     // Round up the size to a multiple of the alignment.  If the function has
1011     // any calls or alloca's, align to the target's StackAlignment value to
1012     // ensure that the callee's frame or the alloca data is suitably aligned;
1013     // otherwise, for leaf functions, align to the TransientStackAlignment
1014     // value.
1015     unsigned StackAlign;
1016     if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
1017         (RegInfo->needsStackRealignment(MF) && MFI.getObjectIndexEnd() != 0))
1018       StackAlign = TFI.getStackAlignment();
1019     else
1020       StackAlign = TFI.getTransientStackAlignment();
1021 
1022     // If the frame pointer is eliminated, all frame offsets will be relative to
1023     // SP not FP. Align to MaxAlign so this works.
1024     StackAlign = std::max(StackAlign, MaxAlign);
1025     Offset = alignTo(Offset, StackAlign, Skew);
1026   }
1027 
1028   // Update frame info to pretend that this is part of the stack...
1029   int64_t StackSize = Offset - LocalAreaOffset;
1030   MFI.setStackSize(StackSize);
1031   NumBytesStackSpace += StackSize;
1032 }
1033 
1034 /// insertPrologEpilogCode - Scan the function for modified callee saved
1035 /// registers, insert spill code for these callee saved registers, then add
1036 /// prolog and epilog code to the function.
1037 void PEI::insertPrologEpilogCode(MachineFunction &MF) {
1038   const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1039 
1040   // Add prologue to the function...
1041   for (MachineBasicBlock *SaveBlock : SaveBlocks)
1042     TFI.emitPrologue(MF, *SaveBlock);
1043 
1044   // Add epilogue to restore the callee-save registers in each exiting block.
1045   for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
1046     TFI.emitEpilogue(MF, *RestoreBlock);
1047 
1048   for (MachineBasicBlock *SaveBlock : SaveBlocks)
1049     TFI.inlineStackProbe(MF, *SaveBlock);
1050 
1051   // Emit additional code that is required to support segmented stacks, if
1052   // we've been asked for it.  This, when linked with a runtime with support
1053   // for segmented stacks (libgcc is one), will result in allocating stack
1054   // space in small chunks instead of one large contiguous block.
1055   if (MF.shouldSplitStack()) {
1056     for (MachineBasicBlock *SaveBlock : SaveBlocks)
1057       TFI.adjustForSegmentedStacks(MF, *SaveBlock);
1058     // Record that there are split-stack functions, so we will emit a
1059     // special section to tell the linker.
1060     MF.getMMI().setHasSplitStack(true);
1061   } else
1062     MF.getMMI().setHasNosplitStack(true);
1063 
1064   // Emit additional code that is required to explicitly handle the stack in
1065   // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
1066   // approach is rather similar to that of Segmented Stacks, but it uses a
1067   // different conditional check and another BIF for allocating more stack
1068   // space.
1069   if (MF.getFunction().getCallingConv() == CallingConv::HiPE)
1070     for (MachineBasicBlock *SaveBlock : SaveBlocks)
1071       TFI.adjustForHiPEPrologue(MF, *SaveBlock);
1072 }
1073 
1074 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1075 /// register references and actual offsets.
1076 void PEI::replaceFrameIndices(MachineFunction &MF) {
1077   const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1078   if (!TFI.needsFrameIndexResolution(MF)) return;
1079 
1080   // Store SPAdj at exit of a basic block.
1081   SmallVector<int, 8> SPState;
1082   SPState.resize(MF.getNumBlockIDs());
1083   df_iterator_default_set<MachineBasicBlock*> Reachable;
1084 
1085   // Iterate over the reachable blocks in DFS order.
1086   for (auto DFI = df_ext_begin(&MF, Reachable), DFE = df_ext_end(&MF, Reachable);
1087        DFI != DFE; ++DFI) {
1088     int SPAdj = 0;
1089     // Check the exit state of the DFS stack predecessor.
1090     if (DFI.getPathLength() >= 2) {
1091       MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1092       assert(Reachable.count(StackPred) &&
1093              "DFS stack predecessor is already visited.\n");
1094       SPAdj = SPState[StackPred->getNumber()];
1095     }
1096     MachineBasicBlock *BB = *DFI;
1097     replaceFrameIndices(BB, MF, SPAdj);
1098     SPState[BB->getNumber()] = SPAdj;
1099   }
1100 
1101   // Handle the unreachable blocks.
1102   for (auto &BB : MF) {
1103     if (Reachable.count(&BB))
1104       // Already handled in DFS traversal.
1105       continue;
1106     int SPAdj = 0;
1107     replaceFrameIndices(&BB, MF, SPAdj);
1108   }
1109 }
1110 
1111 void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1112                               int &SPAdj) {
1113   assert(MF.getSubtarget().getRegisterInfo() &&
1114          "getRegisterInfo() must be implemented!");
1115   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1116   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1117   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
1118 
1119   if (RS && FrameIndexEliminationScavenging)
1120     RS->enterBasicBlock(*BB);
1121 
1122   bool InsideCallSequence = false;
1123 
1124   for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
1125     if (TII.isFrameInstr(*I)) {
1126       InsideCallSequence = TII.isFrameSetup(*I);
1127       SPAdj += TII.getSPAdjust(*I);
1128       I = TFI->eliminateCallFramePseudoInstr(MF, *BB, I);
1129       continue;
1130     }
1131 
1132     MachineInstr &MI = *I;
1133     bool DoIncr = true;
1134     bool DidFinishLoop = true;
1135     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1136       if (!MI.getOperand(i).isFI())
1137         continue;
1138 
1139       // Frame indices in debug values are encoded in a target independent
1140       // way with simply the frame index and offset rather than any
1141       // target-specific addressing mode.
1142       if (MI.isDebugValue()) {
1143         assert(i == 0 && "Frame indices can only appear as the first "
1144                          "operand of a DBG_VALUE machine instruction");
1145         unsigned Reg;
1146         int64_t Offset =
1147             TFI->getFrameIndexReference(MF, MI.getOperand(0).getIndex(), Reg);
1148         MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
1149         MI.getOperand(0).setIsDebug();
1150         auto *DIExpr = DIExpression::prepend(MI.getDebugExpression(),
1151                                              DIExpression::NoDeref, Offset);
1152         MI.getOperand(3).setMetadata(DIExpr);
1153         continue;
1154       }
1155 
1156       // TODO: This code should be commoned with the code for
1157       // PATCHPOINT. There's no good reason for the difference in
1158       // implementation other than historical accident.  The only
1159       // remaining difference is the unconditional use of the stack
1160       // pointer as the base register.
1161       if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
1162         assert((!MI.isDebugValue() || i == 0) &&
1163                "Frame indicies can only appear as the first operand of a "
1164                "DBG_VALUE machine instruction");
1165         unsigned Reg;
1166         MachineOperand &Offset = MI.getOperand(i + 1);
1167         int refOffset = TFI->getFrameIndexReferencePreferSP(
1168             MF, MI.getOperand(i).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
1169         Offset.setImm(Offset.getImm() + refOffset + SPAdj);
1170         MI.getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
1171         continue;
1172       }
1173 
1174       // Some instructions (e.g. inline asm instructions) can have
1175       // multiple frame indices and/or cause eliminateFrameIndex
1176       // to insert more than one instruction. We need the register
1177       // scavenger to go through all of these instructions so that
1178       // it can update its register information. We keep the
1179       // iterator at the point before insertion so that we can
1180       // revisit them in full.
1181       bool AtBeginning = (I == BB->begin());
1182       if (!AtBeginning) --I;
1183 
1184       // If this instruction has a FrameIndex operand, we need to
1185       // use that target machine register info object to eliminate
1186       // it.
1187       TRI.eliminateFrameIndex(MI, SPAdj, i,
1188                               FrameIndexEliminationScavenging ?  RS : nullptr);
1189 
1190       // Reset the iterator if we were at the beginning of the BB.
1191       if (AtBeginning) {
1192         I = BB->begin();
1193         DoIncr = false;
1194       }
1195 
1196       DidFinishLoop = false;
1197       break;
1198     }
1199 
1200     // If we are looking at a call sequence, we need to keep track of
1201     // the SP adjustment made by each instruction in the sequence.
1202     // This includes both the frame setup/destroy pseudos (handled above),
1203     // as well as other instructions that have side effects w.r.t the SP.
1204     // Note that this must come after eliminateFrameIndex, because
1205     // if I itself referred to a frame index, we shouldn't count its own
1206     // adjustment.
1207     if (DidFinishLoop && InsideCallSequence)
1208       SPAdj += TII.getSPAdjust(MI);
1209 
1210     if (DoIncr && I != BB->end()) ++I;
1211 
1212     // Update register states.
1213     if (RS && FrameIndexEliminationScavenging && DidFinishLoop)
1214       RS->forward(MI);
1215   }
1216 }
1217