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