1 //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===//
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 #include "PPCRegisterInfo.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"
47 #define DEBUG_TYPE "reginfo"
49 #define GET_REGINFO_TARGET_DESC
50 #include "PPCGenRegisterInfo.inc"
53 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
54 cl::desc("Enable use of a base pointer for complex stack frames"));
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"));
60 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
61 : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
63 TM.isPPC64() ? 0 : 1),
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;
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;
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;
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)
100 // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
101 // when it checks for ZERO folding.
104 return &PPC::G8RC_NOX0RegClass;
105 return &PPC::GPRC_NOR0RegClass;
109 return &PPC::G8RCRegClass;
110 return &PPC::GPRCRegClass;
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;
124 if (Subtarget.isDarwinABI())
126 ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_SaveList
127 : CSR_Darwin64_SaveList)
128 : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_SaveList
129 : CSR_Darwin32_SaveList);
131 if (TM.isPPC64() && MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
132 return CSR_SRV464_TLS_PE_SaveList;
134 // On PPC64, we might need to save r2 (but only if it is not reserved).
135 bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2);
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);
147 PPCRegisterInfo::getCalleeSavedRegsViaCopy(const MachineFunction *MF) const {
148 assert(MF && "Invalid MachineFunction pointer.");
149 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
150 if (Subtarget.isDarwinABI())
154 if (MF->getFunction()->getCallingConv() != CallingConv::CXX_FAST_TLS)
156 if (!MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
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())
163 ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList
164 : CSR_SVR464_Altivec_ViaCopy_SaveList;
167 ? CSR_SVR464_R2_ViaCopy_SaveList
168 : CSR_SVR464_ViaCopy_SaveList;
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;
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);
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);
196 PPCRegisterInfo::getNoPreservedMask() const {
197 return CSR_NoRegs_RegMask;
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));
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);
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);
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);
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);
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);
227 markSuperRegs(Reserved, PPC::R1);
228 markSuperRegs(Reserved, PPC::LR);
229 markSuperRegs(Reserved, PPC::LR8);
230 markSuperRegs(Reserved, PPC::RM);
232 if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec())
233 markSuperRegs(Reserved, PPC::VRSAVE);
235 // The SVR4 ABI reserves r2 and r13
236 if (Subtarget.isSVR4ABI()) {
237 // We only reserve r2 if we need to use the TOC pointer. If we have no
238 // explicit uses of the TOC pointer (meaning we're a leaf function with
239 // no constant-pool loads, etc.) and we have no potential uses inside an
240 // inline asm block, then we can treat r2 has an ordinary callee-saved
242 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
243 if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
244 markSuperRegs(Reserved, PPC::R2); // System-reserved register
245 markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
248 // On PPC64, r13 is the thread pointer. Never allocate this register.
250 markSuperRegs(Reserved, PPC::R13);
252 if (TFI->needsFP(MF))
253 markSuperRegs(Reserved, PPC::R31);
255 bool IsPositionIndependent = TM.isPositionIndependent();
256 if (hasBasePointer(MF)) {
257 if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent)
258 markSuperRegs(Reserved, PPC::R29);
260 markSuperRegs(Reserved, PPC::R30);
263 if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent)
264 markSuperRegs(Reserved, PPC::R30);
266 // Reserve Altivec registers when Altivec is unavailable.
267 if (!Subtarget.hasAltivec())
268 for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
269 IE = PPC::VRRCRegClass.end(); I != IE; ++I)
270 markSuperRegs(Reserved, *I);
272 assert(checkAllSuperRegsMarked(Reserved));
276 bool PPCRegisterInfo::isCallerPreservedPhysReg(unsigned PhysReg,
277 const MachineFunction &MF) const {
278 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
279 if (TM.isELFv2ABI() && PhysReg == PPC::X2) {
280 // X2 is guaranteed to be preserved within a function if it is reserved.
281 // The reason it's reserved is that it's the TOC pointer (and the function
282 // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
283 // with no TOC access), we can't claim that it is preserved.
284 return (getReservedRegs(MF).test(PPC::X2));
290 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
291 MachineFunction &MF) const {
292 const PPCFrameLowering *TFI = getFrameLowering(MF);
293 const unsigned DefaultSafety = 1;
295 switch (RC->getID()) {
298 case PPC::G8RC_NOX0RegClassID:
299 case PPC::GPRC_NOR0RegClassID:
300 case PPC::G8RCRegClassID:
301 case PPC::GPRCRegClassID: {
302 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
303 return 32 - FP - DefaultSafety;
305 case PPC::F8RCRegClassID:
306 case PPC::F4RCRegClassID:
307 case PPC::QFRCRegClassID:
308 case PPC::QSRCRegClassID:
309 case PPC::QBRCRegClassID:
310 case PPC::VRRCRegClassID:
311 case PPC::VFRCRegClassID:
312 case PPC::VSLRCRegClassID:
313 return 32 - DefaultSafety;
314 case PPC::VSRCRegClassID:
315 case PPC::VSFRCRegClassID:
316 case PPC::VSSRCRegClassID:
317 return 64 - DefaultSafety;
318 case PPC::CRRCRegClassID:
319 return 8 - DefaultSafety;
323 const TargetRegisterClass *
324 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
325 const MachineFunction &MF) const {
326 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
327 if (Subtarget.hasVSX()) {
328 // With VSX, we can inflate various sub-register classes to the full VSX
331 if (RC == &PPC::F8RCRegClass)
332 return &PPC::VSFRCRegClass;
333 else if (RC == &PPC::VRRCRegClass)
334 return &PPC::VSRCRegClass;
335 else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector())
336 return &PPC::VSSRCRegClass;
339 return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
342 //===----------------------------------------------------------------------===//
343 // Stack Frame Processing methods
344 //===----------------------------------------------------------------------===//
346 /// lowerDynamicAlloc - Generate the code for allocating an object in the
347 /// current frame. The sequence of code will be in the general form
349 /// addi R0, SP, \#frameSize ; get the address of the previous frame
350 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
351 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
353 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
354 // Get the instruction.
355 MachineInstr &MI = *II;
356 // Get the instruction's basic block.
357 MachineBasicBlock &MBB = *MI.getParent();
358 // Get the basic block's function.
359 MachineFunction &MF = *MBB.getParent();
360 // Get the frame info.
361 MachineFrameInfo &MFI = MF.getFrameInfo();
362 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
363 // Get the instruction info.
364 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
365 // Determine whether 64-bit pointers are used.
366 bool LP64 = TM.isPPC64();
367 DebugLoc dl = MI.getDebugLoc();
369 // Get the maximum call stack size.
370 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
371 // Get the total frame size.
372 unsigned FrameSize = MFI.getStackSize();
374 // Get stack alignments.
375 const PPCFrameLowering *TFI = getFrameLowering(MF);
376 unsigned TargetAlign = TFI->getStackAlignment();
377 unsigned MaxAlign = MFI.getMaxAlignment();
378 assert((maxCallFrameSize & (MaxAlign-1)) == 0 &&
379 "Maximum call-frame size not sufficiently aligned");
381 // Determine the previous frame's address. If FrameSize can't be
382 // represented as 16 bits or we need special alignment, then we load the
383 // previous frame's address from 0(SP). Why not do an addis of the hi?
384 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
385 // Constructing the constant and adding would take 3 instructions.
386 // Fortunately, a frame greater than 32K is rare.
387 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
388 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
389 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
391 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
392 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
396 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
400 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
405 bool KillNegSizeReg = MI.getOperand(1).isKill();
406 unsigned NegSizeReg = MI.getOperand(1).getReg();
408 // Grow the stack and update the stack pointer link, then determine the
409 // address of new allocated space.
411 if (MaxAlign > TargetAlign) {
412 unsigned UnalNegSizeReg = NegSizeReg;
413 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
415 // Unfortunately, there is no andi, only andi., and we can't insert that
416 // here because we might clobber cr0 while it is live.
417 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
418 .addImm(~(MaxAlign-1));
420 unsigned NegSizeReg1 = NegSizeReg;
421 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
422 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
423 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
424 .addReg(NegSizeReg1, RegState::Kill);
425 KillNegSizeReg = true;
428 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
429 .addReg(Reg, RegState::Kill)
431 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
432 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
434 .addImm(maxCallFrameSize);
436 if (MaxAlign > TargetAlign) {
437 unsigned UnalNegSizeReg = NegSizeReg;
438 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
440 // Unfortunately, there is no andi, only andi., and we can't insert that
441 // here because we might clobber cr0 while it is live.
442 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
443 .addImm(~(MaxAlign-1));
445 unsigned NegSizeReg1 = NegSizeReg;
446 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
447 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
448 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
449 .addReg(NegSizeReg1, RegState::Kill);
450 KillNegSizeReg = true;
453 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
454 .addReg(Reg, RegState::Kill)
456 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
457 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
459 .addImm(maxCallFrameSize);
462 // Discard the DYNALLOC instruction.
466 void PPCRegisterInfo::lowerDynamicAreaOffset(
467 MachineBasicBlock::iterator II) const {
468 // Get the instruction.
469 MachineInstr &MI = *II;
470 // Get the instruction's basic block.
471 MachineBasicBlock &MBB = *MI.getParent();
472 // Get the basic block's function.
473 MachineFunction &MF = *MBB.getParent();
474 // Get the frame info.
475 MachineFrameInfo &MFI = MF.getFrameInfo();
476 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
477 // Get the instruction info.
478 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
480 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
481 DebugLoc dl = MI.getDebugLoc();
482 BuildMI(MBB, II, dl, TII.get(PPC::LI), MI.getOperand(0).getReg())
483 .addImm(maxCallFrameSize);
487 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
488 /// reserving a whole register (R0), we scrounge for one here. This generates
491 /// mfcr rA ; Move the conditional register into GPR rA.
492 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
493 /// stw rA, FI ; Store rA to the frame.
495 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
496 unsigned FrameIndex) const {
497 // Get the instruction.
498 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>
499 // Get the instruction's basic block.
500 MachineBasicBlock &MBB = *MI.getParent();
501 MachineFunction &MF = *MBB.getParent();
502 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
503 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
504 DebugLoc dl = MI.getDebugLoc();
506 bool LP64 = TM.isPPC64();
507 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
508 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
510 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
511 unsigned SrcReg = MI.getOperand(0).getReg();
513 // We need to store the CR in the low 4-bits of the saved value. First, issue
514 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
515 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
516 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
518 // If the saved register wasn't CR0, shift the bits left so that they are in
520 if (SrcReg != PPC::CR0) {
522 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
524 // rlwinm rA, rA, ShiftBits, 0, 31.
525 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
526 .addReg(Reg1, RegState::Kill)
527 .addImm(getEncodingValue(SrcReg) * 4)
532 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
533 .addReg(Reg, RegState::Kill),
536 // Discard the pseudo instruction.
540 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
541 unsigned FrameIndex) const {
542 // Get the instruction.
543 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset>
544 // Get the instruction's basic block.
545 MachineBasicBlock &MBB = *MI.getParent();
546 MachineFunction &MF = *MBB.getParent();
547 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
548 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
549 DebugLoc dl = MI.getDebugLoc();
551 bool LP64 = TM.isPPC64();
552 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
553 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
555 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
556 unsigned DestReg = MI.getOperand(0).getReg();
557 assert(MI.definesRegister(DestReg) &&
558 "RESTORE_CR does not define its destination");
560 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
563 // If the reloaded register isn't CR0, shift the bits right so that they are
564 // in the right CR's slot.
565 if (DestReg != PPC::CR0) {
567 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
569 unsigned ShiftBits = getEncodingValue(DestReg)*4;
570 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
571 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
572 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
576 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
577 .addReg(Reg, RegState::Kill);
579 // Discard the pseudo instruction.
583 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
584 unsigned FrameIndex) const {
585 // Get the instruction.
586 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset>
587 // Get the instruction's basic block.
588 MachineBasicBlock &MBB = *MI.getParent();
589 MachineFunction &MF = *MBB.getParent();
590 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
591 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
592 DebugLoc dl = MI.getDebugLoc();
594 bool LP64 = TM.isPPC64();
595 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
596 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
598 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
599 unsigned SrcReg = MI.getOperand(0).getReg();
601 BuildMI(MBB, II, dl, TII.get(TargetOpcode::KILL),
602 getCRFromCRBit(SrcReg))
603 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
605 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
606 .addReg(getCRFromCRBit(SrcReg));
608 // If the saved register wasn't CR0LT, shift the bits left so that the bit to
609 // store is the first one. Mask all but that bit.
611 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
613 // rlwinm rA, rA, ShiftBits, 0, 0.
614 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
615 .addReg(Reg1, RegState::Kill)
616 .addImm(getEncodingValue(SrcReg))
617 .addImm(0).addImm(0);
619 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
620 .addReg(Reg, RegState::Kill),
623 // Discard the pseudo instruction.
627 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
628 unsigned FrameIndex) const {
629 // Get the instruction.
630 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset>
631 // Get the instruction's basic block.
632 MachineBasicBlock &MBB = *MI.getParent();
633 MachineFunction &MF = *MBB.getParent();
634 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
635 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
636 DebugLoc dl = MI.getDebugLoc();
638 bool LP64 = TM.isPPC64();
639 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
640 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
642 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
643 unsigned DestReg = MI.getOperand(0).getReg();
644 assert(MI.definesRegister(DestReg) &&
645 "RESTORE_CRBIT does not define its destination");
647 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
650 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
652 unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
653 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
654 .addReg(getCRFromCRBit(DestReg));
656 unsigned ShiftBits = getEncodingValue(DestReg);
657 // rlwimi r11, r10, 32-ShiftBits, ..., ...
658 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
659 .addReg(RegO, RegState::Kill)
660 .addReg(Reg, RegState::Kill)
661 .addImm(ShiftBits ? 32 - ShiftBits : 0)
665 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
666 getCRFromCRBit(DestReg))
667 .addReg(RegO, RegState::Kill)
668 // Make sure we have a use dependency all the way through this
669 // sequence of instructions. We can't have the other bits in the CR
670 // modified in between the mfocrf and the mtocrf.
671 .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
673 // Discard the pseudo instruction.
677 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II,
678 unsigned FrameIndex) const {
679 // Get the instruction.
680 MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset>
681 // Get the instruction's basic block.
682 MachineBasicBlock &MBB = *MI.getParent();
683 MachineFunction &MF = *MBB.getParent();
684 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
685 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
686 DebugLoc dl = MI.getDebugLoc();
688 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
689 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
690 unsigned SrcReg = MI.getOperand(0).getReg();
692 BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
693 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
696 BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill),
699 // Discard the pseudo instruction.
703 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II,
704 unsigned FrameIndex) const {
705 // Get the instruction.
706 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset>
707 // Get the instruction's basic block.
708 MachineBasicBlock &MBB = *MI.getParent();
709 MachineFunction &MF = *MBB.getParent();
710 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
711 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
712 DebugLoc dl = MI.getDebugLoc();
714 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
715 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
716 unsigned DestReg = MI.getOperand(0).getReg();
717 assert(MI.definesRegister(DestReg) &&
718 "RESTORE_VRSAVE does not define its destination");
720 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
723 BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
724 .addReg(Reg, RegState::Kill);
726 // Discard the pseudo instruction.
730 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
731 unsigned Reg, int &FrameIdx) const {
732 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
733 // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
734 // ABI, return true to prevent allocating an additional frame slot.
735 // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
736 // is arbitrary and will be subsequently ignored. For 32-bit, we have
737 // previously created the stack slot if needed, so return its FrameIdx.
738 if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
742 const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
743 FrameIdx = FI->getCRSpillFrameIndex();
750 // Figure out if the offset in the instruction must be a multiple of 4.
751 // This is true for instructions like "STD".
752 static bool usesIXAddr(const MachineInstr &MI) {
753 unsigned OpC = MI.getOpcode();
766 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
767 static unsigned getOffsetONFromFION(const MachineInstr &MI,
768 unsigned FIOperandNum) {
769 // Take into account whether it's an add or mem instruction
770 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
771 if (MI.isInlineAsm())
772 OffsetOperandNo = FIOperandNum - 1;
773 else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
774 MI.getOpcode() == TargetOpcode::PATCHPOINT)
775 OffsetOperandNo = FIOperandNum + 1;
777 return OffsetOperandNo;
781 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
782 int SPAdj, unsigned FIOperandNum,
783 RegScavenger *RS) const {
784 assert(SPAdj == 0 && "Unexpected");
786 // Get the instruction.
787 MachineInstr &MI = *II;
788 // Get the instruction's basic block.
789 MachineBasicBlock &MBB = *MI.getParent();
790 // Get the basic block's function.
791 MachineFunction &MF = *MBB.getParent();
792 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
793 // Get the instruction info.
794 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
795 // Get the frame info.
796 MachineFrameInfo &MFI = MF.getFrameInfo();
797 DebugLoc dl = MI.getDebugLoc();
799 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
801 // Get the frame index.
802 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
804 // Get the frame pointer save index. Users of this index are primarily
805 // DYNALLOC instructions.
806 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
807 int FPSI = FI->getFramePointerSaveIndex();
808 // Get the instruction opcode.
809 unsigned OpC = MI.getOpcode();
811 if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
812 lowerDynamicAreaOffset(II);
816 // Special case for dynamic alloca.
817 if (FPSI && FrameIndex == FPSI &&
818 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
819 lowerDynamicAlloc(II);
823 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
824 if (OpC == PPC::SPILL_CR) {
825 lowerCRSpilling(II, FrameIndex);
827 } else if (OpC == PPC::RESTORE_CR) {
828 lowerCRRestore(II, FrameIndex);
830 } else if (OpC == PPC::SPILL_CRBIT) {
831 lowerCRBitSpilling(II, FrameIndex);
833 } else if (OpC == PPC::RESTORE_CRBIT) {
834 lowerCRBitRestore(II, FrameIndex);
836 } else if (OpC == PPC::SPILL_VRSAVE) {
837 lowerVRSAVESpilling(II, FrameIndex);
839 } else if (OpC == PPC::RESTORE_VRSAVE) {
840 lowerVRSAVERestore(II, FrameIndex);
844 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
845 MI.getOperand(FIOperandNum).ChangeToRegister(
846 FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
848 // Figure out if the offset in the instruction is shifted right two bits.
849 bool isIXAddr = usesIXAddr(MI);
851 // If the instruction is not present in ImmToIdxMap, then it has no immediate
852 // form (and must be r+r).
853 bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
854 OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
856 // Now add the frame object offset to the offset from r1.
857 int Offset = MFI.getObjectOffset(FrameIndex);
858 Offset += MI.getOperand(OffsetOperandNo).getImm();
860 // If we're not using a Frame Pointer that has been set to the value of the
861 // SP before having the stack size subtracted from it, then add the stack size
862 // to Offset to get the correct offset.
863 // Naked functions have stack size 0, although getStackSize may not reflect
864 // that because we didn't call all the pieces that compute it for naked
866 if (!MF.getFunction()->hasFnAttribute(Attribute::Naked)) {
867 if (!(hasBasePointer(MF) && FrameIndex < 0))
868 Offset += MFI.getStackSize();
871 // If we can, encode the offset directly into the instruction. If this is a
872 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
873 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
874 // clear can be encoded. This is extremely uncommon, because normally you
875 // only "std" to a stack slot that is at least 4-byte aligned, but it can
876 // happen in invalid code.
877 assert(OpC != PPC::DBG_VALUE &&
878 "This should be handled in a target-independent way");
879 if (!noImmForm && ((isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) ||
880 OpC == TargetOpcode::STACKMAP ||
881 OpC == TargetOpcode::PATCHPOINT)) {
882 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
886 // The offset doesn't fit into a single register, scavenge one to build the
889 bool is64Bit = TM.isPPC64();
890 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
891 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
892 const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
893 unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
894 SReg = MF.getRegInfo().createVirtualRegister(RC);
896 // Insert a set of rA with the full offset value before the ld, st, or add
897 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
898 .addImm(Offset >> 16);
899 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
900 .addReg(SRegHi, RegState::Kill)
903 // Convert into indexed form of the instruction:
905 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
906 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
907 unsigned OperandBase;
911 else if (OpC != TargetOpcode::INLINEASM) {
912 assert(ImmToIdxMap.count(OpC) &&
913 "No indexed form of load or store available!");
914 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
915 MI.setDesc(TII.get(NewOpcode));
918 OperandBase = OffsetOperandNo;
921 unsigned StackReg = MI.getOperand(FIOperandNum).getReg();
922 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
923 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
926 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
927 const PPCFrameLowering *TFI = getFrameLowering(MF);
930 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
932 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
935 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
936 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
937 if (!hasBasePointer(MF))
938 return getFrameRegister(MF);
943 if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
949 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
950 if (!EnableBasePointer)
952 if (AlwaysBasePointer)
955 // If we need to realign the stack, then the stack pointer can no longer
956 // serve as an offset into the caller's stack space. As a result, we need a
958 return needsStackRealignment(MF);
961 /// Returns true if the instruction's frame index
962 /// reference would be better served by a base register other than FP
963 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
964 /// references it should create new base registers for.
965 bool PPCRegisterInfo::
966 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
967 assert(Offset < 0 && "Local offset must be negative");
969 // It's the load/store FI references that cause issues, as it can be difficult
970 // to materialize the offset if it won't fit in the literal field. Estimate
971 // based on the size of the local frame and some conservative assumptions
972 // about the rest of the stack frame (note, this is pre-regalloc, so
973 // we don't know everything for certain yet) whether this offset is likely
974 // to be out of range of the immediate. Return true if so.
976 // We only generate virtual base registers for loads and stores that have
977 // an r+i form. Return false for everything else.
978 unsigned OpC = MI->getOpcode();
979 if (!ImmToIdxMap.count(OpC))
982 // Don't generate a new virtual base register just to add zero to it.
983 if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
984 MI->getOperand(2).getImm() == 0)
987 MachineBasicBlock &MBB = *MI->getParent();
988 MachineFunction &MF = *MBB.getParent();
989 const PPCFrameLowering *TFI = getFrameLowering(MF);
990 unsigned StackEst = TFI->determineFrameLayout(MF, false, true);
992 // If we likely don't need a stack frame, then we probably don't need a
993 // virtual base register either.
997 // Estimate an offset from the stack pointer.
998 // The incoming offset is relating to the SP at the start of the function,
999 // but when we access the local it'll be relative to the SP after local
1000 // allocation, so adjust our SP-relative offset by that allocation size.
1003 // The frame pointer will point to the end of the stack, so estimate the
1004 // offset as the difference between the object offset and the FP location.
1005 return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
1008 /// Insert defining instruction(s) for BaseReg to
1009 /// be a pointer to FrameIdx at the beginning of the basic block.
1010 void PPCRegisterInfo::
1011 materializeFrameBaseRegister(MachineBasicBlock *MBB,
1012 unsigned BaseReg, int FrameIdx,
1013 int64_t Offset) const {
1014 unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
1016 MachineBasicBlock::iterator Ins = MBB->begin();
1017 DebugLoc DL; // Defaults to "unknown"
1018 if (Ins != MBB->end())
1019 DL = Ins->getDebugLoc();
1021 const MachineFunction &MF = *MBB->getParent();
1022 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1023 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1024 const MCInstrDesc &MCID = TII.get(ADDriOpc);
1025 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1026 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
1028 BuildMI(*MBB, Ins, DL, MCID, BaseReg)
1029 .addFrameIndex(FrameIdx).addImm(Offset);
1032 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
1033 int64_t Offset) const {
1034 unsigned FIOperandNum = 0;
1035 while (!MI.getOperand(FIOperandNum).isFI()) {
1037 assert(FIOperandNum < MI.getNumOperands() &&
1038 "Instr doesn't have FrameIndex operand!");
1041 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
1042 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1043 Offset += MI.getOperand(OffsetOperandNo).getImm();
1044 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1046 MachineBasicBlock &MBB = *MI.getParent();
1047 MachineFunction &MF = *MBB.getParent();
1048 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1049 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1050 const MCInstrDesc &MCID = MI.getDesc();
1051 MachineRegisterInfo &MRI = MF.getRegInfo();
1052 MRI.constrainRegClass(BaseReg,
1053 TII.getRegClass(MCID, FIOperandNum, this, MF));
1056 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1058 int64_t Offset) const {
1059 unsigned FIOperandNum = 0;
1060 while (!MI->getOperand(FIOperandNum).isFI()) {
1062 assert(FIOperandNum < MI->getNumOperands() &&
1063 "Instr doesn't have FrameIndex operand!");
1066 unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
1067 Offset += MI->getOperand(OffsetOperandNo).getImm();
1069 return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
1070 MI->getOpcode() == TargetOpcode::STACKMAP ||
1071 MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1072 (isInt<16>(Offset) && (!usesIXAddr(*MI) || (Offset & 3) == 0));