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