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,
126 // 32-bit SVR4 calling convention.
127 static const unsigned SVR4_CalleeSavedRegs[] = {
129 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
130 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
131 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
132 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
134 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
135 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
136 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
137 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
140 PPC::CR2, PPC::CR3, PPC::CR4,
144 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
145 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
146 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
150 // 64-bit Darwin calling convention.
151 static const unsigned Darwin64_CalleeSavedRegs[] = {
153 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
154 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
155 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
156 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
158 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
159 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
160 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
161 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
164 PPC::CR2, PPC::CR3, PPC::CR4,
165 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
166 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
167 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
172 // 64-bit SVR4 calling convention.
173 static const unsigned SVR4_64_CalleeSavedRegs[] = {
175 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
176 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
177 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
178 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
180 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
181 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
182 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
183 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
186 PPC::CR2, PPC::CR3, PPC::CR4,
190 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
191 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
192 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
197 if (Subtarget.isDarwinABI())
198 return Subtarget.isPPC64() ? Darwin64_CalleeSavedRegs :
199 Darwin32_CalleeSavedRegs;
201 return Subtarget.isPPC64() ? SVR4_64_CalleeSavedRegs : SVR4_CalleeSavedRegs;
204 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
205 BitVector Reserved(getNumRegs());
206 const PPCFrameLowering *PPCFI =
207 static_cast<const PPCFrameLowering*>(MF.getTarget().getFrameLowering());
209 Reserved.set(PPC::R0);
210 Reserved.set(PPC::R1);
211 Reserved.set(PPC::LR);
212 Reserved.set(PPC::LR8);
213 Reserved.set(PPC::RM);
215 // The SVR4 ABI reserves r2 and r13
216 if (Subtarget.isSVR4ABI()) {
217 Reserved.set(PPC::R2); // System-reserved register
218 Reserved.set(PPC::R13); // Small Data Area pointer register
220 // Reserve R2 on Darwin to hack around the problem of save/restore of CR
221 // when the stack frame is too big to address directly; we need two regs.
223 if (Subtarget.isDarwinABI()) {
224 Reserved.set(PPC::R2);
227 // On PPC64, r13 is the thread pointer. Never allocate this register.
228 // Note that this is over conservative, as it also prevents allocation of R31
229 // when the FP is not needed.
230 if (Subtarget.isPPC64()) {
231 Reserved.set(PPC::R13);
232 Reserved.set(PPC::R31);
234 if (!requiresRegisterScavenging(MF))
235 Reserved.set(PPC::R0); // FIXME (64-bit): Remove
237 Reserved.set(PPC::X0);
238 Reserved.set(PPC::X1);
239 Reserved.set(PPC::X13);
240 Reserved.set(PPC::X31);
242 // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
243 if (Subtarget.isSVR4ABI()) {
244 Reserved.set(PPC::X2);
246 // Reserve R2 on Darwin to hack around the problem of save/restore of CR
247 // when the stack frame is too big to address directly; we need two regs.
249 if (Subtarget.isDarwinABI()) {
250 Reserved.set(PPC::X2);
254 if (PPCFI->needsFP(MF))
255 Reserved.set(PPC::R31);
261 PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
262 MachineFunction &MF) const {
263 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
264 const unsigned DefaultSafety = 1;
266 switch (RC->getID()) {
269 case PPC::G8RCRegClassID:
270 case PPC::GPRCRegClassID: {
271 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
272 return 32 - FP - DefaultSafety;
274 case PPC::F8RCRegClassID:
275 case PPC::F4RCRegClassID:
276 case PPC::VRRCRegClassID:
277 return 32 - DefaultSafety;
278 case PPC::CRRCRegClassID:
279 return 8 - DefaultSafety;
283 //===----------------------------------------------------------------------===//
284 // Stack Frame Processing methods
285 //===----------------------------------------------------------------------===//
287 void PPCRegisterInfo::
288 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
289 MachineBasicBlock::iterator I) const {
290 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
291 I->getOpcode() == PPC::ADJCALLSTACKUP) {
292 // Add (actually subtract) back the amount the callee popped on return.
293 if (int CalleeAmt = I->getOperand(1).getImm()) {
294 bool is64Bit = Subtarget.isPPC64();
296 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
297 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
298 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
299 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
300 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
301 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
302 MachineInstr *MI = I;
303 DebugLoc dl = MI->getDebugLoc();
305 if (isInt<16>(CalleeAmt)) {
306 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg).addReg(StackReg).
309 MachineBasicBlock::iterator MBBI = I;
310 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
311 .addImm(CalleeAmt >> 16);
312 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
313 .addReg(TmpReg, RegState::Kill)
314 .addImm(CalleeAmt & 0xFFFF);
315 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
322 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
326 /// findScratchRegister - Find a 'free' PPC register. Try for a call-clobbered
327 /// register first and then a spilled callee-saved register if that fails.
329 unsigned findScratchRegister(MachineBasicBlock::iterator II, RegScavenger *RS,
330 const TargetRegisterClass *RC, int SPAdj) {
331 assert(RS && "Register scavenging must be on");
332 unsigned Reg = RS->FindUnusedReg(RC);
333 // FIXME: move ARM callee-saved reg scan to target independent code, then
334 // search for already spilled CS register here.
336 Reg = RS->scavengeRegister(RC, II, SPAdj);
340 /// lowerDynamicAlloc - Generate the code for allocating an object in the
341 /// current frame. The sequence of code with be in the general form
343 /// addi R0, SP, \#frameSize ; get the address of the previous frame
344 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
345 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
347 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II,
348 int SPAdj, RegScavenger *RS) 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 // Determine whether 64-bit pointers are used.
358 bool LP64 = Subtarget.isPPC64();
359 DebugLoc dl = MI.getDebugLoc();
361 // Get the maximum call stack size.
362 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
363 // Get the total frame size.
364 unsigned FrameSize = MFI->getStackSize();
366 // Get stack alignments.
367 unsigned TargetAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
368 unsigned MaxAlign = MFI->getMaxAlignment();
369 if (MaxAlign > TargetAlign)
370 report_fatal_error("Dynamic alloca with large aligns not supported");
372 // Determine the previous frame's address. If FrameSize can't be
373 // represented as 16 bits or we need special alignment, then we load the
374 // previous frame's address from 0(SP). Why not do an addis of the hi?
375 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
376 // Constructing the constant and adding would take 3 instructions.
377 // Fortunately, a frame greater than 32K is rare.
378 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
379 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
380 const TargetRegisterClass *RC = LP64 ? G8RC : GPRC;
382 // FIXME (64-bit): Use "findScratchRegister"
384 if (requiresRegisterScavenging(MF))
385 Reg = findScratchRegister(II, RS, RC, SPAdj);
389 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
390 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
394 if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Use "true" part.
395 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
399 BuildMI(MBB, II, dl, TII.get(PPC::LD), PPC::X0)
403 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
408 // Grow the stack and update the stack pointer link, then determine the
409 // address of new allocated space.
411 if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Use "true" part.
412 BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
413 .addReg(Reg, RegState::Kill)
415 .addReg(MI.getOperand(1).getReg());
417 BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
418 .addReg(PPC::X0, RegState::Kill)
420 .addReg(MI.getOperand(1).getReg());
422 if (!MI.getOperand(1).isKill())
423 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
425 .addImm(maxCallFrameSize);
427 // Implicitly kill the register.
428 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
430 .addImm(maxCallFrameSize)
431 .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
433 BuildMI(MBB, II, dl, TII.get(PPC::STWUX))
434 .addReg(Reg, RegState::Kill)
436 .addReg(MI.getOperand(1).getReg());
438 if (!MI.getOperand(1).isKill())
439 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
441 .addImm(maxCallFrameSize);
443 // Implicitly kill the register.
444 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
446 .addImm(maxCallFrameSize)
447 .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
450 // Discard the DYNALLOC instruction.
454 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
455 /// reserving a whole register (R0), we scrounge for one here. This generates
458 /// mfcr rA ; Move the conditional register into GPR rA.
459 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
460 /// stw rA, FI ; Store rA to the frame.
462 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
463 unsigned FrameIndex, int SPAdj,
464 RegScavenger *RS) const {
465 // Get the instruction.
466 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>, <FI>
467 // Get the instruction's basic block.
468 MachineBasicBlock &MBB = *MI.getParent();
469 DebugLoc dl = MI.getDebugLoc();
471 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
472 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
473 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
474 unsigned Reg = findScratchRegister(II, RS, RC, SPAdj);
475 unsigned SrcReg = MI.getOperand(0).getReg();
476 bool LP64 = Subtarget.isPPC64();
478 // We need to store the CR in the low 4-bits of the saved value. First, issue
479 // an MFCRpsued to save all of the CRBits and, if needed, kill the SrcReg.
480 BuildMI(MBB, II, dl, TII.get(PPC::MFCRpseud), Reg)
481 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
483 // If the saved register wasn't CR0, shift the bits left so that they are in
485 if (SrcReg != PPC::CR0)
486 // rlwinm rA, rA, ShiftBits, 0, 31.
487 BuildMI(MBB, II, dl, TII.get(PPC::RLWINM), Reg)
488 .addReg(Reg, RegState::Kill)
489 .addImm(getPPCRegisterNumbering(SrcReg) * 4)
493 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
494 .addReg(Reg, getKillRegState(MI.getOperand(1).getImm())),
497 // Discard the pseudo instruction.
502 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
503 int SPAdj, RegScavenger *RS) const {
504 assert(SPAdj == 0 && "Unexpected");
506 // Get the instruction.
507 MachineInstr &MI = *II;
508 // Get the instruction's basic block.
509 MachineBasicBlock &MBB = *MI.getParent();
510 // Get the basic block's function.
511 MachineFunction &MF = *MBB.getParent();
512 // Get the frame info.
513 MachineFrameInfo *MFI = MF.getFrameInfo();
514 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
515 DebugLoc dl = MI.getDebugLoc();
517 // Find out which operand is the frame index.
518 unsigned FIOperandNo = 0;
519 while (!MI.getOperand(FIOperandNo).isFI()) {
521 assert(FIOperandNo != MI.getNumOperands() &&
522 "Instr doesn't have FrameIndex operand!");
524 // Take into account whether it's an add or mem instruction
525 unsigned OffsetOperandNo = (FIOperandNo == 2) ? 1 : 2;
526 if (MI.isInlineAsm())
527 OffsetOperandNo = FIOperandNo-1;
529 // Get the frame index.
530 int FrameIndex = MI.getOperand(FIOperandNo).getIndex();
532 // Get the frame pointer save index. Users of this index are primarily
533 // DYNALLOC instructions.
534 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
535 int FPSI = FI->getFramePointerSaveIndex();
536 // Get the instruction opcode.
537 unsigned OpC = MI.getOpcode();
539 // Special case for dynamic alloca.
540 if (FPSI && FrameIndex == FPSI &&
541 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
542 lowerDynamicAlloc(II, SPAdj, RS);
546 // Special case for pseudo-op SPILL_CR.
547 if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Enable by default.
548 if (OpC == PPC::SPILL_CR) {
549 lowerCRSpilling(II, FrameIndex, SPAdj, RS);
553 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
554 MI.getOperand(FIOperandNo).ChangeToRegister(TFI->hasFP(MF) ?
558 // Figure out if the offset in the instruction is shifted right two bits. This
559 // is true for instructions like "STD", which the machine implicitly adds two
561 bool isIXAddr = false;
571 // Now add the frame object offset to the offset from r1.
572 int Offset = MFI->getObjectOffset(FrameIndex);
574 Offset += MI.getOperand(OffsetOperandNo).getImm();
576 Offset += MI.getOperand(OffsetOperandNo).getImm() << 2;
578 // If we're not using a Frame Pointer that has been set to the value of the
579 // SP before having the stack size subtracted from it, then add the stack size
580 // to Offset to get the correct offset.
581 // Naked functions have stack size 0, although getStackSize may not reflect that
582 // because we didn't call all the pieces that compute it for naked functions.
583 if (!MF.getFunction()->hasFnAttr(Attribute::Naked))
584 Offset += MFI->getStackSize();
586 // If we can, encode the offset directly into the instruction. If this is a
587 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
588 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
589 // clear can be encoded. This is extremely uncommon, because normally you
590 // only "std" to a stack slot that is at least 4-byte aligned, but it can
591 // happen in invalid code.
592 if (isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) {
594 Offset >>= 2; // The actual encoded value has the low two bits zero.
595 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
599 // The offset doesn't fit into a single register, scavenge one to build the
601 // FIXME: figure out what SPAdj is doing here.
603 // FIXME (64-bit): Use "findScratchRegister".
605 if (requiresRegisterScavenging(MF))
606 SReg = findScratchRegister(II, RS, &PPC::GPRCRegClass, SPAdj);
610 // Insert a set of rA with the full offset value before the ld, st, or add
611 BuildMI(MBB, II, dl, TII.get(PPC::LIS), SReg)
612 .addImm(Offset >> 16);
613 BuildMI(MBB, II, dl, TII.get(PPC::ORI), SReg)
614 .addReg(SReg, RegState::Kill)
617 // Convert into indexed form of the instruction:
619 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
620 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
621 unsigned OperandBase;
623 if (OpC != TargetOpcode::INLINEASM) {
624 assert(ImmToIdxMap.count(OpC) &&
625 "No indexed form of load or store available!");
626 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
627 MI.setDesc(TII.get(NewOpcode));
630 OperandBase = OffsetOperandNo;
633 unsigned StackReg = MI.getOperand(FIOperandNo).getReg();
634 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
635 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false);
638 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
639 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
641 if (!Subtarget.isPPC64())
642 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
644 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
647 unsigned PPCRegisterInfo::getEHExceptionRegister() const {
648 return !Subtarget.isPPC64() ? PPC::R3 : PPC::X3;
651 unsigned PPCRegisterInfo::getEHHandlerRegister() const {
652 return !Subtarget.isPPC64() ? PPC::R4 : PPC::X4;