LLVM  10.0.0svn
PrologEpilogInserter.cpp
Go to the documentation of this file.
1 //===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass is responsible for finalizing the functions frame layout, saving
10 // callee saved registers, and for emitting prolog & epilog code for the
11 // function.
12 //
13 // This pass must be run after register allocation. After this pass is
14 // executed, it is illegal to construct MO_FrameIndex operands.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Statistic.h"
47 #include "llvm/IR/Attributes.h"
48 #include "llvm/IR/CallingConv.h"
50 #include "llvm/IR/DiagnosticInfo.h"
51 #include "llvm/IR/Function.h"
52 #include "llvm/IR/InlineAsm.h"
53 #include "llvm/IR/LLVMContext.h"
54 #include "llvm/MC/MCRegisterInfo.h"
55 #include "llvm/Pass.h"
56 #include "llvm/Support/CodeGen.h"
58 #include "llvm/Support/Debug.h"
64 #include <algorithm>
65 #include <cassert>
66 #include <cstdint>
67 #include <functional>
68 #include <limits>
69 #include <utility>
70 #include <vector>
71 
72 using namespace llvm;
73 
74 #define DEBUG_TYPE "prologepilog"
75 
77 
78 STATISTIC(NumLeafFuncWithSpills, "Number of leaf functions with CSRs");
79 STATISTIC(NumFuncSeen, "Number of functions seen in PEI");
80 
81 
82 namespace {
83 
84 class PEI : public MachineFunctionPass {
85 public:
86  static char ID;
87 
88  PEI() : MachineFunctionPass(ID) {
90  }
91 
92  void getAnalysisUsage(AnalysisUsage &AU) const override;
93 
94  /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
95  /// frame indexes with appropriate references.
96  bool runOnMachineFunction(MachineFunction &MF) override;
97 
98 private:
99  RegScavenger *RS;
100 
101  // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
102  // stack frame indexes.
103  unsigned MinCSFrameIndex = std::numeric_limits<unsigned>::max();
104  unsigned MaxCSFrameIndex = 0;
105 
106  // Save and Restore blocks of the current function. Typically there is a
107  // single save block, unless Windows EH funclets are involved.
108  MBBVector SaveBlocks;
109  MBBVector RestoreBlocks;
110 
111  // Flag to control whether to use the register scavenger to resolve
112  // frame index materialization registers. Set according to
113  // TRI->requiresFrameIndexScavenging() for the current function.
114  bool FrameIndexVirtualScavenging;
115 
116  // Flag to control whether the scavenger should be passed even though
117  // FrameIndexVirtualScavenging is used.
118  bool FrameIndexEliminationScavenging;
119 
120  // Emit remarks.
121  MachineOptimizationRemarkEmitter *ORE = nullptr;
122 
123  void calculateCallFrameInfo(MachineFunction &MF);
124  void calculateSaveRestoreBlocks(MachineFunction &MF);
125  void spillCalleeSavedRegs(MachineFunction &MF);
126 
127  void calculateFrameObjectOffsets(MachineFunction &MF);
128  void replaceFrameIndices(MachineFunction &MF);
129  void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
130  int &SPAdj);
131  void insertPrologEpilogCode(MachineFunction &MF);
132 };
133 
134 } // end anonymous namespace
135 
136 char PEI::ID = 0;
137 
139 
140 static cl::opt<unsigned>
141 WarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1),
142  cl::desc("Warn for stack size bigger than the given"
143  " number"));
144 
145 INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false,
146  false)
151  "Prologue/Epilogue Insertion & Frame Finalization", false,
152  false)
153 
155  return new PEI();
156 }
157 
158 STATISTIC(NumBytesStackSpace,
159  "Number of bytes used for stack in all functions");
160 
161 void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
162  AU.setPreservesCFG();
167 }
168 
169 /// StackObjSet - A set of stack object indexes
171 
172 using SavedDbgValuesMap =
174 
175 /// Stash DBG_VALUEs that describe parameters and which are placed at the start
176 /// of the block. Later on, after the prologue code has been emitted, the
177 /// stashed DBG_VALUEs will be reinserted at the start of the block.
179  SavedDbgValuesMap &EntryDbgValues) {
180  SmallVector<const MachineInstr *, 4> FrameIndexValues;
181 
182  for (auto &MI : MBB) {
183  if (!MI.isDebugInstr())
184  break;
185  if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter())
186  continue;
187  if (MI.getOperand(0).isFI()) {
188  // We can only emit valid locations for frame indices after the frame
189  // setup, so do not stash away them.
190  FrameIndexValues.push_back(&MI);
191  continue;
192  }
193  const DILocalVariable *Var = MI.getDebugVariable();
194  const DIExpression *Expr = MI.getDebugExpression();
195  auto Overlaps = [Var, Expr](const MachineInstr *DV) {
196  return Var == DV->getDebugVariable() &&
197  Expr->fragmentsOverlap(DV->getDebugExpression());
198  };
199  // See if the debug value overlaps with any preceding debug value that will
200  // not be stashed. If that is the case, then we can't stash this value, as
201  // we would then reorder the values at reinsertion.
202  if (llvm::none_of(FrameIndexValues, Overlaps))
203  EntryDbgValues[&MBB].push_back(&MI);
204  }
205 
206  // Remove stashed debug values from the block.
207  if (EntryDbgValues.count(&MBB))
208  for (auto *MI : EntryDbgValues[&MBB])
209  MI->removeFromParent();
210 }
211 
212 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
213 /// frame indexes with appropriate references.
214 bool PEI::runOnMachineFunction(MachineFunction &MF) {
215  NumFuncSeen++;
216  const Function &F = MF.getFunction();
219 
220  RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
221  FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
222  ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
223 
224  // Calculate the MaxCallFrameSize and AdjustsStack variables for the
225  // function's frame information. Also eliminates call frame pseudo
226  // instructions.
227  calculateCallFrameInfo(MF);
228 
229  // Determine placement of CSR spill/restore code and prolog/epilog code:
230  // place all spills in the entry block, all restores in return blocks.
231  calculateSaveRestoreBlocks(MF);
232 
233  // Stash away DBG_VALUEs that should not be moved by insertion of prolog code.
234  SavedDbgValuesMap EntryDbgValues;
235  for (MachineBasicBlock *SaveBlock : SaveBlocks)
236  stashEntryDbgValues(*SaveBlock, EntryDbgValues);
237 
238  // Handle CSR spilling and restoring, for targets that need it.
239  if (MF.getTarget().usesPhysRegsForPEI())
240  spillCalleeSavedRegs(MF);
241 
242  // Allow the target machine to make final modifications to the function
243  // before the frame layout is finalized.
245 
246  // Calculate actual frame offsets for all abstract stack objects...
247  calculateFrameObjectOffsets(MF);
248 
249  // Add prolog and epilog code to the function. This function is required
250  // to align the stack frame as necessary for any stack variables or
251  // called functions. Because of this, calculateCalleeSavedRegisters()
252  // must be called before this function in order to set the AdjustsStack
253  // and MaxCallFrameSize variables.
254  if (!F.hasFnAttribute(Attribute::Naked))
255  insertPrologEpilogCode(MF);
256 
257  // Reinsert stashed debug values at the start of the entry blocks.
258  for (auto &I : EntryDbgValues)
259  I.first->insert(I.first->begin(), I.second.begin(), I.second.end());
260 
261  // Replace all MO_FrameIndex operands with physical register references
262  // and actual offsets.
263  //
264  replaceFrameIndices(MF);
265 
266  // If register scavenging is needed, as we've enabled doing it as a
267  // post-pass, scavenge the virtual registers that frame index elimination
268  // inserted.
269  if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
270  scavengeFrameVirtualRegs(MF, *RS);
271 
272  // Warn on stack size when we exceeds the given limit.
273  MachineFrameInfo &MFI = MF.getFrameInfo();
274  uint64_t StackSize = MFI.getStackSize();
275  if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
276  DiagnosticInfoStackSize DiagStackSize(F, StackSize);
277  F.getContext().diagnose(DiagStackSize);
278  }
279  ORE->emit([&]() {
280  return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
281  MF.getFunction().getSubprogram(),
282  &MF.front())
283  << ore::NV("NumStackBytes", StackSize) << " stack bytes in function";
284  });
285 
286  delete RS;
287  SaveBlocks.clear();
288  RestoreBlocks.clear();
289  MFI.setSavePoint(nullptr);
290  MFI.setRestorePoint(nullptr);
291  return true;
292 }
293 
294 /// Calculate the MaxCallFrameSize and AdjustsStack
295 /// variables for the function's frame information and eliminate call frame
296 /// pseudo instructions.
297 void PEI::calculateCallFrameInfo(MachineFunction &MF) {
298  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
300  MachineFrameInfo &MFI = MF.getFrameInfo();
301 
302  unsigned MaxCallFrameSize = 0;
303  bool AdjustsStack = MFI.adjustsStack();
304 
305  // Get the function call frame set-up and tear-down instruction opcode
306  unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
307  unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
308 
309  // Early exit for targets which have no call frame setup/destroy pseudo
310  // instructions.
311  if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
312  return;
313 
314  std::vector<MachineBasicBlock::iterator> FrameSDOps;
315  for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
316  for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
317  if (TII.isFrameInstr(*I)) {
318  unsigned Size = TII.getFrameSize(*I);
319  if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
320  AdjustsStack = true;
321  FrameSDOps.push_back(I);
322  } else if (I->isInlineAsm()) {
323  // Some inline asm's need a stack frame, as indicated by operand 1.
324  unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
325  if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
326  AdjustsStack = true;
327  }
328 
330  (MFI.getMaxCallFrameSize() == MaxCallFrameSize &&
331  MFI.adjustsStack() == AdjustsStack));
332  MFI.setAdjustsStack(AdjustsStack);
333  MFI.setMaxCallFrameSize(MaxCallFrameSize);
334 
335  for (std::vector<MachineBasicBlock::iterator>::iterator
336  i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
338 
339  // If call frames are not being included as part of the stack frame, and
340  // the target doesn't indicate otherwise, remove the call frame pseudos
341  // here. The sub/add sp instruction pairs are still inserted, but we don't
342  // need to track the SP adjustment for frame index elimination.
343  if (TFI->canSimplifyCallFramePseudos(MF))
344  TFI->eliminateCallFramePseudoInstr(MF, *I->getParent(), I);
345  }
346 }
347 
348 /// Compute the sets of entry and return blocks for saving and restoring
349 /// callee-saved registers, and placing prolog and epilog code.
350 void PEI::calculateSaveRestoreBlocks(MachineFunction &MF) {
351  const MachineFrameInfo &MFI = MF.getFrameInfo();
352 
353  // Even when we do not change any CSR, we still want to insert the
354  // prologue and epilogue of the function.
355  // So set the save points for those.
356 
357  // Use the points found by shrink-wrapping, if any.
358  if (MFI.getSavePoint()) {
359  SaveBlocks.push_back(MFI.getSavePoint());
360  assert(MFI.getRestorePoint() && "Both restore and save must be set");
361  MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
362  // If RestoreBlock does not have any successor and is not a return block
363  // then the end point is unreachable and we do not need to insert any
364  // epilogue.
365  if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
366  RestoreBlocks.push_back(RestoreBlock);
367  return;
368  }
369 
370  // Save refs to entry and return blocks.
371  SaveBlocks.push_back(&MF.front());
372  for (MachineBasicBlock &MBB : MF) {
373  if (MBB.isEHFuncletEntry())
374  SaveBlocks.push_back(&MBB);
375  if (MBB.isReturnBlock())
376  RestoreBlocks.push_back(&MBB);
377  }
378 }
379 
381  const BitVector &SavedRegs,
382  unsigned &MinCSFrameIndex,
383  unsigned &MaxCSFrameIndex) {
384  if (SavedRegs.empty())
385  return;
386 
387  const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
388  const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
389 
390  std::vector<CalleeSavedInfo> CSI;
391  for (unsigned i = 0; CSRegs[i]; ++i) {
392  unsigned Reg = CSRegs[i];
393  if (SavedRegs.test(Reg))
394  CSI.push_back(CalleeSavedInfo(Reg));
395  }
396 
398  MachineFrameInfo &MFI = F.getFrameInfo();
399  if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
400  // If target doesn't implement this, use generic code.
401 
402  if (CSI.empty())
403  return; // Early exit if no callee saved registers are modified!
404 
405  unsigned NumFixedSpillSlots;
406  const TargetFrameLowering::SpillSlot *FixedSpillSlots =
407  TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
408 
409  // Now that we know which registers need to be saved and restored, allocate
410  // stack slots for them.
411  for (auto &CS : CSI) {
412  // If the target has spilled this register to another register, we don't
413  // need to allocate a stack slot.
414  if (CS.isSpilledToReg())
415  continue;
416 
417  unsigned Reg = CS.getReg();
418  const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
419 
420  int FrameIdx;
421  if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
422  CS.setFrameIdx(FrameIdx);
423  continue;
424  }
425 
426  // Check to see if this physreg must be spilled to a particular stack slot
427  // on this target.
428  const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
429  while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
430  FixedSlot->Reg != Reg)
431  ++FixedSlot;
432 
433  unsigned Size = RegInfo->getSpillSize(*RC);
434  if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
435  // Nope, just spill it anywhere convenient.
436  unsigned Align = RegInfo->getSpillAlignment(*RC);
437  unsigned StackAlign = TFI->getStackAlignment();
438 
439  // We may not be able to satisfy the desired alignment specification of
440  // the TargetRegisterClass if the stack alignment is smaller. Use the
441  // min.
442  Align = std::min(Align, StackAlign);
443  FrameIdx = MFI.CreateStackObject(Size, Align, true);
444  if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
445  if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
446  } else {
447  // Spill it to the stack where we must.
448  FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset);
449  }
450 
451  CS.setFrameIdx(FrameIdx);
452  }
453  }
454 
455  MFI.setCalleeSavedInfo(CSI);
456 }
457 
458 /// Helper function to update the liveness information for the callee-saved
459 /// registers.
460 static void updateLiveness(MachineFunction &MF) {
461  MachineFrameInfo &MFI = MF.getFrameInfo();
462  // Visited will contain all the basic blocks that are in the region
463  // where the callee saved registers are alive:
464  // - Anything that is not Save or Restore -> LiveThrough.
465  // - Save -> LiveIn.
466  // - Restore -> LiveOut.
467  // The live-out is not attached to the block, so no need to keep
468  // Restore in this set.
471  MachineBasicBlock *Entry = &MF.front();
472  MachineBasicBlock *Save = MFI.getSavePoint();
473 
474  if (!Save)
475  Save = Entry;
476 
477  if (Entry != Save) {
478  WorkList.push_back(Entry);
479  Visited.insert(Entry);
480  }
481  Visited.insert(Save);
482 
483  MachineBasicBlock *Restore = MFI.getRestorePoint();
484  if (Restore)
485  // By construction Restore cannot be visited, otherwise it
486  // means there exists a path to Restore that does not go
487  // through Save.
488  WorkList.push_back(Restore);
489 
490  while (!WorkList.empty()) {
491  const MachineBasicBlock *CurBB = WorkList.pop_back_val();
492  // By construction, the region that is after the save point is
493  // dominated by the Save and post-dominated by the Restore.
494  if (CurBB == Save && Save != Restore)
495  continue;
496  // Enqueue all the successors not already visited.
497  // Those are by construction either before Save or after Restore.
498  for (MachineBasicBlock *SuccBB : CurBB->successors())
499  if (Visited.insert(SuccBB).second)
500  WorkList.push_back(SuccBB);
501  }
502 
503  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
504 
506  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
507  for (MachineBasicBlock *MBB : Visited) {
508  MCPhysReg Reg = CSI[i].getReg();
509  // Add the callee-saved register as live-in.
510  // It's killed at the spill.
511  if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg))
512  MBB->addLiveIn(Reg);
513  }
514  // If callee-saved register is spilled to another register rather than
515  // spilling to stack, the destination register has to be marked as live for
516  // each MBB between the prologue and epilogue so that it is not clobbered
517  // before it is reloaded in the epilogue. The Visited set contains all
518  // blocks outside of the region delimited by prologue/epilogue.
519  if (CSI[i].isSpilledToReg()) {
520  for (MachineBasicBlock &MBB : MF) {
521  if (Visited.count(&MBB))
522  continue;
523  MCPhysReg DstReg = CSI[i].getDstReg();
524  if (!MBB.isLiveIn(DstReg))
525  MBB.addLiveIn(DstReg);
526  }
527  }
528  }
529 
530 }
531 
532 /// Insert restore code for the callee-saved registers used in the function.
533 static void insertCSRSaves(MachineBasicBlock &SaveBlock,
535  MachineFunction &MF = *SaveBlock.getParent();
536  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
539 
540  MachineBasicBlock::iterator I = SaveBlock.begin();
541  if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
542  for (const CalleeSavedInfo &CS : CSI) {
543  // Insert the spill to the stack frame.
544  unsigned Reg = CS.getReg();
545 
546  if (CS.isSpilledToReg()) {
547  BuildMI(SaveBlock, I, DebugLoc(),
548  TII.get(TargetOpcode::COPY), CS.getDstReg())
549  .addReg(Reg, getKillRegState(true));
550  } else {
551  const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
552  TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC,
553  TRI);
554  }
555  }
556  }
557 }
558 
559 /// Insert restore code for the callee-saved registers used in the function.
560 static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
561  std::vector<CalleeSavedInfo> &CSI) {
562  MachineFunction &MF = *RestoreBlock.getParent();
563  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
566 
567  // Restore all registers immediately before the return and any
568  // terminators that precede it.
570 
571  if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
572  for (const CalleeSavedInfo &CI : reverse(CSI)) {
573  unsigned Reg = CI.getReg();
574  if (CI.isSpilledToReg()) {
575  BuildMI(RestoreBlock, I, DebugLoc(), TII.get(TargetOpcode::COPY), Reg)
576  .addReg(CI.getDstReg(), getKillRegState(true));
577  } else {
578  const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
579  TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
580  assert(I != RestoreBlock.begin() &&
581  "loadRegFromStackSlot didn't insert any code!");
582  // Insert in reverse order. loadRegFromStackSlot can insert
583  // multiple instructions.
584  }
585  }
586  }
587 }
588 
589 void PEI::spillCalleeSavedRegs(MachineFunction &MF) {
590  // We can't list this requirement in getRequiredProperties because some
591  // targets (WebAssembly) use virtual registers past this point, and the pass
592  // pipeline is set up without giving the passes a chance to look at the
593  // TargetMachine.
594  // FIXME: Find a way to express this in getRequiredProperties.
597 
598  const Function &F = MF.getFunction();
600  MachineFrameInfo &MFI = MF.getFrameInfo();
601  MinCSFrameIndex = std::numeric_limits<unsigned>::max();
602  MaxCSFrameIndex = 0;
603 
604  // Determine which of the registers in the callee save list should be saved.
605  BitVector SavedRegs;
606  TFI->determineCalleeSaves(MF, SavedRegs, RS);
607 
608  // Assign stack slots for any callee-saved registers that must be spilled.
609  assignCalleeSavedSpillSlots(MF, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex);
610 
611  // Add the code to save and restore the callee saved registers.
612  if (!F.hasFnAttribute(Attribute::Naked)) {
613  MFI.setCalleeSavedInfoValid(true);
614 
615  std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
616  if (!CSI.empty()) {
617  if (!MFI.hasCalls())
618  NumLeafFuncWithSpills++;
619 
620  for (MachineBasicBlock *SaveBlock : SaveBlocks) {
621  insertCSRSaves(*SaveBlock, CSI);
622  // Update the live-in information of all the blocks up to the save
623  // point.
624  updateLiveness(MF);
625  }
626  for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
627  insertCSRRestores(*RestoreBlock, CSI);
628  }
629  }
630 }
631 
632 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
633 static inline void
635  bool StackGrowsDown, int64_t &Offset,
636  unsigned &MaxAlign, unsigned Skew) {
637  // If the stack grows down, add the object size to find the lowest address.
638  if (StackGrowsDown)
639  Offset += MFI.getObjectSize(FrameIdx);
640 
641  unsigned Align = MFI.getObjectAlignment(FrameIdx);
642 
643  // If the alignment of this object is greater than that of the stack, then
644  // increase the stack alignment to match.
645  MaxAlign = std::max(MaxAlign, Align);
646 
647  // Adjust to alignment boundary.
648  Offset = alignTo(Offset, Align, Skew);
649 
650  if (StackGrowsDown) {
651  LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
652  << "]\n");
653  MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
654  } else {
655  LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
656  << "]\n");
657  MFI.setObjectOffset(FrameIdx, Offset);
658  Offset += MFI.getObjectSize(FrameIdx);
659  }
660 }
661 
662 /// Compute which bytes of fixed and callee-save stack area are unused and keep
663 /// track of them in StackBytesFree.
664 static inline void
665 computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown,
666  unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex,
667  int64_t FixedCSEnd, BitVector &StackBytesFree) {
668  // Avoid undefined int64_t -> int conversion below in extreme case.
669  if (FixedCSEnd > std::numeric_limits<int>::max())
670  return;
671 
672  StackBytesFree.resize(FixedCSEnd, true);
673 
674  SmallVector<int, 16> AllocatedFrameSlots;
675  // Add fixed objects.
676  for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
677  // StackSlot scavenging is only implemented for the default stack.
678  if (MFI.getStackID(i) == TargetStackID::Default)
679  AllocatedFrameSlots.push_back(i);
680  // Add callee-save objects.
681  for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i)
682  if (MFI.getStackID(i) == TargetStackID::Default)
683  AllocatedFrameSlots.push_back(i);
684 
685  for (int i : AllocatedFrameSlots) {
686  // These are converted from int64_t, but they should always fit in int
687  // because of the FixedCSEnd check above.
688  int ObjOffset = MFI.getObjectOffset(i);
689  int ObjSize = MFI.getObjectSize(i);
690  int ObjStart, ObjEnd;
691  if (StackGrowsDown) {
692  // ObjOffset is negative when StackGrowsDown is true.
693  ObjStart = -ObjOffset - ObjSize;
694  ObjEnd = -ObjOffset;
695  } else {
696  ObjStart = ObjOffset;
697  ObjEnd = ObjOffset + ObjSize;
698  }
699  // Ignore fixed holes that are in the previous stack frame.
700  if (ObjEnd > 0)
701  StackBytesFree.reset(ObjStart, ObjEnd);
702  }
703 }
704 
705 /// Assign frame object to an unused portion of the stack in the fixed stack
706 /// object range. Return true if the allocation was successful.
707 static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
708  bool StackGrowsDown, unsigned MaxAlign,
709  BitVector &StackBytesFree) {
710  if (MFI.isVariableSizedObjectIndex(FrameIdx))
711  return false;
712 
713  if (StackBytesFree.none()) {
714  // clear it to speed up later scavengeStackSlot calls to
715  // StackBytesFree.none()
716  StackBytesFree.clear();
717  return false;
718  }
719 
720  unsigned ObjAlign = MFI.getObjectAlignment(FrameIdx);
721  if (ObjAlign > MaxAlign)
722  return false;
723 
724  int64_t ObjSize = MFI.getObjectSize(FrameIdx);
725  int FreeStart;
726  for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
727  FreeStart = StackBytesFree.find_next(FreeStart)) {
728 
729  // Check that free space has suitable alignment.
730  unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
731  if (alignTo(ObjStart, ObjAlign) != ObjStart)
732  continue;
733 
734  if (FreeStart + ObjSize > StackBytesFree.size())
735  return false;
736 
737  bool AllBytesFree = true;
738  for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
739  if (!StackBytesFree.test(FreeStart + Byte)) {
740  AllBytesFree = false;
741  break;
742  }
743  if (AllBytesFree)
744  break;
745  }
746 
747  if (FreeStart == -1)
748  return false;
749 
750  if (StackGrowsDown) {
751  int ObjStart = -(FreeStart + ObjSize);
752  LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
753  << ObjStart << "]\n");
754  MFI.setObjectOffset(FrameIdx, ObjStart);
755  } else {
756  LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
757  << FreeStart << "]\n");
758  MFI.setObjectOffset(FrameIdx, FreeStart);
759  }
760 
761  StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
762  return true;
763 }
764 
765 /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
766 /// those required to be close to the Stack Protector) to stack offsets.
767 static void
768 AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
769  SmallSet<int, 16> &ProtectedObjs,
770  MachineFrameInfo &MFI, bool StackGrowsDown,
771  int64_t &Offset, unsigned &MaxAlign, unsigned Skew) {
772 
773  for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
774  E = UnassignedObjs.end(); I != E; ++I) {
775  int i = *I;
776  AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew);
777  ProtectedObjs.insert(i);
778  }
779 }
780 
781 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
782 /// abstract stack objects.
783 void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
785 
786  bool StackGrowsDown =
788 
789  // Loop over all of the stack objects, assigning sequential addresses...
790  MachineFrameInfo &MFI = MF.getFrameInfo();
791 
792  // Start at the beginning of the local area.
793  // The Offset is the distance from the stack top in the direction
794  // of stack growth -- so it's always nonnegative.
795  int LocalAreaOffset = TFI.getOffsetOfLocalArea();
796  if (StackGrowsDown)
797  LocalAreaOffset = -LocalAreaOffset;
798  assert(LocalAreaOffset >= 0
799  && "Local area offset should be in direction of stack growth");
800  int64_t Offset = LocalAreaOffset;
801 
802  // Skew to be applied to alignment.
803  unsigned Skew = TFI.getStackAlignmentSkew(MF);
804 
805 #ifdef EXPENSIVE_CHECKS
806  for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i)
807  if (!MFI.isDeadObjectIndex(i) &&
809  assert(MFI.getObjectAlignment(i) <= MFI.getMaxAlignment() &&
810  "MaxAlignment is invalid");
811 #endif
812 
813  // If there are fixed sized objects that are preallocated in the local area,
814  // non-fixed objects can't be allocated right at the start of local area.
815  // Adjust 'Offset' to point to the end of last fixed sized preallocated
816  // object.
817  for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
818  if (MFI.getStackID(i) !=
819  TargetStackID::Default) // Only allocate objects on the default stack.
820  continue;
821 
822  int64_t FixedOff;
823  if (StackGrowsDown) {
824  // The maximum distance from the stack pointer is at lower address of
825  // the object -- which is given by offset. For down growing stack
826  // the offset is negative, so we negate the offset to get the distance.
827  FixedOff = -MFI.getObjectOffset(i);
828  } else {
829  // The maximum distance from the start pointer is at the upper
830  // address of the object.
831  FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
832  }
833  if (FixedOff > Offset) Offset = FixedOff;
834  }
835 
836  // First assign frame offsets to stack objects that are used to spill
837  // callee saved registers.
838  if (StackGrowsDown) {
839  for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
840  if (MFI.getStackID(i) !=
841  TargetStackID::Default) // Only allocate objects on the default stack.
842  continue;
843 
844  // If the stack grows down, we need to add the size to find the lowest
845  // address of the object.
846  Offset += MFI.getObjectSize(i);
847 
848  unsigned Align = MFI.getObjectAlignment(i);
849  // Adjust to alignment boundary
850  Offset = alignTo(Offset, Align, Skew);
851 
852  LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << -Offset << "]\n");
853  MFI.setObjectOffset(i, -Offset); // Set the computed offset
854  }
855  } else if (MaxCSFrameIndex >= MinCSFrameIndex) {
856  // Be careful about underflow in comparisons agains MinCSFrameIndex.
857  for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) {
858  if (MFI.getStackID(i) !=
859  TargetStackID::Default) // Only allocate objects on the default stack.
860  continue;
861 
862  if (MFI.isDeadObjectIndex(i))
863  continue;
864 
865  unsigned Align = MFI.getObjectAlignment(i);
866  // Adjust to alignment boundary
867  Offset = alignTo(Offset, Align, Skew);
868 
869  LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << Offset << "]\n");
870  MFI.setObjectOffset(i, Offset);
871  Offset += MFI.getObjectSize(i);
872  }
873  }
874 
875  // FixedCSEnd is the stack offset to the end of the fixed and callee-save
876  // stack area.
877  int64_t FixedCSEnd = Offset;
878  unsigned MaxAlign = MFI.getMaxAlignment();
879 
880  // Make sure the special register scavenging spill slot is closest to the
881  // incoming stack pointer if a frame pointer is required and is closer
882  // to the incoming rather than the final stack pointer.
883  const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
884  bool EarlyScavengingSlots = (TFI.hasFP(MF) &&
885  TFI.isFPCloseToIncomingSP() &&
886  RegInfo->useFPForScavengingIndex(MF) &&
887  !RegInfo->needsStackRealignment(MF));
888  if (RS && EarlyScavengingSlots) {
889  SmallVector<int, 2> SFIs;
890  RS->getScavengingFrameIndices(SFIs);
891  for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
892  IE = SFIs.end(); I != IE; ++I)
893  AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
894  }
895 
896  // FIXME: Once this is working, then enable flag will change to a target
897  // check for whether the frame is large enough to want to use virtual
898  // frame index registers. Functions which don't want/need this optimization
899  // will continue to use the existing code path.
901  unsigned Align = MFI.getLocalFrameMaxAlign().value();
902 
903  // Adjust to alignment boundary.
904  Offset = alignTo(Offset, Align, Skew);
905 
906  LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
907 
908  // Resolve offsets for objects in the local block.
909  for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
910  std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
911  int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
912  LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
913  << "]\n");
914  MFI.setObjectOffset(Entry.first, FIOffset);
915  }
916  // Allocate the local block
917  Offset += MFI.getLocalFrameSize();
918 
919  MaxAlign = std::max(Align, MaxAlign);
920  }
921 
922  // Retrieve the Exception Handler registration node.
923  int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
924  if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo())
925  EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
926 
927  // Make sure that the stack protector comes before the local variables on the
928  // stack.
929  SmallSet<int, 16> ProtectedObjs;
930  if (MFI.hasStackProtectorIndex()) {
931  int StackProtectorFI = MFI.getStackProtectorIndex();
932  StackObjSet LargeArrayObjs;
933  StackObjSet SmallArrayObjs;
934  StackObjSet AddrOfObjs;
935 
936  // If we need a stack protector, we need to make sure that
937  // LocalStackSlotPass didn't already allocate a slot for it.
938  // If we are told to use the LocalStackAllocationBlock, the stack protector
939  // is expected to be already pre-allocated.
941  AdjustStackOffset(MFI, StackProtectorFI, StackGrowsDown, Offset, MaxAlign,
942  Skew);
943  else if (!MFI.isObjectPreAllocated(MFI.getStackProtectorIndex()))
945  "Stack protector not pre-allocated by LocalStackSlotPass.");
946 
947  // Assign large stack objects first.
948  for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
950  continue;
951  if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
952  continue;
953  if (RS && RS->isScavengingFrameIndex((int)i))
954  continue;
955  if (MFI.isDeadObjectIndex(i))
956  continue;
957  if (StackProtectorFI == (int)i || EHRegNodeFrameIndex == (int)i)
958  continue;
959  if (MFI.getStackID(i) !=
960  TargetStackID::Default) // Only allocate objects on the default stack.
961  continue;
962 
963  switch (MFI.getObjectSSPLayout(i)) {
965  continue;
967  SmallArrayObjs.insert(i);
968  continue;
970  AddrOfObjs.insert(i);
971  continue;
973  LargeArrayObjs.insert(i);
974  continue;
975  }
976  llvm_unreachable("Unexpected SSPLayoutKind.");
977  }
978 
979  // We expect **all** the protected stack objects to be pre-allocated by
980  // LocalStackSlotPass. If it turns out that PEI still has to allocate some
981  // of them, we may end up messing up the expected order of the objects.
983  !(LargeArrayObjs.empty() && SmallArrayObjs.empty() &&
984  AddrOfObjs.empty()))
985  llvm_unreachable("Found protected stack objects not pre-allocated by "
986  "LocalStackSlotPass.");
987 
988  AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
989  Offset, MaxAlign, Skew);
990  AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
991  Offset, MaxAlign, Skew);
992  AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
993  Offset, MaxAlign, Skew);
994  }
995 
996  SmallVector<int, 8> ObjectsToAllocate;
997 
998  // Then prepare to assign frame offsets to stack objects that are not used to
999  // spill callee saved registers.
1000  for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
1002  continue;
1003  if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
1004  continue;
1005  if (RS && RS->isScavengingFrameIndex((int)i))
1006  continue;
1007  if (MFI.isDeadObjectIndex(i))
1008  continue;
1009  if (MFI.getStackProtectorIndex() == (int)i || EHRegNodeFrameIndex == (int)i)
1010  continue;
1011  if (ProtectedObjs.count(i))
1012  continue;
1013  if (MFI.getStackID(i) !=
1014  TargetStackID::Default) // Only allocate objects on the default stack.
1015  continue;
1016 
1017  // Add the objects that we need to allocate to our working set.
1018  ObjectsToAllocate.push_back(i);
1019  }
1020 
1021  // Allocate the EH registration node first if one is present.
1022  if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
1023  AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
1024  MaxAlign, Skew);
1025 
1026  // Give the targets a chance to order the objects the way they like it.
1027  if (MF.getTarget().getOptLevel() != CodeGenOpt::None &&
1029  TFI.orderFrameObjects(MF, ObjectsToAllocate);
1030 
1031  // Keep track of which bytes in the fixed and callee-save range are used so we
1032  // can use the holes when allocating later stack objects. Only do this if
1033  // stack protector isn't being used and the target requests it and we're
1034  // optimizing.
1035  BitVector StackBytesFree;
1036  if (!ObjectsToAllocate.empty() &&
1039  computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex,
1040  FixedCSEnd, StackBytesFree);
1041 
1042  // Now walk the objects and actually assign base offsets to them.
1043  for (auto &Object : ObjectsToAllocate)
1044  if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
1045  StackBytesFree))
1046  AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign, Skew);
1047 
1048  // Make sure the special register scavenging spill slot is closest to the
1049  // stack pointer.
1050  if (RS && !EarlyScavengingSlots) {
1051  SmallVector<int, 2> SFIs;
1052  RS->getScavengingFrameIndices(SFIs);
1053  for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
1054  IE = SFIs.end(); I != IE; ++I)
1055  AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
1056  }
1057 
1058  if (!TFI.targetHandlesStackFrameRounding()) {
1059  // If we have reserved argument space for call sites in the function
1060  // immediately on entry to the current function, count it as part of the
1061  // overall stack size.
1062  if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF))
1063  Offset += MFI.getMaxCallFrameSize();
1064 
1065  // Round up the size to a multiple of the alignment. If the function has
1066  // any calls or alloca's, align to the target's StackAlignment value to
1067  // ensure that the callee's frame or the alloca data is suitably aligned;
1068  // otherwise, for leaf functions, align to the TransientStackAlignment
1069  // value.
1070  unsigned StackAlign;
1071  if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
1072  (RegInfo->needsStackRealignment(MF) && MFI.getObjectIndexEnd() != 0))
1073  StackAlign = TFI.getStackAlignment();
1074  else
1075  StackAlign = TFI.getTransientStackAlignment();
1076 
1077  // If the frame pointer is eliminated, all frame offsets will be relative to
1078  // SP not FP. Align to MaxAlign so this works.
1079  StackAlign = std::max(StackAlign, MaxAlign);
1080  Offset = alignTo(Offset, StackAlign, Skew);
1081  }
1082 
1083  // Update frame info to pretend that this is part of the stack...
1084  int64_t StackSize = Offset - LocalAreaOffset;
1085  MFI.setStackSize(StackSize);
1086  NumBytesStackSpace += StackSize;
1087 }
1088 
1089 /// insertPrologEpilogCode - Scan the function for modified callee saved
1090 /// registers, insert spill code for these callee saved registers, then add
1091 /// prolog and epilog code to the function.
1092 void PEI::insertPrologEpilogCode(MachineFunction &MF) {
1093  const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1094 
1095  // Add prologue to the function...
1096  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1097  TFI.emitPrologue(MF, *SaveBlock);
1098 
1099  // Add epilogue to restore the callee-save registers in each exiting block.
1100  for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
1101  TFI.emitEpilogue(MF, *RestoreBlock);
1102 
1103  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1104  TFI.inlineStackProbe(MF, *SaveBlock);
1105 
1106  // Emit additional code that is required to support segmented stacks, if
1107  // we've been asked for it. This, when linked with a runtime with support
1108  // for segmented stacks (libgcc is one), will result in allocating stack
1109  // space in small chunks instead of one large contiguous block.
1110  if (MF.shouldSplitStack()) {
1111  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1112  TFI.adjustForSegmentedStacks(MF, *SaveBlock);
1113  // Record that there are split-stack functions, so we will emit a
1114  // special section to tell the linker.
1115  MF.getMMI().setHasSplitStack(true);
1116  } else
1117  MF.getMMI().setHasNosplitStack(true);
1118 
1119  // Emit additional code that is required to explicitly handle the stack in
1120  // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
1121  // approach is rather similar to that of Segmented Stacks, but it uses a
1122  // different conditional check and another BIF for allocating more stack
1123  // space.
1125  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1126  TFI.adjustForHiPEPrologue(MF, *SaveBlock);
1127 }
1128 
1129 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1130 /// register references and actual offsets.
1131 void PEI::replaceFrameIndices(MachineFunction &MF) {
1132  const auto &ST = MF.getSubtarget();
1133  const TargetFrameLowering &TFI = *ST.getFrameLowering();
1134  if (!TFI.needsFrameIndexResolution(MF))
1135  return;
1136 
1137  const TargetRegisterInfo *TRI = ST.getRegisterInfo();
1138 
1139  // Allow the target to determine this after knowing the frame size.
1140  FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
1142 
1143  // Store SPAdj at exit of a basic block.
1144  SmallVector<int, 8> SPState;
1145  SPState.resize(MF.getNumBlockIDs());
1147 
1148  // Iterate over the reachable blocks in DFS order.
1149  for (auto DFI = df_ext_begin(&MF, Reachable), DFE = df_ext_end(&MF, Reachable);
1150  DFI != DFE; ++DFI) {
1151  int SPAdj = 0;
1152  // Check the exit state of the DFS stack predecessor.
1153  if (DFI.getPathLength() >= 2) {
1154  MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1155  assert(Reachable.count(StackPred) &&
1156  "DFS stack predecessor is already visited.\n");
1157  SPAdj = SPState[StackPred->getNumber()];
1158  }
1159  MachineBasicBlock *BB = *DFI;
1160  replaceFrameIndices(BB, MF, SPAdj);
1161  SPState[BB->getNumber()] = SPAdj;
1162  }
1163 
1164  // Handle the unreachable blocks.
1165  for (auto &BB : MF) {
1166  if (Reachable.count(&BB))
1167  // Already handled in DFS traversal.
1168  continue;
1169  int SPAdj = 0;
1170  replaceFrameIndices(&BB, MF, SPAdj);
1171  }
1172 }
1173 
1174 void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1175  int &SPAdj) {
1177  "getRegisterInfo() must be implemented!");
1178  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1181 
1182  if (RS && FrameIndexEliminationScavenging)
1183  RS->enterBasicBlock(*BB);
1184 
1185  bool InsideCallSequence = false;
1186 
1187  for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
1188  if (TII.isFrameInstr(*I)) {
1189  InsideCallSequence = TII.isFrameSetup(*I);
1190  SPAdj += TII.getSPAdjust(*I);
1191  I = TFI->eliminateCallFramePseudoInstr(MF, *BB, I);
1192  continue;
1193  }
1194 
1195  MachineInstr &MI = *I;
1196  bool DoIncr = true;
1197  bool DidFinishLoop = true;
1198  for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1199  if (!MI.getOperand(i).isFI())
1200  continue;
1201 
1202  // Frame indices in debug values are encoded in a target independent
1203  // way with simply the frame index and offset rather than any
1204  // target-specific addressing mode.
1205  if (MI.isDebugValue()) {
1206  assert(i == 0 && "Frame indices can only appear as the first "
1207  "operand of a DBG_VALUE machine instruction");
1208  unsigned Reg;
1209  unsigned FrameIdx = MI.getOperand(0).getIndex();
1210  unsigned Size = MF.getFrameInfo().getObjectSize(FrameIdx);
1211 
1212  int64_t Offset =
1213  TFI->getFrameIndexReference(MF, FrameIdx, Reg);
1214  MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
1215  MI.getOperand(0).setIsDebug();
1216 
1217  const DIExpression *DIExpr = MI.getDebugExpression();
1218 
1219  // If we have a direct DBG_VALUE, and its location expression isn't
1220  // currently complex, then adding an offset will morph it into a
1221  // complex location that is interpreted as being a memory address.
1222  // This changes a pointer-valued variable to dereference that pointer,
1223  // which is incorrect. Fix by adding DW_OP_stack_value.
1224  unsigned PrependFlags = DIExpression::ApplyOffset;
1225  if (!MI.isIndirectDebugValue() && !DIExpr->isComplex())
1226  PrependFlags |= DIExpression::StackValue;
1227 
1228  // If we have DBG_VALUE that is indirect and has a Implicit location
1229  // expression need to insert a deref before prepending a Memory
1230  // location expression. Also after doing this we change the DBG_VALUE
1231  // to be direct.
1232  if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) {
1233  SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size, Size};
1234  bool WithStackValue = true;
1235  DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue);
1236  // Make the DBG_VALUE direct.
1237  MI.getOperand(1).ChangeToRegister(0, false);
1238  }
1239  DIExpr = DIExpression::prepend(DIExpr, PrependFlags, Offset);
1240  MI.getOperand(3).setMetadata(DIExpr);
1241  continue;
1242  }
1243 
1244  // TODO: This code should be commoned with the code for
1245  // PATCHPOINT. There's no good reason for the difference in
1246  // implementation other than historical accident. The only
1247  // remaining difference is the unconditional use of the stack
1248  // pointer as the base register.
1249  if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
1250  assert((!MI.isDebugValue() || i == 0) &&
1251  "Frame indicies can only appear as the first operand of a "
1252  "DBG_VALUE machine instruction");
1253  unsigned Reg;
1254  MachineOperand &Offset = MI.getOperand(i + 1);
1255  int refOffset = TFI->getFrameIndexReferencePreferSP(
1256  MF, MI.getOperand(i).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
1257  Offset.setImm(Offset.getImm() + refOffset + SPAdj);
1258  MI.getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
1259  continue;
1260  }
1261 
1262  // Some instructions (e.g. inline asm instructions) can have
1263  // multiple frame indices and/or cause eliminateFrameIndex
1264  // to insert more than one instruction. We need the register
1265  // scavenger to go through all of these instructions so that
1266  // it can update its register information. We keep the
1267  // iterator at the point before insertion so that we can
1268  // revisit them in full.
1269  bool AtBeginning = (I == BB->begin());
1270  if (!AtBeginning) --I;
1271 
1272  // If this instruction has a FrameIndex operand, we need to
1273  // use that target machine register info object to eliminate
1274  // it.
1275  TRI.eliminateFrameIndex(MI, SPAdj, i,
1276  FrameIndexEliminationScavenging ? RS : nullptr);
1277 
1278  // Reset the iterator if we were at the beginning of the BB.
1279  if (AtBeginning) {
1280  I = BB->begin();
1281  DoIncr = false;
1282  }
1283 
1284  DidFinishLoop = false;
1285  break;
1286  }
1287 
1288  // If we are looking at a call sequence, we need to keep track of
1289  // the SP adjustment made by each instruction in the sequence.
1290  // This includes both the frame setup/destroy pseudos (handled above),
1291  // as well as other instructions that have side effects w.r.t the SP.
1292  // Note that this must come after eliminateFrameIndex, because
1293  // if I itself referred to a frame index, we shouldn't count its own
1294  // adjustment.
1295  if (DidFinishLoop && InsideCallSequence)
1296  SPAdj += TII.getSPAdjust(MI);
1297 
1298  if (DoIncr && I != BB->end()) ++I;
1299 
1300  // Update register states.
1301  if (RS && FrameIndexEliminationScavenging && DidFinishLoop)
1302  RS->forward(MI);
1303  }
1304 }
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:371
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...
bool isReserved(Register PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
void setCalleeSavedInfoValid(bool v)
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool empty() const
empty - Tests whether there are no bits in this bitvector.
Definition: BitVector.h:166
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...
bool hasStackProtectorIndex() const
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:501
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:323
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
A debug info location.
Definition: DebugLoc.h:33
F(f)
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
MachineModuleInfo & getMMI() const
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:92
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.
static DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
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:50
void clear()
clear - Removes all bits from the bitvector. Does not change capacity.
Definition: BitVector.h:366
std::pair< int, int64_t > getLocalFrameObjectMap(int i) const
Get the local offset mapping for a for an object.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
virtual bool usesPhysRegsForPEI() const
True if the target uses physical regs at Prolog/Epilog insertion time.
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:414
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:331
virtual bool hasFP(const MachineFunction &MF) const =0
hasFP - Return true if the specified function should have a dedicated frame pointer register...
virtual bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
The address of this allocation is exposed and triggered protection.
virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const
Returns true if the target requires post PEI scavenging of registers for materializing frame index co...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
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:339
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1179
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:411
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:261
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:141
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:82
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:32
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
static void stashEntryDbgValues(MachineBasicBlock &MBB, SavedDbgValuesMap &EntryDbgValues)
Stash DBG_VALUEs that describe parameters and which are placed at the start of the block...
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:86
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
unsigned getKillRegState(bool B)
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...
Definition: MCRegister.h:19
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++ -*-—===//
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned getObjectAlignment(int ObjectIdx) const
Return the alignment of the specified stack object.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
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:1522
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.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
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:370
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)...
bool isImplicit() const
Return whether this is an implicit location description.
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:438
unsigned getMaxAlignment() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
constexpr double e
Definition: MathExtras.h:57
void setImm(int64_t immVal)
static DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
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:381
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:180
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:205
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:49
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.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
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...
bool isComplex() const
Return whether the location is computed on the expression stack, meaning it cannot be a simple regist...
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:297
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:417
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
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:212
virtual void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
bool isDebugValue() const
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:837
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:374
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:301
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)
DWARF expression.
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: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:201
#define DEBUG_TYPE
virtual bool isFPCloseToIncomingSP() const
isFPCloseToIncomingSP - Return true if the frame pointer is close to the incoming stack pointer...
Representation of each machine instruction.
Definition: MachineInstr.h:64
virtual bool enableStackSlotScavenging(const MachineFunction &MF) const
Returns true if the stack slot holes in the fixed and callee-save stack area should be used when allo...
void setCalleeSavedInfo(const std::vector< CalleeSavedInfo > &CSI)
Used by prolog/epilog inserter to set the function&#39;s callee saved information.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:163
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:55
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:44
TargetOptions Options
void push_back(MachineInstr *MI)
#define I(x, y, z)
Definition: MD5.cpp:58
void setMaxCallFrameSize(unsigned S)
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:72
void ChangeToRegister(Register 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 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:169
uint32_t Size
Definition: Profile.cpp:46
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
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.
uint8_t getStackID(int ObjectIdx) const
Align getLocalFrameMaxAlign() const
Return the required alignment of the local object blob.
MachineBasicBlock * getSavePoint() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasProperty(Property P) const
int CreateStackObject(uint64_t Size, Align 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...
bool needsStackRealignment(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
Array or nested array < SSP-buffer-size.
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
virtual void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &objectsToAllocate) const
Order the symbols in the local stack frame.
bool isMaxCallFrameSizeComputed() const
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
IRTranslator LLVM IR MI
void setIsDebug(bool Val=true)
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
int64_t getLocalFrameObjectCount() const
Return the number of objects allocated into the local object block.
bool isIndirectDebugValue() const
A DBG_VALUE is indirect iff the first operand is a register and the second operand is an immediate...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs, SmallSet< int, 16 > &ProtectedObjs, MachineFrameInfo &MFI, bool StackGrowsDown, int64_t &Offset, unsigned &MaxAlign, unsigned Skew)
AssignProtectedObjSet - Helper function to assign large stack objects (i.e., those required to be clo...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
virtual void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned DestReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const
Load the specified register of the given register class from the specified stack frame index...
bool hasCalls() const
Return true if the current function has any function calls.
void resize(size_type N)
Definition: SmallVector.h:344