1 //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the PowerPC implementation of the TargetRegisterInfo 11 // class. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "PPCRegisterInfo.h" 16 #include "PPC.h" 17 #include "PPCFrameLowering.h" 18 #include "PPCInstrBuilder.h" 19 #include "PPCMachineFunctionInfo.h" 20 #include "PPCSubtarget.h" 21 #include "PPCTargetMachine.h" 22 #include "llvm/ADT/BitVector.h" 23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/Statistic.h" 25 #include "llvm/CodeGen/MachineFrameInfo.h" 26 #include "llvm/CodeGen/MachineFunction.h" 27 #include "llvm/CodeGen/MachineInstrBuilder.h" 28 #include "llvm/CodeGen/MachineModuleInfo.h" 29 #include "llvm/CodeGen/MachineRegisterInfo.h" 30 #include "llvm/CodeGen/RegisterScavenging.h" 31 #include "llvm/CodeGen/TargetFrameLowering.h" 32 #include "llvm/CodeGen/TargetInstrInfo.h" 33 #include "llvm/IR/CallingConv.h" 34 #include "llvm/IR/Constants.h" 35 #include "llvm/IR/Function.h" 36 #include "llvm/IR/Type.h" 37 #include "llvm/Support/CommandLine.h" 38 #include "llvm/Support/Debug.h" 39 #include "llvm/Support/ErrorHandling.h" 40 #include "llvm/Support/MathExtras.h" 41 #include "llvm/Support/raw_ostream.h" 42 #include "llvm/Target/TargetMachine.h" 43 #include "llvm/Target/TargetOptions.h" 44 #include <cstdlib> 45 46 using namespace llvm; 47 48 #define DEBUG_TYPE "reginfo" 49 50 #define GET_REGINFO_TARGET_DESC 51 #include "PPCGenRegisterInfo.inc" 52 53 STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass"); 54 STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass"); 55 56 static cl::opt<bool> 57 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true), 58 cl::desc("Enable use of a base pointer for complex stack frames")); 59 60 static cl::opt<bool> 61 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false), 62 cl::desc("Force the use of a base pointer in every function")); 63 64 static cl::opt<bool> 65 EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false), 66 cl::desc("Enable spills from gpr to vsr rather than stack")); 67 68 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM) 69 : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR, 70 TM.isPPC64() ? 0 : 1, 71 TM.isPPC64() ? 0 : 1), 72 TM(TM) { 73 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX; 74 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX; 75 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX; 76 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX; 77 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX; 78 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX; 79 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX; 80 ImmToIdxMap[PPC::ADDI] = PPC::ADD4; 81 ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32; 82 83 // 64-bit 84 ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8; 85 ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8; 86 ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8; 87 ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX; 88 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; 89 90 // VSX 91 ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX; 92 ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX; 93 ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX; 94 ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX; 95 ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX; 96 ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX; 97 ImmToIdxMap[PPC::LXV] = PPC::LXVX; 98 ImmToIdxMap[PPC::LXSD] = PPC::LXSDX; 99 ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX; 100 ImmToIdxMap[PPC::STXV] = PPC::STXVX; 101 ImmToIdxMap[PPC::STXSD] = PPC::STXSDX; 102 ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX; 103 } 104 105 /// getPointerRegClass - Return the register class to use to hold pointers. 106 /// This is used for addressing modes. 107 const TargetRegisterClass * 108 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) 109 const { 110 // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value 111 // when it checks for ZERO folding. 112 if (Kind == 1) { 113 if (TM.isPPC64()) 114 return &PPC::G8RC_NOX0RegClass; 115 return &PPC::GPRC_NOR0RegClass; 116 } 117 118 if (TM.isPPC64()) 119 return &PPC::G8RCRegClass; 120 return &PPC::GPRCRegClass; 121 } 122 123 const MCPhysReg* 124 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 125 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>(); 126 if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) { 127 if (Subtarget.hasVSX()) 128 return CSR_64_AllRegs_VSX_SaveList; 129 if (Subtarget.hasAltivec()) 130 return CSR_64_AllRegs_Altivec_SaveList; 131 return CSR_64_AllRegs_SaveList; 132 } 133 134 if (Subtarget.isDarwinABI()) 135 return TM.isPPC64() 136 ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_SaveList 137 : CSR_Darwin64_SaveList) 138 : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_SaveList 139 : CSR_Darwin32_SaveList); 140 141 if (TM.isPPC64() && MF->getInfo<PPCFunctionInfo>()->isSplitCSR()) 142 return CSR_SRV464_TLS_PE_SaveList; 143 144 // On PPC64, we might need to save r2 (but only if it is not reserved). 145 bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2); 146 147 if (MF->getFunction().getCallingConv() == CallingConv::Cold) { 148 return TM.isPPC64() 149 ? (Subtarget.hasAltivec() 150 ? (SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList 151 : CSR_SVR64_ColdCC_Altivec_SaveList) 152 : (SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList 153 : CSR_SVR64_ColdCC_SaveList)) 154 : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_SaveList 155 : CSR_SVR32_ColdCC_SaveList); 156 } 157 158 return TM.isPPC64() 159 ? (Subtarget.hasAltivec() 160 ? (SaveR2 ? CSR_SVR464_R2_Altivec_SaveList 161 : CSR_SVR464_Altivec_SaveList) 162 : (SaveR2 ? CSR_SVR464_R2_SaveList : CSR_SVR464_SaveList)) 163 : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_SaveList 164 : CSR_SVR432_SaveList); 165 } 166 167 const MCPhysReg * 168 PPCRegisterInfo::getCalleeSavedRegsViaCopy(const MachineFunction *MF) const { 169 assert(MF && "Invalid MachineFunction pointer."); 170 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>(); 171 if (Subtarget.isDarwinABI()) 172 return nullptr; 173 if (!TM.isPPC64()) 174 return nullptr; 175 if (MF->getFunction().getCallingConv() != CallingConv::CXX_FAST_TLS) 176 return nullptr; 177 if (!MF->getInfo<PPCFunctionInfo>()->isSplitCSR()) 178 return nullptr; 179 180 // On PPC64, we might need to save r2 (but only if it is not reserved). 181 bool SaveR2 = !getReservedRegs(*MF).test(PPC::X2); 182 if (Subtarget.hasAltivec()) 183 return SaveR2 184 ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList 185 : CSR_SVR464_Altivec_ViaCopy_SaveList; 186 else 187 return SaveR2 188 ? CSR_SVR464_R2_ViaCopy_SaveList 189 : CSR_SVR464_ViaCopy_SaveList; 190 } 191 192 const uint32_t * 193 PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF, 194 CallingConv::ID CC) const { 195 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 196 if (CC == CallingConv::AnyReg) { 197 if (Subtarget.hasVSX()) 198 return CSR_64_AllRegs_VSX_RegMask; 199 if (Subtarget.hasAltivec()) 200 return CSR_64_AllRegs_Altivec_RegMask; 201 return CSR_64_AllRegs_RegMask; 202 } 203 204 if (Subtarget.isDarwinABI()) 205 return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_RegMask 206 : CSR_Darwin64_RegMask) 207 : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_RegMask 208 : CSR_Darwin32_RegMask); 209 210 if (CC == CallingConv::Cold) { 211 return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask 212 : CSR_SVR64_ColdCC_RegMask) 213 : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask 214 : CSR_SVR32_ColdCC_RegMask); 215 } 216 217 return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR464_Altivec_RegMask 218 : CSR_SVR464_RegMask) 219 : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_RegMask 220 : CSR_SVR432_RegMask); 221 } 222 223 const uint32_t* 224 PPCRegisterInfo::getNoPreservedMask() const { 225 return CSR_NoRegs_RegMask; 226 } 227 228 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const { 229 for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM}) 230 Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32)); 231 } 232 233 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 234 BitVector Reserved(getNumRegs()); 235 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 236 const PPCFrameLowering *TFI = getFrameLowering(MF); 237 238 // The ZERO register is not really a register, but the representation of r0 239 // when used in instructions that treat r0 as the constant 0. 240 markSuperRegs(Reserved, PPC::ZERO); 241 242 // The FP register is also not really a register, but is the representation 243 // of the frame pointer register used by ISD::FRAMEADDR. 244 markSuperRegs(Reserved, PPC::FP); 245 246 // The BP register is also not really a register, but is the representation 247 // of the base pointer register used by setjmp. 248 markSuperRegs(Reserved, PPC::BP); 249 250 // The counter registers must be reserved so that counter-based loops can 251 // be correctly formed (and the mtctr instructions are not DCE'd). 252 markSuperRegs(Reserved, PPC::CTR); 253 markSuperRegs(Reserved, PPC::CTR8); 254 255 markSuperRegs(Reserved, PPC::R1); 256 markSuperRegs(Reserved, PPC::LR); 257 markSuperRegs(Reserved, PPC::LR8); 258 markSuperRegs(Reserved, PPC::RM); 259 260 if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec()) 261 markSuperRegs(Reserved, PPC::VRSAVE); 262 263 // The SVR4 ABI reserves r2 and r13 264 if (Subtarget.isSVR4ABI()) { 265 // We only reserve r2 if we need to use the TOC pointer. If we have no 266 // explicit uses of the TOC pointer (meaning we're a leaf function with 267 // no constant-pool loads, etc.) and we have no potential uses inside an 268 // inline asm block, then we can treat r2 has an ordinary callee-saved 269 // register. 270 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 271 if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm()) 272 markSuperRegs(Reserved, PPC::R2); // System-reserved register 273 markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register 274 } 275 276 // On PPC64, r13 is the thread pointer. Never allocate this register. 277 if (TM.isPPC64()) 278 markSuperRegs(Reserved, PPC::R13); 279 280 if (TFI->needsFP(MF)) 281 markSuperRegs(Reserved, PPC::R31); 282 283 bool IsPositionIndependent = TM.isPositionIndependent(); 284 if (hasBasePointer(MF)) { 285 if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent) 286 markSuperRegs(Reserved, PPC::R29); 287 else 288 markSuperRegs(Reserved, PPC::R30); 289 } 290 291 if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent) 292 markSuperRegs(Reserved, PPC::R30); 293 294 // Reserve Altivec registers when Altivec is unavailable. 295 if (!Subtarget.hasAltivec()) 296 for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(), 297 IE = PPC::VRRCRegClass.end(); I != IE; ++I) 298 markSuperRegs(Reserved, *I); 299 300 assert(checkAllSuperRegsMarked(Reserved)); 301 return Reserved; 302 } 303 304 bool PPCRegisterInfo::isCallerPreservedPhysReg(unsigned PhysReg, 305 const MachineFunction &MF) const { 306 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg)); 307 if (TM.isELFv2ABI() && PhysReg == PPC::X2) { 308 // X2 is guaranteed to be preserved within a function if it is reserved. 309 // The reason it's reserved is that it's the TOC pointer (and the function 310 // uses the TOC). In functions where it isn't reserved (i.e. leaf functions 311 // with no TOC access), we can't claim that it is preserved. 312 return (getReservedRegs(MF).test(PPC::X2)); 313 } else { 314 return false; 315 } 316 } 317 318 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 319 MachineFunction &MF) const { 320 const PPCFrameLowering *TFI = getFrameLowering(MF); 321 const unsigned DefaultSafety = 1; 322 323 switch (RC->getID()) { 324 default: 325 return 0; 326 case PPC::G8RC_NOX0RegClassID: 327 case PPC::GPRC_NOR0RegClassID: 328 case PPC::G8RCRegClassID: 329 case PPC::GPRCRegClassID: { 330 unsigned FP = TFI->hasFP(MF) ? 1 : 0; 331 return 32 - FP - DefaultSafety; 332 } 333 case PPC::F8RCRegClassID: 334 case PPC::F4RCRegClassID: 335 case PPC::QFRCRegClassID: 336 case PPC::QSRCRegClassID: 337 case PPC::QBRCRegClassID: 338 case PPC::VRRCRegClassID: 339 case PPC::VFRCRegClassID: 340 case PPC::VSLRCRegClassID: 341 return 32 - DefaultSafety; 342 case PPC::VSRCRegClassID: 343 case PPC::VSFRCRegClassID: 344 case PPC::VSSRCRegClassID: 345 return 64 - DefaultSafety; 346 case PPC::CRRCRegClassID: 347 return 8 - DefaultSafety; 348 } 349 } 350 351 const TargetRegisterClass * 352 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 353 const MachineFunction &MF) const { 354 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 355 if (Subtarget.hasVSX()) { 356 // With VSX, we can inflate various sub-register classes to the full VSX 357 // register set. 358 359 // For Power9 we allow the user to enable GPR to vector spills. 360 // FIXME: Currently limited to spilling GP8RC. A follow on patch will add 361 // support to spill GPRC. 362 if (TM.isELFv2ABI()) { 363 if (Subtarget.hasP9Vector() && EnableGPRToVecSpills && 364 RC == &PPC::G8RCRegClass) { 365 InflateGP8RC++; 366 return &PPC::SPILLTOVSRRCRegClass; 367 } 368 if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills) 369 InflateGPRC++; 370 } 371 if (RC == &PPC::F8RCRegClass) 372 return &PPC::VSFRCRegClass; 373 else if (RC == &PPC::VRRCRegClass) 374 return &PPC::VSRCRegClass; 375 else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector()) 376 return &PPC::VSSRCRegClass; 377 } 378 379 return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF); 380 } 381 382 //===----------------------------------------------------------------------===// 383 // Stack Frame Processing methods 384 //===----------------------------------------------------------------------===// 385 386 /// lowerDynamicAlloc - Generate the code for allocating an object in the 387 /// current frame. The sequence of code will be in the general form 388 /// 389 /// addi R0, SP, \#frameSize ; get the address of the previous frame 390 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size 391 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation 392 /// 393 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const { 394 // Get the instruction. 395 MachineInstr &MI = *II; 396 // Get the instruction's basic block. 397 MachineBasicBlock &MBB = *MI.getParent(); 398 // Get the basic block's function. 399 MachineFunction &MF = *MBB.getParent(); 400 // Get the frame info. 401 MachineFrameInfo &MFI = MF.getFrameInfo(); 402 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 403 // Get the instruction info. 404 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 405 // Determine whether 64-bit pointers are used. 406 bool LP64 = TM.isPPC64(); 407 DebugLoc dl = MI.getDebugLoc(); 408 409 // Get the maximum call stack size. 410 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 411 // Get the total frame size. 412 unsigned FrameSize = MFI.getStackSize(); 413 414 // Get stack alignments. 415 const PPCFrameLowering *TFI = getFrameLowering(MF); 416 unsigned TargetAlign = TFI->getStackAlignment(); 417 unsigned MaxAlign = MFI.getMaxAlignment(); 418 assert((maxCallFrameSize & (MaxAlign-1)) == 0 && 419 "Maximum call-frame size not sufficiently aligned"); 420 421 // Determine the previous frame's address. If FrameSize can't be 422 // represented as 16 bits or we need special alignment, then we load the 423 // previous frame's address from 0(SP). Why not do an addis of the hi? 424 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 425 // Constructing the constant and adding would take 3 instructions. 426 // Fortunately, a frame greater than 32K is rare. 427 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 428 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 429 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 430 431 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 432 if (LP64) 433 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), Reg) 434 .addReg(PPC::X31) 435 .addImm(FrameSize); 436 else 437 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg) 438 .addReg(PPC::R31) 439 .addImm(FrameSize); 440 } else if (LP64) { 441 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg) 442 .addImm(0) 443 .addReg(PPC::X1); 444 } else { 445 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg) 446 .addImm(0) 447 .addReg(PPC::R1); 448 } 449 450 bool KillNegSizeReg = MI.getOperand(1).isKill(); 451 unsigned NegSizeReg = MI.getOperand(1).getReg(); 452 453 // Grow the stack and update the stack pointer link, then determine the 454 // address of new allocated space. 455 if (LP64) { 456 if (MaxAlign > TargetAlign) { 457 unsigned UnalNegSizeReg = NegSizeReg; 458 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 459 460 // Unfortunately, there is no andi, only andi., and we can't insert that 461 // here because we might clobber cr0 while it is live. 462 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg) 463 .addImm(~(MaxAlign-1)); 464 465 unsigned NegSizeReg1 = NegSizeReg; 466 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 467 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg) 468 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 469 .addReg(NegSizeReg1, RegState::Kill); 470 KillNegSizeReg = true; 471 } 472 473 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1) 474 .addReg(Reg, RegState::Kill) 475 .addReg(PPC::X1) 476 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 477 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 478 .addReg(PPC::X1) 479 .addImm(maxCallFrameSize); 480 } else { 481 if (MaxAlign > TargetAlign) { 482 unsigned UnalNegSizeReg = NegSizeReg; 483 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 484 485 // Unfortunately, there is no andi, only andi., and we can't insert that 486 // here because we might clobber cr0 while it is live. 487 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg) 488 .addImm(~(MaxAlign-1)); 489 490 unsigned NegSizeReg1 = NegSizeReg; 491 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 492 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg) 493 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 494 .addReg(NegSizeReg1, RegState::Kill); 495 KillNegSizeReg = true; 496 } 497 498 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1) 499 .addReg(Reg, RegState::Kill) 500 .addReg(PPC::R1) 501 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 502 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 503 .addReg(PPC::R1) 504 .addImm(maxCallFrameSize); 505 } 506 507 // Discard the DYNALLOC instruction. 508 MBB.erase(II); 509 } 510 511 void PPCRegisterInfo::lowerDynamicAreaOffset( 512 MachineBasicBlock::iterator II) const { 513 // Get the instruction. 514 MachineInstr &MI = *II; 515 // Get the instruction's basic block. 516 MachineBasicBlock &MBB = *MI.getParent(); 517 // Get the basic block's function. 518 MachineFunction &MF = *MBB.getParent(); 519 // Get the frame info. 520 MachineFrameInfo &MFI = MF.getFrameInfo(); 521 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 522 // Get the instruction info. 523 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 524 525 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 526 bool is64Bit = TM.isPPC64(); 527 DebugLoc dl = MI.getDebugLoc(); 528 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), 529 MI.getOperand(0).getReg()) 530 .addImm(maxCallFrameSize); 531 MBB.erase(II); 532 } 533 534 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 535 /// reserving a whole register (R0), we scrounge for one here. This generates 536 /// code like this: 537 /// 538 /// mfcr rA ; Move the conditional register into GPR rA. 539 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 540 /// stw rA, FI ; Store rA to the frame. 541 /// 542 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 543 unsigned FrameIndex) const { 544 // Get the instruction. 545 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 546 // Get the instruction's basic block. 547 MachineBasicBlock &MBB = *MI.getParent(); 548 MachineFunction &MF = *MBB.getParent(); 549 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 550 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 551 DebugLoc dl = MI.getDebugLoc(); 552 553 bool LP64 = TM.isPPC64(); 554 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 555 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 556 557 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 558 unsigned SrcReg = MI.getOperand(0).getReg(); 559 560 // We need to store the CR in the low 4-bits of the saved value. First, issue 561 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg. 562 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 563 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 564 565 // If the saved register wasn't CR0, shift the bits left so that they are in 566 // CR0's slot. 567 if (SrcReg != PPC::CR0) { 568 unsigned Reg1 = Reg; 569 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 570 571 // rlwinm rA, rA, ShiftBits, 0, 31. 572 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 573 .addReg(Reg1, RegState::Kill) 574 .addImm(getEncodingValue(SrcReg) * 4) 575 .addImm(0) 576 .addImm(31); 577 } 578 579 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 580 .addReg(Reg, RegState::Kill), 581 FrameIndex); 582 583 // Discard the pseudo instruction. 584 MBB.erase(II); 585 } 586 587 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 588 unsigned FrameIndex) const { 589 // Get the instruction. 590 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 591 // Get the instruction's basic block. 592 MachineBasicBlock &MBB = *MI.getParent(); 593 MachineFunction &MF = *MBB.getParent(); 594 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 595 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 596 DebugLoc dl = MI.getDebugLoc(); 597 598 bool LP64 = TM.isPPC64(); 599 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 600 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 601 602 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 603 unsigned DestReg = MI.getOperand(0).getReg(); 604 assert(MI.definesRegister(DestReg) && 605 "RESTORE_CR does not define its destination"); 606 607 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 608 Reg), FrameIndex); 609 610 // If the reloaded register isn't CR0, shift the bits right so that they are 611 // in the right CR's slot. 612 if (DestReg != PPC::CR0) { 613 unsigned Reg1 = Reg; 614 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 615 616 unsigned ShiftBits = getEncodingValue(DestReg)*4; 617 // rlwinm r11, r11, 32-ShiftBits, 0, 31. 618 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 619 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0) 620 .addImm(31); 621 } 622 623 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg) 624 .addReg(Reg, RegState::Kill); 625 626 // Discard the pseudo instruction. 627 MBB.erase(II); 628 } 629 630 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II, 631 unsigned FrameIndex) const { 632 // Get the instruction. 633 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset> 634 // Get the instruction's basic block. 635 MachineBasicBlock &MBB = *MI.getParent(); 636 MachineFunction &MF = *MBB.getParent(); 637 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 638 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 639 DebugLoc dl = MI.getDebugLoc(); 640 641 bool LP64 = TM.isPPC64(); 642 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 643 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 644 645 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 646 unsigned SrcReg = MI.getOperand(0).getReg(); 647 648 BuildMI(MBB, II, dl, TII.get(TargetOpcode::KILL), 649 getCRFromCRBit(SrcReg)) 650 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 651 652 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 653 .addReg(getCRFromCRBit(SrcReg)); 654 655 // If the saved register wasn't CR0LT, shift the bits left so that the bit to 656 // store is the first one. Mask all but that bit. 657 unsigned Reg1 = Reg; 658 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 659 660 // rlwinm rA, rA, ShiftBits, 0, 0. 661 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 662 .addReg(Reg1, RegState::Kill) 663 .addImm(getEncodingValue(SrcReg)) 664 .addImm(0).addImm(0); 665 666 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 667 .addReg(Reg, RegState::Kill), 668 FrameIndex); 669 670 // Discard the pseudo instruction. 671 MBB.erase(II); 672 } 673 674 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, 675 unsigned FrameIndex) const { 676 // Get the instruction. 677 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset> 678 // Get the instruction's basic block. 679 MachineBasicBlock &MBB = *MI.getParent(); 680 MachineFunction &MF = *MBB.getParent(); 681 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 682 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 683 DebugLoc dl = MI.getDebugLoc(); 684 685 bool LP64 = TM.isPPC64(); 686 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 687 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 688 689 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 690 unsigned DestReg = MI.getOperand(0).getReg(); 691 assert(MI.definesRegister(DestReg) && 692 "RESTORE_CRBIT does not define its destination"); 693 694 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 695 Reg), FrameIndex); 696 697 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg); 698 699 unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 700 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO) 701 .addReg(getCRFromCRBit(DestReg)); 702 703 unsigned ShiftBits = getEncodingValue(DestReg); 704 // rlwimi r11, r10, 32-ShiftBits, ..., ... 705 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO) 706 .addReg(RegO, RegState::Kill) 707 .addReg(Reg, RegState::Kill) 708 .addImm(ShiftBits ? 32 - ShiftBits : 0) 709 .addImm(ShiftBits) 710 .addImm(ShiftBits); 711 712 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), 713 getCRFromCRBit(DestReg)) 714 .addReg(RegO, RegState::Kill) 715 // Make sure we have a use dependency all the way through this 716 // sequence of instructions. We can't have the other bits in the CR 717 // modified in between the mfocrf and the mtocrf. 718 .addReg(getCRFromCRBit(DestReg), RegState::Implicit); 719 720 // Discard the pseudo instruction. 721 MBB.erase(II); 722 } 723 724 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II, 725 unsigned FrameIndex) const { 726 // Get the instruction. 727 MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset> 728 // Get the instruction's basic block. 729 MachineBasicBlock &MBB = *MI.getParent(); 730 MachineFunction &MF = *MBB.getParent(); 731 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 732 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 733 DebugLoc dl = MI.getDebugLoc(); 734 735 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 736 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC); 737 unsigned SrcReg = MI.getOperand(0).getReg(); 738 739 BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg) 740 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 741 742 addFrameReference( 743 BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill), 744 FrameIndex); 745 746 // Discard the pseudo instruction. 747 MBB.erase(II); 748 } 749 750 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II, 751 unsigned FrameIndex) const { 752 // Get the instruction. 753 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset> 754 // Get the instruction's basic block. 755 MachineBasicBlock &MBB = *MI.getParent(); 756 MachineFunction &MF = *MBB.getParent(); 757 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 758 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 759 DebugLoc dl = MI.getDebugLoc(); 760 761 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 762 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC); 763 unsigned DestReg = MI.getOperand(0).getReg(); 764 assert(MI.definesRegister(DestReg) && 765 "RESTORE_VRSAVE does not define its destination"); 766 767 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ), 768 Reg), FrameIndex); 769 770 BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg) 771 .addReg(Reg, RegState::Kill); 772 773 // Discard the pseudo instruction. 774 MBB.erase(II); 775 } 776 777 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 778 unsigned Reg, int &FrameIdx) const { 779 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 780 // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4 781 // ABI, return true to prevent allocating an additional frame slot. 782 // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0 783 // is arbitrary and will be subsequently ignored. For 32-bit, we have 784 // previously created the stack slot if needed, so return its FrameIdx. 785 if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) { 786 if (TM.isPPC64()) 787 FrameIdx = 0; 788 else { 789 const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 790 FrameIdx = FI->getCRSpillFrameIndex(); 791 } 792 return true; 793 } 794 return false; 795 } 796 797 // If the offset must be a multiple of some value, return what that value is. 798 static unsigned offsetMinAlign(const MachineInstr &MI) { 799 unsigned OpC = MI.getOpcode(); 800 801 switch (OpC) { 802 default: 803 return 1; 804 case PPC::LWA: 805 case PPC::LWA_32: 806 case PPC::LD: 807 case PPC::LDU: 808 case PPC::STD: 809 case PPC::STDU: 810 case PPC::DFLOADf32: 811 case PPC::DFLOADf64: 812 case PPC::DFSTOREf32: 813 case PPC::DFSTOREf64: 814 case PPC::LXSD: 815 case PPC::LXSSP: 816 case PPC::STXSD: 817 case PPC::STXSSP: 818 return 4; 819 case PPC::LXV: 820 case PPC::STXV: 821 return 16; 822 } 823 } 824 825 // Return the OffsetOperandNo given the FIOperandNum (and the instruction). 826 static unsigned getOffsetONFromFION(const MachineInstr &MI, 827 unsigned FIOperandNum) { 828 // Take into account whether it's an add or mem instruction 829 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2; 830 if (MI.isInlineAsm()) 831 OffsetOperandNo = FIOperandNum - 1; 832 else if (MI.getOpcode() == TargetOpcode::STACKMAP || 833 MI.getOpcode() == TargetOpcode::PATCHPOINT) 834 OffsetOperandNo = FIOperandNum + 1; 835 836 return OffsetOperandNo; 837 } 838 839 void 840 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 841 int SPAdj, unsigned FIOperandNum, 842 RegScavenger *RS) const { 843 assert(SPAdj == 0 && "Unexpected"); 844 845 // Get the instruction. 846 MachineInstr &MI = *II; 847 // Get the instruction's basic block. 848 MachineBasicBlock &MBB = *MI.getParent(); 849 // Get the basic block's function. 850 MachineFunction &MF = *MBB.getParent(); 851 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 852 // Get the instruction info. 853 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 854 // Get the frame info. 855 MachineFrameInfo &MFI = MF.getFrameInfo(); 856 DebugLoc dl = MI.getDebugLoc(); 857 858 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 859 860 // Get the frame index. 861 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 862 863 // Get the frame pointer save index. Users of this index are primarily 864 // DYNALLOC instructions. 865 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 866 int FPSI = FI->getFramePointerSaveIndex(); 867 // Get the instruction opcode. 868 unsigned OpC = MI.getOpcode(); 869 870 if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) { 871 lowerDynamicAreaOffset(II); 872 return; 873 } 874 875 // Special case for dynamic alloca. 876 if (FPSI && FrameIndex == FPSI && 877 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) { 878 lowerDynamicAlloc(II); 879 return; 880 } 881 882 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc. 883 if (OpC == PPC::SPILL_CR) { 884 lowerCRSpilling(II, FrameIndex); 885 return; 886 } else if (OpC == PPC::RESTORE_CR) { 887 lowerCRRestore(II, FrameIndex); 888 return; 889 } else if (OpC == PPC::SPILL_CRBIT) { 890 lowerCRBitSpilling(II, FrameIndex); 891 return; 892 } else if (OpC == PPC::RESTORE_CRBIT) { 893 lowerCRBitRestore(II, FrameIndex); 894 return; 895 } else if (OpC == PPC::SPILL_VRSAVE) { 896 lowerVRSAVESpilling(II, FrameIndex); 897 return; 898 } else if (OpC == PPC::RESTORE_VRSAVE) { 899 lowerVRSAVERestore(II, FrameIndex); 900 return; 901 } 902 903 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). 904 MI.getOperand(FIOperandNum).ChangeToRegister( 905 FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false); 906 907 // If the instruction is not present in ImmToIdxMap, then it has no immediate 908 // form (and must be r+r). 909 bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP && 910 OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC); 911 912 // Now add the frame object offset to the offset from r1. 913 int Offset = MFI.getObjectOffset(FrameIndex); 914 Offset += MI.getOperand(OffsetOperandNo).getImm(); 915 916 // If we're not using a Frame Pointer that has been set to the value of the 917 // SP before having the stack size subtracted from it, then add the stack size 918 // to Offset to get the correct offset. 919 // Naked functions have stack size 0, although getStackSize may not reflect 920 // that because we didn't call all the pieces that compute it for naked 921 // functions. 922 if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) { 923 if (!(hasBasePointer(MF) && FrameIndex < 0)) 924 Offset += MFI.getStackSize(); 925 } 926 927 // If we can, encode the offset directly into the instruction. If this is a 928 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If 929 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits 930 // clear can be encoded. This is extremely uncommon, because normally you 931 // only "std" to a stack slot that is at least 4-byte aligned, but it can 932 // happen in invalid code. 933 assert(OpC != PPC::DBG_VALUE && 934 "This should be handled in a target-independent way"); 935 if (!noImmForm && ((isInt<16>(Offset) && 936 ((Offset % offsetMinAlign(MI)) == 0)) || 937 OpC == TargetOpcode::STACKMAP || 938 OpC == TargetOpcode::PATCHPOINT)) { 939 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 940 return; 941 } 942 943 // The offset doesn't fit into a single register, scavenge one to build the 944 // offset in. 945 946 bool is64Bit = TM.isPPC64(); 947 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 948 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 949 const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC; 950 unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC), 951 SReg = MF.getRegInfo().createVirtualRegister(RC); 952 953 // Insert a set of rA with the full offset value before the ld, st, or add 954 if (isInt<16>(Offset)) 955 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg) 956 .addImm(Offset); 957 else { 958 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi) 959 .addImm(Offset >> 16); 960 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg) 961 .addReg(SRegHi, RegState::Kill) 962 .addImm(Offset); 963 } 964 965 // Convert into indexed form of the instruction: 966 // 967 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 968 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 969 unsigned OperandBase; 970 971 if (noImmForm) 972 OperandBase = 1; 973 else if (OpC != TargetOpcode::INLINEASM) { 974 assert(ImmToIdxMap.count(OpC) && 975 "No indexed form of load or store available!"); 976 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second; 977 MI.setDesc(TII.get(NewOpcode)); 978 OperandBase = 1; 979 } else { 980 OperandBase = OffsetOperandNo; 981 } 982 983 unsigned StackReg = MI.getOperand(FIOperandNum).getReg(); 984 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false); 985 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true); 986 } 987 988 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 989 const PPCFrameLowering *TFI = getFrameLowering(MF); 990 991 if (!TM.isPPC64()) 992 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1; 993 else 994 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1; 995 } 996 997 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const { 998 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 999 if (!hasBasePointer(MF)) 1000 return getFrameRegister(MF); 1001 1002 if (TM.isPPC64()) 1003 return PPC::X30; 1004 1005 if (Subtarget.isSVR4ABI() && TM.isPositionIndependent()) 1006 return PPC::R29; 1007 1008 return PPC::R30; 1009 } 1010 1011 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const { 1012 if (!EnableBasePointer) 1013 return false; 1014 if (AlwaysBasePointer) 1015 return true; 1016 1017 // If we need to realign the stack, then the stack pointer can no longer 1018 // serve as an offset into the caller's stack space. As a result, we need a 1019 // base pointer. 1020 return needsStackRealignment(MF); 1021 } 1022 1023 /// Returns true if the instruction's frame index 1024 /// reference would be better served by a base register other than FP 1025 /// or SP. Used by LocalStackFrameAllocation to determine which frame index 1026 /// references it should create new base registers for. 1027 bool PPCRegisterInfo:: 1028 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 1029 assert(Offset < 0 && "Local offset must be negative"); 1030 1031 // It's the load/store FI references that cause issues, as it can be difficult 1032 // to materialize the offset if it won't fit in the literal field. Estimate 1033 // based on the size of the local frame and some conservative assumptions 1034 // about the rest of the stack frame (note, this is pre-regalloc, so 1035 // we don't know everything for certain yet) whether this offset is likely 1036 // to be out of range of the immediate. Return true if so. 1037 1038 // We only generate virtual base registers for loads and stores that have 1039 // an r+i form. Return false for everything else. 1040 unsigned OpC = MI->getOpcode(); 1041 if (!ImmToIdxMap.count(OpC)) 1042 return false; 1043 1044 // Don't generate a new virtual base register just to add zero to it. 1045 if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) && 1046 MI->getOperand(2).getImm() == 0) 1047 return false; 1048 1049 MachineBasicBlock &MBB = *MI->getParent(); 1050 MachineFunction &MF = *MBB.getParent(); 1051 const PPCFrameLowering *TFI = getFrameLowering(MF); 1052 unsigned StackEst = TFI->determineFrameLayout(MF, false, true); 1053 1054 // If we likely don't need a stack frame, then we probably don't need a 1055 // virtual base register either. 1056 if (!StackEst) 1057 return false; 1058 1059 // Estimate an offset from the stack pointer. 1060 // The incoming offset is relating to the SP at the start of the function, 1061 // but when we access the local it'll be relative to the SP after local 1062 // allocation, so adjust our SP-relative offset by that allocation size. 1063 Offset += StackEst; 1064 1065 // The frame pointer will point to the end of the stack, so estimate the 1066 // offset as the difference between the object offset and the FP location. 1067 return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset); 1068 } 1069 1070 /// Insert defining instruction(s) for BaseReg to 1071 /// be a pointer to FrameIdx at the beginning of the basic block. 1072 void PPCRegisterInfo:: 1073 materializeFrameBaseRegister(MachineBasicBlock *MBB, 1074 unsigned BaseReg, int FrameIdx, 1075 int64_t Offset) const { 1076 unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI; 1077 1078 MachineBasicBlock::iterator Ins = MBB->begin(); 1079 DebugLoc DL; // Defaults to "unknown" 1080 if (Ins != MBB->end()) 1081 DL = Ins->getDebugLoc(); 1082 1083 const MachineFunction &MF = *MBB->getParent(); 1084 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1085 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1086 const MCInstrDesc &MCID = TII.get(ADDriOpc); 1087 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 1088 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); 1089 1090 BuildMI(*MBB, Ins, DL, MCID, BaseReg) 1091 .addFrameIndex(FrameIdx).addImm(Offset); 1092 } 1093 1094 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg, 1095 int64_t Offset) const { 1096 unsigned FIOperandNum = 0; 1097 while (!MI.getOperand(FIOperandNum).isFI()) { 1098 ++FIOperandNum; 1099 assert(FIOperandNum < MI.getNumOperands() && 1100 "Instr doesn't have FrameIndex operand!"); 1101 } 1102 1103 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false); 1104 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 1105 Offset += MI.getOperand(OffsetOperandNo).getImm(); 1106 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 1107 1108 MachineBasicBlock &MBB = *MI.getParent(); 1109 MachineFunction &MF = *MBB.getParent(); 1110 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1111 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1112 const MCInstrDesc &MCID = MI.getDesc(); 1113 MachineRegisterInfo &MRI = MF.getRegInfo(); 1114 MRI.constrainRegClass(BaseReg, 1115 TII.getRegClass(MCID, FIOperandNum, this, MF)); 1116 } 1117 1118 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 1119 unsigned BaseReg, 1120 int64_t Offset) const { 1121 unsigned FIOperandNum = 0; 1122 while (!MI->getOperand(FIOperandNum).isFI()) { 1123 ++FIOperandNum; 1124 assert(FIOperandNum < MI->getNumOperands() && 1125 "Instr doesn't have FrameIndex operand!"); 1126 } 1127 1128 unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum); 1129 Offset += MI->getOperand(OffsetOperandNo).getImm(); 1130 1131 return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm 1132 MI->getOpcode() == TargetOpcode::STACKMAP || 1133 MI->getOpcode() == TargetOpcode::PATCHPOINT || 1134 (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0); 1135 } 1136