Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- MSP430FrameLowering.cpp - MSP430 Frame Information ----------------===// |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 9 | // This file contains the MSP430 implementation of TargetFrameLowering class. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 13 | #include "MSP430FrameLowering.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 14 | #include "MSP430InstrInfo.h" |
| 15 | #include "MSP430MachineFunctionInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 16 | #include "MSP430Subtarget.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 17 | #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 Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/Function.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 28 | bool MSP430FrameLowering::hasFP(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 29 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 30 | |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 31 | return (MF.getTarget().Options.DisableFramePointerElim(MF) || |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 32 | MF.getFrameInfo().hasVarSizedObjects() || |
| 33 | MFI.isFrameAddressTaken()); |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 36 | bool MSP430FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 37 | return !MF.getFrameInfo().hasVarSizedObjects(); |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 40 | void MSP430FrameLowering::emitPrologue(MachineFunction &MF, |
| 41 | MachineBasicBlock &MBB) const { |
| 42 | assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 43 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 44 | MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 45 | const MSP430InstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 46 | *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 47 | |
| 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 Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 52 | uint64_t StackSize = MFI.getStackSize(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 53 | |
| 54 | uint64_t NumBytes = 0; |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 55 | if (hasFP(MF)) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 56 | // 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 Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 63 | MFI.setOffsetAdjustment(-NumBytes); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 64 | |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 65 | // Save FP into the appropriate stack slot... |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 66 | BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r)) |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 67 | .addReg(MSP430::FP, RegState::Kill); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 68 | |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 69 | // Update FP with the new base value... |
| 70 | BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FP) |
| 71 | .addReg(MSP430::SP); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 72 | |
| 73 | // Mark the FramePtr as live-in in every block except the entry. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 74 | for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 75 | I != E; ++I) |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 76 | I->addLiveIn(MSP430::FP); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 77 | |
| 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 Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 88 | if (NumBytes) { // adjust stack pointer: SP -= numbytes |
| 89 | // If there is an SUB16ri of SP immediately before this instruction, merge |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 90 | // the two. |
| 91 | //NumBytes -= mergeSPUpdates(MBB, MBBI, true); |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 92 | // If there is an ADD16ri or SUB16ri of SP immediately after this |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 93 | // instruction, merge the two instructions. |
| 94 | // mergeSPUpdatesDown(MBB, MBBI, &NumBytes); |
| 95 | |
| 96 | if (NumBytes) { |
| 97 | MachineInstr *MI = |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 98 | BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SP) |
| 99 | .addReg(MSP430::SP).addImm(NumBytes); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 100 | // The SRW implicit def is dead. |
| 101 | MI->getOperand(3).setIsDead(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 106 | void MSP430FrameLowering::emitEpilogue(MachineFunction &MF, |
| 107 | MachineBasicBlock &MBB) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 108 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 109 | MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 110 | const MSP430InstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 111 | *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 112 | |
Jakob Stoklund Olesen | 4bc5e38 | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 113 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 114 | 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 Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 125 | uint64_t StackSize = MFI.getStackSize(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 126 | unsigned CSSize = MSP430FI->getCalleeSavedFrameSize(); |
| 127 | uint64_t NumBytes = 0; |
| 128 | |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 129 | if (hasFP(MF)) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 130 | // Calculate required stack adjustment |
| 131 | uint64_t FrameSize = StackSize - 2; |
| 132 | NumBytes = FrameSize - CSSize; |
| 133 | |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 134 | // pop FP. |
| 135 | BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FP); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 136 | } else |
| 137 | NumBytes = StackSize - CSSize; |
| 138 | |
| 139 | // Skip the callee-saved pop instructions. |
| 140 | while (MBBI != MBB.begin()) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 141 | MachineBasicBlock::iterator PI = std::prev(MBBI); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 142 | unsigned Opc = PI->getOpcode(); |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 143 | if (Opc != MSP430::POP16r && !PI->isTerminator()) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 144 | break; |
| 145 | --MBBI; |
| 146 | } |
| 147 | |
| 148 | DL = MBBI->getDebugLoc(); |
| 149 | |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 150 | // If there is an ADD16ri or SUB16ri of SP immediately before this |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 151 | // instruction, merge the two instructions. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 152 | //if (NumBytes || MFI.hasVarSizedObjects()) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 153 | // mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes); |
| 154 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 155 | if (MFI.hasVarSizedObjects()) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 156 | BuildMI(MBB, MBBI, DL, |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 157 | TII.get(MSP430::MOV16rr), MSP430::SP).addReg(MSP430::FP); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 158 | if (CSSize) { |
| 159 | MachineInstr *MI = |
| 160 | BuildMI(MBB, MBBI, DL, |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 161 | TII.get(MSP430::SUB16ri), MSP430::SP) |
| 162 | .addReg(MSP430::SP).addImm(CSSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 163 | // The SRW implicit def is dead. |
| 164 | MI->getOperand(3).setIsDead(); |
| 165 | } |
| 166 | } else { |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 167 | // adjust stack pointer back: SP += numbytes |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 168 | if (NumBytes) { |
| 169 | MachineInstr *MI = |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 170 | BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SP) |
| 171 | .addReg(MSP430::SP).addImm(NumBytes); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 172 | // The SRW implicit def is dead. |
| 173 | MI->getOperand(3).setIsDead(); |
| 174 | } |
| 175 | } |
| 176 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 177 | |
| 178 | // FIXME: Can we eleminate these in favour of generic code? |
| 179 | bool |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 180 | MSP430FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 181 | 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 Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 191 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 192 | 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 | |
| 205 | bool |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 206 | MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 207 | MachineBasicBlock::iterator MI, |
Krzysztof Parzyszek | bea30c6 | 2017-08-10 16:17:32 +0000 | [diff] [blame] | 208 | std::vector<CalleeSavedInfo> &CSI, |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 209 | 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 Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 217 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 218 | |
| 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 Korobeynikov | 0a69176 | 2012-10-17 17:37:11 +0000 | [diff] [blame] | 224 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 225 | MachineBasicBlock::iterator MSP430FrameLowering::eliminateCallFramePseudoInstr( |
| 226 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 227 | MachineBasicBlock::iterator I) const { |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 228 | const MSP430InstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 229 | *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 230 | unsigned StackAlign = getStackAlignment(); |
| 231 | |
| 232 | if (!hasReservedCallFrame(MF)) { |
| 233 | // If the stack pointer can be changed after prologue, turn the |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 234 | // adjcallstackup instruction into a 'sub SP, <amt>' and the |
| 235 | // adjcallstackdown instruction into 'add SP, <amt>' |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 236 | // TODO: consider using push / pop instead of sub + store / add |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 237 | MachineInstr &Old = *I; |
Serge Pavlov | d526b13 | 2017-05-09 13:35:13 +0000 | [diff] [blame] | 238 | uint64_t Amount = TII.getFrameSize(Old); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 239 | 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 Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 245 | MachineInstr *New = nullptr; |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 246 | 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 Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 251 | } else { |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 252 | assert(Old.getOpcode() == TII.getCallFrameDestroyOpcode()); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 253 | // factor out the amount the callee already popped. |
Serge Pavlov | d526b13 | 2017-05-09 13:35:13 +0000 | [diff] [blame] | 254 | Amount -= TII.getFramePoppedByCallee(Old); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 255 | if (Amount) |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 256 | New = BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::ADD16ri), |
| 257 | MSP430::SP) |
| 258 | .addReg(MSP430::SP) |
| 259 | .addImm(Amount); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 260 | } |
| 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 Pavlov | d526b13 | 2017-05-09 13:35:13 +0000 | [diff] [blame] | 273 | if (uint64_t CalleeAmt = TII.getFramePoppedByCallee(*I)) { |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 274 | MachineInstr &Old = *I; |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 275 | MachineInstr *New = |
Duncan P. N. Exon Smith | 8efc5b4 | 2016-07-08 21:19:46 +0000 | [diff] [blame] | 276 | BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::SUB16ri), MSP430::SP) |
| 277 | .addReg(MSP430::SP) |
| 278 | .addImm(CalleeAmt); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 279 | // The SRW implicit def is dead. |
| 280 | New->getOperand(3).setIsDead(); |
| 281 | |
| 282 | MBB.insert(I, New); |
| 283 | } |
| 284 | } |
| 285 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 286 | return MBB.erase(I); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Anton Korobeynikov | 0a69176 | 2012-10-17 17:37:11 +0000 | [diff] [blame] | 289 | void |
Hal Finkel | 5a765fd | 2013-03-14 20:33:40 +0000 | [diff] [blame] | 290 | MSP430FrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF, |
| 291 | RegScavenger *) const { |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 292 | // Create a frame entry for the FP register that must be saved. |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 293 | if (hasFP(MF)) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 294 | int FrameIdx = MF.getFrameInfo().CreateFixedObject(2, -4, true); |
Anton Korobeynikov | 0a69176 | 2012-10-17 17:37:11 +0000 | [diff] [blame] | 295 | (void)FrameIdx; |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 296 | assert(FrameIdx == MF.getFrameInfo().getObjectIndexBegin() && |
Job Noorman | eb19aea | 2014-09-10 06:58:14 +0000 | [diff] [blame] | 297 | "Slot for FP register must be last in order to be found!"); |
Anton Korobeynikov | 0a69176 | 2012-10-17 17:37:11 +0000 | [diff] [blame] | 298 | } |
| 299 | } |