Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 1 | //===-- AVRFrameLowering.cpp - AVR Frame Information ----------------------===// |
| 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 | // |
| 10 | // This file contains the AVR implementation of TargetFrameLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "AVRFrameLowering.h" |
| 15 | |
| 16 | #include "AVR.h" |
| 17 | #include "AVRInstrInfo.h" |
| 18 | #include "AVRMachineFunctionInfo.h" |
| 19 | #include "AVRTargetMachine.h" |
| 20 | #include "MCTargetDesc/AVRMCTargetDesc.h" |
| 21 | |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 25 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 27 | #include "llvm/IR/Function.h" |
| 28 | |
Dylan McKay | 7c2d41a | 2016-10-08 01:09:06 +0000 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 31 | namespace llvm { |
| 32 | |
| 33 | AVRFrameLowering::AVRFrameLowering() |
| 34 | : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 1, -2) {} |
| 35 | |
| 36 | bool AVRFrameLowering::canSimplifyCallFramePseudos( |
| 37 | const MachineFunction &MF) const { |
| 38 | // Always simplify call frame pseudo instructions, even when |
| 39 | // hasReservedCallFrame is false. |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | bool AVRFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
| 44 | // Reserve call frame memory in function prologue under the following |
| 45 | // conditions: |
| 46 | // - Y pointer is reserved to be the frame pointer. |
| 47 | // - The function does not contain variable sized objects. |
Dylan McKay | b16b6d5 | 2016-10-08 01:10:31 +0000 | [diff] [blame] | 48 | |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 49 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Dylan McKay | b16b6d5 | 2016-10-08 01:10:31 +0000 | [diff] [blame] | 50 | return hasFP(MF) && !MFI.hasVarSizedObjects(); |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void AVRFrameLowering::emitPrologue(MachineFunction &MF, |
| 54 | MachineBasicBlock &MBB) const { |
| 55 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 56 | CallingConv::ID CallConv = MF.getFunction()->getCallingConv(); |
| 57 | DebugLoc DL = (MBBI != MBB.end()) ? MBBI->getDebugLoc() : DebugLoc(); |
| 58 | const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>(); |
| 59 | const AVRInstrInfo &TII = *STI.getInstrInfo(); |
Dylan McKay | 634339a | 2017-05-02 00:11:34 +0000 | [diff] [blame^] | 60 | bool HasFP = hasFP(MF); |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 61 | |
| 62 | // Interrupt handlers re-enable interrupts in function entry. |
| 63 | if (CallConv == CallingConv::AVR_INTR) { |
| 64 | BuildMI(MBB, MBBI, DL, TII.get(AVR::BSETs)) |
| 65 | .addImm(0x07) |
| 66 | .setMIFlag(MachineInstr::FrameSetup); |
| 67 | } |
| 68 | |
| 69 | // Emit special prologue code to save R1, R0 and SREG in interrupt/signal |
| 70 | // handlers before saving any other registers. |
| 71 | if (CallConv == CallingConv::AVR_INTR || |
| 72 | CallConv == CallingConv::AVR_SIGNAL) { |
Dylan McKay | 634339a | 2017-05-02 00:11:34 +0000 | [diff] [blame^] | 73 | |
| 74 | // Save the frame pointer if we have one. |
| 75 | if (HasFP) { |
| 76 | BuildMI(MBB, MBBI, DL, TII.get(AVR::PUSHWRr)) |
| 77 | .addReg(AVR::R29R28, RegState::Kill) |
| 78 | .setMIFlag(MachineInstr::FrameSetup); |
| 79 | } |
| 80 | |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 81 | BuildMI(MBB, MBBI, DL, TII.get(AVR::PUSHWRr)) |
| 82 | .addReg(AVR::R1R0, RegState::Kill) |
| 83 | .setMIFlag(MachineInstr::FrameSetup); |
Dylan McKay | 634339a | 2017-05-02 00:11:34 +0000 | [diff] [blame^] | 84 | |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 85 | BuildMI(MBB, MBBI, DL, TII.get(AVR::INRdA), AVR::R0) |
| 86 | .addImm(0x3f) |
| 87 | .setMIFlag(MachineInstr::FrameSetup); |
| 88 | BuildMI(MBB, MBBI, DL, TII.get(AVR::PUSHRr)) |
| 89 | .addReg(AVR::R0, RegState::Kill) |
| 90 | .setMIFlag(MachineInstr::FrameSetup); |
| 91 | BuildMI(MBB, MBBI, DL, TII.get(AVR::EORRdRr)) |
| 92 | .addReg(AVR::R0, RegState::Define) |
| 93 | .addReg(AVR::R0, RegState::Kill) |
| 94 | .addReg(AVR::R0, RegState::Kill) |
| 95 | .setMIFlag(MachineInstr::FrameSetup); |
| 96 | } |
| 97 | |
| 98 | // Early exit if the frame pointer is not needed in this function. |
Dylan McKay | 634339a | 2017-05-02 00:11:34 +0000 | [diff] [blame^] | 99 | if (!HasFP) { |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 104 | const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>(); |
| 105 | unsigned FrameSize = MFI.getStackSize() - AFI->getCalleeSavedFrameSize(); |
| 106 | |
| 107 | // Skip the callee-saved push instructions. |
| 108 | while ( |
| 109 | (MBBI != MBB.end()) && MBBI->getFlag(MachineInstr::FrameSetup) && |
| 110 | (MBBI->getOpcode() == AVR::PUSHRr || MBBI->getOpcode() == AVR::PUSHWRr)) { |
| 111 | ++MBBI; |
| 112 | } |
| 113 | |
| 114 | // Update Y with the new base value. |
| 115 | BuildMI(MBB, MBBI, DL, TII.get(AVR::SPREAD), AVR::R29R28) |
| 116 | .addReg(AVR::SP) |
| 117 | .setMIFlag(MachineInstr::FrameSetup); |
| 118 | |
| 119 | // Mark the FramePtr as live-in in every block except the entry. |
| 120 | for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end(); |
| 121 | I != E; ++I) { |
| 122 | I->addLiveIn(AVR::R29R28); |
| 123 | } |
| 124 | |
| 125 | if (!FrameSize) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // Reserve the necessary frame memory by doing FP -= <size>. |
| 130 | unsigned Opcode = (isUInt<6>(FrameSize)) ? AVR::SBIWRdK : AVR::SUBIWRdK; |
| 131 | |
| 132 | MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opcode), AVR::R29R28) |
| 133 | .addReg(AVR::R29R28, RegState::Kill) |
| 134 | .addImm(FrameSize) |
| 135 | .setMIFlag(MachineInstr::FrameSetup); |
| 136 | // The SREG implicit def is dead. |
| 137 | MI->getOperand(3).setIsDead(); |
| 138 | |
| 139 | // Write back R29R28 to SP and temporarily disable interrupts. |
| 140 | BuildMI(MBB, MBBI, DL, TII.get(AVR::SPWRITE), AVR::SP) |
| 141 | .addReg(AVR::R29R28) |
| 142 | .setMIFlag(MachineInstr::FrameSetup); |
| 143 | } |
| 144 | |
| 145 | void AVRFrameLowering::emitEpilogue(MachineFunction &MF, |
| 146 | MachineBasicBlock &MBB) const { |
| 147 | CallingConv::ID CallConv = MF.getFunction()->getCallingConv(); |
| 148 | bool isHandler = (CallConv == CallingConv::AVR_INTR || |
| 149 | CallConv == CallingConv::AVR_SIGNAL); |
| 150 | |
| 151 | // Early exit if the frame pointer is not needed in this function except for |
| 152 | // signal/interrupt handlers where special code generation is required. |
| 153 | if (!hasFP(MF) && !isHandler) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
Dylan McKay | 552b785 | 2016-10-08 01:10:36 +0000 | [diff] [blame] | 158 | assert(MBBI->getDesc().isReturn() && |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 159 | "Can only insert epilog into returning blocks"); |
Dylan McKay | 552b785 | 2016-10-08 01:10:36 +0000 | [diff] [blame] | 160 | |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 161 | DebugLoc DL = MBBI->getDebugLoc(); |
| 162 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 163 | const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>(); |
| 164 | unsigned FrameSize = MFI.getStackSize() - AFI->getCalleeSavedFrameSize(); |
| 165 | const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>(); |
| 166 | const AVRInstrInfo &TII = *STI.getInstrInfo(); |
| 167 | |
| 168 | // Emit special epilogue code to restore R1, R0 and SREG in interrupt/signal |
| 169 | // handlers at the very end of the function, just before reti. |
| 170 | if (isHandler) { |
| 171 | BuildMI(MBB, MBBI, DL, TII.get(AVR::POPRd), AVR::R0); |
| 172 | BuildMI(MBB, MBBI, DL, TII.get(AVR::OUTARr)) |
| 173 | .addImm(0x3f) |
| 174 | .addReg(AVR::R0, RegState::Kill); |
| 175 | BuildMI(MBB, MBBI, DL, TII.get(AVR::POPWRd), AVR::R1R0); |
Dylan McKay | 634339a | 2017-05-02 00:11:34 +0000 | [diff] [blame^] | 176 | |
| 177 | if (hasFP(MF)) |
| 178 | BuildMI(MBB, MBBI, DL, TII.get(AVR::POPWRd), AVR::R29R28); |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Early exit if there is no need to restore the frame pointer. |
| 182 | if (!FrameSize) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | // Skip the callee-saved pop instructions. |
| 187 | while (MBBI != MBB.begin()) { |
| 188 | MachineBasicBlock::iterator PI = std::prev(MBBI); |
| 189 | int Opc = PI->getOpcode(); |
| 190 | |
| 191 | if (Opc != AVR::POPRd && Opc != AVR::POPWRd && !PI->isTerminator()) { |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | --MBBI; |
| 196 | } |
| 197 | |
| 198 | unsigned Opcode; |
| 199 | |
| 200 | // Select the optimal opcode depending on how big it is. |
| 201 | if (isUInt<6>(FrameSize)) { |
| 202 | Opcode = AVR::ADIWRdK; |
| 203 | } else { |
| 204 | Opcode = AVR::SUBIWRdK; |
| 205 | FrameSize = -FrameSize; |
| 206 | } |
| 207 | |
| 208 | // Restore the frame pointer by doing FP += <size>. |
| 209 | MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opcode), AVR::R29R28) |
| 210 | .addReg(AVR::R29R28, RegState::Kill) |
| 211 | .addImm(FrameSize); |
| 212 | // The SREG implicit def is dead. |
| 213 | MI->getOperand(3).setIsDead(); |
| 214 | |
| 215 | // Write back R29R28 to SP and temporarily disable interrupts. |
| 216 | BuildMI(MBB, MBBI, DL, TII.get(AVR::SPWRITE), AVR::SP) |
| 217 | .addReg(AVR::R29R28, RegState::Kill); |
| 218 | } |
| 219 | |
| 220 | // Return true if the specified function should have a dedicated frame |
| 221 | // pointer register. This is true if the function meets any of the following |
| 222 | // conditions: |
| 223 | // - a register has been spilled |
| 224 | // - has allocas |
| 225 | // - input arguments are passed using the stack |
| 226 | // |
| 227 | // Notice that strictly this is not a frame pointer because it contains SP after |
| 228 | // frame allocation instead of having the original SP in function entry. |
| 229 | bool AVRFrameLowering::hasFP(const MachineFunction &MF) const { |
| 230 | const AVRMachineFunctionInfo *FuncInfo = MF.getInfo<AVRMachineFunctionInfo>(); |
| 231 | |
Dylan McKay | 3bb6eb2 | 2017-05-01 23:16:59 +0000 | [diff] [blame] | 232 | // TODO: We do not always need a frame pointer. |
| 233 | // This can be optimised. |
| 234 | return true; |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | bool AVRFrameLowering::spillCalleeSavedRegisters( |
| 238 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 239 | const std::vector<CalleeSavedInfo> &CSI, |
| 240 | const TargetRegisterInfo *TRI) const { |
| 241 | if (CSI.empty()) { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | unsigned CalleeFrameSize = 0; |
| 246 | DebugLoc DL = MBB.findDebugLoc(MI); |
| 247 | MachineFunction &MF = *MBB.getParent(); |
| 248 | const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>(); |
| 249 | const TargetInstrInfo &TII = *STI.getInstrInfo(); |
| 250 | AVRMachineFunctionInfo *AVRFI = MF.getInfo<AVRMachineFunctionInfo>(); |
| 251 | |
| 252 | for (unsigned i = CSI.size(); i != 0; --i) { |
| 253 | unsigned Reg = CSI[i - 1].getReg(); |
| 254 | bool IsNotLiveIn = !MBB.isLiveIn(Reg); |
| 255 | |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 256 | assert(TRI->getRegSizeInBits(*TRI->getMinimalPhysRegClass(Reg)) == 8 && |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 257 | "Invalid register size"); |
| 258 | |
| 259 | // Add the callee-saved register as live-in only if it is not already a |
| 260 | // live-in register, this usually happens with arguments that are passed |
| 261 | // through callee-saved registers. |
| 262 | if (IsNotLiveIn) { |
| 263 | MBB.addLiveIn(Reg); |
| 264 | } |
| 265 | |
| 266 | // Do not kill the register when it is an input argument. |
| 267 | BuildMI(MBB, MI, DL, TII.get(AVR::PUSHRr)) |
| 268 | .addReg(Reg, getKillRegState(IsNotLiveIn)) |
| 269 | .setMIFlag(MachineInstr::FrameSetup); |
| 270 | ++CalleeFrameSize; |
| 271 | } |
| 272 | |
| 273 | AVRFI->setCalleeSavedFrameSize(CalleeFrameSize); |
| 274 | |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | bool AVRFrameLowering::restoreCalleeSavedRegisters( |
| 279 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 280 | const std::vector<CalleeSavedInfo> &CSI, |
| 281 | const TargetRegisterInfo *TRI) const { |
| 282 | if (CSI.empty()) { |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | DebugLoc DL = MBB.findDebugLoc(MI); |
| 287 | const MachineFunction &MF = *MBB.getParent(); |
| 288 | const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>(); |
| 289 | const TargetInstrInfo &TII = *STI.getInstrInfo(); |
| 290 | |
Dylan McKay | 7c2d41a | 2016-10-08 01:09:06 +0000 | [diff] [blame] | 291 | for (const CalleeSavedInfo &CCSI : CSI) { |
| 292 | unsigned Reg = CCSI.getReg(); |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 293 | |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 294 | assert(TRI->getRegSizeInBits(*TRI->getMinimalPhysRegClass(Reg)) == 8 && |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 295 | "Invalid register size"); |
| 296 | |
| 297 | BuildMI(MBB, MI, DL, TII.get(AVR::POPRd), Reg); |
| 298 | } |
| 299 | |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | /// Replace pseudo store instructions that pass arguments through the stack with |
| 304 | /// real instructions. If insertPushes is true then all instructions are |
| 305 | /// replaced with push instructions, otherwise regular std instructions are |
| 306 | /// inserted. |
| 307 | static void fixStackStores(MachineBasicBlock &MBB, |
| 308 | MachineBasicBlock::iterator MI, |
| 309 | const TargetInstrInfo &TII, bool insertPushes) { |
| 310 | const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>(); |
| 311 | const TargetRegisterInfo &TRI = *STI.getRegisterInfo(); |
| 312 | |
| 313 | // Iterate through the BB until we hit a call instruction or we reach the end. |
| 314 | for (auto I = MI, E = MBB.end(); I != E && !I->isCall();) { |
| 315 | MachineBasicBlock::iterator NextMI = std::next(I); |
| 316 | MachineInstr &MI = *I; |
| 317 | unsigned Opcode = I->getOpcode(); |
| 318 | |
| 319 | // Only care of pseudo store instructions where SP is the base pointer. |
| 320 | if (Opcode != AVR::STDSPQRr && Opcode != AVR::STDWSPQRr) { |
| 321 | I = NextMI; |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | assert(MI.getOperand(0).getReg() == AVR::SP && |
| 326 | "Invalid register, should be SP!"); |
| 327 | if (insertPushes) { |
| 328 | // Replace this instruction with a push. |
| 329 | unsigned SrcReg = MI.getOperand(2).getReg(); |
| 330 | bool SrcIsKill = MI.getOperand(2).isKill(); |
| 331 | |
| 332 | // We can't use PUSHWRr here because when expanded the order of the new |
| 333 | // instructions are reversed from what we need. Perform the expansion now. |
| 334 | if (Opcode == AVR::STDWSPQRr) { |
| 335 | BuildMI(MBB, I, MI.getDebugLoc(), TII.get(AVR::PUSHRr)) |
| 336 | .addReg(TRI.getSubReg(SrcReg, AVR::sub_hi), |
| 337 | getKillRegState(SrcIsKill)); |
| 338 | BuildMI(MBB, I, MI.getDebugLoc(), TII.get(AVR::PUSHRr)) |
| 339 | .addReg(TRI.getSubReg(SrcReg, AVR::sub_lo), |
| 340 | getKillRegState(SrcIsKill)); |
| 341 | } else { |
| 342 | BuildMI(MBB, I, MI.getDebugLoc(), TII.get(AVR::PUSHRr)) |
| 343 | .addReg(SrcReg, getKillRegState(SrcIsKill)); |
| 344 | } |
| 345 | |
| 346 | MI.eraseFromParent(); |
| 347 | I = NextMI; |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | // Replace this instruction with a regular store. Use Y as the base |
| 352 | // pointer since it is guaranteed to contain a copy of SP. |
| 353 | unsigned STOpc = |
| 354 | (Opcode == AVR::STDWSPQRr) ? AVR::STDWPtrQRr : AVR::STDPtrQRr; |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 355 | |
| 356 | MI.setDesc(TII.get(STOpc)); |
| 357 | MI.getOperand(0).setReg(AVR::R29R28); |
| 358 | |
| 359 | I = NextMI; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | MachineBasicBlock::iterator AVRFrameLowering::eliminateCallFramePseudoInstr( |
| 364 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 365 | MachineBasicBlock::iterator MI) const { |
| 366 | const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>(); |
| 367 | const TargetFrameLowering &TFI = *STI.getFrameLowering(); |
| 368 | const AVRInstrInfo &TII = *STI.getInstrInfo(); |
| 369 | |
| 370 | // There is nothing to insert when the call frame memory is allocated during |
| 371 | // function entry. Delete the call frame pseudo and replace all pseudo stores |
| 372 | // with real store instructions. |
| 373 | if (TFI.hasReservedCallFrame(MF)) { |
| 374 | fixStackStores(MBB, MI, TII, false); |
| 375 | return MBB.erase(MI); |
| 376 | } |
| 377 | |
| 378 | DebugLoc DL = MI->getDebugLoc(); |
| 379 | unsigned int Opcode = MI->getOpcode(); |
| 380 | int Amount = MI->getOperand(0).getImm(); |
| 381 | |
| 382 | // Adjcallstackup does not need to allocate stack space for the call, instead |
| 383 | // we insert push instructions that will allocate the necessary stack. |
| 384 | // For adjcallstackdown we convert it into an 'adiw reg, <amt>' handling |
| 385 | // the read and write of SP in I/O space. |
| 386 | if (Amount != 0) { |
| 387 | assert(TFI.getStackAlignment() == 1 && "Unsupported stack alignment"); |
| 388 | |
| 389 | if (Opcode == TII.getCallFrameSetupOpcode()) { |
| 390 | fixStackStores(MBB, MI, TII, true); |
| 391 | } else { |
| 392 | assert(Opcode == TII.getCallFrameDestroyOpcode()); |
| 393 | |
| 394 | // Select the best opcode to adjust SP based on the offset size. |
| 395 | unsigned addOpcode; |
| 396 | if (isUInt<6>(Amount)) { |
| 397 | addOpcode = AVR::ADIWRdK; |
| 398 | } else { |
| 399 | addOpcode = AVR::SUBIWRdK; |
| 400 | Amount = -Amount; |
| 401 | } |
| 402 | |
| 403 | // Build the instruction sequence. |
| 404 | BuildMI(MBB, MI, DL, TII.get(AVR::SPREAD), AVR::R31R30).addReg(AVR::SP); |
| 405 | |
| 406 | MachineInstr *New = BuildMI(MBB, MI, DL, TII.get(addOpcode), AVR::R31R30) |
| 407 | .addReg(AVR::R31R30, RegState::Kill) |
| 408 | .addImm(Amount); |
| 409 | New->getOperand(3).setIsDead(); |
| 410 | |
| 411 | BuildMI(MBB, MI, DL, TII.get(AVR::SPWRITE), AVR::SP) |
| 412 | .addReg(AVR::R31R30, RegState::Kill); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return MBB.erase(MI); |
| 417 | } |
| 418 | |
| 419 | void AVRFrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 420 | BitVector &SavedRegs, |
| 421 | RegScavenger *RS) const { |
| 422 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| 423 | |
Dylan McKay | 634339a | 2017-05-02 00:11:34 +0000 | [diff] [blame^] | 424 | // If we have a frame pointer, the Y register needs to be saved as well. |
| 425 | // We don't do that here however - the prologue and epilogue generation |
| 426 | // code will handle it specially. |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 427 | } |
| 428 | /// The frame analyzer pass. |
| 429 | /// |
| 430 | /// Scans the function for allocas and used arguments |
| 431 | /// that are passed through the stack. |
| 432 | struct AVRFrameAnalyzer : public MachineFunctionPass { |
| 433 | static char ID; |
| 434 | AVRFrameAnalyzer() : MachineFunctionPass(ID) {} |
| 435 | |
| 436 | bool runOnMachineFunction(MachineFunction &MF) { |
| 437 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 438 | AVRMachineFunctionInfo *FuncInfo = MF.getInfo<AVRMachineFunctionInfo>(); |
| 439 | |
| 440 | // If there are no fixed frame indexes during this stage it means there |
| 441 | // are allocas present in the function. |
| 442 | if (MFI.getNumObjects() != MFI.getNumFixedObjects()) { |
| 443 | // Check for the type of allocas present in the function. We only care |
| 444 | // about fixed size allocas so do not give false positives if only |
| 445 | // variable sized allocas are present. |
| 446 | for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) { |
| 447 | // Variable sized objects have size 0. |
| 448 | if (MFI.getObjectSize(i)) { |
| 449 | FuncInfo->setHasAllocas(true); |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // If there are fixed frame indexes present, scan the function to see if |
| 456 | // they are really being used. |
| 457 | if (MFI.getNumFixedObjects() == 0) { |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | // Ok fixed frame indexes present, now scan the function to see if they |
| 462 | // are really being used, otherwise we can ignore them. |
| 463 | for (const MachineBasicBlock &BB : MF) { |
| 464 | for (const MachineInstr &MI : BB) { |
| 465 | int Opcode = MI.getOpcode(); |
| 466 | |
| 467 | if ((Opcode != AVR::LDDRdPtrQ) && (Opcode != AVR::LDDWRdPtrQ) && |
| 468 | (Opcode != AVR::STDPtrQRr) && (Opcode != AVR::STDWPtrQRr)) { |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | for (const MachineOperand &MO : MI.operands()) { |
| 473 | if (!MO.isFI()) { |
| 474 | continue; |
| 475 | } |
| 476 | |
| 477 | if (MFI.isFixedObjectIndex(MO.getIndex())) { |
| 478 | FuncInfo->setHasStackArgs(true); |
| 479 | return false; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return false; |
| 486 | } |
| 487 | |
Dylan McKay | ea55554 | 2016-10-05 12:32:24 +0000 | [diff] [blame] | 488 | StringRef getPassName() const { return "AVR Frame Analyzer"; } |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | char AVRFrameAnalyzer::ID = 0; |
| 492 | |
| 493 | /// Creates instance of the frame analyzer pass. |
| 494 | FunctionPass *createAVRFrameAnalyzerPass() { return new AVRFrameAnalyzer(); } |
| 495 | |
| 496 | /// Create the Dynalloca Stack Pointer Save/Restore pass. |
| 497 | /// Insert a copy of SP before allocating the dynamic stack memory and restore |
| 498 | /// it in function exit to restore the original SP state. This avoids the need |
| 499 | /// of reserving a register pair for a frame pointer. |
| 500 | struct AVRDynAllocaSR : public MachineFunctionPass { |
| 501 | static char ID; |
| 502 | AVRDynAllocaSR() : MachineFunctionPass(ID) {} |
| 503 | |
| 504 | bool runOnMachineFunction(MachineFunction &MF) { |
| 505 | // Early exit when there are no variable sized objects in the function. |
| 506 | if (!MF.getFrameInfo().hasVarSizedObjects()) { |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>(); |
| 511 | const TargetInstrInfo &TII = *STI.getInstrInfo(); |
| 512 | MachineBasicBlock &EntryMBB = MF.front(); |
| 513 | MachineBasicBlock::iterator MBBI = EntryMBB.begin(); |
| 514 | DebugLoc DL = EntryMBB.findDebugLoc(MBBI); |
| 515 | |
| 516 | unsigned SPCopy = |
| 517 | MF.getRegInfo().createVirtualRegister(&AVR::DREGSRegClass); |
| 518 | |
| 519 | // Create a copy of SP in function entry before any dynallocas are |
| 520 | // inserted. |
| 521 | BuildMI(EntryMBB, MBBI, DL, TII.get(AVR::COPY), SPCopy).addReg(AVR::SP); |
| 522 | |
| 523 | // Restore SP in all exit basic blocks. |
| 524 | for (MachineBasicBlock &MBB : MF) { |
| 525 | // If last instruction is a return instruction, add a restore copy. |
| 526 | if (!MBB.empty() && MBB.back().isReturn()) { |
| 527 | MBBI = MBB.getLastNonDebugInstr(); |
| 528 | DL = MBBI->getDebugLoc(); |
| 529 | BuildMI(MBB, MBBI, DL, TII.get(AVR::COPY), AVR::SP) |
| 530 | .addReg(SPCopy, RegState::Kill); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return true; |
| 535 | } |
| 536 | |
Dylan McKay | ea55554 | 2016-10-05 12:32:24 +0000 | [diff] [blame] | 537 | StringRef getPassName() const { |
Dylan McKay | 192405a | 2016-10-05 11:48:56 +0000 | [diff] [blame] | 538 | return "AVR dynalloca stack pointer save/restore"; |
| 539 | } |
| 540 | }; |
| 541 | |
| 542 | char AVRDynAllocaSR::ID = 0; |
| 543 | |
| 544 | /// createAVRDynAllocaSRPass - returns an instance of the dynalloca stack |
| 545 | /// pointer save/restore pass. |
| 546 | FunctionPass *createAVRDynAllocaSRPass() { return new AVRDynAllocaSR(); } |
| 547 | |
| 548 | } // end of namespace llvm |
| 549 | |