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