blob: de60ad9bd7e6f070a2be784edb588b96ad3bdbf5 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- MSP430FrameLowering.cpp - MSP430 Frame Information ----------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00006//
7//===----------------------------------------------------------------------===//
8//
Anton Korobeynikov2f931282011-01-10 12:39:04 +00009// This file contains the MSP430 implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000010//
11//===----------------------------------------------------------------------===//
12
Anton Korobeynikov2f931282011-01-10 12:39:04 +000013#include "MSP430FrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000014#include "MSP430InstrInfo.h"
15#include "MSP430MachineFunctionInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000016#include "MSP430Subtarget.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Function.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000025
26using namespace llvm;
27
Anton Korobeynikov2f931282011-01-10 12:39:04 +000028bool MSP430FrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000029 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000030
Nick Lewycky50f02cb2011-12-02 22:16:29 +000031 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +000032 MF.getFrameInfo().hasVarSizedObjects() ||
33 MFI.isFrameAddressTaken());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000034}
35
Anton Korobeynikov2f931282011-01-10 12:39:04 +000036bool MSP430FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000037 return !MF.getFrameInfo().hasVarSizedObjects();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000038}
39
Quentin Colombet61b305e2015-05-05 17:38:16 +000040void MSP430FrameLowering::emitPrologue(MachineFunction &MF,
41 MachineBasicBlock &MBB) const {
42 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Matthias Braun941a7052016-07-28 18:40:00 +000043 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000044 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000045 const MSP430InstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +000046 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000047
48 MachineBasicBlock::iterator MBBI = MBB.begin();
49 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
50
51 // Get the number of bytes to allocate from the FrameInfo.
Matthias Braun941a7052016-07-28 18:40:00 +000052 uint64_t StackSize = MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000053
54 uint64_t NumBytes = 0;
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000055 if (hasFP(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000056 // Calculate required stack adjustment
57 uint64_t FrameSize = StackSize - 2;
58 NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize();
59
60 // Get the offset of the stack slot for the EBP register... which is
61 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
62 // Update the frame offset adjustment.
Matthias Braun941a7052016-07-28 18:40:00 +000063 MFI.setOffsetAdjustment(-NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000064
Job Noormaneb19aea2014-09-10 06:58:14 +000065 // Save FP into the appropriate stack slot...
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000066 BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
Job Noormaneb19aea2014-09-10 06:58:14 +000067 .addReg(MSP430::FP, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000068
Job Noormaneb19aea2014-09-10 06:58:14 +000069 // Update FP with the new base value...
70 BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FP)
71 .addReg(MSP430::SP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000072
73 // Mark the FramePtr as live-in in every block except the entry.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000074 for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000075 I != E; ++I)
Job Noormaneb19aea2014-09-10 06:58:14 +000076 I->addLiveIn(MSP430::FP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000077
78 } else
79 NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize();
80
81 // Skip the callee-saved push instructions.
82 while (MBBI != MBB.end() && (MBBI->getOpcode() == MSP430::PUSH16r))
83 ++MBBI;
84
85 if (MBBI != MBB.end())
86 DL = MBBI->getDebugLoc();
87
Job Noormaneb19aea2014-09-10 06:58:14 +000088 if (NumBytes) { // adjust stack pointer: SP -= numbytes
89 // If there is an SUB16ri of SP immediately before this instruction, merge
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000090 // the two.
91 //NumBytes -= mergeSPUpdates(MBB, MBBI, true);
Job Noormaneb19aea2014-09-10 06:58:14 +000092 // If there is an ADD16ri or SUB16ri of SP immediately after this
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000093 // instruction, merge the two instructions.
94 // mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
95
96 if (NumBytes) {
97 MachineInstr *MI =
Job Noormaneb19aea2014-09-10 06:58:14 +000098 BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SP)
99 .addReg(MSP430::SP).addImm(NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000100 // The SRW implicit def is dead.
101 MI->getOperand(3).setIsDead();
102 }
103 }
104}
105
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000106void MSP430FrameLowering::emitEpilogue(MachineFunction &MF,
107 MachineBasicBlock &MBB) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000108 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000109 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000110 const MSP430InstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000111 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000112
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000113 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000114 unsigned RetOpcode = MBBI->getOpcode();
115 DebugLoc DL = MBBI->getDebugLoc();
116
117 switch (RetOpcode) {
118 case MSP430::RET:
119 case MSP430::RETI: break; // These are ok
120 default:
121 llvm_unreachable("Can only insert epilog into returning blocks");
122 }
123
124 // Get the number of bytes to allocate from the FrameInfo
Matthias Braun941a7052016-07-28 18:40:00 +0000125 uint64_t StackSize = MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000126 unsigned CSSize = MSP430FI->getCalleeSavedFrameSize();
127 uint64_t NumBytes = 0;
128
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000129 if (hasFP(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000130 // Calculate required stack adjustment
131 uint64_t FrameSize = StackSize - 2;
132 NumBytes = FrameSize - CSSize;
133
Job Noormaneb19aea2014-09-10 06:58:14 +0000134 // pop FP.
135 BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000136 } else
137 NumBytes = StackSize - CSSize;
138
139 // Skip the callee-saved pop instructions.
140 while (MBBI != MBB.begin()) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000141 MachineBasicBlock::iterator PI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000142 unsigned Opc = PI->getOpcode();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000143 if (Opc != MSP430::POP16r && !PI->isTerminator())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000144 break;
145 --MBBI;
146 }
147
148 DL = MBBI->getDebugLoc();
149
Job Noormaneb19aea2014-09-10 06:58:14 +0000150 // If there is an ADD16ri or SUB16ri of SP immediately before this
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000151 // instruction, merge the two instructions.
Matthias Braun941a7052016-07-28 18:40:00 +0000152 //if (NumBytes || MFI.hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000153 // mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
154
Matthias Braun941a7052016-07-28 18:40:00 +0000155 if (MFI.hasVarSizedObjects()) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000156 BuildMI(MBB, MBBI, DL,
Job Noormaneb19aea2014-09-10 06:58:14 +0000157 TII.get(MSP430::MOV16rr), MSP430::SP).addReg(MSP430::FP);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000158 if (CSSize) {
159 MachineInstr *MI =
160 BuildMI(MBB, MBBI, DL,
Job Noormaneb19aea2014-09-10 06:58:14 +0000161 TII.get(MSP430::SUB16ri), MSP430::SP)
162 .addReg(MSP430::SP).addImm(CSSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000163 // The SRW implicit def is dead.
164 MI->getOperand(3).setIsDead();
165 }
166 } else {
Job Noormaneb19aea2014-09-10 06:58:14 +0000167 // adjust stack pointer back: SP += numbytes
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000168 if (NumBytes) {
169 MachineInstr *MI =
Job Noormaneb19aea2014-09-10 06:58:14 +0000170 BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SP)
171 .addReg(MSP430::SP).addImm(NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000172 // The SRW implicit def is dead.
173 MI->getOperand(3).setIsDead();
174 }
175 }
176}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000177
178// FIXME: Can we eleminate these in favour of generic code?
179bool
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000180MSP430FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000181 MachineBasicBlock::iterator MI,
182 const std::vector<CalleeSavedInfo> &CSI,
183 const TargetRegisterInfo *TRI) const {
184 if (CSI.empty())
185 return false;
186
187 DebugLoc DL;
188 if (MI != MBB.end()) DL = MI->getDebugLoc();
189
190 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000191 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000192 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
193 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
194
195 for (unsigned i = CSI.size(); i != 0; --i) {
196 unsigned Reg = CSI[i-1].getReg();
197 // Add the callee-saved register as live-in. It's killed at the spill.
198 MBB.addLiveIn(Reg);
199 BuildMI(MBB, MI, DL, TII.get(MSP430::PUSH16r))
200 .addReg(Reg, RegState::Kill);
201 }
202 return true;
203}
204
205bool
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000206MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
207 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +0000208 std::vector<CalleeSavedInfo> &CSI,
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000209 const TargetRegisterInfo *TRI) const {
210 if (CSI.empty())
211 return false;
212
213 DebugLoc DL;
214 if (MI != MBB.end()) DL = MI->getDebugLoc();
215
216 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000217 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000218
219 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
220 BuildMI(MBB, MI, DL, TII.get(MSP430::POP16r), CSI[i].getReg());
221
222 return true;
223}
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000224
Hans Wennborge1a2e902016-03-31 18:33:38 +0000225MachineBasicBlock::iterator MSP430FrameLowering::eliminateCallFramePseudoInstr(
226 MachineFunction &MF, MachineBasicBlock &MBB,
227 MachineBasicBlock::iterator I) const {
Eli Bendersky8da87162013-02-21 20:05:00 +0000228 const MSP430InstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000229 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +0000230 unsigned StackAlign = getStackAlignment();
231
232 if (!hasReservedCallFrame(MF)) {
233 // If the stack pointer can be changed after prologue, turn the
Job Noormaneb19aea2014-09-10 06:58:14 +0000234 // adjcallstackup instruction into a 'sub SP, <amt>' and the
235 // adjcallstackdown instruction into 'add SP, <amt>'
Eli Bendersky8da87162013-02-21 20:05:00 +0000236 // TODO: consider using push / pop instead of sub + store / add
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000237 MachineInstr &Old = *I;
Serge Pavlovd526b132017-05-09 13:35:13 +0000238 uint64_t Amount = TII.getFrameSize(Old);
Eli Bendersky8da87162013-02-21 20:05:00 +0000239 if (Amount != 0) {
240 // We need to keep the stack aligned properly. To do this, we round the
241 // amount of space needed for the outgoing arguments up to the next
242 // alignment boundary.
243 Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
244
Craig Topper062a2ba2014-04-25 05:30:21 +0000245 MachineInstr *New = nullptr;
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000246 if (Old.getOpcode() == TII.getCallFrameSetupOpcode()) {
247 New =
248 BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::SUB16ri), MSP430::SP)
249 .addReg(MSP430::SP)
250 .addImm(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +0000251 } else {
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000252 assert(Old.getOpcode() == TII.getCallFrameDestroyOpcode());
Eli Bendersky8da87162013-02-21 20:05:00 +0000253 // factor out the amount the callee already popped.
Serge Pavlovd526b132017-05-09 13:35:13 +0000254 Amount -= TII.getFramePoppedByCallee(Old);
Eli Bendersky8da87162013-02-21 20:05:00 +0000255 if (Amount)
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000256 New = BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::ADD16ri),
257 MSP430::SP)
258 .addReg(MSP430::SP)
259 .addImm(Amount);
Eli Bendersky8da87162013-02-21 20:05:00 +0000260 }
261
262 if (New) {
263 // The SRW implicit def is dead.
264 New->getOperand(3).setIsDead();
265
266 // Replace the pseudo instruction with a new instruction...
267 MBB.insert(I, New);
268 }
269 }
270 } else if (I->getOpcode() == TII.getCallFrameDestroyOpcode()) {
271 // If we are performing frame pointer elimination and if the callee pops
272 // something off the stack pointer, add it back.
Serge Pavlovd526b132017-05-09 13:35:13 +0000273 if (uint64_t CalleeAmt = TII.getFramePoppedByCallee(*I)) {
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000274 MachineInstr &Old = *I;
Eli Bendersky8da87162013-02-21 20:05:00 +0000275 MachineInstr *New =
Duncan P. N. Exon Smith8efc5b42016-07-08 21:19:46 +0000276 BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::SUB16ri), MSP430::SP)
277 .addReg(MSP430::SP)
278 .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
Hans Wennborge1a2e902016-03-31 18:33:38 +0000286 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +0000287}
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)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000294 int FrameIdx = MF.getFrameInfo().CreateFixedObject(2, -4, true);
Anton Korobeynikov0a691762012-10-17 17:37:11 +0000295 (void)FrameIdx;
Matthias Braun941a7052016-07-28 18:40:00 +0000296 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}