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