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