1 //===-- ARMBaseRegisterInfo.cpp - ARM Register 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 base ARM implementation of TargetRegisterInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ARMBaseRegisterInfo.h" 15 #include "ARM.h" 16 #include "ARMBaseInstrInfo.h" 17 #include "ARMFrameLowering.h" 18 #include "ARMMachineFunctionInfo.h" 19 #include "ARMSubtarget.h" 20 #include "MCTargetDesc/ARMAddressingModes.h" 21 #include "llvm/ADT/BitVector.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/CodeGen/MachineConstantPool.h" 24 #include "llvm/CodeGen/MachineFrameInfo.h" 25 #include "llvm/CodeGen/MachineFunction.h" 26 #include "llvm/CodeGen/MachineInstrBuilder.h" 27 #include "llvm/CodeGen/MachineRegisterInfo.h" 28 #include "llvm/CodeGen/RegisterScavenging.h" 29 #include "llvm/CodeGen/VirtRegMap.h" 30 #include "llvm/IR/Constants.h" 31 #include "llvm/IR/DerivedTypes.h" 32 #include "llvm/IR/Function.h" 33 #include "llvm/IR/LLVMContext.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include "llvm/Target/TargetFrameLowering.h" 38 #include "llvm/Target/TargetMachine.h" 39 #include "llvm/Target/TargetOptions.h" 40 41 #define GET_REGINFO_TARGET_DESC 42 #include "ARMGenRegisterInfo.inc" 43 44 using namespace llvm; 45 46 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii, 47 const ARMSubtarget &sti) 48 : ARMGenRegisterInfo(ARM::LR, 0, 0, ARM::PC), TII(tii), STI(sti), 49 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11), 50 BasePtr(ARM::R6) { 51 } 52 53 const uint16_t* 54 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 55 bool ghcCall = false; 56 57 if (MF) { 58 const Function *F = MF->getFunction(); 59 ghcCall = (F ? F->getCallingConv() == CallingConv::GHC : false); 60 } 61 62 if (ghcCall) { 63 return CSR_GHC_SaveList; 64 } 65 else { 66 return (STI.isTargetIOS() && !STI.isAAPCS_ABI()) 67 ? CSR_iOS_SaveList : CSR_AAPCS_SaveList; 68 } 69 } 70 71 const uint32_t* 72 ARMBaseRegisterInfo::getCallPreservedMask(CallingConv::ID) const { 73 return (STI.isTargetIOS() && !STI.isAAPCS_ABI()) 74 ? CSR_iOS_RegMask : CSR_AAPCS_RegMask; 75 } 76 77 const uint32_t* 78 ARMBaseRegisterInfo::getThisReturnPreservedMask(CallingConv::ID) const { 79 return (STI.isTargetIOS() && !STI.isAAPCS_ABI()) 80 ? CSR_iOS_ThisReturn_RegMask : CSR_AAPCS_ThisReturn_RegMask; 81 } 82 83 const uint32_t* 84 ARMBaseRegisterInfo::getNoPreservedMask() const { 85 return CSR_NoRegs_RegMask; 86 } 87 88 BitVector ARMBaseRegisterInfo:: 89 getReservedRegs(const MachineFunction &MF) const { 90 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 91 92 // FIXME: avoid re-calculating this every time. 93 BitVector Reserved(getNumRegs()); 94 Reserved.set(ARM::SP); 95 Reserved.set(ARM::PC); 96 Reserved.set(ARM::FPSCR); 97 if (TFI->hasFP(MF)) 98 Reserved.set(FramePtr); 99 if (hasBasePointer(MF)) 100 Reserved.set(BasePtr); 101 // Some targets reserve R9. 102 if (STI.isR9Reserved()) 103 Reserved.set(ARM::R9); 104 // Reserve D16-D31 if the subtarget doesn't support them. 105 if (!STI.hasVFP3() || STI.hasD16()) { 106 assert(ARM::D31 == ARM::D16 + 15); 107 for (unsigned i = 0; i != 16; ++i) 108 Reserved.set(ARM::D16 + i); 109 } 110 const TargetRegisterClass *RC = &ARM::GPRPairRegClass; 111 for(TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I!=E; ++I) 112 for (MCSubRegIterator SI(*I, this); SI.isValid(); ++SI) 113 if (Reserved.test(*SI)) Reserved.set(*I); 114 115 return Reserved; 116 } 117 118 const TargetRegisterClass* 119 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC) 120 const { 121 const TargetRegisterClass *Super = RC; 122 TargetRegisterClass::sc_iterator I = RC->getSuperClasses(); 123 do { 124 switch (Super->getID()) { 125 case ARM::GPRRegClassID: 126 case ARM::SPRRegClassID: 127 case ARM::DPRRegClassID: 128 case ARM::QPRRegClassID: 129 case ARM::QQPRRegClassID: 130 case ARM::QQQQPRRegClassID: 131 case ARM::GPRPairRegClassID: 132 return Super; 133 } 134 Super = *I++; 135 } while (Super); 136 return RC; 137 } 138 139 const TargetRegisterClass * 140 ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) 141 const { 142 return &ARM::GPRRegClass; 143 } 144 145 const TargetRegisterClass * 146 ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { 147 if (RC == &ARM::CCRRegClass) 148 return 0; // Can't copy CCR registers. 149 return RC; 150 } 151 152 unsigned 153 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 154 MachineFunction &MF) const { 155 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 156 157 switch (RC->getID()) { 158 default: 159 return 0; 160 case ARM::tGPRRegClassID: 161 return TFI->hasFP(MF) ? 4 : 5; 162 case ARM::GPRRegClassID: { 163 unsigned FP = TFI->hasFP(MF) ? 1 : 0; 164 return 10 - FP - (STI.isR9Reserved() ? 1 : 0); 165 } 166 case ARM::SPRRegClassID: // Currently not used as 'rep' register class. 167 case ARM::DPRRegClassID: 168 return 32 - 10; 169 } 170 } 171 172 // Get the other register in a GPRPair. 173 static unsigned getPairedGPR(unsigned Reg, bool Odd, const MCRegisterInfo *RI) { 174 for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers) 175 if (ARM::GPRPairRegClass.contains(*Supers)) 176 return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0); 177 return 0; 178 } 179 180 // Resolve the RegPairEven / RegPairOdd register allocator hints. 181 void 182 ARMBaseRegisterInfo::getRegAllocationHints(unsigned VirtReg, 183 ArrayRef<MCPhysReg> Order, 184 SmallVectorImpl<MCPhysReg> &Hints, 185 const MachineFunction &MF, 186 const VirtRegMap *VRM) const { 187 const MachineRegisterInfo &MRI = MF.getRegInfo(); 188 std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg); 189 190 unsigned Odd; 191 switch (Hint.first) { 192 case ARMRI::RegPairEven: 193 Odd = 0; 194 break; 195 case ARMRI::RegPairOdd: 196 Odd = 1; 197 break; 198 default: 199 TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM); 200 return; 201 } 202 203 // This register should preferably be even (Odd == 0) or odd (Odd == 1). 204 // Check if the other part of the pair has already been assigned, and provide 205 // the paired register as the first hint. 206 unsigned PairedPhys = 0; 207 if (VRM && VRM->hasPhys(Hint.second)) { 208 PairedPhys = getPairedGPR(VRM->getPhys(Hint.second), Odd, this); 209 if (PairedPhys && MRI.isReserved(PairedPhys)) 210 PairedPhys = 0; 211 } 212 213 // First prefer the paired physreg. 214 if (PairedPhys && 215 std::find(Order.begin(), Order.end(), PairedPhys) != Order.end()) 216 Hints.push_back(PairedPhys); 217 218 // Then prefer even or odd registers. 219 for (unsigned I = 0, E = Order.size(); I != E; ++I) { 220 unsigned Reg = Order[I]; 221 if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd) 222 continue; 223 // Don't provide hints that are paired to a reserved register. 224 unsigned Paired = getPairedGPR(Reg, !Odd, this); 225 if (!Paired || MRI.isReserved(Paired)) 226 continue; 227 Hints.push_back(Reg); 228 } 229 } 230 231 void 232 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg, 233 MachineFunction &MF) const { 234 MachineRegisterInfo *MRI = &MF.getRegInfo(); 235 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg); 236 if ((Hint.first == (unsigned)ARMRI::RegPairOdd || 237 Hint.first == (unsigned)ARMRI::RegPairEven) && 238 TargetRegisterInfo::isVirtualRegister(Hint.second)) { 239 // If 'Reg' is one of the even / odd register pair and it's now changed 240 // (e.g. coalesced) into a different register. The other register of the 241 // pair allocation hint must be updated to reflect the relationship 242 // change. 243 unsigned OtherReg = Hint.second; 244 Hint = MRI->getRegAllocationHint(OtherReg); 245 if (Hint.second == Reg) 246 // Make sure the pair has not already divorced. 247 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg); 248 } 249 } 250 251 bool 252 ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const { 253 // CortexA9 has a Write-after-write hazard for NEON registers. 254 if (!STI.isLikeA9()) 255 return false; 256 257 switch (RC->getID()) { 258 case ARM::DPRRegClassID: 259 case ARM::DPR_8RegClassID: 260 case ARM::DPR_VFP2RegClassID: 261 case ARM::QPRRegClassID: 262 case ARM::QPR_8RegClassID: 263 case ARM::QPR_VFP2RegClassID: 264 case ARM::SPRRegClassID: 265 case ARM::SPR_8RegClassID: 266 // Avoid reusing S, D, and Q registers. 267 // Don't increase register pressure for QQ and QQQQ. 268 return true; 269 default: 270 return false; 271 } 272 } 273 274 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const { 275 const MachineFrameInfo *MFI = MF.getFrameInfo(); 276 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 277 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 278 279 // When outgoing call frames are so large that we adjust the stack pointer 280 // around the call, we can no longer use the stack pointer to reach the 281 // emergency spill slot. 282 if (needsStackRealignment(MF) && !TFI->hasReservedCallFrame(MF)) 283 return true; 284 285 // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited 286 // negative range for ldr/str (255), and thumb1 is positive offsets only. 287 // It's going to be better to use the SP or Base Pointer instead. When there 288 // are variable sized objects, we can't reference off of the SP, so we 289 // reserve a Base Pointer. 290 if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) { 291 // Conservatively estimate whether the negative offset from the frame 292 // pointer will be sufficient to reach. If a function has a smallish 293 // frame, it's less likely to have lots of spills and callee saved 294 // space, so it's all more likely to be within range of the frame pointer. 295 // If it's wrong, the scavenger will still enable access to work, it just 296 // won't be optimal. 297 if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128) 298 return false; 299 return true; 300 } 301 302 return false; 303 } 304 305 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const { 306 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 307 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 308 // We can't realign the stack if: 309 // 1. Dynamic stack realignment is explicitly disabled, 310 // 2. This is a Thumb1 function (it's not useful, so we don't bother), or 311 // 3. There are VLAs in the function and the base pointer is disabled. 312 if (!MF.getTarget().Options.RealignStack) 313 return false; 314 if (AFI->isThumb1OnlyFunction()) 315 return false; 316 // Stack realignment requires a frame pointer. If we already started 317 // register allocation with frame pointer elimination, it is too late now. 318 if (!MRI->canReserveReg(FramePtr)) 319 return false; 320 // We may also need a base pointer if there are dynamic allocas or stack 321 // pointer adjustments around calls. 322 if (MF.getTarget().getFrameLowering()->hasReservedCallFrame(MF)) 323 return true; 324 // A base pointer is required and allowed. Check that it isn't too late to 325 // reserve it. 326 return MRI->canReserveReg(BasePtr); 327 } 328 329 bool ARMBaseRegisterInfo:: 330 needsStackRealignment(const MachineFunction &MF) const { 331 const MachineFrameInfo *MFI = MF.getFrameInfo(); 332 const Function *F = MF.getFunction(); 333 unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment(); 334 bool requiresRealignment = 335 ((MFI->getMaxAlignment() > StackAlign) || 336 F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 337 Attribute::StackAlignment)); 338 339 return requiresRealignment && canRealignStack(MF); 340 } 341 342 bool ARMBaseRegisterInfo:: 343 cannotEliminateFrame(const MachineFunction &MF) const { 344 const MachineFrameInfo *MFI = MF.getFrameInfo(); 345 if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI->adjustsStack()) 346 return true; 347 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken() 348 || needsStackRealignment(MF); 349 } 350 351 unsigned 352 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 353 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 354 355 if (TFI->hasFP(MF)) 356 return FramePtr; 357 return ARM::SP; 358 } 359 360 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const { 361 llvm_unreachable("What is the exception register"); 362 } 363 364 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const { 365 llvm_unreachable("What is the exception handler register"); 366 } 367 368 /// emitLoadConstPool - Emits a load from constpool to materialize the 369 /// specified immediate. 370 void ARMBaseRegisterInfo:: 371 emitLoadConstPool(MachineBasicBlock &MBB, 372 MachineBasicBlock::iterator &MBBI, 373 DebugLoc dl, 374 unsigned DestReg, unsigned SubIdx, int Val, 375 ARMCC::CondCodes Pred, 376 unsigned PredReg, unsigned MIFlags) const { 377 MachineFunction &MF = *MBB.getParent(); 378 MachineConstantPool *ConstantPool = MF.getConstantPool(); 379 const Constant *C = 380 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val); 381 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4); 382 383 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp)) 384 .addReg(DestReg, getDefRegState(true), SubIdx) 385 .addConstantPoolIndex(Idx) 386 .addImm(0).addImm(Pred).addReg(PredReg) 387 .setMIFlags(MIFlags); 388 } 389 390 bool ARMBaseRegisterInfo:: 391 requiresRegisterScavenging(const MachineFunction &MF) const { 392 return true; 393 } 394 395 bool ARMBaseRegisterInfo:: 396 trackLivenessAfterRegAlloc(const MachineFunction &MF) const { 397 return true; 398 } 399 400 bool ARMBaseRegisterInfo:: 401 requiresFrameIndexScavenging(const MachineFunction &MF) const { 402 return true; 403 } 404 405 bool ARMBaseRegisterInfo:: 406 requiresVirtualBaseRegisters(const MachineFunction &MF) const { 407 return true; 408 } 409 410 int64_t ARMBaseRegisterInfo:: 411 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const { 412 const MCInstrDesc &Desc = MI->getDesc(); 413 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); 414 int64_t InstrOffs = 0; 415 int Scale = 1; 416 unsigned ImmIdx = 0; 417 switch (AddrMode) { 418 case ARMII::AddrModeT2_i8: 419 case ARMII::AddrModeT2_i12: 420 case ARMII::AddrMode_i12: 421 InstrOffs = MI->getOperand(Idx+1).getImm(); 422 Scale = 1; 423 break; 424 case ARMII::AddrMode5: { 425 // VFP address mode. 426 const MachineOperand &OffOp = MI->getOperand(Idx+1); 427 InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm()); 428 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub) 429 InstrOffs = -InstrOffs; 430 Scale = 4; 431 break; 432 } 433 case ARMII::AddrMode2: { 434 ImmIdx = Idx+2; 435 InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm()); 436 if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub) 437 InstrOffs = -InstrOffs; 438 break; 439 } 440 case ARMII::AddrMode3: { 441 ImmIdx = Idx+2; 442 InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm()); 443 if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub) 444 InstrOffs = -InstrOffs; 445 break; 446 } 447 case ARMII::AddrModeT1_s: { 448 ImmIdx = Idx+1; 449 InstrOffs = MI->getOperand(ImmIdx).getImm(); 450 Scale = 4; 451 break; 452 } 453 default: 454 llvm_unreachable("Unsupported addressing mode!"); 455 } 456 457 return InstrOffs * Scale; 458 } 459 460 /// needsFrameBaseReg - Returns true if the instruction's frame index 461 /// reference would be better served by a base register other than FP 462 /// or SP. Used by LocalStackFrameAllocation to determine which frame index 463 /// references it should create new base registers for. 464 bool ARMBaseRegisterInfo:: 465 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 466 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) { 467 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!"); 468 } 469 470 // It's the load/store FI references that cause issues, as it can be difficult 471 // to materialize the offset if it won't fit in the literal field. Estimate 472 // based on the size of the local frame and some conservative assumptions 473 // about the rest of the stack frame (note, this is pre-regalloc, so 474 // we don't know everything for certain yet) whether this offset is likely 475 // to be out of range of the immediate. Return true if so. 476 477 // We only generate virtual base registers for loads and stores, so 478 // return false for everything else. 479 unsigned Opc = MI->getOpcode(); 480 switch (Opc) { 481 case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12: 482 case ARM::STRi12: case ARM::STRH: case ARM::STRBi12: 483 case ARM::t2LDRi12: case ARM::t2LDRi8: 484 case ARM::t2STRi12: case ARM::t2STRi8: 485 case ARM::VLDRS: case ARM::VLDRD: 486 case ARM::VSTRS: case ARM::VSTRD: 487 case ARM::tSTRspi: case ARM::tLDRspi: 488 break; 489 default: 490 return false; 491 } 492 493 // Without a virtual base register, if the function has variable sized 494 // objects, all fixed-size local references will be via the frame pointer, 495 // Approximate the offset and see if it's legal for the instruction. 496 // Note that the incoming offset is based on the SP value at function entry, 497 // so it'll be negative. 498 MachineFunction &MF = *MI->getParent()->getParent(); 499 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 500 MachineFrameInfo *MFI = MF.getFrameInfo(); 501 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 502 503 // Estimate an offset from the frame pointer. 504 // Conservatively assume all callee-saved registers get pushed. R4-R6 505 // will be earlier than the FP, so we ignore those. 506 // R7, LR 507 int64_t FPOffset = Offset - 8; 508 // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15 509 if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction()) 510 FPOffset -= 80; 511 // Estimate an offset from the stack pointer. 512 // The incoming offset is relating to the SP at the start of the function, 513 // but when we access the local it'll be relative to the SP after local 514 // allocation, so adjust our SP-relative offset by that allocation size. 515 Offset = -Offset; 516 Offset += MFI->getLocalFrameSize(); 517 // Assume that we'll have at least some spill slots allocated. 518 // FIXME: This is a total SWAG number. We should run some statistics 519 // and pick a real one. 520 Offset += 128; // 128 bytes of spill slots 521 522 // If there is a frame pointer, try using it. 523 // The FP is only available if there is no dynamic realignment. We 524 // don't know for sure yet whether we'll need that, so we guess based 525 // on whether there are any local variables that would trigger it. 526 unsigned StackAlign = TFI->getStackAlignment(); 527 if (TFI->hasFP(MF) && 528 !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) { 529 if (isFrameOffsetLegal(MI, FPOffset)) 530 return false; 531 } 532 // If we can reference via the stack pointer, try that. 533 // FIXME: This (and the code that resolves the references) can be improved 534 // to only disallow SP relative references in the live range of 535 // the VLA(s). In practice, it's unclear how much difference that 536 // would make, but it may be worth doing. 537 if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset)) 538 return false; 539 540 // The offset likely isn't legal, we want to allocate a virtual base register. 541 return true; 542 } 543 544 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to 545 /// be a pointer to FrameIdx at the beginning of the basic block. 546 void ARMBaseRegisterInfo:: 547 materializeFrameBaseRegister(MachineBasicBlock *MBB, 548 unsigned BaseReg, int FrameIdx, 549 int64_t Offset) const { 550 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>(); 551 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : 552 (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri); 553 554 MachineBasicBlock::iterator Ins = MBB->begin(); 555 DebugLoc DL; // Defaults to "unknown" 556 if (Ins != MBB->end()) 557 DL = Ins->getDebugLoc(); 558 559 const MCInstrDesc &MCID = TII.get(ADDriOpc); 560 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 561 const MachineFunction &MF = *MBB->getParent(); 562 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); 563 564 MachineInstrBuilder MIB = AddDefaultPred(BuildMI(*MBB, Ins, DL, MCID, BaseReg) 565 .addFrameIndex(FrameIdx).addImm(Offset)); 566 567 if (!AFI->isThumb1OnlyFunction()) 568 AddDefaultCC(MIB); 569 } 570 571 void 572 ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I, 573 unsigned BaseReg, int64_t Offset) const { 574 MachineInstr &MI = *I; 575 MachineBasicBlock &MBB = *MI.getParent(); 576 MachineFunction &MF = *MBB.getParent(); 577 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 578 int Off = Offset; // ARM doesn't need the general 64-bit offsets 579 unsigned i = 0; 580 581 assert(!AFI->isThumb1OnlyFunction() && 582 "This resolveFrameIndex does not support Thumb1!"); 583 584 while (!MI.getOperand(i).isFI()) { 585 ++i; 586 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 587 } 588 bool Done = false; 589 if (!AFI->isThumbFunction()) 590 Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII); 591 else { 592 assert(AFI->isThumb2Function()); 593 Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII); 594 } 595 assert (Done && "Unable to resolve frame index!"); 596 (void)Done; 597 } 598 599 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 600 int64_t Offset) const { 601 const MCInstrDesc &Desc = MI->getDesc(); 602 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask); 603 unsigned i = 0; 604 605 while (!MI->getOperand(i).isFI()) { 606 ++i; 607 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!"); 608 } 609 610 // AddrMode4 and AddrMode6 cannot handle any offset. 611 if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6) 612 return Offset == 0; 613 614 unsigned NumBits = 0; 615 unsigned Scale = 1; 616 bool isSigned = true; 617 switch (AddrMode) { 618 case ARMII::AddrModeT2_i8: 619 case ARMII::AddrModeT2_i12: 620 // i8 supports only negative, and i12 supports only positive, so 621 // based on Offset sign, consider the appropriate instruction 622 Scale = 1; 623 if (Offset < 0) { 624 NumBits = 8; 625 Offset = -Offset; 626 } else { 627 NumBits = 12; 628 } 629 break; 630 case ARMII::AddrMode5: 631 // VFP address mode. 632 NumBits = 8; 633 Scale = 4; 634 break; 635 case ARMII::AddrMode_i12: 636 case ARMII::AddrMode2: 637 NumBits = 12; 638 break; 639 case ARMII::AddrMode3: 640 NumBits = 8; 641 break; 642 case ARMII::AddrModeT1_s: 643 NumBits = 5; 644 Scale = 4; 645 isSigned = false; 646 break; 647 default: 648 llvm_unreachable("Unsupported addressing mode!"); 649 } 650 651 Offset += getFrameIndexInstrOffset(MI, i); 652 // Make sure the offset is encodable for instructions that scale the 653 // immediate. 654 if ((Offset & (Scale-1)) != 0) 655 return false; 656 657 if (isSigned && Offset < 0) 658 Offset = -Offset; 659 660 unsigned Mask = (1 << NumBits) - 1; 661 if ((unsigned)Offset <= Mask * Scale) 662 return true; 663 664 return false; 665 } 666 667 void 668 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 669 int SPAdj, unsigned FIOperandNum, 670 RegScavenger *RS) const { 671 MachineInstr &MI = *II; 672 MachineBasicBlock &MBB = *MI.getParent(); 673 MachineFunction &MF = *MBB.getParent(); 674 const ARMFrameLowering *TFI = 675 static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering()); 676 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 677 assert(!AFI->isThumb1OnlyFunction() && 678 "This eliminateFrameIndex does not support Thumb1!"); 679 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 680 unsigned FrameReg; 681 682 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj); 683 684 // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the 685 // call frame setup/destroy instructions have already been eliminated. That 686 // means the stack pointer cannot be used to access the emergency spill slot 687 // when !hasReservedCallFrame(). 688 #ifndef NDEBUG 689 if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){ 690 assert(TFI->hasReservedCallFrame(MF) && 691 "Cannot use SP to access the emergency spill slot in " 692 "functions without a reserved call frame"); 693 assert(!MF.getFrameInfo()->hasVarSizedObjects() && 694 "Cannot use SP to access the emergency spill slot in " 695 "functions with variable sized frame objects"); 696 } 697 #endif // NDEBUG 698 699 // Special handling of dbg_value instructions. 700 if (MI.isDebugValue()) { 701 MI.getOperand(FIOperandNum). ChangeToRegister(FrameReg, false /*isDef*/); 702 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset); 703 return; 704 } 705 706 // Modify MI as necessary to handle as much of 'Offset' as possible 707 bool Done = false; 708 if (!AFI->isThumbFunction()) 709 Done = rewriteARMFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII); 710 else { 711 assert(AFI->isThumb2Function()); 712 Done = rewriteT2FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII); 713 } 714 if (Done) 715 return; 716 717 // If we get here, the immediate doesn't fit into the instruction. We folded 718 // as much as possible above, handle the rest, providing a register that is 719 // SP+LargeImm. 720 assert((Offset || 721 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 || 722 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) && 723 "This code isn't needed if offset already handled!"); 724 725 unsigned ScratchReg = 0; 726 int PIdx = MI.findFirstPredOperandIdx(); 727 ARMCC::CondCodes Pred = (PIdx == -1) 728 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm(); 729 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg(); 730 if (Offset == 0) 731 // Must be addrmode4/6. 732 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, false); 733 else { 734 ScratchReg = MF.getRegInfo().createVirtualRegister(&ARM::GPRRegClass); 735 if (!AFI->isThumbFunction()) 736 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, 737 Offset, Pred, PredReg, TII); 738 else { 739 assert(AFI->isThumb2Function()); 740 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, 741 Offset, Pred, PredReg, TII); 742 } 743 // Update the original instruction to use the scratch register. 744 MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false,true); 745 } 746 } 747