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