blob: eb720809e47cca6910468081b330a0b04c070259 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- MSP430FrameLowering.cpp - MSP430 Frame Information ----------------===//
Anton Korobeynikovf7183ed2010-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//
8//===----------------------------------------------------------------------===//
9//
Anton Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the MSP430 implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "MSP430FrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000015#include "MSP430InstrInfo.h"
16#include "MSP430MachineFunctionInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000017#include "MSP430Subtarget.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000018#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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000025#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000027
28using namespace llvm;
29
Anton Korobeynikov2f931282011-01-10 12:39:04 +000030bool MSP430FrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000031 const MachineFrameInfo *MFI = MF.getFrameInfo();
32
Nick Lewycky50f02cb2011-12-02 22:16:29 +000033 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000034 MF.getFrameInfo()->hasVarSizedObjects() ||
35 MFI->isFrameAddressTaken());
36}
37
Anton Korobeynikov2f931282011-01-10 12:39:04 +000038bool MSP430FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000039 return !MF.getFrameInfo()->hasVarSizedObjects();
40}
41
Quentin Colombet61b305e2015-05-05 17:38:16 +000042void MSP430FrameLowering::emitPrologue(MachineFunction &MF,
43 MachineBasicBlock &MBB) const {
44 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000045 MachineFrameInfo *MFI = MF.getFrameInfo();
46 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000047 const MSP430InstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +000048 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000049
50 MachineBasicBlock::iterator MBBI = MBB.begin();
51 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
52
53 // Get the number of bytes to allocate from the FrameInfo.
54 uint64_t StackSize = MFI->getStackSize();
55
56 uint64_t NumBytes = 0;
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000057 if (hasFP(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000058 // Calculate required stack adjustment
59 uint64_t FrameSize = StackSize - 2;
60 NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize();
61
62 // Get the offset of the stack slot for the EBP register... which is
63 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
64 // Update the frame offset adjustment.
65 MFI->setOffsetAdjustment(-NumBytes);
66
Job Noormaneb19aea2014-09-10 06:58:14 +000067 // Save FP into the appropriate stack slot...
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000068 BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
Job Noormaneb19aea2014-09-10 06:58:14 +000069 .addReg(MSP430::FP, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000070
Job Noormaneb19aea2014-09-10 06:58:14 +000071 // Update FP with the new base value...
72 BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FP)
73 .addReg(MSP430::SP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000074
75 // Mark the FramePtr as live-in in every block except the entry.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000076 for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000077 I != E; ++I)
Job Noormaneb19aea2014-09-10 06:58:14 +000078 I->addLiveIn(MSP430::FP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000079
80 } else
81 NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize();
82
83 // Skip the callee-saved push instructions.
84 while (MBBI != MBB.end() && (MBBI->getOpcode() == MSP430::PUSH16r))
85 ++MBBI;
86
87 if (MBBI != MBB.end())
88 DL = MBBI->getDebugLoc();
89
Job Noormaneb19aea2014-09-10 06:58:14 +000090 if (NumBytes) { // adjust stack pointer: SP -= numbytes
91 // If there is an SUB16ri of SP immediately before this instruction, merge
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000092 // the two.
93 //NumBytes -= mergeSPUpdates(MBB, MBBI, true);
Job Noormaneb19aea2014-09-10 06:58:14 +000094 // If there is an ADD16ri or SUB16ri of SP immediately after this
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000095 // instruction, merge the two instructions.
96 // mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
97
98 if (NumBytes) {
99 MachineInstr *MI =
Job Noormaneb19aea2014-09-10 06:58:14 +0000100 BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SP)
101 .addReg(MSP430::SP).addImm(NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000102 // The SRW implicit def is dead.
103 MI->getOperand(3).setIsDead();
104 }
105 }
106}
107
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000108void MSP430FrameLowering::emitEpilogue(MachineFunction &MF,
109 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000110 const MachineFrameInfo *MFI = MF.getFrameInfo();
111 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000112 const MSP430InstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000113 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000114
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000115 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000116 unsigned RetOpcode = MBBI->getOpcode();
117 DebugLoc DL = MBBI->getDebugLoc();
118
119 switch (RetOpcode) {
120 case MSP430::RET:
121 case MSP430::RETI: break; // These are ok
122 default:
123 llvm_unreachable("Can only insert epilog into returning blocks");
124 }
125
126 // Get the number of bytes to allocate from the FrameInfo
127 uint64_t StackSize = MFI->getStackSize();
128 unsigned CSSize = MSP430FI->getCalleeSavedFrameSize();
129 uint64_t NumBytes = 0;
130
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000131 if (hasFP(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000132 // Calculate required stack adjustment
133 uint64_t FrameSize = StackSize - 2;
134 NumBytes = FrameSize - CSSize;
135
Job Noormaneb19aea2014-09-10 06:58:14 +0000136 // pop FP.
137 BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000138 } else
139 NumBytes = StackSize - CSSize;
140
141 // Skip the callee-saved pop instructions.
142 while (MBBI != MBB.begin()) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000143 MachineBasicBlock::iterator PI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000144 unsigned Opc = PI->getOpcode();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000145 if (Opc != MSP430::POP16r && !PI->isTerminator())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000146 break;
147 --MBBI;
148 }
149
150 DL = MBBI->getDebugLoc();
151
Job Noormaneb19aea2014-09-10 06:58:14 +0000152 // If there is an ADD16ri or SUB16ri of SP immediately before this
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000153 // instruction, merge the two instructions.
154 //if (NumBytes || MFI->hasVarSizedObjects())
155 // mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
156
157 if (MFI->hasVarSizedObjects()) {
158 BuildMI(MBB, MBBI, DL,
Job Noormaneb19aea2014-09-10 06:58:14 +0000159 TII.get(MSP430::MOV16rr), MSP430::SP).addReg(MSP430::FP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000160 if (CSSize) {
161 MachineInstr *MI =
162 BuildMI(MBB, MBBI, DL,
Job Noormaneb19aea2014-09-10 06:58:14 +0000163 TII.get(MSP430::SUB16ri), MSP430::SP)
164 .addReg(MSP430::SP).addImm(CSSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000165 // The SRW implicit def is dead.
166 MI->getOperand(3).setIsDead();
167 }
168 } else {
Job Noormaneb19aea2014-09-10 06:58:14 +0000169 // adjust stack pointer back: SP += numbytes
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000170 if (NumBytes) {
171 MachineInstr *MI =
Job Noormaneb19aea2014-09-10 06:58:14 +0000172 BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SP)
173 .addReg(MSP430::SP).addImm(NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000174 // The SRW implicit def is dead.
175 MI->getOperand(3).setIsDead();
176 }
177 }
178}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000179
180// FIXME: Can we eleminate these in favour of generic code?
181bool
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000182MSP430FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000183 MachineBasicBlock::iterator MI,
184 const std::vector<CalleeSavedInfo> &CSI,
185 const TargetRegisterInfo *TRI) const {
186 if (CSI.empty())
187 return false;
188
189 DebugLoc DL;
190 if (MI != MBB.end()) DL = MI->getDebugLoc();
191
192 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000193 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000194 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
195 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
196
197 for (unsigned i = CSI.size(); i != 0; --i) {
198 unsigned Reg = CSI[i-1].getReg();
199 // Add the callee-saved register as live-in. It's killed at the spill.
200 MBB.addLiveIn(Reg);
201 BuildMI(MBB, MI, DL, TII.get(MSP430::PUSH16r))
202 .addReg(Reg, RegState::Kill);
203 }
204 return true;
205}
206
207bool
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000208MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
209 MachineBasicBlock::iterator MI,
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000210 const std::vector<CalleeSavedInfo> &CSI,
211 const TargetRegisterInfo *TRI) const {
212 if (CSI.empty())
213 return false;
214
215 DebugLoc DL;
216 if (MI != MBB.end()) DL = MI->getDebugLoc();
217
218 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000219 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000220
221 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
222 BuildMI(MBB, MI, DL, TII.get(MSP430::POP16r), CSI[i].getReg());
223
224 return true;
225}
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000226
Eli Bendersky8da87162013-02-21 20:05:00 +0000227void MSP430FrameLowering::
228eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
229 MachineBasicBlock::iterator I) const {
230 const MSP430InstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000231 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +0000232 unsigned StackAlign = getStackAlignment();
233
234 if (!hasReservedCallFrame(MF)) {
235 // If the stack pointer can be changed after prologue, turn the
Job Noormaneb19aea2014-09-10 06:58:14 +0000236 // adjcallstackup instruction into a 'sub SP, <amt>' and the
237 // adjcallstackdown instruction into 'add SP, <amt>'
Eli Bendersky8da87162013-02-21 20:05:00 +0000238 // TODO: consider using push / pop instead of sub + store / add
239 MachineInstr *Old = I;
240 uint64_t Amount = Old->getOperand(0).getImm();
241 if (Amount != 0) {
242 // We need to keep the stack aligned properly. To do this, we round the
243 // amount of space needed for the outgoing arguments up to the next
244 // alignment boundary.
245 Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
246
Craig Topper062a2ba2014-04-25 05:30:21 +0000247 MachineInstr *New = nullptr;
Eli Bendersky8da87162013-02-21 20:05:00 +0000248 if (Old->getOpcode() == TII.getCallFrameSetupOpcode()) {
249 New = BuildMI(MF, Old->getDebugLoc(),
Job Noormaneb19aea2014-09-10 06:58:14 +0000250 TII.get(MSP430::SUB16ri), MSP430::SP)
251 .addReg(MSP430::SP).addImm(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +0000252 } else {
253 assert(Old->getOpcode() == TII.getCallFrameDestroyOpcode());
254 // factor out the amount the callee already popped.
255 uint64_t CalleeAmt = Old->getOperand(1).getImm();
256 Amount -= CalleeAmt;
257 if (Amount)
258 New = BuildMI(MF, Old->getDebugLoc(),
Job Noormaneb19aea2014-09-10 06:58:14 +0000259 TII.get(MSP430::ADD16ri), MSP430::SP)
260 .addReg(MSP430::SP).addImm(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +0000261 }
262
263 if (New) {
264 // The SRW implicit def is dead.
265 New->getOperand(3).setIsDead();
266
267 // Replace the pseudo instruction with a new instruction...
268 MBB.insert(I, New);
269 }
270 }
271 } else if (I->getOpcode() == TII.getCallFrameDestroyOpcode()) {
272 // If we are performing frame pointer elimination and if the callee pops
273 // something off the stack pointer, add it back.
274 if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
275 MachineInstr *Old = I;
276 MachineInstr *New =
277 BuildMI(MF, Old->getDebugLoc(), TII.get(MSP430::SUB16ri),
Job Noormaneb19aea2014-09-10 06:58:14 +0000278 MSP430::SP).addReg(MSP430::SP).addImm(CalleeAmt);
Eli Bendersky8da87162013-02-21 20:05:00 +0000279 // The SRW implicit def is dead.
280 New->getOperand(3).setIsDead();
281
282 MBB.insert(I, New);
283 }
284 }
285
286 MBB.erase(I);
287}
288
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000289void
Hal Finkel5a765fd2013-03-14 20:33:40 +0000290MSP430FrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
291 RegScavenger *) const {
Job Noormaneb19aea2014-09-10 06:58:14 +0000292 // Create a frame entry for the FP register that must be saved.
Eli Bendersky8da87162013-02-21 20:05:00 +0000293 if (hasFP(MF)) {
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000294 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(2, -4, true);
295 (void)FrameIdx;
296 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
Job Noormaneb19aea2014-09-10 06:58:14 +0000297 "Slot for FP register must be last in order to be found!");
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000298 }
299}