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