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 | // |
| 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 Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 10 | // This file contains the MSP430 implementation of TargetFrameLowering class. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 14 | #include "MSP430FrameLowering.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 15 | #include "MSP430InstrInfo.h" |
| 16 | #include "MSP430MachineFunctionInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame^] | 17 | #include "MSP430Subtarget.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/Function.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 30 | bool MSP430FrameLowering::hasFP(const MachineFunction &MF) const { |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 31 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 32 | |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 33 | return (MF.getTarget().Options.DisableFramePointerElim(MF) || |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 34 | MF.getFrameInfo()->hasVarSizedObjects() || |
| 35 | MFI->isFrameAddressTaken()); |
| 36 | } |
| 37 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 38 | bool MSP430FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 39 | return !MF.getFrameInfo()->hasVarSizedObjects(); |
| 40 | } |
| 41 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 42 | void MSP430FrameLowering::emitPrologue(MachineFunction &MF) const { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 43 | MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB |
| 44 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 45 | MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 46 | const MSP430InstrInfo &TII = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame^] | 47 | *static_cast<const MSP430InstrInfo *>( |
| 48 | MF.getTarget().getSubtargetImpl()->getInstrInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 49 | |
| 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 Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 57 | if (hasFP(MF)) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 58 | // 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 Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 76 | for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 77 | 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 Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 108 | void MSP430FrameLowering::emitEpilogue(MachineFunction &MF, |
| 109 | MachineBasicBlock &MBB) const { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 110 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 111 | MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 112 | const MSP430InstrInfo &TII = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame^] | 113 | *static_cast<const MSP430InstrInfo *>( |
| 114 | MF.getTarget().getSubtargetImpl()->getInstrInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 115 | |
Jakob Stoklund Olesen | 4bc5e38 | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 116 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 117 | 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 Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 132 | if (hasFP(MF)) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 133 | // 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 Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 144 | MachineBasicBlock::iterator PI = std::prev(MBBI); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 145 | unsigned Opc = PI->getOpcode(); |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 146 | if (Opc != MSP430::POP16r && !PI->isTerminator()) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 147 | 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 Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 180 | |
| 181 | // FIXME: Can we eleminate these in favour of generic code? |
| 182 | bool |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 183 | MSP430FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 184 | 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 Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame^] | 194 | const TargetInstrInfo &TII = |
| 195 | *MF.getTarget().getSubtargetImpl()->getInstrInfo(); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 196 | 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 | |
| 209 | bool |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 210 | MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 211 | MachineBasicBlock::iterator MI, |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 212 | 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 Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame^] | 221 | const TargetInstrInfo &TII = |
| 222 | *MF.getTarget().getSubtargetImpl()->getInstrInfo(); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 223 | |
| 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 Korobeynikov | 0a69176 | 2012-10-17 17:37:11 +0000 | [diff] [blame] | 229 | |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 230 | void MSP430FrameLowering:: |
| 231 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 232 | MachineBasicBlock::iterator I) const { |
| 233 | const MSP430InstrInfo &TII = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame^] | 234 | *static_cast<const MSP430InstrInfo *>( |
| 235 | MF.getTarget().getSubtargetImpl()->getInstrInfo()); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 236 | 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 Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 251 | MachineInstr *New = nullptr; |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 252 | 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 Korobeynikov | 0a69176 | 2012-10-17 17:37:11 +0000 | [diff] [blame] | 293 | void |
Hal Finkel | 5a765fd | 2013-03-14 20:33:40 +0000 | [diff] [blame] | 294 | MSP430FrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF, |
| 295 | RegScavenger *) const { |
Anton Korobeynikov | 0a69176 | 2012-10-17 17:37:11 +0000 | [diff] [blame] | 296 | // Create a frame entry for the FPW register that must be saved. |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 297 | if (hasFP(MF)) { |
Anton Korobeynikov | 0a69176 | 2012-10-17 17:37:11 +0000 | [diff] [blame] | 298 | 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 | } |