blob: 3291853bf891cd82eebea8174fdac7772b5992bf [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();
87 return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
88}
89
Akira Hatanaka69c19f72011-05-23 20:16:59 +000090bool MipsFrameLowering::targetHandlesStackFrameRounding() const {
91 return true;
Anton Korobeynikov33464912010-11-15 00:06:54 +000092}
93
Akira Hatanaka69c19f72011-05-23 20:16:59 +000094static unsigned AlignOffset(unsigned Offset, unsigned Align) {
95 return (Offset + Align - 1) / Align * Align;
96}
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000097
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +000098// expand pair of register and immediate if the immediate doesn't fit in the
99// 16-bit offset field.
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000100// e.g.
101// if OrigImm = 0x10000, OrigReg = $sp:
102// generate the following sequence of instrs:
103// lui $at, hi(0x10000)
104// addu $at, $sp, $at
105//
106// (NewReg, NewImm) = ($at, lo(Ox10000))
107// return true
108static bool expandRegLargeImmPair(unsigned OrigReg, int OrigImm,
109 unsigned& NewReg, int& NewImm,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000110 MachineBasicBlock& MBB,
111 MachineBasicBlock::iterator I) {
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000112 // OrigImm fits in the 16-bit field
113 if (OrigImm < 0x8000 && OrigImm >= -0x8000) {
114 NewReg = OrigReg;
115 NewImm = OrigImm;
116 return false;
117 }
118
119 MachineFunction* MF = MBB.getParent();
120 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
121 DebugLoc DL = I->getDebugLoc();
Akira Hatanakace98deb2011-05-24 21:22:21 +0000122 int ImmLo = (short)(OrigImm & 0xffff);
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000123 int ImmHi = (((unsigned)OrigImm & 0xffff0000) >> 16) +
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000124 ((OrigImm & 0x8000) != 0);
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000125
126 // FIXME: change this when mips goes MC".
127 BuildMI(MBB, I, DL, TII->get(Mips::NOAT));
128 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::AT).addImm(ImmHi);
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000129 BuildMI(MBB, I, DL, TII->get(Mips::ADDu), Mips::AT).addReg(OrigReg)
130 .addReg(Mips::AT);
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000131 NewReg = Mips::AT;
132 NewImm = ImmLo;
133
134 return true;
135}
136
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000137void MipsFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000138 MachineBasicBlock &MBB = MF.front();
139 MachineFrameInfo *MFI = MF.getFrameInfo();
140 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
141 const MipsRegisterInfo *RegInfo =
142 static_cast<const MipsRegisterInfo*>(MF.getTarget().getRegisterInfo());
143 const MipsInstrInfo &TII =
144 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
145 MachineBasicBlock::iterator MBBI = MBB.begin();
146 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
147 bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000148 unsigned NewReg = 0;
149 int NewImm = 0;
150 bool ATUsed;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000151
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000152 // First, compute final stack size.
153 unsigned RegSize = STI.isGP32bit() ? 4 : 8;
154 unsigned StackAlign = getStackAlignment();
155 unsigned LocalVarAreaOffset = MipsFI->needGPSaveRestore() ?
156 (MFI->getObjectOffset(MipsFI->getGPFI()) + RegSize) :
157 MFI->getMaxCallFrameSize();
158 unsigned StackSize = AlignOffset(LocalVarAreaOffset, StackAlign) +
159 AlignOffset(MFI->getStackSize(), StackAlign);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000160
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000161 // Update stack size
162 MFI->setStackSize(StackSize);
163
Anton Korobeynikov33464912010-11-15 00:06:54 +0000164 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOREORDER));
165
166 // TODO: check need from GP here.
167 if (isPIC && STI.isABI_O32())
168 BuildMI(MBB, MBBI, dl, TII.get(Mips::CPLOAD))
169 .addReg(RegInfo->getPICCallReg());
170 BuildMI(MBB, MBBI, dl, TII.get(Mips::NOMACRO));
171
Akira Hatanakaf346c692011-05-21 02:29:26 +0000172 // No need to allocate space on the stack.
173 if (StackSize == 0 && !MFI->adjustsStack()) return;
174
Anton Korobeynikov33464912010-11-15 00:06:54 +0000175 // Adjust stack : addi sp, sp, (-imm)
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000176 ATUsed = expandRegLargeImmPair(Mips::SP, -StackSize, NewReg, NewImm, MBB,
Akira Hatanaka8d580652011-04-07 20:25:10 +0000177 MBBI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000178 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDiu), Mips::SP)
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000179 .addReg(NewReg).addImm(NewImm);
180
181 // FIXME: change this when mips goes MC".
182 if (ATUsed)
183 BuildMI(MBB, MBBI, dl, TII.get(Mips::ATMACRO));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000184
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000185 // if framepointer enabled, set it to point to the stack pointer.
Akira Hatanakaf346c692011-05-21 02:29:26 +0000186 if (hasFP(MF)) {
187 // Find the instruction past the last instruction that saves a callee-saved
188 // register to the stack.
189 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
190
191 for (unsigned i = 0; i < CSI.size(); ++i)
192 ++MBBI;
193
194 // Insert instruction "move $fp, $sp" at this location.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000195 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDu), Mips::FP)
196 .addReg(Mips::SP).addReg(Mips::ZERO);
Akira Hatanakaf346c692011-05-21 02:29:26 +0000197 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000198
199 // Restore GP from the saved stack location
200 if (MipsFI->needGPSaveRestore())
201 BuildMI(MBB, MBBI, dl, TII.get(Mips::CPRESTORE))
Akira Hatanaka69c19f72011-05-23 20:16:59 +0000202 .addImm(MFI->getObjectOffset(MipsFI->getGPFI()));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000203}
204
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000205void MipsFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000206 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000207 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000208 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000209 const MipsInstrInfo &TII =
210 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
211 DebugLoc dl = MBBI->getDebugLoc();
212
213 // Get the number of bytes from FrameInfo
Akira Hatanakaf346c692011-05-21 02:29:26 +0000214 unsigned StackSize = MFI->getStackSize();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000215
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000216 unsigned NewReg = 0;
217 int NewImm = 0;
Bill Wendling0546f732011-03-04 21:38:47 +0000218 bool ATUsed = false;
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000219
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000220 // if framepointer enabled, restore the stack pointer.
Akira Hatanakaf346c692011-05-21 02:29:26 +0000221 if (hasFP(MF)) {
222 // Find the first instruction that restores a callee-saved register.
223 MachineBasicBlock::iterator I = MBBI;
224
225 for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i)
226 --I;
227
228 // Insert instruction "move $sp, $fp" at this location.
229 BuildMI(MBB, I, dl, TII.get(Mips::ADDu), Mips::SP)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000230 .addReg(Mips::FP).addReg(Mips::ZERO);
Akira Hatanakaf346c692011-05-21 02:29:26 +0000231 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000232
Anton Korobeynikov33464912010-11-15 00:06:54 +0000233 // adjust stack : insert addi sp, sp, (imm)
Akira Hatanakaf346c692011-05-21 02:29:26 +0000234 if (StackSize) {
235 ATUsed = expandRegLargeImmPair(Mips::SP, StackSize, NewReg, NewImm, MBB,
Akira Hatanaka3f92b432011-04-07 20:23:26 +0000236 MBBI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000237 BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDiu), Mips::SP)
Bruno Cardoso Lopes99027d72011-03-04 20:48:08 +0000238 .addReg(NewReg).addImm(NewImm);
239
240 // FIXME: change this when mips goes MC".
241 if (ATUsed)
242 BuildMI(MBB, MBBI, dl, TII.get(Mips::ATMACRO));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000243 }
244}
Bruno Cardoso Lopesfb67faa2011-01-18 19:50:18 +0000245
246void MipsFrameLowering::
Akira Hatanaka17a1e872011-05-20 18:39:33 +0000247processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
248 RegScavenger *RS) const {
249 MachineRegisterInfo& MRI = MF.getRegInfo();
250 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
251
252 // FIXME: remove this code if register allocator can correctly mark
253 // $fp and $ra used or unused.
254
255 // Mark $fp and $ra as used or unused.
256 if (hasFP(MF))
257 MRI.setPhysRegUsed(Mips::FP);
258
259 // The register allocator might determine $ra is used after seeing
260 // instruction "jr $ra", but we do not want PrologEpilogInserter to insert
261 // instructions to save/restore $ra unless there is a function call.
262 // To correct this, $ra is explicitly marked unused if there is no
263 // function call.
264 if (MipsFI->hasCall())
265 MRI.setPhysRegUsed(Mips::RA);
266 else
267 MRI.setPhysRegUnused(Mips::RA);
268}