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 #define DEBUG_TYPE "reginfo" 16 #include "PPCRegisterInfo.h" 17 #include "PPC.h" 18 #include "PPCFrameLowering.h" 19 #include "PPCInstrBuilder.h" 20 #include "PPCMachineFunctionInfo.h" 21 #include "PPCSubtarget.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/CodeGen/ValueTypes.h" 31 #include "llvm/IR/CallingConv.h" 32 #include "llvm/IR/Constants.h" 33 #include "llvm/IR/Function.h" 34 #include "llvm/IR/Type.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/Debug.h" 37 #include "llvm/Support/ErrorHandling.h" 38 #include "llvm/Support/MathExtras.h" 39 #include "llvm/Support/raw_ostream.h" 40 #include "llvm/Target/TargetFrameLowering.h" 41 #include "llvm/Target/TargetInstrInfo.h" 42 #include "llvm/Target/TargetMachine.h" 43 #include "llvm/Target/TargetOptions.h" 44 #include <cstdlib> 45 46 #define GET_REGINFO_TARGET_DESC 47 #include "PPCGenRegisterInfo.inc" 48 49 using namespace llvm; 50 51 PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST, 52 const TargetInstrInfo &tii) 53 : PPCGenRegisterInfo(ST.isPPC64() ? PPC::LR8 : PPC::LR, 54 ST.isPPC64() ? 0 : 1, 55 ST.isPPC64() ? 0 : 1), 56 Subtarget(ST), TII(tii) { 57 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX; 58 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX; 59 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX; 60 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX; 61 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX; 62 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX; 63 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX; 64 ImmToIdxMap[PPC::ADDI] = PPC::ADD4; 65 66 // 64-bit 67 ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8; 68 ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8; 69 ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8; 70 ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX; 71 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; ImmToIdxMap[PPC::STD_32] = PPC::STDX_32; 72 } 73 74 /// getPointerRegClass - Return the register class to use to hold pointers. 75 /// This is used for addressing modes. 76 const TargetRegisterClass * 77 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) 78 const { 79 if (Kind == 1) { 80 if (Subtarget.isPPC64()) 81 return &PPC::G8RC_NOX0RegClass; 82 return &PPC::GPRC_NOR0RegClass; 83 } 84 85 if (Subtarget.isPPC64()) 86 return &PPC::G8RCRegClass; 87 return &PPC::GPRCRegClass; 88 } 89 90 const uint16_t* 91 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 92 if (Subtarget.isDarwinABI()) 93 return Subtarget.isPPC64() ? CSR_Darwin64_SaveList : 94 CSR_Darwin32_SaveList; 95 96 return Subtarget.isPPC64() ? CSR_SVR464_SaveList : CSR_SVR432_SaveList; 97 } 98 99 const uint32_t* 100 PPCRegisterInfo::getCallPreservedMask(CallingConv::ID CC) const { 101 if (Subtarget.isDarwinABI()) 102 return Subtarget.isPPC64() ? CSR_Darwin64_RegMask : 103 CSR_Darwin32_RegMask; 104 105 return Subtarget.isPPC64() ? CSR_SVR464_RegMask : CSR_SVR432_RegMask; 106 } 107 108 const uint32_t* 109 PPCRegisterInfo::getNoPreservedMask() const { 110 // The naming here is inverted: The CSR_NoRegs_Altivec has the 111 // Altivec registers masked so that they're not saved and restored around 112 // instructions with this preserved mask. 113 114 if (!Subtarget.hasAltivec()) 115 return CSR_NoRegs_Altivec_RegMask; 116 117 return CSR_NoRegs_RegMask; 118 } 119 120 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 121 BitVector Reserved(getNumRegs()); 122 const PPCFrameLowering *PPCFI = 123 static_cast<const PPCFrameLowering*>(MF.getTarget().getFrameLowering()); 124 125 // The ZERO register is not really a register, but the representation of r0 126 // when used in instructions that treat r0 as the constant 0. 127 Reserved.set(PPC::ZERO); 128 129 // The FP register is also not really a register, but is the representation 130 // of the frame pointer register used by ISD::FRAMEADDR. 131 Reserved.set(PPC::FP); 132 Reserved.set(PPC::FP8); 133 134 Reserved.set(PPC::R0); 135 Reserved.set(PPC::R1); 136 Reserved.set(PPC::LR); 137 Reserved.set(PPC::LR8); 138 Reserved.set(PPC::RM); 139 140 // The SVR4 ABI reserves r2 and r13 141 if (Subtarget.isSVR4ABI()) { 142 Reserved.set(PPC::R2); // System-reserved register 143 Reserved.set(PPC::R13); // Small Data Area pointer register 144 } 145 146 // On PPC64, r13 is the thread pointer. Never allocate this register. 147 if (Subtarget.isPPC64()) { 148 Reserved.set(PPC::R13); 149 150 Reserved.set(PPC::X0); 151 Reserved.set(PPC::X1); 152 Reserved.set(PPC::X13); 153 154 if (PPCFI->needsFP(MF)) 155 Reserved.set(PPC::X31); 156 157 // The 64-bit SVR4 ABI reserves r2 for the TOC pointer. 158 if (Subtarget.isSVR4ABI()) { 159 Reserved.set(PPC::X2); 160 } 161 } 162 163 if (PPCFI->needsFP(MF)) 164 Reserved.set(PPC::R31); 165 166 return Reserved; 167 } 168 169 unsigned 170 PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 171 MachineFunction &MF) const { 172 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 173 const unsigned DefaultSafety = 1; 174 175 switch (RC->getID()) { 176 default: 177 return 0; 178 case PPC::G8RC_NOX0RegClassID: 179 case PPC::GPRC_NOR0RegClassID: 180 case PPC::G8RCRegClassID: 181 case PPC::GPRCRegClassID: { 182 unsigned FP = TFI->hasFP(MF) ? 1 : 0; 183 return 32 - FP - DefaultSafety; 184 } 185 case PPC::F8RCRegClassID: 186 case PPC::F4RCRegClassID: 187 case PPC::VRRCRegClassID: 188 return 32 - DefaultSafety; 189 case PPC::CRRCRegClassID: 190 return 8 - DefaultSafety; 191 } 192 } 193 194 //===----------------------------------------------------------------------===// 195 // Stack Frame Processing methods 196 //===----------------------------------------------------------------------===// 197 198 /// lowerDynamicAlloc - Generate the code for allocating an object in the 199 /// current frame. The sequence of code with be in the general form 200 /// 201 /// addi R0, SP, \#frameSize ; get the address of the previous frame 202 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size 203 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation 204 /// 205 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II, 206 int SPAdj, RegScavenger *RS) const { 207 // Get the instruction. 208 MachineInstr &MI = *II; 209 // Get the instruction's basic block. 210 MachineBasicBlock &MBB = *MI.getParent(); 211 // Get the basic block's function. 212 MachineFunction &MF = *MBB.getParent(); 213 // Get the frame info. 214 MachineFrameInfo *MFI = MF.getFrameInfo(); 215 // Determine whether 64-bit pointers are used. 216 bool LP64 = Subtarget.isPPC64(); 217 DebugLoc dl = MI.getDebugLoc(); 218 219 // Get the maximum call stack size. 220 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize(); 221 // Get the total frame size. 222 unsigned FrameSize = MFI->getStackSize(); 223 224 // Get stack alignments. 225 unsigned TargetAlign = MF.getTarget().getFrameLowering()->getStackAlignment(); 226 unsigned MaxAlign = MFI->getMaxAlignment(); 227 if (MaxAlign > TargetAlign) 228 report_fatal_error("Dynamic alloca with large aligns not supported"); 229 230 // Determine the previous frame's address. If FrameSize can't be 231 // represented as 16 bits or we need special alignment, then we load the 232 // previous frame's address from 0(SP). Why not do an addis of the hi? 233 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 234 // Constructing the constant and adding would take 3 instructions. 235 // Fortunately, a frame greater than 32K is rare. 236 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 237 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 238 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 239 240 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 241 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg) 242 .addReg(PPC::R31) 243 .addImm(FrameSize); 244 } else if (LP64) { 245 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg) 246 .addImm(0) 247 .addReg(PPC::X1); 248 } else { 249 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg) 250 .addImm(0) 251 .addReg(PPC::R1); 252 } 253 254 // Grow the stack and update the stack pointer link, then determine the 255 // address of new allocated space. 256 if (LP64) { 257 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1) 258 .addReg(Reg, RegState::Kill) 259 .addReg(PPC::X1) 260 .addReg(MI.getOperand(1).getReg()); 261 if (!MI.getOperand(1).isKill()) 262 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 263 .addReg(PPC::X1) 264 .addImm(maxCallFrameSize); 265 else 266 // Implicitly kill the register. 267 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 268 .addReg(PPC::X1) 269 .addImm(maxCallFrameSize) 270 .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill); 271 } else { 272 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1) 273 .addReg(Reg, RegState::Kill) 274 .addReg(PPC::R1) 275 .addReg(MI.getOperand(1).getReg()); 276 277 if (!MI.getOperand(1).isKill()) 278 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 279 .addReg(PPC::R1) 280 .addImm(maxCallFrameSize); 281 else 282 // Implicitly kill the register. 283 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 284 .addReg(PPC::R1) 285 .addImm(maxCallFrameSize) 286 .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill); 287 } 288 289 // Discard the DYNALLOC instruction. 290 MBB.erase(II); 291 } 292 293 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 294 /// reserving a whole register (R0), we scrounge for one here. This generates 295 /// code like this: 296 /// 297 /// mfcr rA ; Move the conditional register into GPR rA. 298 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 299 /// stw rA, FI ; Store rA to the frame. 300 /// 301 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 302 unsigned FrameIndex, int SPAdj, 303 RegScavenger *RS) const { 304 // Get the instruction. 305 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 306 // Get the instruction's basic block. 307 MachineBasicBlock &MBB = *MI.getParent(); 308 DebugLoc dl = MI.getDebugLoc(); 309 310 // FIXME: Once LLVM supports creating virtual registers here, or the register 311 // scavenger can return multiple registers, stop using reserved registers 312 // here. 313 (void) SPAdj; 314 (void) RS; 315 316 bool LP64 = Subtarget.isPPC64(); 317 unsigned Reg = LP64 ? PPC::X0 : PPC::R0; 318 unsigned SrcReg = MI.getOperand(0).getReg(); 319 320 // We need to store the CR in the low 4-bits of the saved value. First, issue 321 // an MFCRpsued to save all of the CRBits and, if needed, kill the SrcReg. 322 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFCR8pseud : PPC::MFCRpseud), Reg) 323 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 324 325 // If the saved register wasn't CR0, shift the bits left so that they are in 326 // CR0's slot. 327 if (SrcReg != PPC::CR0) 328 // rlwinm rA, rA, ShiftBits, 0, 31. 329 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 330 .addReg(Reg, RegState::Kill) 331 .addImm(getPPCRegisterNumbering(SrcReg) * 4) 332 .addImm(0) 333 .addImm(31); 334 335 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 336 .addReg(Reg, getKillRegState(MI.getOperand(1).getImm())), 337 FrameIndex); 338 339 // Discard the pseudo instruction. 340 MBB.erase(II); 341 } 342 343 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 344 unsigned FrameIndex, int SPAdj, 345 RegScavenger *RS) const { 346 // Get the instruction. 347 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 348 // Get the instruction's basic block. 349 MachineBasicBlock &MBB = *MI.getParent(); 350 DebugLoc dl = MI.getDebugLoc(); 351 352 // FIXME: Once LLVM supports creating virtual registers here, or the register 353 // scavenger can return multiple registers, stop using reserved registers 354 // here. 355 (void) SPAdj; 356 (void) RS; 357 358 bool LP64 = Subtarget.isPPC64(); 359 unsigned Reg = LP64 ? PPC::X0 : PPC::R0; 360 unsigned DestReg = MI.getOperand(0).getReg(); 361 assert(MI.definesRegister(DestReg) && 362 "RESTORE_CR does not define its destination"); 363 364 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 365 Reg), FrameIndex); 366 367 // If the reloaded register isn't CR0, shift the bits right so that they are 368 // in the right CR's slot. 369 if (DestReg != PPC::CR0) { 370 unsigned ShiftBits = getPPCRegisterNumbering(DestReg)*4; 371 // rlwinm r11, r11, 32-ShiftBits, 0, 31. 372 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 373 .addReg(Reg).addImm(32-ShiftBits).addImm(0) 374 .addImm(31); 375 } 376 377 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTCRF8 : PPC::MTCRF), DestReg) 378 .addReg(Reg); 379 380 // Discard the pseudo instruction. 381 MBB.erase(II); 382 } 383 384 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II, 385 unsigned FrameIndex, int SPAdj, 386 RegScavenger *RS) const { 387 // Get the instruction. 388 MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset> 389 // Get the instruction's basic block. 390 MachineBasicBlock &MBB = *MI.getParent(); 391 DebugLoc dl = MI.getDebugLoc(); 392 393 // FIXME: Once LLVM supports creating virtual registers here, or the register 394 // scavenger can return multiple registers, stop using reserved registers 395 // here. 396 (void) SPAdj; 397 (void) RS; 398 399 unsigned Reg = PPC::R0; 400 unsigned SrcReg = MI.getOperand(0).getReg(); 401 402 BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg) 403 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 404 405 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::STW)) 406 .addReg(Reg, getKillRegState(MI.getOperand(1).getImm())), 407 FrameIndex); 408 409 // Discard the pseudo instruction. 410 MBB.erase(II); 411 } 412 413 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II, 414 unsigned FrameIndex, int SPAdj, 415 RegScavenger *RS) const { 416 // Get the instruction. 417 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset> 418 // Get the instruction's basic block. 419 MachineBasicBlock &MBB = *MI.getParent(); 420 DebugLoc dl = MI.getDebugLoc(); 421 422 // FIXME: Once LLVM supports creating virtual registers here, or the register 423 // scavenger can return multiple registers, stop using reserved registers 424 // here. 425 (void) SPAdj; 426 (void) RS; 427 428 unsigned Reg = PPC::R0; 429 unsigned DestReg = MI.getOperand(0).getReg(); 430 assert(MI.definesRegister(DestReg) && 431 "RESTORE_VRSAVE does not define its destination"); 432 433 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ), 434 Reg), FrameIndex); 435 436 BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg) 437 .addReg(Reg); 438 439 // Discard the pseudo instruction. 440 MBB.erase(II); 441 } 442 443 bool 444 PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 445 unsigned Reg, int &FrameIdx) const { 446 447 // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4 448 // ABI, return true to prevent allocating an additional frame slot. 449 // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0 450 // is arbitrary and will be subsequently ignored. For 32-bit, we have 451 // previously created the stack slot if needed, so return its FrameIdx. 452 if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) { 453 if (Subtarget.isPPC64()) 454 FrameIdx = 0; 455 else { 456 const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 457 FrameIdx = FI->getCRSpillFrameIndex(); 458 } 459 return true; 460 } 461 return false; 462 } 463 464 void 465 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 466 int SPAdj, unsigned FIOperandNum, 467 RegScavenger *RS) const { 468 assert(SPAdj == 0 && "Unexpected"); 469 470 // Get the instruction. 471 MachineInstr &MI = *II; 472 // Get the instruction's basic block. 473 MachineBasicBlock &MBB = *MI.getParent(); 474 // Get the basic block's function. 475 MachineFunction &MF = *MBB.getParent(); 476 // Get the frame info. 477 MachineFrameInfo *MFI = MF.getFrameInfo(); 478 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 479 DebugLoc dl = MI.getDebugLoc(); 480 481 // Take into account whether it's an add or mem instruction 482 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2; 483 if (MI.isInlineAsm()) 484 OffsetOperandNo = FIOperandNum-1; 485 486 // Get the frame index. 487 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 488 489 // Get the frame pointer save index. Users of this index are primarily 490 // DYNALLOC instructions. 491 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 492 int FPSI = FI->getFramePointerSaveIndex(); 493 // Get the instruction opcode. 494 unsigned OpC = MI.getOpcode(); 495 496 // Special case for dynamic alloca. 497 if (FPSI && FrameIndex == FPSI && 498 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) { 499 lowerDynamicAlloc(II, SPAdj, RS); 500 return; 501 } 502 503 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc. 504 if (OpC == PPC::SPILL_CR) { 505 lowerCRSpilling(II, FrameIndex, SPAdj, RS); 506 return; 507 } else if (OpC == PPC::RESTORE_CR) { 508 lowerCRRestore(II, FrameIndex, SPAdj, RS); 509 return; 510 } else if (OpC == PPC::SPILL_VRSAVE) { 511 lowerVRSAVESpilling(II, FrameIndex, SPAdj, RS); 512 return; 513 } else if (OpC == PPC::RESTORE_VRSAVE) { 514 lowerVRSAVERestore(II, FrameIndex, SPAdj, RS); 515 return; 516 } 517 518 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). 519 520 bool is64Bit = Subtarget.isPPC64(); 521 MI.getOperand(FIOperandNum).ChangeToRegister(TFI->hasFP(MF) ? 522 (is64Bit ? PPC::X31 : PPC::R31) : 523 (is64Bit ? PPC::X1 : PPC::R1), 524 false); 525 526 // Figure out if the offset in the instruction is shifted right two bits. This 527 // is true for instructions like "STD", which the machine implicitly adds two 528 // low zeros to. 529 bool isIXAddr = false; 530 switch (OpC) { 531 case PPC::LWA: 532 case PPC::LD: 533 case PPC::STD: 534 case PPC::STD_32: 535 isIXAddr = true; 536 break; 537 } 538 539 bool noImmForm = false; 540 switch (OpC) { 541 case PPC::LVEBX: 542 case PPC::LVEHX: 543 case PPC::LVEWX: 544 case PPC::LVX: 545 case PPC::LVXL: 546 case PPC::LVSL: 547 case PPC::LVSR: 548 case PPC::STVEBX: 549 case PPC::STVEHX: 550 case PPC::STVEWX: 551 case PPC::STVX: 552 case PPC::STVXL: 553 noImmForm = true; 554 break; 555 } 556 557 // Now add the frame object offset to the offset from r1. 558 int Offset = MFI->getObjectOffset(FrameIndex); 559 if (!isIXAddr) 560 Offset += MI.getOperand(OffsetOperandNo).getImm(); 561 else 562 Offset += MI.getOperand(OffsetOperandNo).getImm() << 2; 563 564 // If we're not using a Frame Pointer that has been set to the value of the 565 // SP before having the stack size subtracted from it, then add the stack size 566 // to Offset to get the correct offset. 567 // Naked functions have stack size 0, although getStackSize may not reflect that 568 // because we didn't call all the pieces that compute it for naked functions. 569 if (!MF.getFunction()->getAttributes(). 570 hasAttribute(AttributeSet::FunctionIndex, Attribute::Naked)) 571 Offset += MFI->getStackSize(); 572 573 // If we can, encode the offset directly into the instruction. If this is a 574 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If 575 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits 576 // clear can be encoded. This is extremely uncommon, because normally you 577 // only "std" to a stack slot that is at least 4-byte aligned, but it can 578 // happen in invalid code. 579 if (OpC == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm 580 (!noImmForm && 581 isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0))) { 582 if (isIXAddr) 583 Offset >>= 2; // The actual encoded value has the low two bits zero. 584 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 585 return; 586 } 587 588 // The offset doesn't fit into a single register, scavenge one to build the 589 // offset in. 590 591 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 592 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 593 unsigned SReg = MF.getRegInfo().createVirtualRegister(is64Bit ? G8RC : GPRC); 594 595 // Insert a set of rA with the full offset value before the ld, st, or add 596 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SReg) 597 .addImm(Offset >> 16); 598 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg) 599 .addReg(SReg, RegState::Kill) 600 .addImm(Offset); 601 602 // Convert into indexed form of the instruction: 603 // 604 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 605 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 606 unsigned OperandBase; 607 608 if (noImmForm) 609 OperandBase = 1; 610 else if (OpC != TargetOpcode::INLINEASM) { 611 assert(ImmToIdxMap.count(OpC) && 612 "No indexed form of load or store available!"); 613 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second; 614 MI.setDesc(TII.get(NewOpcode)); 615 OperandBase = 1; 616 } else { 617 OperandBase = OffsetOperandNo; 618 } 619 620 unsigned StackReg = MI.getOperand(FIOperandNum).getReg(); 621 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false); 622 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true); 623 } 624 625 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 626 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); 627 628 if (!Subtarget.isPPC64()) 629 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1; 630 else 631 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1; 632 } 633 634 unsigned PPCRegisterInfo::getEHExceptionRegister() const { 635 return !Subtarget.isPPC64() ? PPC::R3 : PPC::X3; 636 } 637 638 unsigned PPCRegisterInfo::getEHHandlerRegister() const { 639 return !Subtarget.isPPC64() ? PPC::R4 : PPC::X4; 640 } 641