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