xref: /llvm-project/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp (revision 11049f8f07330c30c689d54ed91d63909b360cf2)
1 //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the PowerPC implementation of the TargetRegisterInfo
11 // class.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "PPCRegisterInfo.h"
16 #include "PPC.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"
43 #include <cstdlib>
44 
45 using namespace llvm;
46 
47 #define DEBUG_TYPE "reginfo"
48 
49 #define GET_REGINFO_TARGET_DESC
50 #include "PPCGenRegisterInfo.inc"
51 
52 static cl::opt<bool>
53 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
54          cl::desc("Enable use of a base pointer for complex stack frames"));
55 
56 static cl::opt<bool>
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"));
59 
60 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
61   : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
62                        TM.isPPC64() ? 0 : 1,
63                        TM.isPPC64() ? 0 : 1),
64     TM(TM) {
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;
74 
75   // 64-bit
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;
81 }
82 
83 /// getPointerRegClass - Return the register class to use to hold pointers.
84 /// This is used for addressing modes.
85 const TargetRegisterClass *
86 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
87                                                                        const {
88   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
89   // when it checks for ZERO folding.
90   if (Kind == 1) {
91     if (TM.isPPC64())
92       return &PPC::G8RC_NOX0RegClass;
93     return &PPC::GPRC_NOR0RegClass;
94   }
95 
96   if (TM.isPPC64())
97     return &PPC::G8RCRegClass;
98   return &PPC::GPRCRegClass;
99 }
100 
101 const MCPhysReg*
102 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
103   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
104   if (MF->getFunction()->getCallingConv() == CallingConv::AnyReg) {
105     if (Subtarget.hasVSX())
106       return CSR_64_AllRegs_VSX_SaveList;
107     if (Subtarget.hasAltivec())
108       return CSR_64_AllRegs_Altivec_SaveList;
109     return CSR_64_AllRegs_SaveList;
110   }
111 
112   if (Subtarget.isDarwinABI())
113     return TM.isPPC64()
114                ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_SaveList
115                                          : CSR_Darwin64_SaveList)
116                : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_SaveList
117                                          : CSR_Darwin32_SaveList);
118 
119   if (TM.isPPC64() && MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
120     return CSR_SRV464_TLS_PE_SaveList;
121 
122   // On PPC64, we might need to save r2 (but only if it is not reserved).
123   bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2);
124 
125   return TM.isPPC64()
126              ? (Subtarget.hasAltivec()
127                     ? (SaveR2 ? CSR_SVR464_R2_Altivec_SaveList
128                               : CSR_SVR464_Altivec_SaveList)
129                     : (SaveR2 ? CSR_SVR464_R2_SaveList : CSR_SVR464_SaveList))
130              : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_SaveList
131                                        : CSR_SVR432_SaveList);
132 }
133 
134 const MCPhysReg *
135 PPCRegisterInfo::getCalleeSavedRegsViaCopy(const MachineFunction *MF) const {
136   assert(MF && "Invalid MachineFunction pointer.");
137   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
138   if (Subtarget.isDarwinABI())
139     return nullptr;
140   if (!TM.isPPC64())
141     return nullptr;
142   if (MF->getFunction()->getCallingConv() != CallingConv::CXX_FAST_TLS)
143     return nullptr;
144   if (!MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
145     return nullptr;
146 
147   // On PPC64, we might need to save r2 (but only if it is not reserved).
148   bool SaveR2 = !getReservedRegs(*MF).test(PPC::X2);
149   if (Subtarget.hasAltivec())
150     return SaveR2
151       ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList
152       : CSR_SVR464_Altivec_ViaCopy_SaveList;
153   else
154     return SaveR2
155       ? CSR_SVR464_R2_ViaCopy_SaveList
156       : CSR_SVR464_ViaCopy_SaveList;
157 }
158 
159 const uint32_t *
160 PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
161                                       CallingConv::ID CC) const {
162   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
163   if (CC == CallingConv::AnyReg) {
164     if (Subtarget.hasVSX())
165       return CSR_64_AllRegs_VSX_RegMask;
166     if (Subtarget.hasAltivec())
167       return CSR_64_AllRegs_Altivec_RegMask;
168     return CSR_64_AllRegs_RegMask;
169   }
170 
171   if (Subtarget.isDarwinABI())
172     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_RegMask
173                                                   : CSR_Darwin64_RegMask)
174                         : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_RegMask
175                                                   : CSR_Darwin32_RegMask);
176 
177   return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR464_Altivec_RegMask
178                                                 : CSR_SVR464_RegMask)
179                       : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_RegMask
180                                                 : CSR_SVR432_RegMask);
181 }
182 
183 const uint32_t*
184 PPCRegisterInfo::getNoPreservedMask() const {
185   return CSR_NoRegs_RegMask;
186 }
187 
188 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
189   for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
190     Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
191 }
192 
193 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
194   BitVector Reserved(getNumRegs());
195   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
196   const PPCFrameLowering *TFI = getFrameLowering(MF);
197 
198   // The ZERO register is not really a register, but the representation of r0
199   // when used in instructions that treat r0 as the constant 0.
200   Reserved.set(PPC::ZERO);
201   Reserved.set(PPC::ZERO8);
202 
203   // The FP register is also not really a register, but is the representation
204   // of the frame pointer register used by ISD::FRAMEADDR.
205   Reserved.set(PPC::FP);
206   Reserved.set(PPC::FP8);
207 
208   // The BP register is also not really a register, but is the representation
209   // of the base pointer register used by setjmp.
210   Reserved.set(PPC::BP);
211   Reserved.set(PPC::BP8);
212 
213   // The counter registers must be reserved so that counter-based loops can
214   // be correctly formed (and the mtctr instructions are not DCE'd).
215   Reserved.set(PPC::CTR);
216   Reserved.set(PPC::CTR8);
217 
218   Reserved.set(PPC::R1);
219   Reserved.set(PPC::LR);
220   Reserved.set(PPC::LR8);
221   Reserved.set(PPC::RM);
222 
223   if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec())
224     Reserved.set(PPC::VRSAVE);
225 
226   // The SVR4 ABI reserves r2 and r13
227   if (Subtarget.isSVR4ABI()) {
228     Reserved.set(PPC::R2);  // System-reserved register
229     Reserved.set(PPC::R13); // Small Data Area pointer register
230   }
231 
232   // On PPC64, r13 is the thread pointer. Never allocate this register.
233   if (TM.isPPC64()) {
234     Reserved.set(PPC::R13);
235 
236     Reserved.set(PPC::X1);
237     Reserved.set(PPC::X13);
238 
239     if (TFI->needsFP(MF))
240       Reserved.set(PPC::X31);
241 
242     if (hasBasePointer(MF))
243       Reserved.set(PPC::X30);
244 
245     // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
246     if (Subtarget.isSVR4ABI()) {
247       // We only reserve r2 if we need to use the TOC pointer. If we have no
248       // explicit uses of the TOC pointer (meaning we're a leaf function with
249       // no constant-pool loads, etc.) and we have no potential uses inside an
250       // inline asm block, then we can treat r2 has an ordinary callee-saved
251       // register.
252       const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
253       if (FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
254         Reserved.set(PPC::X2);
255       else
256         Reserved.reset(PPC::R2);
257     }
258   }
259 
260   if (TFI->needsFP(MF))
261     Reserved.set(PPC::R31);
262 
263   bool IsPositionIndependent = TM.isPositionIndependent();
264   if (hasBasePointer(MF)) {
265     if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent)
266       Reserved.set(PPC::R29);
267     else
268       Reserved.set(PPC::R30);
269   }
270 
271   if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent)
272     Reserved.set(PPC::R30);
273 
274   // Reserve Altivec registers when Altivec is unavailable.
275   if (!Subtarget.hasAltivec())
276     for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
277          IE = PPC::VRRCRegClass.end(); I != IE; ++I)
278       Reserved.set(*I);
279 
280   return Reserved;
281 }
282 
283 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
284                                               MachineFunction &MF) const {
285   const PPCFrameLowering *TFI = getFrameLowering(MF);
286   const unsigned DefaultSafety = 1;
287 
288   switch (RC->getID()) {
289   default:
290     return 0;
291   case PPC::G8RC_NOX0RegClassID:
292   case PPC::GPRC_NOR0RegClassID:
293   case PPC::G8RCRegClassID:
294   case PPC::GPRCRegClassID: {
295     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
296     return 32 - FP - DefaultSafety;
297   }
298   case PPC::F8RCRegClassID:
299   case PPC::F4RCRegClassID:
300   case PPC::QFRCRegClassID:
301   case PPC::QSRCRegClassID:
302   case PPC::QBRCRegClassID:
303   case PPC::VRRCRegClassID:
304   case PPC::VFRCRegClassID:
305   case PPC::VSLRCRegClassID:
306     return 32 - DefaultSafety;
307   case PPC::VSRCRegClassID:
308   case PPC::VSFRCRegClassID:
309   case PPC::VSSRCRegClassID:
310     return 64 - DefaultSafety;
311   case PPC::CRRCRegClassID:
312     return 8 - DefaultSafety;
313   }
314 }
315 
316 const TargetRegisterClass *
317 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
318                                            const MachineFunction &MF) const {
319   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
320   if (Subtarget.hasVSX()) {
321     // With VSX, we can inflate various sub-register classes to the full VSX
322     // register set.
323 
324     if (RC == &PPC::F8RCRegClass)
325       return &PPC::VSFRCRegClass;
326     else if (RC == &PPC::VRRCRegClass)
327       return &PPC::VSRCRegClass;
328     else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector())
329       return &PPC::VSSRCRegClass;
330   }
331 
332   return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
333 }
334 
335 //===----------------------------------------------------------------------===//
336 // Stack Frame Processing methods
337 //===----------------------------------------------------------------------===//
338 
339 /// lowerDynamicAlloc - Generate the code for allocating an object in the
340 /// current frame.  The sequence of code will be in the general form
341 ///
342 ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
343 ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
344 ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
345 ///
346 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
347   // Get the instruction.
348   MachineInstr &MI = *II;
349   // Get the instruction's basic block.
350   MachineBasicBlock &MBB = *MI.getParent();
351   // Get the basic block's function.
352   MachineFunction &MF = *MBB.getParent();
353   // Get the frame info.
354   MachineFrameInfo &MFI = MF.getFrameInfo();
355   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
356   // Get the instruction info.
357   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
358   // Determine whether 64-bit pointers are used.
359   bool LP64 = TM.isPPC64();
360   DebugLoc dl = MI.getDebugLoc();
361 
362   // Get the maximum call stack size.
363   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
364   // Get the total frame size.
365   unsigned FrameSize = MFI.getStackSize();
366 
367   // Get stack alignments.
368   const PPCFrameLowering *TFI = getFrameLowering(MF);
369   unsigned TargetAlign = TFI->getStackAlignment();
370   unsigned MaxAlign = MFI.getMaxAlignment();
371   assert((maxCallFrameSize & (MaxAlign-1)) == 0 &&
372          "Maximum call-frame size not sufficiently aligned");
373 
374   // Determine the previous frame's address.  If FrameSize can't be
375   // represented as 16 bits or we need special alignment, then we load the
376   // previous frame's address from 0(SP).  Why not do an addis of the hi?
377   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
378   // Constructing the constant and adding would take 3 instructions.
379   // Fortunately, a frame greater than 32K is rare.
380   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
381   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
382   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
383 
384   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
385     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
386       .addReg(PPC::R31)
387       .addImm(FrameSize);
388   } else if (LP64) {
389     BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
390       .addImm(0)
391       .addReg(PPC::X1);
392   } else {
393     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
394       .addImm(0)
395       .addReg(PPC::R1);
396   }
397 
398   bool KillNegSizeReg = MI.getOperand(1).isKill();
399   unsigned NegSizeReg = MI.getOperand(1).getReg();
400 
401   // Grow the stack and update the stack pointer link, then determine the
402   // address of new allocated space.
403   if (LP64) {
404     if (MaxAlign > TargetAlign) {
405       unsigned UnalNegSizeReg = NegSizeReg;
406       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
407 
408       // Unfortunately, there is no andi, only andi., and we can't insert that
409       // here because we might clobber cr0 while it is live.
410       BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
411         .addImm(~(MaxAlign-1));
412 
413       unsigned NegSizeReg1 = NegSizeReg;
414       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
415       BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
416         .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
417         .addReg(NegSizeReg1, RegState::Kill);
418       KillNegSizeReg = true;
419     }
420 
421     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
422       .addReg(Reg, RegState::Kill)
423       .addReg(PPC::X1)
424       .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
425     BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
426       .addReg(PPC::X1)
427       .addImm(maxCallFrameSize);
428   } else {
429     if (MaxAlign > TargetAlign) {
430       unsigned UnalNegSizeReg = NegSizeReg;
431       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
432 
433       // Unfortunately, there is no andi, only andi., and we can't insert that
434       // here because we might clobber cr0 while it is live.
435       BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
436         .addImm(~(MaxAlign-1));
437 
438       unsigned NegSizeReg1 = NegSizeReg;
439       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
440       BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
441         .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
442         .addReg(NegSizeReg1, RegState::Kill);
443       KillNegSizeReg = true;
444     }
445 
446     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
447       .addReg(Reg, RegState::Kill)
448       .addReg(PPC::R1)
449       .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
450     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
451       .addReg(PPC::R1)
452       .addImm(maxCallFrameSize);
453   }
454 
455   // Discard the DYNALLOC instruction.
456   MBB.erase(II);
457 }
458 
459 void PPCRegisterInfo::lowerDynamicAreaOffset(
460     MachineBasicBlock::iterator II) const {
461   // Get the instruction.
462   MachineInstr &MI = *II;
463   // Get the instruction's basic block.
464   MachineBasicBlock &MBB = *MI.getParent();
465   // Get the basic block's function.
466   MachineFunction &MF = *MBB.getParent();
467   // Get the frame info.
468   MachineFrameInfo &MFI = MF.getFrameInfo();
469   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
470   // Get the instruction info.
471   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
472 
473   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
474   DebugLoc dl = MI.getDebugLoc();
475   BuildMI(MBB, II, dl, TII.get(PPC::LI), MI.getOperand(0).getReg())
476       .addImm(maxCallFrameSize);
477   MBB.erase(II);
478 }
479 
480 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
481 /// reserving a whole register (R0), we scrounge for one here. This generates
482 /// code like this:
483 ///
484 ///   mfcr rA                  ; Move the conditional register into GPR rA.
485 ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
486 ///   stw rA, FI               ; Store rA to the frame.
487 ///
488 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
489                                       unsigned FrameIndex) const {
490   // Get the instruction.
491   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
492   // Get the instruction's basic block.
493   MachineBasicBlock &MBB = *MI.getParent();
494   MachineFunction &MF = *MBB.getParent();
495   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
496   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
497   DebugLoc dl = MI.getDebugLoc();
498 
499   bool LP64 = TM.isPPC64();
500   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
501   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
502 
503   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
504   unsigned SrcReg = MI.getOperand(0).getReg();
505 
506   // We need to store the CR in the low 4-bits of the saved value. First, issue
507   // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
508   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
509       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
510 
511   // If the saved register wasn't CR0, shift the bits left so that they are in
512   // CR0's slot.
513   if (SrcReg != PPC::CR0) {
514     unsigned Reg1 = Reg;
515     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
516 
517     // rlwinm rA, rA, ShiftBits, 0, 31.
518     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
519       .addReg(Reg1, RegState::Kill)
520       .addImm(getEncodingValue(SrcReg) * 4)
521       .addImm(0)
522       .addImm(31);
523   }
524 
525   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
526                     .addReg(Reg, RegState::Kill),
527                     FrameIndex);
528 
529   // Discard the pseudo instruction.
530   MBB.erase(II);
531 }
532 
533 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
534                                       unsigned FrameIndex) const {
535   // Get the instruction.
536   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
537   // Get the instruction's basic block.
538   MachineBasicBlock &MBB = *MI.getParent();
539   MachineFunction &MF = *MBB.getParent();
540   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
541   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
542   DebugLoc dl = MI.getDebugLoc();
543 
544   bool LP64 = TM.isPPC64();
545   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
546   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
547 
548   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
549   unsigned DestReg = MI.getOperand(0).getReg();
550   assert(MI.definesRegister(DestReg) &&
551     "RESTORE_CR does not define its destination");
552 
553   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
554                               Reg), FrameIndex);
555 
556   // If the reloaded register isn't CR0, shift the bits right so that they are
557   // in the right CR's slot.
558   if (DestReg != PPC::CR0) {
559     unsigned Reg1 = Reg;
560     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
561 
562     unsigned ShiftBits = getEncodingValue(DestReg)*4;
563     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
564     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
565              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
566              .addImm(31);
567   }
568 
569   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
570              .addReg(Reg, RegState::Kill);
571 
572   // Discard the pseudo instruction.
573   MBB.erase(II);
574 }
575 
576 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
577                                          unsigned FrameIndex) const {
578   // Get the instruction.
579   MachineInstr &MI = *II;       // ; SPILL_CRBIT <SrcReg>, <offset>
580   // Get the instruction's basic block.
581   MachineBasicBlock &MBB = *MI.getParent();
582   MachineFunction &MF = *MBB.getParent();
583   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
584   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
585   DebugLoc dl = MI.getDebugLoc();
586 
587   bool LP64 = TM.isPPC64();
588   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
589   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
590 
591   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
592   unsigned SrcReg = MI.getOperand(0).getReg();
593 
594   BuildMI(MBB, II, dl, TII.get(TargetOpcode::KILL),
595           getCRFromCRBit(SrcReg))
596           .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
597 
598   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
599       .addReg(getCRFromCRBit(SrcReg));
600 
601   // If the saved register wasn't CR0LT, shift the bits left so that the bit to
602   // store is the first one. Mask all but that bit.
603   unsigned Reg1 = Reg;
604   Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
605 
606   // rlwinm rA, rA, ShiftBits, 0, 0.
607   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
608     .addReg(Reg1, RegState::Kill)
609     .addImm(getEncodingValue(SrcReg))
610     .addImm(0).addImm(0);
611 
612   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
613                     .addReg(Reg, RegState::Kill),
614                     FrameIndex);
615 
616   // Discard the pseudo instruction.
617   MBB.erase(II);
618 }
619 
620 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
621                                       unsigned FrameIndex) const {
622   // Get the instruction.
623   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CRBIT <offset>
624   // Get the instruction's basic block.
625   MachineBasicBlock &MBB = *MI.getParent();
626   MachineFunction &MF = *MBB.getParent();
627   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
628   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
629   DebugLoc dl = MI.getDebugLoc();
630 
631   bool LP64 = TM.isPPC64();
632   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
633   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
634 
635   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
636   unsigned DestReg = MI.getOperand(0).getReg();
637   assert(MI.definesRegister(DestReg) &&
638     "RESTORE_CRBIT does not define its destination");
639 
640   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
641                               Reg), FrameIndex);
642 
643   BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
644 
645   unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
646   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
647           .addReg(getCRFromCRBit(DestReg));
648 
649   unsigned ShiftBits = getEncodingValue(DestReg);
650   // rlwimi r11, r10, 32-ShiftBits, ..., ...
651   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
652       .addReg(RegO, RegState::Kill)
653       .addReg(Reg, RegState::Kill)
654       .addImm(ShiftBits ? 32 - ShiftBits : 0)
655       .addImm(ShiftBits)
656       .addImm(ShiftBits);
657 
658   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
659           getCRFromCRBit(DestReg))
660       .addReg(RegO, RegState::Kill)
661       // Make sure we have a use dependency all the way through this
662       // sequence of instructions. We can't have the other bits in the CR
663       // modified in between the mfocrf and the mtocrf.
664       .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
665 
666   // Discard the pseudo instruction.
667   MBB.erase(II);
668 }
669 
670 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II,
671                                           unsigned FrameIndex) const {
672   // Get the instruction.
673   MachineInstr &MI = *II;       // ; SPILL_VRSAVE <SrcReg>, <offset>
674   // Get the instruction's basic block.
675   MachineBasicBlock &MBB = *MI.getParent();
676   MachineFunction &MF = *MBB.getParent();
677   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
678   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
679   DebugLoc dl = MI.getDebugLoc();
680 
681   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
682   unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
683   unsigned SrcReg = MI.getOperand(0).getReg();
684 
685   BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
686       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
687 
688   addFrameReference(
689       BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill),
690       FrameIndex);
691 
692   // Discard the pseudo instruction.
693   MBB.erase(II);
694 }
695 
696 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II,
697                                          unsigned FrameIndex) const {
698   // Get the instruction.
699   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_VRSAVE <offset>
700   // Get the instruction's basic block.
701   MachineBasicBlock &MBB = *MI.getParent();
702   MachineFunction &MF = *MBB.getParent();
703   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
704   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
705   DebugLoc dl = MI.getDebugLoc();
706 
707   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
708   unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
709   unsigned DestReg = MI.getOperand(0).getReg();
710   assert(MI.definesRegister(DestReg) &&
711     "RESTORE_VRSAVE does not define its destination");
712 
713   addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
714                               Reg), FrameIndex);
715 
716   BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
717              .addReg(Reg, RegState::Kill);
718 
719   // Discard the pseudo instruction.
720   MBB.erase(II);
721 }
722 
723 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
724                                            unsigned Reg, int &FrameIdx) const {
725   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
726   // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
727   // ABI, return true to prevent allocating an additional frame slot.
728   // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
729   // is arbitrary and will be subsequently ignored.  For 32-bit, we have
730   // previously created the stack slot if needed, so return its FrameIdx.
731   if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
732     if (TM.isPPC64())
733       FrameIdx = 0;
734     else {
735       const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
736       FrameIdx = FI->getCRSpillFrameIndex();
737     }
738     return true;
739   }
740   return false;
741 }
742 
743 // Figure out if the offset in the instruction must be a multiple of 4.
744 // This is true for instructions like "STD".
745 static bool usesIXAddr(const MachineInstr &MI) {
746   unsigned OpC = MI.getOpcode();
747 
748   switch (OpC) {
749   default:
750     return false;
751   case PPC::LWA:
752   case PPC::LWA_32:
753   case PPC::LD:
754   case PPC::STD:
755     return true;
756   }
757 }
758 
759 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
760 static unsigned getOffsetONFromFION(const MachineInstr &MI,
761                                     unsigned FIOperandNum) {
762   // Take into account whether it's an add or mem instruction
763   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
764   if (MI.isInlineAsm())
765     OffsetOperandNo = FIOperandNum - 1;
766   else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
767            MI.getOpcode() == TargetOpcode::PATCHPOINT)
768     OffsetOperandNo = FIOperandNum + 1;
769 
770   return OffsetOperandNo;
771 }
772 
773 void
774 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
775                                      int SPAdj, unsigned FIOperandNum,
776                                      RegScavenger *RS) const {
777   assert(SPAdj == 0 && "Unexpected");
778 
779   // Get the instruction.
780   MachineInstr &MI = *II;
781   // Get the instruction's basic block.
782   MachineBasicBlock &MBB = *MI.getParent();
783   // Get the basic block's function.
784   MachineFunction &MF = *MBB.getParent();
785   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
786   // Get the instruction info.
787   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
788   // Get the frame info.
789   MachineFrameInfo &MFI = MF.getFrameInfo();
790   DebugLoc dl = MI.getDebugLoc();
791 
792   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
793 
794   // Get the frame index.
795   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
796 
797   // Get the frame pointer save index.  Users of this index are primarily
798   // DYNALLOC instructions.
799   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
800   int FPSI = FI->getFramePointerSaveIndex();
801   // Get the instruction opcode.
802   unsigned OpC = MI.getOpcode();
803 
804   if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
805     lowerDynamicAreaOffset(II);
806     return;
807   }
808 
809   // Special case for dynamic alloca.
810   if (FPSI && FrameIndex == FPSI &&
811       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
812     lowerDynamicAlloc(II);
813     return;
814   }
815 
816   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
817   if (OpC == PPC::SPILL_CR) {
818     lowerCRSpilling(II, FrameIndex);
819     return;
820   } else if (OpC == PPC::RESTORE_CR) {
821     lowerCRRestore(II, FrameIndex);
822     return;
823   } else if (OpC == PPC::SPILL_CRBIT) {
824     lowerCRBitSpilling(II, FrameIndex);
825     return;
826   } else if (OpC == PPC::RESTORE_CRBIT) {
827     lowerCRBitRestore(II, FrameIndex);
828     return;
829   } else if (OpC == PPC::SPILL_VRSAVE) {
830     lowerVRSAVESpilling(II, FrameIndex);
831     return;
832   } else if (OpC == PPC::RESTORE_VRSAVE) {
833     lowerVRSAVERestore(II, FrameIndex);
834     return;
835   }
836 
837   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
838   MI.getOperand(FIOperandNum).ChangeToRegister(
839     FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
840 
841   // Figure out if the offset in the instruction is shifted right two bits.
842   bool isIXAddr = usesIXAddr(MI);
843 
844   // If the instruction is not present in ImmToIdxMap, then it has no immediate
845   // form (and must be r+r).
846   bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
847                    OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
848 
849   // Now add the frame object offset to the offset from r1.
850   int Offset = MFI.getObjectOffset(FrameIndex);
851   Offset += MI.getOperand(OffsetOperandNo).getImm();
852 
853   // If we're not using a Frame Pointer that has been set to the value of the
854   // SP before having the stack size subtracted from it, then add the stack size
855   // to Offset to get the correct offset.
856   // Naked functions have stack size 0, although getStackSize may not reflect
857   // that because we didn't call all the pieces that compute it for naked
858   // functions.
859   if (!MF.getFunction()->hasFnAttribute(Attribute::Naked)) {
860     if (!(hasBasePointer(MF) && FrameIndex < 0))
861       Offset += MFI.getStackSize();
862   }
863 
864   // If we can, encode the offset directly into the instruction.  If this is a
865   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
866   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
867   // clear can be encoded.  This is extremely uncommon, because normally you
868   // only "std" to a stack slot that is at least 4-byte aligned, but it can
869   // happen in invalid code.
870   assert(OpC != PPC::DBG_VALUE &&
871          "This should be handled in a target-independent way");
872   if (!noImmForm && ((isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) ||
873                      OpC == TargetOpcode::STACKMAP ||
874                      OpC == TargetOpcode::PATCHPOINT)) {
875     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
876     return;
877   }
878 
879   // The offset doesn't fit into a single register, scavenge one to build the
880   // offset in.
881 
882   bool is64Bit = TM.isPPC64();
883   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
884   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
885   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
886   unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
887            SReg = MF.getRegInfo().createVirtualRegister(RC);
888 
889   // Insert a set of rA with the full offset value before the ld, st, or add
890   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
891     .addImm(Offset >> 16);
892   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
893     .addReg(SRegHi, RegState::Kill)
894     .addImm(Offset);
895 
896   // Convert into indexed form of the instruction:
897   //
898   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
899   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
900   unsigned OperandBase;
901 
902   if (noImmForm)
903     OperandBase = 1;
904   else if (OpC != TargetOpcode::INLINEASM) {
905     assert(ImmToIdxMap.count(OpC) &&
906            "No indexed form of load or store available!");
907     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
908     MI.setDesc(TII.get(NewOpcode));
909     OperandBase = 1;
910   } else {
911     OperandBase = OffsetOperandNo;
912   }
913 
914   unsigned StackReg = MI.getOperand(FIOperandNum).getReg();
915   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
916   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
917 }
918 
919 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
920   const PPCFrameLowering *TFI = getFrameLowering(MF);
921 
922   if (!TM.isPPC64())
923     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
924   else
925     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
926 }
927 
928 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
929   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
930   if (!hasBasePointer(MF))
931     return getFrameRegister(MF);
932 
933   if (TM.isPPC64())
934     return PPC::X30;
935 
936   if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
937     return PPC::R29;
938 
939   return PPC::R30;
940 }
941 
942 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
943   if (!EnableBasePointer)
944     return false;
945   if (AlwaysBasePointer)
946     return true;
947 
948   // If we need to realign the stack, then the stack pointer can no longer
949   // serve as an offset into the caller's stack space. As a result, we need a
950   // base pointer.
951   return needsStackRealignment(MF);
952 }
953 
954 /// Returns true if the instruction's frame index
955 /// reference would be better served by a base register other than FP
956 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
957 /// references it should create new base registers for.
958 bool PPCRegisterInfo::
959 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
960   assert(Offset < 0 && "Local offset must be negative");
961 
962   // It's the load/store FI references that cause issues, as it can be difficult
963   // to materialize the offset if it won't fit in the literal field. Estimate
964   // based on the size of the local frame and some conservative assumptions
965   // about the rest of the stack frame (note, this is pre-regalloc, so
966   // we don't know everything for certain yet) whether this offset is likely
967   // to be out of range of the immediate. Return true if so.
968 
969   // We only generate virtual base registers for loads and stores that have
970   // an r+i form. Return false for everything else.
971   unsigned OpC = MI->getOpcode();
972   if (!ImmToIdxMap.count(OpC))
973     return false;
974 
975   // Don't generate a new virtual base register just to add zero to it.
976   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
977       MI->getOperand(2).getImm() == 0)
978     return false;
979 
980   MachineBasicBlock &MBB = *MI->getParent();
981   MachineFunction &MF = *MBB.getParent();
982   const PPCFrameLowering *TFI = getFrameLowering(MF);
983   unsigned StackEst = TFI->determineFrameLayout(MF, false, true);
984 
985   // If we likely don't need a stack frame, then we probably don't need a
986   // virtual base register either.
987   if (!StackEst)
988     return false;
989 
990   // Estimate an offset from the stack pointer.
991   // The incoming offset is relating to the SP at the start of the function,
992   // but when we access the local it'll be relative to the SP after local
993   // allocation, so adjust our SP-relative offset by that allocation size.
994   Offset += StackEst;
995 
996   // The frame pointer will point to the end of the stack, so estimate the
997   // offset as the difference between the object offset and the FP location.
998   return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
999 }
1000 
1001 /// Insert defining instruction(s) for BaseReg to
1002 /// be a pointer to FrameIdx at the beginning of the basic block.
1003 void PPCRegisterInfo::
1004 materializeFrameBaseRegister(MachineBasicBlock *MBB,
1005                              unsigned BaseReg, int FrameIdx,
1006                              int64_t Offset) const {
1007   unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
1008 
1009   MachineBasicBlock::iterator Ins = MBB->begin();
1010   DebugLoc DL;                  // Defaults to "unknown"
1011   if (Ins != MBB->end())
1012     DL = Ins->getDebugLoc();
1013 
1014   const MachineFunction &MF = *MBB->getParent();
1015   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1016   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1017   const MCInstrDesc &MCID = TII.get(ADDriOpc);
1018   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1019   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
1020 
1021   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
1022     .addFrameIndex(FrameIdx).addImm(Offset);
1023 }
1024 
1025 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
1026                                         int64_t Offset) const {
1027   unsigned FIOperandNum = 0;
1028   while (!MI.getOperand(FIOperandNum).isFI()) {
1029     ++FIOperandNum;
1030     assert(FIOperandNum < MI.getNumOperands() &&
1031            "Instr doesn't have FrameIndex operand!");
1032   }
1033 
1034   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
1035   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1036   Offset += MI.getOperand(OffsetOperandNo).getImm();
1037   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1038 
1039   MachineBasicBlock &MBB = *MI.getParent();
1040   MachineFunction &MF = *MBB.getParent();
1041   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1042   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1043   const MCInstrDesc &MCID = MI.getDesc();
1044   MachineRegisterInfo &MRI = MF.getRegInfo();
1045   MRI.constrainRegClass(BaseReg,
1046                         TII.getRegClass(MCID, FIOperandNum, this, MF));
1047 }
1048 
1049 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1050                                          unsigned BaseReg,
1051                                          int64_t Offset) const {
1052   unsigned FIOperandNum = 0;
1053   while (!MI->getOperand(FIOperandNum).isFI()) {
1054     ++FIOperandNum;
1055     assert(FIOperandNum < MI->getNumOperands() &&
1056            "Instr doesn't have FrameIndex operand!");
1057   }
1058 
1059   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
1060   Offset += MI->getOperand(OffsetOperandNo).getImm();
1061 
1062   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
1063          MI->getOpcode() == TargetOpcode::STACKMAP ||
1064          MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1065          (isInt<16>(Offset) && (!usesIXAddr(*MI) || (Offset & 3) == 0));
1066 }
1067