1 //===-- Thumb1FrameLowering.cpp - Thumb1 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 Thumb1 implementation of TargetFrameLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "Thumb1FrameLowering.h" 15 #include "ARMMachineFunctionInfo.h" 16 #include "llvm/CodeGen/LivePhysRegs.h" 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" 22 23 using namespace llvm; 24 25 Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget &sti) 26 : ARMFrameLowering(sti) {} 27 28 bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const{ 29 const MachineFrameInfo *FFI = MF.getFrameInfo(); 30 unsigned CFSize = FFI->getMaxCallFrameSize(); 31 // It's not always a good idea to include the call frame as part of the 32 // stack frame. ARM (especially Thumb) has small immediate offset to 33 // address the stack frame. So a large call frame can cause poor codegen 34 // and may even makes it impossible to scavenge a register. 35 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4 36 return false; 37 38 return !MF.getFrameInfo()->hasVarSizedObjects(); 39 } 40 41 static void 42 emitSPUpdate(MachineBasicBlock &MBB, 43 MachineBasicBlock::iterator &MBBI, 44 const TargetInstrInfo &TII, DebugLoc dl, 45 const ThumbRegisterInfo &MRI, 46 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) { 47 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, TII, 48 MRI, MIFlags); 49 } 50 51 52 void Thumb1FrameLowering:: 53 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 54 MachineBasicBlock::iterator I) const { 55 const Thumb1InstrInfo &TII = 56 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo()); 57 const ThumbRegisterInfo *RegInfo = 58 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo()); 59 if (!hasReservedCallFrame(MF)) { 60 // If we have alloca, convert as follows: 61 // ADJCALLSTACKDOWN -> sub, sp, sp, amount 62 // ADJCALLSTACKUP -> add, sp, sp, amount 63 MachineInstr *Old = I; 64 DebugLoc dl = Old->getDebugLoc(); 65 unsigned Amount = Old->getOperand(0).getImm(); 66 if (Amount != 0) { 67 // We need to keep the stack aligned properly. To do this, we round the 68 // amount of space needed for the outgoing arguments up to the next 69 // alignment boundary. 70 unsigned Align = getStackAlignment(); 71 Amount = (Amount+Align-1)/Align*Align; 72 73 // Replace the pseudo instruction with a new instruction... 74 unsigned Opc = Old->getOpcode(); 75 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { 76 emitSPUpdate(MBB, I, TII, dl, *RegInfo, -Amount); 77 } else { 78 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); 79 emitSPUpdate(MBB, I, TII, dl, *RegInfo, Amount); 80 } 81 } 82 } 83 MBB.erase(I); 84 } 85 86 void Thumb1FrameLowering::emitPrologue(MachineFunction &MF, 87 MachineBasicBlock &MBB) const { 88 MachineBasicBlock::iterator MBBI = MBB.begin(); 89 MachineFrameInfo *MFI = MF.getFrameInfo(); 90 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 91 MachineModuleInfo &MMI = MF.getMMI(); 92 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 93 const ThumbRegisterInfo *RegInfo = 94 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo()); 95 const Thumb1InstrInfo &TII = 96 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo()); 97 98 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(); 99 unsigned NumBytes = MFI->getStackSize(); 100 assert(NumBytes >= ArgRegsSaveSize && 101 "ArgRegsSaveSize is included in NumBytes"); 102 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 103 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 104 unsigned FramePtr = RegInfo->getFrameRegister(MF); 105 unsigned BasePtr = RegInfo->getBaseRegister(); 106 int CFAOffset = 0; 107 108 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4. 109 NumBytes = (NumBytes + 3) & ~3; 110 MFI->setStackSize(NumBytes); 111 112 // Determine the sizes of each callee-save spill areas and record which frame 113 // belongs to which callee-save spill areas. 114 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; 115 int FramePtrSpillFI = 0; 116 117 if (ArgRegsSaveSize) { 118 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -ArgRegsSaveSize, 119 MachineInstr::FrameSetup); 120 CFAOffset -= ArgRegsSaveSize; 121 unsigned CFIIndex = MMI.addFrameInst( 122 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); 123 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 124 .addCFIIndex(CFIIndex) 125 .setMIFlags(MachineInstr::FrameSetup); 126 } 127 128 if (!AFI->hasStackFrame()) { 129 if (NumBytes - ArgRegsSaveSize != 0) { 130 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -(NumBytes - ArgRegsSaveSize), 131 MachineInstr::FrameSetup); 132 CFAOffset -= NumBytes - ArgRegsSaveSize; 133 unsigned CFIIndex = MMI.addFrameInst( 134 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); 135 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 136 .addCFIIndex(CFIIndex) 137 .setMIFlags(MachineInstr::FrameSetup); 138 } 139 return; 140 } 141 142 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 143 unsigned Reg = CSI[i].getReg(); 144 int FI = CSI[i].getFrameIdx(); 145 switch (Reg) { 146 case ARM::R8: 147 case ARM::R9: 148 case ARM::R10: 149 case ARM::R11: 150 if (STI.isTargetMachO()) { 151 GPRCS2Size += 4; 152 break; 153 } 154 // fallthrough 155 case ARM::R4: 156 case ARM::R5: 157 case ARM::R6: 158 case ARM::R7: 159 case ARM::LR: 160 if (Reg == FramePtr) 161 FramePtrSpillFI = FI; 162 GPRCS1Size += 4; 163 break; 164 default: 165 DPRCSSize += 8; 166 } 167 } 168 169 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) { 170 ++MBBI; 171 if (MBBI != MBB.end()) 172 dl = MBBI->getDebugLoc(); 173 } 174 175 // Determine starting offsets of spill areas. 176 unsigned DPRCSOffset = NumBytes - ArgRegsSaveSize - (GPRCS1Size + GPRCS2Size + DPRCSSize); 177 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; 178 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; 179 bool HasFP = hasFP(MF); 180 if (HasFP) 181 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + 182 NumBytes); 183 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); 184 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); 185 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); 186 NumBytes = DPRCSOffset; 187 188 int FramePtrOffsetInBlock = 0; 189 unsigned adjustedGPRCS1Size = GPRCS1Size; 190 if (tryFoldSPUpdateIntoPushPop(STI, MF, std::prev(MBBI), NumBytes)) { 191 FramePtrOffsetInBlock = NumBytes; 192 adjustedGPRCS1Size += NumBytes; 193 NumBytes = 0; 194 } 195 196 if (adjustedGPRCS1Size) { 197 CFAOffset -= adjustedGPRCS1Size; 198 unsigned CFIIndex = MMI.addFrameInst( 199 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); 200 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 201 .addCFIIndex(CFIIndex) 202 .setMIFlags(MachineInstr::FrameSetup); 203 } 204 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), 205 E = CSI.end(); I != E; ++I) { 206 unsigned Reg = I->getReg(); 207 int FI = I->getFrameIdx(); 208 switch (Reg) { 209 case ARM::R8: 210 case ARM::R9: 211 case ARM::R10: 212 case ARM::R11: 213 case ARM::R12: 214 if (STI.isTargetMachO()) 215 break; 216 // fallthough 217 case ARM::R0: 218 case ARM::R1: 219 case ARM::R2: 220 case ARM::R3: 221 case ARM::R4: 222 case ARM::R5: 223 case ARM::R6: 224 case ARM::R7: 225 case ARM::LR: 226 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 227 nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI))); 228 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 229 .addCFIIndex(CFIIndex) 230 .setMIFlags(MachineInstr::FrameSetup); 231 break; 232 } 233 } 234 235 236 // Adjust FP so it point to the stack slot that contains the previous FP. 237 if (HasFP) { 238 FramePtrOffsetInBlock += MFI->getObjectOffset(FramePtrSpillFI) 239 + GPRCS1Size + ArgRegsSaveSize; 240 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr) 241 .addReg(ARM::SP).addImm(FramePtrOffsetInBlock / 4) 242 .setMIFlags(MachineInstr::FrameSetup)); 243 if(FramePtrOffsetInBlock) { 244 CFAOffset += FramePtrOffsetInBlock; 245 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa( 246 nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset)); 247 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 248 .addCFIIndex(CFIIndex) 249 .setMIFlags(MachineInstr::FrameSetup); 250 } else { 251 unsigned CFIIndex = 252 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister( 253 nullptr, MRI->getDwarfRegNum(FramePtr, true))); 254 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 255 .addCFIIndex(CFIIndex) 256 .setMIFlags(MachineInstr::FrameSetup); 257 } 258 if (NumBytes > 508) 259 // If offset is > 508 then sp cannot be adjusted in a single instruction, 260 // try restoring from fp instead. 261 AFI->setShouldRestoreSPFromFP(true); 262 } 263 264 if (NumBytes) { 265 // Insert it after all the callee-save spills. 266 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes, 267 MachineInstr::FrameSetup); 268 if (!HasFP) { 269 CFAOffset -= NumBytes; 270 unsigned CFIIndex = MMI.addFrameInst( 271 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); 272 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 273 .addCFIIndex(CFIIndex) 274 .setMIFlags(MachineInstr::FrameSetup); 275 } 276 } 277 278 if (STI.isTargetELF() && HasFP) 279 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - 280 AFI->getFramePtrSpillOffset()); 281 282 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); 283 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); 284 AFI->setDPRCalleeSavedAreaSize(DPRCSSize); 285 286 // Thumb1 does not currently support dynamic stack realignment. Report a 287 // fatal error rather then silently generate bad code. 288 if (RegInfo->needsStackRealignment(MF)) 289 report_fatal_error("Dynamic stack realignment not supported for thumb1."); 290 291 // If we need a base pointer, set it up here. It's whatever the value 292 // of the stack pointer is at this point. Any variable size objects 293 // will be allocated after this, so we can still use the base pointer 294 // to reference locals. 295 if (RegInfo->hasBasePointer(MF)) 296 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), BasePtr) 297 .addReg(ARM::SP)); 298 299 // If the frame has variable sized objects then the epilogue must restore 300 // the sp from fp. We can assume there's an FP here since hasFP already 301 // checks for hasVarSizedObjects. 302 if (MFI->hasVarSizedObjects()) 303 AFI->setShouldRestoreSPFromFP(true); 304 } 305 306 static bool isCSRestore(MachineInstr *MI, const MCPhysReg *CSRegs) { 307 if (MI->getOpcode() == ARM::tLDRspi && 308 MI->getOperand(1).isFI() && 309 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs)) 310 return true; 311 else if (MI->getOpcode() == ARM::tPOP) { 312 // The first two operands are predicates. The last two are 313 // imp-def and imp-use of SP. Check everything in between. 314 for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i) 315 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) 316 return false; 317 return true; 318 } 319 return false; 320 } 321 322 void Thumb1FrameLowering::emitEpilogue(MachineFunction &MF, 323 MachineBasicBlock &MBB) const { 324 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); 325 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 326 MachineFrameInfo *MFI = MF.getFrameInfo(); 327 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 328 const ThumbRegisterInfo *RegInfo = 329 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo()); 330 const Thumb1InstrInfo &TII = 331 *static_cast<const Thumb1InstrInfo *>(STI.getInstrInfo()); 332 333 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(); 334 int NumBytes = (int)MFI->getStackSize(); 335 assert((unsigned)NumBytes >= ArgRegsSaveSize && 336 "ArgRegsSaveSize is included in NumBytes"); 337 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); 338 unsigned FramePtr = RegInfo->getFrameRegister(MF); 339 340 if (!AFI->hasStackFrame()) { 341 if (NumBytes - ArgRegsSaveSize != 0) 342 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes - ArgRegsSaveSize); 343 } else { 344 // Unwind MBBI to point to first LDR / VLDRD. 345 if (MBBI != MBB.begin()) { 346 do 347 --MBBI; 348 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs)); 349 if (!isCSRestore(MBBI, CSRegs)) 350 ++MBBI; 351 } 352 353 // Move SP to start of FP callee save spill area. 354 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + 355 AFI->getGPRCalleeSavedArea2Size() + 356 AFI->getDPRCalleeSavedAreaSize() + 357 ArgRegsSaveSize); 358 359 if (AFI->shouldRestoreSPFromFP()) { 360 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; 361 // Reset SP based on frame pointer only if the stack frame extends beyond 362 // frame pointer stack slot, the target is ELF and the function has FP, or 363 // the target uses var sized objects. 364 if (NumBytes) { 365 assert(!MFI->getPristineRegs(MF).test(ARM::R4) && 366 "No scratch register to restore SP from FP!"); 367 emitThumbRegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, 368 TII, *RegInfo); 369 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 370 ARM::SP) 371 .addReg(ARM::R4)); 372 } else 373 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 374 ARM::SP) 375 .addReg(FramePtr)); 376 } else { 377 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET && 378 &MBB.front() != MBBI && std::prev(MBBI)->getOpcode() == ARM::tPOP) { 379 MachineBasicBlock::iterator PMBBI = std::prev(MBBI); 380 if (!tryFoldSPUpdateIntoPushPop(STI, MF, PMBBI, NumBytes)) 381 emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes); 382 } else if (!tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes)) 383 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes); 384 } 385 } 386 387 if (needPopSpecialFixUp(MF)) { 388 bool Done = emitPopSpecialFixUp(MBB, /* DoIt */ true); 389 (void)Done; 390 assert(Done && "Emission of the special fixup failed!?"); 391 } 392 } 393 394 bool Thumb1FrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const { 395 if (!needPopSpecialFixUp(*MBB.getParent())) 396 return true; 397 398 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB); 399 return emitPopSpecialFixUp(*TmpMBB, /* DoIt */ false); 400 } 401 402 bool Thumb1FrameLowering::needPopSpecialFixUp(const MachineFunction &MF) const { 403 ARMFunctionInfo *AFI = 404 const_cast<MachineFunction *>(&MF)->getInfo<ARMFunctionInfo>(); 405 if (AFI->getArgRegsSaveSize()) 406 return true; 407 408 bool IsV4PopReturn = false; 409 for (const CalleeSavedInfo &CSI : MF.getFrameInfo()->getCalleeSavedInfo()) 410 if (CSI.getReg() == ARM::LR) 411 IsV4PopReturn = true; 412 return IsV4PopReturn && STI.hasV4TOps() && !STI.hasV5TOps(); 413 } 414 415 bool Thumb1FrameLowering::emitPopSpecialFixUp(MachineBasicBlock &MBB, 416 bool DoIt) const { 417 MachineFunction &MF = *MBB.getParent(); 418 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 419 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(); 420 const TargetInstrInfo &TII = *STI.getInstrInfo(); 421 const ThumbRegisterInfo *RegInfo = 422 static_cast<const ThumbRegisterInfo *>(STI.getRegisterInfo()); 423 424 // If MBBI is a return instruction, we may be able to directly restore 425 // LR in the PC. 426 // This is possible if we do not need to emit any SP update. 427 // Otherwise, we need a temporary register to pop the value 428 // and copy that value into LR. 429 auto MBBI = MBB.getFirstTerminator(); 430 if (!ArgRegsSaveSize && MBBI != MBB.end() && 431 MBBI->getOpcode() == ARM::tBX_RET) { 432 if (!DoIt) 433 return true; 434 MachineInstrBuilder MIB = 435 AddDefaultPred( 436 BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII.get(ARM::tPOP_RET))) 437 .addReg(ARM::PC, RegState::Define); 438 MIB.copyImplicitOps(&*MBBI); 439 // erase the old tBX_RET instruction 440 MBB.erase(MBBI); 441 return true; 442 } 443 444 // Look for a temporary register to use. 445 // First, compute the liveness information. 446 LivePhysRegs UsedRegs(STI.getRegisterInfo()); 447 UsedRegs.addLiveOuts(&MBB, /*AddPristines*/ true); 448 // The semantic of pristines changed recently and now, 449 // the callee-saved registers that are touched in the function 450 // are not part of the pristines set anymore. 451 // Add those callee-saved now. 452 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); 453 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF); 454 for (unsigned i = 0; CSRegs[i]; ++i) 455 UsedRegs.addReg(CSRegs[i]); 456 457 DebugLoc dl = DebugLoc(); 458 if (MBBI != MBB.end()) { 459 dl = MBBI->getDebugLoc(); 460 auto InstUpToMBBI = MBB.end(); 461 // The post-decrement is on purpose here. 462 // We want to have the liveness right before MBBI. 463 while (InstUpToMBBI-- != MBBI) 464 UsedRegs.stepBackward(*InstUpToMBBI); 465 } 466 467 // Look for a register that can be directly use in the POP. 468 unsigned PopReg = 0; 469 // And some temporary register, just in case. 470 unsigned TemporaryReg = 0; 471 BitVector PopFriendly = 472 TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::tGPRRegClassID)); 473 assert(PopFriendly.any() && "No allocatable pop-friendly register?!"); 474 // Rebuild the GPRs from the high registers because they are removed 475 // form the GPR reg class for thumb1. 476 BitVector GPRsNoLRSP = 477 TRI->getAllocatableSet(MF, TRI->getRegClass(ARM::hGPRRegClassID)); 478 GPRsNoLRSP |= PopFriendly; 479 GPRsNoLRSP.reset(ARM::LR); 480 GPRsNoLRSP.reset(ARM::SP); 481 GPRsNoLRSP.reset(ARM::PC); 482 for (int Register = GPRsNoLRSP.find_first(); Register != -1; 483 Register = GPRsNoLRSP.find_next(Register)) { 484 if (!UsedRegs.contains(Register)) { 485 // Remember the first pop-friendly register and exit. 486 if (PopFriendly.test(Register)) { 487 PopReg = Register; 488 TemporaryReg = 0; 489 break; 490 } 491 // Otherwise, remember that the register will be available to 492 // save a pop-friendly register. 493 TemporaryReg = Register; 494 } 495 } 496 497 if (!DoIt && !PopReg && !TemporaryReg) 498 return false; 499 500 assert((PopReg || TemporaryReg) && "Cannot get LR"); 501 502 if (TemporaryReg) { 503 assert(!PopReg && "Unnecessary MOV is about to be inserted"); 504 PopReg = PopFriendly.find_first(); 505 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr)) 506 .addReg(TemporaryReg, RegState::Define) 507 .addReg(PopReg, RegState::Kill)); 508 } 509 510 assert(PopReg && "Do not know how to get LR"); 511 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP))) 512 .addReg(PopReg, RegState::Define); 513 514 emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize); 515 516 if (!TemporaryReg && MBBI != MBB.end() && MBBI->getOpcode() == ARM::tBX_RET) { 517 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX)) 518 .addReg(PopReg, RegState::Kill); 519 AddDefaultPred(MIB); 520 MIB.copyImplicitOps(&*MBBI); 521 // erase the old tBX_RET instruction 522 MBB.erase(MBBI); 523 return true; 524 } 525 526 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr)) 527 .addReg(ARM::LR, RegState::Define) 528 .addReg(PopReg, RegState::Kill)); 529 530 if (TemporaryReg) { 531 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr)) 532 .addReg(PopReg, RegState::Define) 533 .addReg(TemporaryReg, RegState::Kill)); 534 } 535 536 return true; 537 } 538 539 bool Thumb1FrameLowering:: 540 spillCalleeSavedRegisters(MachineBasicBlock &MBB, 541 MachineBasicBlock::iterator MI, 542 const std::vector<CalleeSavedInfo> &CSI, 543 const TargetRegisterInfo *TRI) const { 544 if (CSI.empty()) 545 return false; 546 547 DebugLoc DL; 548 const TargetInstrInfo &TII = *STI.getInstrInfo(); 549 550 if (MI != MBB.end()) DL = MI->getDebugLoc(); 551 552 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(ARM::tPUSH)); 553 AddDefaultPred(MIB); 554 for (unsigned i = CSI.size(); i != 0; --i) { 555 unsigned Reg = CSI[i-1].getReg(); 556 bool isKill = true; 557 558 // Add the callee-saved register as live-in unless it's LR and 559 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress 560 // then it's already added to the function and entry block live-in sets. 561 if (Reg == ARM::LR) { 562 MachineFunction &MF = *MBB.getParent(); 563 if (MF.getFrameInfo()->isReturnAddressTaken() && 564 MF.getRegInfo().isLiveIn(Reg)) 565 isKill = false; 566 } 567 568 if (isKill) 569 MBB.addLiveIn(Reg); 570 571 MIB.addReg(Reg, getKillRegState(isKill)); 572 } 573 MIB.setMIFlags(MachineInstr::FrameSetup); 574 return true; 575 } 576 577 bool Thumb1FrameLowering:: 578 restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 579 MachineBasicBlock::iterator MI, 580 const std::vector<CalleeSavedInfo> &CSI, 581 const TargetRegisterInfo *TRI) const { 582 if (CSI.empty()) 583 return false; 584 585 MachineFunction &MF = *MBB.getParent(); 586 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 587 const TargetInstrInfo &TII = *STI.getInstrInfo(); 588 589 bool isVarArg = AFI->getArgRegsSaveSize() > 0; 590 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc(); 591 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(ARM::tPOP)); 592 AddDefaultPred(MIB); 593 594 bool NumRegs = false; 595 for (unsigned i = CSI.size(); i != 0; --i) { 596 unsigned Reg = CSI[i-1].getReg(); 597 if (Reg == ARM::LR && MBB.succ_empty()) { 598 // Special epilogue for vararg functions. See emitEpilogue 599 if (isVarArg) 600 continue; 601 // ARMv4T requires BX, see emitEpilogue 602 if (STI.hasV4TOps() && !STI.hasV5TOps()) 603 continue; 604 Reg = ARM::PC; 605 (*MIB).setDesc(TII.get(ARM::tPOP_RET)); 606 if (MI != MBB.end()) 607 MIB.copyImplicitOps(&*MI); 608 MI = MBB.erase(MI); 609 } 610 MIB.addReg(Reg, getDefRegState(true)); 611 NumRegs = true; 612 } 613 614 // It's illegal to emit pop instruction without operands. 615 if (NumRegs) 616 MBB.insert(MI, &*MIB); 617 else 618 MF.DeleteMachineInstr(MIB); 619 620 return true; 621 } 622