xref: /freebsd/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the PowerPC implementation of the TargetRegisterInfo
100b57cec5SDimitry Andric // class.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "PPCRegisterInfo.h"
150b57cec5SDimitry Andric #include "PPCFrameLowering.h"
160b57cec5SDimitry Andric #include "PPCInstrBuilder.h"
170b57cec5SDimitry Andric #include "PPCMachineFunctionInfo.h"
180b57cec5SDimitry Andric #include "PPCSubtarget.h"
190b57cec5SDimitry Andric #include "PPCTargetMachine.h"
200b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
210b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
31*81ad6265SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
320b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
330b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
340b57cec5SDimitry Andric #include "llvm/IR/Function.h"
350b57cec5SDimitry Andric #include "llvm/IR/Type.h"
360b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
370b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
380b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
390b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
400b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
410b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
420b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
430b57cec5SDimitry Andric #include <cstdlib>
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric using namespace llvm;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric #define DEBUG_TYPE "reginfo"
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC
500b57cec5SDimitry Andric #include "PPCGenRegisterInfo.inc"
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass");
530b57cec5SDimitry Andric STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass");
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric static cl::opt<bool>
560b57cec5SDimitry Andric EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
570b57cec5SDimitry Andric          cl::desc("Enable use of a base pointer for complex stack frames"));
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric static cl::opt<bool>
600b57cec5SDimitry Andric AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
610b57cec5SDimitry Andric          cl::desc("Force the use of a base pointer in every function"));
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric static cl::opt<bool>
640b57cec5SDimitry Andric EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false),
650b57cec5SDimitry Andric          cl::desc("Enable spills from gpr to vsr rather than stack"));
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric static cl::opt<bool>
680b57cec5SDimitry Andric StackPtrConst("ppc-stack-ptr-caller-preserved",
690b57cec5SDimitry Andric                 cl::desc("Consider R1 caller preserved so stack saves of "
700b57cec5SDimitry Andric                          "caller preserved registers can be LICM candidates"),
710b57cec5SDimitry Andric                 cl::init(true), cl::Hidden);
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric static cl::opt<unsigned>
740b57cec5SDimitry Andric MaxCRBitSpillDist("ppc-max-crbit-spill-dist",
750b57cec5SDimitry Andric                   cl::desc("Maximum search distance for definition of CR bit "
760b57cec5SDimitry Andric                            "spill on ppc"),
770b57cec5SDimitry Andric                   cl::Hidden, cl::init(100));
780b57cec5SDimitry Andric 
79e8d8bef9SDimitry Andric // Copies/moves of physical accumulators are expensive operations
80e8d8bef9SDimitry Andric // that should be avoided whenever possible. MMA instructions are
81e8d8bef9SDimitry Andric // meant to be used in performance-sensitive computational kernels.
82e8d8bef9SDimitry Andric // This option is provided, at least for the time being, to give the
83e8d8bef9SDimitry Andric // user a tool to detect this expensive operation and either rework
84e8d8bef9SDimitry Andric // their code or report a compiler bug if that turns out to be the
85e8d8bef9SDimitry Andric // cause.
86e8d8bef9SDimitry Andric #ifndef NDEBUG
87e8d8bef9SDimitry Andric static cl::opt<bool>
88e8d8bef9SDimitry Andric ReportAccMoves("ppc-report-acc-moves",
89e8d8bef9SDimitry Andric                cl::desc("Emit information about accumulator register spills "
90e8d8bef9SDimitry Andric                         "and copies"),
91e8d8bef9SDimitry Andric                cl::Hidden, cl::init(false));
92e8d8bef9SDimitry Andric #endif
93e8d8bef9SDimitry Andric 
94*81ad6265SDimitry Andric extern cl::opt<bool> DisableAutoPairedVecSt;
95*81ad6265SDimitry Andric 
960b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC);
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
990b57cec5SDimitry Andric   : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
1000b57cec5SDimitry Andric                        TM.isPPC64() ? 0 : 1,
1010b57cec5SDimitry Andric                        TM.isPPC64() ? 0 : 1),
1020b57cec5SDimitry Andric     TM(TM) {
1030b57cec5SDimitry Andric   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
1040b57cec5SDimitry Andric   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
1050b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
1060b57cec5SDimitry Andric   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
1070b57cec5SDimitry Andric   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
1080b57cec5SDimitry Andric   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
1090b57cec5SDimitry Andric   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
1100b57cec5SDimitry Andric   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
1110b57cec5SDimitry Andric   ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   // 64-bit
1140b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
1150b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
1160b57cec5SDimitry Andric   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
1170b57cec5SDimitry Andric   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
1180b57cec5SDimitry Andric   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
119*81ad6265SDimitry Andric   ImmToIdxMap[PPC::LQ] = PPC::LQX_PSEUDO;
120*81ad6265SDimitry Andric   ImmToIdxMap[PPC::STQ] = PPC::STQX_PSEUDO;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   // VSX
1230b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX;
1240b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX;
1250b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX;
1260b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX;
1270b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX;
1280b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX;
1290b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXV] = PPC::LXVX;
1300b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXSD] = PPC::LXSDX;
1310b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX;
1320b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXV] = PPC::STXVX;
1330b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXSD] = PPC::STXSDX;
1340b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   // SPE
1370b57cec5SDimitry Andric   ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX;
1380b57cec5SDimitry Andric   ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX;
1390b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX;
1400b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX;
141fe6060f1SDimitry Andric 
142fe6060f1SDimitry Andric   // Power10
143349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLBZ]   = PPC::LBZX; ImmToIdxMap[PPC::PLBZ8]   = PPC::LBZX8;
144349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLHZ]   = PPC::LHZX; ImmToIdxMap[PPC::PLHZ8]   = PPC::LHZX8;
145349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLHA]   = PPC::LHAX; ImmToIdxMap[PPC::PLHA8]   = PPC::LHAX8;
146349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLWZ]   = PPC::LWZX; ImmToIdxMap[PPC::PLWZ8]   = PPC::LWZX8;
147349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLWA]   = PPC::LWAX; ImmToIdxMap[PPC::PLWA8]   = PPC::LWAX;
148349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLD]    = PPC::LDX;  ImmToIdxMap[PPC::PSTD]   = PPC::STDX;
149349cc55cSDimitry Andric 
150349cc55cSDimitry Andric   ImmToIdxMap[PPC::PSTB]   = PPC::STBX; ImmToIdxMap[PPC::PSTB8]   = PPC::STBX8;
151349cc55cSDimitry Andric   ImmToIdxMap[PPC::PSTH]   = PPC::STHX; ImmToIdxMap[PPC::PSTH8]   = PPC::STHX8;
152349cc55cSDimitry Andric   ImmToIdxMap[PPC::PSTW]   = PPC::STWX; ImmToIdxMap[PPC::PSTW8]   = PPC::STWX8;
153349cc55cSDimitry Andric 
154349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLFS]   = PPC::LFSX; ImmToIdxMap[PPC::PSTFS]   = PPC::STFSX;
155349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLFD]   = PPC::LFDX; ImmToIdxMap[PPC::PSTFD]   = PPC::STFDX;
156349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLXSSP] = PPC::LXSSPX; ImmToIdxMap[PPC::PSTXSSP] = PPC::STXSSPX;
157349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLXSD]  = PPC::LXSDX; ImmToIdxMap[PPC::PSTXSD]  = PPC::STXSDX;
158349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLXV]   = PPC::LXVX; ImmToIdxMap[PPC::PSTXV]  = PPC::STXVX;
159349cc55cSDimitry Andric 
160fe6060f1SDimitry Andric   ImmToIdxMap[PPC::LXVP]   = PPC::LXVPX;
161fe6060f1SDimitry Andric   ImmToIdxMap[PPC::STXVP]  = PPC::STXVPX;
162fe6060f1SDimitry Andric   ImmToIdxMap[PPC::PLXVP]  = PPC::LXVPX;
163fe6060f1SDimitry Andric   ImmToIdxMap[PPC::PSTXVP] = PPC::STXVPX;
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric /// getPointerRegClass - Return the register class to use to hold pointers.
1670b57cec5SDimitry Andric /// This is used for addressing modes.
1680b57cec5SDimitry Andric const TargetRegisterClass *
1690b57cec5SDimitry Andric PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
1700b57cec5SDimitry Andric                                                                        const {
1710b57cec5SDimitry Andric   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
1720b57cec5SDimitry Andric   // when it checks for ZERO folding.
1730b57cec5SDimitry Andric   if (Kind == 1) {
1740b57cec5SDimitry Andric     if (TM.isPPC64())
1750b57cec5SDimitry Andric       return &PPC::G8RC_NOX0RegClass;
1760b57cec5SDimitry Andric     return &PPC::GPRC_NOR0RegClass;
1770b57cec5SDimitry Andric   }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   if (TM.isPPC64())
1800b57cec5SDimitry Andric     return &PPC::G8RCRegClass;
1810b57cec5SDimitry Andric   return &PPC::GPRCRegClass;
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric const MCPhysReg*
1850b57cec5SDimitry Andric PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
1860b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
1870b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) {
1885ffd83dbSDimitry Andric     if (!TM.isPPC64() && Subtarget.isAIXABI())
1895ffd83dbSDimitry Andric       report_fatal_error("AnyReg unimplemented on 32-bit AIX.");
190fe6060f1SDimitry Andric     if (Subtarget.hasVSX()) {
191*81ad6265SDimitry Andric       if (Subtarget.pairedVectorMemops())
192*81ad6265SDimitry Andric         return CSR_64_AllRegs_VSRP_SaveList;
193fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
194fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_VSX_SaveList;
1950b57cec5SDimitry Andric       return CSR_64_AllRegs_VSX_SaveList;
196fe6060f1SDimitry Andric     }
197fe6060f1SDimitry Andric     if (Subtarget.hasAltivec()) {
198fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
199fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_Altivec_SaveList;
2000b57cec5SDimitry Andric       return CSR_64_AllRegs_Altivec_SaveList;
201fe6060f1SDimitry Andric     }
2020b57cec5SDimitry Andric     return CSR_64_AllRegs_SaveList;
2030b57cec5SDimitry Andric   }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   // On PPC64, we might need to save r2 (but only if it is not reserved).
2065ffd83dbSDimitry Andric   // We do not need to treat R2 as callee-saved when using PC-Relative calls
2075ffd83dbSDimitry Andric   // because any direct uses of R2 will cause it to be reserved. If the function
2085ffd83dbSDimitry Andric   // is a leaf or the only uses of R2 are implicit uses for calls, the calls
2095ffd83dbSDimitry Andric   // will use the @notoc relocation which will cause this function to set the
2105ffd83dbSDimitry Andric   // st_other bit to 1, thereby communicating to its caller that it arbitrarily
2115ffd83dbSDimitry Andric   // clobbers the TOC.
2125ffd83dbSDimitry Andric   bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) &&
2135ffd83dbSDimitry Andric                 !Subtarget.isUsingPCRelativeCalls();
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   // Cold calling convention CSRs.
2160b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::Cold) {
2175ffd83dbSDimitry Andric     if (Subtarget.isAIXABI())
2185ffd83dbSDimitry Andric       report_fatal_error("Cold calling unimplemented on AIX.");
2190b57cec5SDimitry Andric     if (TM.isPPC64()) {
220*81ad6265SDimitry Andric       if (Subtarget.pairedVectorMemops())
221*81ad6265SDimitry Andric         return SaveR2 ? CSR_SVR64_ColdCC_R2_VSRP_SaveList
222*81ad6265SDimitry Andric                       : CSR_SVR64_ColdCC_VSRP_SaveList;
2230b57cec5SDimitry Andric       if (Subtarget.hasAltivec())
2240b57cec5SDimitry Andric         return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
2250b57cec5SDimitry Andric                       : CSR_SVR64_ColdCC_Altivec_SaveList;
2260b57cec5SDimitry Andric       return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
2270b57cec5SDimitry Andric                     : CSR_SVR64_ColdCC_SaveList;
2280b57cec5SDimitry Andric     }
2290b57cec5SDimitry Andric     // 32-bit targets.
230*81ad6265SDimitry Andric     if (Subtarget.pairedVectorMemops())
231*81ad6265SDimitry Andric       return CSR_SVR32_ColdCC_VSRP_SaveList;
232*81ad6265SDimitry Andric     else if (Subtarget.hasAltivec())
2330b57cec5SDimitry Andric       return CSR_SVR32_ColdCC_Altivec_SaveList;
2340b57cec5SDimitry Andric     else if (Subtarget.hasSPE())
2350b57cec5SDimitry Andric       return CSR_SVR32_ColdCC_SPE_SaveList;
2360b57cec5SDimitry Andric     return CSR_SVR32_ColdCC_SaveList;
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric   // Standard calling convention CSRs.
2390b57cec5SDimitry Andric   if (TM.isPPC64()) {
240*81ad6265SDimitry Andric     if (Subtarget.pairedVectorMemops())
241*81ad6265SDimitry Andric       return SaveR2 ? CSR_SVR464_R2_VSRP_SaveList : CSR_SVR464_VSRP_SaveList;
242fe6060f1SDimitry Andric     if (Subtarget.hasAltivec() &&
243fe6060f1SDimitry Andric         (!Subtarget.isAIXABI() || TM.getAIXExtendedAltivecABI())) {
2445ffd83dbSDimitry Andric       return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList
2455ffd83dbSDimitry Andric                     : CSR_PPC64_Altivec_SaveList;
246fe6060f1SDimitry Andric     }
2475ffd83dbSDimitry Andric     return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList;
2480b57cec5SDimitry Andric   }
2490b57cec5SDimitry Andric   // 32-bit targets.
250e8d8bef9SDimitry Andric   if (Subtarget.isAIXABI()) {
251e8d8bef9SDimitry Andric     if (Subtarget.hasAltivec())
252fe6060f1SDimitry Andric       return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_Altivec_SaveList
253fe6060f1SDimitry Andric                                            : CSR_AIX32_SaveList;
2545ffd83dbSDimitry Andric     return CSR_AIX32_SaveList;
255e8d8bef9SDimitry Andric   }
256*81ad6265SDimitry Andric   if (Subtarget.pairedVectorMemops())
257*81ad6265SDimitry Andric     return CSR_SVR432_VSRP_SaveList;
2580b57cec5SDimitry Andric   if (Subtarget.hasAltivec())
2590b57cec5SDimitry Andric     return CSR_SVR432_Altivec_SaveList;
2600b57cec5SDimitry Andric   else if (Subtarget.hasSPE())
2610b57cec5SDimitry Andric     return CSR_SVR432_SPE_SaveList;
2620b57cec5SDimitry Andric   return CSR_SVR432_SaveList;
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric const uint32_t *
2660b57cec5SDimitry Andric PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
2670b57cec5SDimitry Andric                                       CallingConv::ID CC) const {
2680b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
2690b57cec5SDimitry Andric   if (CC == CallingConv::AnyReg) {
270fe6060f1SDimitry Andric     if (Subtarget.hasVSX()) {
271*81ad6265SDimitry Andric       if (Subtarget.pairedVectorMemops())
272*81ad6265SDimitry Andric         return CSR_64_AllRegs_VSRP_RegMask;
273fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
274fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_VSX_RegMask;
2750b57cec5SDimitry Andric       return CSR_64_AllRegs_VSX_RegMask;
276fe6060f1SDimitry Andric     }
277fe6060f1SDimitry Andric     if (Subtarget.hasAltivec()) {
278fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
279fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_Altivec_RegMask;
2800b57cec5SDimitry Andric       return CSR_64_AllRegs_Altivec_RegMask;
281fe6060f1SDimitry Andric     }
2820b57cec5SDimitry Andric     return CSR_64_AllRegs_RegMask;
2830b57cec5SDimitry Andric   }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   if (Subtarget.isAIXABI()) {
286fe6060f1SDimitry Andric     return TM.isPPC64()
287fe6060f1SDimitry Andric                ? ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
288fe6060f1SDimitry Andric                       ? CSR_PPC64_Altivec_RegMask
289e8d8bef9SDimitry Andric                       : CSR_PPC64_RegMask)
290fe6060f1SDimitry Andric                : ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
291fe6060f1SDimitry Andric                       ? CSR_AIX32_Altivec_RegMask
292e8d8bef9SDimitry Andric                       : CSR_AIX32_RegMask);
2930b57cec5SDimitry Andric   }
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric   if (CC == CallingConv::Cold) {
296*81ad6265SDimitry Andric     if (TM.isPPC64())
297*81ad6265SDimitry Andric       return Subtarget.pairedVectorMemops()
298*81ad6265SDimitry Andric                  ? CSR_SVR64_ColdCC_VSRP_RegMask
299*81ad6265SDimitry Andric                  : (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
300*81ad6265SDimitry Andric                                            : CSR_SVR64_ColdCC_RegMask);
301*81ad6265SDimitry Andric     else
302*81ad6265SDimitry Andric       return Subtarget.pairedVectorMemops()
303*81ad6265SDimitry Andric                  ? CSR_SVR32_ColdCC_VSRP_RegMask
304*81ad6265SDimitry Andric                  : (Subtarget.hasAltivec()
305*81ad6265SDimitry Andric                         ? CSR_SVR32_ColdCC_Altivec_RegMask
306*81ad6265SDimitry Andric                         : (Subtarget.hasSPE() ? CSR_SVR32_ColdCC_SPE_RegMask
3070b57cec5SDimitry Andric                                               : CSR_SVR32_ColdCC_RegMask));
3080b57cec5SDimitry Andric   }
3090b57cec5SDimitry Andric 
310*81ad6265SDimitry Andric   if (TM.isPPC64())
311*81ad6265SDimitry Andric     return Subtarget.pairedVectorMemops()
312*81ad6265SDimitry Andric                ? CSR_SVR464_VSRP_RegMask
313*81ad6265SDimitry Andric                : (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask
314*81ad6265SDimitry Andric                                          : CSR_PPC64_RegMask);
315*81ad6265SDimitry Andric   else
316*81ad6265SDimitry Andric     return Subtarget.pairedVectorMemops()
317*81ad6265SDimitry Andric                ? CSR_SVR432_VSRP_RegMask
3185ffd83dbSDimitry Andric                : (Subtarget.hasAltivec()
3195ffd83dbSDimitry Andric                       ? CSR_SVR432_Altivec_RegMask
3205ffd83dbSDimitry Andric                       : (Subtarget.hasSPE() ? CSR_SVR432_SPE_RegMask
3210b57cec5SDimitry Andric                                             : CSR_SVR432_RegMask));
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric const uint32_t*
3250b57cec5SDimitry Andric PPCRegisterInfo::getNoPreservedMask() const {
3260b57cec5SDimitry Andric   return CSR_NoRegs_RegMask;
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
3300b57cec5SDimitry Andric   for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
3310b57cec5SDimitry Andric     Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
3350b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
3360b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
3370b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric   // The ZERO register is not really a register, but the representation of r0
3400b57cec5SDimitry Andric   // when used in instructions that treat r0 as the constant 0.
3410b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::ZERO);
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   // The FP register is also not really a register, but is the representation
3440b57cec5SDimitry Andric   // of the frame pointer register used by ISD::FRAMEADDR.
3450b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::FP);
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric   // The BP register is also not really a register, but is the representation
3480b57cec5SDimitry Andric   // of the base pointer register used by setjmp.
3490b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::BP);
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   // The counter registers must be reserved so that counter-based loops can
3520b57cec5SDimitry Andric   // be correctly formed (and the mtctr instructions are not DCE'd).
3530b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::CTR);
3540b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::CTR8);
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::R1);
3570b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::LR);
3580b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::LR8);
3590b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::RM);
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::VRSAVE);
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   // The SVR4 ABI reserves r2 and r13
3640b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI()) {
3650b57cec5SDimitry Andric     // We only reserve r2 if we need to use the TOC pointer. If we have no
3660b57cec5SDimitry Andric     // explicit uses of the TOC pointer (meaning we're a leaf function with
3670b57cec5SDimitry Andric     // no constant-pool loads, etc.) and we have no potential uses inside an
3680b57cec5SDimitry Andric     // inline asm block, then we can treat r2 has an ordinary callee-saved
3690b57cec5SDimitry Andric     // register.
3700b57cec5SDimitry Andric     const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3710b57cec5SDimitry Andric     if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
3720b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R2);  // System-reserved register
3730b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
3740b57cec5SDimitry Andric   }
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   // Always reserve r2 on AIX for now.
3770b57cec5SDimitry Andric   // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions.
3780b57cec5SDimitry Andric   if (Subtarget.isAIXABI())
3790b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R2);  // System-reserved register
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   // On PPC64, r13 is the thread pointer. Never allocate this register.
3820b57cec5SDimitry Andric   if (TM.isPPC64())
3830b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R13);
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric   if (TFI->needsFP(MF))
3860b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R31);
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric   bool IsPositionIndependent = TM.isPositionIndependent();
3890b57cec5SDimitry Andric   if (hasBasePointer(MF)) {
3908bcb0991SDimitry Andric     if (Subtarget.is32BitELFABI() && IsPositionIndependent)
3910b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R29);
3920b57cec5SDimitry Andric     else
3930b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R30);
3940b57cec5SDimitry Andric   }
3950b57cec5SDimitry Andric 
3968bcb0991SDimitry Andric   if (Subtarget.is32BitELFABI() && IsPositionIndependent)
3970b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R30);
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   // Reserve Altivec registers when Altivec is unavailable.
4000b57cec5SDimitry Andric   if (!Subtarget.hasAltivec())
4010b57cec5SDimitry Andric     for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
4020b57cec5SDimitry Andric          IE = PPC::VRRCRegClass.end(); I != IE; ++I)
4030b57cec5SDimitry Andric       markSuperRegs(Reserved, *I);
4040b57cec5SDimitry Andric 
405fe6060f1SDimitry Andric   if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
406fe6060f1SDimitry Andric       !TM.getAIXExtendedAltivecABI()) {
407fe6060f1SDimitry Andric     //  In the AIX default Altivec ABI, vector registers VR20-VR31 are reserved
408fe6060f1SDimitry Andric     //  and cannot be used.
409fe6060f1SDimitry Andric     for (auto Reg : CSR_Altivec_SaveList) {
410fe6060f1SDimitry Andric       if (Reg == 0)
411fe6060f1SDimitry Andric         break;
412fe6060f1SDimitry Andric       markSuperRegs(Reserved, Reg);
413fe6060f1SDimitry Andric       for (MCRegAliasIterator AS(Reg, this, true); AS.isValid(); ++AS) {
414fe6060f1SDimitry Andric         Reserved.set(*AS);
415fe6060f1SDimitry Andric       }
416fe6060f1SDimitry Andric     }
417fe6060f1SDimitry Andric   }
418fe6060f1SDimitry Andric 
4190b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
4200b57cec5SDimitry Andric   return Reserved;
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric 
42304eeddc0SDimitry Andric bool PPCRegisterInfo::isAsmClobberable(const MachineFunction &MF,
42404eeddc0SDimitry Andric                                        MCRegister PhysReg) const {
42504eeddc0SDimitry Andric   // We cannot use getReservedRegs() to find the registers that are not asm
42604eeddc0SDimitry Andric   // clobberable because there are some reserved registers which can be
42704eeddc0SDimitry Andric   // clobbered by inline asm. For example, when LR is clobbered, the register is
42804eeddc0SDimitry Andric   // saved and restored. We will hardcode the registers that are not asm
42904eeddc0SDimitry Andric   // cloberable in this function.
43004eeddc0SDimitry Andric 
43104eeddc0SDimitry Andric   // The stack pointer (R1/X1) is not clobberable by inline asm
43204eeddc0SDimitry Andric   return PhysReg != PPC::R1 && PhysReg != PPC::X1;
43304eeddc0SDimitry Andric }
43404eeddc0SDimitry Andric 
4350b57cec5SDimitry Andric bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const {
4360b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
4370b57cec5SDimitry Andric   const PPCInstrInfo *InstrInfo =  Subtarget.getInstrInfo();
4380b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4390b57cec5SDimitry Andric   const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
4400b57cec5SDimitry Andric 
441fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "requiresFrameIndexScavenging for " << MF.getName()
442fe6060f1SDimitry Andric                     << ".\n");
4430b57cec5SDimitry Andric   // If the callee saved info is invalid we have to default to true for safety.
444fe6060f1SDimitry Andric   if (!MFI.isCalleeSavedInfoValid()) {
445fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "TRUE - Invalid callee saved info.\n");
4460b57cec5SDimitry Andric     return true;
447fe6060f1SDimitry Andric   }
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   // We will require the use of X-Forms because the frame is larger than what
4500b57cec5SDimitry Andric   // can be represented in signed 16 bits that fit in the immediate of a D-Form.
4510b57cec5SDimitry Andric   // If we need an X-Form then we need a register to store the address offset.
4520b57cec5SDimitry Andric   unsigned FrameSize = MFI.getStackSize();
4530b57cec5SDimitry Andric   // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
454fe6060f1SDimitry Andric   if (FrameSize & ~0x7FFF) {
455fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "TRUE - Frame size is too large for D-Form.\n");
4560b57cec5SDimitry Andric     return true;
457fe6060f1SDimitry Andric   }
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   // The callee saved info is valid so it can be traversed.
4600b57cec5SDimitry Andric   // Checking for registers that need saving that do not have load or store
4610b57cec5SDimitry Andric   // forms where the address offset is an immediate.
4620b57cec5SDimitry Andric   for (unsigned i = 0; i < Info.size(); i++) {
463fe6060f1SDimitry Andric     // If the spill is to a register no scavenging is required.
464fe6060f1SDimitry Andric     if (Info[i].isSpilledToReg())
465fe6060f1SDimitry Andric       continue;
466fe6060f1SDimitry Andric 
4670b57cec5SDimitry Andric     int FrIdx = Info[i].getFrameIdx();
46804eeddc0SDimitry Andric     Register Reg = Info[i].getReg();
4690b57cec5SDimitry Andric 
4705ffd83dbSDimitry Andric     const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg);
4715ffd83dbSDimitry Andric     unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC);
4720b57cec5SDimitry Andric     if (!MFI.isFixedObjectIndex(FrIdx)) {
4730b57cec5SDimitry Andric       // This is not a fixed object. If it requires alignment then we may still
4740b57cec5SDimitry Andric       // need to use the XForm.
475fe6060f1SDimitry Andric       if (offsetMinAlignForOpcode(Opcode) > 1) {
476fe6060f1SDimitry Andric         LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
477fe6060f1SDimitry Andric                           << " for register " << printReg(Reg, this) << ".\n");
478fe6060f1SDimitry Andric         LLVM_DEBUG(dbgs() << "TRUE - Not fixed frame object that requires "
479fe6060f1SDimitry Andric                           << "alignment.\n");
4800b57cec5SDimitry Andric         return true;
4810b57cec5SDimitry Andric       }
482fe6060f1SDimitry Andric     }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric     // This is eiher:
4850b57cec5SDimitry Andric     // 1) A fixed frame index object which we know are aligned so
4860b57cec5SDimitry Andric     // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
487480093f4SDimitry Andric     // need to consider the alignment here.
4880b57cec5SDimitry Andric     // 2) A not fixed object but in that case we now know that the min required
4890b57cec5SDimitry Andric     // alignment is no more than 1 based on the previous check.
490fe6060f1SDimitry Andric     if (InstrInfo->isXFormMemOp(Opcode)) {
491fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
492fe6060f1SDimitry Andric                         << " for register " << printReg(Reg, this) << ".\n");
493fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "TRUE - Memory operand is X-Form.\n");
4940b57cec5SDimitry Andric       return true;
4950b57cec5SDimitry Andric     }
496*81ad6265SDimitry Andric 
497*81ad6265SDimitry Andric     // This is a spill/restore of a quadword.
498*81ad6265SDimitry Andric     if ((Opcode == PPC::RESTORE_QUADWORD) || (Opcode == PPC::SPILL_QUADWORD)) {
499*81ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
500*81ad6265SDimitry Andric                         << " for register " << printReg(Reg, this) << ".\n");
501*81ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "TRUE - Memory operand is a quadword.\n");
502*81ad6265SDimitry Andric       return true;
503*81ad6265SDimitry Andric     }
504fe6060f1SDimitry Andric   }
505fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "FALSE - Scavenging is not required.\n");
5060b57cec5SDimitry Andric   return false;
5070b57cec5SDimitry Andric }
5080b57cec5SDimitry Andric 
509fe6060f1SDimitry Andric bool PPCRegisterInfo::requiresVirtualBaseRegisters(
510fe6060f1SDimitry Andric     const MachineFunction &MF) const {
511fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
512fe6060f1SDimitry Andric   // Do not use virtual base registers when ROP protection is turned on.
513fe6060f1SDimitry Andric   // Virtual base registers break the layout of the local variable space and may
514fe6060f1SDimitry Andric   // push the ROP Hash location past the 512 byte range of the ROP store
515fe6060f1SDimitry Andric   // instruction.
516fe6060f1SDimitry Andric   return !Subtarget.hasROPProtect();
517fe6060f1SDimitry Andric }
518fe6060f1SDimitry Andric 
5195ffd83dbSDimitry Andric bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg,
5200b57cec5SDimitry Andric                                                const MachineFunction &MF) const {
5218bcb0991SDimitry Andric   assert(Register::isPhysicalRegister(PhysReg));
5220b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
5230b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
5240b57cec5SDimitry Andric 
525fe6060f1SDimitry Andric   if (!Subtarget.is64BitELFABI() && !Subtarget.isAIXABI())
5260b57cec5SDimitry Andric     return false;
527fe6060f1SDimitry Andric   if (PhysReg == Subtarget.getTOCPointerRegister())
528fe6060f1SDimitry Andric     // X2/R2 is guaranteed to be preserved within a function if it is reserved.
5290b57cec5SDimitry Andric     // The reason it's reserved is that it's the TOC pointer (and the function
5300b57cec5SDimitry Andric     // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
5310b57cec5SDimitry Andric     // with no TOC access), we can't claim that it is preserved.
532fe6060f1SDimitry Andric     return (getReservedRegs(MF).test(PhysReg));
533fe6060f1SDimitry Andric   if (StackPtrConst && PhysReg == Subtarget.getStackPointerRegister() &&
534fe6060f1SDimitry Andric       !MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment())
5350b57cec5SDimitry Andric     // The value of the stack pointer does not change within a function after
5360b57cec5SDimitry Andric     // the prologue and before the epilogue if there are no dynamic allocations
537fe6060f1SDimitry Andric     // and no inline asm which clobbers X1/R1.
5380b57cec5SDimitry Andric     return true;
5390b57cec5SDimitry Andric   return false;
5400b57cec5SDimitry Andric }
5410b57cec5SDimitry Andric 
542fe6060f1SDimitry Andric bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
543fe6060f1SDimitry Andric                                             ArrayRef<MCPhysReg> Order,
544fe6060f1SDimitry Andric                                             SmallVectorImpl<MCPhysReg> &Hints,
545fe6060f1SDimitry Andric                                             const MachineFunction &MF,
546fe6060f1SDimitry Andric                                             const VirtRegMap *VRM,
547fe6060f1SDimitry Andric                                             const LiveRegMatrix *Matrix) const {
548fe6060f1SDimitry Andric   const MachineRegisterInfo *MRI = &MF.getRegInfo();
549fe6060f1SDimitry Andric 
550fe6060f1SDimitry Andric   // Call the base implementation first to set any hints based on the usual
551fe6060f1SDimitry Andric   // heuristics and decide what the return value should be. We want to return
552fe6060f1SDimitry Andric   // the same value returned by the base implementation. If the base
553fe6060f1SDimitry Andric   // implementation decides to return true and force the allocation then we
554fe6060f1SDimitry Andric   // will leave it as such. On the other hand if the base implementation
555fe6060f1SDimitry Andric   // decides to return false the following code will not force the allocation
556fe6060f1SDimitry Andric   // as we are just looking to provide a hint.
557fe6060f1SDimitry Andric   bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
558fe6060f1SDimitry Andric       VirtReg, Order, Hints, MF, VRM, Matrix);
559fe6060f1SDimitry Andric   // We are interested in instructions that copy values to ACC/UACC.
560fe6060f1SDimitry Andric   // The copy into UACC will be simply a COPY to a subreg so we
561fe6060f1SDimitry Andric   // want to allocate the corresponding physical subreg for the source.
562fe6060f1SDimitry Andric   // The copy into ACC will be a BUILD_UACC so we want to allocate
563fe6060f1SDimitry Andric   // the same number UACC for the source.
564fe6060f1SDimitry Andric   for (MachineInstr &Use : MRI->reg_nodbg_instructions(VirtReg)) {
565fe6060f1SDimitry Andric     const MachineOperand *ResultOp = nullptr;
566fe6060f1SDimitry Andric     Register ResultReg;
567fe6060f1SDimitry Andric     switch (Use.getOpcode()) {
568fe6060f1SDimitry Andric     case TargetOpcode::COPY: {
569fe6060f1SDimitry Andric       ResultOp = &Use.getOperand(0);
570fe6060f1SDimitry Andric       ResultReg = ResultOp->getReg();
571fe6060f1SDimitry Andric       if (Register::isVirtualRegister(ResultReg) &&
572fe6060f1SDimitry Andric           MRI->getRegClass(ResultReg)->contains(PPC::UACC0) &&
573fe6060f1SDimitry Andric           VRM->hasPhys(ResultReg)) {
574fe6060f1SDimitry Andric         Register UACCPhys = VRM->getPhys(ResultReg);
575fe6060f1SDimitry Andric         Register HintReg = getSubReg(UACCPhys, ResultOp->getSubReg());
576349cc55cSDimitry Andric         // Ensure that the hint is a VSRp register.
577349cc55cSDimitry Andric         if (HintReg >= PPC::VSRp0 && HintReg <= PPC::VSRp31)
578fe6060f1SDimitry Andric           Hints.push_back(HintReg);
579fe6060f1SDimitry Andric       }
580fe6060f1SDimitry Andric       break;
581fe6060f1SDimitry Andric     }
582fe6060f1SDimitry Andric     case PPC::BUILD_UACC: {
583fe6060f1SDimitry Andric       ResultOp = &Use.getOperand(0);
584fe6060f1SDimitry Andric       ResultReg = ResultOp->getReg();
585fe6060f1SDimitry Andric       if (MRI->getRegClass(ResultReg)->contains(PPC::ACC0) &&
586fe6060f1SDimitry Andric           VRM->hasPhys(ResultReg)) {
587fe6060f1SDimitry Andric         Register ACCPhys = VRM->getPhys(ResultReg);
588fe6060f1SDimitry Andric         assert((ACCPhys >= PPC::ACC0 && ACCPhys <= PPC::ACC7) &&
589fe6060f1SDimitry Andric                "Expecting an ACC register for BUILD_UACC.");
590fe6060f1SDimitry Andric         Register HintReg = PPC::UACC0 + (ACCPhys - PPC::ACC0);
591fe6060f1SDimitry Andric         Hints.push_back(HintReg);
592fe6060f1SDimitry Andric       }
593fe6060f1SDimitry Andric       break;
594fe6060f1SDimitry Andric     }
595fe6060f1SDimitry Andric     }
596fe6060f1SDimitry Andric   }
597fe6060f1SDimitry Andric   return BaseImplRetVal;
598fe6060f1SDimitry Andric }
599fe6060f1SDimitry Andric 
6000b57cec5SDimitry Andric unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
6010b57cec5SDimitry Andric                                               MachineFunction &MF) const {
6020b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
6030b57cec5SDimitry Andric   const unsigned DefaultSafety = 1;
6040b57cec5SDimitry Andric 
6050b57cec5SDimitry Andric   switch (RC->getID()) {
6060b57cec5SDimitry Andric   default:
6070b57cec5SDimitry Andric     return 0;
6080b57cec5SDimitry Andric   case PPC::G8RC_NOX0RegClassID:
6090b57cec5SDimitry Andric   case PPC::GPRC_NOR0RegClassID:
6100b57cec5SDimitry Andric   case PPC::SPERCRegClassID:
6110b57cec5SDimitry Andric   case PPC::G8RCRegClassID:
6120b57cec5SDimitry Andric   case PPC::GPRCRegClassID: {
6130b57cec5SDimitry Andric     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
6140b57cec5SDimitry Andric     return 32 - FP - DefaultSafety;
6150b57cec5SDimitry Andric   }
6160b57cec5SDimitry Andric   case PPC::F4RCRegClassID:
617fe6060f1SDimitry Andric   case PPC::F8RCRegClassID:
6180b57cec5SDimitry Andric   case PPC::VSLRCRegClassID:
6190b57cec5SDimitry Andric     return 32 - DefaultSafety;
620fe6060f1SDimitry Andric   case PPC::VFRCRegClassID:
621fe6060f1SDimitry Andric   case PPC::VRRCRegClassID: {
622fe6060f1SDimitry Andric     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
623fe6060f1SDimitry Andric     // Vector registers VR20-VR31 are reserved and cannot be used in the default
624fe6060f1SDimitry Andric     // Altivec ABI on AIX.
625fe6060f1SDimitry Andric     if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
626fe6060f1SDimitry Andric       return 20 - DefaultSafety;
627fe6060f1SDimitry Andric   }
628fe6060f1SDimitry Andric     return 32 - DefaultSafety;
6290b57cec5SDimitry Andric   case PPC::VSFRCRegClassID:
6300b57cec5SDimitry Andric   case PPC::VSSRCRegClassID:
631fe6060f1SDimitry Andric   case PPC::VSRCRegClassID: {
632fe6060f1SDimitry Andric     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
633fe6060f1SDimitry Andric     if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
634fe6060f1SDimitry Andric       // Vector registers VR20-VR31 are reserved and cannot be used in the
635fe6060f1SDimitry Andric       // default Altivec ABI on AIX.
636fe6060f1SDimitry Andric       return 52 - DefaultSafety;
637fe6060f1SDimitry Andric   }
6380b57cec5SDimitry Andric     return 64 - DefaultSafety;
6390b57cec5SDimitry Andric   case PPC::CRRCRegClassID:
6400b57cec5SDimitry Andric     return 8 - DefaultSafety;
6410b57cec5SDimitry Andric   }
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric 
6440b57cec5SDimitry Andric const TargetRegisterClass *
6450b57cec5SDimitry Andric PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
6460b57cec5SDimitry Andric                                            const MachineFunction &MF) const {
6470b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
648fe6060f1SDimitry Andric   const auto *DefaultSuperclass =
649fe6060f1SDimitry Andric       TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
6500b57cec5SDimitry Andric   if (Subtarget.hasVSX()) {
6510b57cec5SDimitry Andric     // With VSX, we can inflate various sub-register classes to the full VSX
6520b57cec5SDimitry Andric     // register set.
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric     // For Power9 we allow the user to enable GPR to vector spills.
6550b57cec5SDimitry Andric     // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
6560b57cec5SDimitry Andric     // support to spill GPRC.
657fe6060f1SDimitry Andric     if (TM.isELFv2ABI() || Subtarget.isAIXABI()) {
6580b57cec5SDimitry Andric       if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
6590b57cec5SDimitry Andric           RC == &PPC::G8RCRegClass) {
6600b57cec5SDimitry Andric         InflateGP8RC++;
6610b57cec5SDimitry Andric         return &PPC::SPILLTOVSRRCRegClass;
6620b57cec5SDimitry Andric       }
6630b57cec5SDimitry Andric       if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
6640b57cec5SDimitry Andric         InflateGPRC++;
6650b57cec5SDimitry Andric     }
666fe6060f1SDimitry Andric 
667fe6060f1SDimitry Andric     for (const auto *I = RC->getSuperClasses(); *I; ++I) {
668fe6060f1SDimitry Andric       if (getRegSizeInBits(**I) != getRegSizeInBits(*RC))
669fe6060f1SDimitry Andric         continue;
670fe6060f1SDimitry Andric 
671fe6060f1SDimitry Andric       switch ((*I)->getID()) {
672fe6060f1SDimitry Andric       case PPC::VSSRCRegClassID:
673fe6060f1SDimitry Andric         return Subtarget.hasP8Vector() ? *I : DefaultSuperclass;
674fe6060f1SDimitry Andric       case PPC::VSFRCRegClassID:
675fe6060f1SDimitry Andric       case PPC::VSRCRegClassID:
676fe6060f1SDimitry Andric         return *I;
677fe6060f1SDimitry Andric       case PPC::VSRpRCRegClassID:
678fe6060f1SDimitry Andric         return Subtarget.pairedVectorMemops() ? *I : DefaultSuperclass;
679fe6060f1SDimitry Andric       case PPC::ACCRCRegClassID:
680fe6060f1SDimitry Andric       case PPC::UACCRCRegClassID:
681fe6060f1SDimitry Andric         return Subtarget.hasMMA() ? *I : DefaultSuperclass;
682fe6060f1SDimitry Andric       }
683fe6060f1SDimitry Andric     }
6840b57cec5SDimitry Andric   }
6850b57cec5SDimitry Andric 
686fe6060f1SDimitry Andric   return DefaultSuperclass;
6870b57cec5SDimitry Andric }
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6900b57cec5SDimitry Andric // Stack Frame Processing methods
6910b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric /// lowerDynamicAlloc - Generate the code for allocating an object in the
6940b57cec5SDimitry Andric /// current frame.  The sequence of code will be in the general form
6950b57cec5SDimitry Andric ///
6960b57cec5SDimitry Andric ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
6970b57cec5SDimitry Andric ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
6980b57cec5SDimitry Andric ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
6990b57cec5SDimitry Andric ///
7000b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
7010b57cec5SDimitry Andric   // Get the instruction.
7020b57cec5SDimitry Andric   MachineInstr &MI = *II;
7030b57cec5SDimitry Andric   // Get the instruction's basic block.
7040b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
7050b57cec5SDimitry Andric   // Get the basic block's function.
7060b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
7070b57cec5SDimitry Andric   // Get the frame info.
7080b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
7090b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
7100b57cec5SDimitry Andric   // Get the instruction info.
7110b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
7120b57cec5SDimitry Andric   // Determine whether 64-bit pointers are used.
7130b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
7140b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric   // Get the maximum call stack size.
7170b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
7185ffd83dbSDimitry Andric   Align MaxAlign = MFI.getMaxAlign();
7195ffd83dbSDimitry Andric   assert(isAligned(MaxAlign, maxCallFrameSize) &&
7200b57cec5SDimitry Andric          "Maximum call-frame size not sufficiently aligned");
7215ffd83dbSDimitry Andric   (void)MaxAlign;
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
7240b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
7258bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
7260b57cec5SDimitry Andric   bool KillNegSizeReg = MI.getOperand(1).isKill();
7278bcb0991SDimitry Andric   Register NegSizeReg = MI.getOperand(1).getReg();
7280b57cec5SDimitry Andric 
7295ffd83dbSDimitry Andric   prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg);
7300b57cec5SDimitry Andric   // Grow the stack and update the stack pointer link, then determine the
7310b57cec5SDimitry Andric   // address of new allocated space.
7320b57cec5SDimitry Andric   if (LP64) {
7330b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
7340b57cec5SDimitry Andric         .addReg(Reg, RegState::Kill)
7350b57cec5SDimitry Andric         .addReg(PPC::X1)
7360b57cec5SDimitry Andric         .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
7370b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
7380b57cec5SDimitry Andric         .addReg(PPC::X1)
7390b57cec5SDimitry Andric         .addImm(maxCallFrameSize);
7400b57cec5SDimitry Andric   } else {
7410b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
7420b57cec5SDimitry Andric         .addReg(Reg, RegState::Kill)
7430b57cec5SDimitry Andric         .addReg(PPC::R1)
7440b57cec5SDimitry Andric         .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
7450b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
7460b57cec5SDimitry Andric         .addReg(PPC::R1)
7470b57cec5SDimitry Andric         .addImm(maxCallFrameSize);
7480b57cec5SDimitry Andric   }
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric   // Discard the DYNALLOC instruction.
7510b57cec5SDimitry Andric   MBB.erase(II);
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric 
7545ffd83dbSDimitry Andric /// To accomplish dynamic stack allocation, we have to calculate exact size
7555ffd83dbSDimitry Andric /// subtracted from the stack pointer according alignment information and get
7565ffd83dbSDimitry Andric /// previous frame pointer.
7575ffd83dbSDimitry Andric void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II,
7585ffd83dbSDimitry Andric                                            Register &NegSizeReg,
7595ffd83dbSDimitry Andric                                            bool &KillNegSizeReg,
7605ffd83dbSDimitry Andric                                            Register &FramePointer) const {
7615ffd83dbSDimitry Andric   // Get the instruction.
7625ffd83dbSDimitry Andric   MachineInstr &MI = *II;
7635ffd83dbSDimitry Andric   // Get the instruction's basic block.
7645ffd83dbSDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
7655ffd83dbSDimitry Andric   // Get the basic block's function.
7665ffd83dbSDimitry Andric   MachineFunction &MF = *MBB.getParent();
7675ffd83dbSDimitry Andric   // Get the frame info.
7685ffd83dbSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
7695ffd83dbSDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
7705ffd83dbSDimitry Andric   // Get the instruction info.
7715ffd83dbSDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
7725ffd83dbSDimitry Andric   // Determine whether 64-bit pointers are used.
7735ffd83dbSDimitry Andric   bool LP64 = TM.isPPC64();
7745ffd83dbSDimitry Andric   DebugLoc dl = MI.getDebugLoc();
7755ffd83dbSDimitry Andric   // Get the total frame size.
7765ffd83dbSDimitry Andric   unsigned FrameSize = MFI.getStackSize();
7775ffd83dbSDimitry Andric 
7785ffd83dbSDimitry Andric   // Get stack alignments.
7795ffd83dbSDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
7805ffd83dbSDimitry Andric   Align TargetAlign = TFI->getStackAlign();
7815ffd83dbSDimitry Andric   Align MaxAlign = MFI.getMaxAlign();
7825ffd83dbSDimitry Andric 
7835ffd83dbSDimitry Andric   // Determine the previous frame's address.  If FrameSize can't be
7845ffd83dbSDimitry Andric   // represented as 16 bits or we need special alignment, then we load the
7855ffd83dbSDimitry Andric   // previous frame's address from 0(SP).  Why not do an addis of the hi?
7865ffd83dbSDimitry Andric   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
7875ffd83dbSDimitry Andric   // Constructing the constant and adding would take 3 instructions.
7885ffd83dbSDimitry Andric   // Fortunately, a frame greater than 32K is rare.
7895ffd83dbSDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
7905ffd83dbSDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
7915ffd83dbSDimitry Andric 
7925ffd83dbSDimitry Andric   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
7935ffd83dbSDimitry Andric     if (LP64)
7945ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer)
7955ffd83dbSDimitry Andric           .addReg(PPC::X31)
7965ffd83dbSDimitry Andric           .addImm(FrameSize);
7975ffd83dbSDimitry Andric     else
7985ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer)
7995ffd83dbSDimitry Andric           .addReg(PPC::R31)
8005ffd83dbSDimitry Andric           .addImm(FrameSize);
8015ffd83dbSDimitry Andric   } else if (LP64) {
8025ffd83dbSDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer)
8035ffd83dbSDimitry Andric         .addImm(0)
8045ffd83dbSDimitry Andric         .addReg(PPC::X1);
8055ffd83dbSDimitry Andric   } else {
8065ffd83dbSDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer)
8075ffd83dbSDimitry Andric         .addImm(0)
8085ffd83dbSDimitry Andric         .addReg(PPC::R1);
8095ffd83dbSDimitry Andric   }
8105ffd83dbSDimitry Andric   // Determine the actual NegSizeReg according to alignment info.
8115ffd83dbSDimitry Andric   if (LP64) {
8125ffd83dbSDimitry Andric     if (MaxAlign > TargetAlign) {
8135ffd83dbSDimitry Andric       unsigned UnalNegSizeReg = NegSizeReg;
8145ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
8155ffd83dbSDimitry Andric 
8165ffd83dbSDimitry Andric       // Unfortunately, there is no andi, only andi., and we can't insert that
8175ffd83dbSDimitry Andric       // here because we might clobber cr0 while it is live.
8185ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
8195ffd83dbSDimitry Andric           .addImm(~(MaxAlign.value() - 1));
8205ffd83dbSDimitry Andric 
8215ffd83dbSDimitry Andric       unsigned NegSizeReg1 = NegSizeReg;
8225ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
8235ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
8245ffd83dbSDimitry Andric           .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
8255ffd83dbSDimitry Andric           .addReg(NegSizeReg1, RegState::Kill);
8265ffd83dbSDimitry Andric       KillNegSizeReg = true;
8275ffd83dbSDimitry Andric     }
8285ffd83dbSDimitry Andric   } else {
8295ffd83dbSDimitry Andric     if (MaxAlign > TargetAlign) {
8305ffd83dbSDimitry Andric       unsigned UnalNegSizeReg = NegSizeReg;
8315ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
8325ffd83dbSDimitry Andric 
8335ffd83dbSDimitry Andric       // Unfortunately, there is no andi, only andi., and we can't insert that
8345ffd83dbSDimitry Andric       // here because we might clobber cr0 while it is live.
8355ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
8365ffd83dbSDimitry Andric           .addImm(~(MaxAlign.value() - 1));
8375ffd83dbSDimitry Andric 
8385ffd83dbSDimitry Andric       unsigned NegSizeReg1 = NegSizeReg;
8395ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
8405ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
8415ffd83dbSDimitry Andric           .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
8425ffd83dbSDimitry Andric           .addReg(NegSizeReg1, RegState::Kill);
8435ffd83dbSDimitry Andric       KillNegSizeReg = true;
8445ffd83dbSDimitry Andric     }
8455ffd83dbSDimitry Andric   }
8465ffd83dbSDimitry Andric }
8475ffd83dbSDimitry Andric 
8485ffd83dbSDimitry Andric void PPCRegisterInfo::lowerPrepareProbedAlloca(
8495ffd83dbSDimitry Andric     MachineBasicBlock::iterator II) const {
8505ffd83dbSDimitry Andric   MachineInstr &MI = *II;
8515ffd83dbSDimitry Andric   // Get the instruction's basic block.
8525ffd83dbSDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8535ffd83dbSDimitry Andric   // Get the basic block's function.
8545ffd83dbSDimitry Andric   MachineFunction &MF = *MBB.getParent();
8555ffd83dbSDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
8565ffd83dbSDimitry Andric   // Get the instruction info.
8575ffd83dbSDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
8585ffd83dbSDimitry Andric   // Determine whether 64-bit pointers are used.
8595ffd83dbSDimitry Andric   bool LP64 = TM.isPPC64();
8605ffd83dbSDimitry Andric   DebugLoc dl = MI.getDebugLoc();
8615ffd83dbSDimitry Andric   Register FramePointer = MI.getOperand(0).getReg();
862590d96feSDimitry Andric   const Register ActualNegSizeReg = MI.getOperand(1).getReg();
8635ffd83dbSDimitry Andric   bool KillNegSizeReg = MI.getOperand(2).isKill();
8645ffd83dbSDimitry Andric   Register NegSizeReg = MI.getOperand(2).getReg();
865590d96feSDimitry Andric   const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR);
866590d96feSDimitry Andric   // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg.
867590d96feSDimitry Andric   if (FramePointer == NegSizeReg) {
868590d96feSDimitry Andric     assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, "
869590d96feSDimitry Andric                              "NegSizeReg should be killed");
870590d96feSDimitry Andric     // FramePointer is clobbered earlier than the use of NegSizeReg in
871590d96feSDimitry Andric     // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid
872590d96feSDimitry Andric     // misuse.
873590d96feSDimitry Andric     BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
874590d96feSDimitry Andric         .addReg(NegSizeReg)
875590d96feSDimitry Andric         .addReg(NegSizeReg);
876590d96feSDimitry Andric     NegSizeReg = ActualNegSizeReg;
877590d96feSDimitry Andric     KillNegSizeReg = false;
8785ffd83dbSDimitry Andric   }
879590d96feSDimitry Andric   prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer);
880590d96feSDimitry Andric   // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign >
881590d96feSDimitry Andric   // TargetAlign.
882590d96feSDimitry Andric   if (NegSizeReg != ActualNegSizeReg)
883590d96feSDimitry Andric     BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
884590d96feSDimitry Andric         .addReg(NegSizeReg)
885590d96feSDimitry Andric         .addReg(NegSizeReg);
8865ffd83dbSDimitry Andric   MBB.erase(II);
8875ffd83dbSDimitry Andric }
8885ffd83dbSDimitry Andric 
8890b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAreaOffset(
8900b57cec5SDimitry Andric     MachineBasicBlock::iterator II) const {
8910b57cec5SDimitry Andric   // Get the instruction.
8920b57cec5SDimitry Andric   MachineInstr &MI = *II;
8930b57cec5SDimitry Andric   // Get the instruction's basic block.
8940b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8950b57cec5SDimitry Andric   // Get the basic block's function.
8960b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
8970b57cec5SDimitry Andric   // Get the frame info.
8980b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
8990b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9000b57cec5SDimitry Andric   // Get the instruction info.
9010b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
9040b57cec5SDimitry Andric   bool is64Bit = TM.isPPC64();
9050b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
9060b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
9070b57cec5SDimitry Andric           MI.getOperand(0).getReg())
9080b57cec5SDimitry Andric       .addImm(maxCallFrameSize);
9090b57cec5SDimitry Andric   MBB.erase(II);
9100b57cec5SDimitry Andric }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
9130b57cec5SDimitry Andric /// reserving a whole register (R0), we scrounge for one here. This generates
9140b57cec5SDimitry Andric /// code like this:
9150b57cec5SDimitry Andric ///
9160b57cec5SDimitry Andric ///   mfcr rA                  ; Move the conditional register into GPR rA.
9170b57cec5SDimitry Andric ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
9180b57cec5SDimitry Andric ///   stw rA, FI               ; Store rA to the frame.
9190b57cec5SDimitry Andric ///
9200b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
9210b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
9220b57cec5SDimitry Andric   // Get the instruction.
9230b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
9240b57cec5SDimitry Andric   // Get the instruction's basic block.
9250b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
9260b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
9270b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9280b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
9290b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
9300b57cec5SDimitry Andric 
9310b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
9320b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
9330b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
9340b57cec5SDimitry Andric 
9358bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9368bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric   // We need to store the CR in the low 4-bits of the saved value. First, issue
9390b57cec5SDimitry Andric   // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
9400b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
9410b57cec5SDimitry Andric       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric   // If the saved register wasn't CR0, shift the bits left so that they are in
9440b57cec5SDimitry Andric   // CR0's slot.
9450b57cec5SDimitry Andric   if (SrcReg != PPC::CR0) {
9465ffd83dbSDimitry Andric     Register Reg1 = Reg;
9470b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric     // rlwinm rA, rA, ShiftBits, 0, 31.
9500b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
9510b57cec5SDimitry Andric       .addReg(Reg1, RegState::Kill)
9520b57cec5SDimitry Andric       .addImm(getEncodingValue(SrcReg) * 4)
9530b57cec5SDimitry Andric       .addImm(0)
9540b57cec5SDimitry Andric       .addImm(31);
9550b57cec5SDimitry Andric   }
9560b57cec5SDimitry Andric 
9570b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
9580b57cec5SDimitry Andric                     .addReg(Reg, RegState::Kill),
9590b57cec5SDimitry Andric                     FrameIndex);
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric   // Discard the pseudo instruction.
9620b57cec5SDimitry Andric   MBB.erase(II);
9630b57cec5SDimitry Andric }
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
9660b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
9670b57cec5SDimitry Andric   // Get the instruction.
9680b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
9690b57cec5SDimitry Andric   // Get the instruction's basic block.
9700b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
9710b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
9720b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9730b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
9740b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
9770b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
9780b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
9790b57cec5SDimitry Andric 
9808bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9818bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
9820b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
9830b57cec5SDimitry Andric     "RESTORE_CR does not define its destination");
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
9860b57cec5SDimitry Andric                               Reg), FrameIndex);
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric   // If the reloaded register isn't CR0, shift the bits right so that they are
9890b57cec5SDimitry Andric   // in the right CR's slot.
9900b57cec5SDimitry Andric   if (DestReg != PPC::CR0) {
9915ffd83dbSDimitry Andric     Register Reg1 = Reg;
9920b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric     unsigned ShiftBits = getEncodingValue(DestReg)*4;
9950b57cec5SDimitry Andric     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
9960b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
9970b57cec5SDimitry Andric              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
9980b57cec5SDimitry Andric              .addImm(31);
9990b57cec5SDimitry Andric   }
10000b57cec5SDimitry Andric 
10010b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
10020b57cec5SDimitry Andric              .addReg(Reg, RegState::Kill);
10030b57cec5SDimitry Andric 
10040b57cec5SDimitry Andric   // Discard the pseudo instruction.
10050b57cec5SDimitry Andric   MBB.erase(II);
10060b57cec5SDimitry Andric }
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
10090b57cec5SDimitry Andric                                          unsigned FrameIndex) const {
10100b57cec5SDimitry Andric   // Get the instruction.
10110b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_CRBIT <SrcReg>, <offset>
10120b57cec5SDimitry Andric   // Get the instruction's basic block.
10130b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
10140b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
10150b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
10160b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
10170b57cec5SDimitry Andric   const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
10180b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
10210b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
10220b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
10230b57cec5SDimitry Andric 
10248bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
10258bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
10260b57cec5SDimitry Andric 
10270b57cec5SDimitry Andric   // Search up the BB to find the definition of the CR bit.
1028480093f4SDimitry Andric   MachineBasicBlock::reverse_iterator Ins = MI;
1029480093f4SDimitry Andric   MachineBasicBlock::reverse_iterator Rend = MBB.rend();
1030480093f4SDimitry Andric   ++Ins;
10310b57cec5SDimitry Andric   unsigned CRBitSpillDistance = 0;
1032480093f4SDimitry Andric   bool SeenUse = false;
1033480093f4SDimitry Andric   for (; Ins != Rend; ++Ins) {
10340b57cec5SDimitry Andric     // Definition found.
10350b57cec5SDimitry Andric     if (Ins->modifiesRegister(SrcReg, TRI))
10360b57cec5SDimitry Andric       break;
1037480093f4SDimitry Andric     // Use found.
1038480093f4SDimitry Andric     if (Ins->readsRegister(SrcReg, TRI))
1039480093f4SDimitry Andric       SeenUse = true;
10400b57cec5SDimitry Andric     // Unable to find CR bit definition within maximum search distance.
10410b57cec5SDimitry Andric     if (CRBitSpillDistance == MaxCRBitSpillDist) {
10420b57cec5SDimitry Andric       Ins = MI;
10430b57cec5SDimitry Andric       break;
10440b57cec5SDimitry Andric     }
10450b57cec5SDimitry Andric     // Skip debug instructions when counting CR bit spill distance.
10460b57cec5SDimitry Andric     if (!Ins->isDebugInstr())
10470b57cec5SDimitry Andric       CRBitSpillDistance++;
10480b57cec5SDimitry Andric   }
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric   // Unable to find the definition of the CR bit in the MBB.
10510b57cec5SDimitry Andric   if (Ins == MBB.rend())
10520b57cec5SDimitry Andric     Ins = MI;
10530b57cec5SDimitry Andric 
1054480093f4SDimitry Andric   bool SpillsKnownBit = false;
10550b57cec5SDimitry Andric   // There is no need to extract the CR bit if its value is already known.
10560b57cec5SDimitry Andric   switch (Ins->getOpcode()) {
10570b57cec5SDimitry Andric   case PPC::CRUNSET:
10580b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
10590b57cec5SDimitry Andric       .addImm(0);
1060480093f4SDimitry Andric     SpillsKnownBit = true;
10610b57cec5SDimitry Andric     break;
10620b57cec5SDimitry Andric   case PPC::CRSET:
10630b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
10640b57cec5SDimitry Andric       .addImm(-32768);
1065480093f4SDimitry Andric     SpillsKnownBit = true;
10660b57cec5SDimitry Andric     break;
10670b57cec5SDimitry Andric   default:
1068e8d8bef9SDimitry Andric     // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all
1069e8d8bef9SDimitry Andric     // bits (specifically, it produces a -1 if the CR bit is set). Ultimately,
1070e8d8bef9SDimitry Andric     // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit
1071e8d8bef9SDimitry Andric     // register), and SETNBC will set this.
1072e8d8bef9SDimitry Andric     if (Subtarget.isISA3_1()) {
1073e8d8bef9SDimitry Andric       BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg)
1074e8d8bef9SDimitry Andric           .addReg(SrcReg, RegState::Undef);
1075e8d8bef9SDimitry Andric       break;
1076e8d8bef9SDimitry Andric     }
1077e8d8bef9SDimitry Andric 
1078480093f4SDimitry Andric     // On Power9, we can use SETB to extract the LT bit. This only works for
1079480093f4SDimitry Andric     // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value
1080480093f4SDimitry Andric     // of the bit we care about (32-bit sign bit) will be set to the value of
1081480093f4SDimitry Andric     // the LT bit (regardless of the other bits in the CR field).
1082480093f4SDimitry Andric     if (Subtarget.isISA3_0()) {
1083480093f4SDimitry Andric       if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT ||
1084480093f4SDimitry Andric           SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT ||
1085480093f4SDimitry Andric           SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT ||
1086480093f4SDimitry Andric           SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) {
1087480093f4SDimitry Andric         BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg)
1088480093f4SDimitry Andric           .addReg(getCRFromCRBit(SrcReg), RegState::Undef);
1089480093f4SDimitry Andric         break;
1090480093f4SDimitry Andric       }
1091480093f4SDimitry Andric     }
1092480093f4SDimitry Andric 
10930b57cec5SDimitry Andric     // We need to move the CR field that contains the CR bit we are spilling.
10940b57cec5SDimitry Andric     // The super register may not be explicitly defined (i.e. it can be defined
10950b57cec5SDimitry Andric     // by a CR-logical that only defines the subreg) so we state that the CR
10960b57cec5SDimitry Andric     // field is undef. Also, in order to preserve the kill flag on the CR bit,
10970b57cec5SDimitry Andric     // we add it as an implicit use.
10980b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
10990b57cec5SDimitry Andric       .addReg(getCRFromCRBit(SrcReg), RegState::Undef)
11000b57cec5SDimitry Andric       .addReg(SrcReg,
11010b57cec5SDimitry Andric               RegState::Implicit | getKillRegState(MI.getOperand(0).isKill()));
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric     // If the saved register wasn't CR0LT, shift the bits left so that the bit
11040b57cec5SDimitry Andric     // to store is the first one. Mask all but that bit.
11055ffd83dbSDimitry Andric     Register Reg1 = Reg;
11060b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric     // rlwinm rA, rA, ShiftBits, 0, 0.
11090b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
11100b57cec5SDimitry Andric       .addReg(Reg1, RegState::Kill)
11110b57cec5SDimitry Andric       .addImm(getEncodingValue(SrcReg))
11120b57cec5SDimitry Andric       .addImm(0).addImm(0);
11130b57cec5SDimitry Andric   }
11140b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
11150b57cec5SDimitry Andric                     .addReg(Reg, RegState::Kill),
11160b57cec5SDimitry Andric                     FrameIndex);
11170b57cec5SDimitry Andric 
1118480093f4SDimitry Andric   bool KillsCRBit = MI.killsRegister(SrcReg, TRI);
11190b57cec5SDimitry Andric   // Discard the pseudo instruction.
11200b57cec5SDimitry Andric   MBB.erase(II);
1121480093f4SDimitry Andric   if (SpillsKnownBit && KillsCRBit && !SeenUse) {
1122480093f4SDimitry Andric     Ins->setDesc(TII.get(PPC::UNENCODED_NOP));
1123*81ad6265SDimitry Andric     Ins->removeOperand(0);
1124480093f4SDimitry Andric   }
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
11280b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
11290b57cec5SDimitry Andric   // Get the instruction.
11300b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CRBIT <offset>
11310b57cec5SDimitry Andric   // Get the instruction's basic block.
11320b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
11330b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
11340b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
11350b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
11360b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
11370b57cec5SDimitry Andric 
11380b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
11390b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
11400b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
11410b57cec5SDimitry Andric 
11428bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
11438bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
11440b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
11450b57cec5SDimitry Andric     "RESTORE_CRBIT does not define its destination");
11460b57cec5SDimitry Andric 
11470b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
11480b57cec5SDimitry Andric                               Reg), FrameIndex);
11490b57cec5SDimitry Andric 
11500b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
11510b57cec5SDimitry Andric 
11528bcb0991SDimitry Andric   Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
11530b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
11540b57cec5SDimitry Andric           .addReg(getCRFromCRBit(DestReg));
11550b57cec5SDimitry Andric 
11560b57cec5SDimitry Andric   unsigned ShiftBits = getEncodingValue(DestReg);
11570b57cec5SDimitry Andric   // rlwimi r11, r10, 32-ShiftBits, ..., ...
11580b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
11590b57cec5SDimitry Andric       .addReg(RegO, RegState::Kill)
11600b57cec5SDimitry Andric       .addReg(Reg, RegState::Kill)
11610b57cec5SDimitry Andric       .addImm(ShiftBits ? 32 - ShiftBits : 0)
11620b57cec5SDimitry Andric       .addImm(ShiftBits)
11630b57cec5SDimitry Andric       .addImm(ShiftBits);
11640b57cec5SDimitry Andric 
11650b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
11660b57cec5SDimitry Andric           getCRFromCRBit(DestReg))
11670b57cec5SDimitry Andric       .addReg(RegO, RegState::Kill)
11680b57cec5SDimitry Andric       // Make sure we have a use dependency all the way through this
11690b57cec5SDimitry Andric       // sequence of instructions. We can't have the other bits in the CR
11700b57cec5SDimitry Andric       // modified in between the mfocrf and the mtocrf.
11710b57cec5SDimitry Andric       .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric   // Discard the pseudo instruction.
11740b57cec5SDimitry Andric   MBB.erase(II);
11750b57cec5SDimitry Andric }
11760b57cec5SDimitry Andric 
1177e8d8bef9SDimitry Andric void PPCRegisterInfo::emitAccCopyInfo(MachineBasicBlock &MBB,
1178e8d8bef9SDimitry Andric                                       MCRegister DestReg, MCRegister SrcReg) {
1179e8d8bef9SDimitry Andric #ifdef NDEBUG
1180e8d8bef9SDimitry Andric   return;
1181e8d8bef9SDimitry Andric #else
1182e8d8bef9SDimitry Andric   if (ReportAccMoves) {
1183e8d8bef9SDimitry Andric     std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc";
1184e8d8bef9SDimitry Andric     std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc";
1185e8d8bef9SDimitry Andric     dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n";
1186e8d8bef9SDimitry Andric     MBB.dump();
1187e8d8bef9SDimitry Andric   }
1188e8d8bef9SDimitry Andric #endif
1189e8d8bef9SDimitry Andric }
1190e8d8bef9SDimitry Andric 
1191e8d8bef9SDimitry Andric static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed,
1192e8d8bef9SDimitry Andric                                     bool IsRestore) {
1193e8d8bef9SDimitry Andric #ifdef NDEBUG
1194e8d8bef9SDimitry Andric   return;
1195e8d8bef9SDimitry Andric #else
1196e8d8bef9SDimitry Andric   if (ReportAccMoves) {
1197e8d8bef9SDimitry Andric     dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register "
1198e8d8bef9SDimitry Andric            << (IsRestore ? "restore" : "spill") << ":\n";
1199e8d8bef9SDimitry Andric     MBB.dump();
1200e8d8bef9SDimitry Andric   }
1201e8d8bef9SDimitry Andric #endif
1202e8d8bef9SDimitry Andric }
1203e8d8bef9SDimitry Andric 
1204*81ad6265SDimitry Andric static void spillRegPairs(MachineBasicBlock &MBB,
1205*81ad6265SDimitry Andric                           MachineBasicBlock::iterator II, DebugLoc DL,
1206*81ad6265SDimitry Andric                           const TargetInstrInfo &TII, Register SrcReg,
1207*81ad6265SDimitry Andric                           unsigned FrameIndex, bool IsLittleEndian,
1208*81ad6265SDimitry Andric                           bool IsKilled, bool TwoPairs) {
1209*81ad6265SDimitry Andric   unsigned Offset = 0;
1210*81ad6265SDimitry Andric   if (TwoPairs)
1211*81ad6265SDimitry Andric     Offset = IsLittleEndian ? 48 : 0;
1212*81ad6265SDimitry Andric   else
1213*81ad6265SDimitry Andric     Offset = IsLittleEndian ? 16 : 0;
1214*81ad6265SDimitry Andric   Register Reg = (SrcReg > PPC::VSRp15) ? PPC::V0 + (SrcReg - PPC::VSRp16) * 2
1215*81ad6265SDimitry Andric                                         : PPC::VSL0 + (SrcReg - PPC::VSRp0) * 2;
1216*81ad6265SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXV))
1217*81ad6265SDimitry Andric                         .addReg(Reg, getKillRegState(IsKilled)),
1218*81ad6265SDimitry Andric                     FrameIndex, Offset);
1219*81ad6265SDimitry Andric   Offset += IsLittleEndian ? -16 : 16;
1220*81ad6265SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXV))
1221*81ad6265SDimitry Andric                         .addReg(Reg + 1, getKillRegState(IsKilled)),
1222*81ad6265SDimitry Andric                     FrameIndex, Offset);
1223*81ad6265SDimitry Andric   if (TwoPairs) {
1224*81ad6265SDimitry Andric     Offset += IsLittleEndian ? -16 : 16;
1225*81ad6265SDimitry Andric     addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXV))
1226*81ad6265SDimitry Andric                           .addReg(Reg + 2, getKillRegState(IsKilled)),
1227*81ad6265SDimitry Andric                       FrameIndex, Offset);
1228*81ad6265SDimitry Andric     Offset += IsLittleEndian ? -16 : 16;
1229*81ad6265SDimitry Andric     addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXV))
1230*81ad6265SDimitry Andric                           .addReg(Reg + 3, getKillRegState(IsKilled)),
1231*81ad6265SDimitry Andric                       FrameIndex, Offset);
1232*81ad6265SDimitry Andric   }
1233*81ad6265SDimitry Andric }
1234*81ad6265SDimitry Andric 
1235*81ad6265SDimitry Andric /// Remove any STXVP[X] instructions and split them out into a pair of
1236*81ad6265SDimitry Andric /// STXV[X] instructions if --disable-auto-paired-vec-st is specified on
1237*81ad6265SDimitry Andric /// the command line.
1238*81ad6265SDimitry Andric void PPCRegisterInfo::lowerOctWordSpilling(MachineBasicBlock::iterator II,
1239*81ad6265SDimitry Andric                                            unsigned FrameIndex) const {
1240*81ad6265SDimitry Andric   assert(DisableAutoPairedVecSt &&
1241*81ad6265SDimitry Andric          "Expecting to do this only if paired vector stores are disabled.");
1242*81ad6265SDimitry Andric   MachineInstr &MI = *II; // STXVP <SrcReg>, <offset>
1243*81ad6265SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
1244*81ad6265SDimitry Andric   MachineFunction &MF = *MBB.getParent();
1245*81ad6265SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1246*81ad6265SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1247*81ad6265SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
1248*81ad6265SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
1249*81ad6265SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
1250*81ad6265SDimitry Andric   bool IsKilled = MI.getOperand(0).isKill();
1251*81ad6265SDimitry Andric   spillRegPairs(MBB, II, DL, TII, SrcReg, FrameIndex, IsLittleEndian, IsKilled,
1252*81ad6265SDimitry Andric                 /* TwoPairs */ false);
1253*81ad6265SDimitry Andric   // Discard the original instruction.
1254*81ad6265SDimitry Andric   MBB.erase(II);
1255*81ad6265SDimitry Andric }
1256*81ad6265SDimitry Andric 
1257e8d8bef9SDimitry Andric /// lowerACCSpilling - Generate the code for spilling the accumulator register.
1258e8d8bef9SDimitry Andric /// Similarly to other spills/reloads that use pseudo-ops, we do not actually
1259e8d8bef9SDimitry Andric /// eliminate the FrameIndex here nor compute the stack offset. We simply
1260e8d8bef9SDimitry Andric /// create a real instruction with an FI and rely on eliminateFrameIndex to
1261e8d8bef9SDimitry Andric /// handle the FI elimination.
1262e8d8bef9SDimitry Andric void PPCRegisterInfo::lowerACCSpilling(MachineBasicBlock::iterator II,
12630b57cec5SDimitry Andric                                        unsigned FrameIndex) const {
1264e8d8bef9SDimitry Andric   MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset>
12650b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
12660b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
12670b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
12680b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1269e8d8bef9SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
12708bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
1271e8d8bef9SDimitry Andric   bool IsKilled = MI.getOperand(0).isKill();
12720b57cec5SDimitry Andric 
1273e8d8bef9SDimitry Andric   bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg);
1274e8d8bef9SDimitry Andric   Register Reg =
1275e8d8bef9SDimitry Andric       PPC::VSRp0 + (SrcReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1276e8d8bef9SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
12770b57cec5SDimitry Andric 
1278e8d8bef9SDimitry Andric   emitAccSpillRestoreInfo(MBB, IsPrimed, false);
1279e8d8bef9SDimitry Andric 
1280e8d8bef9SDimitry Andric   // De-prime the register being spilled, create two stores for the pair
1281e8d8bef9SDimitry Andric   // subregisters accounting for endianness and then re-prime the register if
1282e8d8bef9SDimitry Andric   // it isn't killed.  This uses the Offset parameter to addFrameReference() to
1283e8d8bef9SDimitry Andric   // adjust the offset of the store that is within the 64-byte stack slot.
1284e8d8bef9SDimitry Andric   if (IsPrimed)
1285e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg);
1286*81ad6265SDimitry Andric   if (DisableAutoPairedVecSt)
1287*81ad6265SDimitry Andric     spillRegPairs(MBB, II, DL, TII, Reg, FrameIndex, IsLittleEndian, IsKilled,
1288*81ad6265SDimitry Andric                   /* TwoPairs */ true);
1289*81ad6265SDimitry Andric   else {
1290e8d8bef9SDimitry Andric     addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1291e8d8bef9SDimitry Andric                           .addReg(Reg, getKillRegState(IsKilled)),
1292e8d8bef9SDimitry Andric                       FrameIndex, IsLittleEndian ? 32 : 0);
1293e8d8bef9SDimitry Andric     addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1294e8d8bef9SDimitry Andric                           .addReg(Reg + 1, getKillRegState(IsKilled)),
1295e8d8bef9SDimitry Andric                       FrameIndex, IsLittleEndian ? 0 : 32);
1296*81ad6265SDimitry Andric   }
1297e8d8bef9SDimitry Andric   if (IsPrimed && !IsKilled)
1298e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg);
12990b57cec5SDimitry Andric 
13000b57cec5SDimitry Andric   // Discard the pseudo instruction.
13010b57cec5SDimitry Andric   MBB.erase(II);
13020b57cec5SDimitry Andric }
13030b57cec5SDimitry Andric 
1304e8d8bef9SDimitry Andric /// lowerACCRestore - Generate the code to restore the accumulator register.
1305e8d8bef9SDimitry Andric void PPCRegisterInfo::lowerACCRestore(MachineBasicBlock::iterator II,
13060b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
1307e8d8bef9SDimitry Andric   MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset>
13080b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
13090b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
13100b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
13110b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1312e8d8bef9SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
13130b57cec5SDimitry Andric 
13148bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
13150b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
1316e8d8bef9SDimitry Andric          "RESTORE_ACC does not define its destination");
13170b57cec5SDimitry Andric 
1318e8d8bef9SDimitry Andric   bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg);
1319e8d8bef9SDimitry Andric   Register Reg =
1320e8d8bef9SDimitry Andric       PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1321e8d8bef9SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
13220b57cec5SDimitry Andric 
1323e8d8bef9SDimitry Andric   emitAccSpillRestoreInfo(MBB, IsPrimed, true);
1324e8d8bef9SDimitry Andric 
1325e8d8bef9SDimitry Andric   // Create two loads for the pair subregisters accounting for endianness and
1326e8d8bef9SDimitry Andric   // then prime the accumulator register being restored.
1327e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg),
1328e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 32 : 0);
1329e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1),
1330e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 0 : 32);
1331e8d8bef9SDimitry Andric   if (IsPrimed)
1332e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg);
13330b57cec5SDimitry Andric 
13340b57cec5SDimitry Andric   // Discard the pseudo instruction.
13350b57cec5SDimitry Andric   MBB.erase(II);
13360b57cec5SDimitry Andric }
13370b57cec5SDimitry Andric 
1338fe6060f1SDimitry Andric /// lowerQuadwordSpilling - Generate code to spill paired general register.
1339fe6060f1SDimitry Andric void PPCRegisterInfo::lowerQuadwordSpilling(MachineBasicBlock::iterator II,
1340fe6060f1SDimitry Andric                                             unsigned FrameIndex) const {
1341fe6060f1SDimitry Andric   MachineInstr &MI = *II;
1342fe6060f1SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
1343fe6060f1SDimitry Andric   MachineFunction &MF = *MBB.getParent();
1344fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1345fe6060f1SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1346fe6060f1SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
1347fe6060f1SDimitry Andric 
1348fe6060f1SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
1349fe6060f1SDimitry Andric   bool IsKilled = MI.getOperand(0).isKill();
1350fe6060f1SDimitry Andric 
1351fe6060f1SDimitry Andric   Register Reg = PPC::X0 + (SrcReg - PPC::G8p0) * 2;
1352fe6060f1SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
1353fe6060f1SDimitry Andric 
1354fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
1355fe6060f1SDimitry Andric                         .addReg(Reg, getKillRegState(IsKilled)),
1356fe6060f1SDimitry Andric                     FrameIndex, IsLittleEndian ? 8 : 0);
1357fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
1358fe6060f1SDimitry Andric                         .addReg(Reg + 1, getKillRegState(IsKilled)),
1359fe6060f1SDimitry Andric                     FrameIndex, IsLittleEndian ? 0 : 8);
1360fe6060f1SDimitry Andric 
1361fe6060f1SDimitry Andric   // Discard the pseudo instruction.
1362fe6060f1SDimitry Andric   MBB.erase(II);
1363fe6060f1SDimitry Andric }
1364fe6060f1SDimitry Andric 
1365fe6060f1SDimitry Andric /// lowerQuadwordRestore - Generate code to restore paired general register.
1366fe6060f1SDimitry Andric void PPCRegisterInfo::lowerQuadwordRestore(MachineBasicBlock::iterator II,
1367fe6060f1SDimitry Andric                                            unsigned FrameIndex) const {
1368fe6060f1SDimitry Andric   MachineInstr &MI = *II;
1369fe6060f1SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
1370fe6060f1SDimitry Andric   MachineFunction &MF = *MBB.getParent();
1371fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1372fe6060f1SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1373fe6060f1SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
1374fe6060f1SDimitry Andric 
1375fe6060f1SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
1376fe6060f1SDimitry Andric   assert(MI.definesRegister(DestReg) &&
1377fe6060f1SDimitry Andric          "RESTORE_QUADWORD does not define its destination");
1378fe6060f1SDimitry Andric 
1379fe6060f1SDimitry Andric   Register Reg = PPC::X0 + (DestReg - PPC::G8p0) * 2;
1380fe6060f1SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
1381fe6060f1SDimitry Andric 
1382fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg), FrameIndex,
1383fe6060f1SDimitry Andric                     IsLittleEndian ? 8 : 0);
1384fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg + 1), FrameIndex,
1385fe6060f1SDimitry Andric                     IsLittleEndian ? 0 : 8);
1386fe6060f1SDimitry Andric 
1387fe6060f1SDimitry Andric   // Discard the pseudo instruction.
1388fe6060f1SDimitry Andric   MBB.erase(II);
1389fe6060f1SDimitry Andric }
1390fe6060f1SDimitry Andric 
13910b57cec5SDimitry Andric bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
13925ffd83dbSDimitry Andric                                            Register Reg, int &FrameIdx) const {
13935ffd83dbSDimitry Andric   // For the nonvolatile condition registers (CR2, CR3, CR4) return true to
13945ffd83dbSDimitry Andric   // prevent allocating an additional frame slot.
13955ffd83dbSDimitry Andric   // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8,
13965ffd83dbSDimitry Andric   // for 32-bit AIX the CR save area is in the linkage area at SP+4.
13975ffd83dbSDimitry Andric   // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos
13985ffd83dbSDimitry Andric   // valid.
13995ffd83dbSDimitry Andric   // For 32-bit ELF, we have previously created the stack slot if needed, so
14005ffd83dbSDimitry Andric   // return its FrameIdx.
14015ffd83dbSDimitry Andric   if (PPC::CR2 <= Reg && Reg <= PPC::CR4) {
14025ffd83dbSDimitry Andric     FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex();
14030b57cec5SDimitry Andric     return true;
14040b57cec5SDimitry Andric   }
14050b57cec5SDimitry Andric   return false;
14060b57cec5SDimitry Andric }
14070b57cec5SDimitry Andric 
14080b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is.
14090b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC) {
14100b57cec5SDimitry Andric   switch (OpC) {
14110b57cec5SDimitry Andric   default:
14120b57cec5SDimitry Andric     return 1;
14130b57cec5SDimitry Andric   case PPC::LWA:
14140b57cec5SDimitry Andric   case PPC::LWA_32:
14150b57cec5SDimitry Andric   case PPC::LD:
14160b57cec5SDimitry Andric   case PPC::LDU:
14170b57cec5SDimitry Andric   case PPC::STD:
14180b57cec5SDimitry Andric   case PPC::STDU:
14190b57cec5SDimitry Andric   case PPC::DFLOADf32:
14200b57cec5SDimitry Andric   case PPC::DFLOADf64:
14210b57cec5SDimitry Andric   case PPC::DFSTOREf32:
14220b57cec5SDimitry Andric   case PPC::DFSTOREf64:
14230b57cec5SDimitry Andric   case PPC::LXSD:
14240b57cec5SDimitry Andric   case PPC::LXSSP:
14250b57cec5SDimitry Andric   case PPC::STXSD:
14260b57cec5SDimitry Andric   case PPC::STXSSP:
1427fe6060f1SDimitry Andric   case PPC::STQ:
14280b57cec5SDimitry Andric     return 4;
14290b57cec5SDimitry Andric   case PPC::EVLDD:
14300b57cec5SDimitry Andric   case PPC::EVSTDD:
14310b57cec5SDimitry Andric     return 8;
14320b57cec5SDimitry Andric   case PPC::LXV:
14330b57cec5SDimitry Andric   case PPC::STXV:
1434fe6060f1SDimitry Andric   case PPC::LQ:
1435fe6060f1SDimitry Andric   case PPC::LXVP:
1436fe6060f1SDimitry Andric   case PPC::STXVP:
14370b57cec5SDimitry Andric     return 16;
14380b57cec5SDimitry Andric   }
14390b57cec5SDimitry Andric }
14400b57cec5SDimitry Andric 
14410b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is.
14420b57cec5SDimitry Andric static unsigned offsetMinAlign(const MachineInstr &MI) {
14430b57cec5SDimitry Andric   unsigned OpC = MI.getOpcode();
14440b57cec5SDimitry Andric   return offsetMinAlignForOpcode(OpC);
14450b57cec5SDimitry Andric }
14460b57cec5SDimitry Andric 
14470b57cec5SDimitry Andric // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
14480b57cec5SDimitry Andric static unsigned getOffsetONFromFION(const MachineInstr &MI,
14490b57cec5SDimitry Andric                                     unsigned FIOperandNum) {
14500b57cec5SDimitry Andric   // Take into account whether it's an add or mem instruction
14510b57cec5SDimitry Andric   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
14520b57cec5SDimitry Andric   if (MI.isInlineAsm())
14530b57cec5SDimitry Andric     OffsetOperandNo = FIOperandNum - 1;
14540b57cec5SDimitry Andric   else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
14550b57cec5SDimitry Andric            MI.getOpcode() == TargetOpcode::PATCHPOINT)
14560b57cec5SDimitry Andric     OffsetOperandNo = FIOperandNum + 1;
14570b57cec5SDimitry Andric 
14580b57cec5SDimitry Andric   return OffsetOperandNo;
14590b57cec5SDimitry Andric }
14600b57cec5SDimitry Andric 
14610b57cec5SDimitry Andric void
14620b57cec5SDimitry Andric PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
14630b57cec5SDimitry Andric                                      int SPAdj, unsigned FIOperandNum,
14640b57cec5SDimitry Andric                                      RegScavenger *RS) const {
14650b57cec5SDimitry Andric   assert(SPAdj == 0 && "Unexpected");
14660b57cec5SDimitry Andric 
14670b57cec5SDimitry Andric   // Get the instruction.
14680b57cec5SDimitry Andric   MachineInstr &MI = *II;
14690b57cec5SDimitry Andric   // Get the instruction's basic block.
14700b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
14710b57cec5SDimitry Andric   // Get the basic block's function.
14720b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
14730b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
14740b57cec5SDimitry Andric   // Get the instruction info.
1475349cc55cSDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
14760b57cec5SDimitry Andric   // Get the frame info.
14770b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
14780b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
14810b57cec5SDimitry Andric 
14820b57cec5SDimitry Andric   // Get the frame index.
14830b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric   // Get the frame pointer save index.  Users of this index are primarily
14860b57cec5SDimitry Andric   // DYNALLOC instructions.
14870b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
14880b57cec5SDimitry Andric   int FPSI = FI->getFramePointerSaveIndex();
14890b57cec5SDimitry Andric   // Get the instruction opcode.
14900b57cec5SDimitry Andric   unsigned OpC = MI.getOpcode();
14910b57cec5SDimitry Andric 
14920b57cec5SDimitry Andric   if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
14930b57cec5SDimitry Andric     lowerDynamicAreaOffset(II);
14940b57cec5SDimitry Andric     return;
14950b57cec5SDimitry Andric   }
14960b57cec5SDimitry Andric 
14970b57cec5SDimitry Andric   // Special case for dynamic alloca.
14980b57cec5SDimitry Andric   if (FPSI && FrameIndex == FPSI &&
14990b57cec5SDimitry Andric       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
15000b57cec5SDimitry Andric     lowerDynamicAlloc(II);
15010b57cec5SDimitry Andric     return;
15020b57cec5SDimitry Andric   }
15030b57cec5SDimitry Andric 
15045ffd83dbSDimitry Andric   if (FPSI && FrameIndex == FPSI &&
15055ffd83dbSDimitry Andric       (OpC == PPC::PREPARE_PROBED_ALLOCA_64 ||
1506590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_32 ||
1507590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64 ||
1508590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32)) {
15095ffd83dbSDimitry Andric     lowerPrepareProbedAlloca(II);
15105ffd83dbSDimitry Andric     return;
15115ffd83dbSDimitry Andric   }
15125ffd83dbSDimitry Andric 
15130b57cec5SDimitry Andric   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
15140b57cec5SDimitry Andric   if (OpC == PPC::SPILL_CR) {
15150b57cec5SDimitry Andric     lowerCRSpilling(II, FrameIndex);
15160b57cec5SDimitry Andric     return;
15170b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_CR) {
15180b57cec5SDimitry Andric     lowerCRRestore(II, FrameIndex);
15190b57cec5SDimitry Andric     return;
15200b57cec5SDimitry Andric   } else if (OpC == PPC::SPILL_CRBIT) {
15210b57cec5SDimitry Andric     lowerCRBitSpilling(II, FrameIndex);
15220b57cec5SDimitry Andric     return;
15230b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_CRBIT) {
15240b57cec5SDimitry Andric     lowerCRBitRestore(II, FrameIndex);
15250b57cec5SDimitry Andric     return;
1526e8d8bef9SDimitry Andric   } else if (OpC == PPC::SPILL_ACC || OpC == PPC::SPILL_UACC) {
1527e8d8bef9SDimitry Andric     lowerACCSpilling(II, FrameIndex);
15280b57cec5SDimitry Andric     return;
1529e8d8bef9SDimitry Andric   } else if (OpC == PPC::RESTORE_ACC || OpC == PPC::RESTORE_UACC) {
1530e8d8bef9SDimitry Andric     lowerACCRestore(II, FrameIndex);
15310b57cec5SDimitry Andric     return;
1532*81ad6265SDimitry Andric   } else if (OpC == PPC::STXVP && DisableAutoPairedVecSt) {
1533*81ad6265SDimitry Andric     lowerOctWordSpilling(II, FrameIndex);
1534*81ad6265SDimitry Andric     return;
1535fe6060f1SDimitry Andric   } else if (OpC == PPC::SPILL_QUADWORD) {
1536fe6060f1SDimitry Andric     lowerQuadwordSpilling(II, FrameIndex);
1537fe6060f1SDimitry Andric     return;
1538fe6060f1SDimitry Andric   } else if (OpC == PPC::RESTORE_QUADWORD) {
1539fe6060f1SDimitry Andric     lowerQuadwordRestore(II, FrameIndex);
1540fe6060f1SDimitry Andric     return;
15410b57cec5SDimitry Andric   }
15420b57cec5SDimitry Andric 
15430b57cec5SDimitry Andric   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
15440b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(
15450b57cec5SDimitry Andric     FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
15460b57cec5SDimitry Andric 
15470b57cec5SDimitry Andric   // If the instruction is not present in ImmToIdxMap, then it has no immediate
15480b57cec5SDimitry Andric   // form (and must be r+r).
15490b57cec5SDimitry Andric   bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
15500b57cec5SDimitry Andric                    OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
15510b57cec5SDimitry Andric 
15520b57cec5SDimitry Andric   // Now add the frame object offset to the offset from r1.
1553*81ad6265SDimitry Andric   int64_t Offset = MFI.getObjectOffset(FrameIndex);
15540b57cec5SDimitry Andric   Offset += MI.getOperand(OffsetOperandNo).getImm();
15550b57cec5SDimitry Andric 
15560b57cec5SDimitry Andric   // If we're not using a Frame Pointer that has been set to the value of the
15570b57cec5SDimitry Andric   // SP before having the stack size subtracted from it, then add the stack size
15580b57cec5SDimitry Andric   // to Offset to get the correct offset.
15590b57cec5SDimitry Andric   // Naked functions have stack size 0, although getStackSize may not reflect
15600b57cec5SDimitry Andric   // that because we didn't call all the pieces that compute it for naked
15610b57cec5SDimitry Andric   // functions.
15620b57cec5SDimitry Andric   if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
15630b57cec5SDimitry Andric     if (!(hasBasePointer(MF) && FrameIndex < 0))
15640b57cec5SDimitry Andric       Offset += MFI.getStackSize();
15650b57cec5SDimitry Andric   }
15660b57cec5SDimitry Andric 
1567fe6060f1SDimitry Andric   // If we encounter an LXVP/STXVP with an offset that doesn't fit, we can
1568fe6060f1SDimitry Andric   // transform it to the prefixed version so we don't have to use the XForm.
1569fe6060f1SDimitry Andric   if ((OpC == PPC::LXVP || OpC == PPC::STXVP) &&
1570fe6060f1SDimitry Andric       (!isInt<16>(Offset) || (Offset % offsetMinAlign(MI)) != 0) &&
1571fe6060f1SDimitry Andric       Subtarget.hasPrefixInstrs()) {
1572fe6060f1SDimitry Andric     unsigned NewOpc = OpC == PPC::LXVP ? PPC::PLXVP : PPC::PSTXVP;
1573fe6060f1SDimitry Andric     MI.setDesc(TII.get(NewOpc));
1574fe6060f1SDimitry Andric     OpC = NewOpc;
1575fe6060f1SDimitry Andric   }
1576fe6060f1SDimitry Andric 
15770b57cec5SDimitry Andric   // If we can, encode the offset directly into the instruction.  If this is a
15780b57cec5SDimitry Andric   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
15790b57cec5SDimitry Andric   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
15800b57cec5SDimitry Andric   // clear can be encoded.  This is extremely uncommon, because normally you
15810b57cec5SDimitry Andric   // only "std" to a stack slot that is at least 4-byte aligned, but it can
15820b57cec5SDimitry Andric   // happen in invalid code.
15830b57cec5SDimitry Andric   assert(OpC != PPC::DBG_VALUE &&
15840b57cec5SDimitry Andric          "This should be handled in a target-independent way");
1585fe6060f1SDimitry Andric   // FIXME: This should be factored out to a separate function as prefixed
1586fe6060f1SDimitry Andric   // instructions add a number of opcodes for which we can use 34-bit imm.
15870b57cec5SDimitry Andric   bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ?
15880b57cec5SDimitry Andric                             isUInt<8>(Offset) :
15890b57cec5SDimitry Andric                             isInt<16>(Offset);
1590349cc55cSDimitry Andric   if (TII.isPrefixed(MI.getOpcode()))
1591fe6060f1SDimitry Andric     OffsetFitsMnemonic = isInt<34>(Offset);
15920b57cec5SDimitry Andric   if (!noImmForm && ((OffsetFitsMnemonic &&
15930b57cec5SDimitry Andric                       ((Offset % offsetMinAlign(MI)) == 0)) ||
15940b57cec5SDimitry Andric                      OpC == TargetOpcode::STACKMAP ||
15950b57cec5SDimitry Andric                      OpC == TargetOpcode::PATCHPOINT)) {
15960b57cec5SDimitry Andric     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
15970b57cec5SDimitry Andric     return;
15980b57cec5SDimitry Andric   }
15990b57cec5SDimitry Andric 
16000b57cec5SDimitry Andric   // The offset doesn't fit into a single register, scavenge one to build the
16010b57cec5SDimitry Andric   // offset in.
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric   bool is64Bit = TM.isPPC64();
16040b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
16050b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
16060b57cec5SDimitry Andric   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
16075ffd83dbSDimitry Andric   Register SRegHi = MF.getRegInfo().createVirtualRegister(RC),
16080b57cec5SDimitry Andric            SReg = MF.getRegInfo().createVirtualRegister(RC);
1609*81ad6265SDimitry Andric   unsigned NewOpcode = 0u;
16100b57cec5SDimitry Andric 
16110b57cec5SDimitry Andric   // Insert a set of rA with the full offset value before the ld, st, or add
16120b57cec5SDimitry Andric   if (isInt<16>(Offset))
16130b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
16140b57cec5SDimitry Andric         .addImm(Offset);
1615*81ad6265SDimitry Andric   else if (isInt<32>(Offset)) {
16160b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
16170b57cec5SDimitry Andric         .addImm(Offset >> 16);
16180b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
16190b57cec5SDimitry Andric         .addReg(SRegHi, RegState::Kill)
16200b57cec5SDimitry Andric         .addImm(Offset);
1621*81ad6265SDimitry Andric   } else {
1622*81ad6265SDimitry Andric     assert(is64Bit && "Huge stack is only supported on PPC64");
1623*81ad6265SDimitry Andric     TII.materializeImmPostRA(MBB, II, dl, SReg, Offset);
16240b57cec5SDimitry Andric   }
16250b57cec5SDimitry Andric 
16260b57cec5SDimitry Andric   // Convert into indexed form of the instruction:
16270b57cec5SDimitry Andric   //
16280b57cec5SDimitry Andric   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
16290b57cec5SDimitry Andric   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
16300b57cec5SDimitry Andric   unsigned OperandBase;
16310b57cec5SDimitry Andric 
16320b57cec5SDimitry Andric   if (noImmForm)
16330b57cec5SDimitry Andric     OperandBase = 1;
16340b57cec5SDimitry Andric   else if (OpC != TargetOpcode::INLINEASM &&
16350b57cec5SDimitry Andric            OpC != TargetOpcode::INLINEASM_BR) {
16360b57cec5SDimitry Andric     assert(ImmToIdxMap.count(OpC) &&
16370b57cec5SDimitry Andric            "No indexed form of load or store available!");
1638*81ad6265SDimitry Andric     NewOpcode = ImmToIdxMap.find(OpC)->second;
16390b57cec5SDimitry Andric     MI.setDesc(TII.get(NewOpcode));
16400b57cec5SDimitry Andric     OperandBase = 1;
16410b57cec5SDimitry Andric   } else {
16420b57cec5SDimitry Andric     OperandBase = OffsetOperandNo;
16430b57cec5SDimitry Andric   }
16440b57cec5SDimitry Andric 
16458bcb0991SDimitry Andric   Register StackReg = MI.getOperand(FIOperandNum).getReg();
16460b57cec5SDimitry Andric   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
16470b57cec5SDimitry Andric   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
1648*81ad6265SDimitry Andric 
1649*81ad6265SDimitry Andric   // Since these are not real X-Form instructions, we must
1650*81ad6265SDimitry Andric   // add the registers and access 0(NewReg) rather than
1651*81ad6265SDimitry Andric   // emitting the X-Form pseudo.
1652*81ad6265SDimitry Andric   if (NewOpcode == PPC::LQX_PSEUDO || NewOpcode == PPC::STQX_PSEUDO) {
1653*81ad6265SDimitry Andric     assert(is64Bit && "Quadword loads/stores only supported in 64-bit mode");
1654*81ad6265SDimitry Andric     Register NewReg = MF.getRegInfo().createVirtualRegister(&PPC::G8RCRegClass);
1655*81ad6265SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADD8), NewReg)
1656*81ad6265SDimitry Andric         .addReg(SReg, RegState::Kill)
1657*81ad6265SDimitry Andric         .addReg(StackReg);
1658*81ad6265SDimitry Andric     MI.setDesc(TII.get(NewOpcode == PPC::LQX_PSEUDO ? PPC::LQ : PPC::STQ));
1659*81ad6265SDimitry Andric     MI.getOperand(OperandBase + 1).ChangeToRegister(NewReg, false);
1660*81ad6265SDimitry Andric     MI.getOperand(OperandBase).ChangeToImmediate(0);
1661*81ad6265SDimitry Andric   }
16620b57cec5SDimitry Andric }
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
16650b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
16660b57cec5SDimitry Andric 
16670b57cec5SDimitry Andric   if (!TM.isPPC64())
16680b57cec5SDimitry Andric     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
16690b57cec5SDimitry Andric   else
16700b57cec5SDimitry Andric     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
16710b57cec5SDimitry Andric }
16720b57cec5SDimitry Andric 
16730b57cec5SDimitry Andric Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
16740b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
16750b57cec5SDimitry Andric   if (!hasBasePointer(MF))
16760b57cec5SDimitry Andric     return getFrameRegister(MF);
16770b57cec5SDimitry Andric 
16780b57cec5SDimitry Andric   if (TM.isPPC64())
16790b57cec5SDimitry Andric     return PPC::X30;
16800b57cec5SDimitry Andric 
16810b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
16820b57cec5SDimitry Andric     return PPC::R29;
16830b57cec5SDimitry Andric 
16840b57cec5SDimitry Andric   return PPC::R30;
16850b57cec5SDimitry Andric }
16860b57cec5SDimitry Andric 
16870b57cec5SDimitry Andric bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
16880b57cec5SDimitry Andric   if (!EnableBasePointer)
16890b57cec5SDimitry Andric     return false;
16900b57cec5SDimitry Andric   if (AlwaysBasePointer)
16910b57cec5SDimitry Andric     return true;
16920b57cec5SDimitry Andric 
16930b57cec5SDimitry Andric   // If we need to realign the stack, then the stack pointer can no longer
16940b57cec5SDimitry Andric   // serve as an offset into the caller's stack space. As a result, we need a
16950b57cec5SDimitry Andric   // base pointer.
1696fe6060f1SDimitry Andric   return hasStackRealignment(MF);
16970b57cec5SDimitry Andric }
16980b57cec5SDimitry Andric 
16990b57cec5SDimitry Andric /// Returns true if the instruction's frame index
17000b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
17010b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
17020b57cec5SDimitry Andric /// references it should create new base registers for.
17030b57cec5SDimitry Andric bool PPCRegisterInfo::
17040b57cec5SDimitry Andric needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
17050b57cec5SDimitry Andric   assert(Offset < 0 && "Local offset must be negative");
17060b57cec5SDimitry Andric 
17070b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
17080b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
17090b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
17100b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
17110b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
17120b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
17130b57cec5SDimitry Andric 
17140b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores that have
17150b57cec5SDimitry Andric   // an r+i form. Return false for everything else.
17160b57cec5SDimitry Andric   unsigned OpC = MI->getOpcode();
17170b57cec5SDimitry Andric   if (!ImmToIdxMap.count(OpC))
17180b57cec5SDimitry Andric     return false;
17190b57cec5SDimitry Andric 
17200b57cec5SDimitry Andric   // Don't generate a new virtual base register just to add zero to it.
17210b57cec5SDimitry Andric   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
17220b57cec5SDimitry Andric       MI->getOperand(2).getImm() == 0)
17230b57cec5SDimitry Andric     return false;
17240b57cec5SDimitry Andric 
17250b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI->getParent();
17260b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
17270b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
17280b57cec5SDimitry Andric   unsigned StackEst = TFI->determineFrameLayout(MF, true);
17290b57cec5SDimitry Andric 
17300b57cec5SDimitry Andric   // If we likely don't need a stack frame, then we probably don't need a
17310b57cec5SDimitry Andric   // virtual base register either.
17320b57cec5SDimitry Andric   if (!StackEst)
17330b57cec5SDimitry Andric     return false;
17340b57cec5SDimitry Andric 
17350b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
17360b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
17370b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
17380b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
17390b57cec5SDimitry Andric   Offset += StackEst;
17400b57cec5SDimitry Andric 
17410b57cec5SDimitry Andric   // The frame pointer will point to the end of the stack, so estimate the
17420b57cec5SDimitry Andric   // offset as the difference between the object offset and the FP location.
17430b57cec5SDimitry Andric   return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
17440b57cec5SDimitry Andric }
17450b57cec5SDimitry Andric 
17460b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to
17470b57cec5SDimitry Andric /// be a pointer to FrameIdx at the beginning of the basic block.
1748e8d8bef9SDimitry Andric Register PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
17495ffd83dbSDimitry Andric                                                        int FrameIdx,
17500b57cec5SDimitry Andric                                                        int64_t Offset) const {
17510b57cec5SDimitry Andric   unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
17520b57cec5SDimitry Andric 
17530b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
17540b57cec5SDimitry Andric   DebugLoc DL;                  // Defaults to "unknown"
17550b57cec5SDimitry Andric   if (Ins != MBB->end())
17560b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
17590b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
17600b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
17610b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII.get(ADDriOpc);
17620b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1763e8d8bef9SDimitry Andric   const TargetRegisterClass *RC = getPointerRegClass(MF);
1764e8d8bef9SDimitry Andric   Register BaseReg = MRI.createVirtualRegister(RC);
17650b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
17660b57cec5SDimitry Andric 
17670b57cec5SDimitry Andric   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
17680b57cec5SDimitry Andric     .addFrameIndex(FrameIdx).addImm(Offset);
1769e8d8bef9SDimitry Andric 
1770e8d8bef9SDimitry Andric   return BaseReg;
17710b57cec5SDimitry Andric }
17720b57cec5SDimitry Andric 
17735ffd83dbSDimitry Andric void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
17740b57cec5SDimitry Andric                                         int64_t Offset) const {
17750b57cec5SDimitry Andric   unsigned FIOperandNum = 0;
17760b57cec5SDimitry Andric   while (!MI.getOperand(FIOperandNum).isFI()) {
17770b57cec5SDimitry Andric     ++FIOperandNum;
17780b57cec5SDimitry Andric     assert(FIOperandNum < MI.getNumOperands() &&
17790b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
17800b57cec5SDimitry Andric   }
17810b57cec5SDimitry Andric 
17820b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
17830b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
17840b57cec5SDimitry Andric   Offset += MI.getOperand(OffsetOperandNo).getImm();
17850b57cec5SDimitry Andric   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
17860b57cec5SDimitry Andric 
17870b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
17880b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
17890b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
17900b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
17910b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
17920b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
17930b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg,
17940b57cec5SDimitry Andric                         TII.getRegClass(MCID, FIOperandNum, this, MF));
17950b57cec5SDimitry Andric }
17960b57cec5SDimitry Andric 
17970b57cec5SDimitry Andric bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
17985ffd83dbSDimitry Andric                                          Register BaseReg,
17990b57cec5SDimitry Andric                                          int64_t Offset) const {
18000b57cec5SDimitry Andric   unsigned FIOperandNum = 0;
18010b57cec5SDimitry Andric   while (!MI->getOperand(FIOperandNum).isFI()) {
18020b57cec5SDimitry Andric     ++FIOperandNum;
18030b57cec5SDimitry Andric     assert(FIOperandNum < MI->getNumOperands() &&
18040b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
18050b57cec5SDimitry Andric   }
18060b57cec5SDimitry Andric 
18070b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
18080b57cec5SDimitry Andric   Offset += MI->getOperand(OffsetOperandNo).getImm();
18090b57cec5SDimitry Andric 
18100b57cec5SDimitry Andric   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
18110b57cec5SDimitry Andric          MI->getOpcode() == TargetOpcode::STACKMAP ||
18120b57cec5SDimitry Andric          MI->getOpcode() == TargetOpcode::PATCHPOINT ||
18130b57cec5SDimitry Andric          (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
18140b57cec5SDimitry Andric }
1815