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 "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/RegisterScavenging.h"
29 #include "llvm/IR/CallingConv.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/Type.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetFrameLowering.h"
39 #include "llvm/Target/TargetInstrInfo.h"
40 #include "llvm/Target/TargetMachine.h"
41 #include "llvm/Target/TargetOptions.h"
46 #define DEBUG_TYPE "reginfo"
48 #define GET_REGINFO_TARGET_DESC
49 #include "PPCGenRegisterInfo.inc"
52 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
53 cl::desc("Enable use of a base pointer for complex stack frames"));
56 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
57 cl::desc("Force the use of a base pointer in every function"));
59 PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST)
60 : PPCGenRegisterInfo(ST.isPPC64() ? PPC::LR8 : PPC::LR,
62 ST.isPPC64() ? 0 : 1),
64 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
65 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
66 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
67 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
68 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
69 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
70 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
71 ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
72 ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
75 ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
76 ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
77 ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
78 ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
79 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
82 /// getPointerRegClass - Return the register class to use to hold pointers.
83 /// This is used for addressing modes.
84 const TargetRegisterClass *
85 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
87 // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
88 // when it checks for ZERO folding.
90 if (Subtarget.isPPC64())
91 return &PPC::G8RC_NOX0RegClass;
92 return &PPC::GPRC_NOR0RegClass;
95 if (Subtarget.isPPC64())
96 return &PPC::G8RCRegClass;
97 return &PPC::GPRCRegClass;
101 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
102 if (MF->getFunction()->getCallingConv() == CallingConv::AnyReg) {
103 if (Subtarget.hasVSX())
104 return CSR_64_AllRegs_VSX_SaveList;
105 if (Subtarget.hasAltivec())
106 return CSR_64_AllRegs_Altivec_SaveList;
107 return CSR_64_AllRegs_SaveList;
110 if (Subtarget.isDarwinABI())
111 return Subtarget.isPPC64() ? (Subtarget.hasAltivec() ?
112 CSR_Darwin64_Altivec_SaveList :
113 CSR_Darwin64_SaveList) :
114 (Subtarget.hasAltivec() ?
115 CSR_Darwin32_Altivec_SaveList :
116 CSR_Darwin32_SaveList);
118 // On PPC64, we might need to save r2 (but only if it is not reserved).
119 bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2);
121 return Subtarget.isPPC64() ? (Subtarget.hasAltivec() ?
122 (SaveR2 ? CSR_SVR464_R2_Altivec_SaveList :
123 CSR_SVR464_Altivec_SaveList) :
124 (SaveR2 ? CSR_SVR464_R2_SaveList :
125 CSR_SVR464_SaveList)) :
126 (Subtarget.hasAltivec() ?
127 CSR_SVR432_Altivec_SaveList :
128 CSR_SVR432_SaveList);
132 PPCRegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
133 if (CC == CallingConv::AnyReg) {
134 if (Subtarget.hasVSX())
135 return CSR_64_AllRegs_VSX_RegMask;
136 if (Subtarget.hasAltivec())
137 return CSR_64_AllRegs_Altivec_RegMask;
138 return CSR_64_AllRegs_RegMask;
141 if (Subtarget.isDarwinABI())
142 return Subtarget.isPPC64() ? (Subtarget.hasAltivec() ?
143 CSR_Darwin64_Altivec_RegMask :
144 CSR_Darwin64_RegMask) :
145 (Subtarget.hasAltivec() ?
146 CSR_Darwin32_Altivec_RegMask :
147 CSR_Darwin32_RegMask);
149 return Subtarget.isPPC64() ? (Subtarget.hasAltivec() ?
150 CSR_SVR464_Altivec_RegMask :
151 CSR_SVR464_RegMask) :
152 (Subtarget.hasAltivec() ?
153 CSR_SVR432_Altivec_RegMask :
158 PPCRegisterInfo::getNoPreservedMask() const {
159 return CSR_NoRegs_RegMask;
162 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
163 for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
164 Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
167 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
168 BitVector Reserved(getNumRegs());
169 const PPCFrameLowering *PPCFI =
170 static_cast<const PPCFrameLowering *>(Subtarget.getFrameLowering());
172 // The ZERO register is not really a register, but the representation of r0
173 // when used in instructions that treat r0 as the constant 0.
174 Reserved.set(PPC::ZERO);
175 Reserved.set(PPC::ZERO8);
177 // The FP register is also not really a register, but is the representation
178 // of the frame pointer register used by ISD::FRAMEADDR.
179 Reserved.set(PPC::FP);
180 Reserved.set(PPC::FP8);
182 // The BP register is also not really a register, but is the representation
183 // of the base pointer register used by setjmp.
184 Reserved.set(PPC::BP);
185 Reserved.set(PPC::BP8);
187 // The counter registers must be reserved so that counter-based loops can
188 // be correctly formed (and the mtctr instructions are not DCE'd).
189 Reserved.set(PPC::CTR);
190 Reserved.set(PPC::CTR8);
192 Reserved.set(PPC::R1);
193 Reserved.set(PPC::LR);
194 Reserved.set(PPC::LR8);
195 Reserved.set(PPC::RM);
197 if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec())
198 Reserved.set(PPC::VRSAVE);
200 // The SVR4 ABI reserves r2 and r13
201 if (Subtarget.isSVR4ABI()) {
202 Reserved.set(PPC::R2); // System-reserved register
203 Reserved.set(PPC::R13); // Small Data Area pointer register
206 // On PPC64, r13 is the thread pointer. Never allocate this register.
207 if (Subtarget.isPPC64()) {
208 Reserved.set(PPC::R13);
210 Reserved.set(PPC::X1);
211 Reserved.set(PPC::X13);
213 if (PPCFI->needsFP(MF))
214 Reserved.set(PPC::X31);
216 if (hasBasePointer(MF))
217 Reserved.set(PPC::X30);
219 // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
220 if (Subtarget.isSVR4ABI()) {
221 // We only reserve r2 if we need to use the TOC pointer. If we have no
222 // explicit uses of the TOC pointer (meaning we're a leaf function with
223 // no constant-pool loads, etc.) and we have no potential uses inside an
224 // inline asm block, then we can treat r2 has an ordinary callee-saved
226 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
227 if (FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
228 Reserved.set(PPC::X2);
230 Reserved.reset(PPC::R2);
234 if (PPCFI->needsFP(MF))
235 Reserved.set(PPC::R31);
237 if (hasBasePointer(MF)) {
238 if (Subtarget.isSVR4ABI() && !Subtarget.isPPC64() &&
239 MF.getTarget().getRelocationModel() == Reloc::PIC_)
240 Reserved.set(PPC::R29);
242 Reserved.set(PPC::R30);
245 if (Subtarget.isSVR4ABI() && !Subtarget.isPPC64() &&
246 MF.getTarget().getRelocationModel() == Reloc::PIC_)
247 Reserved.set(PPC::R30);
249 // Reserve Altivec registers when Altivec is unavailable.
250 if (!Subtarget.hasAltivec())
251 for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
252 IE = PPC::VRRCRegClass.end(); I != IE; ++I)
258 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
259 MachineFunction &MF) const {
260 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
261 const unsigned DefaultSafety = 1;
263 switch (RC->getID()) {
266 case PPC::G8RC_NOX0RegClassID:
267 case PPC::GPRC_NOR0RegClassID:
268 case PPC::G8RCRegClassID:
269 case PPC::GPRCRegClassID: {
270 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
271 return 32 - FP - DefaultSafety;
273 case PPC::F8RCRegClassID:
274 case PPC::F4RCRegClassID:
275 case PPC::QFRCRegClassID:
276 case PPC::QSRCRegClassID:
277 case PPC::QBRCRegClassID:
278 case PPC::VRRCRegClassID:
279 case PPC::VFRCRegClassID:
280 case PPC::VSLRCRegClassID:
281 case PPC::VSHRCRegClassID:
282 return 32 - DefaultSafety;
283 case PPC::VSRCRegClassID:
284 case PPC::VSFRCRegClassID:
285 return 64 - DefaultSafety;
286 case PPC::CRRCRegClassID:
287 return 8 - DefaultSafety;
291 const TargetRegisterClass *PPCRegisterInfo::getLargestLegalSuperClass(
292 const TargetRegisterClass *RC) const {
293 if (Subtarget.hasVSX()) {
294 // With VSX, we can inflate various sub-register classes to the full VSX
297 if (RC == &PPC::F8RCRegClass)
298 return &PPC::VSFRCRegClass;
299 else if (RC == &PPC::VRRCRegClass)
300 return &PPC::VSRCRegClass;
303 return TargetRegisterInfo::getLargestLegalSuperClass(RC);
306 //===----------------------------------------------------------------------===//
307 // Stack Frame Processing methods
308 //===----------------------------------------------------------------------===//
310 /// lowerDynamicAlloc - Generate the code for allocating an object in the
311 /// current frame. The sequence of code with be in the general form
313 /// addi R0, SP, \#frameSize ; get the address of the previous frame
314 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
315 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
317 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
318 // Get the instruction.
319 MachineInstr &MI = *II;
320 // Get the instruction's basic block.
321 MachineBasicBlock &MBB = *MI.getParent();
322 // Get the basic block's function.
323 MachineFunction &MF = *MBB.getParent();
324 // Get the frame info.
325 MachineFrameInfo *MFI = MF.getFrameInfo();
326 // Get the instruction info.
327 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
328 // Determine whether 64-bit pointers are used.
329 bool LP64 = Subtarget.isPPC64();
330 DebugLoc dl = MI.getDebugLoc();
332 // Get the maximum call stack size.
333 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
334 // Get the total frame size.
335 unsigned FrameSize = MFI->getStackSize();
337 // Get stack alignments.
338 unsigned TargetAlign = Subtarget.getFrameLowering()->getStackAlignment();
339 unsigned MaxAlign = MFI->getMaxAlignment();
340 assert((maxCallFrameSize & (MaxAlign-1)) == 0 &&
341 "Maximum call-frame size not sufficiently aligned");
343 // Determine the previous frame's address. If FrameSize can't be
344 // represented as 16 bits or we need special alignment, then we load the
345 // previous frame's address from 0(SP). Why not do an addis of the hi?
346 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
347 // Constructing the constant and adding would take 3 instructions.
348 // Fortunately, a frame greater than 32K is rare.
349 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
350 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
351 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
353 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
354 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
358 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
362 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
367 bool KillNegSizeReg = MI.getOperand(1).isKill();
368 unsigned NegSizeReg = MI.getOperand(1).getReg();
370 // Grow the stack and update the stack pointer link, then determine the
371 // address of new allocated space.
373 if (MaxAlign > TargetAlign) {
374 unsigned UnalNegSizeReg = NegSizeReg;
375 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
377 // Unfortunately, there is no andi, only andi., and we can't insert that
378 // here because we might clobber cr0 while it is live.
379 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
380 .addImm(~(MaxAlign-1));
382 unsigned NegSizeReg1 = NegSizeReg;
383 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
384 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
385 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
386 .addReg(NegSizeReg1, RegState::Kill);
387 KillNegSizeReg = true;
390 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
391 .addReg(Reg, RegState::Kill)
393 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
394 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
396 .addImm(maxCallFrameSize);
398 if (MaxAlign > TargetAlign) {
399 unsigned UnalNegSizeReg = NegSizeReg;
400 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
402 // Unfortunately, there is no andi, only andi., and we can't insert that
403 // here because we might clobber cr0 while it is live.
404 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
405 .addImm(~(MaxAlign-1));
407 unsigned NegSizeReg1 = NegSizeReg;
408 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
409 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
410 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
411 .addReg(NegSizeReg1, RegState::Kill);
412 KillNegSizeReg = true;
415 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
416 .addReg(Reg, RegState::Kill)
418 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
419 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
421 .addImm(maxCallFrameSize);
424 // Discard the DYNALLOC instruction.
428 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
429 /// reserving a whole register (R0), we scrounge for one here. This generates
432 /// mfcr rA ; Move the conditional register into GPR rA.
433 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
434 /// stw rA, FI ; Store rA to the frame.
436 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
437 unsigned FrameIndex) const {
438 // Get the instruction.
439 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>
440 // Get the instruction's basic block.
441 MachineBasicBlock &MBB = *MI.getParent();
442 MachineFunction &MF = *MBB.getParent();
443 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
444 DebugLoc dl = MI.getDebugLoc();
446 bool LP64 = Subtarget.isPPC64();
447 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
448 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
450 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
451 unsigned SrcReg = MI.getOperand(0).getReg();
453 // We need to store the CR in the low 4-bits of the saved value. First, issue
454 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
455 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
456 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
458 // If the saved register wasn't CR0, shift the bits left so that they are in
460 if (SrcReg != PPC::CR0) {
462 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
464 // rlwinm rA, rA, ShiftBits, 0, 31.
465 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
466 .addReg(Reg1, RegState::Kill)
467 .addImm(getEncodingValue(SrcReg) * 4)
472 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
473 .addReg(Reg, RegState::Kill),
476 // Discard the pseudo instruction.
480 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
481 unsigned FrameIndex) const {
482 // Get the instruction.
483 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset>
484 // Get the instruction's basic block.
485 MachineBasicBlock &MBB = *MI.getParent();
486 MachineFunction &MF = *MBB.getParent();
487 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
488 DebugLoc dl = MI.getDebugLoc();
490 bool LP64 = Subtarget.isPPC64();
491 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
492 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
494 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
495 unsigned DestReg = MI.getOperand(0).getReg();
496 assert(MI.definesRegister(DestReg) &&
497 "RESTORE_CR does not define its destination");
499 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
502 // If the reloaded register isn't CR0, shift the bits right so that they are
503 // in the right CR's slot.
504 if (DestReg != PPC::CR0) {
506 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
508 unsigned ShiftBits = getEncodingValue(DestReg)*4;
509 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
510 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
511 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
515 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
516 .addReg(Reg, RegState::Kill);
518 // Discard the pseudo instruction.
522 static unsigned getCRFromCRBit(unsigned SrcReg) {
524 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
525 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
527 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
528 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
530 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
531 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
533 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
534 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
536 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
537 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
539 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
540 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
542 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
543 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
545 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
546 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
549 assert(Reg != 0 && "Invalid CR bit register");
553 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
554 unsigned FrameIndex) const {
555 // Get the instruction.
556 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset>
557 // Get the instruction's basic block.
558 MachineBasicBlock &MBB = *MI.getParent();
559 MachineFunction &MF = *MBB.getParent();
560 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
561 DebugLoc dl = MI.getDebugLoc();
563 bool LP64 = Subtarget.isPPC64();
564 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
565 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
567 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
568 unsigned SrcReg = MI.getOperand(0).getReg();
570 BuildMI(MBB, II, dl, TII.get(TargetOpcode::KILL),
571 getCRFromCRBit(SrcReg))
572 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
574 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
575 .addReg(getCRFromCRBit(SrcReg));
577 // If the saved register wasn't CR0LT, shift the bits left so that the bit to
578 // store is the first one. Mask all but that bit.
580 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
582 // rlwinm rA, rA, ShiftBits, 0, 0.
583 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
584 .addReg(Reg1, RegState::Kill)
585 .addImm(getEncodingValue(SrcReg))
586 .addImm(0).addImm(0);
588 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
589 .addReg(Reg, RegState::Kill),
592 // Discard the pseudo instruction.
596 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
597 unsigned FrameIndex) const {
598 // Get the instruction.
599 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset>
600 // Get the instruction's basic block.
601 MachineBasicBlock &MBB = *MI.getParent();
602 MachineFunction &MF = *MBB.getParent();
603 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
604 DebugLoc dl = MI.getDebugLoc();
606 bool LP64 = Subtarget.isPPC64();
607 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
608 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
610 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
611 unsigned DestReg = MI.getOperand(0).getReg();
612 assert(MI.definesRegister(DestReg) &&
613 "RESTORE_CRBIT does not define its destination");
615 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
618 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
620 unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
621 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
622 .addReg(getCRFromCRBit(DestReg));
624 unsigned ShiftBits = getEncodingValue(DestReg);
625 // rlwimi r11, r10, 32-ShiftBits, ..., ...
626 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
627 .addReg(RegO, RegState::Kill).addReg(Reg, RegState::Kill)
628 .addImm(ShiftBits ? 32-ShiftBits : 0)
629 .addImm(ShiftBits).addImm(ShiftBits);
631 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
632 getCRFromCRBit(DestReg))
633 .addReg(RegO, RegState::Kill)
634 // Make sure we have a use dependency all the way through this
635 // sequence of instructions. We can't have the other bits in the CR
636 // modified in between the mfocrf and the mtocrf.
637 .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
639 // Discard the pseudo instruction.
643 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II,
644 unsigned FrameIndex) const {
645 // Get the instruction.
646 MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset>
647 // Get the instruction's basic block.
648 MachineBasicBlock &MBB = *MI.getParent();
649 MachineFunction &MF = *MBB.getParent();
650 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
651 DebugLoc dl = MI.getDebugLoc();
653 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
654 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
655 unsigned SrcReg = MI.getOperand(0).getReg();
657 BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
658 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
660 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::STW))
661 .addReg(Reg, RegState::Kill),
664 // Discard the pseudo instruction.
668 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II,
669 unsigned FrameIndex) const {
670 // Get the instruction.
671 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset>
672 // Get the instruction's basic block.
673 MachineBasicBlock &MBB = *MI.getParent();
674 MachineFunction &MF = *MBB.getParent();
675 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
676 DebugLoc dl = MI.getDebugLoc();
678 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
679 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
680 unsigned DestReg = MI.getOperand(0).getReg();
681 assert(MI.definesRegister(DestReg) &&
682 "RESTORE_VRSAVE does not define its destination");
684 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
687 BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
688 .addReg(Reg, RegState::Kill);
690 // Discard the pseudo instruction.
695 PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
696 unsigned Reg, int &FrameIdx) const {
698 // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
699 // ABI, return true to prevent allocating an additional frame slot.
700 // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
701 // is arbitrary and will be subsequently ignored. For 32-bit, we have
702 // previously created the stack slot if needed, so return its FrameIdx.
703 if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
704 if (Subtarget.isPPC64())
707 const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
708 FrameIdx = FI->getCRSpillFrameIndex();
715 // Figure out if the offset in the instruction must be a multiple of 4.
716 // This is true for instructions like "STD".
717 static bool usesIXAddr(const MachineInstr &MI) {
718 unsigned OpC = MI.getOpcode();
731 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
732 static unsigned getOffsetONFromFION(const MachineInstr &MI,
733 unsigned FIOperandNum) {
734 // Take into account whether it's an add or mem instruction
735 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
736 if (MI.isInlineAsm())
737 OffsetOperandNo = FIOperandNum - 1;
738 else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
739 MI.getOpcode() == TargetOpcode::PATCHPOINT)
740 OffsetOperandNo = FIOperandNum + 1;
742 return OffsetOperandNo;
746 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
747 int SPAdj, unsigned FIOperandNum,
748 RegScavenger *RS) const {
749 assert(SPAdj == 0 && "Unexpected");
751 // Get the instruction.
752 MachineInstr &MI = *II;
753 // Get the instruction's basic block.
754 MachineBasicBlock &MBB = *MI.getParent();
755 // Get the basic block's function.
756 MachineFunction &MF = *MBB.getParent();
757 // Get the instruction info.
758 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
759 // Get the frame info.
760 MachineFrameInfo *MFI = MF.getFrameInfo();
761 DebugLoc dl = MI.getDebugLoc();
763 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
765 // Get the frame index.
766 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
768 // Get the frame pointer save index. Users of this index are primarily
769 // DYNALLOC instructions.
770 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
771 int FPSI = FI->getFramePointerSaveIndex();
772 // Get the instruction opcode.
773 unsigned OpC = MI.getOpcode();
775 // Special case for dynamic alloca.
776 if (FPSI && FrameIndex == FPSI &&
777 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
778 lowerDynamicAlloc(II);
782 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
783 if (OpC == PPC::SPILL_CR) {
784 lowerCRSpilling(II, FrameIndex);
786 } else if (OpC == PPC::RESTORE_CR) {
787 lowerCRRestore(II, FrameIndex);
789 } else if (OpC == PPC::SPILL_CRBIT) {
790 lowerCRBitSpilling(II, FrameIndex);
792 } else if (OpC == PPC::RESTORE_CRBIT) {
793 lowerCRBitRestore(II, FrameIndex);
795 } else if (OpC == PPC::SPILL_VRSAVE) {
796 lowerVRSAVESpilling(II, FrameIndex);
798 } else if (OpC == PPC::RESTORE_VRSAVE) {
799 lowerVRSAVERestore(II, FrameIndex);
803 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
804 MI.getOperand(FIOperandNum).ChangeToRegister(
805 FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
807 // Figure out if the offset in the instruction is shifted right two bits.
808 bool isIXAddr = usesIXAddr(MI);
810 // If the instruction is not present in ImmToIdxMap, then it has no immediate
811 // form (and must be r+r).
812 bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
813 OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
815 // Now add the frame object offset to the offset from r1.
816 int Offset = MFI->getObjectOffset(FrameIndex);
817 Offset += MI.getOperand(OffsetOperandNo).getImm();
819 // If we're not using a Frame Pointer that has been set to the value of the
820 // SP before having the stack size subtracted from it, then add the stack size
821 // to Offset to get the correct offset.
822 // Naked functions have stack size 0, although getStackSize may not reflect that
823 // because we didn't call all the pieces that compute it for naked functions.
824 if (!MF.getFunction()->hasFnAttribute(Attribute::Naked)) {
825 if (!(hasBasePointer(MF) && FrameIndex < 0))
826 Offset += MFI->getStackSize();
829 // If we can, encode the offset directly into the instruction. If this is a
830 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
831 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
832 // clear can be encoded. This is extremely uncommon, because normally you
833 // only "std" to a stack slot that is at least 4-byte aligned, but it can
834 // happen in invalid code.
835 assert(OpC != PPC::DBG_VALUE &&
836 "This should be handled in a target-independent way");
837 if (!noImmForm && ((isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) ||
838 OpC == TargetOpcode::STACKMAP ||
839 OpC == TargetOpcode::PATCHPOINT)) {
840 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
844 // The offset doesn't fit into a single register, scavenge one to build the
847 bool is64Bit = Subtarget.isPPC64();
848 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
849 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
850 const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
851 unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
852 SReg = MF.getRegInfo().createVirtualRegister(RC);
854 // Insert a set of rA with the full offset value before the ld, st, or add
855 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
856 .addImm(Offset >> 16);
857 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
858 .addReg(SRegHi, RegState::Kill)
861 // Convert into indexed form of the instruction:
863 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
864 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
865 unsigned OperandBase;
869 else if (OpC != TargetOpcode::INLINEASM) {
870 assert(ImmToIdxMap.count(OpC) &&
871 "No indexed form of load or store available!");
872 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
873 MI.setDesc(TII.get(NewOpcode));
876 OperandBase = OffsetOperandNo;
879 unsigned StackReg = MI.getOperand(FIOperandNum).getReg();
880 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
881 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
884 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
885 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
887 if (!Subtarget.isPPC64())
888 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
890 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
893 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
894 if (!hasBasePointer(MF))
895 return getFrameRegister(MF);
897 if (Subtarget.isPPC64())
900 if (Subtarget.isSVR4ABI() &&
901 MF.getTarget().getRelocationModel() == Reloc::PIC_)
907 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
908 if (!EnableBasePointer)
910 if (AlwaysBasePointer)
913 // If we need to realign the stack, then the stack pointer can no longer
914 // serve as an offset into the caller's stack space. As a result, we need a
916 return needsStackRealignment(MF);
919 bool PPCRegisterInfo::canRealignStack(const MachineFunction &MF) const {
920 if (MF.getFunction()->hasFnAttribute("no-realign-stack"))
926 bool PPCRegisterInfo::needsStackRealignment(const MachineFunction &MF) const {
927 const MachineFrameInfo *MFI = MF.getFrameInfo();
928 const Function *F = MF.getFunction();
929 unsigned StackAlign = Subtarget.getFrameLowering()->getStackAlignment();
930 bool requiresRealignment = ((MFI->getMaxAlignment() > StackAlign) ||
931 F->hasFnAttribute(Attribute::StackAlignment));
933 return requiresRealignment && canRealignStack(MF);
936 /// Returns true if the instruction's frame index
937 /// reference would be better served by a base register other than FP
938 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
939 /// references it should create new base registers for.
940 bool PPCRegisterInfo::
941 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
942 assert(Offset < 0 && "Local offset must be negative");
944 // It's the load/store FI references that cause issues, as it can be difficult
945 // to materialize the offset if it won't fit in the literal field. Estimate
946 // based on the size of the local frame and some conservative assumptions
947 // about the rest of the stack frame (note, this is pre-regalloc, so
948 // we don't know everything for certain yet) whether this offset is likely
949 // to be out of range of the immediate. Return true if so.
951 // We only generate virtual base registers for loads and stores that have
952 // an r+i form. Return false for everything else.
953 unsigned OpC = MI->getOpcode();
954 if (!ImmToIdxMap.count(OpC))
957 // Don't generate a new virtual base register just to add zero to it.
958 if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
959 MI->getOperand(2).getImm() == 0)
962 MachineBasicBlock &MBB = *MI->getParent();
963 MachineFunction &MF = *MBB.getParent();
965 const PPCFrameLowering *PPCFI =
966 static_cast<const PPCFrameLowering *>(Subtarget.getFrameLowering());
968 PPCFI->determineFrameLayout(MF, false, true);
970 // If we likely don't need a stack frame, then we probably don't need a
971 // virtual base register either.
975 // Estimate an offset from the stack pointer.
976 // The incoming offset is relating to the SP at the start of the function,
977 // but when we access the local it'll be relative to the SP after local
978 // allocation, so adjust our SP-relative offset by that allocation size.
981 // The frame pointer will point to the end of the stack, so estimate the
982 // offset as the difference between the object offset and the FP location.
983 return !isFrameOffsetLegal(MI, Offset);
986 /// Insert defining instruction(s) for BaseReg to
987 /// be a pointer to FrameIdx at the beginning of the basic block.
988 void PPCRegisterInfo::
989 materializeFrameBaseRegister(MachineBasicBlock *MBB,
990 unsigned BaseReg, int FrameIdx,
991 int64_t Offset) const {
992 unsigned ADDriOpc = Subtarget.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
994 MachineBasicBlock::iterator Ins = MBB->begin();
995 DebugLoc DL; // Defaults to "unknown"
996 if (Ins != MBB->end())
997 DL = Ins->getDebugLoc();
999 const MachineFunction &MF = *MBB->getParent();
1000 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1001 const MCInstrDesc &MCID = TII.get(ADDriOpc);
1002 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1003 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
1005 BuildMI(*MBB, Ins, DL, MCID, BaseReg)
1006 .addFrameIndex(FrameIdx).addImm(Offset);
1009 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
1010 int64_t Offset) const {
1011 unsigned FIOperandNum = 0;
1012 while (!MI.getOperand(FIOperandNum).isFI()) {
1014 assert(FIOperandNum < MI.getNumOperands() &&
1015 "Instr doesn't have FrameIndex operand!");
1018 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
1019 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1020 Offset += MI.getOperand(OffsetOperandNo).getImm();
1021 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1023 MachineBasicBlock &MBB = *MI.getParent();
1024 MachineFunction &MF = *MBB.getParent();
1025 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1026 const MCInstrDesc &MCID = MI.getDesc();
1027 MachineRegisterInfo &MRI = MF.getRegInfo();
1028 MRI.constrainRegClass(BaseReg,
1029 TII.getRegClass(MCID, FIOperandNum, this, MF));
1032 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
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 unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
1042 Offset += MI->getOperand(OffsetOperandNo).getImm();
1044 return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
1045 MI->getOpcode() == TargetOpcode::STACKMAP ||
1046 MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1047 (isInt<16>(Offset) && (!usesIXAddr(*MI) || (Offset & 3) == 0));