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 markSuperRegs(Reserved, PPC::R2); // System-reserved register 238 markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register 239 } 240 241 if (TM.isPPC64()) { 242 // On PPC64, r13 is the thread pointer. Never allocate this register. 243 markSuperRegs(Reserved, PPC::R13); 244 245 // The 64-bit SVR4 ABI reserves r2 for the TOC pointer. 246 if (Subtarget.isSVR4ABI()) { 247 // We only reserve r2 if we need to use the TOC pointer. If we have no 248 // explicit uses of the TOC pointer (meaning we're a leaf function with 249 // no constant-pool loads, etc.) and we have no potential uses inside an 250 // inline asm block, then we can treat r2 has an ordinary callee-saved 251 // register. 252 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 253 if (!FuncInfo->usesTOCBasePtr() && !MF.hasInlineAsm()) { 254 for (MCSuperRegIterator Super(PPC::R2, this, true); Super.isValid(); 255 ++Super) 256 Reserved.reset(*Super); 257 } 258 } 259 } 260 261 if (TFI->needsFP(MF)) 262 markSuperRegs(Reserved, PPC::R31); 263 264 bool IsPositionIndependent = TM.isPositionIndependent(); 265 if (hasBasePointer(MF)) { 266 if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent) 267 markSuperRegs(Reserved, PPC::R29); 268 else 269 markSuperRegs(Reserved, PPC::R30); 270 } 271 272 if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent) 273 markSuperRegs(Reserved, PPC::R30); 274 275 // Reserve Altivec registers when Altivec is unavailable. 276 if (!Subtarget.hasAltivec()) 277 for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(), 278 IE = PPC::VRRCRegClass.end(); I != IE; ++I) 279 markSuperRegs(Reserved, *I); 280 281 assert(checkAllSuperRegsMarked(Reserved)); 282 return Reserved; 283 } 284 285 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 286 MachineFunction &MF) const { 287 const PPCFrameLowering *TFI = getFrameLowering(MF); 288 const unsigned DefaultSafety = 1; 289 290 switch (RC->getID()) { 291 default: 292 return 0; 293 case PPC::G8RC_NOX0RegClassID: 294 case PPC::GPRC_NOR0RegClassID: 295 case PPC::G8RCRegClassID: 296 case PPC::GPRCRegClassID: { 297 unsigned FP = TFI->hasFP(MF) ? 1 : 0; 298 return 32 - FP - DefaultSafety; 299 } 300 case PPC::F8RCRegClassID: 301 case PPC::F4RCRegClassID: 302 case PPC::QFRCRegClassID: 303 case PPC::QSRCRegClassID: 304 case PPC::QBRCRegClassID: 305 case PPC::VRRCRegClassID: 306 case PPC::VFRCRegClassID: 307 case PPC::VSLRCRegClassID: 308 return 32 - DefaultSafety; 309 case PPC::VSRCRegClassID: 310 case PPC::VSFRCRegClassID: 311 case PPC::VSSRCRegClassID: 312 return 64 - DefaultSafety; 313 case PPC::CRRCRegClassID: 314 return 8 - DefaultSafety; 315 } 316 } 317 318 const TargetRegisterClass * 319 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 320 const MachineFunction &MF) const { 321 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 322 if (Subtarget.hasVSX()) { 323 // With VSX, we can inflate various sub-register classes to the full VSX 324 // register set. 325 326 if (RC == &PPC::F8RCRegClass) 327 return &PPC::VSFRCRegClass; 328 else if (RC == &PPC::VRRCRegClass) 329 return &PPC::VSRCRegClass; 330 else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector()) 331 return &PPC::VSSRCRegClass; 332 } 333 334 return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF); 335 } 336 337 //===----------------------------------------------------------------------===// 338 // Stack Frame Processing methods 339 //===----------------------------------------------------------------------===// 340 341 /// lowerDynamicAlloc - Generate the code for allocating an object in the 342 /// current frame. The sequence of code will be in the general form 343 /// 344 /// addi R0, SP, \#frameSize ; get the address of the previous frame 345 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size 346 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation 347 /// 348 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const { 349 // Get the instruction. 350 MachineInstr &MI = *II; 351 // Get the instruction's basic block. 352 MachineBasicBlock &MBB = *MI.getParent(); 353 // Get the basic block's function. 354 MachineFunction &MF = *MBB.getParent(); 355 // Get the frame info. 356 MachineFrameInfo &MFI = MF.getFrameInfo(); 357 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 358 // Get the instruction info. 359 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 360 // Determine whether 64-bit pointers are used. 361 bool LP64 = TM.isPPC64(); 362 DebugLoc dl = MI.getDebugLoc(); 363 364 // Get the maximum call stack size. 365 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 366 // Get the total frame size. 367 unsigned FrameSize = MFI.getStackSize(); 368 369 // Get stack alignments. 370 const PPCFrameLowering *TFI = getFrameLowering(MF); 371 unsigned TargetAlign = TFI->getStackAlignment(); 372 unsigned MaxAlign = MFI.getMaxAlignment(); 373 assert((maxCallFrameSize & (MaxAlign-1)) == 0 && 374 "Maximum call-frame size not sufficiently aligned"); 375 376 // Determine the previous frame's address. If FrameSize can't be 377 // represented as 16 bits or we need special alignment, then we load the 378 // previous frame's address from 0(SP). Why not do an addis of the hi? 379 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 380 // Constructing the constant and adding would take 3 instructions. 381 // Fortunately, a frame greater than 32K is rare. 382 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 383 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 384 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 385 386 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 387 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg) 388 .addReg(PPC::R31) 389 .addImm(FrameSize); 390 } else if (LP64) { 391 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg) 392 .addImm(0) 393 .addReg(PPC::X1); 394 } else { 395 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg) 396 .addImm(0) 397 .addReg(PPC::R1); 398 } 399 400 bool KillNegSizeReg = MI.getOperand(1).isKill(); 401 unsigned NegSizeReg = MI.getOperand(1).getReg(); 402 403 // Grow the stack and update the stack pointer link, then determine the 404 // address of new allocated space. 405 if (LP64) { 406 if (MaxAlign > TargetAlign) { 407 unsigned UnalNegSizeReg = NegSizeReg; 408 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 409 410 // Unfortunately, there is no andi, only andi., and we can't insert that 411 // here because we might clobber cr0 while it is live. 412 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg) 413 .addImm(~(MaxAlign-1)); 414 415 unsigned NegSizeReg1 = NegSizeReg; 416 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 417 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg) 418 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 419 .addReg(NegSizeReg1, RegState::Kill); 420 KillNegSizeReg = true; 421 } 422 423 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1) 424 .addReg(Reg, RegState::Kill) 425 .addReg(PPC::X1) 426 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 427 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 428 .addReg(PPC::X1) 429 .addImm(maxCallFrameSize); 430 } else { 431 if (MaxAlign > TargetAlign) { 432 unsigned UnalNegSizeReg = NegSizeReg; 433 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 434 435 // Unfortunately, there is no andi, only andi., and we can't insert that 436 // here because we might clobber cr0 while it is live. 437 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg) 438 .addImm(~(MaxAlign-1)); 439 440 unsigned NegSizeReg1 = NegSizeReg; 441 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 442 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg) 443 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 444 .addReg(NegSizeReg1, RegState::Kill); 445 KillNegSizeReg = true; 446 } 447 448 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1) 449 .addReg(Reg, RegState::Kill) 450 .addReg(PPC::R1) 451 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 452 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 453 .addReg(PPC::R1) 454 .addImm(maxCallFrameSize); 455 } 456 457 // Discard the DYNALLOC instruction. 458 MBB.erase(II); 459 } 460 461 void PPCRegisterInfo::lowerDynamicAreaOffset( 462 MachineBasicBlock::iterator II) const { 463 // Get the instruction. 464 MachineInstr &MI = *II; 465 // Get the instruction's basic block. 466 MachineBasicBlock &MBB = *MI.getParent(); 467 // Get the basic block's function. 468 MachineFunction &MF = *MBB.getParent(); 469 // Get the frame info. 470 MachineFrameInfo &MFI = MF.getFrameInfo(); 471 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 472 // Get the instruction info. 473 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 474 475 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 476 DebugLoc dl = MI.getDebugLoc(); 477 BuildMI(MBB, II, dl, TII.get(PPC::LI), MI.getOperand(0).getReg()) 478 .addImm(maxCallFrameSize); 479 MBB.erase(II); 480 } 481 482 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 483 /// reserving a whole register (R0), we scrounge for one here. This generates 484 /// code like this: 485 /// 486 /// mfcr rA ; Move the conditional register into GPR rA. 487 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 488 /// stw rA, FI ; Store rA to the frame. 489 /// 490 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 491 unsigned FrameIndex) const { 492 // Get the instruction. 493 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 494 // Get the instruction's basic block. 495 MachineBasicBlock &MBB = *MI.getParent(); 496 MachineFunction &MF = *MBB.getParent(); 497 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 498 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 499 DebugLoc dl = MI.getDebugLoc(); 500 501 bool LP64 = TM.isPPC64(); 502 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 503 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 504 505 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 506 unsigned SrcReg = MI.getOperand(0).getReg(); 507 508 // We need to store the CR in the low 4-bits of the saved value. First, issue 509 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg. 510 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 511 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 512 513 // If the saved register wasn't CR0, shift the bits left so that they are in 514 // CR0's slot. 515 if (SrcReg != PPC::CR0) { 516 unsigned Reg1 = Reg; 517 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 518 519 // rlwinm rA, rA, ShiftBits, 0, 31. 520 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 521 .addReg(Reg1, RegState::Kill) 522 .addImm(getEncodingValue(SrcReg) * 4) 523 .addImm(0) 524 .addImm(31); 525 } 526 527 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 528 .addReg(Reg, RegState::Kill), 529 FrameIndex); 530 531 // Discard the pseudo instruction. 532 MBB.erase(II); 533 } 534 535 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 536 unsigned FrameIndex) const { 537 // Get the instruction. 538 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 539 // Get the instruction's basic block. 540 MachineBasicBlock &MBB = *MI.getParent(); 541 MachineFunction &MF = *MBB.getParent(); 542 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 543 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 544 DebugLoc dl = MI.getDebugLoc(); 545 546 bool LP64 = TM.isPPC64(); 547 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 548 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 549 550 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 551 unsigned DestReg = MI.getOperand(0).getReg(); 552 assert(MI.definesRegister(DestReg) && 553 "RESTORE_CR does not define its destination"); 554 555 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 556 Reg), FrameIndex); 557 558 // If the reloaded register isn't CR0, shift the bits right so that they are 559 // in the right CR's slot. 560 if (DestReg != PPC::CR0) { 561 unsigned Reg1 = Reg; 562 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 563 564 unsigned ShiftBits = getEncodingValue(DestReg)*4; 565 // rlwinm r11, r11, 32-ShiftBits, 0, 31. 566 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 567 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0) 568 .addImm(31); 569 } 570 571 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg) 572 .addReg(Reg, RegState::Kill); 573 574 // Discard the pseudo instruction. 575 MBB.erase(II); 576 } 577 578 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II, 579 unsigned FrameIndex) const { 580 // Get the instruction. 581 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset> 582 // Get the instruction's basic block. 583 MachineBasicBlock &MBB = *MI.getParent(); 584 MachineFunction &MF = *MBB.getParent(); 585 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 586 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 587 DebugLoc dl = MI.getDebugLoc(); 588 589 bool LP64 = TM.isPPC64(); 590 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 591 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 592 593 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 594 unsigned SrcReg = MI.getOperand(0).getReg(); 595 596 BuildMI(MBB, II, dl, TII.get(TargetOpcode::KILL), 597 getCRFromCRBit(SrcReg)) 598 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 599 600 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 601 .addReg(getCRFromCRBit(SrcReg)); 602 603 // If the saved register wasn't CR0LT, shift the bits left so that the bit to 604 // store is the first one. Mask all but that bit. 605 unsigned Reg1 = Reg; 606 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 607 608 // rlwinm rA, rA, ShiftBits, 0, 0. 609 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 610 .addReg(Reg1, RegState::Kill) 611 .addImm(getEncodingValue(SrcReg)) 612 .addImm(0).addImm(0); 613 614 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 615 .addReg(Reg, RegState::Kill), 616 FrameIndex); 617 618 // Discard the pseudo instruction. 619 MBB.erase(II); 620 } 621 622 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, 623 unsigned FrameIndex) const { 624 // Get the instruction. 625 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset> 626 // Get the instruction's basic block. 627 MachineBasicBlock &MBB = *MI.getParent(); 628 MachineFunction &MF = *MBB.getParent(); 629 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 630 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 631 DebugLoc dl = MI.getDebugLoc(); 632 633 bool LP64 = TM.isPPC64(); 634 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 635 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 636 637 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 638 unsigned DestReg = MI.getOperand(0).getReg(); 639 assert(MI.definesRegister(DestReg) && 640 "RESTORE_CRBIT does not define its destination"); 641 642 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 643 Reg), FrameIndex); 644 645 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg); 646 647 unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 648 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO) 649 .addReg(getCRFromCRBit(DestReg)); 650 651 unsigned ShiftBits = getEncodingValue(DestReg); 652 // rlwimi r11, r10, 32-ShiftBits, ..., ... 653 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO) 654 .addReg(RegO, RegState::Kill) 655 .addReg(Reg, RegState::Kill) 656 .addImm(ShiftBits ? 32 - ShiftBits : 0) 657 .addImm(ShiftBits) 658 .addImm(ShiftBits); 659 660 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), 661 getCRFromCRBit(DestReg)) 662 .addReg(RegO, RegState::Kill) 663 // Make sure we have a use dependency all the way through this 664 // sequence of instructions. We can't have the other bits in the CR 665 // modified in between the mfocrf and the mtocrf. 666 .addReg(getCRFromCRBit(DestReg), RegState::Implicit); 667 668 // Discard the pseudo instruction. 669 MBB.erase(II); 670 } 671 672 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II, 673 unsigned FrameIndex) const { 674 // Get the instruction. 675 MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset> 676 // Get the instruction's basic block. 677 MachineBasicBlock &MBB = *MI.getParent(); 678 MachineFunction &MF = *MBB.getParent(); 679 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 680 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 681 DebugLoc dl = MI.getDebugLoc(); 682 683 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 684 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC); 685 unsigned SrcReg = MI.getOperand(0).getReg(); 686 687 BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg) 688 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 689 690 addFrameReference( 691 BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill), 692 FrameIndex); 693 694 // Discard the pseudo instruction. 695 MBB.erase(II); 696 } 697 698 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II, 699 unsigned FrameIndex) const { 700 // Get the instruction. 701 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset> 702 // Get the instruction's basic block. 703 MachineBasicBlock &MBB = *MI.getParent(); 704 MachineFunction &MF = *MBB.getParent(); 705 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 706 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 707 DebugLoc dl = MI.getDebugLoc(); 708 709 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 710 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC); 711 unsigned DestReg = MI.getOperand(0).getReg(); 712 assert(MI.definesRegister(DestReg) && 713 "RESTORE_VRSAVE does not define its destination"); 714 715 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ), 716 Reg), FrameIndex); 717 718 BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg) 719 .addReg(Reg, RegState::Kill); 720 721 // Discard the pseudo instruction. 722 MBB.erase(II); 723 } 724 725 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 726 unsigned Reg, int &FrameIdx) const { 727 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 728 // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4 729 // ABI, return true to prevent allocating an additional frame slot. 730 // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0 731 // is arbitrary and will be subsequently ignored. For 32-bit, we have 732 // previously created the stack slot if needed, so return its FrameIdx. 733 if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) { 734 if (TM.isPPC64()) 735 FrameIdx = 0; 736 else { 737 const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 738 FrameIdx = FI->getCRSpillFrameIndex(); 739 } 740 return true; 741 } 742 return false; 743 } 744 745 // Figure out if the offset in the instruction must be a multiple of 4. 746 // This is true for instructions like "STD". 747 static bool usesIXAddr(const MachineInstr &MI) { 748 unsigned OpC = MI.getOpcode(); 749 750 switch (OpC) { 751 default: 752 return false; 753 case PPC::LWA: 754 case PPC::LWA_32: 755 case PPC::LD: 756 case PPC::STD: 757 return true; 758 } 759 } 760 761 // Return the OffsetOperandNo given the FIOperandNum (and the instruction). 762 static unsigned getOffsetONFromFION(const MachineInstr &MI, 763 unsigned FIOperandNum) { 764 // Take into account whether it's an add or mem instruction 765 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2; 766 if (MI.isInlineAsm()) 767 OffsetOperandNo = FIOperandNum - 1; 768 else if (MI.getOpcode() == TargetOpcode::STACKMAP || 769 MI.getOpcode() == TargetOpcode::PATCHPOINT) 770 OffsetOperandNo = FIOperandNum + 1; 771 772 return OffsetOperandNo; 773 } 774 775 void 776 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 777 int SPAdj, unsigned FIOperandNum, 778 RegScavenger *RS) const { 779 assert(SPAdj == 0 && "Unexpected"); 780 781 // Get the instruction. 782 MachineInstr &MI = *II; 783 // Get the instruction's basic block. 784 MachineBasicBlock &MBB = *MI.getParent(); 785 // Get the basic block's function. 786 MachineFunction &MF = *MBB.getParent(); 787 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 788 // Get the instruction info. 789 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 790 // Get the frame info. 791 MachineFrameInfo &MFI = MF.getFrameInfo(); 792 DebugLoc dl = MI.getDebugLoc(); 793 794 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 795 796 // Get the frame index. 797 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 798 799 // Get the frame pointer save index. Users of this index are primarily 800 // DYNALLOC instructions. 801 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 802 int FPSI = FI->getFramePointerSaveIndex(); 803 // Get the instruction opcode. 804 unsigned OpC = MI.getOpcode(); 805 806 if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) { 807 lowerDynamicAreaOffset(II); 808 return; 809 } 810 811 // Special case for dynamic alloca. 812 if (FPSI && FrameIndex == FPSI && 813 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) { 814 lowerDynamicAlloc(II); 815 return; 816 } 817 818 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc. 819 if (OpC == PPC::SPILL_CR) { 820 lowerCRSpilling(II, FrameIndex); 821 return; 822 } else if (OpC == PPC::RESTORE_CR) { 823 lowerCRRestore(II, FrameIndex); 824 return; 825 } else if (OpC == PPC::SPILL_CRBIT) { 826 lowerCRBitSpilling(II, FrameIndex); 827 return; 828 } else if (OpC == PPC::RESTORE_CRBIT) { 829 lowerCRBitRestore(II, FrameIndex); 830 return; 831 } else if (OpC == PPC::SPILL_VRSAVE) { 832 lowerVRSAVESpilling(II, FrameIndex); 833 return; 834 } else if (OpC == PPC::RESTORE_VRSAVE) { 835 lowerVRSAVERestore(II, FrameIndex); 836 return; 837 } 838 839 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). 840 MI.getOperand(FIOperandNum).ChangeToRegister( 841 FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false); 842 843 // Figure out if the offset in the instruction is shifted right two bits. 844 bool isIXAddr = usesIXAddr(MI); 845 846 // If the instruction is not present in ImmToIdxMap, then it has no immediate 847 // form (and must be r+r). 848 bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP && 849 OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC); 850 851 // Now add the frame object offset to the offset from r1. 852 int Offset = MFI.getObjectOffset(FrameIndex); 853 Offset += MI.getOperand(OffsetOperandNo).getImm(); 854 855 // If we're not using a Frame Pointer that has been set to the value of the 856 // SP before having the stack size subtracted from it, then add the stack size 857 // to Offset to get the correct offset. 858 // Naked functions have stack size 0, although getStackSize may not reflect 859 // that because we didn't call all the pieces that compute it for naked 860 // functions. 861 if (!MF.getFunction()->hasFnAttribute(Attribute::Naked)) { 862 if (!(hasBasePointer(MF) && FrameIndex < 0)) 863 Offset += MFI.getStackSize(); 864 } 865 866 // If we can, encode the offset directly into the instruction. If this is a 867 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If 868 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits 869 // clear can be encoded. This is extremely uncommon, because normally you 870 // only "std" to a stack slot that is at least 4-byte aligned, but it can 871 // happen in invalid code. 872 assert(OpC != PPC::DBG_VALUE && 873 "This should be handled in a target-independent way"); 874 if (!noImmForm && ((isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) || 875 OpC == TargetOpcode::STACKMAP || 876 OpC == TargetOpcode::PATCHPOINT)) { 877 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 878 return; 879 } 880 881 // The offset doesn't fit into a single register, scavenge one to build the 882 // offset in. 883 884 bool is64Bit = TM.isPPC64(); 885 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 886 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 887 const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC; 888 unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC), 889 SReg = MF.getRegInfo().createVirtualRegister(RC); 890 891 // Insert a set of rA with the full offset value before the ld, st, or add 892 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi) 893 .addImm(Offset >> 16); 894 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg) 895 .addReg(SRegHi, RegState::Kill) 896 .addImm(Offset); 897 898 // Convert into indexed form of the instruction: 899 // 900 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 901 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 902 unsigned OperandBase; 903 904 if (noImmForm) 905 OperandBase = 1; 906 else if (OpC != TargetOpcode::INLINEASM) { 907 assert(ImmToIdxMap.count(OpC) && 908 "No indexed form of load or store available!"); 909 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second; 910 MI.setDesc(TII.get(NewOpcode)); 911 OperandBase = 1; 912 } else { 913 OperandBase = OffsetOperandNo; 914 } 915 916 unsigned StackReg = MI.getOperand(FIOperandNum).getReg(); 917 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false); 918 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true); 919 } 920 921 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 922 const PPCFrameLowering *TFI = getFrameLowering(MF); 923 924 if (!TM.isPPC64()) 925 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1; 926 else 927 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1; 928 } 929 930 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const { 931 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 932 if (!hasBasePointer(MF)) 933 return getFrameRegister(MF); 934 935 if (TM.isPPC64()) 936 return PPC::X30; 937 938 if (Subtarget.isSVR4ABI() && TM.isPositionIndependent()) 939 return PPC::R29; 940 941 return PPC::R30; 942 } 943 944 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const { 945 if (!EnableBasePointer) 946 return false; 947 if (AlwaysBasePointer) 948 return true; 949 950 // If we need to realign the stack, then the stack pointer can no longer 951 // serve as an offset into the caller's stack space. As a result, we need a 952 // base pointer. 953 return needsStackRealignment(MF); 954 } 955 956 /// Returns true if the instruction's frame index 957 /// reference would be better served by a base register other than FP 958 /// or SP. Used by LocalStackFrameAllocation to determine which frame index 959 /// references it should create new base registers for. 960 bool PPCRegisterInfo:: 961 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 962 assert(Offset < 0 && "Local offset must be negative"); 963 964 // It's the load/store FI references that cause issues, as it can be difficult 965 // to materialize the offset if it won't fit in the literal field. Estimate 966 // based on the size of the local frame and some conservative assumptions 967 // about the rest of the stack frame (note, this is pre-regalloc, so 968 // we don't know everything for certain yet) whether this offset is likely 969 // to be out of range of the immediate. Return true if so. 970 971 // We only generate virtual base registers for loads and stores that have 972 // an r+i form. Return false for everything else. 973 unsigned OpC = MI->getOpcode(); 974 if (!ImmToIdxMap.count(OpC)) 975 return false; 976 977 // Don't generate a new virtual base register just to add zero to it. 978 if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) && 979 MI->getOperand(2).getImm() == 0) 980 return false; 981 982 MachineBasicBlock &MBB = *MI->getParent(); 983 MachineFunction &MF = *MBB.getParent(); 984 const PPCFrameLowering *TFI = getFrameLowering(MF); 985 unsigned StackEst = TFI->determineFrameLayout(MF, false, true); 986 987 // If we likely don't need a stack frame, then we probably don't need a 988 // virtual base register either. 989 if (!StackEst) 990 return false; 991 992 // Estimate an offset from the stack pointer. 993 // The incoming offset is relating to the SP at the start of the function, 994 // but when we access the local it'll be relative to the SP after local 995 // allocation, so adjust our SP-relative offset by that allocation size. 996 Offset += StackEst; 997 998 // The frame pointer will point to the end of the stack, so estimate the 999 // offset as the difference between the object offset and the FP location. 1000 return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset); 1001 } 1002 1003 /// Insert defining instruction(s) for BaseReg to 1004 /// be a pointer to FrameIdx at the beginning of the basic block. 1005 void PPCRegisterInfo:: 1006 materializeFrameBaseRegister(MachineBasicBlock *MBB, 1007 unsigned BaseReg, int FrameIdx, 1008 int64_t Offset) const { 1009 unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI; 1010 1011 MachineBasicBlock::iterator Ins = MBB->begin(); 1012 DebugLoc DL; // Defaults to "unknown" 1013 if (Ins != MBB->end()) 1014 DL = Ins->getDebugLoc(); 1015 1016 const MachineFunction &MF = *MBB->getParent(); 1017 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1018 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1019 const MCInstrDesc &MCID = TII.get(ADDriOpc); 1020 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 1021 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); 1022 1023 BuildMI(*MBB, Ins, DL, MCID, BaseReg) 1024 .addFrameIndex(FrameIdx).addImm(Offset); 1025 } 1026 1027 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg, 1028 int64_t Offset) const { 1029 unsigned FIOperandNum = 0; 1030 while (!MI.getOperand(FIOperandNum).isFI()) { 1031 ++FIOperandNum; 1032 assert(FIOperandNum < MI.getNumOperands() && 1033 "Instr doesn't have FrameIndex operand!"); 1034 } 1035 1036 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false); 1037 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 1038 Offset += MI.getOperand(OffsetOperandNo).getImm(); 1039 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 1040 1041 MachineBasicBlock &MBB = *MI.getParent(); 1042 MachineFunction &MF = *MBB.getParent(); 1043 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1044 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1045 const MCInstrDesc &MCID = MI.getDesc(); 1046 MachineRegisterInfo &MRI = MF.getRegInfo(); 1047 MRI.constrainRegClass(BaseReg, 1048 TII.getRegClass(MCID, FIOperandNum, this, MF)); 1049 } 1050 1051 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 1052 unsigned BaseReg, 1053 int64_t Offset) const { 1054 unsigned FIOperandNum = 0; 1055 while (!MI->getOperand(FIOperandNum).isFI()) { 1056 ++FIOperandNum; 1057 assert(FIOperandNum < MI->getNumOperands() && 1058 "Instr doesn't have FrameIndex operand!"); 1059 } 1060 1061 unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum); 1062 Offset += MI->getOperand(OffsetOperandNo).getImm(); 1063 1064 return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm 1065 MI->getOpcode() == TargetOpcode::STACKMAP || 1066 MI->getOpcode() == TargetOpcode::PATCHPOINT || 1067 (isInt<16>(Offset) && (!usesIXAddr(*MI) || (Offset & 3) == 0)); 1068 } 1069