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 return Subtarget.isPPC64() ? (Subtarget.hasAltivec() ?
119 CSR_SVR464_Altivec_SaveList :
120 CSR_SVR464_SaveList) :
121 (Subtarget.hasAltivec() ?
122 CSR_SVR432_Altivec_SaveList :
123 CSR_SVR432_SaveList);
127 PPCRegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
128 if (CC == CallingConv::AnyReg) {
129 if (Subtarget.hasVSX())
130 return CSR_64_AllRegs_VSX_RegMask;
131 if (Subtarget.hasAltivec())
132 return CSR_64_AllRegs_Altivec_RegMask;
133 return CSR_64_AllRegs_RegMask;
136 if (Subtarget.isDarwinABI())
137 return Subtarget.isPPC64() ? (Subtarget.hasAltivec() ?
138 CSR_Darwin64_Altivec_RegMask :
139 CSR_Darwin64_RegMask) :
140 (Subtarget.hasAltivec() ?
141 CSR_Darwin32_Altivec_RegMask :
142 CSR_Darwin32_RegMask);
144 return Subtarget.isPPC64() ? (Subtarget.hasAltivec() ?
145 CSR_SVR464_Altivec_RegMask :
146 CSR_SVR464_RegMask) :
147 (Subtarget.hasAltivec() ?
148 CSR_SVR432_Altivec_RegMask :
153 PPCRegisterInfo::getNoPreservedMask() const {
154 return CSR_NoRegs_RegMask;
157 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
158 unsigned PseudoRegs[] = { PPC::ZERO, PPC::ZERO8, PPC::RM };
159 for (unsigned i = 0, ie = array_lengthof(PseudoRegs); i != ie; ++i) {
160 unsigned Reg = PseudoRegs[i];
161 Mask[Reg / 32] &= ~(1u << (Reg % 32));
165 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
166 BitVector Reserved(getNumRegs());
167 const PPCFrameLowering *PPCFI =
168 static_cast<const PPCFrameLowering *>(Subtarget.getFrameLowering());
170 // The ZERO register is not really a register, but the representation of r0
171 // when used in instructions that treat r0 as the constant 0.
172 Reserved.set(PPC::ZERO);
173 Reserved.set(PPC::ZERO8);
175 // The FP register is also not really a register, but is the representation
176 // of the frame pointer register used by ISD::FRAMEADDR.
177 Reserved.set(PPC::FP);
178 Reserved.set(PPC::FP8);
180 // The BP register is also not really a register, but is the representation
181 // of the base pointer register used by setjmp.
182 Reserved.set(PPC::BP);
183 Reserved.set(PPC::BP8);
185 // The counter registers must be reserved so that counter-based loops can
186 // be correctly formed (and the mtctr instructions are not DCE'd).
187 Reserved.set(PPC::CTR);
188 Reserved.set(PPC::CTR8);
190 Reserved.set(PPC::R1);
191 Reserved.set(PPC::LR);
192 Reserved.set(PPC::LR8);
193 Reserved.set(PPC::RM);
195 if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec())
196 Reserved.set(PPC::VRSAVE);
198 // The SVR4 ABI reserves r2 and r13
199 if (Subtarget.isSVR4ABI()) {
200 Reserved.set(PPC::R2); // System-reserved register
201 Reserved.set(PPC::R13); // Small Data Area pointer register
204 // On PPC64, r13 is the thread pointer. Never allocate this register.
205 if (Subtarget.isPPC64()) {
206 Reserved.set(PPC::R13);
208 Reserved.set(PPC::X1);
209 Reserved.set(PPC::X13);
211 if (PPCFI->needsFP(MF))
212 Reserved.set(PPC::X31);
214 if (hasBasePointer(MF))
215 Reserved.set(PPC::X30);
217 // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
218 if (Subtarget.isSVR4ABI()) {
219 Reserved.set(PPC::X2);
223 if (PPCFI->needsFP(MF))
224 Reserved.set(PPC::R31);
226 if (hasBasePointer(MF)) {
227 if (Subtarget.isSVR4ABI() && !Subtarget.isPPC64() &&
228 MF.getTarget().getRelocationModel() == Reloc::PIC_)
229 Reserved.set(PPC::R29);
231 Reserved.set(PPC::R30);
234 if (Subtarget.isSVR4ABI() && !Subtarget.isPPC64() &&
235 MF.getTarget().getRelocationModel() == Reloc::PIC_)
236 Reserved.set(PPC::R30);
238 // Reserve Altivec registers when Altivec is unavailable.
239 if (!Subtarget.hasAltivec())
240 for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
241 IE = PPC::VRRCRegClass.end(); I != IE; ++I)
247 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
248 MachineFunction &MF) const {
249 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
250 const unsigned DefaultSafety = 1;
252 switch (RC->getID()) {
255 case PPC::G8RC_NOX0RegClassID:
256 case PPC::GPRC_NOR0RegClassID:
257 case PPC::G8RCRegClassID:
258 case PPC::GPRCRegClassID: {
259 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
260 return 32 - FP - DefaultSafety;
262 case PPC::F8RCRegClassID:
263 case PPC::F4RCRegClassID:
264 case PPC::VRRCRegClassID:
265 case PPC::VFRCRegClassID:
266 case PPC::VSLRCRegClassID:
267 case PPC::VSHRCRegClassID:
268 return 32 - DefaultSafety;
269 case PPC::VSRCRegClassID:
270 case PPC::VSFRCRegClassID:
271 return 64 - DefaultSafety;
272 case PPC::CRRCRegClassID:
273 return 8 - DefaultSafety;
277 const TargetRegisterClass *PPCRegisterInfo::getLargestLegalSuperClass(
278 const TargetRegisterClass *RC) const {
279 if (Subtarget.hasVSX()) {
280 // With VSX, we can inflate various sub-register classes to the full VSX
283 if (RC == &PPC::F8RCRegClass)
284 return &PPC::VSFRCRegClass;
285 else if (RC == &PPC::VRRCRegClass)
286 return &PPC::VSRCRegClass;
289 return TargetRegisterInfo::getLargestLegalSuperClass(RC);
292 //===----------------------------------------------------------------------===//
293 // Stack Frame Processing methods
294 //===----------------------------------------------------------------------===//
296 /// lowerDynamicAlloc - Generate the code for allocating an object in the
297 /// current frame. The sequence of code with be in the general form
299 /// addi R0, SP, \#frameSize ; get the address of the previous frame
300 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
301 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
303 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
304 // Get the instruction.
305 MachineInstr &MI = *II;
306 // Get the instruction's basic block.
307 MachineBasicBlock &MBB = *MI.getParent();
308 // Get the basic block's function.
309 MachineFunction &MF = *MBB.getParent();
310 // Get the frame info.
311 MachineFrameInfo *MFI = MF.getFrameInfo();
312 // Get the instruction info.
313 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
314 // Determine whether 64-bit pointers are used.
315 bool LP64 = Subtarget.isPPC64();
316 DebugLoc dl = MI.getDebugLoc();
318 // Get the maximum call stack size.
319 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
320 // Get the total frame size.
321 unsigned FrameSize = MFI->getStackSize();
323 // Get stack alignments.
324 unsigned TargetAlign = Subtarget.getFrameLowering()->getStackAlignment();
325 unsigned MaxAlign = MFI->getMaxAlignment();
326 assert((maxCallFrameSize & (MaxAlign-1)) == 0 &&
327 "Maximum call-frame size not sufficiently aligned");
329 // Determine the previous frame's address. If FrameSize can't be
330 // represented as 16 bits or we need special alignment, then we load the
331 // previous frame's address from 0(SP). Why not do an addis of the hi?
332 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
333 // Constructing the constant and adding would take 3 instructions.
334 // Fortunately, a frame greater than 32K is rare.
335 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
336 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
337 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
339 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
340 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
344 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
348 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
353 bool KillNegSizeReg = MI.getOperand(1).isKill();
354 unsigned NegSizeReg = MI.getOperand(1).getReg();
356 // Grow the stack and update the stack pointer link, then determine the
357 // address of new allocated space.
359 if (MaxAlign > TargetAlign) {
360 unsigned UnalNegSizeReg = NegSizeReg;
361 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
363 // Unfortunately, there is no andi, only andi., and we can't insert that
364 // here because we might clobber cr0 while it is live.
365 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
366 .addImm(~(MaxAlign-1));
368 unsigned NegSizeReg1 = NegSizeReg;
369 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
370 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
371 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
372 .addReg(NegSizeReg1, RegState::Kill);
373 KillNegSizeReg = true;
376 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
377 .addReg(Reg, RegState::Kill)
379 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
380 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
382 .addImm(maxCallFrameSize);
384 if (MaxAlign > TargetAlign) {
385 unsigned UnalNegSizeReg = NegSizeReg;
386 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
388 // Unfortunately, there is no andi, only andi., and we can't insert that
389 // here because we might clobber cr0 while it is live.
390 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
391 .addImm(~(MaxAlign-1));
393 unsigned NegSizeReg1 = NegSizeReg;
394 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
395 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
396 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
397 .addReg(NegSizeReg1, RegState::Kill);
398 KillNegSizeReg = true;
401 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
402 .addReg(Reg, RegState::Kill)
404 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
405 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
407 .addImm(maxCallFrameSize);
410 // Discard the DYNALLOC instruction.
414 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
415 /// reserving a whole register (R0), we scrounge for one here. This generates
418 /// mfcr rA ; Move the conditional register into GPR rA.
419 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
420 /// stw rA, FI ; Store rA to the frame.
422 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
423 unsigned FrameIndex) const {
424 // Get the instruction.
425 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>
426 // Get the instruction's basic block.
427 MachineBasicBlock &MBB = *MI.getParent();
428 MachineFunction &MF = *MBB.getParent();
429 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
430 DebugLoc dl = MI.getDebugLoc();
432 bool LP64 = Subtarget.isPPC64();
433 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
434 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
436 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
437 unsigned SrcReg = MI.getOperand(0).getReg();
439 // We need to store the CR in the low 4-bits of the saved value. First, issue
440 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
441 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
442 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
444 // If the saved register wasn't CR0, shift the bits left so that they are in
446 if (SrcReg != PPC::CR0) {
448 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
450 // rlwinm rA, rA, ShiftBits, 0, 31.
451 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
452 .addReg(Reg1, RegState::Kill)
453 .addImm(getEncodingValue(SrcReg) * 4)
458 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
459 .addReg(Reg, RegState::Kill),
462 // Discard the pseudo instruction.
466 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
467 unsigned FrameIndex) const {
468 // Get the instruction.
469 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset>
470 // Get the instruction's basic block.
471 MachineBasicBlock &MBB = *MI.getParent();
472 MachineFunction &MF = *MBB.getParent();
473 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
474 DebugLoc dl = MI.getDebugLoc();
476 bool LP64 = Subtarget.isPPC64();
477 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
478 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
480 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
481 unsigned DestReg = MI.getOperand(0).getReg();
482 assert(MI.definesRegister(DestReg) &&
483 "RESTORE_CR does not define its destination");
485 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
488 // If the reloaded register isn't CR0, shift the bits right so that they are
489 // in the right CR's slot.
490 if (DestReg != PPC::CR0) {
492 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
494 unsigned ShiftBits = getEncodingValue(DestReg)*4;
495 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
496 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
497 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
501 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
502 .addReg(Reg, RegState::Kill);
504 // Discard the pseudo instruction.
508 static unsigned getCRFromCRBit(unsigned SrcReg) {
510 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
511 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
513 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
514 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
516 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
517 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
519 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
520 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
522 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
523 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
525 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
526 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
528 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
529 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
531 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
532 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
535 assert(Reg != 0 && "Invalid CR bit register");
539 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
540 unsigned FrameIndex) const {
541 // Get the instruction.
542 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset>
543 // Get the instruction's basic block.
544 MachineBasicBlock &MBB = *MI.getParent();
545 MachineFunction &MF = *MBB.getParent();
546 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
547 DebugLoc dl = MI.getDebugLoc();
549 bool LP64 = Subtarget.isPPC64();
550 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
551 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
553 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
554 unsigned SrcReg = MI.getOperand(0).getReg();
556 BuildMI(MBB, II, dl, TII.get(TargetOpcode::KILL),
557 getCRFromCRBit(SrcReg))
558 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
560 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
561 .addReg(getCRFromCRBit(SrcReg));
563 // If the saved register wasn't CR0LT, shift the bits left so that the bit to
564 // store is the first one. Mask all but that bit.
566 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
568 // rlwinm rA, rA, ShiftBits, 0, 0.
569 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
570 .addReg(Reg1, RegState::Kill)
571 .addImm(getEncodingValue(SrcReg))
572 .addImm(0).addImm(0);
574 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
575 .addReg(Reg, RegState::Kill),
578 // Discard the pseudo instruction.
582 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
583 unsigned FrameIndex) const {
584 // Get the instruction.
585 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset>
586 // Get the instruction's basic block.
587 MachineBasicBlock &MBB = *MI.getParent();
588 MachineFunction &MF = *MBB.getParent();
589 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
590 DebugLoc dl = MI.getDebugLoc();
592 bool LP64 = Subtarget.isPPC64();
593 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
594 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
596 unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
597 unsigned DestReg = MI.getOperand(0).getReg();
598 assert(MI.definesRegister(DestReg) &&
599 "RESTORE_CRBIT does not define its destination");
601 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
604 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
606 unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
607 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
608 .addReg(getCRFromCRBit(DestReg));
610 unsigned ShiftBits = getEncodingValue(DestReg);
611 // rlwimi r11, r10, 32-ShiftBits, ..., ...
612 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
613 .addReg(RegO, RegState::Kill).addReg(Reg, RegState::Kill)
614 .addImm(ShiftBits ? 32-ShiftBits : 0)
615 .addImm(ShiftBits).addImm(ShiftBits);
617 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
618 getCRFromCRBit(DestReg))
619 .addReg(RegO, RegState::Kill)
620 // Make sure we have a use dependency all the way through this
621 // sequence of instructions. We can't have the other bits in the CR
622 // modified in between the mfocrf and the mtocrf.
623 .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
625 // Discard the pseudo instruction.
629 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II,
630 unsigned FrameIndex) const {
631 // Get the instruction.
632 MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset>
633 // Get the instruction's basic block.
634 MachineBasicBlock &MBB = *MI.getParent();
635 MachineFunction &MF = *MBB.getParent();
636 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
637 DebugLoc dl = MI.getDebugLoc();
639 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
640 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
641 unsigned SrcReg = MI.getOperand(0).getReg();
643 BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
644 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
646 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::STW))
647 .addReg(Reg, RegState::Kill),
650 // Discard the pseudo instruction.
654 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II,
655 unsigned FrameIndex) const {
656 // Get the instruction.
657 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset>
658 // Get the instruction's basic block.
659 MachineBasicBlock &MBB = *MI.getParent();
660 MachineFunction &MF = *MBB.getParent();
661 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
662 DebugLoc dl = MI.getDebugLoc();
664 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
665 unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
666 unsigned DestReg = MI.getOperand(0).getReg();
667 assert(MI.definesRegister(DestReg) &&
668 "RESTORE_VRSAVE does not define its destination");
670 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
673 BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
674 .addReg(Reg, RegState::Kill);
676 // Discard the pseudo instruction.
681 PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
682 unsigned Reg, int &FrameIdx) const {
684 // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
685 // ABI, return true to prevent allocating an additional frame slot.
686 // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
687 // is arbitrary and will be subsequently ignored. For 32-bit, we have
688 // previously created the stack slot if needed, so return its FrameIdx.
689 if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
690 if (Subtarget.isPPC64())
693 const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
694 FrameIdx = FI->getCRSpillFrameIndex();
701 // Figure out if the offset in the instruction must be a multiple of 4.
702 // This is true for instructions like "STD".
703 static bool usesIXAddr(const MachineInstr &MI) {
704 unsigned OpC = MI.getOpcode();
717 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
718 static unsigned getOffsetONFromFION(const MachineInstr &MI,
719 unsigned FIOperandNum) {
720 // Take into account whether it's an add or mem instruction
721 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
722 if (MI.isInlineAsm())
723 OffsetOperandNo = FIOperandNum - 1;
724 else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
725 MI.getOpcode() == TargetOpcode::PATCHPOINT)
726 OffsetOperandNo = FIOperandNum + 1;
728 return OffsetOperandNo;
732 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
733 int SPAdj, unsigned FIOperandNum,
734 RegScavenger *RS) const {
735 assert(SPAdj == 0 && "Unexpected");
737 // Get the instruction.
738 MachineInstr &MI = *II;
739 // Get the instruction's basic block.
740 MachineBasicBlock &MBB = *MI.getParent();
741 // Get the basic block's function.
742 MachineFunction &MF = *MBB.getParent();
743 // Get the instruction info.
744 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
745 // Get the frame info.
746 MachineFrameInfo *MFI = MF.getFrameInfo();
747 DebugLoc dl = MI.getDebugLoc();
749 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
751 // Get the frame index.
752 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
754 // Get the frame pointer save index. Users of this index are primarily
755 // DYNALLOC instructions.
756 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
757 int FPSI = FI->getFramePointerSaveIndex();
758 // Get the instruction opcode.
759 unsigned OpC = MI.getOpcode();
761 // Special case for dynamic alloca.
762 if (FPSI && FrameIndex == FPSI &&
763 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
764 lowerDynamicAlloc(II);
768 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
769 if (OpC == PPC::SPILL_CR) {
770 lowerCRSpilling(II, FrameIndex);
772 } else if (OpC == PPC::RESTORE_CR) {
773 lowerCRRestore(II, FrameIndex);
775 } else if (OpC == PPC::SPILL_CRBIT) {
776 lowerCRBitSpilling(II, FrameIndex);
778 } else if (OpC == PPC::RESTORE_CRBIT) {
779 lowerCRBitRestore(II, FrameIndex);
781 } else if (OpC == PPC::SPILL_VRSAVE) {
782 lowerVRSAVESpilling(II, FrameIndex);
784 } else if (OpC == PPC::RESTORE_VRSAVE) {
785 lowerVRSAVERestore(II, FrameIndex);
789 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
790 MI.getOperand(FIOperandNum).ChangeToRegister(
791 FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
793 // Figure out if the offset in the instruction is shifted right two bits.
794 bool isIXAddr = usesIXAddr(MI);
796 // If the instruction is not present in ImmToIdxMap, then it has no immediate
797 // form (and must be r+r).
798 bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
799 OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
801 // Now add the frame object offset to the offset from r1.
802 int Offset = MFI->getObjectOffset(FrameIndex);
803 Offset += MI.getOperand(OffsetOperandNo).getImm();
805 // If we're not using a Frame Pointer that has been set to the value of the
806 // SP before having the stack size subtracted from it, then add the stack size
807 // to Offset to get the correct offset.
808 // Naked functions have stack size 0, although getStackSize may not reflect that
809 // because we didn't call all the pieces that compute it for naked functions.
810 if (!MF.getFunction()->getAttributes().
811 hasAttribute(AttributeSet::FunctionIndex, Attribute::Naked)) {
812 if (!(hasBasePointer(MF) && FrameIndex < 0))
813 Offset += MFI->getStackSize();
816 // If we can, encode the offset directly into the instruction. If this is a
817 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
818 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
819 // clear can be encoded. This is extremely uncommon, because normally you
820 // only "std" to a stack slot that is at least 4-byte aligned, but it can
821 // happen in invalid code.
822 assert(OpC != PPC::DBG_VALUE &&
823 "This should be handled in a target-independent way");
824 if (!noImmForm && ((isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) ||
825 OpC == TargetOpcode::STACKMAP ||
826 OpC == TargetOpcode::PATCHPOINT)) {
827 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
831 // The offset doesn't fit into a single register, scavenge one to build the
834 bool is64Bit = Subtarget.isPPC64();
835 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
836 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
837 const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
838 unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
839 SReg = MF.getRegInfo().createVirtualRegister(RC);
841 // Insert a set of rA with the full offset value before the ld, st, or add
842 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
843 .addImm(Offset >> 16);
844 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
845 .addReg(SRegHi, RegState::Kill)
848 // Convert into indexed form of the instruction:
850 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
851 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
852 unsigned OperandBase;
856 else if (OpC != TargetOpcode::INLINEASM) {
857 assert(ImmToIdxMap.count(OpC) &&
858 "No indexed form of load or store available!");
859 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
860 MI.setDesc(TII.get(NewOpcode));
863 OperandBase = OffsetOperandNo;
866 unsigned StackReg = MI.getOperand(FIOperandNum).getReg();
867 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
868 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
871 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
872 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
874 if (!Subtarget.isPPC64())
875 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
877 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
880 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
881 if (!hasBasePointer(MF))
882 return getFrameRegister(MF);
884 if (Subtarget.isPPC64())
887 if (Subtarget.isSVR4ABI() &&
888 MF.getTarget().getRelocationModel() == Reloc::PIC_)
894 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
895 if (!EnableBasePointer)
897 if (AlwaysBasePointer)
900 // If we need to realign the stack, then the stack pointer can no longer
901 // serve as an offset into the caller's stack space. As a result, we need a
903 return needsStackRealignment(MF);
906 bool PPCRegisterInfo::canRealignStack(const MachineFunction &MF) const {
907 if (MF.getFunction()->hasFnAttribute("no-realign-stack"))
913 bool PPCRegisterInfo::needsStackRealignment(const MachineFunction &MF) const {
914 const MachineFrameInfo *MFI = MF.getFrameInfo();
915 const Function *F = MF.getFunction();
916 unsigned StackAlign = Subtarget.getFrameLowering()->getStackAlignment();
917 bool requiresRealignment =
918 ((MFI->getMaxAlignment() > StackAlign) ||
919 F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
920 Attribute::StackAlignment));
922 return requiresRealignment && canRealignStack(MF);
925 /// Returns true if the instruction's frame index
926 /// reference would be better served by a base register other than FP
927 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
928 /// references it should create new base registers for.
929 bool PPCRegisterInfo::
930 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
931 assert(Offset < 0 && "Local offset must be negative");
933 // It's the load/store FI references that cause issues, as it can be difficult
934 // to materialize the offset if it won't fit in the literal field. Estimate
935 // based on the size of the local frame and some conservative assumptions
936 // about the rest of the stack frame (note, this is pre-regalloc, so
937 // we don't know everything for certain yet) whether this offset is likely
938 // to be out of range of the immediate. Return true if so.
940 // We only generate virtual base registers for loads and stores that have
941 // an r+i form. Return false for everything else.
942 unsigned OpC = MI->getOpcode();
943 if (!ImmToIdxMap.count(OpC))
946 // Don't generate a new virtual base register just to add zero to it.
947 if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
948 MI->getOperand(2).getImm() == 0)
951 MachineBasicBlock &MBB = *MI->getParent();
952 MachineFunction &MF = *MBB.getParent();
954 const PPCFrameLowering *PPCFI =
955 static_cast<const PPCFrameLowering *>(Subtarget.getFrameLowering());
957 PPCFI->determineFrameLayout(MF, false, true);
959 // If we likely don't need a stack frame, then we probably don't need a
960 // virtual base register either.
964 // Estimate an offset from the stack pointer.
965 // The incoming offset is relating to the SP at the start of the function,
966 // but when we access the local it'll be relative to the SP after local
967 // allocation, so adjust our SP-relative offset by that allocation size.
970 // The frame pointer will point to the end of the stack, so estimate the
971 // offset as the difference between the object offset and the FP location.
972 return !isFrameOffsetLegal(MI, Offset);
975 /// Insert defining instruction(s) for BaseReg to
976 /// be a pointer to FrameIdx at the beginning of the basic block.
977 void PPCRegisterInfo::
978 materializeFrameBaseRegister(MachineBasicBlock *MBB,
979 unsigned BaseReg, int FrameIdx,
980 int64_t Offset) const {
981 unsigned ADDriOpc = Subtarget.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
983 MachineBasicBlock::iterator Ins = MBB->begin();
984 DebugLoc DL; // Defaults to "unknown"
985 if (Ins != MBB->end())
986 DL = Ins->getDebugLoc();
988 const MachineFunction &MF = *MBB->getParent();
989 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
990 const MCInstrDesc &MCID = TII.get(ADDriOpc);
991 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
992 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
994 BuildMI(*MBB, Ins, DL, MCID, BaseReg)
995 .addFrameIndex(FrameIdx).addImm(Offset);
998 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
999 int64_t Offset) const {
1000 unsigned FIOperandNum = 0;
1001 while (!MI.getOperand(FIOperandNum).isFI()) {
1003 assert(FIOperandNum < MI.getNumOperands() &&
1004 "Instr doesn't have FrameIndex operand!");
1007 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
1008 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1009 Offset += MI.getOperand(OffsetOperandNo).getImm();
1010 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1012 MachineBasicBlock &MBB = *MI.getParent();
1013 MachineFunction &MF = *MBB.getParent();
1014 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1015 const MCInstrDesc &MCID = MI.getDesc();
1016 MachineRegisterInfo &MRI = MF.getRegInfo();
1017 MRI.constrainRegClass(BaseReg,
1018 TII.getRegClass(MCID, FIOperandNum, this, MF));
1021 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1022 int64_t Offset) const {
1023 unsigned FIOperandNum = 0;
1024 while (!MI->getOperand(FIOperandNum).isFI()) {
1026 assert(FIOperandNum < MI->getNumOperands() &&
1027 "Instr doesn't have FrameIndex operand!");
1030 unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
1031 Offset += MI->getOperand(OffsetOperandNo).getImm();
1033 return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
1034 MI->getOpcode() == TargetOpcode::STACKMAP ||
1035 MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1036 (isInt<16>(Offset) && (!usesIXAddr(*MI) || (Offset & 3) == 0));