1 //=======- ARMFrameLowering.cpp - ARM Frame Information --------*- C++ -*-====// 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 ARM implementation of TargetFrameLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ARMFrameLowering.h" 15 #include "ARMBaseInstrInfo.h" 16 #include "ARMBaseRegisterInfo.h" 17 #include "ARMMachineFunctionInfo.h" 18 #include "MCTargetDesc/ARMAddressingModes.h" 19 #include "llvm/CodeGen/MachineFrameInfo.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/RegisterScavenging.h" 24 #include "llvm/Target/TargetOptions.h" 25 26 using namespace llvm; 27 28 /// hasFP - Return true if the specified function should have a dedicated frame 29 /// pointer register. This is true if the function has variable sized allocas 30 /// or if frame pointer elimination is disabled. 31 bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { 32 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 33 34 // Mac OS X requires FP not to be clobbered for backtracing purpose. 35 if (STI.isTargetDarwin()) 36 return true; 37 38 const MachineFrameInfo *MFI = MF.getFrameInfo(); 39 // Always eliminate non-leaf frame pointers. 40 return ((MF.getTarget().Options.DisableFramePointerElim(MF) && 41 MFI->hasCalls()) || 42 RegInfo->needsStackRealignment(MF) || 43 MFI->hasVarSizedObjects() || 44 MFI->isFrameAddressTaken()); 45 } 46 47 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is 48 /// not required, we reserve argument space for call sites in the function 49 /// immediately on entry to the current function. This eliminates the need for 50 /// add/sub sp brackets around call sites. Returns true if the call frame is 51 /// included as part of the stack frame. 52 bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 53 const MachineFrameInfo *FFI = MF.getFrameInfo(); 54 unsigned CFSize = FFI->getMaxCallFrameSize(); 55 // It's not always a good idea to include the call frame as part of the 56 // stack frame. ARM (especially Thumb) has small immediate offset to 57 // address the stack frame. So a large call frame can cause poor codegen 58 // and may even makes it impossible to scavenge a register. 59 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 60 return false; 61 62 return !MF.getFrameInfo()->hasVarSizedObjects(); 63 } 64 65 /// canSimplifyCallFramePseudos - If there is a reserved call frame, the 66 /// call frame pseudos can be simplified. Unlike most targets, having a FP 67 /// is not sufficient here since we still may reference some objects via SP 68 /// even when FP is available in Thumb2 mode. 69 bool 70 ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { 71 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); 72 } 73 74 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) { 75 for (unsigned i = 0; CSRegs[i]; ++i) 76 if (Reg == CSRegs[i]) 77 return true; 78 return false; 79 } 80 81 static bool isCSRestore(MachineInstr *MI, 82 const ARMBaseInstrInfo &TII, 83 const unsigned *CSRegs) { 84 // Integer spill area is handled with "pop". 85 if (MI->getOpcode() == ARM::LDMIA_RET || 86 MI->getOpcode() == ARM::t2LDMIA_RET || 87 MI->getOpcode() == ARM::LDMIA_UPD || 88 MI->getOpcode() == ARM::t2LDMIA_UPD || 89 MI->getOpcode() == ARM::VLDMDIA_UPD) { 90 // The first two operands are predicates. The last two are 91 // imp-def and imp-use of SP. Check everything in between. 92 for (int i = 5, e = MI->getNumOperands(); i != e; ++i) 93 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) 94 return false; 95 return true; 96 } 97 if ((MI->getOpcode() == ARM::LDR_POST_IMM || 98 MI->getOpcode() == ARM::LDR_POST_REG || 99 MI->getOpcode() == ARM::t2LDR_POST) && 100 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && 101 MI->getOperand(1).getReg() == ARM::SP) 102 return true; 103 104 return false; 105 } 106 107 static void 108 emitSPUpdate(bool isARM, 109 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, 110 DebugLoc dl, const ARMBaseInstrInfo &TII, 111 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) { 112 if (isARM) 113 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, 114 ARMCC::AL, 0, TII, MIFlags); 115 else 116 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, 117 ARMCC::AL, 0, TII, MIFlags); 118 } 119 120 void ARMFrameLowering::emitPrologue(MachineFunction &MF) const { 121 MachineBasicBlock &MBB = MF.front(); 122 MachineBasicBlock::iterator MBBI = MBB.begin(); 123 MachineFrameInfo *MFI = MF.getFrameInfo(); 124 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 125 const ARMBaseRegisterInfo *RegInfo = 126 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 127 const ARMBaseInstrInfo &TII = 128 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 129 assert(!AFI->isThumb1OnlyFunction() && 130 "This emitPrologue does not support Thumb1!"); 131 bool isARM = !AFI->isThumbFunction(); 132 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); 133 unsigned NumBytes = MFI->getStackSize(); 134 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 135 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 136 unsigned FramePtr = RegInfo->getFrameRegister(MF); 137 138 // Determine the sizes of each callee-save spill areas and record which frame 139 // belongs to which callee-save spill areas. 140 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; 141 int FramePtrSpillFI = 0; 142 143 // Allocate the vararg register save area. This is not counted in NumBytes. 144 if (VARegSaveSize) 145 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize, 146 MachineInstr::FrameSetup); 147 148 if (!AFI->hasStackFrame()) { 149 if (NumBytes != 0) 150 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 151 MachineInstr::FrameSetup); 152 return; 153 } 154 155 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 156 unsigned Reg = CSI[i].getReg(); 157 int FI = CSI[i].getFrameIdx(); 158 switch (Reg) { 159 case ARM::R4: 160 case ARM::R5: 161 case ARM::R6: 162 case ARM::R7: 163 case ARM::LR: 164 if (Reg == FramePtr) 165 FramePtrSpillFI = FI; 166 AFI->addGPRCalleeSavedArea1Frame(FI); 167 GPRCS1Size += 4; 168 break; 169 case ARM::R8: 170 case ARM::R9: 171 case ARM::R10: 172 case ARM::R11: 173 if (Reg == FramePtr) 174 FramePtrSpillFI = FI; 175 if (STI.isTargetDarwin()) { 176 AFI->addGPRCalleeSavedArea2Frame(FI); 177 GPRCS2Size += 4; 178 } else { 179 AFI->addGPRCalleeSavedArea1Frame(FI); 180 GPRCS1Size += 4; 181 } 182 break; 183 default: 184 AFI->addDPRCalleeSavedAreaFrame(FI); 185 DPRCSSize += 8; 186 } 187 } 188 189 // Move past area 1. 190 if (GPRCS1Size > 0) MBBI++; 191 192 // Set FP to point to the stack slot that contains the previous FP. 193 // For Darwin, FP is R7, which has now been stored in spill area 1. 194 // Otherwise, if this is not Darwin, all the callee-saved registers go 195 // into spill area 1, including the FP in R11. In either case, it is 196 // now safe to emit this assignment. 197 bool HasFP = hasFP(MF); 198 if (HasFP) { 199 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri; 200 MachineInstrBuilder MIB = 201 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr) 202 .addFrameIndex(FramePtrSpillFI).addImm(0) 203 .setMIFlag(MachineInstr::FrameSetup); 204 AddDefaultCC(AddDefaultPred(MIB)); 205 } 206 207 // Move past area 2. 208 if (GPRCS2Size > 0) MBBI++; 209 210 // Determine starting offsets of spill areas. 211 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize); 212 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; 213 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; 214 if (HasFP) 215 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + 216 NumBytes); 217 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); 218 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); 219 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); 220 221 // Move past area 3. 222 if (DPRCSSize > 0) { 223 MBBI++; 224 // Since vpush register list cannot have gaps, there may be multiple vpush 225 // instructions in the prologue. 226 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) 227 MBBI++; 228 } 229 230 NumBytes = DPRCSOffset; 231 if (NumBytes) { 232 // Adjust SP after all the callee-save spills. 233 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 234 MachineInstr::FrameSetup); 235 if (HasFP && isARM) 236 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 237 // Note it's not safe to do this in Thumb2 mode because it would have 238 // taken two instructions: 239 // mov sp, r7 240 // sub sp, #24 241 // If an interrupt is taken between the two instructions, then sp is in 242 // an inconsistent state (pointing to the middle of callee-saved area). 243 // The interrupt handler can end up clobbering the registers. 244 AFI->setShouldRestoreSPFromFP(true); 245 } 246 247 if (STI.isTargetELF() && hasFP(MF)) 248 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - 249 AFI->getFramePtrSpillOffset()); 250 251 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); 252 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); 253 AFI->setDPRCalleeSavedAreaSize(DPRCSSize); 254 255 // If we need dynamic stack realignment, do it here. Be paranoid and make 256 // sure if we also have VLAs, we have a base pointer for frame access. 257 if (RegInfo->needsStackRealignment(MF)) { 258 unsigned MaxAlign = MFI->getMaxAlignment(); 259 assert (!AFI->isThumb1OnlyFunction()); 260 if (!AFI->isThumbFunction()) { 261 // Emit bic sp, sp, MaxAlign 262 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 263 TII.get(ARM::BICri), ARM::SP) 264 .addReg(ARM::SP, RegState::Kill) 265 .addImm(MaxAlign-1))); 266 } else { 267 // We cannot use sp as source/dest register here, thus we're emitting the 268 // following sequence: 269 // mov r4, sp 270 // bic r4, r4, MaxAlign 271 // mov sp, r4 272 // FIXME: It will be better just to find spare register here. 273 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) 274 .addReg(ARM::SP, RegState::Kill)); 275 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 276 TII.get(ARM::t2BICri), ARM::R4) 277 .addReg(ARM::R4, RegState::Kill) 278 .addImm(MaxAlign-1))); 279 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) 280 .addReg(ARM::R4, RegState::Kill)); 281 } 282 283 AFI->setShouldRestoreSPFromFP(true); 284 } 285 286 // If we need a base pointer, set it up here. It's whatever the value 287 // of the stack pointer is at this point. Any variable size objects 288 // will be allocated after this, so we can still use the base pointer 289 // to reference locals. 290 // FIXME: Clarify FrameSetup flags here. 291 if (RegInfo->hasBasePointer(MF)) { 292 if (isARM) 293 BuildMI(MBB, MBBI, dl, 294 TII.get(ARM::MOVr), RegInfo->getBaseRegister()) 295 .addReg(ARM::SP) 296 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 297 else 298 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 299 RegInfo->getBaseRegister()) 300 .addReg(ARM::SP)); 301 } 302 303 // If the frame has variable sized objects then the epilogue must restore 304 // the sp from fp. We can assume there's an FP here since hasFP already 305 // checks for hasVarSizedObjects. 306 if (MFI->hasVarSizedObjects()) 307 AFI->setShouldRestoreSPFromFP(true); 308 } 309 310 void ARMFrameLowering::emitEpilogue(MachineFunction &MF, 311 MachineBasicBlock &MBB) const { 312 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 313 assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); 314 unsigned RetOpcode = MBBI->getOpcode(); 315 DebugLoc dl = MBBI->getDebugLoc(); 316 MachineFrameInfo *MFI = MF.getFrameInfo(); 317 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 318 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 319 const ARMBaseInstrInfo &TII = 320 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 321 assert(!AFI->isThumb1OnlyFunction() && 322 "This emitEpilogue does not support Thumb1!"); 323 bool isARM = !AFI->isThumbFunction(); 324 325 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); 326 int NumBytes = (int)MFI->getStackSize(); 327 unsigned FramePtr = RegInfo->getFrameRegister(MF); 328 329 if (!AFI->hasStackFrame()) { 330 if (NumBytes != 0) 331 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 332 } else { 333 // Unwind MBBI to point to first LDR / VLDRD. 334 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); 335 if (MBBI != MBB.begin()) { 336 do 337 --MBBI; 338 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); 339 if (!isCSRestore(MBBI, TII, CSRegs)) 340 ++MBBI; 341 } 342 343 // Move SP to start of FP callee save spill area. 344 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + 345 AFI->getGPRCalleeSavedArea2Size() + 346 AFI->getDPRCalleeSavedAreaSize()); 347 348 // Reset SP based on frame pointer only if the stack frame extends beyond 349 // frame pointer stack slot or target is ELF and the function has FP. 350 if (AFI->shouldRestoreSPFromFP()) { 351 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; 352 if (NumBytes) { 353 if (isARM) 354 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, 355 ARMCC::AL, 0, TII); 356 else { 357 // It's not possible to restore SP from FP in a single instruction. 358 // For Darwin, this looks like: 359 // mov sp, r7 360 // sub sp, #24 361 // This is bad, if an interrupt is taken after the mov, sp is in an 362 // inconsistent state. 363 // Use the first callee-saved register as a scratch register. 364 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && 365 "No scratch register to restore SP from FP!"); 366 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, 367 ARMCC::AL, 0, TII); 368 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 369 ARM::SP) 370 .addReg(ARM::R4)); 371 } 372 } else { 373 // Thumb2 or ARM. 374 if (isARM) 375 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) 376 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 377 else 378 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 379 ARM::SP) 380 .addReg(FramePtr)); 381 } 382 } else if (NumBytes) 383 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 384 385 // Increment past our save areas. 386 if (AFI->getDPRCalleeSavedAreaSize()) { 387 MBBI++; 388 // Since vpop register list cannot have gaps, there may be multiple vpop 389 // instructions in the epilogue. 390 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) 391 MBBI++; 392 } 393 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; 394 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; 395 } 396 397 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND || 398 RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) { 399 // Tail call return: adjust the stack pointer and jump to callee. 400 MBBI = MBB.getLastNonDebugInstr(); 401 MachineOperand &JumpTarget = MBBI->getOperand(0); 402 403 // Jump to label or value in register. 404 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND) { 405 unsigned TCOpcode = (RetOpcode == ARM::TCRETURNdi) 406 ? (STI.isThumb() ? ARM::tTAILJMPd : ARM::TAILJMPd) 407 : (STI.isThumb() ? ARM::tTAILJMPdND : ARM::TAILJMPdND); 408 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); 409 if (JumpTarget.isGlobal()) 410 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), 411 JumpTarget.getTargetFlags()); 412 else { 413 assert(JumpTarget.isSymbol()); 414 MIB.addExternalSymbol(JumpTarget.getSymbolName(), 415 JumpTarget.getTargetFlags()); 416 } 417 418 // Add the default predicate in Thumb mode. 419 if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0); 420 } else if (RetOpcode == ARM::TCRETURNri) { 421 BuildMI(MBB, MBBI, dl, 422 TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). 423 addReg(JumpTarget.getReg(), RegState::Kill); 424 } else if (RetOpcode == ARM::TCRETURNriND) { 425 BuildMI(MBB, MBBI, dl, 426 TII.get(STI.isThumb() ? ARM::tTAILJMPrND : ARM::TAILJMPrND)). 427 addReg(JumpTarget.getReg(), RegState::Kill); 428 } 429 430 MachineInstr *NewMI = prior(MBBI); 431 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) 432 NewMI->addOperand(MBBI->getOperand(i)); 433 434 // Delete the pseudo instruction TCRETURN. 435 MBB.erase(MBBI); 436 MBBI = NewMI; 437 } 438 439 if (VARegSaveSize) 440 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize); 441 } 442 443 /// getFrameIndexReference - Provide a base+offset reference to an FI slot for 444 /// debug info. It's the same as what we use for resolving the code-gen 445 /// references for now. FIXME: This can go wrong when references are 446 /// SP-relative and simple call frames aren't used. 447 int 448 ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 449 unsigned &FrameReg) const { 450 return ResolveFrameIndexReference(MF, FI, FrameReg, 0); 451 } 452 453 int 454 ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, 455 int FI, unsigned &FrameReg, 456 int SPAdj) const { 457 const MachineFrameInfo *MFI = MF.getFrameInfo(); 458 const ARMBaseRegisterInfo *RegInfo = 459 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 460 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 461 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); 462 int FPOffset = Offset - AFI->getFramePtrSpillOffset(); 463 bool isFixed = MFI->isFixedObjectIndex(FI); 464 465 FrameReg = ARM::SP; 466 Offset += SPAdj; 467 if (AFI->isGPRCalleeSavedArea1Frame(FI)) 468 return Offset - AFI->getGPRCalleeSavedArea1Offset(); 469 else if (AFI->isGPRCalleeSavedArea2Frame(FI)) 470 return Offset - AFI->getGPRCalleeSavedArea2Offset(); 471 else if (AFI->isDPRCalleeSavedAreaFrame(FI)) 472 return Offset - AFI->getDPRCalleeSavedAreaOffset(); 473 474 // When dynamically realigning the stack, use the frame pointer for 475 // parameters, and the stack/base pointer for locals. 476 if (RegInfo->needsStackRealignment(MF)) { 477 assert (hasFP(MF) && "dynamic stack realignment without a FP!"); 478 if (isFixed) { 479 FrameReg = RegInfo->getFrameRegister(MF); 480 Offset = FPOffset; 481 } else if (MFI->hasVarSizedObjects()) { 482 assert(RegInfo->hasBasePointer(MF) && 483 "VLAs and dynamic stack alignment, but missing base pointer!"); 484 FrameReg = RegInfo->getBaseRegister(); 485 } 486 return Offset; 487 } 488 489 // If there is a frame pointer, use it when we can. 490 if (hasFP(MF) && AFI->hasStackFrame()) { 491 // Use frame pointer to reference fixed objects. Use it for locals if 492 // there are VLAs (and thus the SP isn't reliable as a base). 493 if (isFixed || (MFI->hasVarSizedObjects() && 494 !RegInfo->hasBasePointer(MF))) { 495 FrameReg = RegInfo->getFrameRegister(MF); 496 return FPOffset; 497 } else if (MFI->hasVarSizedObjects()) { 498 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); 499 if (AFI->isThumb2Function()) { 500 // Try to use the frame pointer if we can, else use the base pointer 501 // since it's available. This is handy for the emergency spill slot, in 502 // particular. 503 if (FPOffset >= -255 && FPOffset < 0) { 504 FrameReg = RegInfo->getFrameRegister(MF); 505 return FPOffset; 506 } 507 } 508 } else if (AFI->isThumb2Function()) { 509 // Use add <rd>, sp, #<imm8> 510 // ldr <rd>, [sp, #<imm8>] 511 // if at all possible to save space. 512 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) 513 return Offset; 514 // In Thumb2 mode, the negative offset is very limited. Try to avoid 515 // out of range references. ldr <rt>,[<rn>, #-<imm8>] 516 if (FPOffset >= -255 && FPOffset < 0) { 517 FrameReg = RegInfo->getFrameRegister(MF); 518 return FPOffset; 519 } 520 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { 521 // Otherwise, use SP or FP, whichever is closer to the stack slot. 522 FrameReg = RegInfo->getFrameRegister(MF); 523 return FPOffset; 524 } 525 } 526 // Use the base pointer if we have one. 527 if (RegInfo->hasBasePointer(MF)) 528 FrameReg = RegInfo->getBaseRegister(); 529 return Offset; 530 } 531 532 int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, 533 int FI) const { 534 unsigned FrameReg; 535 return getFrameIndexReference(MF, FI, FrameReg); 536 } 537 538 void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, 539 MachineBasicBlock::iterator MI, 540 const std::vector<CalleeSavedInfo> &CSI, 541 unsigned StmOpc, unsigned StrOpc, 542 bool NoGap, 543 bool(*Func)(unsigned, bool), 544 unsigned MIFlags) const { 545 MachineFunction &MF = *MBB.getParent(); 546 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 547 548 DebugLoc DL; 549 if (MI != MBB.end()) DL = MI->getDebugLoc(); 550 551 SmallVector<std::pair<unsigned,bool>, 4> Regs; 552 unsigned i = CSI.size(); 553 while (i != 0) { 554 unsigned LastReg = 0; 555 for (; i != 0; --i) { 556 unsigned Reg = CSI[i-1].getReg(); 557 if (!(Func)(Reg, STI.isTargetDarwin())) continue; 558 559 // Add the callee-saved register as live-in unless it's LR and 560 // @llvm.returnaddress is called. If LR is returned for 561 // @llvm.returnaddress then it's already added to the function and 562 // entry block live-in sets. 563 bool isKill = true; 564 if (Reg == ARM::LR) { 565 if (MF.getFrameInfo()->isReturnAddressTaken() && 566 MF.getRegInfo().isLiveIn(Reg)) 567 isKill = false; 568 } 569 570 if (isKill) 571 MBB.addLiveIn(Reg); 572 573 // If NoGap is true, push consecutive registers and then leave the rest 574 // for other instructions. e.g. 575 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} 576 if (NoGap && LastReg && LastReg != Reg-1) 577 break; 578 LastReg = Reg; 579 Regs.push_back(std::make_pair(Reg, isKill)); 580 } 581 582 if (Regs.empty()) 583 continue; 584 if (Regs.size() > 1 || StrOpc== 0) { 585 MachineInstrBuilder MIB = 586 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) 587 .addReg(ARM::SP).setMIFlags(MIFlags)); 588 for (unsigned i = 0, e = Regs.size(); i < e; ++i) 589 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); 590 } else if (Regs.size() == 1) { 591 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), 592 ARM::SP) 593 .addReg(Regs[0].first, getKillRegState(Regs[0].second)) 594 .addReg(ARM::SP).setMIFlags(MIFlags) 595 .addImm(-4); 596 AddDefaultPred(MIB); 597 } 598 Regs.clear(); 599 } 600 } 601 602 void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, 603 MachineBasicBlock::iterator MI, 604 const std::vector<CalleeSavedInfo> &CSI, 605 unsigned LdmOpc, unsigned LdrOpc, 606 bool isVarArg, bool NoGap, 607 bool(*Func)(unsigned, bool)) const { 608 MachineFunction &MF = *MBB.getParent(); 609 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 610 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 611 DebugLoc DL = MI->getDebugLoc(); 612 unsigned RetOpcode = MI->getOpcode(); 613 bool isTailCall = (RetOpcode == ARM::TCRETURNdi || 614 RetOpcode == ARM::TCRETURNdiND || 615 RetOpcode == ARM::TCRETURNri || 616 RetOpcode == ARM::TCRETURNriND); 617 618 SmallVector<unsigned, 4> Regs; 619 unsigned i = CSI.size(); 620 while (i != 0) { 621 unsigned LastReg = 0; 622 bool DeleteRet = false; 623 for (; i != 0; --i) { 624 unsigned Reg = CSI[i-1].getReg(); 625 if (!(Func)(Reg, STI.isTargetDarwin())) continue; 626 627 if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) { 628 Reg = ARM::PC; 629 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; 630 // Fold the return instruction into the LDM. 631 DeleteRet = true; 632 } 633 634 // If NoGap is true, pop consecutive registers and then leave the rest 635 // for other instructions. e.g. 636 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} 637 if (NoGap && LastReg && LastReg != Reg-1) 638 break; 639 640 LastReg = Reg; 641 Regs.push_back(Reg); 642 } 643 644 if (Regs.empty()) 645 continue; 646 if (Regs.size() > 1 || LdrOpc == 0) { 647 MachineInstrBuilder MIB = 648 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) 649 .addReg(ARM::SP)); 650 for (unsigned i = 0, e = Regs.size(); i < e; ++i) 651 MIB.addReg(Regs[i], getDefRegState(true)); 652 if (DeleteRet) { 653 MIB->copyImplicitOps(&*MI); 654 MI->eraseFromParent(); 655 } 656 MI = MIB; 657 } else if (Regs.size() == 1) { 658 // If we adjusted the reg to PC from LR above, switch it back here. We 659 // only do that for LDM. 660 if (Regs[0] == ARM::PC) 661 Regs[0] = ARM::LR; 662 MachineInstrBuilder MIB = 663 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) 664 .addReg(ARM::SP, RegState::Define) 665 .addReg(ARM::SP); 666 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once 667 // that refactoring is complete (eventually). 668 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { 669 MIB.addReg(0); 670 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); 671 } else 672 MIB.addImm(4); 673 AddDefaultPred(MIB); 674 } 675 Regs.clear(); 676 } 677 } 678 679 bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 680 MachineBasicBlock::iterator MI, 681 const std::vector<CalleeSavedInfo> &CSI, 682 const TargetRegisterInfo *TRI) const { 683 if (CSI.empty()) 684 return false; 685 686 MachineFunction &MF = *MBB.getParent(); 687 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 688 689 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; 690 unsigned PushOneOpc = AFI->isThumbFunction() ? 691 ARM::t2STR_PRE : ARM::STR_PRE_IMM; 692 unsigned FltOpc = ARM::VSTMDDB_UPD; 693 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 694 MachineInstr::FrameSetup); 695 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 696 MachineInstr::FrameSetup); 697 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, 698 MachineInstr::FrameSetup); 699 700 return true; 701 } 702 703 bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 704 MachineBasicBlock::iterator MI, 705 const std::vector<CalleeSavedInfo> &CSI, 706 const TargetRegisterInfo *TRI) const { 707 if (CSI.empty()) 708 return false; 709 710 MachineFunction &MF = *MBB.getParent(); 711 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 712 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0; 713 714 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; 715 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM; 716 unsigned FltOpc = ARM::VLDMDIA_UPD; 717 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register); 718 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 719 &isARMArea2Register); 720 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 721 &isARMArea1Register); 722 723 return true; 724 } 725 726 // FIXME: Make generic? 727 static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, 728 const ARMBaseInstrInfo &TII) { 729 unsigned FnSize = 0; 730 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); 731 MBBI != E; ++MBBI) { 732 const MachineBasicBlock &MBB = *MBBI; 733 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); 734 I != E; ++I) 735 FnSize += TII.GetInstSizeInBytes(I); 736 } 737 return FnSize; 738 } 739 740 /// estimateStackSize - Estimate and return the size of the frame. 741 /// FIXME: Make generic? 742 static unsigned estimateStackSize(MachineFunction &MF) { 743 const MachineFrameInfo *MFI = MF.getFrameInfo(); 744 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 745 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 746 unsigned MaxAlign = MFI->getMaxAlignment(); 747 int Offset = 0; 748 749 // This code is very, very similar to PEI::calculateFrameObjectOffsets(). 750 // It really should be refactored to share code. Until then, changes 751 // should keep in mind that there's tight coupling between the two. 752 753 for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) { 754 int FixedOff = -MFI->getObjectOffset(i); 755 if (FixedOff > Offset) Offset = FixedOff; 756 } 757 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { 758 if (MFI->isDeadObjectIndex(i)) 759 continue; 760 Offset += MFI->getObjectSize(i); 761 unsigned Align = MFI->getObjectAlignment(i); 762 // Adjust to alignment boundary 763 Offset = (Offset+Align-1)/Align*Align; 764 765 MaxAlign = std::max(Align, MaxAlign); 766 } 767 768 if (MFI->adjustsStack() && TFI->hasReservedCallFrame(MF)) 769 Offset += MFI->getMaxCallFrameSize(); 770 771 // Round up the size to a multiple of the alignment. If the function has 772 // any calls or alloca's, align to the target's StackAlignment value to 773 // ensure that the callee's frame or the alloca data is suitably aligned; 774 // otherwise, for leaf functions, align to the TransientStackAlignment 775 // value. 776 unsigned StackAlign; 777 if (MFI->adjustsStack() || MFI->hasVarSizedObjects() || 778 (RegInfo->needsStackRealignment(MF) && MFI->getObjectIndexEnd() != 0)) 779 StackAlign = TFI->getStackAlignment(); 780 else 781 StackAlign = TFI->getTransientStackAlignment(); 782 783 // If the frame pointer is eliminated, all frame offsets will be relative to 784 // SP not FP. Align to MaxAlign so this works. 785 StackAlign = std::max(StackAlign, MaxAlign); 786 unsigned AlignMask = StackAlign - 1; 787 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask); 788 789 return (unsigned)Offset; 790 } 791 792 /// estimateRSStackSizeLimit - Look at each instruction that references stack 793 /// frames and return the stack size limit beyond which some of these 794 /// instructions will require a scratch register during their expansion later. 795 // FIXME: Move to TII? 796 static unsigned estimateRSStackSizeLimit(MachineFunction &MF, 797 const TargetFrameLowering *TFI) { 798 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 799 unsigned Limit = (1 << 12) - 1; 800 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) { 801 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); 802 I != E; ++I) { 803 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 804 if (!I->getOperand(i).isFI()) continue; 805 806 // When using ADDri to get the address of a stack object, 255 is the 807 // largest offset guaranteed to fit in the immediate offset. 808 if (I->getOpcode() == ARM::ADDri) { 809 Limit = std::min(Limit, (1U << 8) - 1); 810 break; 811 } 812 813 // Otherwise check the addressing mode. 814 switch (I->getDesc().TSFlags & ARMII::AddrModeMask) { 815 case ARMII::AddrMode3: 816 case ARMII::AddrModeT2_i8: 817 Limit = std::min(Limit, (1U << 8) - 1); 818 break; 819 case ARMII::AddrMode5: 820 case ARMII::AddrModeT2_i8s4: 821 Limit = std::min(Limit, ((1U << 8) - 1) * 4); 822 break; 823 case ARMII::AddrModeT2_i12: 824 // i12 supports only positive offset so these will be converted to 825 // i8 opcodes. See llvm::rewriteT2FrameIndex. 826 if (TFI->hasFP(MF) && AFI->hasStackFrame()) 827 Limit = std::min(Limit, (1U << 8) - 1); 828 break; 829 case ARMII::AddrMode4: 830 case ARMII::AddrMode6: 831 // Addressing modes 4 & 6 (load/store) instructions can't encode an 832 // immediate offset for stack references. 833 return 0; 834 default: 835 break; 836 } 837 break; // At most one FI per instruction 838 } 839 } 840 } 841 842 return Limit; 843 } 844 845 void 846 ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 847 RegScavenger *RS) const { 848 // This tells PEI to spill the FP as if it is any other callee-save register 849 // to take advantage the eliminateFrameIndex machinery. This also ensures it 850 // is spilled in the order specified by getCalleeSavedRegs() to make it easier 851 // to combine multiple loads / stores. 852 bool CanEliminateFrame = true; 853 bool CS1Spilled = false; 854 bool LRSpilled = false; 855 unsigned NumGPRSpills = 0; 856 SmallVector<unsigned, 4> UnspilledCS1GPRs; 857 SmallVector<unsigned, 4> UnspilledCS2GPRs; 858 const ARMBaseRegisterInfo *RegInfo = 859 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 860 const ARMBaseInstrInfo &TII = 861 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 862 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 863 MachineFrameInfo *MFI = MF.getFrameInfo(); 864 unsigned FramePtr = RegInfo->getFrameRegister(MF); 865 866 // Spill R4 if Thumb2 function requires stack realignment - it will be used as 867 // scratch register. Also spill R4 if Thumb2 function has varsized objects, 868 // since it's not always possible to restore sp from fp in a single 869 // instruction. 870 // FIXME: It will be better just to find spare register here. 871 if (AFI->isThumb2Function() && 872 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) 873 MF.getRegInfo().setPhysRegUsed(ARM::R4); 874 875 if (AFI->isThumb1OnlyFunction()) { 876 // Spill LR if Thumb1 function uses variable length argument lists. 877 if (AFI->getVarArgsRegSaveSize() > 0) 878 MF.getRegInfo().setPhysRegUsed(ARM::LR); 879 880 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know 881 // for sure what the stack size will be, but for this, an estimate is good 882 // enough. If there anything changes it, it'll be a spill, which implies 883 // we've used all the registers and so R4 is already used, so not marking 884 // it here will be OK. 885 // FIXME: It will be better just to find spare register here. 886 unsigned StackSize = estimateStackSize(MF); 887 if (MFI->hasVarSizedObjects() || StackSize > 508) 888 MF.getRegInfo().setPhysRegUsed(ARM::R4); 889 } 890 891 // Spill the BasePtr if it's used. 892 if (RegInfo->hasBasePointer(MF)) 893 MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister()); 894 895 // Don't spill FP if the frame can be eliminated. This is determined 896 // by scanning the callee-save registers to see if any is used. 897 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); 898 for (unsigned i = 0; CSRegs[i]; ++i) { 899 unsigned Reg = CSRegs[i]; 900 bool Spilled = false; 901 if (MF.getRegInfo().isPhysRegOrOverlapUsed(Reg)) { 902 Spilled = true; 903 CanEliminateFrame = false; 904 } 905 906 if (!ARM::GPRRegisterClass->contains(Reg)) 907 continue; 908 909 if (Spilled) { 910 NumGPRSpills++; 911 912 if (!STI.isTargetDarwin()) { 913 if (Reg == ARM::LR) 914 LRSpilled = true; 915 CS1Spilled = true; 916 continue; 917 } 918 919 // Keep track if LR and any of R4, R5, R6, and R7 is spilled. 920 switch (Reg) { 921 case ARM::LR: 922 LRSpilled = true; 923 // Fallthrough 924 case ARM::R4: case ARM::R5: 925 case ARM::R6: case ARM::R7: 926 CS1Spilled = true; 927 break; 928 default: 929 break; 930 } 931 } else { 932 if (!STI.isTargetDarwin()) { 933 UnspilledCS1GPRs.push_back(Reg); 934 continue; 935 } 936 937 switch (Reg) { 938 case ARM::R4: case ARM::R5: 939 case ARM::R6: case ARM::R7: 940 case ARM::LR: 941 UnspilledCS1GPRs.push_back(Reg); 942 break; 943 default: 944 UnspilledCS2GPRs.push_back(Reg); 945 break; 946 } 947 } 948 } 949 950 bool ForceLRSpill = false; 951 if (!LRSpilled && AFI->isThumb1OnlyFunction()) { 952 unsigned FnSize = GetFunctionSizeInBytes(MF, TII); 953 // Force LR to be spilled if the Thumb function size is > 2048. This enables 954 // use of BL to implement far jump. If it turns out that it's not needed 955 // then the branch fix up path will undo it. 956 if (FnSize >= (1 << 11)) { 957 CanEliminateFrame = false; 958 ForceLRSpill = true; 959 } 960 } 961 962 // If any of the stack slot references may be out of range of an immediate 963 // offset, make sure a register (or a spill slot) is available for the 964 // register scavenger. Note that if we're indexing off the frame pointer, the 965 // effective stack size is 4 bytes larger since the FP points to the stack 966 // slot of the previous FP. Also, if we have variable sized objects in the 967 // function, stack slot references will often be negative, and some of 968 // our instructions are positive-offset only, so conservatively consider 969 // that case to want a spill slot (or register) as well. Similarly, if 970 // the function adjusts the stack pointer during execution and the 971 // adjustments aren't already part of our stack size estimate, our offset 972 // calculations may be off, so be conservative. 973 // FIXME: We could add logic to be more precise about negative offsets 974 // and which instructions will need a scratch register for them. Is it 975 // worth the effort and added fragility? 976 bool BigStack = 977 (RS && 978 (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= 979 estimateRSStackSizeLimit(MF, this))) 980 || MFI->hasVarSizedObjects() 981 || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); 982 983 bool ExtraCSSpill = false; 984 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { 985 AFI->setHasStackFrame(true); 986 987 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. 988 // Spill LR as well so we can fold BX_RET to the registers restore (LDM). 989 if (!LRSpilled && CS1Spilled) { 990 MF.getRegInfo().setPhysRegUsed(ARM::LR); 991 NumGPRSpills++; 992 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(), 993 UnspilledCS1GPRs.end(), (unsigned)ARM::LR)); 994 ForceLRSpill = false; 995 ExtraCSSpill = true; 996 } 997 998 if (hasFP(MF)) { 999 MF.getRegInfo().setPhysRegUsed(FramePtr); 1000 NumGPRSpills++; 1001 } 1002 1003 // If stack and double are 8-byte aligned and we are spilling an odd number 1004 // of GPRs, spill one extra callee save GPR so we won't have to pad between 1005 // the integer and double callee save areas. 1006 unsigned TargetAlign = getStackAlignment(); 1007 if (TargetAlign == 8 && (NumGPRSpills & 1)) { 1008 if (CS1Spilled && !UnspilledCS1GPRs.empty()) { 1009 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { 1010 unsigned Reg = UnspilledCS1GPRs[i]; 1011 // Don't spill high register if the function is thumb1 1012 if (!AFI->isThumb1OnlyFunction() || 1013 isARMLowRegister(Reg) || Reg == ARM::LR) { 1014 MF.getRegInfo().setPhysRegUsed(Reg); 1015 if (!RegInfo->isReservedReg(MF, Reg)) 1016 ExtraCSSpill = true; 1017 break; 1018 } 1019 } 1020 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { 1021 unsigned Reg = UnspilledCS2GPRs.front(); 1022 MF.getRegInfo().setPhysRegUsed(Reg); 1023 if (!RegInfo->isReservedReg(MF, Reg)) 1024 ExtraCSSpill = true; 1025 } 1026 } 1027 1028 // Estimate if we might need to scavenge a register at some point in order 1029 // to materialize a stack offset. If so, either spill one additional 1030 // callee-saved register or reserve a special spill slot to facilitate 1031 // register scavenging. Thumb1 needs a spill slot for stack pointer 1032 // adjustments also, even when the frame itself is small. 1033 if (BigStack && !ExtraCSSpill) { 1034 // If any non-reserved CS register isn't spilled, just spill one or two 1035 // extra. That should take care of it! 1036 unsigned NumExtras = TargetAlign / 4; 1037 SmallVector<unsigned, 2> Extras; 1038 while (NumExtras && !UnspilledCS1GPRs.empty()) { 1039 unsigned Reg = UnspilledCS1GPRs.back(); 1040 UnspilledCS1GPRs.pop_back(); 1041 if (!RegInfo->isReservedReg(MF, Reg) && 1042 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || 1043 Reg == ARM::LR)) { 1044 Extras.push_back(Reg); 1045 NumExtras--; 1046 } 1047 } 1048 // For non-Thumb1 functions, also check for hi-reg CS registers 1049 if (!AFI->isThumb1OnlyFunction()) { 1050 while (NumExtras && !UnspilledCS2GPRs.empty()) { 1051 unsigned Reg = UnspilledCS2GPRs.back(); 1052 UnspilledCS2GPRs.pop_back(); 1053 if (!RegInfo->isReservedReg(MF, Reg)) { 1054 Extras.push_back(Reg); 1055 NumExtras--; 1056 } 1057 } 1058 } 1059 if (Extras.size() && NumExtras == 0) { 1060 for (unsigned i = 0, e = Extras.size(); i != e; ++i) { 1061 MF.getRegInfo().setPhysRegUsed(Extras[i]); 1062 } 1063 } else if (!AFI->isThumb1OnlyFunction()) { 1064 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot 1065 // closest to SP or frame pointer. 1066 const TargetRegisterClass *RC = ARM::GPRRegisterClass; 1067 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 1068 RC->getAlignment(), 1069 false)); 1070 } 1071 } 1072 } 1073 1074 if (ForceLRSpill) { 1075 MF.getRegInfo().setPhysRegUsed(ARM::LR); 1076 AFI->setLRIsSpilledForFarJump(true); 1077 } 1078 } 1079