blob: 98c91b1c9f883f5ed441bfe2ea8d8c76e1afe9c6 [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
Akira Hatanakade5a0b62012-01-25 04:12:04 +000097// Build an instruction sequence to load an immediate that is too large to fit
98// in 16-bit and add the result to Reg.
99static void expandLargeImm(unsigned Reg, int64_t Imm, bool IsN64,
100 const MipsInstrInfo &TII, MachineBasicBlock& MBB,
101 MachineBasicBlock::iterator II, DebugLoc DL) {
102 unsigned LUi = IsN64 ? Mips::LUi64 : Mips::LUi;
103 unsigned ADDu = IsN64 ? Mips::DADDu : Mips::ADDu;
Jia Liubb481f82012-02-28 07:46:26 +0000104 unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO;
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000105 unsigned ATReg = IsN64 ? Mips::AT_64 : Mips::AT;
106 MipsAnalyzeImmediate AnalyzeImm;
107 const MipsAnalyzeImmediate::InstSeq &Seq =
108 AnalyzeImm.Analyze(Imm, IsN64 ? 64 : 32, false /* LastInstrIsADDiu */);
109 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000110
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000111 // The first instruction can be a LUi, which is different from other
112 // instructions (ADDiu, ORI and SLL) in that it does not have a register
113 // operand.
114 if (Inst->Opc == LUi)
115 BuildMI(MBB, II, DL, TII.get(LUi), ATReg)
116 .addImm(SignExtend64<16>(Inst->ImmOpnd));
117 else
118 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg)
119 .addImm(SignExtend64<16>(Inst->ImmOpnd));
120
121 // Build the remaining instructions in Seq.
122 for (++Inst; Inst != Seq.end(); ++Inst)
123 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg)
124 .addImm(SignExtend64<16>(Inst->ImmOpnd));
Jia Liubb481f82012-02-28 07:46:26 +0000125
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000126 BuildMI(MBB, II, DL, TII.get(ADDu), Reg).addReg(Reg).addReg(ATReg);
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000127}
128
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000129void MipsFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000130 MachineBasicBlock &MBB = MF.front();
131 MachineFrameInfo *MFI = MF.getFrameInfo();
132 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
133 const MipsRegisterInfo *RegInfo =
134 static_cast<const MipsRegisterInfo*>(MF.getTarget().getRegisterInfo());
135 const MipsInstrInfo &TII =
136 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
137 MachineBasicBlock::iterator MBBI = MBB.begin();
138 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
Akira Hatanaka1b719502011-11-15 18:53:55 +0000139 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
140 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
141 unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000142 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
143 unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000144
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000145 // First, compute final stack size.
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000146 unsigned StackAlign = getStackAlignment();
Akira Hatanaka30cec402012-05-12 03:18:00 +0000147 uint64_t StackSize =
148 RoundUpToAlignment(MipsFI->getMaxCallFrameSize(), StackAlign) +
149 RoundUpToAlignment(MFI->getStackSize(), StackAlign);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000150
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000151 // Update stack size
Jia Liubb481f82012-02-28 07:46:26 +0000152 MFI->setStackSize(StackSize);
153
Akira Hatanakaf346c692011-05-21 02:29:26 +0000154 // No need to allocate space on the stack.
155 if (StackSize == 0 && !MFI->adjustsStack()) return;
156
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000157 MachineModuleInfo &MMI = MF.getMMI();
158 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
159 MachineLocation DstML, SrcML;
160
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000161 // Adjust stack.
162 if (isInt<16>(-StackSize)) // addi sp, sp, (-stacksize)
163 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), SP).addReg(SP).addImm(-StackSize);
Akira Hatanakaf93b8632012-03-28 00:22:50 +0000164 else { // Expand immediate that doesn't fit in 16-bit.
165 MipsFI->setEmitNOAT();
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000166 expandLargeImm(SP, -StackSize, STI.isABI_N64(), TII, MBB, MBBI, dl);
Akira Hatanakaf93b8632012-03-28 00:22:50 +0000167 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000168
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000169 // emit ".cfi_def_cfa_offset StackSize"
170 MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol();
171 BuildMI(MBB, MBBI, dl,
172 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel);
173 DstML = MachineLocation(MachineLocation::VirtualFP);
174 SrcML = MachineLocation(MachineLocation::VirtualFP, -StackSize);
175 Moves.push_back(MachineMove(AdjustSPLabel, DstML, SrcML));
176
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000177 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000178
179 if (CSI.size()) {
Akira Hatanaka0f843822011-06-07 18:58:42 +0000180 // Find the instruction past the last instruction that saves a callee-saved
181 // register to the stack.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000182 for (unsigned i = 0; i < CSI.size(); ++i)
183 ++MBBI;
Jia Liubb481f82012-02-28 07:46:26 +0000184
Akira Hatanaka0f843822011-06-07 18:58:42 +0000185 // Iterate over list of callee-saved registers and emit .cfi_offset
186 // directives.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000187 MCSymbol *CSLabel = MMI.getContext().CreateTempSymbol();
188 BuildMI(MBB, MBBI, dl,
189 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(CSLabel);
Jia Liubb481f82012-02-28 07:46:26 +0000190
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000191 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
192 E = CSI.end(); I != E; ++I) {
193 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
194 unsigned Reg = I->getReg();
195
196 // If Reg is a double precision register, emit two cfa_offsets,
197 // one for each of the paired single precision registers.
Craig Topper420761a2012-04-20 07:30:17 +0000198 if (Mips::AFGR64RegClass.contains(Reg)) {
Craig Topper9ebfbf82012-03-05 05:37:41 +0000199 const uint16_t *SubRegs = RegInfo->getSubRegisters(Reg);
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000200 MachineLocation DstML0(MachineLocation::VirtualFP, Offset);
201 MachineLocation DstML1(MachineLocation::VirtualFP, Offset + 4);
202 MachineLocation SrcML0(*SubRegs);
203 MachineLocation SrcML1(*(SubRegs + 1));
204
205 if (!STI.isLittle())
206 std::swap(SrcML0, SrcML1);
207
208 Moves.push_back(MachineMove(CSLabel, DstML0, SrcML0));
209 Moves.push_back(MachineMove(CSLabel, DstML1, SrcML1));
Craig Topper420761a2012-04-20 07:30:17 +0000210 } else {
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000211 // Reg is either in CPURegs or FGR32.
212 DstML = MachineLocation(MachineLocation::VirtualFP, Offset);
213 SrcML = MachineLocation(Reg);
214 Moves.push_back(MachineMove(CSLabel, DstML, SrcML));
215 }
216 }
Jia Liubb481f82012-02-28 07:46:26 +0000217 }
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000218
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000219 // if framepointer enabled, set it to point to the stack pointer.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000220 if (hasFP(MF)) {
Jia Liubb481f82012-02-28 07:46:26 +0000221 // Insert instruction "move $fp, $sp" at this location.
Akira Hatanaka1b719502011-11-15 18:53:55 +0000222 BuildMI(MBB, MBBI, dl, TII.get(ADDu), FP).addReg(SP).addReg(ZERO);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000223
Jia Liubb481f82012-02-28 07:46:26 +0000224 // emit ".cfi_def_cfa_register $fp"
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000225 MCSymbol *SetFPLabel = MMI.getContext().CreateTempSymbol();
226 BuildMI(MBB, MBBI, dl,
227 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(SetFPLabel);
Akira Hatanaka1b719502011-11-15 18:53:55 +0000228 DstML = MachineLocation(FP);
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000229 SrcML = MachineLocation(MachineLocation::VirtualFP);
230 Moves.push_back(MachineMove(SetFPLabel, DstML, SrcML));
231 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000232}
233
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000234void MipsFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000235 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000236 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000237 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000238 const MipsInstrInfo &TII =
239 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
240 DebugLoc dl = MBBI->getDebugLoc();
Akira Hatanaka1b719502011-11-15 18:53:55 +0000241 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
242 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
243 unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
244 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
245 unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000246
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000247 // if framepointer enabled, restore the stack pointer.
Akira Hatanakaf346c692011-05-21 02:29:26 +0000248 if (hasFP(MF)) {
249 // Find the first instruction that restores a callee-saved register.
250 MachineBasicBlock::iterator I = MBBI;
Jia Liubb481f82012-02-28 07:46:26 +0000251
Akira Hatanakaf346c692011-05-21 02:29:26 +0000252 for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i)
253 --I;
254
255 // Insert instruction "move $sp, $fp" at this location.
Akira Hatanaka1b719502011-11-15 18:53:55 +0000256 BuildMI(MBB, I, dl, TII.get(ADDu), SP).addReg(FP).addReg(ZERO);
Akira Hatanakaf346c692011-05-21 02:29:26 +0000257 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000258
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000259 // Get the number of bytes from FrameInfo
260 uint64_t StackSize = MFI->getStackSize();
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000261
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000262 if (!StackSize)
263 return;
264
265 // Adjust stack.
266 if (isInt<16>(StackSize)) // addi sp, sp, (-stacksize)
267 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), SP).addReg(SP).addImm(StackSize);
268 else // Expand immediate that doesn't fit in 16-bit.
269 expandLargeImm(SP, StackSize, STI.isABI_N64(), TII, MBB, MBBI, dl);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000270}
Bruno Cardoso Lopesfb67faa2011-01-18 19:50:18 +0000271
272void MipsFrameLowering::
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000273processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
274 RegScavenger *RS) const {
275 MachineRegisterInfo& MRI = MF.getRegInfo();
Akira Hatanaka1b719502011-11-15 18:53:55 +0000276 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000277
278 // FIXME: remove this code if register allocator can correctly mark
279 // $fp and $ra used or unused.
280
281 // Mark $fp and $ra as used or unused.
282 if (hasFP(MF))
Akira Hatanaka1b719502011-11-15 18:53:55 +0000283 MRI.setPhysRegUsed(FP);
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000284
Jia Liubb481f82012-02-28 07:46:26 +0000285 // The register allocator might determine $ra is used after seeing
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000286 // instruction "jr $ra", but we do not want PrologEpilogInserter to insert
287 // instructions to save/restore $ra unless there is a function call.
288 // To correct this, $ra is explicitly marked unused if there is no
289 // function call.
Akira Hatanaka33458fe2011-05-26 20:30:31 +0000290 if (MF.getFrameInfo()->hasCalls())
Akira Hatanaka4bd73ca2012-01-25 04:19:22 +0000291 MRI.setPhysRegUsed(Mips::RA);
292 else {
293 MRI.setPhysRegUnused(Mips::RA);
294 MRI.setPhysRegUnused(Mips::RA_64);
295 }
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000296}