2017-12-18 20:10:56 +00:00
|
|
|
//===- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function ---===//
|
2009-06-02 17:52:33 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass is responsible for finalizing the functions frame layout, saving
|
|
|
|
// callee saved registers, and for emitting prolog & epilog code for the
|
|
|
|
// function.
|
|
|
|
//
|
|
|
|
// This pass must be run after register allocation. After this pass is
|
|
|
|
// executed, it is illegal to construct MO_FrameIndex operands.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2009-06-02 17:52:33 +00:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2013-12-22 00:04:03 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
|
|
|
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
|
2009-06-02 17:52:33 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/CodeGen/StackProtector.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/CodeGen/TargetFrameLowering.h"
|
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetOpcodes.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2015-05-27 18:44:32 +00:00
|
|
|
#include "llvm/CodeGen/WinEHFuncInfo.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/IR/Attributes.h"
|
|
|
|
#include "llvm/IR/CallingConv.h"
|
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/IR/InlineAsm.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CodeGen.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-09-17 15:48:55 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
2013-12-22 00:04:03 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2017-12-18 20:10:56 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-05-29 16:25:25 +00:00
|
|
|
#define DEBUG_TYPE "prologepilog"
|
2014-11-24 09:08:18 +00:00
|
|
|
|
2017-12-18 20:10:56 +00:00
|
|
|
using MBBVector = SmallVector<MachineBasicBlock *, 4>;
|
2016-07-23 20:41:05 +00:00
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
namespace {
|
2017-12-18 20:10:56 +00:00
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
class PEI : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2017-12-18 20:10:56 +00:00
|
|
|
|
2017-05-22 19:43:28 +00:00
|
|
|
PEI() : MachineFunctionPass(ID) {
|
2015-05-27 18:44:32 +00:00
|
|
|
initializePEIPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
|
|
|
|
/// frame indexes with appropriate references.
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
RegScavenger *RS;
|
|
|
|
|
|
|
|
// MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
|
|
|
|
// stack frame indexes.
|
2016-07-23 20:41:05 +00:00
|
|
|
unsigned MinCSFrameIndex = std::numeric_limits<unsigned>::max();
|
|
|
|
unsigned MaxCSFrameIndex = 0;
|
2015-05-27 18:44:32 +00:00
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
// Save and Restore blocks of the current function. Typically there is a
|
|
|
|
// single save block, unless Windows EH funclets are involved.
|
2016-07-23 20:41:05 +00:00
|
|
|
MBBVector SaveBlocks;
|
|
|
|
MBBVector RestoreBlocks;
|
2015-05-27 18:44:32 +00:00
|
|
|
|
|
|
|
// Flag to control whether to use the register scavenger to resolve
|
|
|
|
// frame index materialization registers. Set according to
|
|
|
|
// TRI->requiresFrameIndexScavenging() for the current function.
|
|
|
|
bool FrameIndexVirtualScavenging;
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
// Flag to control whether the scavenger should be passed even though
|
|
|
|
// FrameIndexVirtualScavenging is used.
|
|
|
|
bool FrameIndexEliminationScavenging;
|
|
|
|
|
2017-12-18 20:10:56 +00:00
|
|
|
// Emit remarks.
|
|
|
|
MachineOptimizationRemarkEmitter *ORE = nullptr;
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
void calculateCallFrameInfo(MachineFunction &Fn);
|
|
|
|
void calculateSaveRestoreBlocks(MachineFunction &Fn);
|
2017-12-18 20:10:56 +00:00
|
|
|
void spillCalleeSavedRegs(MachineFunction &MF);
|
2016-07-23 20:41:05 +00:00
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
void calculateFrameObjectOffsets(MachineFunction &Fn);
|
|
|
|
void replaceFrameIndices(MachineFunction &Fn);
|
|
|
|
void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn,
|
|
|
|
int &SPAdj);
|
|
|
|
void insertPrologEpilogCode(MachineFunction &Fn);
|
|
|
|
};
|
2017-12-18 20:10:56 +00:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2015-05-27 18:44:32 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
char PEI::ID = 0;
|
2017-12-18 20:10:56 +00:00
|
|
|
|
2012-04-14 13:54:10 +00:00
|
|
|
char &llvm::PrologEpilogCodeInserterID = PEI::ID;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
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"));
|
|
|
|
|
2017-05-29 16:25:25 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false,
|
2017-05-22 19:43:28 +00:00
|
|
|
false)
|
2011-02-20 12:57:14 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
2014-11-24 09:08:18 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(StackProtector)
|
2017-12-18 20:10:56 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
|
2017-05-29 16:25:25 +00:00
|
|
|
INITIALIZE_PASS_END(PEI, DEBUG_TYPE,
|
2017-05-22 19:43:28 +00:00
|
|
|
"Prologue/Epilogue Insertion & Frame Finalization", false,
|
|
|
|
false)
|
2016-07-23 20:41:05 +00:00
|
|
|
|
2017-05-22 19:43:28 +00:00
|
|
|
MachineFunctionPass *llvm::createPrologEpilogInserterPass() {
|
|
|
|
return new PEI();
|
2016-07-23 20:41:05 +00:00
|
|
|
}
|
2010-09-17 15:48:55 +00:00
|
|
|
|
2011-10-20 21:10:27 +00:00
|
|
|
STATISTIC(NumBytesStackSpace,
|
|
|
|
"Number of bytes used for stack in all functions");
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
2014-11-24 09:08:18 +00:00
|
|
|
AU.addRequired<StackProtector>();
|
2017-12-18 20:10:56 +00:00
|
|
|
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
|
2013-12-22 00:04:03 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
/// StackObjSet - A set of stack object indexes
|
2017-12-18 20:10:56 +00:00
|
|
|
using StackObjSet = SmallSetVector<int, 8>;
|
2014-11-24 09:08:18 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
|
|
|
|
/// frame indexes with appropriate references.
|
|
|
|
bool PEI::runOnMachineFunction(MachineFunction &Fn) {
|
2017-12-18 20:10:56 +00:00
|
|
|
const Function &F = Fn.getFunction();
|
2015-01-18 16:17:27 +00:00
|
|
|
const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
|
|
|
|
const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
|
2011-02-20 12:57:14 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : nullptr;
|
2009-10-14 17:57:32 +00:00
|
|
|
FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn);
|
2017-01-02 19:17:04 +00:00
|
|
|
FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
|
|
|
|
TRI->requiresFrameIndexReplacementScavenging(Fn);
|
2017-12-18 20:10:56 +00:00
|
|
|
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2010-05-27 15:15:58 +00:00
|
|
|
// Calculate the MaxCallFrameSize and AdjustsStack variables for the
|
|
|
|
// function's frame information. Also eliminates call frame pseudo
|
|
|
|
// instructions.
|
2016-07-23 20:41:05 +00:00
|
|
|
calculateCallFrameInfo(Fn);
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
// Determine placement of CSR spill/restore code and prolog/epilog code:
|
2013-12-22 00:04:03 +00:00
|
|
|
// place all spills in the entry block, all restores in return blocks.
|
2016-07-23 20:41:05 +00:00
|
|
|
calculateSaveRestoreBlocks(Fn);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
// Handle CSR spilling and restoring, for targets that need it.
|
2017-12-18 20:10:56 +00:00
|
|
|
if (Fn.getTarget().usesPhysRegsForPEI())
|
|
|
|
spillCalleeSavedRegs(Fn);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Allow the target machine to make final modifications to the function
|
|
|
|
// before the frame layout is finalized.
|
2013-04-08 18:41:23 +00:00
|
|
|
TFI->processFunctionBeforeFrameFinalized(Fn, RS);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Calculate actual frame offsets for all abstract stack objects...
|
|
|
|
calculateFrameObjectOffsets(Fn);
|
|
|
|
|
|
|
|
// Add prolog and epilog code to the function. This function is required
|
|
|
|
// to align the stack frame as necessary for any stack variables or
|
2010-05-27 15:15:58 +00:00
|
|
|
// called functions. Because of this, calculateCalleeSavedRegisters()
|
|
|
|
// must be called before this function in order to set the AdjustsStack
|
2009-06-02 17:52:33 +00:00
|
|
|
// and MaxCallFrameSize variables.
|
2017-12-18 20:10:56 +00:00
|
|
|
if (!F.hasFnAttribute(Attribute::Naked))
|
2009-10-14 17:57:32 +00:00
|
|
|
insertPrologEpilogCode(Fn);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Replace all MO_FrameIndex operands with physical register references
|
|
|
|
// and actual offsets.
|
|
|
|
//
|
|
|
|
replaceFrameIndices(Fn);
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// If register scavenging is needed, as we've enabled doing it as a
|
2014-11-24 09:08:18 +00:00
|
|
|
// post-pass, scavenge the virtual registers that frame index elimination
|
2009-10-14 17:57:32 +00:00
|
|
|
// inserted.
|
2017-12-18 20:10:56 +00:00
|
|
|
if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging)
|
|
|
|
scavengeFrameVirtualRegs(Fn, *RS);
|
2012-04-14 13:54:10 +00:00
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
// Warn on stack size when we exceeds the given limit.
|
2017-01-02 19:17:04 +00:00
|
|
|
MachineFrameInfo &MFI = Fn.getFrameInfo();
|
|
|
|
uint64_t StackSize = MFI.getStackSize();
|
2014-11-24 09:08:18 +00:00
|
|
|
if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) {
|
2017-12-18 20:10:56 +00:00
|
|
|
DiagnosticInfoStackSize DiagStackSize(F, StackSize);
|
|
|
|
F.getContext().diagnose(DiagStackSize);
|
2014-11-24 09:08:18 +00:00
|
|
|
}
|
2013-12-22 00:04:03 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
delete RS;
|
2015-12-30 11:46:15 +00:00
|
|
|
SaveBlocks.clear();
|
2015-05-27 18:44:32 +00:00
|
|
|
RestoreBlocks.clear();
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setSavePoint(nullptr);
|
|
|
|
MFI.setRestorePoint(nullptr);
|
2009-06-02 17:52:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
/// Calculate the MaxCallFrameSize and AdjustsStack
|
2009-10-14 17:57:32 +00:00
|
|
|
/// variables for the function's frame information and eliminate call frame
|
|
|
|
/// pseudo instructions.
|
2016-07-23 20:41:05 +00:00
|
|
|
void PEI::calculateCallFrameInfo(MachineFunction &Fn) {
|
2015-01-18 16:17:27 +00:00
|
|
|
const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
|
|
|
|
const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
|
2017-01-02 19:17:04 +00:00
|
|
|
MachineFrameInfo &MFI = Fn.getFrameInfo();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
unsigned MaxCallFrameSize = 0;
|
2017-01-02 19:17:04 +00:00
|
|
|
bool AdjustsStack = MFI.adjustsStack();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Get the function call frame set-up and tear-down instruction opcode
|
2015-05-27 18:44:32 +00:00
|
|
|
unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
|
|
|
|
unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// Early exit for targets which have no call frame setup/destroy pseudo
|
|
|
|
// instructions.
|
2015-05-27 18:44:32 +00:00
|
|
|
if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
|
2009-06-02 17:52:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<MachineBasicBlock::iterator> FrameSDOps;
|
|
|
|
for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
|
|
|
|
for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
|
2017-04-16 16:01:22 +00:00
|
|
|
if (TII.isFrameInstr(*I)) {
|
|
|
|
unsigned Size = TII.getFrameSize(*I);
|
2009-06-02 17:52:33 +00:00
|
|
|
if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
|
2010-05-27 15:15:58 +00:00
|
|
|
AdjustsStack = true;
|
2009-06-02 17:52:33 +00:00
|
|
|
FrameSDOps.push_back(I);
|
2010-02-16 09:30:23 +00:00
|
|
|
} else if (I->isInlineAsm()) {
|
2010-07-13 17:19:57 +00:00
|
|
|
// Some inline asm's need a stack frame, as indicated by operand 1.
|
2011-02-20 12:57:14 +00:00
|
|
|
unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
|
|
|
|
if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
|
2010-07-13 17:19:57 +00:00
|
|
|
AdjustsStack = true;
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 17:12:57 +00:00
|
|
|
assert(!MFI.isMaxCallFrameSizeComputed() ||
|
|
|
|
(MFI.getMaxCallFrameSize() == MaxCallFrameSize &&
|
|
|
|
MFI.adjustsStack() == AdjustsStack));
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setAdjustsStack(AdjustsStack);
|
|
|
|
MFI.setMaxCallFrameSize(MaxCallFrameSize);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
for (std::vector<MachineBasicBlock::iterator>::iterator
|
|
|
|
i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) {
|
|
|
|
MachineBasicBlock::iterator I = *i;
|
|
|
|
|
|
|
|
// If call frames are not being included as part of the stack frame, and
|
2010-03-03 17:27:15 +00:00
|
|
|
// the target doesn't indicate otherwise, remove the call frame pseudos
|
|
|
|
// here. The sub/add sp instruction pairs are still inserted, but we don't
|
|
|
|
// need to track the SP adjustment for frame index elimination.
|
2011-02-20 12:57:14 +00:00
|
|
|
if (TFI->canSimplifyCallFramePseudos(Fn))
|
2013-04-08 18:41:23 +00:00
|
|
|
TFI->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
/// Compute the sets of entry and return blocks for saving and restoring
|
|
|
|
/// callee-saved registers, and placing prolog and epilog code.
|
|
|
|
void PEI::calculateSaveRestoreBlocks(MachineFunction &Fn) {
|
2017-01-02 19:17:04 +00:00
|
|
|
const MachineFrameInfo &MFI = Fn.getFrameInfo();
|
2016-07-23 20:41:05 +00:00
|
|
|
|
|
|
|
// Even when we do not change any CSR, we still want to insert the
|
|
|
|
// prologue and epilogue of the function.
|
|
|
|
// So set the save points for those.
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
// Use the points found by shrink-wrapping, if any.
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.getSavePoint()) {
|
|
|
|
SaveBlocks.push_back(MFI.getSavePoint());
|
|
|
|
assert(MFI.getRestorePoint() && "Both restore and save must be set");
|
|
|
|
MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
|
2016-07-23 20:41:05 +00:00
|
|
|
// If RestoreBlock does not have any successor and is not a return block
|
|
|
|
// then the end point is unreachable and we do not need to insert any
|
|
|
|
// epilogue.
|
|
|
|
if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
|
|
|
|
RestoreBlocks.push_back(RestoreBlock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save refs to entry and return blocks.
|
|
|
|
SaveBlocks.push_back(&Fn.front());
|
|
|
|
for (MachineBasicBlock &MBB : Fn) {
|
|
|
|
if (MBB.isEHFuncletEntry())
|
|
|
|
SaveBlocks.push_back(&MBB);
|
|
|
|
if (MBB.isReturnBlock())
|
|
|
|
RestoreBlocks.push_back(&MBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void assignCalleeSavedSpillSlots(MachineFunction &F,
|
|
|
|
const BitVector &SavedRegs,
|
|
|
|
unsigned &MinCSFrameIndex,
|
|
|
|
unsigned &MaxCSFrameIndex) {
|
2015-08-07 23:01:33 +00:00
|
|
|
if (SavedRegs.empty())
|
2009-10-14 17:57:32 +00:00
|
|
|
return;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2015-08-07 23:01:33 +00:00
|
|
|
const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo();
|
2017-04-16 16:01:22 +00:00
|
|
|
const MCPhysReg *CSRegs = F.getRegInfo().getCalleeSavedRegs();
|
2010-05-04 16:11:02 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
std::vector<CalleeSavedInfo> CSI;
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i) {
|
|
|
|
unsigned Reg = CSRegs[i];
|
2015-08-07 23:01:33 +00:00
|
|
|
if (SavedRegs.test(Reg))
|
2010-07-13 17:19:57 +00:00
|
|
|
CSI.push_back(CalleeSavedInfo(Reg));
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 23:01:33 +00:00
|
|
|
const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering();
|
2017-01-02 19:17:04 +00:00
|
|
|
MachineFrameInfo &MFI = F.getFrameInfo();
|
2014-11-24 09:08:18 +00:00
|
|
|
if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) {
|
|
|
|
// If target doesn't implement this, use generic code.
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
if (CSI.empty())
|
|
|
|
return; // Early exit if no callee saved registers are modified!
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
unsigned NumFixedSpillSlots;
|
|
|
|
const TargetFrameLowering::SpillSlot *FixedSpillSlots =
|
|
|
|
TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
// Now that we know which registers need to be saved and restored, allocate
|
|
|
|
// stack slots for them.
|
2016-07-23 20:41:05 +00:00
|
|
|
for (auto &CS : CSI) {
|
|
|
|
unsigned Reg = CS.getReg();
|
2014-11-24 09:08:18 +00:00
|
|
|
const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
int FrameIdx;
|
|
|
|
if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) {
|
2016-07-23 20:41:05 +00:00
|
|
|
CS.setFrameIdx(FrameIdx);
|
2014-11-24 09:08:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if this physreg must be spilled to a particular stack slot
|
|
|
|
// on this target.
|
|
|
|
const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots;
|
|
|
|
while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots &&
|
|
|
|
FixedSlot->Reg != Reg)
|
|
|
|
++FixedSlot;
|
|
|
|
|
2017-04-26 19:45:00 +00:00
|
|
|
unsigned Size = RegInfo->getSpillSize(*RC);
|
2014-11-24 09:08:18 +00:00
|
|
|
if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) {
|
|
|
|
// Nope, just spill it anywhere convenient.
|
2017-04-26 19:45:00 +00:00
|
|
|
unsigned Align = RegInfo->getSpillAlignment(*RC);
|
2014-11-24 09:08:18 +00:00
|
|
|
unsigned StackAlign = TFI->getStackAlignment();
|
|
|
|
|
|
|
|
// We may not be able to satisfy the desired alignment specification of
|
|
|
|
// the TargetRegisterClass if the stack alignment is smaller. Use the
|
|
|
|
// min.
|
|
|
|
Align = std::min(Align, StackAlign);
|
2017-04-26 19:45:00 +00:00
|
|
|
FrameIdx = MFI.CreateStackObject(Size, Align, true);
|
2014-11-24 09:08:18 +00:00
|
|
|
if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
|
|
|
|
if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
|
|
|
|
} else {
|
|
|
|
// Spill it to the stack where we must.
|
2017-04-26 19:45:00 +00:00
|
|
|
FrameIdx = MFI.CreateFixedSpillStackObject(Size, FixedSlot->Offset);
|
2014-11-24 09:08:18 +00:00
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
CS.setFrameIdx(FrameIdx);
|
2014-11-24 09:08:18 +00:00
|
|
|
}
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setCalleeSavedInfo(CSI);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
/// Helper function to update the liveness information for the callee-saved
|
|
|
|
/// registers.
|
|
|
|
static void updateLiveness(MachineFunction &MF) {
|
2017-01-02 19:17:04 +00:00
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
2015-05-27 18:44:32 +00:00
|
|
|
// Visited will contain all the basic blocks that are in the region
|
|
|
|
// where the callee saved registers are alive:
|
|
|
|
// - Anything that is not Save or Restore -> LiveThrough.
|
|
|
|
// - Save -> LiveIn.
|
|
|
|
// - Restore -> LiveOut.
|
|
|
|
// The live-out is not attached to the block, so no need to keep
|
|
|
|
// Restore in this set.
|
|
|
|
SmallPtrSet<MachineBasicBlock *, 8> Visited;
|
|
|
|
SmallVector<MachineBasicBlock *, 8> WorkList;
|
|
|
|
MachineBasicBlock *Entry = &MF.front();
|
2017-01-02 19:17:04 +00:00
|
|
|
MachineBasicBlock *Save = MFI.getSavePoint();
|
2015-05-27 18:44:32 +00:00
|
|
|
|
|
|
|
if (!Save)
|
|
|
|
Save = Entry;
|
|
|
|
|
|
|
|
if (Entry != Save) {
|
|
|
|
WorkList.push_back(Entry);
|
|
|
|
Visited.insert(Entry);
|
|
|
|
}
|
|
|
|
Visited.insert(Save);
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
MachineBasicBlock *Restore = MFI.getRestorePoint();
|
2015-05-27 18:44:32 +00:00
|
|
|
if (Restore)
|
|
|
|
// By construction Restore cannot be visited, otherwise it
|
|
|
|
// means there exists a path to Restore that does not go
|
|
|
|
// through Save.
|
|
|
|
WorkList.push_back(Restore);
|
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
const MachineBasicBlock *CurBB = WorkList.pop_back_val();
|
|
|
|
// By construction, the region that is after the save point is
|
|
|
|
// dominated by the Save and post-dominated by the Restore.
|
2015-12-30 11:46:15 +00:00
|
|
|
if (CurBB == Save && Save != Restore)
|
2015-05-27 18:44:32 +00:00
|
|
|
continue;
|
|
|
|
// Enqueue all the successors not already visited.
|
|
|
|
// Those are by construction either before Save or after Restore.
|
|
|
|
for (MachineBasicBlock *SuccBB : CurBB->successors())
|
|
|
|
if (Visited.insert(SuccBB).second)
|
|
|
|
WorkList.push_back(SuccBB);
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
|
2015-05-27 18:44:32 +00:00
|
|
|
|
2017-05-29 16:25:25 +00:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
2015-05-27 18:44:32 +00:00
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
|
2015-12-30 11:46:15 +00:00
|
|
|
for (MachineBasicBlock *MBB : Visited) {
|
|
|
|
MCPhysReg Reg = CSI[i].getReg();
|
2015-05-27 18:44:32 +00:00
|
|
|
// Add the callee-saved register as live-in.
|
|
|
|
// It's killed at the spill.
|
2017-05-29 16:25:25 +00:00
|
|
|
if (!MRI.isReserved(Reg) && !MBB->isLiveIn(Reg))
|
2015-12-30 11:46:15 +00:00
|
|
|
MBB->addLiveIn(Reg);
|
|
|
|
}
|
2015-05-27 18:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:10:56 +00:00
|
|
|
/// Insert restore code for the callee-saved registers used in the function.
|
|
|
|
static void insertCSRSaves(MachineBasicBlock &SaveBlock,
|
|
|
|
ArrayRef<CalleeSavedInfo> CSI) {
|
|
|
|
MachineFunction &Fn = *SaveBlock.getParent();
|
2015-01-18 16:17:27 +00:00
|
|
|
const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
|
|
|
|
const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
|
|
|
|
const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
|
2017-12-18 20:10:56 +00:00
|
|
|
|
|
|
|
MachineBasicBlock::iterator I = SaveBlock.begin();
|
|
|
|
if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
|
|
|
|
for (const CalleeSavedInfo &CS : CSI) {
|
|
|
|
// Insert the spill to the stack frame.
|
|
|
|
unsigned Reg = CS.getReg();
|
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
|
|
|
TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC,
|
|
|
|
TRI);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-18 20:10:56 +00:00
|
|
|
}
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2017-12-18 20:10:56 +00:00
|
|
|
/// Insert restore code for the callee-saved registers used in the function.
|
|
|
|
static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
|
|
|
|
std::vector<CalleeSavedInfo> &CSI) {
|
|
|
|
MachineFunction &Fn = *RestoreBlock.getParent();
|
|
|
|
const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
|
|
|
|
const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
|
|
|
|
const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
|
|
|
|
|
|
|
|
// Restore all registers immediately before the return and any
|
|
|
|
// terminators that precede it.
|
|
|
|
MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator();
|
|
|
|
|
|
|
|
if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
|
|
|
|
for (const CalleeSavedInfo &CI : reverse(CSI)) {
|
|
|
|
unsigned Reg = CI.getReg();
|
|
|
|
const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
|
|
|
|
TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
|
|
|
|
assert(I != RestoreBlock.begin() &&
|
|
|
|
"loadRegFromStackSlot didn't insert any code!");
|
|
|
|
// Insert in reverse order. loadRegFromStackSlot can insert
|
|
|
|
// multiple instructions.
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:10:56 +00:00
|
|
|
void PEI::spillCalleeSavedRegs(MachineFunction &Fn) {
|
|
|
|
// We can't list this requirement in getRequiredProperties because some
|
|
|
|
// targets (WebAssembly) use virtual registers past this point, and the pass
|
|
|
|
// pipeline is set up without giving the passes a chance to look at the
|
|
|
|
// TargetMachine.
|
|
|
|
// FIXME: Find a way to express this in getRequiredProperties.
|
|
|
|
assert(Fn.getProperties().hasProperty(
|
|
|
|
MachineFunctionProperties::Property::NoVRegs));
|
|
|
|
|
|
|
|
const Function &F = Fn.getFunction();
|
2016-07-23 20:41:05 +00:00
|
|
|
const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
|
2017-12-18 20:10:56 +00:00
|
|
|
MachineFrameInfo &MFI = Fn.getFrameInfo();
|
2016-07-23 20:41:05 +00:00
|
|
|
MinCSFrameIndex = std::numeric_limits<unsigned>::max();
|
|
|
|
MaxCSFrameIndex = 0;
|
|
|
|
|
|
|
|
// Determine which of the registers in the callee save list should be saved.
|
|
|
|
BitVector SavedRegs;
|
|
|
|
TFI->determineCalleeSaves(Fn, SavedRegs, RS);
|
|
|
|
|
|
|
|
// Assign stack slots for any callee-saved registers that must be spilled.
|
|
|
|
assignCalleeSavedSpillSlots(Fn, SavedRegs, MinCSFrameIndex, MaxCSFrameIndex);
|
|
|
|
|
|
|
|
// Add the code to save and restore the callee saved registers.
|
2017-12-18 20:10:56 +00:00
|
|
|
if (!F.hasFnAttribute(Attribute::Naked)) {
|
|
|
|
MFI.setCalleeSavedInfoValid(true);
|
|
|
|
|
|
|
|
std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
|
|
|
|
if (!CSI.empty()) {
|
|
|
|
for (MachineBasicBlock *SaveBlock : SaveBlocks) {
|
|
|
|
insertCSRSaves(*SaveBlock, CSI);
|
|
|
|
// Update the live-in information of all the blocks up to the save
|
|
|
|
// point.
|
|
|
|
updateLiveness(Fn);
|
|
|
|
}
|
|
|
|
for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
|
|
|
|
insertCSRRestores(*RestoreBlock, CSI);
|
|
|
|
}
|
|
|
|
}
|
2016-07-23 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
|
|
|
|
static inline void
|
2017-01-02 19:17:04 +00:00
|
|
|
AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
|
2009-06-02 17:52:33 +00:00
|
|
|
bool StackGrowsDown, int64_t &Offset,
|
2015-12-30 11:46:15 +00:00
|
|
|
unsigned &MaxAlign, unsigned Skew) {
|
2009-10-14 17:57:32 +00:00
|
|
|
// If the stack grows down, add the object size to find the lowest address.
|
2009-06-02 17:52:33 +00:00
|
|
|
if (StackGrowsDown)
|
2017-01-02 19:17:04 +00:00
|
|
|
Offset += MFI.getObjectSize(FrameIdx);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
unsigned Align = MFI.getObjectAlignment(FrameIdx);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// If the alignment of this object is greater than that of the stack, then
|
|
|
|
// increase the stack alignment to match.
|
|
|
|
MaxAlign = std::max(MaxAlign, Align);
|
|
|
|
|
|
|
|
// Adjust to alignment boundary.
|
2016-07-23 20:41:05 +00:00
|
|
|
Offset = alignTo(Offset, Align, Skew);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
if (StackGrowsDown) {
|
2010-09-17 15:48:55 +00:00
|
|
|
DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n");
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
|
2009-06-02 17:52:33 +00:00
|
|
|
} else {
|
2010-09-17 15:48:55 +00:00
|
|
|
DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n");
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setObjectOffset(FrameIdx, Offset);
|
|
|
|
Offset += MFI.getObjectSize(FrameIdx);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
/// Compute which bytes of fixed and callee-save stack area are unused and keep
|
|
|
|
/// track of them in StackBytesFree.
|
|
|
|
static inline void
|
2017-01-02 19:17:04 +00:00
|
|
|
computeFreeStackSlots(MachineFrameInfo &MFI, bool StackGrowsDown,
|
2016-07-23 20:41:05 +00:00
|
|
|
unsigned MinCSFrameIndex, unsigned MaxCSFrameIndex,
|
|
|
|
int64_t FixedCSEnd, BitVector &StackBytesFree) {
|
|
|
|
// Avoid undefined int64_t -> int conversion below in extreme case.
|
|
|
|
if (FixedCSEnd > std::numeric_limits<int>::max())
|
|
|
|
return;
|
|
|
|
|
|
|
|
StackBytesFree.resize(FixedCSEnd, true);
|
|
|
|
|
|
|
|
SmallVector<int, 16> AllocatedFrameSlots;
|
|
|
|
// Add fixed objects.
|
2017-01-02 19:17:04 +00:00
|
|
|
for (int i = MFI.getObjectIndexBegin(); i != 0; ++i)
|
2016-07-23 20:41:05 +00:00
|
|
|
AllocatedFrameSlots.push_back(i);
|
|
|
|
// Add callee-save objects.
|
|
|
|
for (int i = MinCSFrameIndex; i <= (int)MaxCSFrameIndex; ++i)
|
|
|
|
AllocatedFrameSlots.push_back(i);
|
|
|
|
|
|
|
|
for (int i : AllocatedFrameSlots) {
|
|
|
|
// These are converted from int64_t, but they should always fit in int
|
|
|
|
// because of the FixedCSEnd check above.
|
2017-01-02 19:17:04 +00:00
|
|
|
int ObjOffset = MFI.getObjectOffset(i);
|
|
|
|
int ObjSize = MFI.getObjectSize(i);
|
2016-07-23 20:41:05 +00:00
|
|
|
int ObjStart, ObjEnd;
|
|
|
|
if (StackGrowsDown) {
|
|
|
|
// ObjOffset is negative when StackGrowsDown is true.
|
|
|
|
ObjStart = -ObjOffset - ObjSize;
|
|
|
|
ObjEnd = -ObjOffset;
|
|
|
|
} else {
|
|
|
|
ObjStart = ObjOffset;
|
|
|
|
ObjEnd = ObjOffset + ObjSize;
|
|
|
|
}
|
|
|
|
// Ignore fixed holes that are in the previous stack frame.
|
|
|
|
if (ObjEnd > 0)
|
|
|
|
StackBytesFree.reset(ObjStart, ObjEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assign frame object to an unused portion of the stack in the fixed stack
|
|
|
|
/// object range. Return true if the allocation was successful.
|
2017-01-02 19:17:04 +00:00
|
|
|
static inline bool scavengeStackSlot(MachineFrameInfo &MFI, int FrameIdx,
|
2016-07-23 20:41:05 +00:00
|
|
|
bool StackGrowsDown, unsigned MaxAlign,
|
|
|
|
BitVector &StackBytesFree) {
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.isVariableSizedObjectIndex(FrameIdx))
|
2016-07-23 20:41:05 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (StackBytesFree.none()) {
|
|
|
|
// clear it to speed up later scavengeStackSlot calls to
|
|
|
|
// StackBytesFree.none()
|
|
|
|
StackBytesFree.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
unsigned ObjAlign = MFI.getObjectAlignment(FrameIdx);
|
2016-07-23 20:41:05 +00:00
|
|
|
if (ObjAlign > MaxAlign)
|
|
|
|
return false;
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
int64_t ObjSize = MFI.getObjectSize(FrameIdx);
|
2016-07-23 20:41:05 +00:00
|
|
|
int FreeStart;
|
|
|
|
for (FreeStart = StackBytesFree.find_first(); FreeStart != -1;
|
|
|
|
FreeStart = StackBytesFree.find_next(FreeStart)) {
|
|
|
|
|
|
|
|
// Check that free space has suitable alignment.
|
|
|
|
unsigned ObjStart = StackGrowsDown ? FreeStart + ObjSize : FreeStart;
|
|
|
|
if (alignTo(ObjStart, ObjAlign) != ObjStart)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (FreeStart + ObjSize > StackBytesFree.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool AllBytesFree = true;
|
|
|
|
for (unsigned Byte = 0; Byte < ObjSize; ++Byte)
|
|
|
|
if (!StackBytesFree.test(FreeStart + Byte)) {
|
|
|
|
AllBytesFree = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (AllBytesFree)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FreeStart == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (StackGrowsDown) {
|
|
|
|
int ObjStart = -(FreeStart + ObjSize);
|
|
|
|
DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP[" << ObjStart
|
|
|
|
<< "]\n");
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setObjectOffset(FrameIdx, ObjStart);
|
2016-07-23 20:41:05 +00:00
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") scavenged at SP[" << FreeStart
|
|
|
|
<< "]\n");
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setObjectOffset(FrameIdx, FreeStart);
|
2016-07-23 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StackBytesFree.reset(FreeStart, FreeStart + ObjSize);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
/// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
|
|
|
|
/// those required to be close to the Stack Protector) to stack offsets.
|
|
|
|
static void
|
|
|
|
AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
|
|
|
|
SmallSet<int, 16> &ProtectedObjs,
|
2017-01-02 19:17:04 +00:00
|
|
|
MachineFrameInfo &MFI, bool StackGrowsDown,
|
2015-12-30 11:46:15 +00:00
|
|
|
int64_t &Offset, unsigned &MaxAlign, unsigned Skew) {
|
2014-11-24 09:08:18 +00:00
|
|
|
|
|
|
|
for (StackObjSet::const_iterator I = UnassignedObjs.begin(),
|
|
|
|
E = UnassignedObjs.end(); I != E; ++I) {
|
|
|
|
int i = *I;
|
2015-12-30 11:46:15 +00:00
|
|
|
AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew);
|
2014-11-24 09:08:18 +00:00
|
|
|
ProtectedObjs.insert(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
|
|
|
|
/// abstract stack objects.
|
|
|
|
void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
|
2015-01-18 16:17:27 +00:00
|
|
|
const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
|
2014-11-24 09:08:18 +00:00
|
|
|
StackProtector *SP = &getAnalysis<StackProtector>();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
bool StackGrowsDown =
|
2011-02-20 12:57:14 +00:00
|
|
|
TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Loop over all of the stack objects, assigning sequential addresses...
|
2017-01-02 19:17:04 +00:00
|
|
|
MachineFrameInfo &MFI = Fn.getFrameInfo();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Start at the beginning of the local area.
|
|
|
|
// The Offset is the distance from the stack top in the direction
|
|
|
|
// of stack growth -- so it's always nonnegative.
|
2009-10-14 17:57:32 +00:00
|
|
|
int LocalAreaOffset = TFI.getOffsetOfLocalArea();
|
2009-06-02 17:52:33 +00:00
|
|
|
if (StackGrowsDown)
|
2009-10-14 17:57:32 +00:00
|
|
|
LocalAreaOffset = -LocalAreaOffset;
|
|
|
|
assert(LocalAreaOffset >= 0
|
2009-06-02 17:52:33 +00:00
|
|
|
&& "Local area offset should be in direction of stack growth");
|
2009-10-14 17:57:32 +00:00
|
|
|
int64_t Offset = LocalAreaOffset;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
// Skew to be applied to alignment.
|
|
|
|
unsigned Skew = TFI.getStackAlignmentSkew(Fn);
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
// If there are fixed sized objects that are preallocated in the local area,
|
|
|
|
// non-fixed objects can't be allocated right at the start of local area.
|
2016-07-23 20:41:05 +00:00
|
|
|
// Adjust 'Offset' to point to the end of last fixed sized preallocated
|
|
|
|
// object.
|
2017-01-02 19:17:04 +00:00
|
|
|
for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
|
2009-06-02 17:52:33 +00:00
|
|
|
int64_t FixedOff;
|
|
|
|
if (StackGrowsDown) {
|
|
|
|
// The maximum distance from the stack pointer is at lower address of
|
|
|
|
// the object -- which is given by offset. For down growing stack
|
|
|
|
// the offset is negative, so we negate the offset to get the distance.
|
2017-01-02 19:17:04 +00:00
|
|
|
FixedOff = -MFI.getObjectOffset(i);
|
2009-06-02 17:52:33 +00:00
|
|
|
} else {
|
|
|
|
// The maximum distance from the start pointer is at the upper
|
|
|
|
// address of the object.
|
2017-01-02 19:17:04 +00:00
|
|
|
FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
if (FixedOff > Offset) Offset = FixedOff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First assign frame offsets to stack objects that are used to spill
|
|
|
|
// callee saved registers.
|
|
|
|
if (StackGrowsDown) {
|
|
|
|
for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
|
2010-05-04 16:11:02 +00:00
|
|
|
// If the stack grows down, we need to add the size to find the lowest
|
2009-06-02 17:52:33 +00:00
|
|
|
// address of the object.
|
2017-01-02 19:17:04 +00:00
|
|
|
Offset += MFI.getObjectSize(i);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
unsigned Align = MFI.getObjectAlignment(i);
|
2009-06-02 17:52:33 +00:00
|
|
|
// Adjust to alignment boundary
|
2016-07-23 20:41:05 +00:00
|
|
|
Offset = alignTo(Offset, Align, Skew);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << -Offset << "]\n");
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setObjectOffset(i, -Offset); // Set the computed offset
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
2016-07-23 20:41:05 +00:00
|
|
|
} else if (MaxCSFrameIndex >= MinCSFrameIndex) {
|
|
|
|
// Be careful about underflow in comparisons agains MinCSFrameIndex.
|
|
|
|
for (unsigned i = MaxCSFrameIndex; i != MinCSFrameIndex - 1; --i) {
|
2017-05-03 20:26:11 +00:00
|
|
|
if (MFI.isDeadObjectIndex(i))
|
|
|
|
continue;
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
unsigned Align = MFI.getObjectAlignment(i);
|
2009-06-02 17:52:33 +00:00
|
|
|
// Adjust to alignment boundary
|
2016-07-23 20:41:05 +00:00
|
|
|
Offset = alignTo(Offset, Align, Skew);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
DEBUG(dbgs() << "alloc FI(" << i << ") at SP[" << Offset << "]\n");
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setObjectOffset(i, Offset);
|
|
|
|
Offset += MFI.getObjectSize(i);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
// FixedCSEnd is the stack offset to the end of the fixed and callee-save
|
|
|
|
// stack area.
|
|
|
|
int64_t FixedCSEnd = Offset;
|
2017-01-02 19:17:04 +00:00
|
|
|
unsigned MaxAlign = MFI.getMaxAlignment();
|
2010-02-16 09:30:23 +00:00
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
// Make sure the special register scavenging spill slot is closest to the
|
2013-12-22 00:04:03 +00:00
|
|
|
// incoming stack pointer if a frame pointer is required and is closer
|
|
|
|
// to the incoming rather than the final stack pointer.
|
2015-01-18 16:17:27 +00:00
|
|
|
const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
|
2013-12-22 00:04:03 +00:00
|
|
|
bool EarlyScavengingSlots = (TFI.hasFP(Fn) &&
|
|
|
|
TFI.isFPCloseToIncomingSP() &&
|
|
|
|
RegInfo->useFPForScavengingIndex(Fn) &&
|
|
|
|
!RegInfo->needsStackRealignment(Fn));
|
|
|
|
if (RS && EarlyScavengingSlots) {
|
2013-04-08 18:41:23 +00:00
|
|
|
SmallVector<int, 2> SFIs;
|
|
|
|
RS->getScavengingFrameIndices(SFIs);
|
2013-12-22 00:04:03 +00:00
|
|
|
for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
|
|
|
|
IE = SFIs.end(); I != IE; ++I)
|
2015-12-30 11:46:15 +00:00
|
|
|
AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2010-09-17 15:48:55 +00:00
|
|
|
// FIXME: Once this is working, then enable flag will change to a target
|
|
|
|
// check for whether the frame is large enough to want to use virtual
|
|
|
|
// frame index registers. Functions which don't want/need this optimization
|
|
|
|
// will continue to use the existing code path.
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.getUseLocalStackAllocationBlock()) {
|
|
|
|
unsigned Align = MFI.getLocalFrameMaxAlign();
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
// Adjust to alignment boundary.
|
2016-07-23 20:41:05 +00:00
|
|
|
Offset = alignTo(Offset, Align, Skew);
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
|
|
|
|
|
|
|
|
// Resolve offsets for objects in the local block.
|
2017-01-02 19:17:04 +00:00
|
|
|
for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
|
|
|
|
std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
|
2010-09-17 15:48:55 +00:00
|
|
|
int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
|
|
|
|
DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
|
|
|
|
FIOffset << "]\n");
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setObjectOffset(Entry.first, FIOffset);
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
// Allocate the local block
|
2017-01-02 19:17:04 +00:00
|
|
|
Offset += MFI.getLocalFrameSize();
|
2010-09-17 15:48:55 +00:00
|
|
|
|
|
|
|
MaxAlign = std::max(Align, MaxAlign);
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
// Retrieve the Exception Handler registration node.
|
2017-12-18 20:10:56 +00:00
|
|
|
int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
|
2016-07-23 20:41:05 +00:00
|
|
|
if (const WinEHFuncInfo *FuncInfo = Fn.getWinEHFuncInfo())
|
|
|
|
EHRegNodeFrameIndex = FuncInfo->EHRegNodeFrameIndex;
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
// Make sure that the stack protector comes before the local variables on the
|
|
|
|
// stack.
|
2014-11-24 09:08:18 +00:00
|
|
|
SmallSet<int, 16> ProtectedObjs;
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.getStackProtectorIndex() >= 0) {
|
2014-11-24 09:08:18 +00:00
|
|
|
StackObjSet LargeArrayObjs;
|
|
|
|
StackObjSet SmallArrayObjs;
|
|
|
|
StackObjSet AddrOfObjs;
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
AdjustStackOffset(MFI, MFI.getStackProtectorIndex(), StackGrowsDown,
|
2015-12-30 11:46:15 +00:00
|
|
|
Offset, MaxAlign, Skew);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2010-09-17 15:48:55 +00:00
|
|
|
// Assign large stack objects first.
|
2017-01-02 19:17:04 +00:00
|
|
|
for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (MFI.isObjectPreAllocated(i) &&
|
|
|
|
MFI.getUseLocalStackAllocationBlock())
|
2010-09-17 15:48:55 +00:00
|
|
|
continue;
|
|
|
|
if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
|
|
|
|
continue;
|
2013-04-08 18:41:23 +00:00
|
|
|
if (RS && RS->isScavengingFrameIndex((int)i))
|
2010-09-17 15:48:55 +00:00
|
|
|
continue;
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.isDeadObjectIndex(i))
|
2010-09-17 15:48:55 +00:00
|
|
|
continue;
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.getStackProtectorIndex() == (int)i ||
|
2016-07-23 20:41:05 +00:00
|
|
|
EHRegNodeFrameIndex == (int)i)
|
2010-09-17 15:48:55 +00:00
|
|
|
continue;
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
switch (SP->getSSPLayout(MFI.getObjectAllocation(i))) {
|
2014-11-24 09:08:18 +00:00
|
|
|
case StackProtector::SSPLK_None:
|
|
|
|
continue;
|
|
|
|
case StackProtector::SSPLK_SmallArray:
|
|
|
|
SmallArrayObjs.insert(i);
|
|
|
|
continue;
|
|
|
|
case StackProtector::SSPLK_AddrOf:
|
|
|
|
AddrOfObjs.insert(i);
|
|
|
|
continue;
|
|
|
|
case StackProtector::SSPLK_LargeArray:
|
|
|
|
LargeArrayObjs.insert(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unexpected SSPLayoutKind.");
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
2014-11-24 09:08:18 +00:00
|
|
|
|
|
|
|
AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
|
2015-12-30 11:46:15 +00:00
|
|
|
Offset, MaxAlign, Skew);
|
2014-11-24 09:08:18 +00:00
|
|
|
AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown,
|
2015-12-30 11:46:15 +00:00
|
|
|
Offset, MaxAlign, Skew);
|
2014-11-24 09:08:18 +00:00
|
|
|
AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown,
|
2015-12-30 11:46:15 +00:00
|
|
|
Offset, MaxAlign, Skew);
|
2010-09-17 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
SmallVector<int, 8> ObjectsToAllocate;
|
|
|
|
|
|
|
|
// Then prepare to assign frame offsets to stack objects that are not used to
|
|
|
|
// spill callee saved registers.
|
2017-01-02 19:17:04 +00:00
|
|
|
for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (MFI.isObjectPreAllocated(i) && MFI.getUseLocalStackAllocationBlock())
|
2010-09-17 15:48:55 +00:00
|
|
|
continue;
|
2009-06-02 17:52:33 +00:00
|
|
|
if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
|
|
|
|
continue;
|
2013-04-08 18:41:23 +00:00
|
|
|
if (RS && RS->isScavengingFrameIndex((int)i))
|
2009-06-02 17:52:33 +00:00
|
|
|
continue;
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.isDeadObjectIndex(i))
|
2009-06-02 17:52:33 +00:00
|
|
|
continue;
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.getStackProtectorIndex() == (int)i ||
|
2016-07-23 20:41:05 +00:00
|
|
|
EHRegNodeFrameIndex == (int)i)
|
2009-06-02 17:52:33 +00:00
|
|
|
continue;
|
2014-11-24 09:08:18 +00:00
|
|
|
if (ProtectedObjs.count(i))
|
2010-09-17 15:48:55 +00:00
|
|
|
continue;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
// Add the objects that we need to allocate to our working set.
|
|
|
|
ObjectsToAllocate.push_back(i);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
// Allocate the EH registration node first if one is present.
|
2017-12-18 20:10:56 +00:00
|
|
|
if (EHRegNodeFrameIndex != std::numeric_limits<int>::max())
|
2016-07-23 20:41:05 +00:00
|
|
|
AdjustStackOffset(MFI, EHRegNodeFrameIndex, StackGrowsDown, Offset,
|
|
|
|
MaxAlign, Skew);
|
|
|
|
|
|
|
|
// Give the targets a chance to order the objects the way they like it.
|
|
|
|
if (Fn.getTarget().getOptLevel() != CodeGenOpt::None &&
|
|
|
|
Fn.getTarget().Options.StackSymbolOrdering)
|
|
|
|
TFI.orderFrameObjects(Fn, ObjectsToAllocate);
|
|
|
|
|
|
|
|
// Keep track of which bytes in the fixed and callee-save range are used so we
|
|
|
|
// can use the holes when allocating later stack objects. Only do this if
|
|
|
|
// stack protector isn't being used and the target requests it and we're
|
|
|
|
// optimizing.
|
|
|
|
BitVector StackBytesFree;
|
|
|
|
if (!ObjectsToAllocate.empty() &&
|
|
|
|
Fn.getTarget().getOptLevel() != CodeGenOpt::None &&
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.getStackProtectorIndex() < 0 && TFI.enableStackSlotScavenging(Fn))
|
2016-07-23 20:41:05 +00:00
|
|
|
computeFreeStackSlots(MFI, StackGrowsDown, MinCSFrameIndex, MaxCSFrameIndex,
|
|
|
|
FixedCSEnd, StackBytesFree);
|
|
|
|
|
|
|
|
// Now walk the objects and actually assign base offsets to them.
|
|
|
|
for (auto &Object : ObjectsToAllocate)
|
|
|
|
if (!scavengeStackSlot(MFI, Object, StackGrowsDown, MaxAlign,
|
|
|
|
StackBytesFree))
|
|
|
|
AdjustStackOffset(MFI, Object, StackGrowsDown, Offset, MaxAlign, Skew);
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
// Make sure the special register scavenging spill slot is closest to the
|
|
|
|
// stack pointer.
|
2013-12-22 00:04:03 +00:00
|
|
|
if (RS && !EarlyScavengingSlots) {
|
2013-04-08 18:41:23 +00:00
|
|
|
SmallVector<int, 2> SFIs;
|
|
|
|
RS->getScavengingFrameIndices(SFIs);
|
2013-12-22 00:04:03 +00:00
|
|
|
for (SmallVectorImpl<int>::iterator I = SFIs.begin(),
|
|
|
|
IE = SFIs.end(); I != IE; ++I)
|
2015-12-30 11:46:15 +00:00
|
|
|
AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2011-02-20 12:57:14 +00:00
|
|
|
if (!TFI.targetHandlesStackFrameRounding()) {
|
2009-06-02 17:52:33 +00:00
|
|
|
// If we have reserved argument space for call sites in the function
|
|
|
|
// immediately on entry to the current function, count it as part of the
|
|
|
|
// overall stack size.
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.adjustsStack() && TFI.hasReservedCallFrame(Fn))
|
|
|
|
Offset += MFI.getMaxCallFrameSize();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// Round up the size to a multiple of the alignment. If the function has
|
|
|
|
// any calls or alloca's, align to the target's StackAlignment value to
|
|
|
|
// ensure that the callee's frame or the alloca data is suitably aligned;
|
|
|
|
// otherwise, for leaf functions, align to the TransientStackAlignment
|
|
|
|
// value.
|
|
|
|
unsigned StackAlign;
|
2017-01-02 19:17:04 +00:00
|
|
|
if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
|
|
|
|
(RegInfo->needsStackRealignment(Fn) && MFI.getObjectIndexEnd() != 0))
|
2009-10-14 17:57:32 +00:00
|
|
|
StackAlign = TFI.getStackAlignment();
|
|
|
|
else
|
|
|
|
StackAlign = TFI.getTransientStackAlignment();
|
2010-05-27 15:15:58 +00:00
|
|
|
|
|
|
|
// If the frame pointer is eliminated, all frame offsets will be relative to
|
|
|
|
// SP not FP. Align to MaxAlign so this works.
|
2009-10-14 17:57:32 +00:00
|
|
|
StackAlign = std::max(StackAlign, MaxAlign);
|
2016-07-23 20:41:05 +00:00
|
|
|
Offset = alignTo(Offset, StackAlign, Skew);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update frame info to pretend that this is part of the stack...
|
2011-10-20 21:10:27 +00:00
|
|
|
int64_t StackSize = Offset - LocalAreaOffset;
|
2017-01-02 19:17:04 +00:00
|
|
|
MFI.setStackSize(StackSize);
|
2011-10-20 21:10:27 +00:00
|
|
|
NumBytesStackSpace += StackSize;
|
2017-12-18 20:10:56 +00:00
|
|
|
|
|
|
|
ORE->emit([&]() {
|
|
|
|
return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "StackSize",
|
|
|
|
Fn.getFunction().getSubprogram(),
|
|
|
|
&Fn.front())
|
|
|
|
<< ore::NV("NumStackBytes", StackSize) << " stack bytes in function";
|
|
|
|
});
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// insertPrologEpilogCode - Scan the function for modified callee saved
|
|
|
|
/// registers, insert spill code for these callee saved registers, then add
|
|
|
|
/// prolog and epilog code to the function.
|
|
|
|
void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
|
2015-01-18 16:17:27 +00:00
|
|
|
const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Add prologue to the function...
|
2015-12-30 11:46:15 +00:00
|
|
|
for (MachineBasicBlock *SaveBlock : SaveBlocks)
|
|
|
|
TFI.emitPrologue(Fn, *SaveBlock);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
// Add epilogue to restore the callee-save registers in each exiting block.
|
|
|
|
for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
|
|
|
|
TFI.emitEpilogue(Fn, *RestoreBlock);
|
2011-10-20 21:10:27 +00:00
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
for (MachineBasicBlock *SaveBlock : SaveBlocks)
|
|
|
|
TFI.inlineStackProbe(Fn, *SaveBlock);
|
|
|
|
|
2011-10-20 21:10:27 +00:00
|
|
|
// Emit additional code that is required to support segmented stacks, if
|
|
|
|
// we've been asked for it. This, when linked with a runtime with support
|
|
|
|
// for segmented stacks (libgcc is one), will result in allocating stack
|
|
|
|
// space in small chunks instead of one large contiguous block.
|
2015-12-30 11:46:15 +00:00
|
|
|
if (Fn.shouldSplitStack()) {
|
|
|
|
for (MachineBasicBlock *SaveBlock : SaveBlocks)
|
|
|
|
TFI.adjustForSegmentedStacks(Fn, *SaveBlock);
|
2017-12-18 20:10:56 +00:00
|
|
|
// Record that there are split-stack functions, so we will emit a
|
|
|
|
// special section to tell the linker.
|
|
|
|
Fn.getMMI().setHasSplitStack(true);
|
|
|
|
} else
|
|
|
|
Fn.getMMI().setHasNosplitStack(true);
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
// Emit additional code that is required to explicitly handle the stack in
|
|
|
|
// HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The
|
|
|
|
// approach is rather similar to that of Segmented Stacks, but it uses a
|
|
|
|
// different conditional check and another BIF for allocating more stack
|
|
|
|
// space.
|
2017-12-18 20:10:56 +00:00
|
|
|
if (Fn.getFunction().getCallingConv() == CallingConv::HiPE)
|
2015-12-30 11:46:15 +00:00
|
|
|
for (MachineBasicBlock *SaveBlock : SaveBlocks)
|
|
|
|
TFI.adjustForHiPEPrologue(Fn, *SaveBlock);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
|
|
|
|
/// register references and actual offsets.
|
|
|
|
void PEI::replaceFrameIndices(MachineFunction &Fn) {
|
2015-05-27 18:44:32 +00:00
|
|
|
const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
|
|
|
|
if (!TFI.needsFrameIndexResolution(Fn)) return;
|
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
// Store SPAdj at exit of a basic block.
|
|
|
|
SmallVector<int, 8> SPState;
|
|
|
|
SPState.resize(Fn.getNumBlockIDs());
|
2017-01-02 19:17:04 +00:00
|
|
|
df_iterator_default_set<MachineBasicBlock*> Reachable;
|
2013-12-22 00:04:03 +00:00
|
|
|
|
|
|
|
// Iterate over the reachable blocks in DFS order.
|
2015-01-18 16:17:27 +00:00
|
|
|
for (auto DFI = df_ext_begin(&Fn, Reachable), DFE = df_ext_end(&Fn, Reachable);
|
2013-12-22 00:04:03 +00:00
|
|
|
DFI != DFE; ++DFI) {
|
|
|
|
int SPAdj = 0;
|
|
|
|
// Check the exit state of the DFS stack predecessor.
|
|
|
|
if (DFI.getPathLength() >= 2) {
|
|
|
|
MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
|
|
|
|
assert(Reachable.count(StackPred) &&
|
|
|
|
"DFS stack predecessor is already visited.\n");
|
|
|
|
SPAdj = SPState[StackPred->getNumber()];
|
|
|
|
}
|
|
|
|
MachineBasicBlock *BB = *DFI;
|
|
|
|
replaceFrameIndices(BB, Fn, SPAdj);
|
|
|
|
SPState[BB->getNumber()] = SPAdj;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the unreachable blocks.
|
2015-12-30 11:46:15 +00:00
|
|
|
for (auto &BB : Fn) {
|
|
|
|
if (Reachable.count(&BB))
|
2013-12-22 00:04:03 +00:00
|
|
|
// Already handled in DFS traversal.
|
|
|
|
continue;
|
|
|
|
int SPAdj = 0;
|
2015-12-30 11:46:15 +00:00
|
|
|
replaceFrameIndices(&BB, Fn, SPAdj);
|
2013-12-22 00:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn,
|
|
|
|
int &SPAdj) {
|
2015-01-18 16:17:27 +00:00
|
|
|
assert(Fn.getSubtarget().getRegisterInfo() &&
|
|
|
|
"getRegisterInfo() must be implemented!");
|
|
|
|
const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo();
|
|
|
|
const TargetRegisterInfo &TRI = *Fn.getSubtarget().getRegisterInfo();
|
|
|
|
const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
if (RS && FrameIndexEliminationScavenging)
|
|
|
|
RS->enterBasicBlock(*BB);
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2015-01-18 16:17:27 +00:00
|
|
|
bool InsideCallSequence = false;
|
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
|
2017-04-16 16:01:22 +00:00
|
|
|
if (TII.isFrameInstr(*I)) {
|
|
|
|
InsideCallSequence = TII.isFrameSetup(*I);
|
2016-07-23 20:41:05 +00:00
|
|
|
SPAdj += TII.getSPAdjust(*I);
|
|
|
|
I = TFI->eliminateCallFramePseudoInstr(Fn, *BB, I);
|
2013-12-22 00:04:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
MachineInstr &MI = *I;
|
2013-12-22 00:04:03 +00:00
|
|
|
bool DoIncr = true;
|
2016-07-23 20:41:05 +00:00
|
|
|
bool DidFinishLoop = true;
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
|
|
|
|
if (!MI.getOperand(i).isFI())
|
2013-12-22 00:04:03 +00:00
|
|
|
continue;
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
// Frame indices in debug values are encoded in a target independent
|
2013-12-22 00:04:03 +00:00
|
|
|
// way with simply the frame index and offset rather than any
|
|
|
|
// target-specific addressing mode.
|
2016-07-23 20:41:05 +00:00
|
|
|
if (MI.isDebugValue()) {
|
2015-12-30 11:46:15 +00:00
|
|
|
assert(i == 0 && "Frame indices can only appear as the first "
|
2013-12-22 00:04:03 +00:00
|
|
|
"operand of a DBG_VALUE machine instruction");
|
|
|
|
unsigned Reg;
|
2017-12-18 20:10:56 +00:00
|
|
|
int64_t Offset =
|
|
|
|
TFI->getFrameIndexReference(Fn, MI.getOperand(0).getIndex(), Reg);
|
2016-07-23 20:41:05 +00:00
|
|
|
MI.getOperand(0).ChangeToRegister(Reg, false /*isDef*/);
|
2017-12-18 20:10:56 +00:00
|
|
|
auto *DIExpr = DIExpression::prepend(MI.getDebugExpression(),
|
|
|
|
DIExpression::NoDeref, Offset);
|
|
|
|
MI.getOperand(3).setMetadata(DIExpr);
|
2013-12-22 00:04:03 +00:00
|
|
|
continue;
|
2013-04-08 18:41:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 16:17:27 +00:00
|
|
|
// TODO: This code should be commoned with the code for
|
|
|
|
// PATCHPOINT. There's no good reason for the difference in
|
|
|
|
// implementation other than historical accident. The only
|
|
|
|
// remaining difference is the unconditional use of the stack
|
|
|
|
// pointer as the base register.
|
2016-07-23 20:41:05 +00:00
|
|
|
if (MI.getOpcode() == TargetOpcode::STATEPOINT) {
|
|
|
|
assert((!MI.isDebugValue() || i == 0) &&
|
2015-01-18 16:17:27 +00:00
|
|
|
"Frame indicies can only appear as the first operand of a "
|
|
|
|
"DBG_VALUE machine instruction");
|
|
|
|
unsigned Reg;
|
2016-07-23 20:41:05 +00:00
|
|
|
MachineOperand &Offset = MI.getOperand(i + 1);
|
|
|
|
int refOffset = TFI->getFrameIndexReferencePreferSP(
|
|
|
|
Fn, MI.getOperand(i).getIndex(), Reg, /*IgnoreSPUpdates*/ false);
|
2015-01-18 16:17:27 +00:00
|
|
|
Offset.setImm(Offset.getImm() + refOffset);
|
2016-07-23 20:41:05 +00:00
|
|
|
MI.getOperand(i).ChangeToRegister(Reg, false /*isDef*/);
|
2015-01-18 16:17:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
// Some instructions (e.g. inline asm instructions) can have
|
|
|
|
// multiple frame indices and/or cause eliminateFrameIndex
|
|
|
|
// to insert more than one instruction. We need the register
|
|
|
|
// scavenger to go through all of these instructions so that
|
|
|
|
// it can update its register information. We keep the
|
|
|
|
// iterator at the point before insertion so that we can
|
|
|
|
// revisit them in full.
|
|
|
|
bool AtBeginning = (I == BB->begin());
|
|
|
|
if (!AtBeginning) --I;
|
|
|
|
|
|
|
|
// If this instruction has a FrameIndex operand, we need to
|
|
|
|
// use that target machine register info object to eliminate
|
|
|
|
// it.
|
|
|
|
TRI.eliminateFrameIndex(MI, SPAdj, i,
|
2017-01-02 19:17:04 +00:00
|
|
|
FrameIndexEliminationScavenging ? RS : nullptr);
|
2013-12-22 00:04:03 +00:00
|
|
|
|
|
|
|
// Reset the iterator if we were at the beginning of the BB.
|
|
|
|
if (AtBeginning) {
|
|
|
|
I = BB->begin();
|
|
|
|
DoIncr = false;
|
|
|
|
}
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2016-07-23 20:41:05 +00:00
|
|
|
DidFinishLoop = false;
|
2013-12-22 00:04:03 +00:00
|
|
|
break;
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
// If we are looking at a call sequence, we need to keep track of
|
|
|
|
// the SP adjustment made by each instruction in the sequence.
|
|
|
|
// This includes both the frame setup/destroy pseudos (handled above),
|
|
|
|
// as well as other instructions that have side effects w.r.t the SP.
|
2017-01-02 19:17:04 +00:00
|
|
|
// Note that this must come after eliminateFrameIndex, because
|
2015-05-27 18:44:32 +00:00
|
|
|
// if I itself referred to a frame index, we shouldn't count its own
|
|
|
|
// adjustment.
|
2016-07-23 20:41:05 +00:00
|
|
|
if (DidFinishLoop && InsideCallSequence)
|
2015-05-27 18:44:32 +00:00
|
|
|
SPAdj += TII.getSPAdjust(MI);
|
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
if (DoIncr && I != BB->end()) ++I;
|
|
|
|
|
|
|
|
// Update register states.
|
2017-01-02 19:17:04 +00:00
|
|
|
if (RS && FrameIndexEliminationScavenging && DidFinishLoop)
|
2016-07-23 20:41:05 +00:00
|
|
|
RS->forward(MI);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
}
|