1 //===- PPCRegisterInfo.cpp - PowerPC Register Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the PowerPC implementation of the TargetRegisterInfo
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "reginfo"
17 #include "PPCInstrBuilder.h"
18 #include "PPCMachineFunctionInfo.h"
19 #include "PPCRegisterInfo.h"
20 #include "PPCFrameLowering.h"
21 #include "PPCSubtarget.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/Constants.h"
24 #include "llvm/Function.h"
25 #include "llvm/Type.h"
26 #include "llvm/CodeGen/ValueTypes.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/RegisterScavenging.h"
33 #include "llvm/Target/TargetFrameLowering.h"
34 #include "llvm/Target/TargetInstrInfo.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetOptions.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/ADT/BitVector.h"
43 #include "llvm/ADT/STLExtras.h"
46 #define GET_REGINFO_TARGET_DESC
47 #include "PPCGenRegisterInfo.inc"
49 // FIXME (64-bit): Eventually enable by default.
51 cl::opt<bool> EnablePPC32RS("enable-ppc32-regscavenger",
53 cl::desc("Enable PPC32 register scavenger"),
55 cl::opt<bool> EnablePPC64RS("enable-ppc64-regscavenger",
57 cl::desc("Enable PPC64 register scavenger"),
63 // FIXME (64-bit): Should be inlined.
65 PPCRegisterInfo::requiresRegisterScavenging(const MachineFunction &) const {
66 return ((EnablePPC32RS && !Subtarget.isPPC64()) ||
67 (EnablePPC64RS && Subtarget.isPPC64()));
70 PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST,
71 const TargetInstrInfo &tii)
72 : PPCGenRegisterInfo(ST.isPPC64() ? PPC::LR8 : PPC::LR,
74 ST.isPPC64() ? 0 : 1),
75 Subtarget(ST), TII(tii) {
76 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
77 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
78 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
79 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
80 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
81 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
82 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
83 ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
86 ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
87 ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
88 ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
89 ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
90 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; ImmToIdxMap[PPC::STD_32] = PPC::STDX_32;
93 /// getPointerRegClass - Return the register class to use to hold pointers.
94 /// This is used for addressing modes.
95 const TargetRegisterClass *
96 PPCRegisterInfo::getPointerRegClass(unsigned Kind) const {
97 if (Subtarget.isPPC64())
98 return &PPC::G8RCRegClass;
99 return &PPC::GPRCRegClass;
103 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
104 // 32-bit Darwin calling convention.
105 static const unsigned Darwin32_CalleeSavedRegs[] = {
106 PPC::R13, PPC::R14, PPC::R15,
107 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
108 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
109 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
110 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
112 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
113 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
114 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
115 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
118 PPC::CR2, PPC::CR3, PPC::CR4,
119 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
120 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
121 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
123 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
124 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
125 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
130 // 32-bit SVR4 calling convention.
131 static const unsigned SVR4_CalleeSavedRegs[] = {
133 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
134 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
135 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
136 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
138 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
139 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
140 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
141 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
144 PPC::CR2, PPC::CR3, PPC::CR4,
148 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
149 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
150 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
152 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
153 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
154 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
158 // 64-bit Darwin calling convention.
159 static const unsigned Darwin64_CalleeSavedRegs[] = {
161 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
162 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
163 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
164 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
166 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
167 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
168 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
169 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
172 PPC::CR2, PPC::CR3, PPC::CR4,
173 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
174 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
175 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
177 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
178 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
179 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
184 // 64-bit SVR4 calling convention.
185 static const unsigned SVR4_64_CalleeSavedRegs[] = {
187 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
188 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
189 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
190 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
192 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
193 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
194 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
195 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
198 PPC::CR2, PPC::CR3, PPC::CR4,
202 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
203 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
204 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
206 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
207 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
208 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
213 if (Subtarget.isDarwinABI())
214 return Subtarget.isPPC64() ? Darwin64_CalleeSavedRegs :
215 Darwin32_CalleeSavedRegs;
217 return Subtarget.isPPC64() ? SVR4_64_CalleeSavedRegs : SVR4_CalleeSavedRegs;
220 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
221 BitVector Reserved(getNumRegs());
222 const PPCFrameLowering *PPCFI =
223 static_cast<const PPCFrameLowering*>(MF.getTarget().getFrameLowering());
225 Reserved.set(PPC::R0);
226 Reserved.set(PPC::R1);
227 Reserved.set(PPC::LR);
228 Reserved.set(PPC::LR8);
229 Reserved.set(PPC::RM);
231 // The SVR4 ABI reserves r2 and r13
232 if (Subtarget.isSVR4ABI()) {
233 Reserved.set(PPC::R2); // System-reserved register
234 Reserved.set(PPC::R13); // Small Data Area pointer register
236 // Reserve R2 on Darwin to hack around the problem of save/restore of CR
237 // when the stack frame is too big to address directly; we need two regs.
239 if (Subtarget.isDarwinABI()) {
240 Reserved.set(PPC::R2);
243 // On PPC64, r13 is the thread pointer. Never allocate this register.
244 // Note that this is over conservative, as it also prevents allocation of R31
245 // when the FP is not needed.
246 if (Subtarget.isPPC64()) {
247 Reserved.set(PPC::R13);
248 Reserved.set(PPC::R31);
250 if (!requiresRegisterScavenging(MF))
251 Reserved.set(PPC::R0); // FIXME (64-bit): Remove
253 Reserved.set(PPC::X0);
254 Reserved.set(PPC::X1);
255 Reserved.set(PPC::X13);
256 Reserved.set(PPC::X31);
258 // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
259 if (Subtarget.isSVR4ABI()) {
260 Reserved.set(PPC::X2);
262 // Reserve R2 on Darwin to hack around the problem of save/restore of CR
263 // when the stack frame is too big to address directly; we need two regs.
265 if (Subtarget.isDarwinABI()) {
266 Reserved.set(PPC::X2);
270 if (PPCFI->needsFP(MF))
271 Reserved.set(PPC::R31);
277 PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
278 MachineFunction &MF) const {
279 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
280 const unsigned DefaultSafety = 1;
282 switch (RC->getID()) {
285 case PPC::G8RCRegClassID:
286 case PPC::GPRCRegClassID: {
287 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
288 return 32 - FP - DefaultSafety;
290 case PPC::F8RCRegClassID:
291 case PPC::F4RCRegClassID:
292 case PPC::VRRCRegClassID:
293 return 32 - DefaultSafety;
297 //===----------------------------------------------------------------------===//
298 // Stack Frame Processing methods
299 //===----------------------------------------------------------------------===//
301 void PPCRegisterInfo::
302 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
303 MachineBasicBlock::iterator I) const {
304 if (GuaranteedTailCallOpt && I->getOpcode() == PPC::ADJCALLSTACKUP) {
305 // Add (actually subtract) back the amount the callee popped on return.
306 if (int CalleeAmt = I->getOperand(1).getImm()) {
307 bool is64Bit = Subtarget.isPPC64();
309 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
310 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
311 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
312 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
313 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
314 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
315 MachineInstr *MI = I;
316 DebugLoc dl = MI->getDebugLoc();
318 if (isInt<16>(CalleeAmt)) {
319 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg).addReg(StackReg).
322 MachineBasicBlock::iterator MBBI = I;
323 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
324 .addImm(CalleeAmt >> 16);
325 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
326 .addReg(TmpReg, RegState::Kill)
327 .addImm(CalleeAmt & 0xFFFF);
328 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
335 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
339 /// findScratchRegister - Find a 'free' PPC register. Try for a call-clobbered
340 /// register first and then a spilled callee-saved register if that fails.
342 unsigned findScratchRegister(MachineBasicBlock::iterator II, RegScavenger *RS,
343 const TargetRegisterClass *RC, int SPAdj) {
344 assert(RS && "Register scavenging must be on");
345 unsigned Reg = RS->FindUnusedReg(RC);
346 // FIXME: move ARM callee-saved reg scan to target independent code, then
347 // search for already spilled CS register here.
349 Reg = RS->scavengeRegister(RC, II, SPAdj);
353 /// lowerDynamicAlloc - Generate the code for allocating an object in the
354 /// current frame. The sequence of code with be in the general form
356 /// addi R0, SP, \#frameSize ; get the address of the previous frame
357 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
358 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
360 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II,
361 int SPAdj, RegScavenger *RS) const {
362 // Get the instruction.
363 MachineInstr &MI = *II;
364 // Get the instruction's basic block.
365 MachineBasicBlock &MBB = *MI.getParent();
366 // Get the basic block's function.
367 MachineFunction &MF = *MBB.getParent();
368 // Get the frame info.
369 MachineFrameInfo *MFI = MF.getFrameInfo();
370 // Determine whether 64-bit pointers are used.
371 bool LP64 = Subtarget.isPPC64();
372 DebugLoc dl = MI.getDebugLoc();
374 // Get the maximum call stack size.
375 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
376 // Get the total frame size.
377 unsigned FrameSize = MFI->getStackSize();
379 // Get stack alignments.
380 unsigned TargetAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
381 unsigned MaxAlign = MFI->getMaxAlignment();
382 if (MaxAlign > TargetAlign)
383 report_fatal_error("Dynamic alloca with large aligns not supported");
385 // Determine the previous frame's address. If FrameSize can't be
386 // represented as 16 bits or we need special alignment, then we load the
387 // previous frame's address from 0(SP). Why not do an addis of the hi?
388 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
389 // Constructing the constant and adding would take 3 instructions.
390 // Fortunately, a frame greater than 32K is rare.
391 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
392 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
393 const TargetRegisterClass *RC = LP64 ? G8RC : GPRC;
395 // FIXME (64-bit): Use "findScratchRegister"
397 if (requiresRegisterScavenging(MF))
398 Reg = findScratchRegister(II, RS, RC, SPAdj);
402 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
403 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
407 if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Use "true" part.
408 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
412 BuildMI(MBB, II, dl, TII.get(PPC::LD), PPC::X0)
416 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
421 // Grow the stack and update the stack pointer link, then determine the
422 // address of new allocated space.
424 if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Use "true" part.
425 BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
426 .addReg(Reg, RegState::Kill)
428 .addReg(MI.getOperand(1).getReg());
430 BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
431 .addReg(PPC::X0, RegState::Kill)
433 .addReg(MI.getOperand(1).getReg());
435 if (!MI.getOperand(1).isKill())
436 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
438 .addImm(maxCallFrameSize);
440 // Implicitly kill the register.
441 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
443 .addImm(maxCallFrameSize)
444 .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
446 BuildMI(MBB, II, dl, TII.get(PPC::STWUX))
447 .addReg(Reg, RegState::Kill)
449 .addReg(MI.getOperand(1).getReg());
451 if (!MI.getOperand(1).isKill())
452 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
454 .addImm(maxCallFrameSize);
456 // Implicitly kill the register.
457 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
459 .addImm(maxCallFrameSize)
460 .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
463 // Discard the DYNALLOC instruction.
467 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
468 /// reserving a whole register (R0), we scrounge for one here. This generates
471 /// mfcr rA ; Move the conditional register into GPR rA.
472 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
473 /// stw rA, FI ; Store rA to the frame.
475 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
476 unsigned FrameIndex, int SPAdj,
477 RegScavenger *RS) const {
478 // Get the instruction.
479 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>, <FI>
480 // Get the instruction's basic block.
481 MachineBasicBlock &MBB = *MI.getParent();
482 DebugLoc dl = MI.getDebugLoc();
484 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
485 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
486 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
487 unsigned Reg = findScratchRegister(II, RS, RC, SPAdj);
488 unsigned SrcReg = MI.getOperand(0).getReg();
489 bool LP64 = Subtarget.isPPC64();
491 // We need to store the CR in the low 4-bits of the saved value. First, issue
492 // an MFCRpsued to save all of the CRBits and, if needed, kill the SrcReg.
493 BuildMI(MBB, II, dl, TII.get(PPC::MFCRpseud), Reg)
494 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
496 // If the saved register wasn't CR0, shift the bits left so that they are in
498 if (SrcReg != PPC::CR0)
499 // rlwinm rA, rA, ShiftBits, 0, 31.
500 BuildMI(MBB, II, dl, TII.get(PPC::RLWINM), Reg)
501 .addReg(Reg, RegState::Kill)
502 .addImm(getPPCRegisterNumbering(SrcReg) * 4)
506 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
507 .addReg(Reg, getKillRegState(MI.getOperand(1).getImm())),
510 // Discard the pseudo instruction.
515 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
516 int SPAdj, RegScavenger *RS) const {
517 assert(SPAdj == 0 && "Unexpected");
519 // Get the instruction.
520 MachineInstr &MI = *II;
521 // Get the instruction's basic block.
522 MachineBasicBlock &MBB = *MI.getParent();
523 // Get the basic block's function.
524 MachineFunction &MF = *MBB.getParent();
525 // Get the frame info.
526 MachineFrameInfo *MFI = MF.getFrameInfo();
527 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
528 DebugLoc dl = MI.getDebugLoc();
530 // Find out which operand is the frame index.
531 unsigned FIOperandNo = 0;
532 while (!MI.getOperand(FIOperandNo).isFI()) {
534 assert(FIOperandNo != MI.getNumOperands() &&
535 "Instr doesn't have FrameIndex operand!");
537 // Take into account whether it's an add or mem instruction
538 unsigned OffsetOperandNo = (FIOperandNo == 2) ? 1 : 2;
539 if (MI.isInlineAsm())
540 OffsetOperandNo = FIOperandNo-1;
542 // Get the frame index.
543 int FrameIndex = MI.getOperand(FIOperandNo).getIndex();
545 // Get the frame pointer save index. Users of this index are primarily
546 // DYNALLOC instructions.
547 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
548 int FPSI = FI->getFramePointerSaveIndex();
549 // Get the instruction opcode.
550 unsigned OpC = MI.getOpcode();
552 // Special case for dynamic alloca.
553 if (FPSI && FrameIndex == FPSI &&
554 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
555 lowerDynamicAlloc(II, SPAdj, RS);
559 // Special case for pseudo-op SPILL_CR.
560 if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Enable by default.
561 if (OpC == PPC::SPILL_CR) {
562 lowerCRSpilling(II, FrameIndex, SPAdj, RS);
566 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
567 MI.getOperand(FIOperandNo).ChangeToRegister(TFI->hasFP(MF) ?
571 // Figure out if the offset in the instruction is shifted right two bits. This
572 // is true for instructions like "STD", which the machine implicitly adds two
574 bool isIXAddr = false;
584 // Now add the frame object offset to the offset from r1.
585 int Offset = MFI->getObjectOffset(FrameIndex);
587 Offset += MI.getOperand(OffsetOperandNo).getImm();
589 Offset += MI.getOperand(OffsetOperandNo).getImm() << 2;
591 // If we're not using a Frame Pointer that has been set to the value of the
592 // SP before having the stack size subtracted from it, then add the stack size
593 // to Offset to get the correct offset.
594 // Naked functions have stack size 0, although getStackSize may not reflect that
595 // because we didn't call all the pieces that compute it for naked functions.
596 if (!MF.getFunction()->hasFnAttr(Attribute::Naked))
597 Offset += MFI->getStackSize();
599 // If we can, encode the offset directly into the instruction. If this is a
600 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
601 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
602 // clear can be encoded. This is extremely uncommon, because normally you
603 // only "std" to a stack slot that is at least 4-byte aligned, but it can
604 // happen in invalid code.
605 if (isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) {
607 Offset >>= 2; // The actual encoded value has the low two bits zero.
608 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
612 // The offset doesn't fit into a single register, scavenge one to build the
614 // FIXME: figure out what SPAdj is doing here.
616 // FIXME (64-bit): Use "findScratchRegister".
618 if (requiresRegisterScavenging(MF))
619 SReg = findScratchRegister(II, RS, &PPC::GPRCRegClass, SPAdj);
623 // Insert a set of rA with the full offset value before the ld, st, or add
624 BuildMI(MBB, II, dl, TII.get(PPC::LIS), SReg)
625 .addImm(Offset >> 16);
626 BuildMI(MBB, II, dl, TII.get(PPC::ORI), SReg)
627 .addReg(SReg, RegState::Kill)
630 // Convert into indexed form of the instruction:
632 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
633 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
634 unsigned OperandBase;
636 if (OpC != TargetOpcode::INLINEASM) {
637 assert(ImmToIdxMap.count(OpC) &&
638 "No indexed form of load or store available!");
639 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
640 MI.setDesc(TII.get(NewOpcode));
643 OperandBase = OffsetOperandNo;
646 unsigned StackReg = MI.getOperand(FIOperandNo).getReg();
647 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
648 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false);
651 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
652 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
654 if (!Subtarget.isPPC64())
655 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
657 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
660 unsigned PPCRegisterInfo::getEHExceptionRegister() const {
661 return !Subtarget.isPPC64() ? PPC::R3 : PPC::X3;
664 unsigned PPCRegisterInfo::getEHHandlerRegister() const {
665 return !Subtarget.isPPC64() ? PPC::R4 : PPC::X4;