LLVM  8.0.1
PrologEpilogInserter.cpp
Go to the documentation of this file.
1 //===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is responsible for finalizing the functions frame layout, saving
11 // callee saved registers, and for emitting prolog & epilog code for the
12 // function.
13 //
14 // This pass must be run after register allocation. After this pass is
15 // executed, it is illegal to construct MO_FrameIndex operands.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/Statistic.h"
47 #include "llvm/IR/Attributes.h"
48 #include "llvm/IR/CallingConv.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/MC/MCRegisterInfo.h"
55 #include "llvm/Pass.h"
56 #include "llvm/Support/CodeGen.h"
58 #include "llvm/Support/Debug.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 
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 
88  PEI() : MachineFunctionPass(ID) {
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  void insertPrologEpilogCode(MachineFunction &MF);
132 };
133 
134 } // end anonymous namespace
135 
136 char PEI::ID = 0;
137 
139 
140 static cl::opt<unsigned>
141 WarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1),
142  cl::desc("Warn for stack size bigger than the given"
143  " number"));
144 
145 INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false,
146  false)
151  "Prologue/Epilogue Insertion & Frame Finalization", false,
152  false)
153 
155  return new PEI();
156 }
157 
158 STATISTIC(NumBytesStackSpace,
159  "Number of bytes used for stack in all functions");
160 
161 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
162  AU.setPreservesCFG();
167 }
168 
169 /// StackObjSet - A set of stack object indexes
171 
172 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
173 /// frame indexes with appropriate references.
174 bool PEI::runOnMachineFunction(MachineFunction &MF) {
175  NumFuncSeen++;
176  const Function &F = MF.getFunction();
179 
180  RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
181  FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
182  FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
184  ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
185 
186  // Calculate the MaxCallFrameSize and AdjustsStack variables for the
187  // function's frame information. Also eliminates call frame pseudo
188  // instructions.
189  calculateCallFrameInfo(MF);
190 
191  // Determine placement of CSR spill/restore code and prolog/epilog code:
192  // place all spills in the entry block, all restores in return blocks.
193  calculateSaveRestoreBlocks(MF);
194 
195  // Handle CSR spilling and restoring, for targets that need it.
196  if (MF.getTarget().usesPhysRegsForPEI())
197  spillCalleeSavedRegs(MF);
198 
199  // Allow the target machine to make final modifications to the function
200  // before the frame layout is finalized.
202 
203  // Calculate actual frame offsets for all abstract stack objects...
204  calculateFrameObjectOffsets(MF);
205 
206  // Add prolog and epilog code to the function. This function is required
207  // to align the stack frame as necessary for any stack variables or
208  // called functions. Because of this, calculateCalleeSavedRegisters()
209  // must be called before this function in order to set the AdjustsStack
210  // and MaxCallFrameSize variables.
212  insertPrologEpilogCode(MF);
213 
214  // Replace all MO_FrameIndex operands with physical register references
215  // and actual offsets.
216  //
217  replaceFrameIndices(MF);
218 
219  // If register scavenging is needed, as we've enabled doing it as a
220  // post-pass, scavenge the virtual registers that frame index elimination
221  // inserted.
222  if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
223  scavengeFrameVirtualRegs(MF, *RS);
224 
225  // Warn on stack size when we exceeds the given limit.
226  MachineFrameInfo &MFI = MF.getFrameInfo();
227  uint64_t StackSize = MFI.getStackSize();
228  if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
229  DiagnosticInfoStackSize DiagStackSize(F, StackSize);
230  F.getContext().diagnose(DiagStackSize);
231  }
232  ORE->emit([&]() {
233  return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
234  MF.getFunction().getSubprogram(),
235  &MF.front())
236  << ore::NV("NumStackBytes", StackSize) << " stack bytes in function";
237  });
238 
239  delete RS;
240  SaveBlocks.clear();
241  RestoreBlocks.clear();
242  MFI.setSavePoint(nullptr);
243  MFI.setRestorePoint(nullptr);
244  return true;
245 }
246 
247 /// Calculate the MaxCallFrameSize and AdjustsStack
248 /// variables for the function's frame information and eliminate call frame
249 /// pseudo instructions.
250 void PEI::calculateCallFrameInfo(MachineFunction &MF) {
251  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
253  MachineFrameInfo &MFI = MF.getFrameInfo();
254 
255  unsigned MaxCallFrameSize = 0;
256  bool AdjustsStack = MFI.adjustsStack();
257 
258  // Get the function call frame set-up and tear-down instruction opcode
259  unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
260  unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
261 
262  // Early exit for targets which have no call frame setup/destroy pseudo
263  // instructions.
264  if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
265  return;
266 
267  std::vector<MachineBasicBlock::iterator> FrameSDOps;
268  for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
269  for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
270  if (TII.isFrameInstr(*I)) {
271  unsigned Size = TII.getFrameSize(*I);
272  if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
273  AdjustsStack = true;
274  FrameSDOps.push_back(I);
275  } else if (I->isInlineAsm()) {
276  // Some inline asm's need a stack frame, as indicated by operand 1.
277  unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
278  if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
279  AdjustsStack = true;
280  }
281 
283  (MFI.getMaxCallFrameSize() == MaxCallFrameSize &&
284  MFI.adjustsStack() == AdjustsStack));
285  MFI.setAdjustsStack(AdjustsStack);
286  MFI.setMaxCallFrameSize(MaxCallFrameSize);
287 
288  for (std::vector<MachineBasicBlock::iterator>::iterator
289  i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
291 
292  // If call frames are not being included as part of the stack frame, and
293  // the target doesn't indicate otherwise, remove the call frame pseudos
294  // here. The sub/add sp instruction pairs are still inserted, but we don't
295  // need to track the SP adjustment for frame index elimination.
296  if (TFI->canSimplifyCallFramePseudos(MF))
297  TFI->eliminateCallFramePseudoInstr(MF, *I->getParent(), I);
298  }
299 }
300 
301 /// Compute the sets of entry and return blocks for saving and restoring
302 /// callee-saved registers, and placing prolog and epilog code.
303 void PEI::calculateSaveRestoreBlocks(MachineFunction &MF) {
304  const MachineFrameInfo &MFI = MF.getFrameInfo();
305 
306  // Even when we do not change any CSR, we still want to insert the
307  // prologue and epilogue of the function.
308  // So set the save points for those.
309 
310  // Use the points found by shrink-wrapping, if any.
311  if (MFI.getSavePoint()) {
312  SaveBlocks.push_back(MFI.getSavePoint());
313  assert(MFI.getRestorePoint() && "Both restore and save must be set");
314  MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
315  // If RestoreBlock does not have any successor and is not a return block
316  // then the end point is unreachable and we do not need to insert any
317  // epilogue.
318  if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
319  RestoreBlocks.push_back(RestoreBlock);
320  return;
321  }
322 
323  // Save refs to entry and return blocks.
324  SaveBlocks.push_back(&MF.front());
325  for (MachineBasicBlock &MBB : MF) {
326  if (MBB.isEHFuncletEntry())
327  SaveBlocks.push_back(&MBB);
328  if (MBB.isReturnBlock())
329  RestoreBlocks.push_back(&MBB);
330  }
331 }
332 
334  const BitVector &SavedRegs,
335  unsigned &MinCSFrameIndex,
336  unsigned &MaxCSFrameIndex) {
337  if (SavedRegs.empty())
338  return;
339 
340  const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
341  const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
342 
343  std::vector<CalleeSavedInfo> CSI;
344  for (unsigned i = 0; CSRegs[i]; ++i) {
345  unsigned Reg = CSRegs[i];
346  if (SavedRegs.test(Reg))
347  CSI.push_back(CalleeSavedInfo(Reg));
348  }
349 
351  MachineFrameInfo &MFI = F.getFrameInfo();
352  if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
353  // If target doesn't implement this, use generic code.
354 
355  if (CSI.empty())
356  return; // Early exit if no callee saved registers are modified!
357 
358  unsigned NumFixedSpillSlots;
359  const TargetFrameLowering::SpillSlot *FixedSpillSlots =
360  TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
361 
362  // Now that we know which registers need to be saved and restored, allocate
363  // stack slots for them.
364  for (auto &CS : CSI) {
365  // If the target has spilled this register to another register, we don't
366  // need to allocate a stack slot.
367  if (CS.isSpilledToReg())
368  continue;
369 
370  unsigned Reg = CS.getReg();
371  const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
372 
373  int FrameIdx;
374  if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
375  CS.setFrameIdx(FrameIdx);
376  continue;
377  }
378 
379  // Check to see if this physreg must be spilled to a particular stack slot
380  // on this target.
381  const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
382  while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
383  FixedSlot->Reg != Reg)
384  ++FixedSlot;
385 
386  unsigned Size = RegInfo->getSpillSize(*RC);
387  if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
388  // Nope, just spill it anywhere convenient.
389  unsigned Align = RegInfo->getSpillAlignment(*RC);
390  unsigned StackAlign = TFI->getStackAlignment();
391 
392  // We may not be able to satisfy the desired alignment specification of
393  // the TargetRegisterClass if the stack alignment is smaller. Use the
394  // min.
395  Align = std::min(Align, StackAlign);
396  FrameIdx = MFI.CreateStackObject(Size, Align, true);
397  if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
398  if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
399  } else {
400  // Spill it to the stack where we must.
401  FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset);
402  }
403 
404  CS.setFrameIdx(FrameIdx);
405  }
406  }
407 
408  MFI.setCalleeSavedInfo(CSI);
409 }
410 
411 /// Helper function to update the liveness information for the callee-saved
412 /// registers.
413 static void updateLiveness(MachineFunction &MF) {
414  MachineFrameInfo &MFI = MF.getFrameInfo();
415  // Visited will contain all the basic blocks that are in the region
416  // where the callee saved registers are alive:
417  // - Anything that is not Save or Restore -> LiveThrough.
418  // - Save -> LiveIn.
419  // - Restore -> LiveOut.
420  // The live-out is not attached to the block, so no need to keep
421  // Restore in this set.
424  MachineBasicBlock *Entry = &MF.front();
425  MachineBasicBlock *Save = MFI.getSavePoint();
426 
427  if (!Save)
428  Save = Entry;
429 
430  if (Entry != Save) {
431  WorkList.push_back(Entry);
432  Visited.insert(Entry);
433  }
434  Visited.insert(Save);
435 
436  MachineBasicBlock *Restore = MFI.getRestorePoint();
437  if (Restore)
438  // By construction Restore cannot be visited, otherwise it
439  // means there exists a path to Restore that does not go
440  // through Save.
441  WorkList.push_back(Restore);
442 
443  while (!WorkList.empty()) {
444  const MachineBasicBlock *CurBB = WorkList.pop_back_val();
445  // By construction, the region that is after the save point is
446  // dominated by the Save and post-dominated by the Restore.
447  if (CurBB == Save && Save != Restore)
448  continue;
449  // Enqueue all the successors not already visited.
450  // Those are by construction either before Save or after Restore.
451  for (MachineBasicBlock *SuccBB : CurBB->successors())
452  if (Visited.insert(SuccBB).second)
453  WorkList.push_back(SuccBB);
454  }
455 
456  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
457 
459  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
460  for (MachineBasicBlock *MBB : Visited) {
461  MCPhysReg Reg = CSI[i].getReg();
462  // Add the callee-saved register as live-in.
463  // It's killed at the spill.
464  if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg))
465  MBB->addLiveIn(Reg);
466  }
467  // If callee-saved register is spilled to another register rather than
468  // spilling to stack, the destination register has to be marked as live for
469  // each MBB between the prologue and epilogue so that it is not clobbered
470  // before it is reloaded in the epilogue. The Visited set contains all
471  // blocks outside of the region delimited by prologue/epilogue.
472  if (CSI[i].isSpilledToReg()) {
473  for (MachineBasicBlock &MBB : MF) {
474  if (Visited.count(&MBB))
475  continue;
476  MCPhysReg DstReg = CSI[i].getDstReg();
477  if (!MBB.isLiveIn(DstReg))
478  MBB.addLiveIn(DstReg);
479  }
480  }
481  }
482 
483 }
484 
485 /// Insert restore code for the callee-saved registers used in the function.
486 static void insertCSRSaves(MachineBasicBlock &SaveBlock,
488  MachineFunction &MF = *SaveBlock.getParent();
489  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
492 
493  MachineBasicBlock::iterator I = SaveBlock.begin();
494  if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
495  for (const CalleeSavedInfo &CS : CSI) {
496  // Insert the spill to the stack frame.
497  unsigned Reg = CS.getReg();
498  const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
499  TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC,
500  TRI);
501  }
502  }
503 }
504 
505 /// Insert restore code for the callee-saved registers used in the function.
506 static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
507  std::vector<CalleeSavedInfo> &CSI) {
508  MachineFunction &MF = *RestoreBlock.getParent();
509  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
512 
513  // Restore all registers immediately before the return and any
514  // terminators that precede it.
516 
517  if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
518  for (const CalleeSavedInfo &CI : reverse(CSI)) {
519  unsigned Reg = CI.getReg();
520  const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
521  TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
522  assert(I != RestoreBlock.begin() &&
523  "loadRegFromStackSlot didn't insert any code!");
524  // Insert in reverse order. loadRegFromStackSlot can insert
525  // multiple instructions.
526  }
527  }
528 }
529 
530 void PEI::spillCalleeSavedRegs(MachineFunction &MF) {
531  // We can't list this requirement in getRequiredProperties because some
532  // targets (WebAssembly) use virtual registers past this point, and the pass
533  // pipeline is set up without giving the passes a chance to look at the
534  // TargetMachine.
535  // FIXME: Find a way to express this in getRequiredProperties.
538 
539  const Function &F = MF.getFunction();
541  MachineFrameInfo &MFI = MF.getFrameInfo();
542  MinCSFrameIndex = std::numeric_limits<unsigned>::max();
543  MaxCSFrameIndex = 0;
544 
545  // Determine which of the registers in the callee save list should be saved.
546  BitVector SavedRegs;
547  TFI->determineCalleeSaves(MF, SavedRegs, RS);
548 
549  // Assign stack slots for any callee-saved registers that must be spilled.
550  assignCalleeSavedSpillSlots(MF, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex);
551 
552  // Add the code to save and restore the callee saved registers.
553  if (!F.hasFnAttribute(Attribute::Naked)) {
554  MFI.setCalleeSavedInfoValid(true);
555 
556  std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
557  if (!CSI.empty()) {
558  if (!MFI.hasCalls())
559  NumLeafFuncWithSpills++;
560 
561  for (MachineBasicBlock *SaveBlock : SaveBlocks) {
562  insertCSRSaves(*SaveBlock, CSI);
563  // Update the live-in information of all the blocks up to the save
564  // point.
565  updateLiveness(MF);
566  }
567  for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
568  insertCSRRestores(*RestoreBlock, CSI);
569  }
570  }
571 }
572 
573 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
574 static inline void
576  bool StackGrowsDown, int64_t &Offset,
577  unsigned &MaxAlign, unsigned Skew) {
578  // If the stack grows down, add the object size to find the lowest address.
579  if (StackGrowsDown)
580  Offset += MFI.getObjectSize(FrameIdx);
581 
582  unsigned Align = MFI.getObjectAlignment(FrameIdx);
583 
584  // If the alignment of this object is greater than that of the stack, then
585  // increase the stack alignment to match.
586  MaxAlign = std::max(MaxAlign, Align);
587 
588  // Adjust to alignment boundary.
589  Offset = alignTo(Offset, Align, Skew);
590 
591  if (StackGrowsDown) {
592  LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
593  << "]\n");
594  MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
595  } else {
596  LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
597  << "]\n");
598  MFI.setObjectOffset(FrameIdx, Offset);
599  Offset += MFI.getObjectSize(FrameIdx);
600  }
601 }
602 
603 /// Compute which bytes of fixed and callee-save stack area are unused and keep
604 /// track of them in StackBytesFree.
605 static inline void
606 computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown,
607  unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex,
608  int64_t FixedCSEnd, BitVector &StackBytesFree) {
609  // Avoid undefined int64_t -> int conversion below in extreme case.
610  if (FixedCSEnd > std::numeric_limits<int>::max())
611  return;
612 
613  StackBytesFree.resize(FixedCSEnd, true);
614 
615  SmallVector<int, 16> AllocatedFrameSlots;
616  // Add fixed objects.
617  for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
618  AllocatedFrameSlots.push_back(i);
619  // Add callee-save objects.
620  for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i)
621  AllocatedFrameSlots.push_back(i);
622 
623  for (int i : AllocatedFrameSlots) {
624  // These are converted from int64_t, but they should always fit in int
625  // because of the FixedCSEnd check above.
626  int ObjOffset = MFI.getObjectOffset(i);
627  int ObjSize = MFI.getObjectSize(i);
628  int ObjStart, ObjEnd;
629  if (StackGrowsDown) {
630  // ObjOffset is negative when StackGrowsDown is true.
631  ObjStart = -ObjOffset - ObjSize;
632  ObjEnd = -ObjOffset;
633  } else {
634  ObjStart = ObjOffset;
635  ObjEnd = ObjOffset + ObjSize;
636  }
637  // Ignore fixed holes that are in the previous stack frame.
638  if (ObjEnd > 0)
639  StackBytesFree.reset(ObjStart, ObjEnd);
640  }
641 }
642 
643 /// Assign frame object to an unused portion of the stack in the fixed stack
644 /// object range. Return true if the allocation was successful.
645 static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
646  bool StackGrowsDown, unsigned MaxAlign,
647  BitVector &StackBytesFree) {
648  if (MFI.isVariableSizedObjectIndex(FrameIdx))
649  return false;
650 
651  if (StackBytesFree.none()) {
652  // clear it to speed up later scavengeStackSlot calls to
653  // StackBytesFree.none()
654  StackBytesFree.clear();
655  return false;
656  }
657 
658  unsigned ObjAlign = MFI.getObjectAlignment(FrameIdx);
659  if (ObjAlign > MaxAlign)
660  return false;
661 
662  int64_t ObjSize = MFI.getObjectSize(FrameIdx);
663  int FreeStart;
664  for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
665  FreeStart = StackBytesFree.find_next(FreeStart)) {
666 
667  // Check that free space has suitable alignment.
668  unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
669  if (alignTo(ObjStart, ObjAlign) != ObjStart)
670  continue;
671 
672  if (FreeStart + ObjSize > StackBytesFree.size())
673  return false;
674 
675  bool AllBytesFree = true;
676  for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
677  if (!StackBytesFree.test(FreeStart + Byte)) {
678  AllBytesFree = false;
679  break;
680  }
681  if (AllBytesFree)
682  break;
683  }
684 
685  if (FreeStart == -1)
686  return false;
687 
688  if (StackGrowsDown) {
689  int ObjStart = -(FreeStart + ObjSize);
690  LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
691  << ObjStart << "]\n");
692  MFI.setObjectOffset(FrameIdx, ObjStart);
693  } else {
694  LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
695  << FreeStart << "]\n");
696  MFI.setObjectOffset(FrameIdx, FreeStart);
697  }
698 
699  StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
700  return true;
701 }
702 
703 /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
704 /// those required to be close to the Stack Protector) to stack offsets.
705 static void
706 AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
707  SmallSet<int, 16> &ProtectedObjs,
708  MachineFrameInfo &MFI, bool StackGrowsDown,
709  int64_t &Offset, unsigned &MaxAlign, unsigned Skew) {
710 
711  for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
712  E = UnassignedObjs.end(); I != E; ++I) {
713  int i = *I;
714  AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew);
715  ProtectedObjs.insert(i);
716  }
717 }
718 
719 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
720 /// abstract stack objects.
721 void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
723 
724  bool StackGrowsDown =
726 
727  // Loop over all of the stack objects, assigning sequential addresses...
728  MachineFrameInfo &MFI = MF.getFrameInfo();
729 
730  // Start at the beginning of the local area.
731  // The Offset is the distance from the stack top in the direction
732  // of stack growth -- so it's always nonnegative.
733  int LocalAreaOffset = TFI.getOffsetOfLocalArea();
734  if (StackGrowsDown)
735  LocalAreaOffset = -LocalAreaOffset;
736  assert(LocalAreaOffset >= 0
737  && "Local area offset should be in direction of stack growth");
738  int64_t Offset = LocalAreaOffset;
739 
740  // Skew to be applied to alignment.
741  unsigned Skew = TFI.getStackAlignmentSkew(MF);
742 
743  // If there are fixed sized objects that are preallocated in the local area,
744  // non-fixed objects can't be allocated right at the start of local area.
745  // Adjust 'Offset' to point to the end of last fixed sized preallocated
746  // object.
747  for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
748  int64_t FixedOff;
749  if (StackGrowsDown) {
750  // The maximum distance from the stack pointer is at lower address of
751  // the object -- which is given by offset. For down growing stack
752  // the offset is negative, so we negate the offset to get the distance.
753  FixedOff = -MFI.getObjectOffset(i);
754  } else {
755  // The maximum distance from the start pointer is at the upper
756  // address of the object.
757  FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
758  }
759  if (FixedOff > Offset) Offset = FixedOff;
760  }
761 
762  // First assign frame offsets to stack objects that are used to spill
763  // callee saved registers.
764  if (StackGrowsDown) {
765  for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
766  // If the stack grows down, we need to add the size to find the lowest
767  // address of the object.
768  Offset += MFI.getObjectSize(i);
769 
770  unsigned Align = MFI.getObjectAlignment(i);
771  // Adjust to alignment boundary
772  Offset = alignTo(Offset, Align, Skew);
773 
774  LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << -Offset << "]\n");
775  MFI.setObjectOffset(i, -Offset); // Set the computed offset
776  }
777  } else if (MaxCSFrameIndex >= MinCSFrameIndex) {
778  // Be careful about underflow in comparisons agains MinCSFrameIndex.
779  for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) {
780  if (MFI.isDeadObjectIndex(i))
781  continue;
782 
783  unsigned Align = MFI.getObjectAlignment(i);
784  // Adjust to alignment boundary
785  Offset = alignTo(Offset, Align, Skew);
786 
787  LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << Offset << "]\n");
788  MFI.setObjectOffset(i, Offset);
789  Offset += MFI.getObjectSize(i);
790  }
791  }
792 
793  // FixedCSEnd is the stack offset to the end of the fixed and callee-save
794  // stack area.
795  int64_t FixedCSEnd = Offset;
796  unsigned MaxAlign = MFI.getMaxAlignment();
797 
798  // Make sure the special register scavenging spill slot is closest to the
799  // incoming stack pointer if a frame pointer is required and is closer
800  // to the incoming rather than the final stack pointer.
801  const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
802  bool EarlyScavengingSlots = (TFI.hasFP(MF) &&
803  TFI.isFPCloseToIncomingSP() &&
804  RegInfo->useFPForScavengingIndex(MF) &&
805  !RegInfo->needsStackRealignment(MF));
806  if (RS && EarlyScavengingSlots) {
807  SmallVector<int, 2> SFIs;
808  RS->getScavengingFrameIndices(SFIs);
809  for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
810  IE = SFIs.end(); I != IE; ++I)
811  AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
812  }
813 
814  // FIXME: Once this is working, then enable flag will change to a target
815  // check for whether the frame is large enough to want to use virtual
816  // frame index registers. Functions which don't want/need this optimization
817  // will continue to use the existing code path.
819  unsigned Align = MFI.getLocalFrameMaxAlign();
820 
821  // Adjust to alignment boundary.
822  Offset = alignTo(Offset, Align, Skew);
823 
824  LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
825 
826  // Resolve offsets for objects in the local block.
827  for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
828  std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
829  int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
830  LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
831  << "]\n");
832  MFI.setObjectOffset(Entry.first, FIOffset);
833  }
834  // Allocate the local block
835  Offset += MFI.getLocalFrameSize();
836 
837  MaxAlign = std::max(Align, MaxAlign);
838  }
839 
840  // Retrieve the Exception Handler registration node.
841  int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
842  if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo())
843  EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
844 
845  // Make sure that the stack protector comes before the local variables on the
846  // stack.
847  SmallSet<int, 16> ProtectedObjs;
848  if (MFI.getStackProtectorIndex() >= 0) {
849  StackObjSet LargeArrayObjs;
850  StackObjSet SmallArrayObjs;
851  StackObjSet AddrOfObjs;
852 
853  AdjustStackOffset(MFI, MFI.getStackProtectorIndex(), StackGrowsDown,
854  Offset, MaxAlign, Skew);
855 
856  // Assign large stack objects first.
857  for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
858  if (MFI.isObjectPreAllocated(i) &&
860  continue;
861  if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
862  continue;
863  if (RS && RS->isScavengingFrameIndex((int)i))
864  continue;
865  if (MFI.isDeadObjectIndex(i))
866  continue;
867  if (MFI.getStackProtectorIndex() == (int)i ||
868  EHRegNodeFrameIndex == (int)i)
869  continue;
870 
871  switch (MFI.getObjectSSPLayout(i)) {
873  continue;
875  SmallArrayObjs.insert(i);
876  continue;
878  AddrOfObjs.insert(i);
879  continue;
881  LargeArrayObjs.insert(i);
882  continue;
883  }
884  llvm_unreachable("Unexpected SSPLayoutKind.");
885  }
886 
887  AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
888  Offset, MaxAlign, Skew);
889  AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
890  Offset, MaxAlign, Skew);
891  AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
892  Offset, MaxAlign, Skew);
893  }
894 
895  SmallVector<int, 8> ObjectsToAllocate;
896 
897  // Then prepare to assign frame offsets to stack objects that are not used to
898  // spill callee saved registers.
899  for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
901  continue;
902  if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
903  continue;
904  if (RS && RS->isScavengingFrameIndex((int)i))
905  continue;
906  if (MFI.isDeadObjectIndex(i))
907  continue;
908  if (MFI.getStackProtectorIndex() == (int)i ||
909  EHRegNodeFrameIndex == (int)i)
910  continue;
911  if (ProtectedObjs.count(i))
912  continue;
913 
914  // Add the objects that we need to allocate to our working set.
915  ObjectsToAllocate.push_back(i);
916  }
917 
918  // Allocate the EH registration node first if one is present.
919  if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
920  AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
921  MaxAlign, Skew);
922 
923  // Give the targets a chance to order the objects the way they like it.
924  if (MF.getTarget().getOptLevel() != CodeGenOpt::None &&
926  TFI.orderFrameObjects(MF, ObjectsToAllocate);
927 
928  // Keep track of which bytes in the fixed and callee-save range are used so we
929  // can use the holes when allocating later stack objects. Only do this if
930  // stack protector isn't being used and the target requests it and we're
931  // optimizing.
932  BitVector StackBytesFree;
933  if (!ObjectsToAllocate.empty() &&
936  computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex,
937  FixedCSEnd, StackBytesFree);
938 
939  // Now walk the objects and actually assign base offsets to them.
940  for (auto &Object : ObjectsToAllocate)
941  if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
942  StackBytesFree))
943  AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign, Skew);
944 
945  // Make sure the special register scavenging spill slot is closest to the
946  // stack pointer.
947  if (RS && !EarlyScavengingSlots) {
948  SmallVector<int, 2> SFIs;
949  RS->getScavengingFrameIndices(SFIs);
950  for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
951  IE = SFIs.end(); I != IE; ++I)
952  AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
953  }
954 
955  if (!TFI.targetHandlesStackFrameRounding()) {
956  // If we have reserved argument space for call sites in the function
957  // immediately on entry to the current function, count it as part of the
958  // overall stack size.
959  if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF))
960  Offset += MFI.getMaxCallFrameSize();
961 
962  // Round up the size to a multiple of the alignment. If the function has
963  // any calls or alloca's, align to the target's StackAlignment value to
964  // ensure that the callee's frame or the alloca data is suitably aligned;
965  // otherwise, for leaf functions, align to the TransientStackAlignment
966  // value.
967  unsigned StackAlign;
968  if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
969  (RegInfo->needsStackRealignment(MF) && MFI.getObjectIndexEnd() != 0))
970  StackAlign = TFI.getStackAlignment();
971  else
972  StackAlign = TFI.getTransientStackAlignment();
973 
974  // If the frame pointer is eliminated, all frame offsets will be relative to
975  // SP not FP. Align to MaxAlign so this works.
976  StackAlign = std::max(StackAlign, MaxAlign);
977  Offset = alignTo(Offset, StackAlign, Skew);
978  }
979 
980  // Update frame info to pretend that this is part of the stack...
981  int64_t StackSize = Offset - LocalAreaOffset;
982  MFI.setStackSize(StackSize);
983  NumBytesStackSpace += StackSize;
984 }
985 
986 /// insertPrologEpilogCode - Scan the function for modified callee saved
987 /// registers, insert spill code for these callee saved registers, then add
988 /// prolog and epilog code to the function.
989 void PEI::insertPrologEpilogCode(MachineFunction &MF) {
991 
992  // Add prologue to the function...
993  for (MachineBasicBlock *SaveBlock : SaveBlocks)
994  TFI.emitPrologue(MF, *SaveBlock);
995 
996  // Add epilogue to restore the callee-save registers in each exiting block.
997  for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
998  TFI.emitEpilogue(MF, *RestoreBlock);
999 
1000  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1001  TFI.inlineStackProbe(MF, *SaveBlock);
1002 
1003  // Emit additional code that is required to support segmented stacks, if
1004  // we've been asked for it. This, when linked with a runtime with support
1005  // for segmented stacks (libgcc is one), will result in allocating stack
1006  // space in small chunks instead of one large contiguous block.
1007  if (MF.shouldSplitStack()) {
1008  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1009  TFI.adjustForSegmentedStacks(MF, *SaveBlock);
1010  // Record that there are split-stack functions, so we will emit a
1011  // special section to tell the linker.
1012  MF.getMMI().setHasSplitStack(true);
1013  } else
1014  MF.getMMI().setHasNosplitStack(true);
1015 
1016  // Emit additional code that is required to explicitly handle the stack in
1017  // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
1018  // approach is rather similar to that of Segmented Stacks, but it uses a
1019  // different conditional check and another BIF for allocating more stack
1020  // space.
1022  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1023  TFI.adjustForHiPEPrologue(MF, *SaveBlock);
1024 }
1025 
1026 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1027 /// register references and actual offsets.
1028 void PEI::replaceFrameIndices(MachineFunction &MF) {
1029  const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1030  if (!TFI.needsFrameIndexResolution(MF)) return;
1031 
1032  // Store SPAdj at exit of a basic block.
1033  SmallVector<int, 8> SPState;
1034  SPState.resize(MF.getNumBlockIDs());
1036 
1037  // Iterate over the reachable blocks in DFS order.
1038  for (auto DFI = df_ext_begin(&MF, Reachable), DFE = df_ext_end(&MF, Reachable);
1039  DFI != DFE; ++DFI) {
1040  int SPAdj = 0;
1041  // Check the exit state of the DFS stack predecessor.
1042  if (DFI.getPathLength() >= 2) {
1043  MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1044  assert(Reachable.count(StackPred) &&
1045  "DFS stack predecessor is already visited.\n");
1046  SPAdj = SPState[StackPred->getNumber()];
1047  }
1048  MachineBasicBlock *BB = *DFI;
1049  replaceFrameIndices(BB, MF, SPAdj);
1050  SPState[BB->getNumber()] = SPAdj;
1051  }
1052 
1053  // Handle the unreachable blocks.
1054  for (auto &BB : MF) {
1055  if (Reachable.count(&BB))
1056  // Already handled in DFS traversal.
1057  continue;
1058  int SPAdj = 0;
1059  replaceFrameIndices(&BB, MF, SPAdj);
1060  }
1061 }
1062 
1063 void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1064  int &SPAdj) {
1066  "getRegisterInfo() must be implemented!");
1067  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1070 
1071  if (RS && FrameIndexEliminationScavenging)
1072  RS->enterBasicBlock(*BB);
1073 
1074  bool InsideCallSequence = false;
1075 
1076  for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
1077  if (TII.isFrameInstr(*I)) {
1078  InsideCallSequence = TII.isFrameSetup(*I);
1079  SPAdj += TII.getSPAdjust(*I);
1080  I = TFI->eliminateCallFramePseudoInstr(MF, *BB, I);
1081  continue;
1082  }
1083 
1084  MachineInstr &MI = *I;
1085  bool DoIncr = true;
1086  bool DidFinishLoop = true;
1087  for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1088  if (!MI.getOperand(i).isFI())
1089  continue;
1090 
1091  // Frame indices in debug values are encoded in a target independent
1092  // way with simply the frame index and offset rather than any
1093  // target-specific addressing mode.
1094  if (MI.isDebugValue()) {
1095  assert(i == 0 && "Frame indices can only appear as the first "
1096  "operand of a DBG_VALUE machine instruction");
1097  unsigned Reg;
1098  int64_t Offset =
1099  TFI->getFrameIndexReference(MF, MI.getOperand(0).getIndex(), Reg);
1100  MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
1101  MI.getOperand(0).setIsDebug();
1102  auto *DIExpr = DIExpression::prepend(MI.getDebugExpression(),
1104  MI.getOperand(3).setMetadata(DIExpr);
1105  continue;
1106  }
1107 
1108  // TODO: This code should be commoned with the code for
1109  // PATCHPOINT. There's no good reason for the difference in
1110  // implementation other than historical accident. The only
1111  // remaining difference is the unconditional use of the stack
1112  // pointer as the base register.
1113  if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
1114  assert((!MI.isDebugValue() || i == 0) &&
1115  "Frame indicies can only appear as the first operand of a "
1116  "DBG_VALUE machine instruction");
1117  unsigned Reg;
1118  MachineOperand &Offset = MI.getOperand(i + 1);
1119  int refOffset = TFI->getFrameIndexReferencePreferSP(
1120  MF, MI.getOperand(i).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
1121  Offset.setImm(Offset.getImm() + refOffset + SPAdj);
1122  MI.getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
1123  continue;
1124  }
1125 
1126  // Some instructions (e.g. inline asm instructions) can have
1127  // multiple frame indices and/or cause eliminateFrameIndex
1128  // to insert more than one instruction. We need the register
1129  // scavenger to go through all of these instructions so that
1130  // it can update its register information. We keep the
1131  // iterator at the point before insertion so that we can
1132  // revisit them in full.
1133  bool AtBeginning = (I == BB->begin());
1134  if (!AtBeginning) --I;
1135 
1136  // If this instruction has a FrameIndex operand, we need to
1137  // use that target machine register info object to eliminate
1138  // it.
1139  TRI.eliminateFrameIndex(MI, SPAdj, i,
1140  FrameIndexEliminationScavenging ? RS : nullptr);
1141 
1142  // Reset the iterator if we were at the beginning of the BB.
1143  if (AtBeginning) {
1144  I = BB->begin();
1145  DoIncr = false;
1146  }
1147 
1148  DidFinishLoop = false;
1149  break;
1150  }
1151 
1152  // If we are looking at a call sequence, we need to keep track of
1153  // the SP adjustment made by each instruction in the sequence.
1154  // This includes both the frame setup/destroy pseudos (handled above),
1155  // as well as other instructions that have side effects w.r.t the SP.
1156  // Note that this must come after eliminateFrameIndex, because
1157  // if I itself referred to a frame index, we shouldn't count its own
1158  // adjustment.
1159  if (DidFinishLoop && InsideCallSequence)
1160  SPAdj += TII.getSPAdjust(MI);
1161 
1162  if (DoIncr && I != BB->end()) ++I;
1163 
1164  // Update register states.
1165  if (RS && FrameIndexEliminationScavenging && DidFinishLoop)
1166  RS->forward(MI);
1167  }
1168 }
unsigned StackSymbolOrdering
StackSymbolOrdering - When true, this will allow CodeGen to order the local stack symbols (for code s...
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:372
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
void setSavePoint(MachineBasicBlock *NewSave)
void setRestorePoint(MachineBasicBlock *NewRestore)
virtual bool requiresRegisterScavenging(const MachineFunction &MF) const
Returns true if the target requires (and can make use of) the register scavenger. ...
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, bool StackGrowsDown, int64_t &Offset, unsigned &MaxAlign, unsigned Skew)
AdjustStackOffset - Helper function used to adjust the stack frame offset.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
void setCalleeSavedInfoValid(bool v)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
bool empty() const
empty - Tests whether there are no bits in this bitvector.
Definition: BitVector.h:167
static void computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown, unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex, int64_t FixedCSEnd, BitVector &StackBytesFree)
Compute which bytes of fixed and callee-save stack area are unused and keep track of them in StackByt...
void ChangeToRegister(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value...
Did not trigger a stack protector.
virtual int getFrameIndexReference(const MachineFunction &MF, int FI, unsigned &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
virtual bool hasReservedSpillSlot(const MachineFunction &MF, unsigned Reg, int &FrameIdx) const
Return true if target has reserved a spill slot in the stack frame of the given function for the spec...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID&#39;s allocated.
static void updateLiveness(MachineFunction &MF)
Helper function to update the liveness information for the callee-saved registers.
const MachineFunctionProperties & getProperties() const
Get the function properties.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
virtual void adjustForHiPEPrologue(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in the assembly prologue to ex...
static void insertCSRSaves(MachineBasicBlock &SaveBlock, ArrayRef< CalleeSavedInfo > CSI)
Insert restore code for the callee-saved registers used in the function.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const
canSimplifyCallFramePseudos - When possible, it&#39;s best to simplify the call frame pseudo ops before d...
int64_t getLocalFrameSize() const
Get the size of the local object blob.
unsigned Reg
bool test(unsigned Idx) const
Definition: BitVector.h:502
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:321
virtual MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
F(f)
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
MachineModuleInfo & getMMI() const
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:685
unsigned getCallFrameDestroyOpcode() const
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
virtual int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, unsigned &FrameReg, bool IgnoreSPUpdates) const
Same as getFrameIndexReference, except that the stack pointer (as opposed to the frame pointer) will ...
iterator end()
Get an iterator to the end of the SetVector.
Definition: SetVector.h:93
int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it...
unsigned getSpillSize(const TargetRegisterClass &RC) const
Return the size in bytes of the stack slot allocated to hold a spilled copy of a register from class ...
static DIExpression * prepend(const DIExpression *Expr, bool DerefBefore, int64_t Offset=0, bool DerefAfter=false, bool StackValue=false)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value...
iterator_range< succ_iterator > successors()
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
bool isVariableSizedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a variable sized object.
unsigned getSpillAlignment(const TargetRegisterClass &RC) const
Return the minimum required alignment in bytes for a spill slot for a register of this class...
virtual const SpillSlot * getCalleeSavedSpillSlots(unsigned &NumEntries) const
getCalleeSavedSpillSlots - This method returns a pointer to an array of pairs, that contains an entry...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
void clear()
clear - Removes all bits from the bitvector. Does not change capacity.
Definition: BitVector.h:367
std::pair< int, int64_t > getLocalFrameObjectMap(int i) const
Get the local offset mapping for a for an object.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
virtual bool usesPhysRegsForPEI() const
True if the target uses physical regs at Prolog/Epilog insertion time.
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:412
MachineBasicBlock * getRestorePoint() const
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
Definition: BitVector.h:332
virtual bool hasFP(const MachineFunction &MF) const =0
hasFP - Return true if the specified function should have a dedicated frame pointer register...
virtual bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
The address of this allocation is exposed and triggered protection.
virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const
Returns true if the target requires post PEI scavenging of registers for materializing frame index co...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
static cl::opt< unsigned > WarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned) -1), cl::desc("Warn for stack size bigger than the given" " number"))
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
Definition: BitVector.h:340
This file contains the simple types necessary to represent the attributes associated with functions a...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
MachineFunctionPass * createPrologEpilogInserterPass()
virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const
Return the skew that has to be applied to stack alignment under certain conditions (e...
void scavengeFrameVirtualRegs(MachineFunction &MF, RegScavenger &RS)
Replaces all frame index virtual registers with physical registers.
Diagnostic information for optimization analysis remarks.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:267
bool getUseLocalStackAllocationBlock() const
Get whether the local allocation blob should be allocated together or let PEI allocate the locals in ...
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:142
virtual bool targetHandlesStackFrameRounding() const
targetHandlesStackFrameRounding - Returns true if the target is responsible for rounding up the stack...
virtual bool needsFrameIndexResolution(const MachineFunction &MF) const
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:83
int getObjectIndexBegin() const
Return the minimum frame object index.
bool isObjectPreAllocated(int ObjectIdx) const
Return true if the object was pre-allocated into the local block.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
int getObjectIndexEnd() const
Return one past the maximum frame object index.
SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const
virtual void eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const =0
This method must be overriden to eliminate abstract frame indices from instructions which may use the...
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they&#39;re not in a MachineFuncti...
virtual const TargetInstrInfo * getInstrInfo() const
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
virtual void adjustForSegmentedStacks(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Adjust the prologue to have the function use segmented stacks.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
TargetInstrInfo - Interface to description of machine instruction set.
Array or nested array >= SSP-buffer-size.
virtual void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
unsigned getObjectAlignment(int ObjectIdx) const
Return the alignment of the specified stack object.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
This file declares the machine register scavenger class.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
unsigned const MachineRegisterInfo * MRI
void initializePEIPass(PassRegistry &)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1508
df_ext_iterator< T, SetTy > df_ext_end(const T &G, SetTy &S)
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void setStackSize(uint64_t Size)
Set the size of the stack.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
int getStackProtectorIndex() const
Return the index for the stack protector object.
unsigned getCallFrameSetupOpcode() const
These methods return the opcode of the frame setup/destroy instructions if they exist (-1 otherwise)...
Represent the analysis usage information of a pass.
df_ext_iterator< T, SetTy > df_ext_begin(const T &G, SetTy &S)
BitVector & reset()
Definition: BitVector.h:439
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
void setImm(int64_t immVal)
static bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx, bool StackGrowsDown, unsigned MaxAlign, BitVector &StackBytesFree)
Assign frame object to an unused portion of the stack in the fixed stack object range.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
static void assignCalleeSavedSpillSlots(MachineFunction &F, const BitVector &SavedRegs, unsigned &MinCSFrameIndex, unsigned &MaxCSFrameIndex)
INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false, false) INITIALIZE_PASS_END(PEI
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn&#39;t already there.
Definition: SmallSet.h:181
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:193
Used in the streaming interface as the general argument type.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
const MachineBasicBlock & front() const
typename vector_type::const_iterator const_iterator
Definition: SetVector.h:50
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
virtual bool hasReservedCallFrame(const MachineFunction &MF) const
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required, we reserve argument space for call sites in the function immediately on entry to the current function.
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:298
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call...
The optimization diagnostic interface.
bool shouldSplitStack() const
Should we be emitting segmented stack stuff for the function.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:213
virtual void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
bool isDebugValue() const
Definition: MachineInstr.h:997
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Prologue Epilogue Insertion &Frame Finalization
bool isFrameInstr(const MachineInstr &I) const
Returns true if the argument is a frame pseudo instruction.
Information about stack frame layout on the target.
int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:381
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling...
static void insertCSRRestores(MachineBasicBlock &RestoreBlock, std::vector< CalleeSavedInfo > &CSI)
Insert restore code for the callee-saved registers used in the function.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:286
int64_t getFrameSize(const MachineInstr &I) const
Returns size of the frame associated with the given frame instruction.
int64_t getImm() const
void setMetadata(const MDNode *MD)
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
unsigned getTransientStackAlignment() const
getTransientStackAlignment - This method returns the number of bytes to which the stack pointer must ...
virtual bool useFPForScavengingIndex(const MachineFunction &MF) const
Returns true if the target wants to use frame pointer based accesses to spill to the scavenger emerge...
virtual void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const
Store the specified register of the given register class to the specified stack frame index...
char & PrologEpilogCodeInserterID
PrologEpilogCodeInserter - This pass inserts prolog and epilog code, and eliminates abstract frame re...
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
bool none() const
none - Returns true if none of the bits are set.
Definition: BitVector.h:202
#define DEBUG_TYPE
virtual bool isFPCloseToIncomingSP() const
isFPCloseToIncomingSP - Return true if the frame pointer is close to the incoming stack pointer...
Representation of each machine instruction.
Definition: MachineInstr.h:64
virtual bool enableStackSlotScavenging(const MachineFunction &MF) const
Returns true if the stack slot holes in the fixed and callee-save stack area should be used when allo...
void setCalleeSavedInfo(const std::vector< CalleeSavedInfo > &CSI)
Used by prolog/epilog inserter to set the function&#39;s callee saved information.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
virtual bool requiresFrameIndexReplacementScavenging(const MachineFunction &MF) const
Returns true if the target requires using the RegScavenger directly for frame elimination despite usi...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
TargetOptions Options
Definition: TargetMachine.h:97
void push_back(MachineInstr *MI)
#define I(x, y, z)
Definition: MD5.cpp:58
void setMaxCallFrameSize(unsigned S)
unsigned getLocalFrameMaxAlign() const
Return the required alignment of the local object blob.
virtual const TargetFrameLowering * getFrameLowering() const
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:170
uint32_t Size
Definition: Profile.cpp:47
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const TargetRegisterClass * getMinimalPhysRegClass(unsigned Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
virtual void inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const
Replace a StackProbe stub (if any) with the actual probe code inline.
MachineBasicBlock * getSavePoint() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasProperty(Property P) const
bool needsStackRealignment(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
Array or nested array < SSP-buffer-size.
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
virtual void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &objectsToAllocate) const
Order the symbols in the local stack frame.
bool isMaxCallFrameSizeComputed() const
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
IRTranslator LLVM IR MI
void setIsDebug(bool Val=true)
#define LLVM_DEBUG(X)
Definition: Debug.h:123
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
int64_t getLocalFrameObjectCount() const
Return the number of objects allocated into the local object block.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs, SmallSet< int, 16 > &ProtectedObjs, MachineFrameInfo &MFI, bool StackGrowsDown, int64_t &Offset, unsigned &MaxAlign, unsigned Skew)
AssignProtectedObjSet - Helper function to assign large stack objects (i.e., those required to be clo...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
virtual void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned DestReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const
Load the specified register of the given register class from the specified stack frame index...
bool isReserved(unsigned PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
bool hasCalls() const
Return true if the current function has any function calls.
void resize(size_type N)
Definition: SmallVector.h:351