blob: 68adfe6b8a52da43181beec419a07419baa50e19 [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
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000014#include "MipsFrameLowering.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000015#include "MipsInstrInfo.h"
16#include "MipsMachineFunction.h"
17#include "llvm/Function.h"
18#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"
23#include "llvm/Target/TargetData.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/Support/CommandLine.h"
26
27using namespace llvm;
28
29
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000030//===----------------------------------------------------------------------===//
Anton Korobeynikov33464912010-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 Hatanaka4552c9a2011-04-15 21:51:11 +000080//===----------------------------------------------------------------------===//
Anton Korobeynikov33464912010-11-15 00:06:54 +000081
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000082// hasFP - Return true if the specified function should have a dedicated frame
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000083// pointer register. This is true if the function has variable sized allocas or
84// if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000085bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000086 const MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanaka2e591472011-06-02 00:24:44 +000087 return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects()
88 || MFI->isFrameAddressTaken();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000089}
90
Akira Hatanaka69c19f72011-05-23 20:16:59 +000091bool MipsFrameLowering::targetHandlesStackFrameRounding() const {
92 return true;
Anton Korobeynikov33464912010-11-15 00:06:54 +000093}
94
Akira Hatanaka69c19f72011-05-23 20:16:59 +000095static unsigned AlignOffset(unsigned Offset, unsigned Align) {
96 return (Offset + Align - 1) / Align * Align;
97}
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000098
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +000099// expand pair of register and immediate if the immediate doesn't fit in the
100// 16-bit offset field.
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000101// e.g.
102// if OrigImm = 0x10000, OrigReg = $sp:
103// generate the following sequence of instrs:
104// lui $at, hi(0x10000)
105// addu $at, $sp, $at
106//
107// (NewReg, NewImm) = ($at, lo(Ox10000))
108// return true
109static bool expandRegLargeImmPair(unsigned OrigReg, int OrigImm,
110 unsigned& NewReg, int& NewImm,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000111 MachineBasicBlock& MBB,
112 MachineBasicBlock::iterator I) {
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000113 // OrigImm fits in the 16-bit field
114 if (OrigImm < 0x8000 && OrigImm >= -0x8000) {
115 NewReg = OrigReg;
116 NewImm = OrigImm;
117 return false;
118 }
119
120 MachineFunction* MF = MBB.getParent();
121 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
122 DebugLoc DL = I->getDebugLoc();
Akira Hatanakace98deb2011-05-24 21:22:21 +0000123 int ImmLo = (short)(OrigImm & 0xffff);
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000124 int ImmHi = (((unsigned)OrigImm & 0xffff0000) >> 16) +
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000125 ((OrigImm & 0x8000) != 0);
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000126
127 // FIXME: change this when mips goes MC".
128 BuildMI(MBB, I, DL, TII->get(Mips::NOAT));
129 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::AT).addImm(ImmHi);
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000130 BuildMI(MBB, I, DL, TII->get(Mips::ADDu), Mips::AT).addReg(OrigReg)
131 .addReg(Mips::AT);
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000132 NewReg = Mips::AT;
133 NewImm = ImmLo;
134
135 return true;
136}
137
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000138void MipsFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000139 MachineBasicBlock &MBB = MF.front();
140 MachineFrameInfo *MFI = MF.getFrameInfo();
141 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
142 const MipsRegisterInfo *RegInfo =
143 static_cast<const MipsRegisterInfo*>(MF.getTarget().getRegisterInfo());
144 const MipsInstrInfo &TII =
145 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
146 MachineBasicBlock::iterator MBBI = MBB.begin();
147 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
148 bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000149 unsigned NewReg = 0;
150 int NewImm = 0;
151 bool ATUsed;
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000152 unsigned GP = STI.isABI_N64() ? Mips::GP_64 : Mips::GP;
153 unsigned T9 = STI.isABI_N64() ? Mips::T9_64 : Mips::T9;
154 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
155 unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
156 unsigned LUi = STI.isABI_N64() ? Mips::LUi64 : Mips::LUi;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000157
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000158 // First, compute final stack size.
159 unsigned RegSize = STI.isGP32bit() ? 4 : 8;
160 unsigned StackAlign = getStackAlignment();
161 unsigned LocalVarAreaOffset = MipsFI->needGPSaveRestore() ?
162 (MFI->getObjectOffset(MipsFI->getGPFI()) + RegSize) :
Akira Hatanakaf15f4982011-05-25 17:52:48 +0000163 MipsFI->getMaxCallFrameSize();
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000164 unsigned StackSize = AlignOffset(LocalVarAreaOffset, StackAlign) +
Akira Hatanakaf83ba322011-11-07 19:07:35 +0000165 AlignOffset(MipsFI->getRegSaveAreaSize(), StackAlign) +
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000166 AlignOffset(MFI->getStackSize(), StackAlign);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000167
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000168 // Update stack size
169 MFI->setStackSize(StackSize);
170
Anton Korobeynikov33464912010-11-15 00:06:54 +0000171 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOREORDER));
172
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000173 // Emit instructions that set $gp using the the value of $t9.
174 // O32 uses the directive .cpload while N32/64 requires three instructions to
175 // do this.
176 // TODO: Do not emit these instructions if no instructions use $gp.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000177 if (isPIC && STI.isABI_O32())
178 BuildMI(MBB, MBBI, dl, TII.get(Mips::CPLOAD))
179 .addReg(RegInfo->getPICCallReg());
Akira Hatanakaa1fa08f2011-11-11 04:00:29 +0000180 else if (STI.isABI_N64() || (isPIC && STI.isABI_N32())) {
181 // lui $28,%hi(%neg(%gp_rel(fname)))
182 // addu $28,$28,$25
183 // addiu $28,$28,%lo(%neg(%gp_rel(fname)))
184 const GlobalValue *FName = MF.getFunction();
185 BuildMI(MBB, MBBI, dl, TII.get(LUi), GP)
186 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
187 BuildMI(MBB, MBBI, dl, TII.get(ADDu), GP).addReg(GP).addReg(T9);
188 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), GP).addReg(GP)
189 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
190 }
191
Anton Korobeynikov33464912010-11-15 00:06:54 +0000192 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOMACRO));
193
Akira Hatanakaf346c692011-05-21 02:29:26 +0000194 // No need to allocate space on the stack.
195 if (StackSize == 0 && !MFI->adjustsStack()) return;
196
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000197 MachineModuleInfo &MMI = MF.getMMI();
198 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
199 MachineLocation DstML, SrcML;
200
Anton Korobeynikov33464912010-11-15 00:06:54 +0000201 // Adjust stack : addi sp, sp, (-imm)
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000202 ATUsed = expandRegLargeImmPair(Mips::SP, -StackSize, NewReg, NewImm, MBB,
Akira Hatanaka8d580652011-04-07 20:25:10 +0000203 MBBI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000204 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDiu), Mips::SP)
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000205 .addReg(NewReg).addImm(NewImm);
206
207 // FIXME: change this when mips goes MC".
208 if (ATUsed)
209 BuildMI(MBB, MBBI, dl, TII.get(Mips::ATMACRO));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000210
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000211 // emit ".cfi_def_cfa_offset StackSize"
212 MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol();
213 BuildMI(MBB, MBBI, dl,
214 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel);
215 DstML = MachineLocation(MachineLocation::VirtualFP);
216 SrcML = MachineLocation(MachineLocation::VirtualFP, -StackSize);
217 Moves.push_back(MachineMove(AdjustSPLabel, DstML, SrcML));
218
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000219 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000220
221 if (CSI.size()) {
Akira Hatanaka0f843822011-06-07 18:58:42 +0000222 // Find the instruction past the last instruction that saves a callee-saved
223 // register to the stack.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000224 for (unsigned i = 0; i < CSI.size(); ++i)
225 ++MBBI;
Akira Hatanakaf346c692011-05-21 02:29:26 +0000226
Akira Hatanaka0f843822011-06-07 18:58:42 +0000227 // Iterate over list of callee-saved registers and emit .cfi_offset
228 // directives.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000229 MCSymbol *CSLabel = MMI.getContext().CreateTempSymbol();
230 BuildMI(MBB, MBBI, dl,
231 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(CSLabel);
232
233 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
234 E = CSI.end(); I != E; ++I) {
235 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
236 unsigned Reg = I->getReg();
237
238 // If Reg is a double precision register, emit two cfa_offsets,
239 // one for each of the paired single precision registers.
240 if (Mips::AFGR64RegisterClass->contains(Reg)) {
241 const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
242 MachineLocation DstML0(MachineLocation::VirtualFP, Offset);
243 MachineLocation DstML1(MachineLocation::VirtualFP, Offset + 4);
244 MachineLocation SrcML0(*SubRegs);
245 MachineLocation SrcML1(*(SubRegs + 1));
246
247 if (!STI.isLittle())
248 std::swap(SrcML0, SrcML1);
249
250 Moves.push_back(MachineMove(CSLabel, DstML0, SrcML0));
251 Moves.push_back(MachineMove(CSLabel, DstML1, SrcML1));
252 }
253 else {
254 // Reg is either in CPURegs or FGR32.
255 DstML = MachineLocation(MachineLocation::VirtualFP, Offset);
256 SrcML = MachineLocation(Reg);
257 Moves.push_back(MachineMove(CSLabel, DstML, SrcML));
258 }
259 }
260 }
261
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000262 // if framepointer enabled, set it to point to the stack pointer.
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000263 if (hasFP(MF)) {
Akira Hatanakaf346c692011-05-21 02:29:26 +0000264 // Insert instruction "move $fp, $sp" at this location.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000265 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDu), Mips::FP)
266 .addReg(Mips::SP).addReg(Mips::ZERO);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000267
Akira Hatanaka8464fff2011-06-07 02:17:21 +0000268 // emit ".cfi_def_cfa_register $fp"
269 MCSymbol *SetFPLabel = MMI.getContext().CreateTempSymbol();
270 BuildMI(MBB, MBBI, dl,
271 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(SetFPLabel);
272 DstML = MachineLocation(Mips::FP);
273 SrcML = MachineLocation(MachineLocation::VirtualFP);
274 Moves.push_back(MachineMove(SetFPLabel, DstML, SrcML));
275 }
276
Anton Korobeynikov33464912010-11-15 00:06:54 +0000277 // Restore GP from the saved stack location
Akira Hatanaka9029cf22011-08-11 22:42:31 +0000278 if (MipsFI->needGPSaveRestore()) {
279 unsigned Offset = MFI->getObjectOffset(MipsFI->getGPFI());
280 BuildMI(MBB, MBBI, dl, TII.get(Mips::CPRESTORE)).addImm(Offset);
281
282 if (Offset >= 0x8000) {
283 BuildMI(MBB, llvm::prior(MBBI), dl, TII.get(Mips::MACRO));
284 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOMACRO));
285 }
286 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000287}
288
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000289void MipsFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000290 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000291 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000292 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000293 const MipsInstrInfo &TII =
294 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
295 DebugLoc dl = MBBI->getDebugLoc();
296
297 // Get the number of bytes from FrameInfo
Akira Hatanakaf346c692011-05-21 02:29:26 +0000298 unsigned StackSize = MFI->getStackSize();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000299
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000300 unsigned NewReg = 0;
301 int NewImm = 0;
Bill Wendling0546f732011-03-04 21:38:47 +0000302 bool ATUsed = false;
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000303
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000304 // if framepointer enabled, restore the stack pointer.
Akira Hatanakaf346c692011-05-21 02:29:26 +0000305 if (hasFP(MF)) {
306 // Find the first instruction that restores a callee-saved register.
307 MachineBasicBlock::iterator I = MBBI;
308
309 for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i)
310 --I;
311
312 // Insert instruction "move $sp, $fp" at this location.
313 BuildMI(MBB, I, dl, TII.get(Mips::ADDu), Mips::SP)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000314 .addReg(Mips::FP).addReg(Mips::ZERO);
Akira Hatanakaf346c692011-05-21 02:29:26 +0000315 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000316
Anton Korobeynikov33464912010-11-15 00:06:54 +0000317 // adjust stack : insert addi sp, sp, (imm)
Akira Hatanakaf346c692011-05-21 02:29:26 +0000318 if (StackSize) {
319 ATUsed = expandRegLargeImmPair(Mips::SP, StackSize, NewReg, NewImm, MBB,
Akira Hatanaka3f92b432011-04-07 20:23:26 +0000320 MBBI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000321 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDiu), Mips::SP)
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000322 .addReg(NewReg).addImm(NewImm);
323
324 // FIXME: change this when mips goes MC".
325 if (ATUsed)
326 BuildMI(MBB, MBBI, dl, TII.get(Mips::ATMACRO));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000327 }
328}
Bruno Cardoso Lopesfb67faa2011-01-18 19:50:18 +0000329
330void MipsFrameLowering::
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000331processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
332 RegScavenger *RS) const {
333 MachineRegisterInfo& MRI = MF.getRegInfo();
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000334
335 // FIXME: remove this code if register allocator can correctly mark
336 // $fp and $ra used or unused.
337
338 // Mark $fp and $ra as used or unused.
339 if (hasFP(MF))
340 MRI.setPhysRegUsed(Mips::FP);
341
342 // The register allocator might determine $ra is used after seeing
343 // instruction "jr $ra", but we do not want PrologEpilogInserter to insert
344 // instructions to save/restore $ra unless there is a function call.
345 // To correct this, $ra is explicitly marked unused if there is no
346 // function call.
Akira Hatanaka33458fe2011-05-26 20:30:31 +0000347 if (MF.getFrameInfo()->hasCalls())
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000348 MRI.setPhysRegUsed(Mips::RA);
349 else
350 MRI.setPhysRegUnused(Mips::RA);
351}