PrologEpilogInserter.cpp revision 360784
198937Sdes//===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===//
298937Sdes//
398937Sdes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
499060Sdes// See https://llvm.org/LICENSE.txt for license information.
598937Sdes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
698937Sdes//
798937Sdes//===----------------------------------------------------------------------===//
898937Sdes//
998937Sdes// This pass is responsible for finalizing the functions frame layout, saving
1098937Sdes// callee saved registers, and for emitting prolog & epilog code for the
1198937Sdes// function.
1298937Sdes//
1398937Sdes// This pass must be run after register allocation.  After this pass is
1498937Sdes// executed, it is illegal to construct MO_FrameIndex operands.
1598937Sdes//
1698937Sdes//===----------------------------------------------------------------------===//
1798937Sdes
1898937Sdes#include "llvm/ADT/ArrayRef.h"
1998937Sdes#include "llvm/ADT/BitVector.h"
2098937Sdes#include "llvm/ADT/DepthFirstIterator.h"
2198937Sdes#include "llvm/ADT/STLExtras.h"
2298937Sdes#include "llvm/ADT/SetVector.h"
2398937Sdes#include "llvm/ADT/SmallPtrSet.h"
2498937Sdes#include "llvm/ADT/SmallSet.h"
2598937Sdes#include "llvm/ADT/SmallVector.h"
2698937Sdes#include "llvm/ADT/Statistic.h"
2798937Sdes#include "llvm/Analysis/OptimizationRemarkEmitter.h"
2898937Sdes#include "llvm/CodeGen/MachineBasicBlock.h"
2998937Sdes#include "llvm/CodeGen/MachineDominators.h"
3098937Sdes#include "llvm/CodeGen/MachineFrameInfo.h"
3198937Sdes#include "llvm/CodeGen/MachineFunction.h"
3298937Sdes#include "llvm/CodeGen/MachineFunctionPass.h"
3398937Sdes#include "llvm/CodeGen/MachineInstr.h"
3498937Sdes#include "llvm/CodeGen/MachineInstrBuilder.h"
3598937Sdes#include "llvm/CodeGen/MachineLoopInfo.h"
3698937Sdes#include "llvm/CodeGen/MachineModuleInfo.h"
3798937Sdes#include "llvm/CodeGen/MachineOperand.h"
3898937Sdes#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
3998937Sdes#include "llvm/CodeGen/MachineRegisterInfo.h"
4098937Sdes#include "llvm/CodeGen/RegisterScavenging.h"
4198937Sdes#include "llvm/CodeGen/TargetFrameLowering.h"
4298937Sdes#include "llvm/CodeGen/TargetInstrInfo.h"
4398937Sdes#include "llvm/CodeGen/TargetOpcodes.h"
4498937Sdes#include "llvm/CodeGen/TargetRegisterInfo.h"
4598937Sdes#include "llvm/CodeGen/TargetSubtargetInfo.h"
4698937Sdes#include "llvm/CodeGen/WinEHFuncInfo.h"
4798937Sdes#include "llvm/IR/Attributes.h"
4898937Sdes#include "llvm/IR/CallingConv.h"
4998937Sdes#include "llvm/IR/DebugInfoMetadata.h"
5098937Sdes#include "llvm/IR/DiagnosticInfo.h"
5198937Sdes#include "llvm/IR/Function.h"
5298937Sdes#include "llvm/IR/InlineAsm.h"
5398937Sdes#include "llvm/IR/LLVMContext.h"
5498937Sdes#include "llvm/InitializePasses.h"
5598937Sdes#include "llvm/MC/MCRegisterInfo.h"
5698937Sdes#include "llvm/Pass.h"
5798937Sdes#include "llvm/Support/CodeGen.h"
5898937Sdes#include "llvm/Support/CommandLine.h"
5998937Sdes#include "llvm/Support/Debug.h"
6098937Sdes#include "llvm/Support/ErrorHandling.h"
6198937Sdes#include "llvm/Support/MathExtras.h"
6298937Sdes#include "llvm/Support/raw_ostream.h"
6398937Sdes#include "llvm/Target/TargetMachine.h"
6498937Sdes#include "llvm/Target/TargetOptions.h"
6598937Sdes#include <algorithm>
6698937Sdes#include <cassert>
6798937Sdes#include <cstdint>
6898937Sdes#include <functional>
6998937Sdes#include <limits>
7098937Sdes#include <utility>
7198937Sdes#include <vector>
7298937Sdes
7398937Sdesusing namespace llvm;
7498937Sdes
7598937Sdes#define DEBUG_TYPE "prologepilog"
7698937Sdes
7798937Sdesusing MBBVector = SmallVector<MachineBasicBlock *, 4>;
7898937Sdes
7998937SdesSTATISTIC(NumLeafFuncWithSpills, "Number of leaf functions with CSRs");
8098937SdesSTATISTIC(NumFuncSeen, "Number of functions seen in PEI");
8198937Sdes
8298937Sdes
8398937Sdesnamespace {
8498937Sdes
8598937Sdesclass PEI : public MachineFunctionPass {
8698937Sdespublic:
8798937Sdes  static char ID;
8898937Sdes
8998937Sdes  PEI() : MachineFunctionPass(ID) {
9098937Sdes    initializePEIPass(*PassRegistry::getPassRegistry());
9198937Sdes  }
9298937Sdes
9398937Sdes  void getAnalysisUsage(AnalysisUsage &AU) const override;
9498937Sdes
9598937Sdes  /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
9698937Sdes  /// frame indexes with appropriate references.
9798937Sdes  bool runOnMachineFunction(MachineFunction &MF) override;
9898937Sdes
9998937Sdesprivate:
10098937Sdes  RegScavenger *RS;
10198937Sdes
10298937Sdes  // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
10398937Sdes  // stack frame indexes.
10498937Sdes  unsigned MinCSFrameIndex = std::numeric_limits<unsigned>::max();
10598937Sdes  unsigned MaxCSFrameIndex = 0;
10698937Sdes
10798937Sdes  // Save and Restore blocks of the current function. Typically there is a
10898937Sdes  // single save block, unless Windows EH funclets are involved.
10998937Sdes  MBBVector SaveBlocks;
11098937Sdes  MBBVector RestoreBlocks;
11198937Sdes
11298937Sdes  // Flag to control whether to use the register scavenger to resolve
11398937Sdes  // frame index materialization registers. Set according to
11498937Sdes  // TRI->requiresFrameIndexScavenging() for the current function.
11598937Sdes  bool FrameIndexVirtualScavenging;
11698937Sdes
11798937Sdes  // Flag to control whether the scavenger should be passed even though
11898937Sdes  // FrameIndexVirtualScavenging is used.
11998937Sdes  bool FrameIndexEliminationScavenging;
12098937Sdes
12198937Sdes  // Emit remarks.
12298937Sdes  MachineOptimizationRemarkEmitter *ORE = nullptr;
12398937Sdes
12498937Sdes  void calculateCallFrameInfo(MachineFunction &MF);
12598937Sdes  void calculateSaveRestoreBlocks(MachineFunction &MF);
12698937Sdes  void spillCalleeSavedRegs(MachineFunction &MF);
12798937Sdes
12898937Sdes  void calculateFrameObjectOffsets(MachineFunction &MF);
12998937Sdes  void replaceFrameIndices(MachineFunction &MF);
13098937Sdes  void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
13198937Sdes                           int &SPAdj);
13298937Sdes  void insertPrologEpilogCode(MachineFunction &MF);
13398937Sdes};
13498937Sdes
13598937Sdes} // end anonymous namespace
13698937Sdes
13798937Sdeschar PEI::ID = 0;
13898937Sdes
13998937Sdeschar &llvm::PrologEpilogCodeInserterID = PEI::ID;
14098937Sdes
14198937Sdesstatic cl::opt<unsigned>
14298937SdesWarnStackSize("warn-stack-size", cl::Hidden, cl::init((unsigned)-1),
14398937Sdes              cl::desc("Warn for stack size bigger than the given"
14498937Sdes                       " number"));
14598937Sdes
14698937SdesINITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false,
14798937Sdes                      false)
14898937SdesINITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
14998937SdesINITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
15098937SdesINITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
15198937SdesINITIALIZE_PASS_END(PEI, DEBUG_TYPE,
15298937Sdes                    "Prologue/Epilogue Insertion & Frame Finalization", false,
15398937Sdes                    false)
15498937Sdes
15598937SdesMachineFunctionPass *llvm::createPrologEpilogInserterPass() {
15698937Sdes  return new PEI();
15798937Sdes}
15898937Sdes
15998937SdesSTATISTIC(NumBytesStackSpace,
16098937Sdes          "Number of bytes used for stack in all functions");
16198937Sdes
16298937Sdesvoid PEI::getAnalysisUsage(AnalysisUsage &AU) const {
16398937Sdes  AU.setPreservesCFG();
16498937Sdes  AU.addPreserved<MachineLoopInfo>();
16598937Sdes  AU.addPreserved<MachineDominatorTree>();
16698937Sdes  AU.addRequired<MachineOptimizationRemarkEmitterPass>();
16798937Sdes  MachineFunctionPass::getAnalysisUsage(AU);
16898937Sdes}
16998937Sdes
17098937Sdes/// StackObjSet - A set of stack object indexes
17198937Sdesusing StackObjSet = SmallSetVector<int, 8>;
17298937Sdes
17398937Sdesusing SavedDbgValuesMap =
17498937Sdes    SmallDenseMap<MachineBasicBlock *, SmallVector<MachineInstr *, 4>, 4>;
17598937Sdes
17698937Sdes/// Stash DBG_VALUEs that describe parameters and which are placed at the start
17798937Sdes/// of the block. Later on, after the prologue code has been emitted, the
17898937Sdes/// stashed DBG_VALUEs will be reinserted at the start of the block.
17998937Sdesstatic void stashEntryDbgValues(MachineBasicBlock &MBB,
18098937Sdes                                SavedDbgValuesMap &EntryDbgValues) {
18198937Sdes  SmallVector<const MachineInstr *, 4> FrameIndexValues;
18298937Sdes
18398937Sdes  for (auto &MI : MBB) {
18498937Sdes    if (!MI.isDebugInstr())
18598937Sdes      break;
18698937Sdes    if (!MI.isDebugValue() || !MI.getDebugVariable()->isParameter())
18798937Sdes      continue;
18898937Sdes    if (MI.getOperand(0).isFI()) {
18998937Sdes      // We can only emit valid locations for frame indices after the frame
19098937Sdes      // setup, so do not stash away them.
19198937Sdes      FrameIndexValues.push_back(&MI);
19298937Sdes      continue;
19398937Sdes    }
19498937Sdes    const DILocalVariable *Var = MI.getDebugVariable();
19598937Sdes    const DIExpression *Expr = MI.getDebugExpression();
19698937Sdes    auto Overlaps = [Var, Expr](const MachineInstr *DV) {
19798937Sdes      return Var == DV->getDebugVariable() &&
19898937Sdes             Expr->fragmentsOverlap(DV->getDebugExpression());
19998937Sdes    };
20098937Sdes    // See if the debug value overlaps with any preceding debug value that will
20198937Sdes    // not be stashed. If that is the case, then we can't stash this value, as
20298937Sdes    // we would then reorder the values at reinsertion.
20398937Sdes    if (llvm::none_of(FrameIndexValues, Overlaps))
20498937Sdes      EntryDbgValues[&MBB].push_back(&MI);
20598937Sdes  }
20698937Sdes
20798937Sdes  // Remove stashed debug values from the block.
20898937Sdes  if (EntryDbgValues.count(&MBB))
20998937Sdes    for (auto *MI : EntryDbgValues[&MBB])
21098937Sdes      MI->removeFromParent();
21198937Sdes}
21298937Sdes
21398937Sdes/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
21498937Sdes/// frame indexes with appropriate references.
21598937Sdesbool PEI::runOnMachineFunction(MachineFunction &MF) {
21698937Sdes  NumFuncSeen++;
21798937Sdes  const Function &F = MF.getFunction();
21898937Sdes  const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
21998937Sdes  const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
22098937Sdes
22198937Sdes  RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
22298937Sdes  FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
22398937Sdes  ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
22498937Sdes
22598937Sdes  // Calculate the MaxCallFrameSize and AdjustsStack variables for the
22698937Sdes  // function's frame information. Also eliminates call frame pseudo
22798937Sdes  // instructions.
22898937Sdes  calculateCallFrameInfo(MF);
22998937Sdes
23098937Sdes  // Determine placement of CSR spill/restore code and prolog/epilog code:
23198937Sdes  // place all spills in the entry block, all restores in return blocks.
23298937Sdes  calculateSaveRestoreBlocks(MF);
23398937Sdes
23498937Sdes  // Stash away DBG_VALUEs that should not be moved by insertion of prolog code.
23598937Sdes  SavedDbgValuesMap EntryDbgValues;
23698937Sdes  for (MachineBasicBlock *SaveBlock : SaveBlocks)
23798937Sdes    stashEntryDbgValues(*SaveBlock, EntryDbgValues);
23898937Sdes
23998937Sdes  // Handle CSR spilling and restoring, for targets that need it.
24098937Sdes  if (MF.getTarget().usesPhysRegsForPEI())
24198937Sdes    spillCalleeSavedRegs(MF);
24298937Sdes
24398937Sdes  // Allow the target machine to make final modifications to the function
24498937Sdes  // before the frame layout is finalized.
24598937Sdes  TFI->processFunctionBeforeFrameFinalized(MF, RS);
24698937Sdes
24798937Sdes  // Calculate actual frame offsets for all abstract stack objects...
24898937Sdes  calculateFrameObjectOffsets(MF);
24998937Sdes
25098937Sdes  // Add prolog and epilog code to the function.  This function is required
25198937Sdes  // to align the stack frame as necessary for any stack variables or
25298937Sdes  // called functions.  Because of this, calculateCalleeSavedRegisters()
25398937Sdes  // must be called before this function in order to set the AdjustsStack
25498937Sdes  // and MaxCallFrameSize variables.
25598937Sdes  if (!F.hasFnAttribute(Attribute::Naked))
25698937Sdes    insertPrologEpilogCode(MF);
25798937Sdes
25898937Sdes  // Reinsert stashed debug values at the start of the entry blocks.
25998937Sdes  for (auto &I : EntryDbgValues)
26098937Sdes    I.first->insert(I.first->begin(), I.second.begin(), I.second.end());
26198937Sdes
26298937Sdes  // Replace all MO_FrameIndex operands with physical register references
26398937Sdes  // and actual offsets.
26498937Sdes  //
26598937Sdes  replaceFrameIndices(MF);
26698937Sdes
26798937Sdes  // If register scavenging is needed, as we've enabled doing it as a
26898937Sdes  // post-pass, scavenge the virtual registers that frame index elimination
26998937Sdes  // inserted.
27098937Sdes  if (TRI->requiresRegisterScavenging(MF) && FrameIndexVirtualScavenging)
27198937Sdes    scavengeFrameVirtualRegs(MF, *RS);
27298937Sdes
27398937Sdes  // Warn on stack size when we exceeds the given limit.
27498937Sdes  MachineFrameInfo &MFI = MF.getFrameInfo();
27598937Sdes  uint64_t StackSize = MFI.getStackSize();
27698937Sdes  if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
27798937Sdes    DiagnosticInfoStackSize DiagStackSize(F, StackSize);
27898937Sdes    F.getContext().diagnose(DiagStackSize);
27998937Sdes  }
28098937Sdes  ORE->emit([&]() {
28198937Sdes    return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
28298937Sdes                                             MF.getFunction().getSubprogram(),
28398937Sdes                                             &MF.front())
28498937Sdes           << ore::NV("NumStackBytes", StackSize) << " stack bytes in function";
28598937Sdes  });
28698937Sdes
28798937Sdes  delete RS;
28898937Sdes  SaveBlocks.clear();
28998937Sdes  RestoreBlocks.clear();
29098937Sdes  MFI.setSavePoint(nullptr);
29198937Sdes  MFI.setRestorePoint(nullptr);
29298937Sdes  return true;
29398937Sdes}
29498937Sdes
29598937Sdes/// Calculate the MaxCallFrameSize and AdjustsStack
29698937Sdes/// variables for the function's frame information and eliminate call frame
29798937Sdes/// pseudo instructions.
29898937Sdesvoid PEI::calculateCallFrameInfo(MachineFunction &MF) {
29998937Sdes  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
30098937Sdes  const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
30198937Sdes  MachineFrameInfo &MFI = MF.getFrameInfo();
30298937Sdes
30398937Sdes  unsigned MaxCallFrameSize = 0;
30498937Sdes  bool AdjustsStack = MFI.adjustsStack();
30598937Sdes
30698937Sdes  // Get the function call frame set-up and tear-down instruction opcode
30798937Sdes  unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
30898937Sdes  unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
30998937Sdes
31098937Sdes  // Early exit for targets which have no call frame setup/destroy pseudo
31198937Sdes  // instructions.
31298937Sdes  if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
31398937Sdes    return;
31498937Sdes
31598937Sdes  std::vector<MachineBasicBlock::iterator> FrameSDOps;
31698937Sdes  for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
31798937Sdes    for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
31898937Sdes      if (TII.isFrameInstr(*I)) {
31998937Sdes        unsigned Size = TII.getFrameSize(*I);
32098937Sdes        if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
32198937Sdes        AdjustsStack = true;
32298937Sdes        FrameSDOps.push_back(I);
32398937Sdes      } else if (I->isInlineAsm()) {
32498937Sdes        // Some inline asm's need a stack frame, as indicated by operand 1.
32598937Sdes        unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
32698937Sdes        if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
32798937Sdes          AdjustsStack = true;
32898937Sdes      }
32998937Sdes
33098937Sdes  assert(!MFI.isMaxCallFrameSizeComputed() ||
33198937Sdes         (MFI.getMaxCallFrameSize() == MaxCallFrameSize &&
33298937Sdes          MFI.adjustsStack() == AdjustsStack));
33398937Sdes  MFI.setAdjustsStack(AdjustsStack);
33498937Sdes  MFI.setMaxCallFrameSize(MaxCallFrameSize);
33598937Sdes
33698937Sdes  for (std::vector<MachineBasicBlock::iterator>::iterator
33798937Sdes         i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
33898937Sdes    MachineBasicBlock::iterator I = *i;
33998937Sdes
34098937Sdes    // If call frames are not being included as part of the stack frame, and
34198937Sdes    // the target doesn't indicate otherwise, remove the call frame pseudos
34298937Sdes    // here. The sub/add sp instruction pairs are still inserted, but we don't
34398937Sdes    // need to track the SP adjustment for frame index elimination.
34498937Sdes    if (TFI->canSimplifyCallFramePseudos(MF))
34598937Sdes      TFI->eliminateCallFramePseudoInstr(MF, *I->getParent(), I);
34698937Sdes  }
34798937Sdes}
34898937Sdes
34998937Sdes/// Compute the sets of entry and return blocks for saving and restoring
35098937Sdes/// callee-saved registers, and placing prolog and epilog code.
35198937Sdesvoid PEI::calculateSaveRestoreBlocks(MachineFunction &MF) {
35298937Sdes  const MachineFrameInfo &MFI = MF.getFrameInfo();
35398937Sdes
35498937Sdes  // Even when we do not change any CSR, we still want to insert the
35598937Sdes  // prologue and epilogue of the function.
35698937Sdes  // So set the save points for those.
35798937Sdes
35898937Sdes  // Use the points found by shrink-wrapping, if any.
35998937Sdes  if (MFI.getSavePoint()) {
36098937Sdes    SaveBlocks.push_back(MFI.getSavePoint());
36198937Sdes    assert(MFI.getRestorePoint() && "Both restore and save must be set");
36298937Sdes    MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
36398937Sdes    // If RestoreBlock does not have any successor and is not a return block
36498937Sdes    // then the end point is unreachable and we do not need to insert any
36598937Sdes    // epilogue.
36698937Sdes    if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
36798937Sdes      RestoreBlocks.push_back(RestoreBlock);
36898937Sdes    return;
36998937Sdes  }
37098937Sdes
37198937Sdes  // Save refs to entry and return blocks.
37298937Sdes  SaveBlocks.push_back(&MF.front());
37398937Sdes  for (MachineBasicBlock &MBB : MF) {
37498937Sdes    if (MBB.isEHFuncletEntry())
37598937Sdes      SaveBlocks.push_back(&MBB);
37698937Sdes    if (MBB.isReturnBlock())
37798937Sdes      RestoreBlocks.push_back(&MBB);
37898937Sdes  }
37998937Sdes}
38098937Sdes
38198937Sdesstatic void assignCalleeSavedSpillSlots(MachineFunction &F,
38298937Sdes                                        const BitVector &SavedRegs,
38398937Sdes                                        unsigned &MinCSFrameIndex,
38498937Sdes                                        unsigned &MaxCSFrameIndex) {
38598937Sdes  if (SavedRegs.empty())
38698937Sdes    return;
38798937Sdes
38898937Sdes  const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
38998937Sdes  const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
39098937Sdes
39198937Sdes  std::vector<CalleeSavedInfo> CSI;
39298937Sdes  for (unsigned i = 0; CSRegs[i]; ++i) {
39398937Sdes    unsigned Reg = CSRegs[i];
39498937Sdes    if (SavedRegs.test(Reg))
39598937Sdes      CSI.push_back(CalleeSavedInfo(Reg));
39698937Sdes  }
39798937Sdes
39898937Sdes  const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
39998937Sdes  MachineFrameInfo &MFI = F.getFrameInfo();
40098937Sdes  if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
40198937Sdes    // If target doesn't implement this, use generic code.
40298937Sdes
40398937Sdes    if (CSI.empty())
40498937Sdes      return; // Early exit if no callee saved registers are modified!
40598937Sdes
40698937Sdes    unsigned NumFixedSpillSlots;
40798937Sdes    const TargetFrameLowering::SpillSlot *FixedSpillSlots =
40898937Sdes        TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
40998937Sdes
41098937Sdes    // Now that we know which registers need to be saved and restored, allocate
41198937Sdes    // stack slots for them.
41299060Sdes    for (auto &CS : CSI) {
41399060Sdes      // If the target has spilled this register to another register, we don't
41499060Sdes      // need to allocate a stack slot.
41599060Sdes      if (CS.isSpilledToReg())
41699060Sdes        continue;
41799060Sdes
41899060Sdes      unsigned Reg = CS.getReg();
41999060Sdes      const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
42099060Sdes
42199060Sdes      int FrameIdx;
42299060Sdes      if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
42399060Sdes        CS.setFrameIdx(FrameIdx);
42498937Sdes        continue;
42598937Sdes      }
42698937Sdes
42798937Sdes      // Check to see if this physreg must be spilled to a particular stack slot
42898937Sdes      // on this target.
42998937Sdes      const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
43098937Sdes      while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
43198937Sdes             FixedSlot->Reg != Reg)
43298937Sdes        ++FixedSlot;
43398937Sdes
43498937Sdes      unsigned Size = RegInfo->getSpillSize(*RC);
43598937Sdes      if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
43698937Sdes        // Nope, just spill it anywhere convenient.
43798937Sdes        unsigned Align = RegInfo->getSpillAlignment(*RC);
43898937Sdes        unsigned StackAlign = TFI->getStackAlignment();
43998937Sdes
44098937Sdes        // We may not be able to satisfy the desired alignment specification of
44198937Sdes        // the TargetRegisterClass if the stack alignment is smaller. Use the
44298937Sdes        // min.
44398937Sdes        Align = std::min(Align, StackAlign);
44498937Sdes        FrameIdx = MFI.CreateStackObject(Size, Align, true);
44598937Sdes        if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
44698937Sdes        if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
44798937Sdes      } else {
44898937Sdes        // Spill it to the stack where we must.
44998937Sdes        FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset);
45098937Sdes      }
45198937Sdes
45298937Sdes      CS.setFrameIdx(FrameIdx);
45398937Sdes    }
45498937Sdes  }
45598937Sdes
45698937Sdes  MFI.setCalleeSavedInfo(CSI);
45798937Sdes}
45898937Sdes
45998937Sdes/// Helper function to update the liveness information for the callee-saved
46098937Sdes/// registers.
46198937Sdesstatic void updateLiveness(MachineFunction &MF) {
46298937Sdes  MachineFrameInfo &MFI = MF.getFrameInfo();
46398937Sdes  // Visited will contain all the basic blocks that are in the region
46498937Sdes  // where the callee saved registers are alive:
46598937Sdes  // - Anything that is not Save or Restore -> LiveThrough.
46698937Sdes  // - Save -> LiveIn.
46798937Sdes  // - Restore -> LiveOut.
46898937Sdes  // The live-out is not attached to the block, so no need to keep
46998937Sdes  // Restore in this set.
47098937Sdes  SmallPtrSet<MachineBasicBlock *, 8> Visited;
47198937Sdes  SmallVector<MachineBasicBlock *, 8> WorkList;
47298937Sdes  MachineBasicBlock *Entry = &MF.front();
47398937Sdes  MachineBasicBlock *Save = MFI.getSavePoint();
47498937Sdes
47598937Sdes  if (!Save)
47698937Sdes    Save = Entry;
47798937Sdes
47898937Sdes  if (Entry != Save) {
47998937Sdes    WorkList.push_back(Entry);
48098937Sdes    Visited.insert(Entry);
48198937Sdes  }
48298937Sdes  Visited.insert(Save);
48398937Sdes
48498937Sdes  MachineBasicBlock *Restore = MFI.getRestorePoint();
48598937Sdes  if (Restore)
48698937Sdes    // By construction Restore cannot be visited, otherwise it
48798937Sdes    // means there exists a path to Restore that does not go
48898937Sdes    // through Save.
48998937Sdes    WorkList.push_back(Restore);
49098937Sdes
49198937Sdes  while (!WorkList.empty()) {
49298937Sdes    const MachineBasicBlock *CurBB = WorkList.pop_back_val();
49398937Sdes    // By construction, the region that is after the save point is
49498937Sdes    // dominated by the Save and post-dominated by the Restore.
49598937Sdes    if (CurBB == Save && Save != Restore)
49698937Sdes      continue;
49798937Sdes    // Enqueue all the successors not already visited.
49898937Sdes    // Those are by construction either before Save or after Restore.
49998937Sdes    for (MachineBasicBlock *SuccBB : CurBB->successors())
50098937Sdes      if (Visited.insert(SuccBB).second)
50198937Sdes        WorkList.push_back(SuccBB);
50298937Sdes  }
50398937Sdes
50498937Sdes  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
50598937Sdes
50698937Sdes  MachineRegisterInfo &MRI = MF.getRegInfo();
50798937Sdes  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
50898937Sdes    for (MachineBasicBlock *MBB : Visited) {
50998937Sdes      MCPhysReg Reg = CSI[i].getReg();
51098937Sdes      // Add the callee-saved register as live-in.
51198937Sdes      // It's killed at the spill.
51298937Sdes      if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg))
51398937Sdes        MBB->addLiveIn(Reg);
51498937Sdes    }
51598937Sdes    // If callee-saved register is spilled to another register rather than
51698937Sdes    // spilling to stack, the destination register has to be marked as live for
51798937Sdes    // each MBB between the prologue and epilogue so that it is not clobbered
51898937Sdes    // before it is reloaded in the epilogue. The Visited set contains all
51998937Sdes    // blocks outside of the region delimited by prologue/epilogue.
52098937Sdes    if (CSI[i].isSpilledToReg()) {
52198937Sdes      for (MachineBasicBlock &MBB : MF) {
52298937Sdes        if (Visited.count(&MBB))
52398937Sdes          continue;
52498937Sdes        MCPhysReg DstReg = CSI[i].getDstReg();
52598937Sdes        if (!MBB.isLiveIn(DstReg))
52698937Sdes          MBB.addLiveIn(DstReg);
52798937Sdes      }
52898937Sdes    }
52998937Sdes  }
53098937Sdes
53198937Sdes}
53298937Sdes
53398937Sdes/// Insert restore code for the callee-saved registers used in the function.
53498937Sdesstatic void insertCSRSaves(MachineBasicBlock &SaveBlock,
53598937Sdes                           ArrayRef<CalleeSavedInfo> CSI) {
53698937Sdes  MachineFunction &MF = *SaveBlock.getParent();
53798937Sdes  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
53898937Sdes  const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
53998937Sdes  const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
54098937Sdes
54198937Sdes  MachineBasicBlock::iterator I = SaveBlock.begin();
54298937Sdes  if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
54398937Sdes    for (const CalleeSavedInfo &CS : CSI) {
54498937Sdes      // Insert the spill to the stack frame.
54598937Sdes      unsigned Reg = CS.getReg();
546
547      if (CS.isSpilledToReg()) {
548        BuildMI(SaveBlock, I, DebugLoc(),
549                TII.get(TargetOpcode::COPY), CS.getDstReg())
550          .addReg(Reg, getKillRegState(true));
551      } else {
552        const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
553        TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC,
554                                TRI);
555      }
556    }
557  }
558}
559
560/// Insert restore code for the callee-saved registers used in the function.
561static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
562                              std::vector<CalleeSavedInfo> &CSI) {
563  MachineFunction &MF = *RestoreBlock.getParent();
564  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
565  const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
566  const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
567
568  // Restore all registers immediately before the return and any
569  // terminators that precede it.
570  MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator();
571
572  if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
573    for (const CalleeSavedInfo &CI : reverse(CSI)) {
574      unsigned Reg = CI.getReg();
575      if (CI.isSpilledToReg()) {
576        BuildMI(RestoreBlock, I, DebugLoc(), TII.get(TargetOpcode::COPY), Reg)
577          .addReg(CI.getDstReg(), getKillRegState(true));
578      } else {
579        const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
580        TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
581        assert(I != RestoreBlock.begin() &&
582               "loadRegFromStackSlot didn't insert any code!");
583        // Insert in reverse order.  loadRegFromStackSlot can insert
584        // multiple instructions.
585      }
586    }
587  }
588}
589
590void PEI::spillCalleeSavedRegs(MachineFunction &MF) {
591  // We can't list this requirement in getRequiredProperties because some
592  // targets (WebAssembly) use virtual registers past this point, and the pass
593  // pipeline is set up without giving the passes a chance to look at the
594  // TargetMachine.
595  // FIXME: Find a way to express this in getRequiredProperties.
596  assert(MF.getProperties().hasProperty(
597      MachineFunctionProperties::Property::NoVRegs));
598
599  const Function &F = MF.getFunction();
600  const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
601  MachineFrameInfo &MFI = MF.getFrameInfo();
602  MinCSFrameIndex = std::numeric_limits<unsigned>::max();
603  MaxCSFrameIndex = 0;
604
605  // Determine which of the registers in the callee save list should be saved.
606  BitVector SavedRegs;
607  TFI->determineCalleeSaves(MF, SavedRegs, RS);
608
609  // Assign stack slots for any callee-saved registers that must be spilled.
610  assignCalleeSavedSpillSlots(MF, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex);
611
612  // Add the code to save and restore the callee saved registers.
613  if (!F.hasFnAttribute(Attribute::Naked)) {
614    MFI.setCalleeSavedInfoValid(true);
615
616    std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
617    if (!CSI.empty()) {
618      if (!MFI.hasCalls())
619        NumLeafFuncWithSpills++;
620
621      for (MachineBasicBlock *SaveBlock : SaveBlocks) {
622        insertCSRSaves(*SaveBlock, CSI);
623        // Update the live-in information of all the blocks up to the save
624        // point.
625        updateLiveness(MF);
626      }
627      for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
628        insertCSRRestores(*RestoreBlock, CSI);
629    }
630  }
631}
632
633/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
634static inline void
635AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
636                  bool StackGrowsDown, int64_t &Offset,
637                  unsigned &MaxAlign, unsigned Skew) {
638  // If the stack grows down, add the object size to find the lowest address.
639  if (StackGrowsDown)
640    Offset += MFI.getObjectSize(FrameIdx);
641
642  unsigned Align = MFI.getObjectAlignment(FrameIdx);
643
644  // If the alignment of this object is greater than that of the stack, then
645  // increase the stack alignment to match.
646  MaxAlign = std::max(MaxAlign, Align);
647
648  // Adjust to alignment boundary.
649  Offset = alignTo(Offset, Align, Skew);
650
651  if (StackGrowsDown) {
652    LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
653                      << "]\n");
654    MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
655  } else {
656    LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
657                      << "]\n");
658    MFI.setObjectOffset(FrameIdx, Offset);
659    Offset += MFI.getObjectSize(FrameIdx);
660  }
661}
662
663/// Compute which bytes of fixed and callee-save stack area are unused and keep
664/// track of them in StackBytesFree.
665static inline void
666computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown,
667                      unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex,
668                      int64_t FixedCSEnd, BitVector &StackBytesFree) {
669  // Avoid undefined int64_t -> int conversion below in extreme case.
670  if (FixedCSEnd > std::numeric_limits<int>::max())
671    return;
672
673  StackBytesFree.resize(FixedCSEnd, true);
674
675  SmallVector<int, 16> AllocatedFrameSlots;
676  // Add fixed objects.
677  for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
678    // StackSlot scavenging is only implemented for the default stack.
679    if (MFI.getStackID(i) == TargetStackID::Default)
680      AllocatedFrameSlots.push_back(i);
681  // Add callee-save objects.
682  for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i)
683    if (MFI.getStackID(i) == TargetStackID::Default)
684      AllocatedFrameSlots.push_back(i);
685
686  for (int i : AllocatedFrameSlots) {
687    // These are converted from int64_t, but they should always fit in int
688    // because of the FixedCSEnd check above.
689    int ObjOffset = MFI.getObjectOffset(i);
690    int ObjSize = MFI.getObjectSize(i);
691    int ObjStart, ObjEnd;
692    if (StackGrowsDown) {
693      // ObjOffset is negative when StackGrowsDown is true.
694      ObjStart = -ObjOffset - ObjSize;
695      ObjEnd = -ObjOffset;
696    } else {
697      ObjStart = ObjOffset;
698      ObjEnd = ObjOffset + ObjSize;
699    }
700    // Ignore fixed holes that are in the previous stack frame.
701    if (ObjEnd > 0)
702      StackBytesFree.reset(ObjStart, ObjEnd);
703  }
704}
705
706/// Assign frame object to an unused portion of the stack in the fixed stack
707/// object range.  Return true if the allocation was successful.
708static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
709                                     bool StackGrowsDown, unsigned MaxAlign,
710                                     BitVector &StackBytesFree) {
711  if (MFI.isVariableSizedObjectIndex(FrameIdx))
712    return false;
713
714  if (StackBytesFree.none()) {
715    // clear it to speed up later scavengeStackSlot calls to
716    // StackBytesFree.none()
717    StackBytesFree.clear();
718    return false;
719  }
720
721  unsigned ObjAlign = MFI.getObjectAlignment(FrameIdx);
722  if (ObjAlign > MaxAlign)
723    return false;
724
725  int64_t ObjSize = MFI.getObjectSize(FrameIdx);
726  int FreeStart;
727  for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
728       FreeStart = StackBytesFree.find_next(FreeStart)) {
729
730    // Check that free space has suitable alignment.
731    unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
732    if (alignTo(ObjStart, ObjAlign) != ObjStart)
733      continue;
734
735    if (FreeStart + ObjSize > StackBytesFree.size())
736      return false;
737
738    bool AllBytesFree = true;
739    for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
740      if (!StackBytesFree.test(FreeStart + Byte)) {
741        AllBytesFree = false;
742        break;
743      }
744    if (AllBytesFree)
745      break;
746  }
747
748  if (FreeStart == -1)
749    return false;
750
751  if (StackGrowsDown) {
752    int ObjStart = -(FreeStart + ObjSize);
753    LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
754                      << ObjStart << "]\n");
755    MFI.setObjectOffset(FrameIdx, ObjStart);
756  } else {
757    LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP["
758                      << FreeStart << "]\n");
759    MFI.setObjectOffset(FrameIdx, FreeStart);
760  }
761
762  StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
763  return true;
764}
765
766/// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
767/// those required to be close to the Stack Protector) to stack offsets.
768static void
769AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
770                      SmallSet<int, 16> &ProtectedObjs,
771                      MachineFrameInfo &MFI, bool StackGrowsDown,
772                      int64_t &Offset, unsigned &MaxAlign, unsigned Skew) {
773
774  for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
775        E = UnassignedObjs.end(); I != E; ++I) {
776    int i = *I;
777    AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew);
778    ProtectedObjs.insert(i);
779  }
780}
781
782/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
783/// abstract stack objects.
784void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
785  const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
786
787  bool StackGrowsDown =
788    TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
789
790  // Loop over all of the stack objects, assigning sequential addresses...
791  MachineFrameInfo &MFI = MF.getFrameInfo();
792
793  // Start at the beginning of the local area.
794  // The Offset is the distance from the stack top in the direction
795  // of stack growth -- so it's always nonnegative.
796  int LocalAreaOffset = TFI.getOffsetOfLocalArea();
797  if (StackGrowsDown)
798    LocalAreaOffset = -LocalAreaOffset;
799  assert(LocalAreaOffset >= 0
800         && "Local area offset should be in direction of stack growth");
801  int64_t Offset = LocalAreaOffset;
802
803  // Skew to be applied to alignment.
804  unsigned Skew = TFI.getStackAlignmentSkew(MF);
805
806#ifdef EXPENSIVE_CHECKS
807  for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i)
808    if (!MFI.isDeadObjectIndex(i) &&
809        MFI.getStackID(i) == TargetStackID::Default)
810      assert(MFI.getObjectAlignment(i) <= MFI.getMaxAlignment() &&
811             "MaxAlignment is invalid");
812#endif
813
814  // If there are fixed sized objects that are preallocated in the local area,
815  // non-fixed objects can't be allocated right at the start of local area.
816  // Adjust 'Offset' to point to the end of last fixed sized preallocated
817  // object.
818  for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
819    if (MFI.getStackID(i) !=
820        TargetStackID::Default) // Only allocate objects on the default stack.
821      continue;
822
823    int64_t FixedOff;
824    if (StackGrowsDown) {
825      // The maximum distance from the stack pointer is at lower address of
826      // the object -- which is given by offset. For down growing stack
827      // the offset is negative, so we negate the offset to get the distance.
828      FixedOff = -MFI.getObjectOffset(i);
829    } else {
830      // The maximum distance from the start pointer is at the upper
831      // address of the object.
832      FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
833    }
834    if (FixedOff > Offset) Offset = FixedOff;
835  }
836
837  // First assign frame offsets to stack objects that are used to spill
838  // callee saved registers.
839  if (StackGrowsDown) {
840    for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
841      if (MFI.getStackID(i) !=
842          TargetStackID::Default) // Only allocate objects on the default stack.
843        continue;
844
845      // If the stack grows down, we need to add the size to find the lowest
846      // address of the object.
847      Offset += MFI.getObjectSize(i);
848
849      unsigned Align = MFI.getObjectAlignment(i);
850      // Adjust to alignment boundary
851      Offset = alignTo(Offset, Align, Skew);
852
853      LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << -Offset << "]\n");
854      MFI.setObjectOffset(i, -Offset);        // Set the computed offset
855    }
856  } else if (MaxCSFrameIndex >= MinCSFrameIndex) {
857    // Be careful about underflow in comparisons agains MinCSFrameIndex.
858    for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) {
859      if (MFI.getStackID(i) !=
860          TargetStackID::Default) // Only allocate objects on the default stack.
861        continue;
862
863      if (MFI.isDeadObjectIndex(i))
864        continue;
865
866      unsigned Align = MFI.getObjectAlignment(i);
867      // Adjust to alignment boundary
868      Offset = alignTo(Offset, Align, Skew);
869
870      LLVM_DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << Offset << "]\n");
871      MFI.setObjectOffset(i, Offset);
872      Offset += MFI.getObjectSize(i);
873    }
874  }
875
876  // FixedCSEnd is the stack offset to the end of the fixed and callee-save
877  // stack area.
878  int64_t FixedCSEnd = Offset;
879  unsigned MaxAlign = MFI.getMaxAlignment();
880
881  // Make sure the special register scavenging spill slot is closest to the
882  // incoming stack pointer if a frame pointer is required and is closer
883  // to the incoming rather than the final stack pointer.
884  const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
885  bool EarlyScavengingSlots = (TFI.hasFP(MF) &&
886                               TFI.isFPCloseToIncomingSP() &&
887                               RegInfo->useFPForScavengingIndex(MF) &&
888                               !RegInfo->needsStackRealignment(MF));
889  if (RS && EarlyScavengingSlots) {
890    SmallVector<int, 2> SFIs;
891    RS->getScavengingFrameIndices(SFIs);
892    for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
893           IE = SFIs.end(); I != IE; ++I)
894      AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
895  }
896
897  // FIXME: Once this is working, then enable flag will change to a target
898  // check for whether the frame is large enough to want to use virtual
899  // frame index registers. Functions which don't want/need this optimization
900  // will continue to use the existing code path.
901  if (MFI.getUseLocalStackAllocationBlock()) {
902    unsigned Align = MFI.getLocalFrameMaxAlign().value();
903
904    // Adjust to alignment boundary.
905    Offset = alignTo(Offset, Align, Skew);
906
907    LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
908
909    // Resolve offsets for objects in the local block.
910    for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
911      std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
912      int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
913      LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
914                        << "]\n");
915      MFI.setObjectOffset(Entry.first, FIOffset);
916    }
917    // Allocate the local block
918    Offset += MFI.getLocalFrameSize();
919
920    MaxAlign = std::max(Align, MaxAlign);
921  }
922
923  // Retrieve the Exception Handler registration node.
924  int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
925  if (const WinEHFuncInfo *FuncInfo = MF.getWinEHFuncInfo())
926    EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
927
928  // Make sure that the stack protector comes before the local variables on the
929  // stack.
930  SmallSet<int, 16> ProtectedObjs;
931  if (MFI.hasStackProtectorIndex()) {
932    int StackProtectorFI = MFI.getStackProtectorIndex();
933    StackObjSet LargeArrayObjs;
934    StackObjSet SmallArrayObjs;
935    StackObjSet AddrOfObjs;
936
937    // If we need a stack protector, we need to make sure that
938    // LocalStackSlotPass didn't already allocate a slot for it.
939    // If we are told to use the LocalStackAllocationBlock, the stack protector
940    // is expected to be already pre-allocated.
941    if (!MFI.getUseLocalStackAllocationBlock())
942      AdjustStackOffset(MFI, StackProtectorFI, StackGrowsDown, Offset, MaxAlign,
943                        Skew);
944    else if (!MFI.isObjectPreAllocated(MFI.getStackProtectorIndex()))
945      llvm_unreachable(
946          "Stack protector not pre-allocated by LocalStackSlotPass.");
947
948    // Assign large stack objects first.
949    for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
950      if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock())
951        continue;
952      if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
953        continue;
954      if (RS && RS->isScavengingFrameIndex((int)i))
955        continue;
956      if (MFI.isDeadObjectIndex(i))
957        continue;
958      if (StackProtectorFI == (int)i || EHRegNodeFrameIndex == (int)i)
959        continue;
960      if (MFI.getStackID(i) !=
961          TargetStackID::Default) // Only allocate objects on the default stack.
962        continue;
963
964      switch (MFI.getObjectSSPLayout(i)) {
965      case MachineFrameInfo::SSPLK_None:
966        continue;
967      case MachineFrameInfo::SSPLK_SmallArray:
968        SmallArrayObjs.insert(i);
969        continue;
970      case MachineFrameInfo::SSPLK_AddrOf:
971        AddrOfObjs.insert(i);
972        continue;
973      case MachineFrameInfo::SSPLK_LargeArray:
974        LargeArrayObjs.insert(i);
975        continue;
976      }
977      llvm_unreachable("Unexpected SSPLayoutKind.");
978    }
979
980    // We expect **all** the protected stack objects to be pre-allocated by
981    // LocalStackSlotPass. If it turns out that PEI still has to allocate some
982    // of them, we may end up messing up the expected order of the objects.
983    if (MFI.getUseLocalStackAllocationBlock() &&
984        !(LargeArrayObjs.empty() && SmallArrayObjs.empty() &&
985          AddrOfObjs.empty()))
986      llvm_unreachable("Found protected stack objects not pre-allocated by "
987                       "LocalStackSlotPass.");
988
989    AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
990                          Offset, MaxAlign, Skew);
991    AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
992                          Offset, MaxAlign, Skew);
993    AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
994                          Offset, MaxAlign, Skew);
995  }
996
997  SmallVector<int, 8> ObjectsToAllocate;
998
999  // Then prepare to assign frame offsets to stack objects that are not used to
1000  // spill callee saved registers.
1001  for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
1002    if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock())
1003      continue;
1004    if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
1005      continue;
1006    if (RS && RS->isScavengingFrameIndex((int)i))
1007      continue;
1008    if (MFI.isDeadObjectIndex(i))
1009      continue;
1010    if (MFI.getStackProtectorIndex() == (int)i || EHRegNodeFrameIndex == (int)i)
1011      continue;
1012    if (ProtectedObjs.count(i))
1013      continue;
1014    if (MFI.getStackID(i) !=
1015        TargetStackID::Default) // Only allocate objects on the default stack.
1016      continue;
1017
1018    // Add the objects that we need to allocate to our working set.
1019    ObjectsToAllocate.push_back(i);
1020  }
1021
1022  // Allocate the EH registration node first if one is present.
1023  if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
1024    AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
1025                      MaxAlign, Skew);
1026
1027  // Give the targets a chance to order the objects the way they like it.
1028  if (MF.getTarget().getOptLevel() != CodeGenOpt::None &&
1029      MF.getTarget().Options.StackSymbolOrdering)
1030    TFI.orderFrameObjects(MF, ObjectsToAllocate);
1031
1032  // Keep track of which bytes in the fixed and callee-save range are used so we
1033  // can use the holes when allocating later stack objects.  Only do this if
1034  // stack protector isn't being used and the target requests it and we're
1035  // optimizing.
1036  BitVector StackBytesFree;
1037  if (!ObjectsToAllocate.empty() &&
1038      MF.getTarget().getOptLevel() != CodeGenOpt::None &&
1039      MFI.getStackProtectorIndex() < 0 && TFI.enableStackSlotScavenging(MF))
1040    computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex,
1041                          FixedCSEnd, StackBytesFree);
1042
1043  // Now walk the objects and actually assign base offsets to them.
1044  for (auto &Object : ObjectsToAllocate)
1045    if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
1046                           StackBytesFree))
1047      AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign, Skew);
1048
1049  // Make sure the special register scavenging spill slot is closest to the
1050  // stack pointer.
1051  if (RS && !EarlyScavengingSlots) {
1052    SmallVector<int, 2> SFIs;
1053    RS->getScavengingFrameIndices(SFIs);
1054    for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
1055           IE = SFIs.end(); I != IE; ++I)
1056      AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
1057  }
1058
1059  if (!TFI.targetHandlesStackFrameRounding()) {
1060    // If we have reserved argument space for call sites in the function
1061    // immediately on entry to the current function, count it as part of the
1062    // overall stack size.
1063    if (MFI.adjustsStack() && TFI.hasReservedCallFrame(MF))
1064      Offset += MFI.getMaxCallFrameSize();
1065
1066    // Round up the size to a multiple of the alignment.  If the function has
1067    // any calls or alloca's, align to the target's StackAlignment value to
1068    // ensure that the callee's frame or the alloca data is suitably aligned;
1069    // otherwise, for leaf functions, align to the TransientStackAlignment
1070    // value.
1071    unsigned StackAlign;
1072    if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
1073        (RegInfo->needsStackRealignment(MF) && MFI.getObjectIndexEnd() != 0))
1074      StackAlign = TFI.getStackAlignment();
1075    else
1076      StackAlign = TFI.getTransientStackAlignment();
1077
1078    // If the frame pointer is eliminated, all frame offsets will be relative to
1079    // SP not FP. Align to MaxAlign so this works.
1080    StackAlign = std::max(StackAlign, MaxAlign);
1081    Offset = alignTo(Offset, StackAlign, Skew);
1082  }
1083
1084  // Update frame info to pretend that this is part of the stack...
1085  int64_t StackSize = Offset - LocalAreaOffset;
1086  MFI.setStackSize(StackSize);
1087  NumBytesStackSpace += StackSize;
1088}
1089
1090/// insertPrologEpilogCode - Scan the function for modified callee saved
1091/// registers, insert spill code for these callee saved registers, then add
1092/// prolog and epilog code to the function.
1093void PEI::insertPrologEpilogCode(MachineFunction &MF) {
1094  const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1095
1096  // Add prologue to the function...
1097  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1098    TFI.emitPrologue(MF, *SaveBlock);
1099
1100  // Add epilogue to restore the callee-save registers in each exiting block.
1101  for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
1102    TFI.emitEpilogue(MF, *RestoreBlock);
1103
1104  for (MachineBasicBlock *SaveBlock : SaveBlocks)
1105    TFI.inlineStackProbe(MF, *SaveBlock);
1106
1107  // Emit additional code that is required to support segmented stacks, if
1108  // we've been asked for it.  This, when linked with a runtime with support
1109  // for segmented stacks (libgcc is one), will result in allocating stack
1110  // space in small chunks instead of one large contiguous block.
1111  if (MF.shouldSplitStack()) {
1112    for (MachineBasicBlock *SaveBlock : SaveBlocks)
1113      TFI.adjustForSegmentedStacks(MF, *SaveBlock);
1114    // Record that there are split-stack functions, so we will emit a
1115    // special section to tell the linker.
1116    MF.getMMI().setHasSplitStack(true);
1117  } else
1118    MF.getMMI().setHasNosplitStack(true);
1119
1120  // Emit additional code that is required to explicitly handle the stack in
1121  // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
1122  // approach is rather similar to that of Segmented Stacks, but it uses a
1123  // different conditional check and another BIF for allocating more stack
1124  // space.
1125  if (MF.getFunction().getCallingConv() == CallingConv::HiPE)
1126    for (MachineBasicBlock *SaveBlock : SaveBlocks)
1127      TFI.adjustForHiPEPrologue(MF, *SaveBlock);
1128}
1129
1130/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1131/// register references and actual offsets.
1132void PEI::replaceFrameIndices(MachineFunction &MF) {
1133  const auto &ST = MF.getSubtarget();
1134  const TargetFrameLowering &TFI = *ST.getFrameLowering();
1135  if (!TFI.needsFrameIndexResolution(MF))
1136    return;
1137
1138  const TargetRegisterInfo *TRI = ST.getRegisterInfo();
1139
1140  // Allow the target to determine this after knowing the frame size.
1141  FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
1142    TRI->requiresFrameIndexReplacementScavenging(MF);
1143
1144  // Store SPAdj at exit of a basic block.
1145  SmallVector<int, 8> SPState;
1146  SPState.resize(MF.getNumBlockIDs());
1147  df_iterator_default_set<MachineBasicBlock*> Reachable;
1148
1149  // Iterate over the reachable blocks in DFS order.
1150  for (auto DFI = df_ext_begin(&MF, Reachable), DFE = df_ext_end(&MF, Reachable);
1151       DFI != DFE; ++DFI) {
1152    int SPAdj = 0;
1153    // Check the exit state of the DFS stack predecessor.
1154    if (DFI.getPathLength() >= 2) {
1155      MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1156      assert(Reachable.count(StackPred) &&
1157             "DFS stack predecessor is already visited.\n");
1158      SPAdj = SPState[StackPred->getNumber()];
1159    }
1160    MachineBasicBlock *BB = *DFI;
1161    replaceFrameIndices(BB, MF, SPAdj);
1162    SPState[BB->getNumber()] = SPAdj;
1163  }
1164
1165  // Handle the unreachable blocks.
1166  for (auto &BB : MF) {
1167    if (Reachable.count(&BB))
1168      // Already handled in DFS traversal.
1169      continue;
1170    int SPAdj = 0;
1171    replaceFrameIndices(&BB, MF, SPAdj);
1172  }
1173}
1174
1175void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1176                              int &SPAdj) {
1177  assert(MF.getSubtarget().getRegisterInfo() &&
1178         "getRegisterInfo() must be implemented!");
1179  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1180  const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1181  const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
1182
1183  if (RS && FrameIndexEliminationScavenging)
1184    RS->enterBasicBlock(*BB);
1185
1186  bool InsideCallSequence = false;
1187
1188  for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
1189    if (TII.isFrameInstr(*I)) {
1190      InsideCallSequence = TII.isFrameSetup(*I);
1191      SPAdj += TII.getSPAdjust(*I);
1192      I = TFI->eliminateCallFramePseudoInstr(MF, *BB, I);
1193      continue;
1194    }
1195
1196    MachineInstr &MI = *I;
1197    bool DoIncr = true;
1198    bool DidFinishLoop = true;
1199    for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1200      if (!MI.getOperand(i).isFI())
1201        continue;
1202
1203      // Frame indices in debug values are encoded in a target independent
1204      // way with simply the frame index and offset rather than any
1205      // target-specific addressing mode.
1206      if (MI.isDebugValue()) {
1207        assert(i == 0 && "Frame indices can only appear as the first "
1208                         "operand of a DBG_VALUE machine instruction");
1209        unsigned Reg;
1210        unsigned FrameIdx = MI.getOperand(0).getIndex();
1211        unsigned Size = MF.getFrameInfo().getObjectSize(FrameIdx);
1212
1213        int64_t Offset =
1214            TFI->getFrameIndexReference(MF, FrameIdx, Reg);
1215        MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
1216        MI.getOperand(0).setIsDebug();
1217
1218        const DIExpression *DIExpr = MI.getDebugExpression();
1219
1220        // If we have a direct DBG_VALUE, and its location expression isn't
1221        // currently complex, then adding an offset will morph it into a
1222        // complex location that is interpreted as being a memory address.
1223        // This changes a pointer-valued variable to dereference that pointer,
1224        // which is incorrect. Fix by adding DW_OP_stack_value.
1225        unsigned PrependFlags = DIExpression::ApplyOffset;
1226        if (!MI.isIndirectDebugValue() && !DIExpr->isComplex())
1227          PrependFlags |= DIExpression::StackValue;
1228
1229        // If we have DBG_VALUE that is indirect and has a Implicit location
1230        // expression need to insert a deref before prepending a Memory
1231        // location expression. Also after doing this we change the DBG_VALUE
1232        // to be direct.
1233        if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) {
1234          SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size, Size};
1235          bool WithStackValue = true;
1236          DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue);
1237          // Make the DBG_VALUE direct.
1238          MI.getOperand(1).ChangeToRegister(0, false);
1239        }
1240        DIExpr = DIExpression::prepend(DIExpr, PrependFlags, Offset);
1241        MI.getOperand(3).setMetadata(DIExpr);
1242        continue;
1243      }
1244
1245      // TODO: This code should be commoned with the code for
1246      // PATCHPOINT. There's no good reason for the difference in
1247      // implementation other than historical accident.  The only
1248      // remaining difference is the unconditional use of the stack
1249      // pointer as the base register.
1250      if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
1251        assert((!MI.isDebugValue() || i == 0) &&
1252               "Frame indicies can only appear as the first operand of a "
1253               "DBG_VALUE machine instruction");
1254        unsigned Reg;
1255        MachineOperand &Offset = MI.getOperand(i + 1);
1256        int refOffset = TFI->getFrameIndexReferencePreferSP(
1257            MF, MI.getOperand(i).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
1258        Offset.setImm(Offset.getImm() + refOffset + SPAdj);
1259        MI.getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
1260        continue;
1261      }
1262
1263      // Some instructions (e.g. inline asm instructions) can have
1264      // multiple frame indices and/or cause eliminateFrameIndex
1265      // to insert more than one instruction. We need the register
1266      // scavenger to go through all of these instructions so that
1267      // it can update its register information. We keep the
1268      // iterator at the point before insertion so that we can
1269      // revisit them in full.
1270      bool AtBeginning = (I == BB->begin());
1271      if (!AtBeginning) --I;
1272
1273      // If this instruction has a FrameIndex operand, we need to
1274      // use that target machine register info object to eliminate
1275      // it.
1276      TRI.eliminateFrameIndex(MI, SPAdj, i,
1277                              FrameIndexEliminationScavenging ?  RS : nullptr);
1278
1279      // Reset the iterator if we were at the beginning of the BB.
1280      if (AtBeginning) {
1281        I = BB->begin();
1282        DoIncr = false;
1283      }
1284
1285      DidFinishLoop = false;
1286      break;
1287    }
1288
1289    // If we are looking at a call sequence, we need to keep track of
1290    // the SP adjustment made by each instruction in the sequence.
1291    // This includes both the frame setup/destroy pseudos (handled above),
1292    // as well as other instructions that have side effects w.r.t the SP.
1293    // Note that this must come after eliminateFrameIndex, because
1294    // if I itself referred to a frame index, we shouldn't count its own
1295    // adjustment.
1296    if (DidFinishLoop && InsideCallSequence)
1297      SPAdj += TII.getSPAdjust(MI);
1298
1299    if (DoIncr && I != BB->end()) ++I;
1300
1301    // Update register states.
1302    if (RS && FrameIndexEliminationScavenging && DidFinishLoop)
1303      RS->forward(MI);
1304  }
1305}
1306