1 //===-- MSP430FrameLowering.cpp - MSP430 Frame Information ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the MSP430 implementation of TargetFrameLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "MSP430FrameLowering.h"
15 #include "MSP430InstrInfo.h"
16 #include "MSP430MachineFunctionInfo.h"
17 #include "MSP430Subtarget.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/IR/DataLayout.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Target/TargetOptions.h"
30 bool MSP430FrameLowering::hasFP(const MachineFunction &MF) const {
31 const MachineFrameInfo *MFI = MF.getFrameInfo();
33 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
34 MF.getFrameInfo()->hasVarSizedObjects() ||
35 MFI->isFrameAddressTaken());
38 bool MSP430FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
39 return !MF.getFrameInfo()->hasVarSizedObjects();
42 void MSP430FrameLowering::emitPrologue(MachineFunction &MF,
43 MachineBasicBlock &MBB) const {
44 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
45 MachineFrameInfo *MFI = MF.getFrameInfo();
46 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
47 const MSP430InstrInfo &TII =
48 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
50 MachineBasicBlock::iterator MBBI = MBB.begin();
51 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
53 // Get the number of bytes to allocate from the FrameInfo.
54 uint64_t StackSize = MFI->getStackSize();
56 uint64_t NumBytes = 0;
58 // Calculate required stack adjustment
59 uint64_t FrameSize = StackSize - 2;
60 NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize();
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);
67 // Save FP into the appropriate stack slot...
68 BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
69 .addReg(MSP430::FP, RegState::Kill);
71 // Update FP with the new base value...
72 BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FP)
75 // Mark the FramePtr as live-in in every block except the entry.
76 for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
78 I->addLiveIn(MSP430::FP);
81 NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize();
83 // Skip the callee-saved push instructions.
84 while (MBBI != MBB.end() && (MBBI->getOpcode() == MSP430::PUSH16r))
87 if (MBBI != MBB.end())
88 DL = MBBI->getDebugLoc();
90 if (NumBytes) { // adjust stack pointer: SP -= numbytes
91 // If there is an SUB16ri of SP immediately before this instruction, merge
93 //NumBytes -= mergeSPUpdates(MBB, MBBI, true);
94 // If there is an ADD16ri or SUB16ri of SP immediately after this
95 // instruction, merge the two instructions.
96 // mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
100 BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SP)
101 .addReg(MSP430::SP).addImm(NumBytes);
102 // The SRW implicit def is dead.
103 MI->getOperand(3).setIsDead();
108 void MSP430FrameLowering::emitEpilogue(MachineFunction &MF,
109 MachineBasicBlock &MBB) const {
110 const MachineFrameInfo *MFI = MF.getFrameInfo();
111 MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
112 const MSP430InstrInfo &TII =
113 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
115 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
116 unsigned RetOpcode = MBBI->getOpcode();
117 DebugLoc DL = MBBI->getDebugLoc();
121 case MSP430::RETI: break; // These are ok
123 llvm_unreachable("Can only insert epilog into returning blocks");
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;
132 // Calculate required stack adjustment
133 uint64_t FrameSize = StackSize - 2;
134 NumBytes = FrameSize - CSSize;
137 BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FP);
139 NumBytes = StackSize - CSSize;
141 // Skip the callee-saved pop instructions.
142 while (MBBI != MBB.begin()) {
143 MachineBasicBlock::iterator PI = std::prev(MBBI);
144 unsigned Opc = PI->getOpcode();
145 if (Opc != MSP430::POP16r && !PI->isTerminator())
150 DL = MBBI->getDebugLoc();
152 // If there is an ADD16ri or SUB16ri of SP immediately before this
153 // instruction, merge the two instructions.
154 //if (NumBytes || MFI->hasVarSizedObjects())
155 // mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
157 if (MFI->hasVarSizedObjects()) {
158 BuildMI(MBB, MBBI, DL,
159 TII.get(MSP430::MOV16rr), MSP430::SP).addReg(MSP430::FP);
162 BuildMI(MBB, MBBI, DL,
163 TII.get(MSP430::SUB16ri), MSP430::SP)
164 .addReg(MSP430::SP).addImm(CSSize);
165 // The SRW implicit def is dead.
166 MI->getOperand(3).setIsDead();
169 // adjust stack pointer back: SP += numbytes
172 BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SP)
173 .addReg(MSP430::SP).addImm(NumBytes);
174 // The SRW implicit def is dead.
175 MI->getOperand(3).setIsDead();
180 // FIXME: Can we eleminate these in favour of generic code?
182 MSP430FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
183 MachineBasicBlock::iterator MI,
184 const std::vector<CalleeSavedInfo> &CSI,
185 const TargetRegisterInfo *TRI) const {
190 if (MI != MBB.end()) DL = MI->getDebugLoc();
192 MachineFunction &MF = *MBB.getParent();
193 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
194 MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
195 MFI->setCalleeSavedFrameSize(CSI.size() * 2);
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.
201 BuildMI(MBB, MI, DL, TII.get(MSP430::PUSH16r))
202 .addReg(Reg, RegState::Kill);
208 MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
209 MachineBasicBlock::iterator MI,
210 const std::vector<CalleeSavedInfo> &CSI,
211 const TargetRegisterInfo *TRI) const {
216 if (MI != MBB.end()) DL = MI->getDebugLoc();
218 MachineFunction &MF = *MBB.getParent();
219 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
221 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
222 BuildMI(MBB, MI, DL, TII.get(MSP430::POP16r), CSI[i].getReg());
227 void MSP430FrameLowering::
228 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
229 MachineBasicBlock::iterator I) const {
230 const MSP430InstrInfo &TII =
231 *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
232 unsigned StackAlign = getStackAlignment();
234 if (!hasReservedCallFrame(MF)) {
235 // If the stack pointer can be changed after prologue, turn the
236 // adjcallstackup instruction into a 'sub SP, <amt>' and the
237 // adjcallstackdown instruction into 'add SP, <amt>'
238 // TODO: consider using push / pop instead of sub + store / add
239 MachineInstr *Old = I;
240 uint64_t Amount = Old->getOperand(0).getImm();
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;
247 MachineInstr *New = nullptr;
248 if (Old->getOpcode() == TII.getCallFrameSetupOpcode()) {
249 New = BuildMI(MF, Old->getDebugLoc(),
250 TII.get(MSP430::SUB16ri), MSP430::SP)
251 .addReg(MSP430::SP).addImm(Amount);
253 assert(Old->getOpcode() == TII.getCallFrameDestroyOpcode());
254 // factor out the amount the callee already popped.
255 uint64_t CalleeAmt = Old->getOperand(1).getImm();
258 New = BuildMI(MF, Old->getDebugLoc(),
259 TII.get(MSP430::ADD16ri), MSP430::SP)
260 .addReg(MSP430::SP).addImm(Amount);
264 // The SRW implicit def is dead.
265 New->getOperand(3).setIsDead();
267 // Replace the pseudo instruction with a new instruction...
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;
277 BuildMI(MF, Old->getDebugLoc(), TII.get(MSP430::SUB16ri),
278 MSP430::SP).addReg(MSP430::SP).addImm(CalleeAmt);
279 // The SRW implicit def is dead.
280 New->getOperand(3).setIsDead();
290 MSP430FrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
291 RegScavenger *) const {
292 // Create a frame entry for the FP register that must be saved.
294 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(2, -4, true);
296 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
297 "Slot for FP register must be last in order to be found!");