blob: dda20c5c214a0bbd1f074d53910efda5e4ed839c [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//=======- MipsFrameLowering.cpp - Mips Frame Information ------*- C++ -*-====//
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
Akira Hatanakade5a0b62012-01-25 04:12:04 +000014#include "MipsAnalyzeImmediate.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000015#include "MipsFrameLowering.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;
104 unsigned ZEROReg = IsN64 ? Mips::ZERO_64 : Mips::ZERO;
105 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
111 // FIXME: change this when mips goes MC".
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000112 BuildMI(MBB, II, DL, TII.get(Mips::NOAT));
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000113
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000114 // The first instruction can be a LUi, which is different from other
115 // instructions (ADDiu, ORI and SLL) in that it does not have a register
116 // operand.
117 if (Inst->Opc == LUi)
118 BuildMI(MBB, II, DL, TII.get(LUi), ATReg)
119 .addImm(SignExtend64<16>(Inst->ImmOpnd));
120 else
121 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ZEROReg)
122 .addImm(SignExtend64<16>(Inst->ImmOpnd));
123
124 // Build the remaining instructions in Seq.
125 for (++Inst; Inst != Seq.end(); ++Inst)
126 BuildMI(MBB, II, DL, TII.get(Inst->Opc), ATReg).addReg(ATReg)
127 .addImm(SignExtend64<16>(Inst->ImmOpnd));
128
129 BuildMI(MBB, II, DL, TII.get(ADDu), Reg).addReg(Reg).addReg(ATReg);
130 BuildMI(MBB, II, DL, TII.get(Mips::ATMACRO));
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000131}
132
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000133void MipsFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000134 MachineBasicBlock &MBB = MF.front();
135 MachineFrameInfo *MFI = MF.getFrameInfo();
136 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
137 const MipsRegisterInfo *RegInfo =
138 static_cast<const MipsRegisterInfo*>(MF.getTarget().getRegisterInfo());
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000139 MachineRegisterInfo& MRI = MF.getRegInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000140 const MipsInstrInfo &TII =
141 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
142 MachineBasicBlock::iterator MBBI = MBB.begin();
143 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
144 bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000145 unsigned GP = STI.isABI_N64() ? Mips::GP_64 : Mips::GP;
146 unsigned T9 = STI.isABI_N64() ? Mips::T9_64 : Mips::T9;
Akira Hatanaka1b719502011-11-15 18:53:55 +0000147 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
148 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
149 unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000150 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
151 unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
152 unsigned LUi = STI.isABI_N64() ? Mips::LUi64 : Mips::LUi;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000153
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000154 // First, compute final stack size.
155 unsigned RegSize = STI.isGP32bit() ? 4 : 8;
156 unsigned StackAlign = getStackAlignment();
157 unsigned LocalVarAreaOffset = MipsFI->needGPSaveRestore() ?
158 (MFI->getObjectOffset(MipsFI->getGPFI()) + RegSize) :
Akira Hatanakaf15f4982011-05-25 17:52:48 +0000159 MipsFI->getMaxCallFrameSize();
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000160 uint64_t StackSize = RoundUpToAlignment(LocalVarAreaOffset, StackAlign) +
161 RoundUpToAlignment(MFI->getStackSize(), StackAlign);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000162
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000163 // Update stack size
164 MFI->setStackSize(StackSize);
165
Anton Korobeynikov33464912010-11-15 00:06:54 +0000166 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOREORDER));
167
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000168 // Emit instructions that set $gp using the the value of $t9.
169 // O32 uses the directive .cpload while N32/64 requires three instructions to
170 // do this.
171 // TODO: Do not emit these instructions if no instructions use $gp.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000172 if (isPIC && STI.isABI_O32())
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000173 BuildMI(MBB, MBBI, dl, TII.get(Mips::CPLOAD))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000174 .addReg(RegInfo->getPICCallReg());
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000175
176 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOMACRO));
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000177
Akira Hatanakaf346c692011-05-21 02:29:26 +0000178 // No need to allocate space on the stack.
179 if (StackSize == 0 && !MFI->adjustsStack()) return;
180
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000181 MachineModuleInfo &MMI = MF.getMMI();
182 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
183 MachineLocation DstML, SrcML;
184
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000185 // Adjust stack.
186 if (isInt<16>(-StackSize)) // addi sp, sp, (-stacksize)
187 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), SP).addReg(SP).addImm(-StackSize);
188 else // Expand immediate that doesn't fit in 16-bit.
189 expandLargeImm(SP, -StackSize, STI.isABI_N64(), TII, MBB, MBBI, dl);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000190
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000191 // emit ".cfi_def_cfa_offset StackSize"
192 MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol();
193 BuildMI(MBB, MBBI, dl,
194 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel);
195 DstML = MachineLocation(MachineLocation::VirtualFP);
196 SrcML = MachineLocation(MachineLocation::VirtualFP, -StackSize);
197 Moves.push_back(MachineMove(AdjustSPLabel, DstML, SrcML));
198
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000199 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000200
201 if (CSI.size()) {
Akira Hatanaka0f843822011-06-07 18:58:42 +0000202 // Find the instruction past the last instruction that saves a callee-saved
203 // register to the stack.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000204 for (unsigned i = 0; i < CSI.size(); ++i)
205 ++MBBI;
Akira Hatanakaf346c692011-05-21 02:29:26 +0000206
Akira Hatanaka0f843822011-06-07 18:58:42 +0000207 // Iterate over list of callee-saved registers and emit .cfi_offset
208 // directives.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000209 MCSymbol *CSLabel = MMI.getContext().CreateTempSymbol();
210 BuildMI(MBB, MBBI, dl,
211 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(CSLabel);
212
213 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
214 E = CSI.end(); I != E; ++I) {
215 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
216 unsigned Reg = I->getReg();
217
218 // If Reg is a double precision register, emit two cfa_offsets,
219 // one for each of the paired single precision registers.
220 if (Mips::AFGR64RegisterClass->contains(Reg)) {
221 const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
222 MachineLocation DstML0(MachineLocation::VirtualFP, Offset);
223 MachineLocation DstML1(MachineLocation::VirtualFP, Offset + 4);
224 MachineLocation SrcML0(*SubRegs);
225 MachineLocation SrcML1(*(SubRegs + 1));
226
227 if (!STI.isLittle())
228 std::swap(SrcML0, SrcML1);
229
230 Moves.push_back(MachineMove(CSLabel, DstML0, SrcML0));
231 Moves.push_back(MachineMove(CSLabel, DstML1, SrcML1));
232 }
233 else {
234 // Reg is either in CPURegs or FGR32.
235 DstML = MachineLocation(MachineLocation::VirtualFP, Offset);
236 SrcML = MachineLocation(Reg);
237 Moves.push_back(MachineMove(CSLabel, DstML, SrcML));
238 }
239 }
240 }
241
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000242 if ((STI.isABI_N64() || (isPIC && STI.isABI_N32())) &&
243 MRI.isPhysRegUsed(GP)) {
244 // lui $28,%hi(%neg(%gp_rel(fname)))
245 // addu $28,$28,$25
246 // addiu $28,$28,%lo(%neg(%gp_rel(fname)))
247 MachineBasicBlock::iterator InsPos = llvm::prior(MBBI);
248 const GlobalValue *FName = MF.getFunction();
249 BuildMI(MBB, MBBI, dl, TII.get(LUi), GP)
250 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
251 BuildMI(MBB, MBBI, dl, TII.get(ADDu), GP).addReg(GP).addReg(T9);
252 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), GP).addReg(GP)
253 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
254 MBBI = ++InsPos;
255 }
256
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000257 // if framepointer enabled, set it to point to the stack pointer.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000258 if (hasFP(MF)) {
Akira Hatanakaf346c692011-05-21 02:29:26 +0000259 // Insert instruction "move $fp, $sp" at this location.
Akira Hatanaka1b719502011-11-15 18:53:55 +0000260 BuildMI(MBB, MBBI, dl, TII.get(ADDu), FP).addReg(SP).addReg(ZERO);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000261
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000262 // emit ".cfi_def_cfa_register $fp"
263 MCSymbol *SetFPLabel = MMI.getContext().CreateTempSymbol();
264 BuildMI(MBB, MBBI, dl,
265 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(SetFPLabel);
Akira Hatanaka1b719502011-11-15 18:53:55 +0000266 DstML = MachineLocation(FP);
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000267 SrcML = MachineLocation(MachineLocation::VirtualFP);
268 Moves.push_back(MachineMove(SetFPLabel, DstML, SrcML));
269 }
270
Anton Korobeynikov33464912010-11-15 00:06:54 +0000271 // Restore GP from the saved stack location
Akira Hatanaka9029cf22011-08-11 22:42:31 +0000272 if (MipsFI->needGPSaveRestore()) {
273 unsigned Offset = MFI->getObjectOffset(MipsFI->getGPFI());
274 BuildMI(MBB, MBBI, dl, TII.get(Mips::CPRESTORE)).addImm(Offset);
275
276 if (Offset >= 0x8000) {
277 BuildMI(MBB, llvm::prior(MBBI), dl, TII.get(Mips::MACRO));
278 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOMACRO));
279 }
280 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000281}
282
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000283void MipsFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000284 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000285 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000286 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000287 const MipsInstrInfo &TII =
288 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
289 DebugLoc dl = MBBI->getDebugLoc();
Akira Hatanaka1b719502011-11-15 18:53:55 +0000290 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
291 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
292 unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
293 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
294 unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000295
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000296 // if framepointer enabled, restore the stack pointer.
Akira Hatanakaf346c692011-05-21 02:29:26 +0000297 if (hasFP(MF)) {
298 // Find the first instruction that restores a callee-saved register.
299 MachineBasicBlock::iterator I = MBBI;
300
301 for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i)
302 --I;
303
304 // Insert instruction "move $sp, $fp" at this location.
Akira Hatanaka1b719502011-11-15 18:53:55 +0000305 BuildMI(MBB, I, dl, TII.get(ADDu), SP).addReg(FP).addReg(ZERO);
Akira Hatanakaf346c692011-05-21 02:29:26 +0000306 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000307
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000308 // Get the number of bytes from FrameInfo
309 uint64_t StackSize = MFI->getStackSize();
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000310
Akira Hatanakade5a0b62012-01-25 04:12:04 +0000311 if (!StackSize)
312 return;
313
314 // Adjust stack.
315 if (isInt<16>(StackSize)) // addi sp, sp, (-stacksize)
316 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), SP).addReg(SP).addImm(StackSize);
317 else // Expand immediate that doesn't fit in 16-bit.
318 expandLargeImm(SP, StackSize, STI.isABI_N64(), TII, MBB, MBBI, dl);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000319}
Bruno Cardoso Lopesfb67faa2011-01-18 19:50:18 +0000320
321void MipsFrameLowering::
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000322processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
323 RegScavenger *RS) const {
324 MachineRegisterInfo& MRI = MF.getRegInfo();
Akira Hatanaka1b719502011-11-15 18:53:55 +0000325 unsigned RA = STI.isABI_N64() ? Mips::RA_64 : Mips::RA;
326 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000327
328 // FIXME: remove this code if register allocator can correctly mark
329 // $fp and $ra used or unused.
330
331 // Mark $fp and $ra as used or unused.
332 if (hasFP(MF))
Akira Hatanaka1b719502011-11-15 18:53:55 +0000333 MRI.setPhysRegUsed(FP);
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000334
335 // The register allocator might determine $ra is used after seeing
336 // instruction "jr $ra", but we do not want PrologEpilogInserter to insert
337 // instructions to save/restore $ra unless there is a function call.
338 // To correct this, $ra is explicitly marked unused if there is no
339 // function call.
Akira Hatanaka33458fe2011-05-26 20:30:31 +0000340 if (MF.getFrameInfo()->hasCalls())
Akira Hatanaka1b719502011-11-15 18:53:55 +0000341 MRI.setPhysRegUsed(RA);
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000342 else
Akira Hatanaka1b719502011-11-15 18:53:55 +0000343 MRI.setPhysRegUnused(RA);
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000344}