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