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