2012-02-17 16:55:11 +08:00
|
|
|
//===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
|
2010-11-15 08:06:54 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2011-04-16 05:51:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-15 08:06:54 +08:00
|
|
|
//
|
2011-01-10 20:39:04 +08:00
|
|
|
// This file contains the Mips implementation of TargetFrameLowering class.
|
2010-11-15 08:06:54 +08:00
|
|
|
//
|
2011-04-16 05:51:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2011-01-10 20:39:04 +08:00
|
|
|
#include "MipsFrameLowering.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "MCTargetDesc/MipsBaseInfo.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "MipsInstrInfo.h"
|
|
|
|
#include "MipsMachineFunction.h"
|
2012-08-03 02:21:47 +08:00
|
|
|
#include "MipsTargetMachine.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2010-11-15 08:06:54 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
2011-04-16 05:51:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-15 08:06:54 +08:00
|
|
|
//
|
|
|
|
// Stack Frame Processing methods
|
|
|
|
// +----------------------------+
|
|
|
|
//
|
|
|
|
// The stack is allocated decrementing the stack pointer on
|
|
|
|
// the first instruction of a function prologue. Once decremented,
|
|
|
|
// all stack references are done thought a positive offset
|
|
|
|
// from the stack/frame pointer, so the stack is considering
|
|
|
|
// to grow up! Otherwise terrible hacks would have to be made
|
|
|
|
// to get this stack ABI compliant :)
|
|
|
|
//
|
|
|
|
// The stack frame required by the ABI (after call):
|
|
|
|
// Offset
|
|
|
|
//
|
|
|
|
// 0 ----------
|
|
|
|
// 4 Args to pass
|
|
|
|
// . saved $GP (used in PIC)
|
|
|
|
// . Alloca allocations
|
|
|
|
// . Local Area
|
|
|
|
// . CPU "Callee Saved" Registers
|
|
|
|
// . saved FP
|
|
|
|
// . saved RA
|
|
|
|
// . FPU "Callee Saved" Registers
|
|
|
|
// StackSize -----------
|
|
|
|
//
|
|
|
|
// Offset - offset from sp after stack allocation on function prologue
|
|
|
|
//
|
|
|
|
// The sp is the stack pointer subtracted/added from the stack size
|
|
|
|
// at the Prologue/Epilogue
|
|
|
|
//
|
|
|
|
// References to the previous stack (to obtain arguments) are done
|
|
|
|
// with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
// - reference to the actual stack frame
|
|
|
|
// for any local area var there is smt like : FI >= 0, StackOffset: 4
|
|
|
|
// sw REGX, 4(SP)
|
|
|
|
//
|
|
|
|
// - reference to previous stack frame
|
|
|
|
// suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
|
|
|
|
// The emitted instruction will be something like:
|
|
|
|
// lw REGX, 16+StackSize(SP)
|
|
|
|
//
|
|
|
|
// Since the total stack size is unknown on LowerFormalArguments, all
|
|
|
|
// stack references (ObjectOffset) created to reference the function
|
|
|
|
// arguments, are negative numbers. This way, on eliminateFrameIndex it's
|
|
|
|
// possible to detect those references and the offsets are adjusted to
|
|
|
|
// their real location.
|
|
|
|
//
|
2011-04-16 05:51:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-15 08:06:54 +08:00
|
|
|
|
2014-07-19 07:33:47 +08:00
|
|
|
const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) {
|
|
|
|
if (ST.inMips16Mode())
|
2012-08-03 02:21:47 +08:00
|
|
|
return llvm::createMips16FrameLowering(ST);
|
|
|
|
|
|
|
|
return llvm::createMipsSEFrameLowering(ST);
|
|
|
|
}
|
|
|
|
|
2010-11-19 05:19:35 +08:00
|
|
|
// hasFP - Return true if the specified function should have a dedicated frame
|
2015-06-02 21:14:46 +08:00
|
|
|
// pointer register. This is true if the function has variable sized allocas,
|
|
|
|
// if it needs dynamic stack realignment, if frame pointer elimination is
|
|
|
|
// disabled, or if the frame address is taken.
|
2011-01-10 20:39:04 +08:00
|
|
|
bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
|
2016-07-29 02:40:00 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
2015-06-02 21:14:46 +08:00
|
|
|
const TargetRegisterInfo *TRI = STI.getRegisterInfo();
|
|
|
|
|
2011-12-03 06:16:29 +08:00
|
|
|
return MF.getTarget().Options.DisableFramePointerElim(MF) ||
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
|
2015-06-02 21:14:46 +08:00
|
|
|
TRI->needsStackRealignment(MF);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MipsFrameLowering::hasBP(const MachineFunction &MF) const {
|
2016-07-29 02:40:00 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
2015-06-02 21:14:46 +08:00
|
|
|
const TargetRegisterInfo *TRI = STI.getRegisterInfo();
|
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
return MFI.hasVarSizedObjects() && TRI->needsStackRealignment(MF);
|
2010-11-19 05:19:35 +08:00
|
|
|
}
|
2012-11-03 05:10:22 +08:00
|
|
|
|
|
|
|
uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
|
2016-07-29 02:40:00 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
2015-01-30 07:27:36 +08:00
|
|
|
const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
|
2012-11-03 05:10:22 +08:00
|
|
|
|
|
|
|
int64_t Offset = 0;
|
|
|
|
|
|
|
|
// Iterate over fixed sized objects.
|
2016-07-29 02:40:00 +08:00
|
|
|
for (int I = MFI.getObjectIndexBegin(); I != 0; ++I)
|
|
|
|
Offset = std::max(Offset, -MFI.getObjectOffset(I));
|
2012-11-03 05:10:22 +08:00
|
|
|
|
|
|
|
// Conservatively assume all callee-saved registers will be saved.
|
2014-04-04 13:16:06 +08:00
|
|
|
for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
|
2017-04-25 02:55:33 +08:00
|
|
|
unsigned Size = TRI.getSpillSize(*TRI.getMinimalPhysRegClass(*R));
|
2016-01-15 05:06:47 +08:00
|
|
|
Offset = alignTo(Offset + Size, Size);
|
2012-11-03 05:10:22 +08:00
|
|
|
}
|
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
unsigned MaxAlign = MFI.getMaxAlignment();
|
2012-11-03 05:10:22 +08:00
|
|
|
|
|
|
|
// Check that MaxAlign is not zero if there is a stack object that is not a
|
|
|
|
// callee-saved spill.
|
2016-07-29 02:40:00 +08:00
|
|
|
assert(!MFI.getObjectIndexEnd() || MaxAlign);
|
2012-11-03 05:10:22 +08:00
|
|
|
|
|
|
|
// Iterate over other objects.
|
2016-07-29 02:40:00 +08:00
|
|
|
for (unsigned I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I)
|
|
|
|
Offset = alignTo(Offset + MFI.getObjectSize(I), MaxAlign);
|
2012-11-03 05:10:22 +08:00
|
|
|
|
|
|
|
// Call frame.
|
2016-07-29 02:40:00 +08:00
|
|
|
if (MFI.adjustsStack() && hasReservedCallFrame(MF))
|
|
|
|
Offset = alignTo(Offset + MFI.getMaxCallFrameSize(),
|
2016-01-15 05:06:47 +08:00
|
|
|
std::max(MaxAlign, getStackAlignment()));
|
2012-11-03 05:10:22 +08:00
|
|
|
|
2016-01-15 05:06:47 +08:00
|
|
|
return alignTo(Offset, getStackAlignment());
|
2012-11-03 05:10:22 +08:00
|
|
|
}
|
2015-04-02 19:09:40 +08:00
|
|
|
|
|
|
|
// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
|
2016-04-01 02:33:38 +08:00
|
|
|
MachineBasicBlock::iterator MipsFrameLowering::
|
2015-04-02 19:09:40 +08:00
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
|
|
|
unsigned SP = STI.getABI().IsN64() ? Mips::SP_64 : Mips::SP;
|
|
|
|
|
|
|
|
if (!hasReservedCallFrame(MF)) {
|
|
|
|
int64_t Amount = I->getOperand(0).getImm();
|
|
|
|
if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
|
|
|
|
Amount = -Amount;
|
|
|
|
|
|
|
|
STI.getInstrInfo()->adjustStackPtr(SP, Amount, MBB, I);
|
|
|
|
}
|
|
|
|
|
2016-04-01 02:33:38 +08:00
|
|
|
return MBB.erase(I);
|
2015-04-02 19:09:40 +08:00
|
|
|
}
|