1 //===- AArch64RegisterInfo.cpp - AArch64 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 AArch64 implementation of the TargetRegisterInfo 11 // class. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "AArch64RegisterInfo.h" 16 #include "AArch64FrameLowering.h" 17 #include "AArch64InstrInfo.h" 18 #include "AArch64MachineFunctionInfo.h" 19 #include "AArch64Subtarget.h" 20 #include "MCTargetDesc/AArch64AddressingModes.h" 21 #include "llvm/ADT/BitVector.h" 22 #include "llvm/ADT/Triple.h" 23 #include "llvm/CodeGen/MachineFrameInfo.h" 24 #include "llvm/CodeGen/MachineInstrBuilder.h" 25 #include "llvm/CodeGen/MachineRegisterInfo.h" 26 #include "llvm/CodeGen/RegisterScavenging.h" 27 #include "llvm/IR/Function.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include "llvm/Target/TargetFrameLowering.h" 31 #include "llvm/Target/TargetOptions.h" 32 33 using namespace llvm; 34 35 #define GET_REGINFO_TARGET_DESC 36 #include "AArch64GenRegisterInfo.inc" 37 38 AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT) 39 : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {} 40 41 const MCPhysReg * 42 AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 43 assert(MF && "Invalid MachineFunction pointer."); 44 if (MF->getFunction()->getCallingConv() == CallingConv::GHC) 45 // GHC set of callee saved regs is empty as all those regs are 46 // used for passing STG regs around 47 return CSR_AArch64_NoRegs_SaveList; 48 if (MF->getFunction()->getCallingConv() == CallingConv::AnyReg) 49 return CSR_AArch64_AllRegs_SaveList; 50 if (MF->getFunction()->getCallingConv() == CallingConv::CXX_FAST_TLS) 51 return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR() ? 52 CSR_AArch64_CXX_TLS_Darwin_PE_SaveList : 53 CSR_AArch64_CXX_TLS_Darwin_SaveList; 54 else 55 return CSR_AArch64_AAPCS_SaveList; 56 } 57 58 const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy( 59 const MachineFunction *MF) const { 60 assert(MF && "Invalid MachineFunction pointer."); 61 if (MF->getFunction()->getCallingConv() == CallingConv::CXX_FAST_TLS && 62 MF->getInfo<AArch64FunctionInfo>()->isSplitCSR()) 63 return CSR_AArch64_CXX_TLS_Darwin_ViaCopy_SaveList; 64 return nullptr; 65 } 66 67 const uint32_t * 68 AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF, 69 CallingConv::ID CC) const { 70 if (CC == CallingConv::GHC) 71 // This is academic becase all GHC calls are (supposed to be) tail calls 72 return CSR_AArch64_NoRegs_RegMask; 73 if (CC == CallingConv::AnyReg) 74 return CSR_AArch64_AllRegs_RegMask; 75 if (CC == CallingConv::CXX_FAST_TLS) 76 return CSR_AArch64_CXX_TLS_Darwin_RegMask; 77 else 78 return CSR_AArch64_AAPCS_RegMask; 79 } 80 81 const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const { 82 if (TT.isOSDarwin()) 83 return CSR_AArch64_TLS_Darwin_RegMask; 84 85 assert(TT.isOSBinFormatELF() && "only expect Darwin or ELF TLS"); 86 return CSR_AArch64_TLS_ELF_RegMask; 87 } 88 89 const uint32_t * 90 AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF, 91 CallingConv::ID CC) const { 92 // This should return a register mask that is the same as that returned by 93 // getCallPreservedMask but that additionally preserves the register used for 94 // the first i64 argument (which must also be the register used to return a 95 // single i64 return value) 96 // 97 // In case that the calling convention does not use the same register for 98 // both, the function should return NULL (does not currently apply) 99 assert(CC != CallingConv::GHC && "should not be GHC calling convention."); 100 return CSR_AArch64_AAPCS_ThisReturn_RegMask; 101 } 102 103 BitVector 104 AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const { 105 const AArch64FrameLowering *TFI = getFrameLowering(MF); 106 107 // FIXME: avoid re-calculating this every time. 108 BitVector Reserved(getNumRegs()); 109 Reserved.set(AArch64::SP); 110 Reserved.set(AArch64::XZR); 111 Reserved.set(AArch64::WSP); 112 Reserved.set(AArch64::WZR); 113 114 if (TFI->hasFP(MF) || TT.isOSDarwin()) { 115 Reserved.set(AArch64::FP); 116 Reserved.set(AArch64::W29); 117 } 118 119 if (MF.getSubtarget<AArch64Subtarget>().isX18Reserved()) { 120 Reserved.set(AArch64::X18); // Platform register 121 Reserved.set(AArch64::W18); 122 } 123 124 if (hasBasePointer(MF)) { 125 Reserved.set(AArch64::X19); 126 Reserved.set(AArch64::W19); 127 } 128 129 return Reserved; 130 } 131 132 bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF, 133 unsigned Reg) const { 134 const AArch64FrameLowering *TFI = getFrameLowering(MF); 135 136 switch (Reg) { 137 default: 138 break; 139 case AArch64::SP: 140 case AArch64::XZR: 141 case AArch64::WSP: 142 case AArch64::WZR: 143 return true; 144 case AArch64::X18: 145 case AArch64::W18: 146 return MF.getSubtarget<AArch64Subtarget>().isX18Reserved(); 147 case AArch64::FP: 148 case AArch64::W29: 149 return TFI->hasFP(MF) || TT.isOSDarwin(); 150 case AArch64::W19: 151 case AArch64::X19: 152 return hasBasePointer(MF); 153 } 154 155 return false; 156 } 157 158 const TargetRegisterClass * 159 AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF, 160 unsigned Kind) const { 161 return &AArch64::GPR64RegClass; 162 } 163 164 const TargetRegisterClass * 165 AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { 166 if (RC == &AArch64::CCRRegClass) 167 return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV. 168 return RC; 169 } 170 171 unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; } 172 173 bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const { 174 const MachineFrameInfo *MFI = MF.getFrameInfo(); 175 176 // In the presence of variable sized objects, if the fixed stack size is 177 // large enough that referencing from the FP won't result in things being 178 // in range relatively often, we can use a base pointer to allow access 179 // from the other direction like the SP normally works. 180 // Furthermore, if both variable sized objects are present, and the 181 // stack needs to be dynamically re-aligned, the base pointer is the only 182 // reliable way to reference the locals. 183 if (MFI->hasVarSizedObjects()) { 184 if (needsStackRealignment(MF)) 185 return true; 186 // Conservatively estimate whether the negative offset from the frame 187 // pointer will be sufficient to reach. If a function has a smallish 188 // frame, it's less likely to have lots of spills and callee saved 189 // space, so it's all more likely to be within range of the frame pointer. 190 // If it's wrong, we'll materialize the constant and still get to the 191 // object; it's just suboptimal. Negative offsets use the unscaled 192 // load/store instructions, which have a 9-bit signed immediate. 193 return MFI->getLocalFrameSize() >= 256; 194 } 195 196 return false; 197 } 198 199 unsigned 200 AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const { 201 const AArch64FrameLowering *TFI = getFrameLowering(MF); 202 return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP; 203 } 204 205 bool AArch64RegisterInfo::requiresRegisterScavenging( 206 const MachineFunction &MF) const { 207 return true; 208 } 209 210 bool AArch64RegisterInfo::requiresVirtualBaseRegisters( 211 const MachineFunction &MF) const { 212 return true; 213 } 214 215 bool 216 AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const { 217 const MachineFrameInfo *MFI = MF.getFrameInfo(); 218 // AArch64FrameLowering::resolveFrameIndexReference() can always fall back 219 // to the stack pointer, so only put the emergency spill slot next to the 220 // FP when there's no better way to access it (SP or base pointer). 221 return MFI->hasVarSizedObjects() && !hasBasePointer(MF); 222 } 223 224 bool AArch64RegisterInfo::requiresFrameIndexScavenging( 225 const MachineFunction &MF) const { 226 return true; 227 } 228 229 bool 230 AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const { 231 const MachineFrameInfo *MFI = MF.getFrameInfo(); 232 if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI->adjustsStack()) 233 return true; 234 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken(); 235 } 236 237 /// needsFrameBaseReg - Returns true if the instruction's frame index 238 /// reference would be better served by a base register other than FP 239 /// or SP. Used by LocalStackFrameAllocation to determine which frame index 240 /// references it should create new base registers for. 241 bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI, 242 int64_t Offset) const { 243 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) 244 assert(i < MI->getNumOperands() && 245 "Instr doesn't have FrameIndex operand!"); 246 247 // It's the load/store FI references that cause issues, as it can be difficult 248 // to materialize the offset if it won't fit in the literal field. Estimate 249 // based on the size of the local frame and some conservative assumptions 250 // about the rest of the stack frame (note, this is pre-regalloc, so 251 // we don't know everything for certain yet) whether this offset is likely 252 // to be out of range of the immediate. Return true if so. 253 254 // We only generate virtual base registers for loads and stores, so 255 // return false for everything else. 256 if (!MI->mayLoad() && !MI->mayStore()) 257 return false; 258 259 // Without a virtual base register, if the function has variable sized 260 // objects, all fixed-size local references will be via the frame pointer, 261 // Approximate the offset and see if it's legal for the instruction. 262 // Note that the incoming offset is based on the SP value at function entry, 263 // so it'll be negative. 264 MachineFunction &MF = *MI->getParent()->getParent(); 265 const AArch64FrameLowering *TFI = getFrameLowering(MF); 266 MachineFrameInfo *MFI = MF.getFrameInfo(); 267 268 // Estimate an offset from the frame pointer. 269 // Conservatively assume all GPR callee-saved registers get pushed. 270 // FP, LR, X19-X28, D8-D15. 64-bits each. 271 int64_t FPOffset = Offset - 16 * 20; 272 // Estimate an offset from the stack pointer. 273 // The incoming offset is relating to the SP at the start of the function, 274 // but when we access the local it'll be relative to the SP after local 275 // allocation, so adjust our SP-relative offset by that allocation size. 276 Offset += MFI->getLocalFrameSize(); 277 // Assume that we'll have at least some spill slots allocated. 278 // FIXME: This is a total SWAG number. We should run some statistics 279 // and pick a real one. 280 Offset += 128; // 128 bytes of spill slots 281 282 // If there is a frame pointer, try using it. 283 // The FP is only available if there is no dynamic realignment. We 284 // don't know for sure yet whether we'll need that, so we guess based 285 // on whether there are any local variables that would trigger it. 286 if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset)) 287 return false; 288 289 // If we can reference via the stack pointer or base pointer, try that. 290 // FIXME: This (and the code that resolves the references) can be improved 291 // to only disallow SP relative references in the live range of 292 // the VLA(s). In practice, it's unclear how much difference that 293 // would make, but it may be worth doing. 294 if (isFrameOffsetLegal(MI, AArch64::SP, Offset)) 295 return false; 296 297 // The offset likely isn't legal; we want to allocate a virtual base register. 298 return true; 299 } 300 301 bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 302 unsigned BaseReg, 303 int64_t Offset) const { 304 assert(Offset <= INT_MAX && "Offset too big to fit in int."); 305 assert(MI && "Unable to get the legal offset for nil instruction."); 306 int SaveOffset = Offset; 307 return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal; 308 } 309 310 /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx 311 /// at the beginning of the basic block. 312 void AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 313 unsigned BaseReg, 314 int FrameIdx, 315 int64_t Offset) const { 316 MachineBasicBlock::iterator Ins = MBB->begin(); 317 DebugLoc DL; // Defaults to "unknown" 318 if (Ins != MBB->end()) 319 DL = Ins->getDebugLoc(); 320 const MachineFunction &MF = *MBB->getParent(); 321 const AArch64InstrInfo *TII = 322 MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); 323 const MCInstrDesc &MCID = TII->get(AArch64::ADDXri); 324 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 325 MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF)); 326 unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0); 327 328 BuildMI(*MBB, Ins, DL, MCID, BaseReg) 329 .addFrameIndex(FrameIdx) 330 .addImm(Offset) 331 .addImm(Shifter); 332 } 333 334 void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg, 335 int64_t Offset) const { 336 int Off = Offset; // ARM doesn't need the general 64-bit offsets 337 unsigned i = 0; 338 339 while (!MI.getOperand(i).isFI()) { 340 ++i; 341 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); 342 } 343 const MachineFunction *MF = MI.getParent()->getParent(); 344 const AArch64InstrInfo *TII = 345 MF->getSubtarget<AArch64Subtarget>().getInstrInfo(); 346 bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII); 347 assert(Done && "Unable to resolve frame index!"); 348 (void)Done; 349 } 350 351 void AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 352 int SPAdj, unsigned FIOperandNum, 353 RegScavenger *RS) const { 354 assert(SPAdj == 0 && "Unexpected"); 355 356 MachineInstr &MI = *II; 357 MachineBasicBlock &MBB = *MI.getParent(); 358 MachineFunction &MF = *MBB.getParent(); 359 const AArch64InstrInfo *TII = 360 MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); 361 const AArch64FrameLowering *TFI = getFrameLowering(MF); 362 363 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 364 unsigned FrameReg; 365 int Offset; 366 367 // Special handling of dbg_value, stackmap and patchpoint instructions. 368 if (MI.isDebugValue() || MI.getOpcode() == TargetOpcode::STACKMAP || 369 MI.getOpcode() == TargetOpcode::PATCHPOINT) { 370 Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg, 371 /*PreferFP=*/true); 372 Offset += MI.getOperand(FIOperandNum + 1).getImm(); 373 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/); 374 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset); 375 return; 376 } 377 378 // Modify MI as necessary to handle as much of 'Offset' as possible 379 Offset = TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg); 380 if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII)) 381 return; 382 383 assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) && 384 "Emergency spill slot is out of reach"); 385 386 // If we get here, the immediate doesn't fit into the instruction. We folded 387 // as much as possible above. Handle the rest, providing a register that is 388 // SP+LargeImm. 389 unsigned ScratchReg = 390 MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass); 391 emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII); 392 MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false, true); 393 } 394 395 namespace llvm { 396 397 unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 398 MachineFunction &MF) const { 399 const AArch64FrameLowering *TFI = getFrameLowering(MF); 400 401 switch (RC->getID()) { 402 default: 403 return 0; 404 case AArch64::GPR32RegClassID: 405 case AArch64::GPR32spRegClassID: 406 case AArch64::GPR32allRegClassID: 407 case AArch64::GPR64spRegClassID: 408 case AArch64::GPR64allRegClassID: 409 case AArch64::GPR64RegClassID: 410 case AArch64::GPR32commonRegClassID: 411 case AArch64::GPR64commonRegClassID: 412 return 32 - 1 // XZR/SP 413 - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP 414 - MF.getSubtarget<AArch64Subtarget>() 415 .isX18Reserved() // X18 reserved as platform register 416 - hasBasePointer(MF); // X19 417 case AArch64::FPR8RegClassID: 418 case AArch64::FPR16RegClassID: 419 case AArch64::FPR32RegClassID: 420 case AArch64::FPR64RegClassID: 421 case AArch64::FPR128RegClassID: 422 return 32; 423 424 case AArch64::DDRegClassID: 425 case AArch64::DDDRegClassID: 426 case AArch64::DDDDRegClassID: 427 case AArch64::QQRegClassID: 428 case AArch64::QQQRegClassID: 429 case AArch64::QQQQRegClassID: 430 return 32; 431 432 case AArch64::FPR128_loRegClassID: 433 return 16; 434 } 435 } 436 437 } // namespace llvm 438