blob: 8d5eabf59b71324981bb2ebd066d9aef1d25a8f0 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00006//
Akira Hatanakae2489122011-04-15 21:51:11 +00007//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00008//
Anton Korobeynikov2f931282011-01-10 12:39:04 +00009// This file contains the Mips implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000010//
Akira Hatanakae2489122011-04-15 21:51:11 +000011//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000012
Anton Korobeynikov2f931282011-01-10 12:39:04 +000013#include "MipsFrameLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "MCTargetDesc/MipsBaseInfo.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000015#include "MipsInstrInfo.h"
16#include "MipsMachineFunction.h"
Akira Hatanakafab89292012-08-02 18:21:47 +000017#include "MipsTargetMachine.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Function.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000026
27using namespace llvm;
28
29
Akira Hatanakae2489122011-04-15 21:51:11 +000030//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000031//
32// Stack Frame Processing methods
33// +----------------------------+
34//
35// The stack is allocated decrementing the stack pointer on
36// the first instruction of a function prologue. Once decremented,
37// all stack references are done thought a positive offset
38// from the stack/frame pointer, so the stack is considering
39// to grow up! Otherwise terrible hacks would have to be made
40// to get this stack ABI compliant :)
41//
42// The stack frame required by the ABI (after call):
43// Offset
44//
45// 0 ----------
46// 4 Args to pass
47// . saved $GP (used in PIC)
48// . Alloca allocations
49// . Local Area
50// . CPU "Callee Saved" Registers
51// . saved FP
52// . saved RA
53// . FPU "Callee Saved" Registers
54// StackSize -----------
55//
56// Offset - offset from sp after stack allocation on function prologue
57//
58// The sp is the stack pointer subtracted/added from the stack size
59// at the Prologue/Epilogue
60//
61// References to the previous stack (to obtain arguments) are done
62// with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
63//
64// Examples:
65// - reference to the actual stack frame
66// for any local area var there is smt like : FI >= 0, StackOffset: 4
67// sw REGX, 4(SP)
68//
69// - reference to previous stack frame
70// suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
71// The emitted instruction will be something like:
72// lw REGX, 16+StackSize(SP)
73//
74// Since the total stack size is unknown on LowerFormalArguments, all
75// stack references (ObjectOffset) created to reference the function
76// arguments, are negative numbers. This way, on eliminateFrameIndex it's
77// possible to detect those references and the offsets are adjusted to
78// their real location.
79//
Akira Hatanakae2489122011-04-15 21:51:11 +000080//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000081
Eric Christophere54f10e2014-07-18 23:33:47 +000082const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) {
83 if (ST.inMips16Mode())
Akira Hatanakafab89292012-08-02 18:21:47 +000084 return llvm::createMips16FrameLowering(ST);
85
86 return llvm::createMipsSEFrameLowering(ST);
87}
88
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000089// hasFP - Return true if the specified function should have a dedicated frame
Vasileios Kalintirisbb698c72015-06-02 13:14:46 +000090// pointer register. This is true if the function has variable sized allocas,
91// if it needs dynamic stack realignment, if frame pointer elimination is
92// disabled, or if the frame address is taken.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000093bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000094 const MachineFrameInfo &MFI = MF.getFrameInfo();
Vasileios Kalintirisbb698c72015-06-02 13:14:46 +000095 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
96
Nick Lewycky50f02cb2011-12-02 22:16:29 +000097 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +000098 MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
Vasileios Kalintirisbb698c72015-06-02 13:14:46 +000099 TRI->needsStackRealignment(MF);
100}
101
102bool MipsFrameLowering::hasBP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000103 const MachineFrameInfo &MFI = MF.getFrameInfo();
Vasileios Kalintirisbb698c72015-06-02 13:14:46 +0000104 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
105
Matthias Braun941a7052016-07-28 18:40:00 +0000106 return MFI.hasVarSizedObjects() && TRI->needsStackRealignment(MF);
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000107}
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000108
Simon Dardis725acb22017-11-02 12:47:22 +0000109// Estimate the size of the stack, including the incoming arguments. We need to
110// account for register spills, local objects, reserved call frame and incoming
111// arguments. This is required to determine the largest possible positive offset
112// from $sp so that it can be determined if an emergency spill slot for stack
113// addresses is required.
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000114uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000115 const MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +0000116 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000117
Simon Dardis725acb22017-11-02 12:47:22 +0000118 int64_t Size = 0;
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000119
Simon Dardis725acb22017-11-02 12:47:22 +0000120 // Iterate over fixed sized objects which are incoming arguments.
Matthias Braun941a7052016-07-28 18:40:00 +0000121 for (int I = MFI.getObjectIndexBegin(); I != 0; ++I)
Simon Dardis725acb22017-11-02 12:47:22 +0000122 if (MFI.getObjectOffset(I) > 0)
123 Size += MFI.getObjectSize(I);
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000124
125 // Conservatively assume all callee-saved registers will be saved.
Craig Topper840beec2014-04-04 05:16:06 +0000126 for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
Simon Dardis725acb22017-11-02 12:47:22 +0000127 unsigned RegSize = TRI.getSpillSize(*TRI.getMinimalPhysRegClass(*R));
128 Size = alignTo(Size + RegSize, RegSize);
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000129 }
130
Simon Dardis725acb22017-11-02 12:47:22 +0000131 // Get the size of the rest of the frame objects and any possible reserved
132 // call frame, accounting for alignment.
133 return Size + MFI.estimateStackSize(MF);
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000134}
Vasileios Kalintiris580f0672015-04-02 11:09:40 +0000135
136// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
Hans Wennborge1a2e902016-03-31 18:33:38 +0000137MachineBasicBlock::iterator MipsFrameLowering::
Vasileios Kalintiris580f0672015-04-02 11:09:40 +0000138eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
139 MachineBasicBlock::iterator I) const {
140 unsigned SP = STI.getABI().IsN64() ? Mips::SP_64 : Mips::SP;
141
142 if (!hasReservedCallFrame(MF)) {
143 int64_t Amount = I->getOperand(0).getImm();
144 if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
145 Amount = -Amount;
146
147 STI.getInstrInfo()->adjustStackPtr(SP, Amount, MBB, I);
148 }
149
Hans Wennborge1a2e902016-03-31 18:33:38 +0000150 return MBB.erase(I);
Vasileios Kalintiris580f0672015-04-02 11:09:40 +0000151}