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