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