xref: /llvm-project/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp (revision a9968c0a339ab47f5d6da1c513b2754f1e98a596)
1 //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the PowerPC implementation of the TargetRegisterInfo
10 // class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "PPCRegisterInfo.h"
15 #include "PPCFrameLowering.h"
16 #include "PPCInstrBuilder.h"
17 #include "PPCMachineFunctionInfo.h"
18 #include "PPCSubtarget.h"
19 #include "PPCTargetMachine.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.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/CodeGen/TargetFrameLowering.h"
30 #include "llvm/CodeGen/TargetInstrInfo.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Target/TargetMachine.h"
41 #include "llvm/Target/TargetOptions.h"
42 #include <cstdlib>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "reginfo"
47 
48 #define GET_REGINFO_TARGET_DESC
49 #include "PPCGenRegisterInfo.inc"
50 
51 STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass");
52 STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass");
53 
54 static cl::opt<bool>
55 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
56          cl::desc("Enable use of a base pointer for complex stack frames"));
57 
58 static cl::opt<bool>
59 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
60          cl::desc("Force the use of a base pointer in every function"));
61 
62 static cl::opt<bool>
63 EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false),
64          cl::desc("Enable spills from gpr to vsr rather than stack"));
65 
66 static cl::opt<bool>
67 StackPtrConst("ppc-stack-ptr-caller-preserved",
68                 cl::desc("Consider R1 caller preserved so stack saves of "
69                          "caller preserved registers can be LICM candidates"),
70                 cl::init(true), cl::Hidden);
71 
72 static cl::opt<unsigned>
73 MaxCRBitSpillDist("ppc-max-crbit-spill-dist",
74                   cl::desc("Maximum search distance for definition of CR bit "
75                            "spill on ppc"),
76                   cl::Hidden, cl::init(100));
77 
78 // Copies/moves of physical accumulators are expensive operations
79 // that should be avoided whenever possible. MMA instructions are
80 // meant to be used in performance-sensitive computational kernels.
81 // This option is provided, at least for the time being, to give the
82 // user a tool to detect this expensive operation and either rework
83 // their code or report a compiler bug if that turns out to be the
84 // cause.
85 #ifndef NDEBUG
86 static cl::opt<bool>
87 ReportAccMoves("ppc-report-acc-moves",
88                cl::desc("Emit information about accumulator register spills "
89                         "and copies"),
90                cl::Hidden, cl::init(false));
91 #endif
92 
93 static unsigned offsetMinAlignForOpcode(unsigned OpC);
94 
95 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
96   : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
97                        TM.isPPC64() ? 0 : 1,
98                        TM.isPPC64() ? 0 : 1),
99     TM(TM) {
100   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
101   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
102   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
103   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
104   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
105   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
106   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
107   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
108   ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
109 
110   // 64-bit
111   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
112   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
113   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
114   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
115   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
116 
117   // VSX
118   ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX;
119   ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX;
120   ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX;
121   ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX;
122   ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX;
123   ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX;
124   ImmToIdxMap[PPC::LXV] = PPC::LXVX;
125   ImmToIdxMap[PPC::LXSD] = PPC::LXSDX;
126   ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX;
127   ImmToIdxMap[PPC::STXV] = PPC::STXVX;
128   ImmToIdxMap[PPC::STXSD] = PPC::STXSDX;
129   ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX;
130 
131   // SPE
132   ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX;
133   ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX;
134   ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX;
135   ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX;
136 }
137 
138 /// getPointerRegClass - Return the register class to use to hold pointers.
139 /// This is used for addressing modes.
140 const TargetRegisterClass *
141 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
142                                                                        const {
143   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
144   // when it checks for ZERO folding.
145   if (Kind == 1) {
146     if (TM.isPPC64())
147       return &PPC::G8RC_NOX0RegClass;
148     return &PPC::GPRC_NOR0RegClass;
149   }
150 
151   if (TM.isPPC64())
152     return &PPC::G8RCRegClass;
153   return &PPC::GPRCRegClass;
154 }
155 
156 const MCPhysReg*
157 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
158   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
159   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) {
160     if (!TM.isPPC64() && Subtarget.isAIXABI())
161       report_fatal_error("AnyReg unimplemented on 32-bit AIX.");
162     if (Subtarget.hasVSX())
163       return CSR_64_AllRegs_VSX_SaveList;
164     if (Subtarget.hasAltivec())
165       return CSR_64_AllRegs_Altivec_SaveList;
166     return CSR_64_AllRegs_SaveList;
167   }
168 
169   // On PPC64, we might need to save r2 (but only if it is not reserved).
170   // We do not need to treat R2 as callee-saved when using PC-Relative calls
171   // because any direct uses of R2 will cause it to be reserved. If the function
172   // is a leaf or the only uses of R2 are implicit uses for calls, the calls
173   // will use the @notoc relocation which will cause this function to set the
174   // st_other bit to 1, thereby communicating to its caller that it arbitrarily
175   // clobbers the TOC.
176   bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) &&
177                 !Subtarget.isUsingPCRelativeCalls();
178 
179   // Cold calling convention CSRs.
180   if (MF->getFunction().getCallingConv() == CallingConv::Cold) {
181     if (Subtarget.isAIXABI())
182       report_fatal_error("Cold calling unimplemented on AIX.");
183     if (TM.isPPC64()) {
184       if (Subtarget.hasAltivec())
185         return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
186                       : CSR_SVR64_ColdCC_Altivec_SaveList;
187       return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
188                     : CSR_SVR64_ColdCC_SaveList;
189     }
190     // 32-bit targets.
191     if (Subtarget.hasAltivec())
192       return CSR_SVR32_ColdCC_Altivec_SaveList;
193     else if (Subtarget.hasSPE())
194       return CSR_SVR32_ColdCC_SPE_SaveList;
195     return CSR_SVR32_ColdCC_SaveList;
196   }
197   // Standard calling convention CSRs.
198   if (TM.isPPC64()) {
199     if (Subtarget.hasAltivec() &&
200         (!Subtarget.isAIXABI() || TM.getAIXExtendedAltivecABI())) {
201       return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList
202                     : CSR_PPC64_Altivec_SaveList;
203     }
204     return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList;
205   }
206   // 32-bit targets.
207   if (Subtarget.isAIXABI()) {
208     if (Subtarget.hasAltivec())
209       return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_Altivec_SaveList
210                                            : CSR_AIX32_SaveList;
211     return CSR_AIX32_SaveList;
212   }
213   if (Subtarget.hasAltivec())
214     return CSR_SVR432_Altivec_SaveList;
215   else if (Subtarget.hasSPE())
216     return CSR_SVR432_SPE_SaveList;
217   return CSR_SVR432_SaveList;
218 }
219 
220 const uint32_t *
221 PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
222                                       CallingConv::ID CC) const {
223   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
224   if (CC == CallingConv::AnyReg) {
225     if (Subtarget.hasVSX())
226       return CSR_64_AllRegs_VSX_RegMask;
227     if (Subtarget.hasAltivec())
228       return CSR_64_AllRegs_Altivec_RegMask;
229     return CSR_64_AllRegs_RegMask;
230   }
231 
232   if (Subtarget.isAIXABI()) {
233     return TM.isPPC64()
234                ? ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
235                       ? CSR_PPC64_Altivec_RegMask
236                       : CSR_PPC64_RegMask)
237                : ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
238                       ? CSR_AIX32_Altivec_RegMask
239                       : CSR_AIX32_RegMask);
240   }
241 
242   if (CC == CallingConv::Cold) {
243     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
244                                                   : CSR_SVR64_ColdCC_RegMask)
245                         : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
246                                                   : (Subtarget.hasSPE()
247                                                   ? CSR_SVR32_ColdCC_SPE_RegMask
248                                                   : CSR_SVR32_ColdCC_RegMask));
249   }
250 
251   return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask
252                                                 : CSR_PPC64_RegMask)
253                       : (Subtarget.hasAltivec()
254                              ? CSR_SVR432_Altivec_RegMask
255                              : (Subtarget.hasSPE() ? CSR_SVR432_SPE_RegMask
256                                                    : CSR_SVR432_RegMask));
257 }
258 
259 const uint32_t*
260 PPCRegisterInfo::getNoPreservedMask() const {
261   return CSR_NoRegs_RegMask;
262 }
263 
264 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
265   for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
266     Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
267 }
268 
269 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
270   BitVector Reserved(getNumRegs());
271   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
272   const PPCFrameLowering *TFI = getFrameLowering(MF);
273 
274   // The ZERO register is not really a register, but the representation of r0
275   // when used in instructions that treat r0 as the constant 0.
276   markSuperRegs(Reserved, PPC::ZERO);
277 
278   // The FP register is also not really a register, but is the representation
279   // of the frame pointer register used by ISD::FRAMEADDR.
280   markSuperRegs(Reserved, PPC::FP);
281 
282   // The BP register is also not really a register, but is the representation
283   // of the base pointer register used by setjmp.
284   markSuperRegs(Reserved, PPC::BP);
285 
286   // The counter registers must be reserved so that counter-based loops can
287   // be correctly formed (and the mtctr instructions are not DCE'd).
288   markSuperRegs(Reserved, PPC::CTR);
289   markSuperRegs(Reserved, PPC::CTR8);
290 
291   markSuperRegs(Reserved, PPC::R1);
292   markSuperRegs(Reserved, PPC::LR);
293   markSuperRegs(Reserved, PPC::LR8);
294   markSuperRegs(Reserved, PPC::RM);
295 
296   markSuperRegs(Reserved, PPC::VRSAVE);
297 
298   // The SVR4 ABI reserves r2 and r13
299   if (Subtarget.isSVR4ABI()) {
300     // We only reserve r2 if we need to use the TOC pointer. If we have no
301     // explicit uses of the TOC pointer (meaning we're a leaf function with
302     // no constant-pool loads, etc.) and we have no potential uses inside an
303     // inline asm block, then we can treat r2 has an ordinary callee-saved
304     // register.
305     const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
306     if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
307       markSuperRegs(Reserved, PPC::R2);  // System-reserved register
308     markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
309   }
310 
311   // Always reserve r2 on AIX for now.
312   // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions.
313   if (Subtarget.isAIXABI())
314     markSuperRegs(Reserved, PPC::R2);  // System-reserved register
315 
316   // On PPC64, r13 is the thread pointer. Never allocate this register.
317   if (TM.isPPC64())
318     markSuperRegs(Reserved, PPC::R13);
319 
320   if (TFI->needsFP(MF))
321     markSuperRegs(Reserved, PPC::R31);
322 
323   bool IsPositionIndependent = TM.isPositionIndependent();
324   if (hasBasePointer(MF)) {
325     if (Subtarget.is32BitELFABI() && IsPositionIndependent)
326       markSuperRegs(Reserved, PPC::R29);
327     else
328       markSuperRegs(Reserved, PPC::R30);
329   }
330 
331   if (Subtarget.is32BitELFABI() && IsPositionIndependent)
332     markSuperRegs(Reserved, PPC::R30);
333 
334   // Reserve Altivec registers when Altivec is unavailable.
335   if (!Subtarget.hasAltivec())
336     for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
337          IE = PPC::VRRCRegClass.end(); I != IE; ++I)
338       markSuperRegs(Reserved, *I);
339 
340   if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
341       !TM.getAIXExtendedAltivecABI()) {
342     //  In the AIX default Altivec ABI, vector registers VR20-VR31 are reserved
343     //  and cannot be used.
344     for (auto Reg : CSR_Altivec_SaveList) {
345       if (Reg == 0)
346         break;
347       markSuperRegs(Reserved, Reg);
348     }
349   }
350 
351   assert(checkAllSuperRegsMarked(Reserved));
352   return Reserved;
353 }
354 
355 bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const {
356   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
357   const PPCInstrInfo *InstrInfo =  Subtarget.getInstrInfo();
358   const MachineFrameInfo &MFI = MF.getFrameInfo();
359   const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
360 
361   // If the callee saved info is invalid we have to default to true for safety.
362   if (!MFI.isCalleeSavedInfoValid())
363     return true;
364 
365   // We will require the use of X-Forms because the frame is larger than what
366   // can be represented in signed 16 bits that fit in the immediate of a D-Form.
367   // If we need an X-Form then we need a register to store the address offset.
368   unsigned FrameSize = MFI.getStackSize();
369   // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
370   if (FrameSize & ~0x7FFF)
371     return true;
372 
373   // The callee saved info is valid so it can be traversed.
374   // Checking for registers that need saving that do not have load or store
375   // forms where the address offset is an immediate.
376   for (unsigned i = 0; i < Info.size(); i++) {
377     int FrIdx = Info[i].getFrameIdx();
378     unsigned Reg = Info[i].getReg();
379 
380     const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg);
381     unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC);
382     if (!MFI.isFixedObjectIndex(FrIdx)) {
383       // This is not a fixed object. If it requires alignment then we may still
384       // need to use the XForm.
385       if (offsetMinAlignForOpcode(Opcode) > 1)
386         return true;
387     }
388 
389     // This is eiher:
390     // 1) A fixed frame index object which we know are aligned so
391     // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
392     // need to consider the alignment here.
393     // 2) A not fixed object but in that case we now know that the min required
394     // alignment is no more than 1 based on the previous check.
395     if (InstrInfo->isXFormMemOp(Opcode))
396       return true;
397   }
398   return false;
399 }
400 
401 bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg,
402                                                const MachineFunction &MF) const {
403   assert(Register::isPhysicalRegister(PhysReg));
404   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
405   const MachineFrameInfo &MFI = MF.getFrameInfo();
406   if (!TM.isPPC64())
407     return false;
408 
409   if (!Subtarget.isSVR4ABI())
410     return false;
411   if (PhysReg == PPC::X2)
412     // X2 is guaranteed to be preserved within a function if it is reserved.
413     // The reason it's reserved is that it's the TOC pointer (and the function
414     // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
415     // with no TOC access), we can't claim that it is preserved.
416     return (getReservedRegs(MF).test(PPC::X2));
417   if (StackPtrConst && (PhysReg == PPC::X1) && !MFI.hasVarSizedObjects()
418       && !MFI.hasOpaqueSPAdjustment())
419     // The value of the stack pointer does not change within a function after
420     // the prologue and before the epilogue if there are no dynamic allocations
421     // and no inline asm which clobbers X1.
422     return true;
423   return false;
424 }
425 
426 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
427                                               MachineFunction &MF) const {
428   const PPCFrameLowering *TFI = getFrameLowering(MF);
429   const unsigned DefaultSafety = 1;
430 
431   switch (RC->getID()) {
432   default:
433     return 0;
434   case PPC::G8RC_NOX0RegClassID:
435   case PPC::GPRC_NOR0RegClassID:
436   case PPC::SPERCRegClassID:
437   case PPC::G8RCRegClassID:
438   case PPC::GPRCRegClassID: {
439     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
440     return 32 - FP - DefaultSafety;
441   }
442   case PPC::F4RCRegClassID:
443   case PPC::F8RCRegClassID:
444   case PPC::VSLRCRegClassID:
445     return 32 - DefaultSafety;
446   case PPC::VFRCRegClassID:
447   case PPC::VRRCRegClassID: {
448     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
449     // Vector registers VR20-VR31 are reserved and cannot be used in the default
450     // Altivec ABI on AIX.
451     if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
452       return 20 - DefaultSafety;
453   }
454     return 32 - DefaultSafety;
455   case PPC::VSFRCRegClassID:
456   case PPC::VSSRCRegClassID:
457   case PPC::VSRCRegClassID: {
458     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
459     if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
460       // Vector registers VR20-VR31 are reserved and cannot be used in the
461       // default Altivec ABI on AIX.
462       return 52 - DefaultSafety;
463   }
464     return 64 - DefaultSafety;
465   case PPC::CRRCRegClassID:
466     return 8 - DefaultSafety;
467   }
468 }
469 
470 const TargetRegisterClass *
471 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
472                                            const MachineFunction &MF) const {
473   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
474   if (Subtarget.hasVSX()) {
475     // With VSX, we can inflate various sub-register classes to the full VSX
476     // register set.
477 
478     // For Power9 we allow the user to enable GPR to vector spills.
479     // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
480     // support to spill GPRC.
481     if (TM.isELFv2ABI() || Subtarget.isAIXABI()) {
482       if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
483           RC == &PPC::G8RCRegClass) {
484         InflateGP8RC++;
485         return &PPC::SPILLTOVSRRCRegClass;
486       }
487       if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
488         InflateGPRC++;
489     }
490     if (RC == &PPC::F8RCRegClass)
491       return &PPC::VSFRCRegClass;
492     else if (RC == &PPC::VRRCRegClass)
493       return &PPC::VSRCRegClass;
494     else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector())
495       return &PPC::VSSRCRegClass;
496   }
497 
498   return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
499 }
500 
501 //===----------------------------------------------------------------------===//
502 // Stack Frame Processing methods
503 //===----------------------------------------------------------------------===//
504 
505 /// lowerDynamicAlloc - Generate the code for allocating an object in the
506 /// current frame.  The sequence of code will be in the general form
507 ///
508 ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
509 ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
510 ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
511 ///
512 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
513   // Get the instruction.
514   MachineInstr &MI = *II;
515   // Get the instruction's basic block.
516   MachineBasicBlock &MBB = *MI.getParent();
517   // Get the basic block's function.
518   MachineFunction &MF = *MBB.getParent();
519   // Get the frame info.
520   MachineFrameInfo &MFI = MF.getFrameInfo();
521   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
522   // Get the instruction info.
523   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
524   // Determine whether 64-bit pointers are used.
525   bool LP64 = TM.isPPC64();
526   DebugLoc dl = MI.getDebugLoc();
527 
528   // Get the maximum call stack size.
529   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
530   Align MaxAlign = MFI.getMaxAlign();
531   assert(isAligned(MaxAlign, maxCallFrameSize) &&
532          "Maximum call-frame size not sufficiently aligned");
533   (void)MaxAlign;
534 
535   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
536   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
537   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
538   bool KillNegSizeReg = MI.getOperand(1).isKill();
539   Register NegSizeReg = MI.getOperand(1).getReg();
540 
541   prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg);
542   // Grow the stack and update the stack pointer link, then determine the
543   // address of new allocated space.
544   if (LP64) {
545     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
546         .addReg(Reg, RegState::Kill)
547         .addReg(PPC::X1)
548         .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
549     BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
550         .addReg(PPC::X1)
551         .addImm(maxCallFrameSize);
552   } else {
553     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
554         .addReg(Reg, RegState::Kill)
555         .addReg(PPC::R1)
556         .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
557     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
558         .addReg(PPC::R1)
559         .addImm(maxCallFrameSize);
560   }
561 
562   // Discard the DYNALLOC instruction.
563   MBB.erase(II);
564 }
565 
566 /// To accomplish dynamic stack allocation, we have to calculate exact size
567 /// subtracted from the stack pointer according alignment information and get
568 /// previous frame pointer.
569 void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II,
570                                            Register &NegSizeReg,
571                                            bool &KillNegSizeReg,
572                                            Register &FramePointer) const {
573   // Get the instruction.
574   MachineInstr &MI = *II;
575   // Get the instruction's basic block.
576   MachineBasicBlock &MBB = *MI.getParent();
577   // Get the basic block's function.
578   MachineFunction &MF = *MBB.getParent();
579   // Get the frame info.
580   MachineFrameInfo &MFI = MF.getFrameInfo();
581   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
582   // Get the instruction info.
583   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
584   // Determine whether 64-bit pointers are used.
585   bool LP64 = TM.isPPC64();
586   DebugLoc dl = MI.getDebugLoc();
587   // Get the total frame size.
588   unsigned FrameSize = MFI.getStackSize();
589 
590   // Get stack alignments.
591   const PPCFrameLowering *TFI = getFrameLowering(MF);
592   Align TargetAlign = TFI->getStackAlign();
593   Align MaxAlign = MFI.getMaxAlign();
594 
595   // Determine the previous frame's address.  If FrameSize can't be
596   // represented as 16 bits or we need special alignment, then we load the
597   // previous frame's address from 0(SP).  Why not do an addis of the hi?
598   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
599   // Constructing the constant and adding would take 3 instructions.
600   // Fortunately, a frame greater than 32K is rare.
601   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
602   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
603 
604   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
605     if (LP64)
606       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer)
607           .addReg(PPC::X31)
608           .addImm(FrameSize);
609     else
610       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer)
611           .addReg(PPC::R31)
612           .addImm(FrameSize);
613   } else if (LP64) {
614     BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer)
615         .addImm(0)
616         .addReg(PPC::X1);
617   } else {
618     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer)
619         .addImm(0)
620         .addReg(PPC::R1);
621   }
622   // Determine the actual NegSizeReg according to alignment info.
623   if (LP64) {
624     if (MaxAlign > TargetAlign) {
625       unsigned UnalNegSizeReg = NegSizeReg;
626       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
627 
628       // Unfortunately, there is no andi, only andi., and we can't insert that
629       // here because we might clobber cr0 while it is live.
630       BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
631           .addImm(~(MaxAlign.value() - 1));
632 
633       unsigned NegSizeReg1 = NegSizeReg;
634       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
635       BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
636           .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
637           .addReg(NegSizeReg1, RegState::Kill);
638       KillNegSizeReg = true;
639     }
640   } else {
641     if (MaxAlign > TargetAlign) {
642       unsigned UnalNegSizeReg = NegSizeReg;
643       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
644 
645       // Unfortunately, there is no andi, only andi., and we can't insert that
646       // here because we might clobber cr0 while it is live.
647       BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
648           .addImm(~(MaxAlign.value() - 1));
649 
650       unsigned NegSizeReg1 = NegSizeReg;
651       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
652       BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
653           .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
654           .addReg(NegSizeReg1, RegState::Kill);
655       KillNegSizeReg = true;
656     }
657   }
658 }
659 
660 void PPCRegisterInfo::lowerPrepareProbedAlloca(
661     MachineBasicBlock::iterator II) const {
662   MachineInstr &MI = *II;
663   // Get the instruction's basic block.
664   MachineBasicBlock &MBB = *MI.getParent();
665   // Get the basic block's function.
666   MachineFunction &MF = *MBB.getParent();
667   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
668   // Get the instruction info.
669   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
670   // Determine whether 64-bit pointers are used.
671   bool LP64 = TM.isPPC64();
672   DebugLoc dl = MI.getDebugLoc();
673   Register FramePointer = MI.getOperand(0).getReg();
674   const Register ActualNegSizeReg = MI.getOperand(1).getReg();
675   bool KillNegSizeReg = MI.getOperand(2).isKill();
676   Register NegSizeReg = MI.getOperand(2).getReg();
677   const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR);
678   // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg.
679   if (FramePointer == NegSizeReg) {
680     assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, "
681                              "NegSizeReg should be killed");
682     // FramePointer is clobbered earlier than the use of NegSizeReg in
683     // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid
684     // misuse.
685     BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
686         .addReg(NegSizeReg)
687         .addReg(NegSizeReg);
688     NegSizeReg = ActualNegSizeReg;
689     KillNegSizeReg = false;
690   }
691   prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer);
692   // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign >
693   // TargetAlign.
694   if (NegSizeReg != ActualNegSizeReg)
695     BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
696         .addReg(NegSizeReg)
697         .addReg(NegSizeReg);
698   MBB.erase(II);
699 }
700 
701 void PPCRegisterInfo::lowerDynamicAreaOffset(
702     MachineBasicBlock::iterator II) const {
703   // Get the instruction.
704   MachineInstr &MI = *II;
705   // Get the instruction's basic block.
706   MachineBasicBlock &MBB = *MI.getParent();
707   // Get the basic block's function.
708   MachineFunction &MF = *MBB.getParent();
709   // Get the frame info.
710   MachineFrameInfo &MFI = MF.getFrameInfo();
711   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
712   // Get the instruction info.
713   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
714 
715   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
716   bool is64Bit = TM.isPPC64();
717   DebugLoc dl = MI.getDebugLoc();
718   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
719           MI.getOperand(0).getReg())
720       .addImm(maxCallFrameSize);
721   MBB.erase(II);
722 }
723 
724 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
725 /// reserving a whole register (R0), we scrounge for one here. This generates
726 /// code like this:
727 ///
728 ///   mfcr rA                  ; Move the conditional register into GPR rA.
729 ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
730 ///   stw rA, FI               ; Store rA to the frame.
731 ///
732 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
733                                       unsigned FrameIndex) const {
734   // Get the instruction.
735   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
736   // Get the instruction's basic block.
737   MachineBasicBlock &MBB = *MI.getParent();
738   MachineFunction &MF = *MBB.getParent();
739   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
740   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
741   DebugLoc dl = MI.getDebugLoc();
742 
743   bool LP64 = TM.isPPC64();
744   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
745   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
746 
747   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
748   Register SrcReg = MI.getOperand(0).getReg();
749 
750   // We need to store the CR in the low 4-bits of the saved value. First, issue
751   // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
752   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
753       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
754 
755   // If the saved register wasn't CR0, shift the bits left so that they are in
756   // CR0's slot.
757   if (SrcReg != PPC::CR0) {
758     Register Reg1 = Reg;
759     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
760 
761     // rlwinm rA, rA, ShiftBits, 0, 31.
762     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
763       .addReg(Reg1, RegState::Kill)
764       .addImm(getEncodingValue(SrcReg) * 4)
765       .addImm(0)
766       .addImm(31);
767   }
768 
769   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
770                     .addReg(Reg, RegState::Kill),
771                     FrameIndex);
772 
773   // Discard the pseudo instruction.
774   MBB.erase(II);
775 }
776 
777 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
778                                       unsigned FrameIndex) const {
779   // Get the instruction.
780   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
781   // Get the instruction's basic block.
782   MachineBasicBlock &MBB = *MI.getParent();
783   MachineFunction &MF = *MBB.getParent();
784   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
785   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
786   DebugLoc dl = MI.getDebugLoc();
787 
788   bool LP64 = TM.isPPC64();
789   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
790   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
791 
792   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
793   Register DestReg = MI.getOperand(0).getReg();
794   assert(MI.definesRegister(DestReg) &&
795     "RESTORE_CR does not define its destination");
796 
797   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
798                               Reg), FrameIndex);
799 
800   // If the reloaded register isn't CR0, shift the bits right so that they are
801   // in the right CR's slot.
802   if (DestReg != PPC::CR0) {
803     Register Reg1 = Reg;
804     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
805 
806     unsigned ShiftBits = getEncodingValue(DestReg)*4;
807     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
808     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
809              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
810              .addImm(31);
811   }
812 
813   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
814              .addReg(Reg, RegState::Kill);
815 
816   // Discard the pseudo instruction.
817   MBB.erase(II);
818 }
819 
820 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
821                                          unsigned FrameIndex) const {
822   // Get the instruction.
823   MachineInstr &MI = *II;       // ; SPILL_CRBIT <SrcReg>, <offset>
824   // Get the instruction's basic block.
825   MachineBasicBlock &MBB = *MI.getParent();
826   MachineFunction &MF = *MBB.getParent();
827   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
828   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
829   const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
830   DebugLoc dl = MI.getDebugLoc();
831 
832   bool LP64 = TM.isPPC64();
833   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
834   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
835 
836   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
837   Register SrcReg = MI.getOperand(0).getReg();
838 
839   // Search up the BB to find the definition of the CR bit.
840   MachineBasicBlock::reverse_iterator Ins = MI;
841   MachineBasicBlock::reverse_iterator Rend = MBB.rend();
842   ++Ins;
843   unsigned CRBitSpillDistance = 0;
844   bool SeenUse = false;
845   for (; Ins != Rend; ++Ins) {
846     // Definition found.
847     if (Ins->modifiesRegister(SrcReg, TRI))
848       break;
849     // Use found.
850     if (Ins->readsRegister(SrcReg, TRI))
851       SeenUse = true;
852     // Unable to find CR bit definition within maximum search distance.
853     if (CRBitSpillDistance == MaxCRBitSpillDist) {
854       Ins = MI;
855       break;
856     }
857     // Skip debug instructions when counting CR bit spill distance.
858     if (!Ins->isDebugInstr())
859       CRBitSpillDistance++;
860   }
861 
862   // Unable to find the definition of the CR bit in the MBB.
863   if (Ins == MBB.rend())
864     Ins = MI;
865 
866   bool SpillsKnownBit = false;
867   // There is no need to extract the CR bit if its value is already known.
868   switch (Ins->getOpcode()) {
869   case PPC::CRUNSET:
870     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
871       .addImm(0);
872     SpillsKnownBit = true;
873     break;
874   case PPC::CRSET:
875     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
876       .addImm(-32768);
877     SpillsKnownBit = true;
878     break;
879   default:
880     // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all
881     // bits (specifically, it produces a -1 if the CR bit is set). Ultimately,
882     // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit
883     // register), and SETNBC will set this.
884     if (Subtarget.isISA3_1()) {
885       BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg)
886           .addReg(SrcReg, RegState::Undef);
887       break;
888     }
889 
890     // On Power9, we can use SETB to extract the LT bit. This only works for
891     // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value
892     // of the bit we care about (32-bit sign bit) will be set to the value of
893     // the LT bit (regardless of the other bits in the CR field).
894     if (Subtarget.isISA3_0()) {
895       if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT ||
896           SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT ||
897           SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT ||
898           SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) {
899         BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg)
900           .addReg(getCRFromCRBit(SrcReg), RegState::Undef);
901         break;
902       }
903     }
904 
905     // We need to move the CR field that contains the CR bit we are spilling.
906     // The super register may not be explicitly defined (i.e. it can be defined
907     // by a CR-logical that only defines the subreg) so we state that the CR
908     // field is undef. Also, in order to preserve the kill flag on the CR bit,
909     // we add it as an implicit use.
910     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
911       .addReg(getCRFromCRBit(SrcReg), RegState::Undef)
912       .addReg(SrcReg,
913               RegState::Implicit | getKillRegState(MI.getOperand(0).isKill()));
914 
915     // If the saved register wasn't CR0LT, shift the bits left so that the bit
916     // to store is the first one. Mask all but that bit.
917     Register Reg1 = Reg;
918     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
919 
920     // rlwinm rA, rA, ShiftBits, 0, 0.
921     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
922       .addReg(Reg1, RegState::Kill)
923       .addImm(getEncodingValue(SrcReg))
924       .addImm(0).addImm(0);
925   }
926   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
927                     .addReg(Reg, RegState::Kill),
928                     FrameIndex);
929 
930   bool KillsCRBit = MI.killsRegister(SrcReg, TRI);
931   // Discard the pseudo instruction.
932   MBB.erase(II);
933   if (SpillsKnownBit && KillsCRBit && !SeenUse) {
934     Ins->setDesc(TII.get(PPC::UNENCODED_NOP));
935     Ins->RemoveOperand(0);
936   }
937 }
938 
939 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
940                                       unsigned FrameIndex) const {
941   // Get the instruction.
942   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CRBIT <offset>
943   // Get the instruction's basic block.
944   MachineBasicBlock &MBB = *MI.getParent();
945   MachineFunction &MF = *MBB.getParent();
946   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
947   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
948   DebugLoc dl = MI.getDebugLoc();
949 
950   bool LP64 = TM.isPPC64();
951   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
952   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
953 
954   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
955   Register DestReg = MI.getOperand(0).getReg();
956   assert(MI.definesRegister(DestReg) &&
957     "RESTORE_CRBIT does not define its destination");
958 
959   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
960                               Reg), FrameIndex);
961 
962   BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
963 
964   Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
965   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
966           .addReg(getCRFromCRBit(DestReg));
967 
968   unsigned ShiftBits = getEncodingValue(DestReg);
969   // rlwimi r11, r10, 32-ShiftBits, ..., ...
970   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
971       .addReg(RegO, RegState::Kill)
972       .addReg(Reg, RegState::Kill)
973       .addImm(ShiftBits ? 32 - ShiftBits : 0)
974       .addImm(ShiftBits)
975       .addImm(ShiftBits);
976 
977   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
978           getCRFromCRBit(DestReg))
979       .addReg(RegO, RegState::Kill)
980       // Make sure we have a use dependency all the way through this
981       // sequence of instructions. We can't have the other bits in the CR
982       // modified in between the mfocrf and the mtocrf.
983       .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
984 
985   // Discard the pseudo instruction.
986   MBB.erase(II);
987 }
988 
989 void PPCRegisterInfo::emitAccCopyInfo(MachineBasicBlock &MBB,
990                                       MCRegister DestReg, MCRegister SrcReg) {
991 #ifdef NDEBUG
992   return;
993 #else
994   if (ReportAccMoves) {
995     std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc";
996     std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc";
997     dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n";
998     MBB.dump();
999   }
1000 #endif
1001 }
1002 
1003 static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed,
1004                                     bool IsRestore) {
1005 #ifdef NDEBUG
1006   return;
1007 #else
1008   if (ReportAccMoves) {
1009     dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register "
1010            << (IsRestore ? "restore" : "spill") << ":\n";
1011     MBB.dump();
1012   }
1013 #endif
1014 }
1015 
1016 /// lowerACCSpilling - Generate the code for spilling the accumulator register.
1017 /// Similarly to other spills/reloads that use pseudo-ops, we do not actually
1018 /// eliminate the FrameIndex here nor compute the stack offset. We simply
1019 /// create a real instruction with an FI and rely on eliminateFrameIndex to
1020 /// handle the FI elimination.
1021 void PPCRegisterInfo::lowerACCSpilling(MachineBasicBlock::iterator II,
1022                                        unsigned FrameIndex) const {
1023   MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset>
1024   MachineBasicBlock &MBB = *MI.getParent();
1025   MachineFunction &MF = *MBB.getParent();
1026   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1027   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1028   DebugLoc DL = MI.getDebugLoc();
1029   Register SrcReg = MI.getOperand(0).getReg();
1030   bool IsKilled = MI.getOperand(0).isKill();
1031 
1032   bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg);
1033   Register Reg =
1034       PPC::VSRp0 + (SrcReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1035   bool IsLittleEndian = Subtarget.isLittleEndian();
1036 
1037   emitAccSpillRestoreInfo(MBB, IsPrimed, false);
1038 
1039   // De-prime the register being spilled, create two stores for the pair
1040   // subregisters accounting for endianness and then re-prime the register if
1041   // it isn't killed.  This uses the Offset parameter to addFrameReference() to
1042   // adjust the offset of the store that is within the 64-byte stack slot.
1043   if (IsPrimed)
1044     BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg);
1045   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1046                         .addReg(Reg, getKillRegState(IsKilled)),
1047                     FrameIndex, IsLittleEndian ? 32 : 0);
1048   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1049                         .addReg(Reg + 1, getKillRegState(IsKilled)),
1050                     FrameIndex, IsLittleEndian ? 0 : 32);
1051   if (IsPrimed && !IsKilled)
1052     BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg);
1053 
1054   // Discard the pseudo instruction.
1055   MBB.erase(II);
1056 }
1057 
1058 /// lowerACCRestore - Generate the code to restore the accumulator register.
1059 void PPCRegisterInfo::lowerACCRestore(MachineBasicBlock::iterator II,
1060                                       unsigned FrameIndex) const {
1061   MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset>
1062   MachineBasicBlock &MBB = *MI.getParent();
1063   MachineFunction &MF = *MBB.getParent();
1064   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1065   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1066   DebugLoc DL = MI.getDebugLoc();
1067 
1068   Register DestReg = MI.getOperand(0).getReg();
1069   assert(MI.definesRegister(DestReg) &&
1070          "RESTORE_ACC does not define its destination");
1071 
1072   bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg);
1073   Register Reg =
1074       PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1075   bool IsLittleEndian = Subtarget.isLittleEndian();
1076 
1077   emitAccSpillRestoreInfo(MBB, IsPrimed, true);
1078 
1079   // Create two loads for the pair subregisters accounting for endianness and
1080   // then prime the accumulator register being restored.
1081   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg),
1082                     FrameIndex, IsLittleEndian ? 32 : 0);
1083   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1),
1084                     FrameIndex, IsLittleEndian ? 0 : 32);
1085   if (IsPrimed)
1086     BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg);
1087 
1088   // Discard the pseudo instruction.
1089   MBB.erase(II);
1090 }
1091 
1092 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
1093                                            Register Reg, int &FrameIdx) const {
1094   // For the nonvolatile condition registers (CR2, CR3, CR4) return true to
1095   // prevent allocating an additional frame slot.
1096   // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8,
1097   // for 32-bit AIX the CR save area is in the linkage area at SP+4.
1098   // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos
1099   // valid.
1100   // For 32-bit ELF, we have previously created the stack slot if needed, so
1101   // return its FrameIdx.
1102   if (PPC::CR2 <= Reg && Reg <= PPC::CR4) {
1103     FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex();
1104     return true;
1105   }
1106   return false;
1107 }
1108 
1109 // If the offset must be a multiple of some value, return what that value is.
1110 static unsigned offsetMinAlignForOpcode(unsigned OpC) {
1111   switch (OpC) {
1112   default:
1113     return 1;
1114   case PPC::LWA:
1115   case PPC::LWA_32:
1116   case PPC::LD:
1117   case PPC::LDU:
1118   case PPC::STD:
1119   case PPC::STDU:
1120   case PPC::DFLOADf32:
1121   case PPC::DFLOADf64:
1122   case PPC::DFSTOREf32:
1123   case PPC::DFSTOREf64:
1124   case PPC::LXSD:
1125   case PPC::LXSSP:
1126   case PPC::STXSD:
1127   case PPC::STXSSP:
1128     return 4;
1129   case PPC::EVLDD:
1130   case PPC::EVSTDD:
1131     return 8;
1132   case PPC::LXV:
1133   case PPC::STXV:
1134     return 16;
1135   }
1136 }
1137 
1138 // If the offset must be a multiple of some value, return what that value is.
1139 static unsigned offsetMinAlign(const MachineInstr &MI) {
1140   unsigned OpC = MI.getOpcode();
1141   return offsetMinAlignForOpcode(OpC);
1142 }
1143 
1144 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
1145 static unsigned getOffsetONFromFION(const MachineInstr &MI,
1146                                     unsigned FIOperandNum) {
1147   // Take into account whether it's an add or mem instruction
1148   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
1149   if (MI.isInlineAsm())
1150     OffsetOperandNo = FIOperandNum - 1;
1151   else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
1152            MI.getOpcode() == TargetOpcode::PATCHPOINT)
1153     OffsetOperandNo = FIOperandNum + 1;
1154 
1155   return OffsetOperandNo;
1156 }
1157 
1158 void
1159 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1160                                      int SPAdj, unsigned FIOperandNum,
1161                                      RegScavenger *RS) const {
1162   assert(SPAdj == 0 && "Unexpected");
1163 
1164   // Get the instruction.
1165   MachineInstr &MI = *II;
1166   // Get the instruction's basic block.
1167   MachineBasicBlock &MBB = *MI.getParent();
1168   // Get the basic block's function.
1169   MachineFunction &MF = *MBB.getParent();
1170   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1171   // Get the instruction info.
1172   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1173   // Get the frame info.
1174   MachineFrameInfo &MFI = MF.getFrameInfo();
1175   DebugLoc dl = MI.getDebugLoc();
1176 
1177   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1178 
1179   // Get the frame index.
1180   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
1181 
1182   // Get the frame pointer save index.  Users of this index are primarily
1183   // DYNALLOC instructions.
1184   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1185   int FPSI = FI->getFramePointerSaveIndex();
1186   // Get the instruction opcode.
1187   unsigned OpC = MI.getOpcode();
1188 
1189   if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
1190     lowerDynamicAreaOffset(II);
1191     return;
1192   }
1193 
1194   // Special case for dynamic alloca.
1195   if (FPSI && FrameIndex == FPSI &&
1196       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
1197     lowerDynamicAlloc(II);
1198     return;
1199   }
1200 
1201   if (FPSI && FrameIndex == FPSI &&
1202       (OpC == PPC::PREPARE_PROBED_ALLOCA_64 ||
1203        OpC == PPC::PREPARE_PROBED_ALLOCA_32 ||
1204        OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64 ||
1205        OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32)) {
1206     lowerPrepareProbedAlloca(II);
1207     return;
1208   }
1209 
1210   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
1211   if (OpC == PPC::SPILL_CR) {
1212     lowerCRSpilling(II, FrameIndex);
1213     return;
1214   } else if (OpC == PPC::RESTORE_CR) {
1215     lowerCRRestore(II, FrameIndex);
1216     return;
1217   } else if (OpC == PPC::SPILL_CRBIT) {
1218     lowerCRBitSpilling(II, FrameIndex);
1219     return;
1220   } else if (OpC == PPC::RESTORE_CRBIT) {
1221     lowerCRBitRestore(II, FrameIndex);
1222     return;
1223   } else if (OpC == PPC::SPILL_ACC || OpC == PPC::SPILL_UACC) {
1224     lowerACCSpilling(II, FrameIndex);
1225     return;
1226   } else if (OpC == PPC::RESTORE_ACC || OpC == PPC::RESTORE_UACC) {
1227     lowerACCRestore(II, FrameIndex);
1228     return;
1229   }
1230 
1231   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
1232   MI.getOperand(FIOperandNum).ChangeToRegister(
1233     FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
1234 
1235   // If the instruction is not present in ImmToIdxMap, then it has no immediate
1236   // form (and must be r+r).
1237   bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
1238                    OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
1239 
1240   // Now add the frame object offset to the offset from r1.
1241   int Offset = MFI.getObjectOffset(FrameIndex);
1242   Offset += MI.getOperand(OffsetOperandNo).getImm();
1243 
1244   // If we're not using a Frame Pointer that has been set to the value of the
1245   // SP before having the stack size subtracted from it, then add the stack size
1246   // to Offset to get the correct offset.
1247   // Naked functions have stack size 0, although getStackSize may not reflect
1248   // that because we didn't call all the pieces that compute it for naked
1249   // functions.
1250   if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
1251     if (!(hasBasePointer(MF) && FrameIndex < 0))
1252       Offset += MFI.getStackSize();
1253   }
1254 
1255   // If we can, encode the offset directly into the instruction.  If this is a
1256   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
1257   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
1258   // clear can be encoded.  This is extremely uncommon, because normally you
1259   // only "std" to a stack slot that is at least 4-byte aligned, but it can
1260   // happen in invalid code.
1261   assert(OpC != PPC::DBG_VALUE &&
1262          "This should be handled in a target-independent way");
1263   bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ?
1264                             isUInt<8>(Offset) :
1265                             isInt<16>(Offset);
1266   if (!noImmForm && ((OffsetFitsMnemonic &&
1267                       ((Offset % offsetMinAlign(MI)) == 0)) ||
1268                      OpC == TargetOpcode::STACKMAP ||
1269                      OpC == TargetOpcode::PATCHPOINT)) {
1270     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1271     return;
1272   }
1273 
1274   // The offset doesn't fit into a single register, scavenge one to build the
1275   // offset in.
1276 
1277   bool is64Bit = TM.isPPC64();
1278   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1279   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1280   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
1281   Register SRegHi = MF.getRegInfo().createVirtualRegister(RC),
1282            SReg = MF.getRegInfo().createVirtualRegister(RC);
1283 
1284   // Insert a set of rA with the full offset value before the ld, st, or add
1285   if (isInt<16>(Offset))
1286     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
1287       .addImm(Offset);
1288   else {
1289     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
1290       .addImm(Offset >> 16);
1291     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
1292       .addReg(SRegHi, RegState::Kill)
1293       .addImm(Offset);
1294   }
1295 
1296   // Convert into indexed form of the instruction:
1297   //
1298   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
1299   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
1300   unsigned OperandBase;
1301 
1302   if (noImmForm)
1303     OperandBase = 1;
1304   else if (OpC != TargetOpcode::INLINEASM &&
1305            OpC != TargetOpcode::INLINEASM_BR) {
1306     assert(ImmToIdxMap.count(OpC) &&
1307            "No indexed form of load or store available!");
1308     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
1309     MI.setDesc(TII.get(NewOpcode));
1310     OperandBase = 1;
1311   } else {
1312     OperandBase = OffsetOperandNo;
1313   }
1314 
1315   Register StackReg = MI.getOperand(FIOperandNum).getReg();
1316   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
1317   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
1318 }
1319 
1320 Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
1321   const PPCFrameLowering *TFI = getFrameLowering(MF);
1322 
1323   if (!TM.isPPC64())
1324     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
1325   else
1326     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
1327 }
1328 
1329 Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
1330   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1331   if (!hasBasePointer(MF))
1332     return getFrameRegister(MF);
1333 
1334   if (TM.isPPC64())
1335     return PPC::X30;
1336 
1337   if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
1338     return PPC::R29;
1339 
1340   return PPC::R30;
1341 }
1342 
1343 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
1344   if (!EnableBasePointer)
1345     return false;
1346   if (AlwaysBasePointer)
1347     return true;
1348 
1349   // If we need to realign the stack, then the stack pointer can no longer
1350   // serve as an offset into the caller's stack space. As a result, we need a
1351   // base pointer.
1352   return hasStackRealignment(MF);
1353 }
1354 
1355 /// Returns true if the instruction's frame index
1356 /// reference would be better served by a base register other than FP
1357 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
1358 /// references it should create new base registers for.
1359 bool PPCRegisterInfo::
1360 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
1361   assert(Offset < 0 && "Local offset must be negative");
1362 
1363   // It's the load/store FI references that cause issues, as it can be difficult
1364   // to materialize the offset if it won't fit in the literal field. Estimate
1365   // based on the size of the local frame and some conservative assumptions
1366   // about the rest of the stack frame (note, this is pre-regalloc, so
1367   // we don't know everything for certain yet) whether this offset is likely
1368   // to be out of range of the immediate. Return true if so.
1369 
1370   // We only generate virtual base registers for loads and stores that have
1371   // an r+i form. Return false for everything else.
1372   unsigned OpC = MI->getOpcode();
1373   if (!ImmToIdxMap.count(OpC))
1374     return false;
1375 
1376   // Don't generate a new virtual base register just to add zero to it.
1377   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
1378       MI->getOperand(2).getImm() == 0)
1379     return false;
1380 
1381   MachineBasicBlock &MBB = *MI->getParent();
1382   MachineFunction &MF = *MBB.getParent();
1383   const PPCFrameLowering *TFI = getFrameLowering(MF);
1384   unsigned StackEst = TFI->determineFrameLayout(MF, true);
1385 
1386   // If we likely don't need a stack frame, then we probably don't need a
1387   // virtual base register either.
1388   if (!StackEst)
1389     return false;
1390 
1391   // Estimate an offset from the stack pointer.
1392   // The incoming offset is relating to the SP at the start of the function,
1393   // but when we access the local it'll be relative to the SP after local
1394   // allocation, so adjust our SP-relative offset by that allocation size.
1395   Offset += StackEst;
1396 
1397   // The frame pointer will point to the end of the stack, so estimate the
1398   // offset as the difference between the object offset and the FP location.
1399   return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
1400 }
1401 
1402 /// Insert defining instruction(s) for BaseReg to
1403 /// be a pointer to FrameIdx at the beginning of the basic block.
1404 Register PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
1405                                                        int FrameIdx,
1406                                                        int64_t Offset) const {
1407   unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
1408 
1409   MachineBasicBlock::iterator Ins = MBB->begin();
1410   DebugLoc DL;                  // Defaults to "unknown"
1411   if (Ins != MBB->end())
1412     DL = Ins->getDebugLoc();
1413 
1414   const MachineFunction &MF = *MBB->getParent();
1415   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1416   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1417   const MCInstrDesc &MCID = TII.get(ADDriOpc);
1418   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1419   const TargetRegisterClass *RC = getPointerRegClass(MF);
1420   Register BaseReg = MRI.createVirtualRegister(RC);
1421   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
1422 
1423   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
1424     .addFrameIndex(FrameIdx).addImm(Offset);
1425 
1426   return BaseReg;
1427 }
1428 
1429 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
1430                                         int64_t Offset) const {
1431   unsigned FIOperandNum = 0;
1432   while (!MI.getOperand(FIOperandNum).isFI()) {
1433     ++FIOperandNum;
1434     assert(FIOperandNum < MI.getNumOperands() &&
1435            "Instr doesn't have FrameIndex operand!");
1436   }
1437 
1438   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
1439   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1440   Offset += MI.getOperand(OffsetOperandNo).getImm();
1441   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1442 
1443   MachineBasicBlock &MBB = *MI.getParent();
1444   MachineFunction &MF = *MBB.getParent();
1445   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1446   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1447   const MCInstrDesc &MCID = MI.getDesc();
1448   MachineRegisterInfo &MRI = MF.getRegInfo();
1449   MRI.constrainRegClass(BaseReg,
1450                         TII.getRegClass(MCID, FIOperandNum, this, MF));
1451 }
1452 
1453 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1454                                          Register BaseReg,
1455                                          int64_t Offset) const {
1456   unsigned FIOperandNum = 0;
1457   while (!MI->getOperand(FIOperandNum).isFI()) {
1458     ++FIOperandNum;
1459     assert(FIOperandNum < MI->getNumOperands() &&
1460            "Instr doesn't have FrameIndex operand!");
1461   }
1462 
1463   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
1464   Offset += MI->getOperand(OffsetOperandNo).getImm();
1465 
1466   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
1467          MI->getOpcode() == TargetOpcode::STACKMAP ||
1468          MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1469          (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
1470 }
1471