blob: 8ab83d7cf3063a20c2c53e16b30ead9e9e08498e [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
Anton Korobeynikov2f931282011-01-10 12:39:04 +000042void MSP430FrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000043 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
44 MachineFrameInfo *MFI = MF.getFrameInfo();
45 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000046 const MSP430InstrInfo &TII =
Eric Christopherd9134482014-08-04 21:25:23 +000047 *static_cast<const MSP430InstrInfo *>(
48 MF.getTarget().getSubtargetImpl()->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
67 // Save FPW into the appropriate stack slot...
68 BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
69 .addReg(MSP430::FPW, RegState::Kill);
70
71 // Update FPW with the new base value...
72 BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FPW)
73 .addReg(MSP430::SPW);
74
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)
78 I->addLiveIn(MSP430::FPW);
79
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
90 if (NumBytes) { // adjust stack pointer: SPW -= numbytes
91 // If there is an SUB16ri of SPW immediately before this instruction, merge
92 // the two.
93 //NumBytes -= mergeSPUpdates(MBB, MBBI, true);
94 // If there is an ADD16ri or SUB16ri of SPW immediately after this
95 // instruction, merge the two instructions.
96 // mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
97
98 if (NumBytes) {
99 MachineInstr *MI =
100 BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SPW)
101 .addReg(MSP430::SPW).addImm(NumBytes);
102 // 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 Christopherd9134482014-08-04 21:25:23 +0000113 *static_cast<const MSP430InstrInfo *>(
114 MF.getTarget().getSubtargetImpl()->getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000115
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000116 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000117 unsigned RetOpcode = MBBI->getOpcode();
118 DebugLoc DL = MBBI->getDebugLoc();
119
120 switch (RetOpcode) {
121 case MSP430::RET:
122 case MSP430::RETI: break; // These are ok
123 default:
124 llvm_unreachable("Can only insert epilog into returning blocks");
125 }
126
127 // Get the number of bytes to allocate from the FrameInfo
128 uint64_t StackSize = MFI->getStackSize();
129 unsigned CSSize = MSP430FI->getCalleeSavedFrameSize();
130 uint64_t NumBytes = 0;
131
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000132 if (hasFP(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000133 // Calculate required stack adjustment
134 uint64_t FrameSize = StackSize - 2;
135 NumBytes = FrameSize - CSSize;
136
137 // pop FPW.
138 BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FPW);
139 } else
140 NumBytes = StackSize - CSSize;
141
142 // Skip the callee-saved pop instructions.
143 while (MBBI != MBB.begin()) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000144 MachineBasicBlock::iterator PI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000145 unsigned Opc = PI->getOpcode();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000146 if (Opc != MSP430::POP16r && !PI->isTerminator())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000147 break;
148 --MBBI;
149 }
150
151 DL = MBBI->getDebugLoc();
152
153 // If there is an ADD16ri or SUB16ri of SPW immediately before this
154 // instruction, merge the two instructions.
155 //if (NumBytes || MFI->hasVarSizedObjects())
156 // mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
157
158 if (MFI->hasVarSizedObjects()) {
159 BuildMI(MBB, MBBI, DL,
160 TII.get(MSP430::MOV16rr), MSP430::SPW).addReg(MSP430::FPW);
161 if (CSSize) {
162 MachineInstr *MI =
163 BuildMI(MBB, MBBI, DL,
164 TII.get(MSP430::SUB16ri), MSP430::SPW)
165 .addReg(MSP430::SPW).addImm(CSSize);
166 // The SRW implicit def is dead.
167 MI->getOperand(3).setIsDead();
168 }
169 } else {
170 // adjust stack pointer back: SPW += numbytes
171 if (NumBytes) {
172 MachineInstr *MI =
173 BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SPW)
174 .addReg(MSP430::SPW).addImm(NumBytes);
175 // The SRW implicit def is dead.
176 MI->getOperand(3).setIsDead();
177 }
178 }
179}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000180
181// FIXME: Can we eleminate these in favour of generic code?
182bool
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000183MSP430FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000184 MachineBasicBlock::iterator MI,
185 const std::vector<CalleeSavedInfo> &CSI,
186 const TargetRegisterInfo *TRI) const {
187 if (CSI.empty())
188 return false;
189
190 DebugLoc DL;
191 if (MI != MBB.end()) DL = MI->getDebugLoc();
192
193 MachineFunction &MF = *MBB.getParent();
Eric Christopherd9134482014-08-04 21:25:23 +0000194 const TargetInstrInfo &TII =
195 *MF.getTarget().getSubtargetImpl()->getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000196 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
197 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
198
199 for (unsigned i = CSI.size(); i != 0; --i) {
200 unsigned Reg = CSI[i-1].getReg();
201 // Add the callee-saved register as live-in. It's killed at the spill.
202 MBB.addLiveIn(Reg);
203 BuildMI(MBB, MI, DL, TII.get(MSP430::PUSH16r))
204 .addReg(Reg, RegState::Kill);
205 }
206 return true;
207}
208
209bool
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000210MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
211 MachineBasicBlock::iterator MI,
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000212 const std::vector<CalleeSavedInfo> &CSI,
213 const TargetRegisterInfo *TRI) const {
214 if (CSI.empty())
215 return false;
216
217 DebugLoc DL;
218 if (MI != MBB.end()) DL = MI->getDebugLoc();
219
220 MachineFunction &MF = *MBB.getParent();
Eric Christopherd9134482014-08-04 21:25:23 +0000221 const TargetInstrInfo &TII =
222 *MF.getTarget().getSubtargetImpl()->getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000223
224 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
225 BuildMI(MBB, MI, DL, TII.get(MSP430::POP16r), CSI[i].getReg());
226
227 return true;
228}
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000229
Eli Bendersky8da87162013-02-21 20:05:00 +0000230void MSP430FrameLowering::
231eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
232 MachineBasicBlock::iterator I) const {
233 const MSP430InstrInfo &TII =
Eric Christopherd9134482014-08-04 21:25:23 +0000234 *static_cast<const MSP430InstrInfo *>(
235 MF.getTarget().getSubtargetImpl()->getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +0000236 unsigned StackAlign = getStackAlignment();
237
238 if (!hasReservedCallFrame(MF)) {
239 // If the stack pointer can be changed after prologue, turn the
240 // adjcallstackup instruction into a 'sub SPW, <amt>' and the
241 // adjcallstackdown instruction into 'add SPW, <amt>'
242 // TODO: consider using push / pop instead of sub + store / add
243 MachineInstr *Old = I;
244 uint64_t Amount = Old->getOperand(0).getImm();
245 if (Amount != 0) {
246 // We need to keep the stack aligned properly. To do this, we round the
247 // amount of space needed for the outgoing arguments up to the next
248 // alignment boundary.
249 Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
250
Craig Topper062a2ba2014-04-25 05:30:21 +0000251 MachineInstr *New = nullptr;
Eli Bendersky8da87162013-02-21 20:05:00 +0000252 if (Old->getOpcode() == TII.getCallFrameSetupOpcode()) {
253 New = BuildMI(MF, Old->getDebugLoc(),
254 TII.get(MSP430::SUB16ri), MSP430::SPW)
255 .addReg(MSP430::SPW).addImm(Amount);
256 } else {
257 assert(Old->getOpcode() == TII.getCallFrameDestroyOpcode());
258 // factor out the amount the callee already popped.
259 uint64_t CalleeAmt = Old->getOperand(1).getImm();
260 Amount -= CalleeAmt;
261 if (Amount)
262 New = BuildMI(MF, Old->getDebugLoc(),
263 TII.get(MSP430::ADD16ri), MSP430::SPW)
264 .addReg(MSP430::SPW).addImm(Amount);
265 }
266
267 if (New) {
268 // The SRW implicit def is dead.
269 New->getOperand(3).setIsDead();
270
271 // Replace the pseudo instruction with a new instruction...
272 MBB.insert(I, New);
273 }
274 }
275 } else if (I->getOpcode() == TII.getCallFrameDestroyOpcode()) {
276 // If we are performing frame pointer elimination and if the callee pops
277 // something off the stack pointer, add it back.
278 if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
279 MachineInstr *Old = I;
280 MachineInstr *New =
281 BuildMI(MF, Old->getDebugLoc(), TII.get(MSP430::SUB16ri),
282 MSP430::SPW).addReg(MSP430::SPW).addImm(CalleeAmt);
283 // The SRW implicit def is dead.
284 New->getOperand(3).setIsDead();
285
286 MBB.insert(I, New);
287 }
288 }
289
290 MBB.erase(I);
291}
292
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000293void
Hal Finkel5a765fd2013-03-14 20:33:40 +0000294MSP430FrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
295 RegScavenger *) const {
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000296 // Create a frame entry for the FPW register that must be saved.
Eli Bendersky8da87162013-02-21 20:05:00 +0000297 if (hasFP(MF)) {
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000298 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(2, -4, true);
299 (void)FrameIdx;
300 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
301 "Slot for FPW register must be last in order to be found!");
302 }
303}