blob: c53b96cc71aad3da93f1c27a09a140daea596e1c [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00009//
Anton Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the Mips implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000013
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "MipsFrameLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/MipsBaseInfo.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000016#include "MipsInstrInfo.h"
17#include "MipsMachineFunction.h"
Akira Hatanakafab89292012-08-02 18:21:47 +000018#include "MipsTargetMachine.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000026#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000028
29using namespace llvm;
30
31
Akira Hatanakae2489122011-04-15 21:51:11 +000032//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000033//
34// Stack Frame Processing methods
35// +----------------------------+
36//
37// The stack is allocated decrementing the stack pointer on
38// the first instruction of a function prologue. Once decremented,
39// all stack references are done thought a positive offset
40// from the stack/frame pointer, so the stack is considering
41// to grow up! Otherwise terrible hacks would have to be made
42// to get this stack ABI compliant :)
43//
44// The stack frame required by the ABI (after call):
45// Offset
46//
47// 0 ----------
48// 4 Args to pass
49// . saved $GP (used in PIC)
50// . Alloca allocations
51// . Local Area
52// . CPU "Callee Saved" Registers
53// . saved FP
54// . saved RA
55// . FPU "Callee Saved" Registers
56// StackSize -----------
57//
58// Offset - offset from sp after stack allocation on function prologue
59//
60// The sp is the stack pointer subtracted/added from the stack size
61// at the Prologue/Epilogue
62//
63// References to the previous stack (to obtain arguments) are done
64// with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
65//
66// Examples:
67// - reference to the actual stack frame
68// for any local area var there is smt like : FI >= 0, StackOffset: 4
69// sw REGX, 4(SP)
70//
71// - reference to previous stack frame
72// suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
73// The emitted instruction will be something like:
74// lw REGX, 16+StackSize(SP)
75//
76// Since the total stack size is unknown on LowerFormalArguments, all
77// stack references (ObjectOffset) created to reference the function
78// arguments, are negative numbers. This way, on eliminateFrameIndex it's
79// possible to detect those references and the offsets are adjusted to
80// their real location.
81//
Akira Hatanakae2489122011-04-15 21:51:11 +000082//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000083
Eric Christophere54f10e2014-07-18 23:33:47 +000084const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) {
85 if (ST.inMips16Mode())
Akira Hatanakafab89292012-08-02 18:21:47 +000086 return llvm::createMips16FrameLowering(ST);
87
88 return llvm::createMipsSEFrameLowering(ST);
89}
90
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000091// hasFP - Return true if the specified function should have a dedicated frame
Vasileios Kalintirisbb698c72015-06-02 13:14:46 +000092// pointer register. This is true if the function has variable sized allocas,
93// if it needs dynamic stack realignment, if frame pointer elimination is
94// disabled, or if the frame address is taken.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000095bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000096 const MachineFrameInfo *MFI = MF.getFrameInfo();
Vasileios Kalintirisbb698c72015-06-02 13:14:46 +000097 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
98
Nick Lewycky50f02cb2011-12-02 22:16:29 +000099 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
Vasileios Kalintirisbb698c72015-06-02 13:14:46 +0000100 MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() ||
101 TRI->needsStackRealignment(MF);
102}
103
104bool MipsFrameLowering::hasBP(const MachineFunction &MF) const {
105 const MachineFrameInfo *MFI = MF.getFrameInfo();
106 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
107
108 return MFI->hasVarSizedObjects() && TRI->needsStackRealignment(MF);
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000109}
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000110
111uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
112 const MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +0000113 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000114
115 int64_t Offset = 0;
116
117 // Iterate over fixed sized objects.
118 for (int I = MFI->getObjectIndexBegin(); I != 0; ++I)
119 Offset = std::max(Offset, -MFI->getObjectOffset(I));
120
121 // Conservatively assume all callee-saved registers will be saved.
Craig Topper840beec2014-04-04 05:16:06 +0000122 for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000123 unsigned Size = TRI.getMinimalPhysRegClass(*R)->getSize();
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000124 Offset = alignTo(Offset + Size, Size);
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000125 }
126
127 unsigned MaxAlign = MFI->getMaxAlignment();
128
129 // Check that MaxAlign is not zero if there is a stack object that is not a
130 // callee-saved spill.
131 assert(!MFI->getObjectIndexEnd() || MaxAlign);
132
133 // Iterate over other objects.
134 for (unsigned I = 0, E = MFI->getObjectIndexEnd(); I != E; ++I)
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000135 Offset = alignTo(Offset + MFI->getObjectSize(I), MaxAlign);
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000136
137 // Call frame.
138 if (MFI->adjustsStack() && hasReservedCallFrame(MF))
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000139 Offset = alignTo(Offset + MFI->getMaxCallFrameSize(),
140 std::max(MaxAlign, getStackAlignment()));
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000141
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000142 return alignTo(Offset, getStackAlignment());
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000143}
Vasileios Kalintiris580f0672015-04-02 11:09:40 +0000144
145// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
Hans Wennborge1a2e902016-03-31 18:33:38 +0000146MachineBasicBlock::iterator MipsFrameLowering::
Vasileios Kalintiris580f0672015-04-02 11:09:40 +0000147eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
148 MachineBasicBlock::iterator I) const {
149 unsigned SP = STI.getABI().IsN64() ? Mips::SP_64 : Mips::SP;
150
151 if (!hasReservedCallFrame(MF)) {
152 int64_t Amount = I->getOperand(0).getImm();
153 if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
154 Amount = -Amount;
155
156 STI.getInstrInfo()->adjustStackPtr(SP, Amount, MBB, I);
157 }
158
Hans Wennborge1a2e902016-03-31 18:33:38 +0000159 return MBB.erase(I);
Vasileios Kalintiris580f0672015-04-02 11:09:40 +0000160}