blob: 4d69c82a1df466ed9b9ffb71fb138863f1b7d37e [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
Anton Korobeynikov33464912010-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 Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Anton Korobeynikov33464912010-11-15 00:06:54 +00009//
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010// This file contains the Mips implementation of TargetFrameLowering class.
Anton Korobeynikov33464912010-11-15 00:06:54 +000011//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Anton Korobeynikov33464912010-11-15 00:06:54 +000013
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000014#include "MipsFrameLowering.h"
Craig Topper79aa3412012-03-17 18:46:09 +000015#include "MipsAnalyzeImmediate.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000016#include "MipsInstrInfo.h"
17#include "MipsMachineFunction.h"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000018#include "MCTargetDesc/MipsBaseInfo.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000019#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetOptions.h"
27#include "llvm/Support/CommandLine.h"
28
29using namespace llvm;
30
31
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000032//===----------------------------------------------------------------------===//
Anton Korobeynikov33464912010-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 Hatanaka4552c9a2011-04-15 21:51:11 +000082//===----------------------------------------------------------------------===//
Anton Korobeynikov33464912010-11-15 00:06:54 +000083
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000084// hasFP - Return true if the specified function should have a dedicated frame
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000085// pointer register. This is true if the function has variable sized allocas or
86// if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000087bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000088 const MachineFrameInfo *MFI = MF.getFrameInfo();
Nick Lewycky8a8d4792011-12-02 22:16:29 +000089 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
90 MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000091}
92
Akira Hatanaka69c19f72011-05-23 20:16:59 +000093bool MipsFrameLowering::targetHandlesStackFrameRounding() const {
94 return true;
Anton Korobeynikov33464912010-11-15 00:06:54 +000095}
96
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000097void MipsFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +000098 MachineBasicBlock &MBB = MF.front();
99 MachineFrameInfo *MFI = MF.getFrameInfo();
100 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
101 const MipsRegisterInfo *RegInfo =
102 static_cast<const MipsRegisterInfo*>(MF.getTarget().getRegisterInfo());
103 const MipsInstrInfo &TII =
104 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
105 MachineBasicBlock::iterator MBBI = MBB.begin();
106 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Akira Hatanaka1b719502011-11-15 18:53:55 +0000107 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
108 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
109 unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000110 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
111 unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000112
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000113 // First, compute final stack size.
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000114 unsigned StackAlign = getStackAlignment();
Akira Hatanaka4782a6e2012-06-27 00:20:39 +0000115 uint64_t StackSize = RoundUpToAlignment(MFI->getStackSize(), StackAlign);
Akira Hatanakade4a1272012-07-25 03:16:47 +0000116 StackSize += RoundUpToAlignment(MipsFI->getMaxCallFrameSize(), StackAlign);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000117
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000118 // Update stack size
Jia Liubb481f82012-02-28 07:46:26 +0000119 MFI->setStackSize(StackSize);
120
Akira Hatanakaf346c692011-05-21 02:29:26 +0000121 // No need to allocate space on the stack.
122 if (StackSize == 0 && !MFI->adjustsStack()) return;
123
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000124 MachineModuleInfo &MMI = MF.getMMI();
125 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
126 MachineLocation DstML, SrcML;
127
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000128 // Adjust stack.
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000129 if (isInt<16>(-StackSize)) {// addi sp, sp, (-stacksize)
130 if (STI.inMips16Mode())
131 BuildMI(MBB, MBBI, dl,
132 TII.get(Mips::SaveRaF16)).addImm(StackSize); // cleanup
133 else
134 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), SP).addReg(SP).addImm(-StackSize);
135 }
Akira Hatanakaf93b8632012-03-28 00:22:50 +0000136 else { // Expand immediate that doesn't fit in 16-bit.
Akira Hatanaka84e09282012-06-14 01:17:13 +0000137 unsigned ATReg = STI.isABI_N64() ? Mips::AT_64 : Mips::AT;
138
139 MF.getInfo<MipsFunctionInfo>()->setEmitNOAT();
140 Mips::loadImmediate(-StackSize, STI.isABI_N64(), TII, MBB, MBBI, dl, false,
141 0);
142 BuildMI(MBB, MBBI, dl, TII.get(ADDu), SP).addReg(SP).addReg(ATReg);
Akira Hatanakaf93b8632012-03-28 00:22:50 +0000143 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000144
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000145 // emit ".cfi_def_cfa_offset StackSize"
146 MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol();
147 BuildMI(MBB, MBBI, dl,
148 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel);
149 DstML = MachineLocation(MachineLocation::VirtualFP);
150 SrcML = MachineLocation(MachineLocation::VirtualFP, -StackSize);
151 Moves.push_back(MachineMove(AdjustSPLabel, DstML, SrcML));
152
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000153 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000154
155 if (CSI.size()) {
Akira Hatanaka0f843822011-06-07 18:58:42 +0000156 // Find the instruction past the last instruction that saves a callee-saved
157 // register to the stack.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000158 for (unsigned i = 0; i < CSI.size(); ++i)
159 ++MBBI;
Jia Liubb481f82012-02-28 07:46:26 +0000160
Akira Hatanaka0f843822011-06-07 18:58:42 +0000161 // Iterate over list of callee-saved registers and emit .cfi_offset
162 // directives.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000163 MCSymbol *CSLabel = MMI.getContext().CreateTempSymbol();
164 BuildMI(MBB, MBBI, dl,
165 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(CSLabel);
Jia Liubb481f82012-02-28 07:46:26 +0000166
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000167 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
168 E = CSI.end(); I != E; ++I) {
169 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
170 unsigned Reg = I->getReg();
171
172 // If Reg is a double precision register, emit two cfa_offsets,
173 // one for each of the paired single precision registers.
Craig Topper420761a2012-04-20 07:30:17 +0000174 if (Mips::AFGR64RegClass.contains(Reg)) {
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000175 MachineLocation DstML0(MachineLocation::VirtualFP, Offset);
176 MachineLocation DstML1(MachineLocation::VirtualFP, Offset + 4);
Jakob Stoklund Olesen6c823822012-05-30 18:40:49 +0000177 MachineLocation SrcML0(RegInfo->getSubReg(Reg, Mips::sub_fpeven));
178 MachineLocation SrcML1(RegInfo->getSubReg(Reg, Mips::sub_fpodd));
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000179
180 if (!STI.isLittle())
181 std::swap(SrcML0, SrcML1);
182
183 Moves.push_back(MachineMove(CSLabel, DstML0, SrcML0));
184 Moves.push_back(MachineMove(CSLabel, DstML1, SrcML1));
Craig Topper420761a2012-04-20 07:30:17 +0000185 } else {
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000186 // Reg is either in CPURegs or FGR32.
187 DstML = MachineLocation(MachineLocation::VirtualFP, Offset);
188 SrcML = MachineLocation(Reg);
189 Moves.push_back(MachineMove(CSLabel, DstML, SrcML));
190 }
191 }
Jia Liubb481f82012-02-28 07:46:26 +0000192 }
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000193
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000194 // if framepointer enabled, set it to point to the stack pointer.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000195 if (hasFP(MF)) {
Jia Liubb481f82012-02-28 07:46:26 +0000196 // Insert instruction "move $fp, $sp" at this location.
Akira Hatanaka1b719502011-11-15 18:53:55 +0000197 BuildMI(MBB, MBBI, dl, TII.get(ADDu), FP).addReg(SP).addReg(ZERO);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000198
Jia Liubb481f82012-02-28 07:46:26 +0000199 // emit ".cfi_def_cfa_register $fp"
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000200 MCSymbol *SetFPLabel = MMI.getContext().CreateTempSymbol();
201 BuildMI(MBB, MBBI, dl,
202 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(SetFPLabel);
Akira Hatanaka1b719502011-11-15 18:53:55 +0000203 DstML = MachineLocation(FP);
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000204 SrcML = MachineLocation(MachineLocation::VirtualFP);
205 Moves.push_back(MachineMove(SetFPLabel, DstML, SrcML));
206 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000207}
208
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000209void MipsFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000210 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000211 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000212 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000213 const MipsInstrInfo &TII =
214 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
215 DebugLoc dl = MBBI->getDebugLoc();
Akira Hatanaka1b719502011-11-15 18:53:55 +0000216 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
217 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
218 unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
219 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
220 unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000221
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000222 // if framepointer enabled, restore the stack pointer.
Akira Hatanakaf346c692011-05-21 02:29:26 +0000223 if (hasFP(MF)) {
224 // Find the first instruction that restores a callee-saved register.
225 MachineBasicBlock::iterator I = MBBI;
Jia Liubb481f82012-02-28 07:46:26 +0000226
Akira Hatanakaf346c692011-05-21 02:29:26 +0000227 for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i)
228 --I;
229
230 // Insert instruction "move $sp, $fp" at this location.
Akira Hatanaka1b719502011-11-15 18:53:55 +0000231 BuildMI(MBB, I, dl, TII.get(ADDu), SP).addReg(FP).addReg(ZERO);
Akira Hatanakaf346c692011-05-21 02:29:26 +0000232 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000233
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000234 // Get the number of bytes from FrameInfo
235 uint64_t StackSize = MFI->getStackSize();
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000236
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000237 if (!StackSize)
238 return;
239
240 // Adjust stack.
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000241 if (isInt<16>(StackSize)) { // addi sp, sp, (-stacksize)
242 if (STI.inMips16Mode())
243 // assumes stacksize multiple of 8
244 BuildMI(MBB, MBBI, dl,
245 TII.get(Mips::RestoreRaF16)).addImm(StackSize);
246 else
247 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), SP).addReg(SP).addImm(StackSize);
248 }
Akira Hatanaka84e09282012-06-14 01:17:13 +0000249 else { // Expand immediate that doesn't fit in 16-bit.
250 unsigned ATReg = STI.isABI_N64() ? Mips::AT_64 : Mips::AT;
251
252 MF.getInfo<MipsFunctionInfo>()->setEmitNOAT();
253 Mips::loadImmediate(StackSize, STI.isABI_N64(), TII, MBB, MBBI, dl, false,
254 0);
255 BuildMI(MBB, MBBI, dl, TII.get(ADDu), SP).addReg(SP).addReg(ATReg);
256 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000257}
Bruno Cardoso Lopesfb67faa2011-01-18 19:50:18 +0000258
259void MipsFrameLowering::
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000260processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
261 RegScavenger *RS) const {
Akira Hatanaka864f6602012-06-14 21:10:56 +0000262 MachineRegisterInfo &MRI = MF.getRegInfo();
Akira Hatanaka1b719502011-11-15 18:53:55 +0000263 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000264
265 // FIXME: remove this code if register allocator can correctly mark
266 // $fp and $ra used or unused.
267
268 // Mark $fp and $ra as used or unused.
269 if (hasFP(MF))
Akira Hatanaka1b719502011-11-15 18:53:55 +0000270 MRI.setPhysRegUsed(FP);
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000271}
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000272
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000273bool MipsFrameLowering::
274spillCalleeSavedRegisters(MachineBasicBlock &MBB,
275 MachineBasicBlock::iterator MI,
276 const std::vector<CalleeSavedInfo> &CSI,
277 const TargetRegisterInfo *TRI) const {
278 MachineFunction *MF = MBB.getParent();
279 MachineBasicBlock *EntryBlock = MF->begin();
280 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
281
282 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
Akira Hatanakaba584fe2012-07-11 00:53:32 +0000283 // Add the callee-saved register as live-in. Do not add if the register is
284 // RA and return address is taken, because it has already been added in
285 // method MipsTargetLowering::LowerRETURNADDR.
286 // It's killed at the spill, unless the register is RA and return address
287 // is taken.
288 unsigned Reg = CSI[i].getReg();
289 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA || Reg == Mips::RA_64)
290 && MF->getFrameInfo()->isReturnAddressTaken();
291 if (!IsRAAndRetAddrIsTaken)
292 EntryBlock->addLiveIn(Reg);
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000293
294 // Insert the spill to the stack frame.
Akira Hatanakaba584fe2012-07-11 00:53:32 +0000295 bool IsKill = !IsRAAndRetAddrIsTaken;
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000296 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
Akira Hatanakaba584fe2012-07-11 00:53:32 +0000297 TII.storeRegToStackSlot(*EntryBlock, MI, Reg, IsKill,
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000298 CSI[i].getFrameIdx(), RC, TRI);
Akira Hatanaka4bd73ca2012-01-25 04:19:22 +0000299 }
Akira Hatanaka182ef6f2012-07-10 00:19:06 +0000300
301 return true;
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000302}