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