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