1 //===-- PPCRegisterInfo.cpp - PowerPC 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 PowerPC implementation of the TargetRegisterInfo 10 // class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "PPCRegisterInfo.h" 15 #include "PPCFrameLowering.h" 16 #include "PPCInstrBuilder.h" 17 #include "PPCMachineFunctionInfo.h" 18 #include "PPCSubtarget.h" 19 #include "PPCTargetMachine.h" 20 #include "llvm/ADT/BitVector.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/CodeGen/MachineFrameInfo.h" 24 #include "llvm/CodeGen/MachineFunction.h" 25 #include "llvm/CodeGen/MachineInstrBuilder.h" 26 #include "llvm/CodeGen/MachineModuleInfo.h" 27 #include "llvm/CodeGen/MachineRegisterInfo.h" 28 #include "llvm/CodeGen/RegisterScavenging.h" 29 #include "llvm/CodeGen/TargetFrameLowering.h" 30 #include "llvm/CodeGen/TargetInstrInfo.h" 31 #include "llvm/CodeGen/VirtRegMap.h" 32 #include "llvm/IR/CallingConv.h" 33 #include "llvm/IR/Constants.h" 34 #include "llvm/IR/Function.h" 35 #include "llvm/IR/Type.h" 36 #include "llvm/Support/CommandLine.h" 37 #include "llvm/Support/Debug.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include "llvm/Support/MathExtras.h" 40 #include "llvm/Support/raw_ostream.h" 41 #include "llvm/Target/TargetMachine.h" 42 #include "llvm/Target/TargetOptions.h" 43 #include <cstdlib> 44 45 using namespace llvm; 46 47 #define DEBUG_TYPE "reginfo" 48 49 #define GET_REGINFO_TARGET_DESC 50 #include "PPCGenRegisterInfo.inc" 51 52 STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass"); 53 STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass"); 54 55 static cl::opt<bool> 56 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true), 57 cl::desc("Enable use of a base pointer for complex stack frames")); 58 59 static cl::opt<bool> 60 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false), 61 cl::desc("Force the use of a base pointer in every function")); 62 63 static cl::opt<bool> 64 EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false), 65 cl::desc("Enable spills from gpr to vsr rather than stack")); 66 67 static cl::opt<bool> 68 StackPtrConst("ppc-stack-ptr-caller-preserved", 69 cl::desc("Consider R1 caller preserved so stack saves of " 70 "caller preserved registers can be LICM candidates"), 71 cl::init(true), cl::Hidden); 72 73 static cl::opt<unsigned> 74 MaxCRBitSpillDist("ppc-max-crbit-spill-dist", 75 cl::desc("Maximum search distance for definition of CR bit " 76 "spill on ppc"), 77 cl::Hidden, cl::init(100)); 78 79 // Copies/moves of physical accumulators are expensive operations 80 // that should be avoided whenever possible. MMA instructions are 81 // meant to be used in performance-sensitive computational kernels. 82 // This option is provided, at least for the time being, to give the 83 // user a tool to detect this expensive operation and either rework 84 // their code or report a compiler bug if that turns out to be the 85 // cause. 86 #ifndef NDEBUG 87 static cl::opt<bool> 88 ReportAccMoves("ppc-report-acc-moves", 89 cl::desc("Emit information about accumulator register spills " 90 "and copies"), 91 cl::Hidden, cl::init(false)); 92 #endif 93 94 extern cl::opt<bool> DisableAutoPairedVecSt; 95 96 static unsigned offsetMinAlignForOpcode(unsigned OpC); 97 98 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM) 99 : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR, 100 TM.isPPC64() ? 0 : 1, 101 TM.isPPC64() ? 0 : 1), 102 TM(TM) { 103 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX; 104 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX; 105 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX; 106 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX; 107 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX; 108 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX; 109 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX; 110 ImmToIdxMap[PPC::ADDI] = PPC::ADD4; 111 ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32; 112 113 // 64-bit 114 ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8; 115 ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8; 116 ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8; 117 ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX; 118 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; 119 ImmToIdxMap[PPC::LQ] = PPC::LQX_PSEUDO; 120 ImmToIdxMap[PPC::STQ] = PPC::STQX_PSEUDO; 121 122 // VSX 123 ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX; 124 ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX; 125 ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX; 126 ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX; 127 ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX; 128 ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX; 129 ImmToIdxMap[PPC::LXV] = PPC::LXVX; 130 ImmToIdxMap[PPC::LXSD] = PPC::LXSDX; 131 ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX; 132 ImmToIdxMap[PPC::STXV] = PPC::STXVX; 133 ImmToIdxMap[PPC::STXSD] = PPC::STXSDX; 134 ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX; 135 136 // SPE 137 ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX; 138 ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX; 139 ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX; 140 ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX; 141 142 // Power10 143 ImmToIdxMap[PPC::PLBZ] = PPC::LBZX; ImmToIdxMap[PPC::PLBZ8] = PPC::LBZX8; 144 ImmToIdxMap[PPC::PLHZ] = PPC::LHZX; ImmToIdxMap[PPC::PLHZ8] = PPC::LHZX8; 145 ImmToIdxMap[PPC::PLHA] = PPC::LHAX; ImmToIdxMap[PPC::PLHA8] = PPC::LHAX8; 146 ImmToIdxMap[PPC::PLWZ] = PPC::LWZX; ImmToIdxMap[PPC::PLWZ8] = PPC::LWZX8; 147 ImmToIdxMap[PPC::PLWA] = PPC::LWAX; ImmToIdxMap[PPC::PLWA8] = PPC::LWAX; 148 ImmToIdxMap[PPC::PLD] = PPC::LDX; ImmToIdxMap[PPC::PSTD] = PPC::STDX; 149 150 ImmToIdxMap[PPC::PSTB] = PPC::STBX; ImmToIdxMap[PPC::PSTB8] = PPC::STBX8; 151 ImmToIdxMap[PPC::PSTH] = PPC::STHX; ImmToIdxMap[PPC::PSTH8] = PPC::STHX8; 152 ImmToIdxMap[PPC::PSTW] = PPC::STWX; ImmToIdxMap[PPC::PSTW8] = PPC::STWX8; 153 154 ImmToIdxMap[PPC::PLFS] = PPC::LFSX; ImmToIdxMap[PPC::PSTFS] = PPC::STFSX; 155 ImmToIdxMap[PPC::PLFD] = PPC::LFDX; ImmToIdxMap[PPC::PSTFD] = PPC::STFDX; 156 ImmToIdxMap[PPC::PLXSSP] = PPC::LXSSPX; ImmToIdxMap[PPC::PSTXSSP] = PPC::STXSSPX; 157 ImmToIdxMap[PPC::PLXSD] = PPC::LXSDX; ImmToIdxMap[PPC::PSTXSD] = PPC::STXSDX; 158 ImmToIdxMap[PPC::PLXV] = PPC::LXVX; ImmToIdxMap[PPC::PSTXV] = PPC::STXVX; 159 160 ImmToIdxMap[PPC::LXVP] = PPC::LXVPX; 161 ImmToIdxMap[PPC::STXVP] = PPC::STXVPX; 162 ImmToIdxMap[PPC::PLXVP] = PPC::LXVPX; 163 ImmToIdxMap[PPC::PSTXVP] = PPC::STXVPX; 164 } 165 166 /// getPointerRegClass - Return the register class to use to hold pointers. 167 /// This is used for addressing modes. 168 const TargetRegisterClass * 169 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) 170 const { 171 // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value 172 // when it checks for ZERO folding. 173 if (Kind == 1) { 174 if (TM.isPPC64()) 175 return &PPC::G8RC_NOX0RegClass; 176 return &PPC::GPRC_NOR0RegClass; 177 } 178 179 if (TM.isPPC64()) 180 return &PPC::G8RCRegClass; 181 return &PPC::GPRCRegClass; 182 } 183 184 const MCPhysReg* 185 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 186 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>(); 187 if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) { 188 if (!TM.isPPC64() && Subtarget.isAIXABI()) 189 report_fatal_error("AnyReg unimplemented on 32-bit AIX."); 190 if (Subtarget.hasVSX()) { 191 if (Subtarget.pairedVectorMemops()) 192 return CSR_64_AllRegs_VSRP_SaveList; 193 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) 194 return CSR_64_AllRegs_AIX_Dflt_VSX_SaveList; 195 return CSR_64_AllRegs_VSX_SaveList; 196 } 197 if (Subtarget.hasAltivec()) { 198 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) 199 return CSR_64_AllRegs_AIX_Dflt_Altivec_SaveList; 200 return CSR_64_AllRegs_Altivec_SaveList; 201 } 202 return CSR_64_AllRegs_SaveList; 203 } 204 205 // On PPC64, we might need to save r2 (but only if it is not reserved). 206 // We do not need to treat R2 as callee-saved when using PC-Relative calls 207 // because any direct uses of R2 will cause it to be reserved. If the function 208 // is a leaf or the only uses of R2 are implicit uses for calls, the calls 209 // will use the @notoc relocation which will cause this function to set the 210 // st_other bit to 1, thereby communicating to its caller that it arbitrarily 211 // clobbers the TOC. 212 bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) && 213 !Subtarget.isUsingPCRelativeCalls(); 214 215 // Cold calling convention CSRs. 216 if (MF->getFunction().getCallingConv() == CallingConv::Cold) { 217 if (Subtarget.isAIXABI()) 218 report_fatal_error("Cold calling unimplemented on AIX."); 219 if (TM.isPPC64()) { 220 if (Subtarget.pairedVectorMemops()) 221 return SaveR2 ? CSR_SVR64_ColdCC_R2_VSRP_SaveList 222 : CSR_SVR64_ColdCC_VSRP_SaveList; 223 if (Subtarget.hasAltivec()) 224 return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList 225 : CSR_SVR64_ColdCC_Altivec_SaveList; 226 return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList 227 : CSR_SVR64_ColdCC_SaveList; 228 } 229 // 32-bit targets. 230 if (Subtarget.pairedVectorMemops()) 231 return CSR_SVR32_ColdCC_VSRP_SaveList; 232 else if (Subtarget.hasAltivec()) 233 return CSR_SVR32_ColdCC_Altivec_SaveList; 234 else if (Subtarget.hasSPE()) 235 return CSR_SVR32_ColdCC_SPE_SaveList; 236 return CSR_SVR32_ColdCC_SaveList; 237 } 238 // Standard calling convention CSRs. 239 if (TM.isPPC64()) { 240 if (Subtarget.pairedVectorMemops()) { 241 if (Subtarget.isAIXABI()) { 242 if (!TM.getAIXExtendedAltivecABI()) 243 return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList; 244 return SaveR2 ? CSR_AIX64_R2_VSRP_SaveList : CSR_AIX64_VSRP_SaveList; 245 } 246 return SaveR2 ? CSR_SVR464_R2_VSRP_SaveList : CSR_SVR464_VSRP_SaveList; 247 } 248 if (Subtarget.hasAltivec() && 249 (!Subtarget.isAIXABI() || TM.getAIXExtendedAltivecABI())) { 250 return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList 251 : CSR_PPC64_Altivec_SaveList; 252 } 253 return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList; 254 } 255 // 32-bit targets. 256 if (Subtarget.isAIXABI()) { 257 if (Subtarget.pairedVectorMemops()) 258 return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_VSRP_SaveList 259 : CSR_AIX32_SaveList; 260 if (Subtarget.hasAltivec()) 261 return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_Altivec_SaveList 262 : CSR_AIX32_SaveList; 263 return CSR_AIX32_SaveList; 264 } 265 if (Subtarget.pairedVectorMemops()) 266 return CSR_SVR432_VSRP_SaveList; 267 if (Subtarget.hasAltivec()) 268 return CSR_SVR432_Altivec_SaveList; 269 else if (Subtarget.hasSPE()) { 270 if (TM.isPositionIndependent() && !TM.isPPC64()) 271 return CSR_SVR432_SPE_NO_S30_31_SaveList; 272 return CSR_SVR432_SPE_SaveList; 273 } 274 return CSR_SVR432_SaveList; 275 } 276 277 const uint32_t * 278 PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF, 279 CallingConv::ID CC) const { 280 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 281 if (CC == CallingConv::AnyReg) { 282 if (Subtarget.hasVSX()) { 283 if (Subtarget.pairedVectorMemops()) 284 return CSR_64_AllRegs_VSRP_RegMask; 285 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) 286 return CSR_64_AllRegs_AIX_Dflt_VSX_RegMask; 287 return CSR_64_AllRegs_VSX_RegMask; 288 } 289 if (Subtarget.hasAltivec()) { 290 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) 291 return CSR_64_AllRegs_AIX_Dflt_Altivec_RegMask; 292 return CSR_64_AllRegs_Altivec_RegMask; 293 } 294 return CSR_64_AllRegs_RegMask; 295 } 296 297 if (Subtarget.isAIXABI()) { 298 if (Subtarget.pairedVectorMemops()) { 299 if (!TM.getAIXExtendedAltivecABI()) 300 return TM.isPPC64() ? CSR_PPC64_RegMask : CSR_AIX32_RegMask; 301 return TM.isPPC64() ? CSR_AIX64_VSRP_RegMask : CSR_AIX32_VSRP_RegMask; 302 } 303 return TM.isPPC64() 304 ? ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI()) 305 ? CSR_PPC64_Altivec_RegMask 306 : CSR_PPC64_RegMask) 307 : ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI()) 308 ? CSR_AIX32_Altivec_RegMask 309 : CSR_AIX32_RegMask); 310 } 311 312 if (CC == CallingConv::Cold) { 313 if (TM.isPPC64()) 314 return Subtarget.pairedVectorMemops() 315 ? CSR_SVR64_ColdCC_VSRP_RegMask 316 : (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask 317 : CSR_SVR64_ColdCC_RegMask); 318 else 319 return Subtarget.pairedVectorMemops() 320 ? CSR_SVR32_ColdCC_VSRP_RegMask 321 : (Subtarget.hasAltivec() 322 ? CSR_SVR32_ColdCC_Altivec_RegMask 323 : (Subtarget.hasSPE() ? CSR_SVR32_ColdCC_SPE_RegMask 324 : CSR_SVR32_ColdCC_RegMask)); 325 } 326 327 if (TM.isPPC64()) 328 return Subtarget.pairedVectorMemops() 329 ? CSR_SVR464_VSRP_RegMask 330 : (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask 331 : CSR_PPC64_RegMask); 332 else 333 return Subtarget.pairedVectorMemops() 334 ? CSR_SVR432_VSRP_RegMask 335 : (Subtarget.hasAltivec() 336 ? CSR_SVR432_Altivec_RegMask 337 : (Subtarget.hasSPE() 338 ? (TM.isPositionIndependent() 339 ? CSR_SVR432_SPE_NO_S30_31_RegMask 340 : CSR_SVR432_SPE_RegMask) 341 : CSR_SVR432_RegMask)); 342 } 343 344 const uint32_t* 345 PPCRegisterInfo::getNoPreservedMask() const { 346 return CSR_NoRegs_RegMask; 347 } 348 349 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const { 350 for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM}) 351 Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32)); 352 } 353 354 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 355 BitVector Reserved(getNumRegs()); 356 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 357 const PPCFrameLowering *TFI = getFrameLowering(MF); 358 359 // The ZERO register is not really a register, but the representation of r0 360 // when used in instructions that treat r0 as the constant 0. 361 markSuperRegs(Reserved, PPC::ZERO); 362 363 // The FP register is also not really a register, but is the representation 364 // of the frame pointer register used by ISD::FRAMEADDR. 365 markSuperRegs(Reserved, PPC::FP); 366 367 // The BP register is also not really a register, but is the representation 368 // of the base pointer register used by setjmp. 369 markSuperRegs(Reserved, PPC::BP); 370 371 // The counter registers must be reserved so that counter-based loops can 372 // be correctly formed (and the mtctr instructions are not DCE'd). 373 markSuperRegs(Reserved, PPC::CTR); 374 markSuperRegs(Reserved, PPC::CTR8); 375 376 markSuperRegs(Reserved, PPC::R1); 377 markSuperRegs(Reserved, PPC::LR); 378 markSuperRegs(Reserved, PPC::LR8); 379 markSuperRegs(Reserved, PPC::RM); 380 381 markSuperRegs(Reserved, PPC::VRSAVE); 382 383 // The SVR4 ABI reserves r2 and r13 384 if (Subtarget.isSVR4ABI()) { 385 // We only reserve r2 if we need to use the TOC pointer. If we have no 386 // explicit uses of the TOC pointer (meaning we're a leaf function with 387 // no constant-pool loads, etc.) and we have no potential uses inside an 388 // inline asm block, then we can treat r2 has an ordinary callee-saved 389 // register. 390 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 391 if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm()) 392 markSuperRegs(Reserved, PPC::R2); // System-reserved register 393 markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register 394 } 395 396 // Always reserve r2 on AIX for now. 397 // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions. 398 if (Subtarget.isAIXABI()) 399 markSuperRegs(Reserved, PPC::R2); // System-reserved register 400 401 // On PPC64, r13 is the thread pointer. Never allocate this register. 402 if (TM.isPPC64()) 403 markSuperRegs(Reserved, PPC::R13); 404 405 if (TFI->needsFP(MF)) 406 markSuperRegs(Reserved, PPC::R31); 407 408 bool IsPositionIndependent = TM.isPositionIndependent(); 409 if (hasBasePointer(MF)) { 410 if (Subtarget.is32BitELFABI() && IsPositionIndependent) 411 markSuperRegs(Reserved, PPC::R29); 412 else 413 markSuperRegs(Reserved, PPC::R30); 414 } 415 416 if (Subtarget.is32BitELFABI() && IsPositionIndependent) 417 markSuperRegs(Reserved, PPC::R30); 418 419 // Reserve Altivec registers when Altivec is unavailable. 420 if (!Subtarget.hasAltivec()) 421 for (MCRegister Reg : PPC::VRRCRegClass) 422 markSuperRegs(Reserved, Reg); 423 424 if (Subtarget.isAIXABI() && Subtarget.hasAltivec() && 425 !TM.getAIXExtendedAltivecABI()) { 426 // In the AIX default Altivec ABI, vector registers VR20-VR31 are reserved 427 // and cannot be used. 428 for (auto Reg : CSR_Altivec_SaveList) { 429 if (Reg == 0) 430 break; 431 markSuperRegs(Reserved, Reg); 432 for (MCRegAliasIterator AS(Reg, this, true); AS.isValid(); ++AS) { 433 Reserved.set(*AS); 434 } 435 } 436 } 437 438 assert(checkAllSuperRegsMarked(Reserved)); 439 return Reserved; 440 } 441 442 bool PPCRegisterInfo::isAsmClobberable(const MachineFunction &MF, 443 MCRegister PhysReg) const { 444 // We cannot use getReservedRegs() to find the registers that are not asm 445 // clobberable because there are some reserved registers which can be 446 // clobbered by inline asm. For example, when LR is clobbered, the register is 447 // saved and restored. We will hardcode the registers that are not asm 448 // cloberable in this function. 449 450 // The stack pointer (R1/X1) is not clobberable by inline asm 451 return PhysReg != PPC::R1 && PhysReg != PPC::X1; 452 } 453 454 bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const { 455 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 456 const PPCInstrInfo *InstrInfo = Subtarget.getInstrInfo(); 457 const MachineFrameInfo &MFI = MF.getFrameInfo(); 458 const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo(); 459 460 LLVM_DEBUG(dbgs() << "requiresFrameIndexScavenging for " << MF.getName() 461 << ".\n"); 462 // If the callee saved info is invalid we have to default to true for safety. 463 if (!MFI.isCalleeSavedInfoValid()) { 464 LLVM_DEBUG(dbgs() << "TRUE - Invalid callee saved info.\n"); 465 return true; 466 } 467 468 // We will require the use of X-Forms because the frame is larger than what 469 // can be represented in signed 16 bits that fit in the immediate of a D-Form. 470 // If we need an X-Form then we need a register to store the address offset. 471 unsigned FrameSize = MFI.getStackSize(); 472 // Signed 16 bits means that the FrameSize cannot be more than 15 bits. 473 if (FrameSize & ~0x7FFF) { 474 LLVM_DEBUG(dbgs() << "TRUE - Frame size is too large for D-Form.\n"); 475 return true; 476 } 477 478 // The callee saved info is valid so it can be traversed. 479 // Checking for registers that need saving that do not have load or store 480 // forms where the address offset is an immediate. 481 for (const CalleeSavedInfo &CSI : Info) { 482 // If the spill is to a register no scavenging is required. 483 if (CSI.isSpilledToReg()) 484 continue; 485 486 int FrIdx = CSI.getFrameIdx(); 487 Register Reg = CSI.getReg(); 488 489 const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg); 490 unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC); 491 if (!MFI.isFixedObjectIndex(FrIdx)) { 492 // This is not a fixed object. If it requires alignment then we may still 493 // need to use the XForm. 494 if (offsetMinAlignForOpcode(Opcode) > 1) { 495 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode) 496 << " for register " << printReg(Reg, this) << ".\n"); 497 LLVM_DEBUG(dbgs() << "TRUE - Not fixed frame object that requires " 498 << "alignment.\n"); 499 return true; 500 } 501 } 502 503 // This is eiher: 504 // 1) A fixed frame index object which we know are aligned so 505 // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't 506 // need to consider the alignment here. 507 // 2) A not fixed object but in that case we now know that the min required 508 // alignment is no more than 1 based on the previous check. 509 if (InstrInfo->isXFormMemOp(Opcode)) { 510 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode) 511 << " for register " << printReg(Reg, this) << ".\n"); 512 LLVM_DEBUG(dbgs() << "TRUE - Memory operand is X-Form.\n"); 513 return true; 514 } 515 516 // This is a spill/restore of a quadword. 517 if ((Opcode == PPC::RESTORE_QUADWORD) || (Opcode == PPC::SPILL_QUADWORD)) { 518 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode) 519 << " for register " << printReg(Reg, this) << ".\n"); 520 LLVM_DEBUG(dbgs() << "TRUE - Memory operand is a quadword.\n"); 521 return true; 522 } 523 } 524 LLVM_DEBUG(dbgs() << "FALSE - Scavenging is not required.\n"); 525 return false; 526 } 527 528 bool PPCRegisterInfo::requiresVirtualBaseRegisters( 529 const MachineFunction &MF) const { 530 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 531 // Do not use virtual base registers when ROP protection is turned on. 532 // Virtual base registers break the layout of the local variable space and may 533 // push the ROP Hash location past the 512 byte range of the ROP store 534 // instruction. 535 return !Subtarget.hasROPProtect(); 536 } 537 538 bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg, 539 const MachineFunction &MF) const { 540 assert(Register::isPhysicalRegister(PhysReg)); 541 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 542 const MachineFrameInfo &MFI = MF.getFrameInfo(); 543 544 if (!Subtarget.is64BitELFABI() && !Subtarget.isAIXABI()) 545 return false; 546 if (PhysReg == Subtarget.getTOCPointerRegister()) 547 // X2/R2 is guaranteed to be preserved within a function if it is reserved. 548 // The reason it's reserved is that it's the TOC pointer (and the function 549 // uses the TOC). In functions where it isn't reserved (i.e. leaf functions 550 // with no TOC access), we can't claim that it is preserved. 551 return (getReservedRegs(MF).test(PhysReg)); 552 if (StackPtrConst && PhysReg == Subtarget.getStackPointerRegister() && 553 !MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment()) 554 // The value of the stack pointer does not change within a function after 555 // the prologue and before the epilogue if there are no dynamic allocations 556 // and no inline asm which clobbers X1/R1. 557 return true; 558 return false; 559 } 560 561 bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg, 562 ArrayRef<MCPhysReg> Order, 563 SmallVectorImpl<MCPhysReg> &Hints, 564 const MachineFunction &MF, 565 const VirtRegMap *VRM, 566 const LiveRegMatrix *Matrix) const { 567 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 568 569 // Call the base implementation first to set any hints based on the usual 570 // heuristics and decide what the return value should be. We want to return 571 // the same value returned by the base implementation. If the base 572 // implementation decides to return true and force the allocation then we 573 // will leave it as such. On the other hand if the base implementation 574 // decides to return false the following code will not force the allocation 575 // as we are just looking to provide a hint. 576 bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints( 577 VirtReg, Order, Hints, MF, VRM, Matrix); 578 // We are interested in instructions that copy values to ACC/UACC. 579 // The copy into UACC will be simply a COPY to a subreg so we 580 // want to allocate the corresponding physical subreg for the source. 581 // The copy into ACC will be a BUILD_UACC so we want to allocate 582 // the same number UACC for the source. 583 const TargetRegisterClass *RegClass = MRI->getRegClass(VirtReg); 584 for (MachineInstr &Use : MRI->reg_nodbg_instructions(VirtReg)) { 585 const MachineOperand *ResultOp = nullptr; 586 Register ResultReg; 587 switch (Use.getOpcode()) { 588 case TargetOpcode::COPY: { 589 ResultOp = &Use.getOperand(0); 590 ResultReg = ResultOp->getReg(); 591 if (Register::isVirtualRegister(ResultReg) && 592 MRI->getRegClass(ResultReg)->contains(PPC::UACC0) && 593 VRM->hasPhys(ResultReg)) { 594 Register UACCPhys = VRM->getPhys(ResultReg); 595 Register HintReg; 596 if (RegClass->contains(PPC::VSRp0)) { 597 HintReg = getSubReg(UACCPhys, ResultOp->getSubReg()); 598 // Ensure that the hint is a VSRp register. 599 if (HintReg >= PPC::VSRp0 && HintReg <= PPC::VSRp31) 600 Hints.push_back(HintReg); 601 } else if (RegClass->contains(PPC::ACC0)) { 602 HintReg = PPC::ACC0 + (UACCPhys - PPC::UACC0); 603 if (HintReg >= PPC::ACC0 && HintReg <= PPC::ACC7) 604 Hints.push_back(HintReg); 605 } 606 } 607 break; 608 } 609 case PPC::BUILD_UACC: { 610 ResultOp = &Use.getOperand(0); 611 ResultReg = ResultOp->getReg(); 612 if (MRI->getRegClass(ResultReg)->contains(PPC::ACC0) && 613 VRM->hasPhys(ResultReg)) { 614 Register ACCPhys = VRM->getPhys(ResultReg); 615 assert((ACCPhys >= PPC::ACC0 && ACCPhys <= PPC::ACC7) && 616 "Expecting an ACC register for BUILD_UACC."); 617 Register HintReg = PPC::UACC0 + (ACCPhys - PPC::ACC0); 618 Hints.push_back(HintReg); 619 } 620 break; 621 } 622 } 623 } 624 return BaseImplRetVal; 625 } 626 627 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 628 MachineFunction &MF) const { 629 const PPCFrameLowering *TFI = getFrameLowering(MF); 630 const unsigned DefaultSafety = 1; 631 632 switch (RC->getID()) { 633 default: 634 return 0; 635 case PPC::G8RC_NOX0RegClassID: 636 case PPC::GPRC_NOR0RegClassID: 637 case PPC::SPERCRegClassID: 638 case PPC::G8RCRegClassID: 639 case PPC::GPRCRegClassID: { 640 unsigned FP = TFI->hasFP(MF) ? 1 : 0; 641 return 32 - FP - DefaultSafety; 642 } 643 case PPC::F4RCRegClassID: 644 case PPC::F8RCRegClassID: 645 case PPC::VSLRCRegClassID: 646 return 32 - DefaultSafety; 647 case PPC::VFRCRegClassID: 648 case PPC::VRRCRegClassID: { 649 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 650 // Vector registers VR20-VR31 are reserved and cannot be used in the default 651 // Altivec ABI on AIX. 652 if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI()) 653 return 20 - DefaultSafety; 654 } 655 return 32 - DefaultSafety; 656 case PPC::VSFRCRegClassID: 657 case PPC::VSSRCRegClassID: 658 case PPC::VSRCRegClassID: { 659 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 660 if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI()) 661 // Vector registers VR20-VR31 are reserved and cannot be used in the 662 // default Altivec ABI on AIX. 663 return 52 - DefaultSafety; 664 } 665 return 64 - DefaultSafety; 666 case PPC::CRRCRegClassID: 667 return 8 - DefaultSafety; 668 } 669 } 670 671 const TargetRegisterClass * 672 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 673 const MachineFunction &MF) const { 674 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 675 const auto *DefaultSuperclass = 676 TargetRegisterInfo::getLargestLegalSuperClass(RC, MF); 677 if (Subtarget.hasVSX()) { 678 // With VSX, we can inflate various sub-register classes to the full VSX 679 // register set. 680 681 // For Power9 we allow the user to enable GPR to vector spills. 682 // FIXME: Currently limited to spilling GP8RC. A follow on patch will add 683 // support to spill GPRC. 684 if (TM.isELFv2ABI() || Subtarget.isAIXABI()) { 685 if (Subtarget.hasP9Vector() && EnableGPRToVecSpills && 686 RC == &PPC::G8RCRegClass) { 687 InflateGP8RC++; 688 return &PPC::SPILLTOVSRRCRegClass; 689 } 690 if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills) 691 InflateGPRC++; 692 } 693 694 for (const auto *I = RC->getSuperClasses(); *I; ++I) { 695 if (getRegSizeInBits(**I) != getRegSizeInBits(*RC)) 696 continue; 697 698 switch ((*I)->getID()) { 699 case PPC::VSSRCRegClassID: 700 return Subtarget.hasP8Vector() ? *I : DefaultSuperclass; 701 case PPC::VSFRCRegClassID: 702 case PPC::VSRCRegClassID: 703 return *I; 704 case PPC::VSRpRCRegClassID: 705 return Subtarget.pairedVectorMemops() ? *I : DefaultSuperclass; 706 case PPC::ACCRCRegClassID: 707 case PPC::UACCRCRegClassID: 708 return Subtarget.hasMMA() ? *I : DefaultSuperclass; 709 } 710 } 711 } 712 713 return DefaultSuperclass; 714 } 715 716 //===----------------------------------------------------------------------===// 717 // Stack Frame Processing methods 718 //===----------------------------------------------------------------------===// 719 720 /// lowerDynamicAlloc - Generate the code for allocating an object in the 721 /// current frame. The sequence of code will be in the general form 722 /// 723 /// addi R0, SP, \#frameSize ; get the address of the previous frame 724 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size 725 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation 726 /// 727 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const { 728 // Get the instruction. 729 MachineInstr &MI = *II; 730 // Get the instruction's basic block. 731 MachineBasicBlock &MBB = *MI.getParent(); 732 // Get the basic block's function. 733 MachineFunction &MF = *MBB.getParent(); 734 // Get the frame info. 735 MachineFrameInfo &MFI = MF.getFrameInfo(); 736 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 737 // Get the instruction info. 738 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 739 // Determine whether 64-bit pointers are used. 740 bool LP64 = TM.isPPC64(); 741 DebugLoc dl = MI.getDebugLoc(); 742 743 // Get the maximum call stack size. 744 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 745 Align MaxAlign = MFI.getMaxAlign(); 746 assert(isAligned(MaxAlign, maxCallFrameSize) && 747 "Maximum call-frame size not sufficiently aligned"); 748 (void)MaxAlign; 749 750 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 751 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 752 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 753 bool KillNegSizeReg = MI.getOperand(1).isKill(); 754 Register NegSizeReg = MI.getOperand(1).getReg(); 755 756 prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg); 757 // Grow the stack and update the stack pointer link, then determine the 758 // address of new allocated space. 759 if (LP64) { 760 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1) 761 .addReg(Reg, RegState::Kill) 762 .addReg(PPC::X1) 763 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 764 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 765 .addReg(PPC::X1) 766 .addImm(maxCallFrameSize); 767 } else { 768 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1) 769 .addReg(Reg, RegState::Kill) 770 .addReg(PPC::R1) 771 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 772 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 773 .addReg(PPC::R1) 774 .addImm(maxCallFrameSize); 775 } 776 777 // Discard the DYNALLOC instruction. 778 MBB.erase(II); 779 } 780 781 /// To accomplish dynamic stack allocation, we have to calculate exact size 782 /// subtracted from the stack pointer according alignment information and get 783 /// previous frame pointer. 784 void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II, 785 Register &NegSizeReg, 786 bool &KillNegSizeReg, 787 Register &FramePointer) const { 788 // Get the instruction. 789 MachineInstr &MI = *II; 790 // Get the instruction's basic block. 791 MachineBasicBlock &MBB = *MI.getParent(); 792 // Get the basic block's function. 793 MachineFunction &MF = *MBB.getParent(); 794 // Get the frame info. 795 MachineFrameInfo &MFI = MF.getFrameInfo(); 796 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 797 // Get the instruction info. 798 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 799 // Determine whether 64-bit pointers are used. 800 bool LP64 = TM.isPPC64(); 801 DebugLoc dl = MI.getDebugLoc(); 802 // Get the total frame size. 803 unsigned FrameSize = MFI.getStackSize(); 804 805 // Get stack alignments. 806 const PPCFrameLowering *TFI = getFrameLowering(MF); 807 Align TargetAlign = TFI->getStackAlign(); 808 Align MaxAlign = MFI.getMaxAlign(); 809 810 // Determine the previous frame's address. If FrameSize can't be 811 // represented as 16 bits or we need special alignment, then we load the 812 // previous frame's address from 0(SP). Why not do an addis of the hi? 813 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 814 // Constructing the constant and adding would take 3 instructions. 815 // Fortunately, a frame greater than 32K is rare. 816 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 817 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 818 819 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 820 if (LP64) 821 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer) 822 .addReg(PPC::X31) 823 .addImm(FrameSize); 824 else 825 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer) 826 .addReg(PPC::R31) 827 .addImm(FrameSize); 828 } else if (LP64) { 829 BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer) 830 .addImm(0) 831 .addReg(PPC::X1); 832 } else { 833 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer) 834 .addImm(0) 835 .addReg(PPC::R1); 836 } 837 // Determine the actual NegSizeReg according to alignment info. 838 if (LP64) { 839 if (MaxAlign > TargetAlign) { 840 unsigned UnalNegSizeReg = NegSizeReg; 841 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 842 843 // Unfortunately, there is no andi, only andi., and we can't insert that 844 // here because we might clobber cr0 while it is live. 845 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg) 846 .addImm(~(MaxAlign.value() - 1)); 847 848 unsigned NegSizeReg1 = NegSizeReg; 849 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 850 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg) 851 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 852 .addReg(NegSizeReg1, RegState::Kill); 853 KillNegSizeReg = true; 854 } 855 } else { 856 if (MaxAlign > TargetAlign) { 857 unsigned UnalNegSizeReg = NegSizeReg; 858 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 859 860 // Unfortunately, there is no andi, only andi., and we can't insert that 861 // here because we might clobber cr0 while it is live. 862 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg) 863 .addImm(~(MaxAlign.value() - 1)); 864 865 unsigned NegSizeReg1 = NegSizeReg; 866 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 867 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg) 868 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 869 .addReg(NegSizeReg1, RegState::Kill); 870 KillNegSizeReg = true; 871 } 872 } 873 } 874 875 void PPCRegisterInfo::lowerPrepareProbedAlloca( 876 MachineBasicBlock::iterator II) const { 877 MachineInstr &MI = *II; 878 // Get the instruction's basic block. 879 MachineBasicBlock &MBB = *MI.getParent(); 880 // Get the basic block's function. 881 MachineFunction &MF = *MBB.getParent(); 882 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 883 // Get the instruction info. 884 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 885 // Determine whether 64-bit pointers are used. 886 bool LP64 = TM.isPPC64(); 887 DebugLoc dl = MI.getDebugLoc(); 888 Register FramePointer = MI.getOperand(0).getReg(); 889 const Register ActualNegSizeReg = MI.getOperand(1).getReg(); 890 bool KillNegSizeReg = MI.getOperand(2).isKill(); 891 Register NegSizeReg = MI.getOperand(2).getReg(); 892 const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR); 893 // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg. 894 if (FramePointer == NegSizeReg) { 895 assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, " 896 "NegSizeReg should be killed"); 897 // FramePointer is clobbered earlier than the use of NegSizeReg in 898 // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid 899 // misuse. 900 BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg) 901 .addReg(NegSizeReg) 902 .addReg(NegSizeReg); 903 NegSizeReg = ActualNegSizeReg; 904 KillNegSizeReg = false; 905 } 906 prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer); 907 // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign > 908 // TargetAlign. 909 if (NegSizeReg != ActualNegSizeReg) 910 BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg) 911 .addReg(NegSizeReg) 912 .addReg(NegSizeReg); 913 MBB.erase(II); 914 } 915 916 void PPCRegisterInfo::lowerDynamicAreaOffset( 917 MachineBasicBlock::iterator II) const { 918 // Get the instruction. 919 MachineInstr &MI = *II; 920 // Get the instruction's basic block. 921 MachineBasicBlock &MBB = *MI.getParent(); 922 // Get the basic block's function. 923 MachineFunction &MF = *MBB.getParent(); 924 // Get the frame info. 925 MachineFrameInfo &MFI = MF.getFrameInfo(); 926 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 927 // Get the instruction info. 928 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 929 930 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 931 bool is64Bit = TM.isPPC64(); 932 DebugLoc dl = MI.getDebugLoc(); 933 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), 934 MI.getOperand(0).getReg()) 935 .addImm(maxCallFrameSize); 936 MBB.erase(II); 937 } 938 939 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 940 /// reserving a whole register (R0), we scrounge for one here. This generates 941 /// code like this: 942 /// 943 /// mfcr rA ; Move the conditional register into GPR rA. 944 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 945 /// stw rA, FI ; Store rA to the frame. 946 /// 947 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 948 unsigned FrameIndex) const { 949 // Get the instruction. 950 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 951 // Get the instruction's basic block. 952 MachineBasicBlock &MBB = *MI.getParent(); 953 MachineFunction &MF = *MBB.getParent(); 954 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 955 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 956 DebugLoc dl = MI.getDebugLoc(); 957 958 bool LP64 = TM.isPPC64(); 959 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 960 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 961 962 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 963 Register SrcReg = MI.getOperand(0).getReg(); 964 965 // We need to store the CR in the low 4-bits of the saved value. First, issue 966 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg. 967 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 968 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 969 970 // If the saved register wasn't CR0, shift the bits left so that they are in 971 // CR0's slot. 972 if (SrcReg != PPC::CR0) { 973 Register Reg1 = Reg; 974 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 975 976 // rlwinm rA, rA, ShiftBits, 0, 31. 977 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 978 .addReg(Reg1, RegState::Kill) 979 .addImm(getEncodingValue(SrcReg) * 4) 980 .addImm(0) 981 .addImm(31); 982 } 983 984 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 985 .addReg(Reg, RegState::Kill), 986 FrameIndex); 987 988 // Discard the pseudo instruction. 989 MBB.erase(II); 990 } 991 992 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 993 unsigned FrameIndex) const { 994 // Get the instruction. 995 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 996 // Get the instruction's basic block. 997 MachineBasicBlock &MBB = *MI.getParent(); 998 MachineFunction &MF = *MBB.getParent(); 999 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1000 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1001 DebugLoc dl = MI.getDebugLoc(); 1002 1003 bool LP64 = TM.isPPC64(); 1004 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1005 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1006 1007 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1008 Register DestReg = MI.getOperand(0).getReg(); 1009 assert(MI.definesRegister(DestReg) && 1010 "RESTORE_CR does not define its destination"); 1011 1012 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 1013 Reg), FrameIndex); 1014 1015 // If the reloaded register isn't CR0, shift the bits right so that they are 1016 // in the right CR's slot. 1017 if (DestReg != PPC::CR0) { 1018 Register Reg1 = Reg; 1019 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1020 1021 unsigned ShiftBits = getEncodingValue(DestReg)*4; 1022 // rlwinm r11, r11, 32-ShiftBits, 0, 31. 1023 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 1024 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0) 1025 .addImm(31); 1026 } 1027 1028 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg) 1029 .addReg(Reg, RegState::Kill); 1030 1031 // Discard the pseudo instruction. 1032 MBB.erase(II); 1033 } 1034 1035 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II, 1036 unsigned FrameIndex) const { 1037 // Get the instruction. 1038 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset> 1039 // Get the instruction's basic block. 1040 MachineBasicBlock &MBB = *MI.getParent(); 1041 MachineFunction &MF = *MBB.getParent(); 1042 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1043 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1044 const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo(); 1045 DebugLoc dl = MI.getDebugLoc(); 1046 1047 bool LP64 = TM.isPPC64(); 1048 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1049 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1050 1051 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1052 Register SrcReg = MI.getOperand(0).getReg(); 1053 1054 // Search up the BB to find the definition of the CR bit. 1055 MachineBasicBlock::reverse_iterator Ins = MI; 1056 MachineBasicBlock::reverse_iterator Rend = MBB.rend(); 1057 ++Ins; 1058 unsigned CRBitSpillDistance = 0; 1059 bool SeenUse = false; 1060 for (; Ins != Rend; ++Ins) { 1061 // Definition found. 1062 if (Ins->modifiesRegister(SrcReg, TRI)) 1063 break; 1064 // Use found. 1065 if (Ins->readsRegister(SrcReg, TRI)) 1066 SeenUse = true; 1067 // Unable to find CR bit definition within maximum search distance. 1068 if (CRBitSpillDistance == MaxCRBitSpillDist) { 1069 Ins = MI; 1070 break; 1071 } 1072 // Skip debug instructions when counting CR bit spill distance. 1073 if (!Ins->isDebugInstr()) 1074 CRBitSpillDistance++; 1075 } 1076 1077 // Unable to find the definition of the CR bit in the MBB. 1078 if (Ins == MBB.rend()) 1079 Ins = MI; 1080 1081 bool SpillsKnownBit = false; 1082 // There is no need to extract the CR bit if its value is already known. 1083 switch (Ins->getOpcode()) { 1084 case PPC::CRUNSET: 1085 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg) 1086 .addImm(0); 1087 SpillsKnownBit = true; 1088 break; 1089 case PPC::CRSET: 1090 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg) 1091 .addImm(-32768); 1092 SpillsKnownBit = true; 1093 break; 1094 default: 1095 // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all 1096 // bits (specifically, it produces a -1 if the CR bit is set). Ultimately, 1097 // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit 1098 // register), and SETNBC will set this. 1099 if (Subtarget.isISA3_1()) { 1100 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg) 1101 .addReg(SrcReg, RegState::Undef); 1102 break; 1103 } 1104 1105 // On Power9, we can use SETB to extract the LT bit. This only works for 1106 // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value 1107 // of the bit we care about (32-bit sign bit) will be set to the value of 1108 // the LT bit (regardless of the other bits in the CR field). 1109 if (Subtarget.isISA3_0()) { 1110 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT || 1111 SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT || 1112 SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT || 1113 SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) { 1114 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg) 1115 .addReg(getCRFromCRBit(SrcReg), RegState::Undef); 1116 break; 1117 } 1118 } 1119 1120 // We need to move the CR field that contains the CR bit we are spilling. 1121 // The super register may not be explicitly defined (i.e. it can be defined 1122 // by a CR-logical that only defines the subreg) so we state that the CR 1123 // field is undef. Also, in order to preserve the kill flag on the CR bit, 1124 // we add it as an implicit use. 1125 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 1126 .addReg(getCRFromCRBit(SrcReg), RegState::Undef) 1127 .addReg(SrcReg, 1128 RegState::Implicit | getKillRegState(MI.getOperand(0).isKill())); 1129 1130 // If the saved register wasn't CR0LT, shift the bits left so that the bit 1131 // to store is the first one. Mask all but that bit. 1132 Register Reg1 = Reg; 1133 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1134 1135 // rlwinm rA, rA, ShiftBits, 0, 0. 1136 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 1137 .addReg(Reg1, RegState::Kill) 1138 .addImm(getEncodingValue(SrcReg)) 1139 .addImm(0).addImm(0); 1140 } 1141 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 1142 .addReg(Reg, RegState::Kill), 1143 FrameIndex); 1144 1145 bool KillsCRBit = MI.killsRegister(SrcReg, TRI); 1146 // Discard the pseudo instruction. 1147 MBB.erase(II); 1148 if (SpillsKnownBit && KillsCRBit && !SeenUse) { 1149 Ins->setDesc(TII.get(PPC::UNENCODED_NOP)); 1150 Ins->removeOperand(0); 1151 } 1152 } 1153 1154 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, 1155 unsigned FrameIndex) const { 1156 // Get the instruction. 1157 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset> 1158 // Get the instruction's basic block. 1159 MachineBasicBlock &MBB = *MI.getParent(); 1160 MachineFunction &MF = *MBB.getParent(); 1161 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1162 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1163 DebugLoc dl = MI.getDebugLoc(); 1164 1165 bool LP64 = TM.isPPC64(); 1166 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1167 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1168 1169 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1170 Register DestReg = MI.getOperand(0).getReg(); 1171 assert(MI.definesRegister(DestReg) && 1172 "RESTORE_CRBIT does not define its destination"); 1173 1174 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 1175 Reg), FrameIndex); 1176 1177 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg); 1178 1179 Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1180 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO) 1181 .addReg(getCRFromCRBit(DestReg)); 1182 1183 unsigned ShiftBits = getEncodingValue(DestReg); 1184 // rlwimi r11, r10, 32-ShiftBits, ..., ... 1185 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO) 1186 .addReg(RegO, RegState::Kill) 1187 .addReg(Reg, RegState::Kill) 1188 .addImm(ShiftBits ? 32 - ShiftBits : 0) 1189 .addImm(ShiftBits) 1190 .addImm(ShiftBits); 1191 1192 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), 1193 getCRFromCRBit(DestReg)) 1194 .addReg(RegO, RegState::Kill) 1195 // Make sure we have a use dependency all the way through this 1196 // sequence of instructions. We can't have the other bits in the CR 1197 // modified in between the mfocrf and the mtocrf. 1198 .addReg(getCRFromCRBit(DestReg), RegState::Implicit); 1199 1200 // Discard the pseudo instruction. 1201 MBB.erase(II); 1202 } 1203 1204 void PPCRegisterInfo::emitAccCopyInfo(MachineBasicBlock &MBB, 1205 MCRegister DestReg, MCRegister SrcReg) { 1206 #ifdef NDEBUG 1207 return; 1208 #else 1209 if (ReportAccMoves) { 1210 std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc"; 1211 std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc"; 1212 dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n"; 1213 MBB.dump(); 1214 } 1215 #endif 1216 } 1217 1218 static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed, 1219 bool IsRestore) { 1220 #ifdef NDEBUG 1221 return; 1222 #else 1223 if (ReportAccMoves) { 1224 dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register " 1225 << (IsRestore ? "restore" : "spill") << ":\n"; 1226 MBB.dump(); 1227 } 1228 #endif 1229 } 1230 1231 static void spillRegPairs(MachineBasicBlock &MBB, 1232 MachineBasicBlock::iterator II, DebugLoc DL, 1233 const TargetInstrInfo &TII, Register SrcReg, 1234 unsigned FrameIndex, bool IsLittleEndian, 1235 bool IsKilled, bool TwoPairs) { 1236 unsigned Offset = 0; 1237 if (TwoPairs) 1238 Offset = IsLittleEndian ? 48 : 0; 1239 else 1240 Offset = IsLittleEndian ? 16 : 0; 1241 Register Reg = (SrcReg > PPC::VSRp15) ? PPC::V0 + (SrcReg - PPC::VSRp16) * 2 1242 : PPC::VSL0 + (SrcReg - PPC::VSRp0) * 2; 1243 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXV)) 1244 .addReg(Reg, getKillRegState(IsKilled)), 1245 FrameIndex, Offset); 1246 Offset += IsLittleEndian ? -16 : 16; 1247 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXV)) 1248 .addReg(Reg + 1, getKillRegState(IsKilled)), 1249 FrameIndex, Offset); 1250 if (TwoPairs) { 1251 Offset += IsLittleEndian ? -16 : 16; 1252 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXV)) 1253 .addReg(Reg + 2, getKillRegState(IsKilled)), 1254 FrameIndex, Offset); 1255 Offset += IsLittleEndian ? -16 : 16; 1256 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXV)) 1257 .addReg(Reg + 3, getKillRegState(IsKilled)), 1258 FrameIndex, Offset); 1259 } 1260 } 1261 1262 /// Remove any STXVP[X] instructions and split them out into a pair of 1263 /// STXV[X] instructions if --disable-auto-paired-vec-st is specified on 1264 /// the command line. 1265 void PPCRegisterInfo::lowerOctWordSpilling(MachineBasicBlock::iterator II, 1266 unsigned FrameIndex) const { 1267 assert(DisableAutoPairedVecSt && 1268 "Expecting to do this only if paired vector stores are disabled."); 1269 MachineInstr &MI = *II; // STXVP <SrcReg>, <offset> 1270 MachineBasicBlock &MBB = *MI.getParent(); 1271 MachineFunction &MF = *MBB.getParent(); 1272 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1273 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1274 DebugLoc DL = MI.getDebugLoc(); 1275 Register SrcReg = MI.getOperand(0).getReg(); 1276 bool IsLittleEndian = Subtarget.isLittleEndian(); 1277 bool IsKilled = MI.getOperand(0).isKill(); 1278 spillRegPairs(MBB, II, DL, TII, SrcReg, FrameIndex, IsLittleEndian, IsKilled, 1279 /* TwoPairs */ false); 1280 // Discard the original instruction. 1281 MBB.erase(II); 1282 } 1283 1284 /// lowerACCSpilling - Generate the code for spilling the accumulator register. 1285 /// Similarly to other spills/reloads that use pseudo-ops, we do not actually 1286 /// eliminate the FrameIndex here nor compute the stack offset. We simply 1287 /// create a real instruction with an FI and rely on eliminateFrameIndex to 1288 /// handle the FI elimination. 1289 void PPCRegisterInfo::lowerACCSpilling(MachineBasicBlock::iterator II, 1290 unsigned FrameIndex) const { 1291 MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset> 1292 MachineBasicBlock &MBB = *MI.getParent(); 1293 MachineFunction &MF = *MBB.getParent(); 1294 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1295 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1296 DebugLoc DL = MI.getDebugLoc(); 1297 Register SrcReg = MI.getOperand(0).getReg(); 1298 bool IsKilled = MI.getOperand(0).isKill(); 1299 1300 bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg); 1301 Register Reg = 1302 PPC::VSRp0 + (SrcReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2; 1303 bool IsLittleEndian = Subtarget.isLittleEndian(); 1304 1305 emitAccSpillRestoreInfo(MBB, IsPrimed, false); 1306 1307 // De-prime the register being spilled, create two stores for the pair 1308 // subregisters accounting for endianness and then re-prime the register if 1309 // it isn't killed. This uses the Offset parameter to addFrameReference() to 1310 // adjust the offset of the store that is within the 64-byte stack slot. 1311 if (IsPrimed) 1312 BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg); 1313 if (DisableAutoPairedVecSt) 1314 spillRegPairs(MBB, II, DL, TII, Reg, FrameIndex, IsLittleEndian, IsKilled, 1315 /* TwoPairs */ true); 1316 else { 1317 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1318 .addReg(Reg, getKillRegState(IsKilled)), 1319 FrameIndex, IsLittleEndian ? 32 : 0); 1320 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1321 .addReg(Reg + 1, getKillRegState(IsKilled)), 1322 FrameIndex, IsLittleEndian ? 0 : 32); 1323 } 1324 if (IsPrimed && !IsKilled) 1325 BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg); 1326 1327 // Discard the pseudo instruction. 1328 MBB.erase(II); 1329 } 1330 1331 /// lowerACCRestore - Generate the code to restore the accumulator register. 1332 void PPCRegisterInfo::lowerACCRestore(MachineBasicBlock::iterator II, 1333 unsigned FrameIndex) const { 1334 MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset> 1335 MachineBasicBlock &MBB = *MI.getParent(); 1336 MachineFunction &MF = *MBB.getParent(); 1337 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1338 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1339 DebugLoc DL = MI.getDebugLoc(); 1340 1341 Register DestReg = MI.getOperand(0).getReg(); 1342 assert(MI.definesRegister(DestReg) && 1343 "RESTORE_ACC does not define its destination"); 1344 1345 bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg); 1346 Register Reg = 1347 PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2; 1348 bool IsLittleEndian = Subtarget.isLittleEndian(); 1349 1350 emitAccSpillRestoreInfo(MBB, IsPrimed, true); 1351 1352 // Create two loads for the pair subregisters accounting for endianness and 1353 // then prime the accumulator register being restored. 1354 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg), 1355 FrameIndex, IsLittleEndian ? 32 : 0); 1356 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1), 1357 FrameIndex, IsLittleEndian ? 0 : 32); 1358 if (IsPrimed) 1359 BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg); 1360 1361 // Discard the pseudo instruction. 1362 MBB.erase(II); 1363 } 1364 1365 /// lowerQuadwordSpilling - Generate code to spill paired general register. 1366 void PPCRegisterInfo::lowerQuadwordSpilling(MachineBasicBlock::iterator II, 1367 unsigned FrameIndex) const { 1368 MachineInstr &MI = *II; 1369 MachineBasicBlock &MBB = *MI.getParent(); 1370 MachineFunction &MF = *MBB.getParent(); 1371 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1372 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1373 DebugLoc DL = MI.getDebugLoc(); 1374 1375 Register SrcReg = MI.getOperand(0).getReg(); 1376 bool IsKilled = MI.getOperand(0).isKill(); 1377 1378 Register Reg = PPC::X0 + (SrcReg - PPC::G8p0) * 2; 1379 bool IsLittleEndian = Subtarget.isLittleEndian(); 1380 1381 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD)) 1382 .addReg(Reg, getKillRegState(IsKilled)), 1383 FrameIndex, IsLittleEndian ? 8 : 0); 1384 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD)) 1385 .addReg(Reg + 1, getKillRegState(IsKilled)), 1386 FrameIndex, IsLittleEndian ? 0 : 8); 1387 1388 // Discard the pseudo instruction. 1389 MBB.erase(II); 1390 } 1391 1392 /// lowerQuadwordRestore - Generate code to restore paired general register. 1393 void PPCRegisterInfo::lowerQuadwordRestore(MachineBasicBlock::iterator II, 1394 unsigned FrameIndex) const { 1395 MachineInstr &MI = *II; 1396 MachineBasicBlock &MBB = *MI.getParent(); 1397 MachineFunction &MF = *MBB.getParent(); 1398 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1399 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1400 DebugLoc DL = MI.getDebugLoc(); 1401 1402 Register DestReg = MI.getOperand(0).getReg(); 1403 assert(MI.definesRegister(DestReg) && 1404 "RESTORE_QUADWORD does not define its destination"); 1405 1406 Register Reg = PPC::X0 + (DestReg - PPC::G8p0) * 2; 1407 bool IsLittleEndian = Subtarget.isLittleEndian(); 1408 1409 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg), FrameIndex, 1410 IsLittleEndian ? 8 : 0); 1411 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg + 1), FrameIndex, 1412 IsLittleEndian ? 0 : 8); 1413 1414 // Discard the pseudo instruction. 1415 MBB.erase(II); 1416 } 1417 1418 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 1419 Register Reg, int &FrameIdx) const { 1420 // For the nonvolatile condition registers (CR2, CR3, CR4) return true to 1421 // prevent allocating an additional frame slot. 1422 // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8, 1423 // for 32-bit AIX the CR save area is in the linkage area at SP+4. 1424 // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos 1425 // valid. 1426 // For 32-bit ELF, we have previously created the stack slot if needed, so 1427 // return its FrameIdx. 1428 if (PPC::CR2 <= Reg && Reg <= PPC::CR4) { 1429 FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex(); 1430 return true; 1431 } 1432 return false; 1433 } 1434 1435 // If the offset must be a multiple of some value, return what that value is. 1436 static unsigned offsetMinAlignForOpcode(unsigned OpC) { 1437 switch (OpC) { 1438 default: 1439 return 1; 1440 case PPC::LWA: 1441 case PPC::LWA_32: 1442 case PPC::LD: 1443 case PPC::LDU: 1444 case PPC::STD: 1445 case PPC::STDU: 1446 case PPC::DFLOADf32: 1447 case PPC::DFLOADf64: 1448 case PPC::DFSTOREf32: 1449 case PPC::DFSTOREf64: 1450 case PPC::LXSD: 1451 case PPC::LXSSP: 1452 case PPC::STXSD: 1453 case PPC::STXSSP: 1454 case PPC::STQ: 1455 return 4; 1456 case PPC::EVLDD: 1457 case PPC::EVSTDD: 1458 return 8; 1459 case PPC::LXV: 1460 case PPC::STXV: 1461 case PPC::LQ: 1462 case PPC::LXVP: 1463 case PPC::STXVP: 1464 return 16; 1465 } 1466 } 1467 1468 // If the offset must be a multiple of some value, return what that value is. 1469 static unsigned offsetMinAlign(const MachineInstr &MI) { 1470 unsigned OpC = MI.getOpcode(); 1471 return offsetMinAlignForOpcode(OpC); 1472 } 1473 1474 // Return the OffsetOperandNo given the FIOperandNum (and the instruction). 1475 static unsigned getOffsetONFromFION(const MachineInstr &MI, 1476 unsigned FIOperandNum) { 1477 // Take into account whether it's an add or mem instruction 1478 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2; 1479 if (MI.isInlineAsm()) 1480 OffsetOperandNo = FIOperandNum - 1; 1481 else if (MI.getOpcode() == TargetOpcode::STACKMAP || 1482 MI.getOpcode() == TargetOpcode::PATCHPOINT) 1483 OffsetOperandNo = FIOperandNum + 1; 1484 1485 return OffsetOperandNo; 1486 } 1487 1488 void 1489 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 1490 int SPAdj, unsigned FIOperandNum, 1491 RegScavenger *RS) const { 1492 assert(SPAdj == 0 && "Unexpected"); 1493 1494 // Get the instruction. 1495 MachineInstr &MI = *II; 1496 // Get the instruction's basic block. 1497 MachineBasicBlock &MBB = *MI.getParent(); 1498 // Get the basic block's function. 1499 MachineFunction &MF = *MBB.getParent(); 1500 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1501 // Get the instruction info. 1502 const PPCInstrInfo &TII = *Subtarget.getInstrInfo(); 1503 // Get the frame info. 1504 MachineFrameInfo &MFI = MF.getFrameInfo(); 1505 DebugLoc dl = MI.getDebugLoc(); 1506 1507 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 1508 1509 // Get the frame index. 1510 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 1511 1512 // Get the frame pointer save index. Users of this index are primarily 1513 // DYNALLOC instructions. 1514 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 1515 int FPSI = FI->getFramePointerSaveIndex(); 1516 // Get the instruction opcode. 1517 unsigned OpC = MI.getOpcode(); 1518 1519 if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) { 1520 lowerDynamicAreaOffset(II); 1521 return; 1522 } 1523 1524 // Special case for dynamic alloca. 1525 if (FPSI && FrameIndex == FPSI && 1526 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) { 1527 lowerDynamicAlloc(II); 1528 return; 1529 } 1530 1531 if (FPSI && FrameIndex == FPSI && 1532 (OpC == PPC::PREPARE_PROBED_ALLOCA_64 || 1533 OpC == PPC::PREPARE_PROBED_ALLOCA_32 || 1534 OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64 || 1535 OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32)) { 1536 lowerPrepareProbedAlloca(II); 1537 return; 1538 } 1539 1540 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc. 1541 if (OpC == PPC::SPILL_CR) { 1542 lowerCRSpilling(II, FrameIndex); 1543 return; 1544 } else if (OpC == PPC::RESTORE_CR) { 1545 lowerCRRestore(II, FrameIndex); 1546 return; 1547 } else if (OpC == PPC::SPILL_CRBIT) { 1548 lowerCRBitSpilling(II, FrameIndex); 1549 return; 1550 } else if (OpC == PPC::RESTORE_CRBIT) { 1551 lowerCRBitRestore(II, FrameIndex); 1552 return; 1553 } else if (OpC == PPC::SPILL_ACC || OpC == PPC::SPILL_UACC) { 1554 lowerACCSpilling(II, FrameIndex); 1555 return; 1556 } else if (OpC == PPC::RESTORE_ACC || OpC == PPC::RESTORE_UACC) { 1557 lowerACCRestore(II, FrameIndex); 1558 return; 1559 } else if (OpC == PPC::STXVP && DisableAutoPairedVecSt) { 1560 lowerOctWordSpilling(II, FrameIndex); 1561 return; 1562 } else if (OpC == PPC::SPILL_QUADWORD) { 1563 lowerQuadwordSpilling(II, FrameIndex); 1564 return; 1565 } else if (OpC == PPC::RESTORE_QUADWORD) { 1566 lowerQuadwordRestore(II, FrameIndex); 1567 return; 1568 } 1569 1570 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). 1571 MI.getOperand(FIOperandNum).ChangeToRegister( 1572 FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false); 1573 1574 // If the instruction is not present in ImmToIdxMap, then it has no immediate 1575 // form (and must be r+r). 1576 bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP && 1577 OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC); 1578 1579 // Now add the frame object offset to the offset from r1. 1580 int64_t Offset = MFI.getObjectOffset(FrameIndex); 1581 Offset += MI.getOperand(OffsetOperandNo).getImm(); 1582 1583 // If we're not using a Frame Pointer that has been set to the value of the 1584 // SP before having the stack size subtracted from it, then add the stack size 1585 // to Offset to get the correct offset. 1586 // Naked functions have stack size 0, although getStackSize may not reflect 1587 // that because we didn't call all the pieces that compute it for naked 1588 // functions. 1589 if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) { 1590 if (!(hasBasePointer(MF) && FrameIndex < 0)) 1591 Offset += MFI.getStackSize(); 1592 } 1593 1594 // If we encounter an LXVP/STXVP with an offset that doesn't fit, we can 1595 // transform it to the prefixed version so we don't have to use the XForm. 1596 if ((OpC == PPC::LXVP || OpC == PPC::STXVP) && 1597 (!isInt<16>(Offset) || (Offset % offsetMinAlign(MI)) != 0) && 1598 Subtarget.hasPrefixInstrs()) { 1599 unsigned NewOpc = OpC == PPC::LXVP ? PPC::PLXVP : PPC::PSTXVP; 1600 MI.setDesc(TII.get(NewOpc)); 1601 OpC = NewOpc; 1602 } 1603 1604 // If we can, encode the offset directly into the instruction. If this is a 1605 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If 1606 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits 1607 // clear can be encoded. This is extremely uncommon, because normally you 1608 // only "std" to a stack slot that is at least 4-byte aligned, but it can 1609 // happen in invalid code. 1610 assert(OpC != PPC::DBG_VALUE && 1611 "This should be handled in a target-independent way"); 1612 // FIXME: This should be factored out to a separate function as prefixed 1613 // instructions add a number of opcodes for which we can use 34-bit imm. 1614 bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ? 1615 isUInt<8>(Offset) : 1616 isInt<16>(Offset); 1617 if (TII.isPrefixed(MI.getOpcode())) 1618 OffsetFitsMnemonic = isInt<34>(Offset); 1619 if (!noImmForm && ((OffsetFitsMnemonic && 1620 ((Offset % offsetMinAlign(MI)) == 0)) || 1621 OpC == TargetOpcode::STACKMAP || 1622 OpC == TargetOpcode::PATCHPOINT)) { 1623 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 1624 return; 1625 } 1626 1627 // The offset doesn't fit into a single register, scavenge one to build the 1628 // offset in. 1629 1630 bool is64Bit = TM.isPPC64(); 1631 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1632 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1633 const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC; 1634 Register SRegHi = MF.getRegInfo().createVirtualRegister(RC), 1635 SReg = MF.getRegInfo().createVirtualRegister(RC); 1636 unsigned NewOpcode = 0u; 1637 1638 // Insert a set of rA with the full offset value before the ld, st, or add 1639 if (isInt<16>(Offset)) 1640 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg) 1641 .addImm(Offset); 1642 else if (isInt<32>(Offset)) { 1643 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi) 1644 .addImm(Offset >> 16); 1645 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg) 1646 .addReg(SRegHi, RegState::Kill) 1647 .addImm(Offset); 1648 } else { 1649 assert(is64Bit && "Huge stack is only supported on PPC64"); 1650 TII.materializeImmPostRA(MBB, II, dl, SReg, Offset); 1651 } 1652 1653 // Convert into indexed form of the instruction: 1654 // 1655 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 1656 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 1657 unsigned OperandBase; 1658 1659 if (noImmForm) 1660 OperandBase = 1; 1661 else if (OpC != TargetOpcode::INLINEASM && 1662 OpC != TargetOpcode::INLINEASM_BR) { 1663 assert(ImmToIdxMap.count(OpC) && 1664 "No indexed form of load or store available!"); 1665 NewOpcode = ImmToIdxMap.find(OpC)->second; 1666 MI.setDesc(TII.get(NewOpcode)); 1667 OperandBase = 1; 1668 } else { 1669 OperandBase = OffsetOperandNo; 1670 } 1671 1672 Register StackReg = MI.getOperand(FIOperandNum).getReg(); 1673 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false); 1674 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true); 1675 1676 // Since these are not real X-Form instructions, we must 1677 // add the registers and access 0(NewReg) rather than 1678 // emitting the X-Form pseudo. 1679 if (NewOpcode == PPC::LQX_PSEUDO || NewOpcode == PPC::STQX_PSEUDO) { 1680 assert(is64Bit && "Quadword loads/stores only supported in 64-bit mode"); 1681 Register NewReg = MF.getRegInfo().createVirtualRegister(&PPC::G8RCRegClass); 1682 BuildMI(MBB, II, dl, TII.get(PPC::ADD8), NewReg) 1683 .addReg(SReg, RegState::Kill) 1684 .addReg(StackReg); 1685 MI.setDesc(TII.get(NewOpcode == PPC::LQX_PSEUDO ? PPC::LQ : PPC::STQ)); 1686 MI.getOperand(OperandBase + 1).ChangeToRegister(NewReg, false); 1687 MI.getOperand(OperandBase).ChangeToImmediate(0); 1688 } 1689 } 1690 1691 Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 1692 const PPCFrameLowering *TFI = getFrameLowering(MF); 1693 1694 if (!TM.isPPC64()) 1695 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1; 1696 else 1697 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1; 1698 } 1699 1700 Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const { 1701 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1702 if (!hasBasePointer(MF)) 1703 return getFrameRegister(MF); 1704 1705 if (TM.isPPC64()) 1706 return PPC::X30; 1707 1708 if (Subtarget.isSVR4ABI() && TM.isPositionIndependent()) 1709 return PPC::R29; 1710 1711 return PPC::R30; 1712 } 1713 1714 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const { 1715 if (!EnableBasePointer) 1716 return false; 1717 if (AlwaysBasePointer) 1718 return true; 1719 1720 // If we need to realign the stack, then the stack pointer can no longer 1721 // serve as an offset into the caller's stack space. As a result, we need a 1722 // base pointer. 1723 return hasStackRealignment(MF); 1724 } 1725 1726 /// Returns true if the instruction's frame index 1727 /// reference would be better served by a base register other than FP 1728 /// or SP. Used by LocalStackFrameAllocation to determine which frame index 1729 /// references it should create new base registers for. 1730 bool PPCRegisterInfo:: 1731 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 1732 assert(Offset < 0 && "Local offset must be negative"); 1733 1734 // It's the load/store FI references that cause issues, as it can be difficult 1735 // to materialize the offset if it won't fit in the literal field. Estimate 1736 // based on the size of the local frame and some conservative assumptions 1737 // about the rest of the stack frame (note, this is pre-regalloc, so 1738 // we don't know everything for certain yet) whether this offset is likely 1739 // to be out of range of the immediate. Return true if so. 1740 1741 // We only generate virtual base registers for loads and stores that have 1742 // an r+i form. Return false for everything else. 1743 unsigned OpC = MI->getOpcode(); 1744 if (!ImmToIdxMap.count(OpC)) 1745 return false; 1746 1747 // Don't generate a new virtual base register just to add zero to it. 1748 if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) && 1749 MI->getOperand(2).getImm() == 0) 1750 return false; 1751 1752 MachineBasicBlock &MBB = *MI->getParent(); 1753 MachineFunction &MF = *MBB.getParent(); 1754 const PPCFrameLowering *TFI = getFrameLowering(MF); 1755 unsigned StackEst = TFI->determineFrameLayout(MF, true); 1756 1757 // If we likely don't need a stack frame, then we probably don't need a 1758 // virtual base register either. 1759 if (!StackEst) 1760 return false; 1761 1762 // Estimate an offset from the stack pointer. 1763 // The incoming offset is relating to the SP at the start of the function, 1764 // but when we access the local it'll be relative to the SP after local 1765 // allocation, so adjust our SP-relative offset by that allocation size. 1766 Offset += StackEst; 1767 1768 // The frame pointer will point to the end of the stack, so estimate the 1769 // offset as the difference between the object offset and the FP location. 1770 return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset); 1771 } 1772 1773 /// Insert defining instruction(s) for BaseReg to 1774 /// be a pointer to FrameIdx at the beginning of the basic block. 1775 Register PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 1776 int FrameIdx, 1777 int64_t Offset) const { 1778 unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI; 1779 1780 MachineBasicBlock::iterator Ins = MBB->begin(); 1781 DebugLoc DL; // Defaults to "unknown" 1782 if (Ins != MBB->end()) 1783 DL = Ins->getDebugLoc(); 1784 1785 const MachineFunction &MF = *MBB->getParent(); 1786 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1787 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1788 const MCInstrDesc &MCID = TII.get(ADDriOpc); 1789 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 1790 const TargetRegisterClass *RC = getPointerRegClass(MF); 1791 Register BaseReg = MRI.createVirtualRegister(RC); 1792 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); 1793 1794 BuildMI(*MBB, Ins, DL, MCID, BaseReg) 1795 .addFrameIndex(FrameIdx).addImm(Offset); 1796 1797 return BaseReg; 1798 } 1799 1800 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 1801 int64_t Offset) const { 1802 unsigned FIOperandNum = 0; 1803 while (!MI.getOperand(FIOperandNum).isFI()) { 1804 ++FIOperandNum; 1805 assert(FIOperandNum < MI.getNumOperands() && 1806 "Instr doesn't have FrameIndex operand!"); 1807 } 1808 1809 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false); 1810 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 1811 Offset += MI.getOperand(OffsetOperandNo).getImm(); 1812 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 1813 1814 MachineBasicBlock &MBB = *MI.getParent(); 1815 MachineFunction &MF = *MBB.getParent(); 1816 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1817 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1818 const MCInstrDesc &MCID = MI.getDesc(); 1819 MachineRegisterInfo &MRI = MF.getRegInfo(); 1820 MRI.constrainRegClass(BaseReg, 1821 TII.getRegClass(MCID, FIOperandNum, this, MF)); 1822 } 1823 1824 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 1825 Register BaseReg, 1826 int64_t Offset) const { 1827 unsigned FIOperandNum = 0; 1828 while (!MI->getOperand(FIOperandNum).isFI()) { 1829 ++FIOperandNum; 1830 assert(FIOperandNum < MI->getNumOperands() && 1831 "Instr doesn't have FrameIndex operand!"); 1832 } 1833 1834 unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum); 1835 Offset += MI->getOperand(OffsetOperandNo).getImm(); 1836 1837 return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm 1838 MI->getOpcode() == TargetOpcode::STACKMAP || 1839 MI->getOpcode() == TargetOpcode::PATCHPOINT || 1840 (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0); 1841 } 1842