xref: /freebsd/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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"
310b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
320b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
330b57cec5SDimitry Andric #include "llvm/IR/Function.h"
340b57cec5SDimitry Andric #include "llvm/IR/Type.h"
350b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
360b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
370b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
380b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
390b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
400b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
410b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
420b57cec5SDimitry Andric #include <cstdlib>
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric using namespace llvm;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric #define DEBUG_TYPE "reginfo"
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC
490b57cec5SDimitry Andric #include "PPCGenRegisterInfo.inc"
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass");
520b57cec5SDimitry Andric STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass");
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric static cl::opt<bool>
550b57cec5SDimitry Andric EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
560b57cec5SDimitry Andric          cl::desc("Enable use of a base pointer for complex stack frames"));
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric static cl::opt<bool>
590b57cec5SDimitry Andric AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
600b57cec5SDimitry Andric          cl::desc("Force the use of a base pointer in every function"));
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric static cl::opt<bool>
630b57cec5SDimitry Andric EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false),
640b57cec5SDimitry Andric          cl::desc("Enable spills from gpr to vsr rather than stack"));
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric static cl::opt<bool>
670b57cec5SDimitry Andric StackPtrConst("ppc-stack-ptr-caller-preserved",
680b57cec5SDimitry Andric                 cl::desc("Consider R1 caller preserved so stack saves of "
690b57cec5SDimitry Andric                          "caller preserved registers can be LICM candidates"),
700b57cec5SDimitry Andric                 cl::init(true), cl::Hidden);
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric static cl::opt<unsigned>
730b57cec5SDimitry Andric MaxCRBitSpillDist("ppc-max-crbit-spill-dist",
740b57cec5SDimitry Andric                   cl::desc("Maximum search distance for definition of CR bit "
750b57cec5SDimitry Andric                            "spill on ppc"),
760b57cec5SDimitry Andric                   cl::Hidden, cl::init(100));
770b57cec5SDimitry Andric 
78e8d8bef9SDimitry Andric // Copies/moves of physical accumulators are expensive operations
79e8d8bef9SDimitry Andric // that should be avoided whenever possible. MMA instructions are
80e8d8bef9SDimitry Andric // meant to be used in performance-sensitive computational kernels.
81e8d8bef9SDimitry Andric // This option is provided, at least for the time being, to give the
82e8d8bef9SDimitry Andric // user a tool to detect this expensive operation and either rework
83e8d8bef9SDimitry Andric // their code or report a compiler bug if that turns out to be the
84e8d8bef9SDimitry Andric // cause.
85e8d8bef9SDimitry Andric #ifndef NDEBUG
86e8d8bef9SDimitry Andric static cl::opt<bool>
87e8d8bef9SDimitry Andric ReportAccMoves("ppc-report-acc-moves",
88e8d8bef9SDimitry Andric                cl::desc("Emit information about accumulator register spills "
89e8d8bef9SDimitry Andric                         "and copies"),
90e8d8bef9SDimitry Andric                cl::Hidden, cl::init(false));
91e8d8bef9SDimitry Andric #endif
92e8d8bef9SDimitry Andric 
930b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC);
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
960b57cec5SDimitry Andric   : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
970b57cec5SDimitry Andric                        TM.isPPC64() ? 0 : 1,
980b57cec5SDimitry Andric                        TM.isPPC64() ? 0 : 1),
990b57cec5SDimitry Andric     TM(TM) {
1000b57cec5SDimitry Andric   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
1010b57cec5SDimitry Andric   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
1020b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
1030b57cec5SDimitry Andric   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
1040b57cec5SDimitry Andric   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
1050b57cec5SDimitry Andric   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
1060b57cec5SDimitry Andric   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
1070b57cec5SDimitry Andric   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
1080b57cec5SDimitry Andric   ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   // 64-bit
1110b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
1120b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
1130b57cec5SDimitry Andric   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
1140b57cec5SDimitry Andric   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
1150b57cec5SDimitry Andric   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // VSX
1180b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX;
1190b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX;
1200b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX;
1210b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX;
1220b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX;
1230b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX;
1240b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXV] = PPC::LXVX;
1250b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXSD] = PPC::LXSDX;
1260b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX;
1270b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXV] = PPC::STXVX;
1280b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXSD] = PPC::STXSDX;
1290b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   // SPE
1320b57cec5SDimitry Andric   ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX;
1330b57cec5SDimitry Andric   ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX;
1340b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX;
1350b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX;
136*fe6060f1SDimitry Andric 
137*fe6060f1SDimitry Andric   // Power10
138*fe6060f1SDimitry Andric   ImmToIdxMap[PPC::LXVP]   = PPC::LXVPX;
139*fe6060f1SDimitry Andric   ImmToIdxMap[PPC::STXVP]  = PPC::STXVPX;
140*fe6060f1SDimitry Andric   ImmToIdxMap[PPC::PLXVP]  = PPC::LXVPX;
141*fe6060f1SDimitry Andric   ImmToIdxMap[PPC::PSTXVP] = PPC::STXVPX;
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric /// getPointerRegClass - Return the register class to use to hold pointers.
1450b57cec5SDimitry Andric /// This is used for addressing modes.
1460b57cec5SDimitry Andric const TargetRegisterClass *
1470b57cec5SDimitry Andric PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
1480b57cec5SDimitry Andric                                                                        const {
1490b57cec5SDimitry Andric   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
1500b57cec5SDimitry Andric   // when it checks for ZERO folding.
1510b57cec5SDimitry Andric   if (Kind == 1) {
1520b57cec5SDimitry Andric     if (TM.isPPC64())
1530b57cec5SDimitry Andric       return &PPC::G8RC_NOX0RegClass;
1540b57cec5SDimitry Andric     return &PPC::GPRC_NOR0RegClass;
1550b57cec5SDimitry Andric   }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   if (TM.isPPC64())
1580b57cec5SDimitry Andric     return &PPC::G8RCRegClass;
1590b57cec5SDimitry Andric   return &PPC::GPRCRegClass;
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric const MCPhysReg*
1630b57cec5SDimitry Andric PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
1640b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
1650b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) {
1665ffd83dbSDimitry Andric     if (!TM.isPPC64() && Subtarget.isAIXABI())
1675ffd83dbSDimitry Andric       report_fatal_error("AnyReg unimplemented on 32-bit AIX.");
168*fe6060f1SDimitry Andric     if (Subtarget.hasVSX()) {
169*fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
170*fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_VSX_SaveList;
1710b57cec5SDimitry Andric       return CSR_64_AllRegs_VSX_SaveList;
172*fe6060f1SDimitry Andric     }
173*fe6060f1SDimitry Andric     if (Subtarget.hasAltivec()) {
174*fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
175*fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_Altivec_SaveList;
1760b57cec5SDimitry Andric       return CSR_64_AllRegs_Altivec_SaveList;
177*fe6060f1SDimitry Andric     }
1780b57cec5SDimitry Andric     return CSR_64_AllRegs_SaveList;
1790b57cec5SDimitry Andric   }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   // On PPC64, we might need to save r2 (but only if it is not reserved).
1825ffd83dbSDimitry Andric   // We do not need to treat R2 as callee-saved when using PC-Relative calls
1835ffd83dbSDimitry Andric   // because any direct uses of R2 will cause it to be reserved. If the function
1845ffd83dbSDimitry Andric   // is a leaf or the only uses of R2 are implicit uses for calls, the calls
1855ffd83dbSDimitry Andric   // will use the @notoc relocation which will cause this function to set the
1865ffd83dbSDimitry Andric   // st_other bit to 1, thereby communicating to its caller that it arbitrarily
1875ffd83dbSDimitry Andric   // clobbers the TOC.
1885ffd83dbSDimitry Andric   bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) &&
1895ffd83dbSDimitry Andric                 !Subtarget.isUsingPCRelativeCalls();
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   // Cold calling convention CSRs.
1920b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::Cold) {
1935ffd83dbSDimitry Andric     if (Subtarget.isAIXABI())
1945ffd83dbSDimitry Andric       report_fatal_error("Cold calling unimplemented on AIX.");
1950b57cec5SDimitry Andric     if (TM.isPPC64()) {
1960b57cec5SDimitry Andric       if (Subtarget.hasAltivec())
1970b57cec5SDimitry Andric         return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
1980b57cec5SDimitry Andric                       : CSR_SVR64_ColdCC_Altivec_SaveList;
1990b57cec5SDimitry Andric       return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
2000b57cec5SDimitry Andric                     : CSR_SVR64_ColdCC_SaveList;
2010b57cec5SDimitry Andric     }
2020b57cec5SDimitry Andric     // 32-bit targets.
2030b57cec5SDimitry Andric     if (Subtarget.hasAltivec())
2040b57cec5SDimitry Andric       return CSR_SVR32_ColdCC_Altivec_SaveList;
2050b57cec5SDimitry Andric     else if (Subtarget.hasSPE())
2060b57cec5SDimitry Andric       return CSR_SVR32_ColdCC_SPE_SaveList;
2070b57cec5SDimitry Andric     return CSR_SVR32_ColdCC_SaveList;
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric   // Standard calling convention CSRs.
2100b57cec5SDimitry Andric   if (TM.isPPC64()) {
211*fe6060f1SDimitry Andric     if (Subtarget.hasAltivec() &&
212*fe6060f1SDimitry Andric         (!Subtarget.isAIXABI() || TM.getAIXExtendedAltivecABI())) {
2135ffd83dbSDimitry Andric       return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList
2145ffd83dbSDimitry Andric                     : CSR_PPC64_Altivec_SaveList;
215*fe6060f1SDimitry Andric     }
2165ffd83dbSDimitry Andric     return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList;
2170b57cec5SDimitry Andric   }
2180b57cec5SDimitry Andric   // 32-bit targets.
219e8d8bef9SDimitry Andric   if (Subtarget.isAIXABI()) {
220e8d8bef9SDimitry Andric     if (Subtarget.hasAltivec())
221*fe6060f1SDimitry Andric       return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_Altivec_SaveList
222*fe6060f1SDimitry Andric                                            : CSR_AIX32_SaveList;
2235ffd83dbSDimitry Andric     return CSR_AIX32_SaveList;
224e8d8bef9SDimitry Andric   }
2250b57cec5SDimitry Andric   if (Subtarget.hasAltivec())
2260b57cec5SDimitry Andric     return CSR_SVR432_Altivec_SaveList;
2270b57cec5SDimitry Andric   else if (Subtarget.hasSPE())
2280b57cec5SDimitry Andric     return CSR_SVR432_SPE_SaveList;
2290b57cec5SDimitry Andric   return CSR_SVR432_SaveList;
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric const uint32_t *
2330b57cec5SDimitry Andric PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
2340b57cec5SDimitry Andric                                       CallingConv::ID CC) const {
2350b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
2360b57cec5SDimitry Andric   if (CC == CallingConv::AnyReg) {
237*fe6060f1SDimitry Andric     if (Subtarget.hasVSX()) {
238*fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
239*fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_VSX_RegMask;
2400b57cec5SDimitry Andric       return CSR_64_AllRegs_VSX_RegMask;
241*fe6060f1SDimitry Andric     }
242*fe6060f1SDimitry Andric     if (Subtarget.hasAltivec()) {
243*fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
244*fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_Altivec_RegMask;
2450b57cec5SDimitry Andric       return CSR_64_AllRegs_Altivec_RegMask;
246*fe6060f1SDimitry Andric     }
2470b57cec5SDimitry Andric     return CSR_64_AllRegs_RegMask;
2480b57cec5SDimitry Andric   }
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   if (Subtarget.isAIXABI()) {
251*fe6060f1SDimitry Andric     return TM.isPPC64()
252*fe6060f1SDimitry Andric                ? ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
253*fe6060f1SDimitry Andric                       ? CSR_PPC64_Altivec_RegMask
254e8d8bef9SDimitry Andric                       : CSR_PPC64_RegMask)
255*fe6060f1SDimitry Andric                : ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
256*fe6060f1SDimitry Andric                       ? CSR_AIX32_Altivec_RegMask
257e8d8bef9SDimitry Andric                       : CSR_AIX32_RegMask);
2580b57cec5SDimitry Andric   }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   if (CC == CallingConv::Cold) {
2610b57cec5SDimitry Andric     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
2620b57cec5SDimitry Andric                                                   : CSR_SVR64_ColdCC_RegMask)
2630b57cec5SDimitry Andric                         : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
2640b57cec5SDimitry Andric                                                   : (Subtarget.hasSPE()
2650b57cec5SDimitry Andric                                                   ? CSR_SVR32_ColdCC_SPE_RegMask
2660b57cec5SDimitry Andric                                                   : CSR_SVR32_ColdCC_RegMask));
2670b57cec5SDimitry Andric   }
2680b57cec5SDimitry Andric 
2695ffd83dbSDimitry Andric   return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask
2705ffd83dbSDimitry Andric                                                 : CSR_PPC64_RegMask)
2715ffd83dbSDimitry Andric                       : (Subtarget.hasAltivec()
2725ffd83dbSDimitry Andric                              ? CSR_SVR432_Altivec_RegMask
2735ffd83dbSDimitry Andric                              : (Subtarget.hasSPE() ? CSR_SVR432_SPE_RegMask
2740b57cec5SDimitry Andric                                                    : CSR_SVR432_RegMask));
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric const uint32_t*
2780b57cec5SDimitry Andric PPCRegisterInfo::getNoPreservedMask() const {
2790b57cec5SDimitry Andric   return CSR_NoRegs_RegMask;
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
2830b57cec5SDimitry Andric   for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
2840b57cec5SDimitry Andric     Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
2880b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
2890b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
2900b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   // The ZERO register is not really a register, but the representation of r0
2930b57cec5SDimitry Andric   // when used in instructions that treat r0 as the constant 0.
2940b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::ZERO);
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   // The FP register is also not really a register, but is the representation
2970b57cec5SDimitry Andric   // of the frame pointer register used by ISD::FRAMEADDR.
2980b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::FP);
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric   // The BP register is also not really a register, but is the representation
3010b57cec5SDimitry Andric   // of the base pointer register used by setjmp.
3020b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::BP);
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   // The counter registers must be reserved so that counter-based loops can
3050b57cec5SDimitry Andric   // be correctly formed (and the mtctr instructions are not DCE'd).
3060b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::CTR);
3070b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::CTR8);
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::R1);
3100b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::LR);
3110b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::LR8);
3120b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::RM);
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::VRSAVE);
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   // The SVR4 ABI reserves r2 and r13
3170b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI()) {
3180b57cec5SDimitry Andric     // We only reserve r2 if we need to use the TOC pointer. If we have no
3190b57cec5SDimitry Andric     // explicit uses of the TOC pointer (meaning we're a leaf function with
3200b57cec5SDimitry Andric     // no constant-pool loads, etc.) and we have no potential uses inside an
3210b57cec5SDimitry Andric     // inline asm block, then we can treat r2 has an ordinary callee-saved
3220b57cec5SDimitry Andric     // register.
3230b57cec5SDimitry Andric     const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3240b57cec5SDimitry Andric     if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
3250b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R2);  // System-reserved register
3260b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
3270b57cec5SDimitry Andric   }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   // Always reserve r2 on AIX for now.
3300b57cec5SDimitry Andric   // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions.
3310b57cec5SDimitry Andric   if (Subtarget.isAIXABI())
3320b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R2);  // System-reserved register
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   // On PPC64, r13 is the thread pointer. Never allocate this register.
3350b57cec5SDimitry Andric   if (TM.isPPC64())
3360b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R13);
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   if (TFI->needsFP(MF))
3390b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R31);
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   bool IsPositionIndependent = TM.isPositionIndependent();
3420b57cec5SDimitry Andric   if (hasBasePointer(MF)) {
3438bcb0991SDimitry Andric     if (Subtarget.is32BitELFABI() && IsPositionIndependent)
3440b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R29);
3450b57cec5SDimitry Andric     else
3460b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R30);
3470b57cec5SDimitry Andric   }
3480b57cec5SDimitry Andric 
3498bcb0991SDimitry Andric   if (Subtarget.is32BitELFABI() && IsPositionIndependent)
3500b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R30);
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   // Reserve Altivec registers when Altivec is unavailable.
3530b57cec5SDimitry Andric   if (!Subtarget.hasAltivec())
3540b57cec5SDimitry Andric     for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
3550b57cec5SDimitry Andric          IE = PPC::VRRCRegClass.end(); I != IE; ++I)
3560b57cec5SDimitry Andric       markSuperRegs(Reserved, *I);
3570b57cec5SDimitry Andric 
358*fe6060f1SDimitry Andric   if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
359*fe6060f1SDimitry Andric       !TM.getAIXExtendedAltivecABI()) {
360*fe6060f1SDimitry Andric     //  In the AIX default Altivec ABI, vector registers VR20-VR31 are reserved
361*fe6060f1SDimitry Andric     //  and cannot be used.
362*fe6060f1SDimitry Andric     for (auto Reg : CSR_Altivec_SaveList) {
363*fe6060f1SDimitry Andric       if (Reg == 0)
364*fe6060f1SDimitry Andric         break;
365*fe6060f1SDimitry Andric       markSuperRegs(Reserved, Reg);
366*fe6060f1SDimitry Andric       for (MCRegAliasIterator AS(Reg, this, true); AS.isValid(); ++AS) {
367*fe6060f1SDimitry Andric         Reserved.set(*AS);
368*fe6060f1SDimitry Andric       }
369*fe6060f1SDimitry Andric     }
370*fe6060f1SDimitry Andric   }
371*fe6060f1SDimitry Andric 
3720b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
3730b57cec5SDimitry Andric   return Reserved;
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const {
3770b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
3780b57cec5SDimitry Andric   const PPCInstrInfo *InstrInfo =  Subtarget.getInstrInfo();
3790b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
3800b57cec5SDimitry Andric   const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
3810b57cec5SDimitry Andric 
382*fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "requiresFrameIndexScavenging for " << MF.getName()
383*fe6060f1SDimitry Andric                     << ".\n");
3840b57cec5SDimitry Andric   // If the callee saved info is invalid we have to default to true for safety.
385*fe6060f1SDimitry Andric   if (!MFI.isCalleeSavedInfoValid()) {
386*fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "TRUE - Invalid callee saved info.\n");
3870b57cec5SDimitry Andric     return true;
388*fe6060f1SDimitry Andric   }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   // We will require the use of X-Forms because the frame is larger than what
3910b57cec5SDimitry Andric   // can be represented in signed 16 bits that fit in the immediate of a D-Form.
3920b57cec5SDimitry Andric   // If we need an X-Form then we need a register to store the address offset.
3930b57cec5SDimitry Andric   unsigned FrameSize = MFI.getStackSize();
3940b57cec5SDimitry Andric   // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
395*fe6060f1SDimitry Andric   if (FrameSize & ~0x7FFF) {
396*fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "TRUE - Frame size is too large for D-Form.\n");
3970b57cec5SDimitry Andric     return true;
398*fe6060f1SDimitry Andric   }
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric   // The callee saved info is valid so it can be traversed.
4010b57cec5SDimitry Andric   // Checking for registers that need saving that do not have load or store
4020b57cec5SDimitry Andric   // forms where the address offset is an immediate.
4030b57cec5SDimitry Andric   for (unsigned i = 0; i < Info.size(); i++) {
404*fe6060f1SDimitry Andric     // If the spill is to a register no scavenging is required.
405*fe6060f1SDimitry Andric     if (Info[i].isSpilledToReg())
406*fe6060f1SDimitry Andric       continue;
407*fe6060f1SDimitry Andric 
4080b57cec5SDimitry Andric     int FrIdx = Info[i].getFrameIdx();
4090b57cec5SDimitry Andric     unsigned Reg = Info[i].getReg();
4100b57cec5SDimitry Andric 
4115ffd83dbSDimitry Andric     const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg);
4125ffd83dbSDimitry Andric     unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC);
4130b57cec5SDimitry Andric     if (!MFI.isFixedObjectIndex(FrIdx)) {
4140b57cec5SDimitry Andric       // This is not a fixed object. If it requires alignment then we may still
4150b57cec5SDimitry Andric       // need to use the XForm.
416*fe6060f1SDimitry Andric       if (offsetMinAlignForOpcode(Opcode) > 1) {
417*fe6060f1SDimitry Andric         LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
418*fe6060f1SDimitry Andric                           << " for register " << printReg(Reg, this) << ".\n");
419*fe6060f1SDimitry Andric         LLVM_DEBUG(dbgs() << "TRUE - Not fixed frame object that requires "
420*fe6060f1SDimitry Andric                           << "alignment.\n");
4210b57cec5SDimitry Andric         return true;
4220b57cec5SDimitry Andric       }
423*fe6060f1SDimitry Andric     }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric     // This is eiher:
4260b57cec5SDimitry Andric     // 1) A fixed frame index object which we know are aligned so
4270b57cec5SDimitry Andric     // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
428480093f4SDimitry Andric     // need to consider the alignment here.
4290b57cec5SDimitry Andric     // 2) A not fixed object but in that case we now know that the min required
4300b57cec5SDimitry Andric     // alignment is no more than 1 based on the previous check.
431*fe6060f1SDimitry Andric     if (InstrInfo->isXFormMemOp(Opcode)) {
432*fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
433*fe6060f1SDimitry Andric                         << " for register " << printReg(Reg, this) << ".\n");
434*fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "TRUE - Memory operand is X-Form.\n");
4350b57cec5SDimitry Andric       return true;
4360b57cec5SDimitry Andric     }
437*fe6060f1SDimitry Andric   }
438*fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "FALSE - Scavenging is not required.\n");
4390b57cec5SDimitry Andric   return false;
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric 
442*fe6060f1SDimitry Andric bool PPCRegisterInfo::requiresVirtualBaseRegisters(
443*fe6060f1SDimitry Andric     const MachineFunction &MF) const {
444*fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
445*fe6060f1SDimitry Andric   // Do not use virtual base registers when ROP protection is turned on.
446*fe6060f1SDimitry Andric   // Virtual base registers break the layout of the local variable space and may
447*fe6060f1SDimitry Andric   // push the ROP Hash location past the 512 byte range of the ROP store
448*fe6060f1SDimitry Andric   // instruction.
449*fe6060f1SDimitry Andric   return !Subtarget.hasROPProtect();
450*fe6060f1SDimitry Andric }
451*fe6060f1SDimitry Andric 
4525ffd83dbSDimitry Andric bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg,
4530b57cec5SDimitry Andric                                                const MachineFunction &MF) const {
4548bcb0991SDimitry Andric   assert(Register::isPhysicalRegister(PhysReg));
4550b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
4560b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4570b57cec5SDimitry Andric 
458*fe6060f1SDimitry Andric   if (!Subtarget.is64BitELFABI() && !Subtarget.isAIXABI())
4590b57cec5SDimitry Andric     return false;
460*fe6060f1SDimitry Andric   if (PhysReg == Subtarget.getTOCPointerRegister())
461*fe6060f1SDimitry Andric     // X2/R2 is guaranteed to be preserved within a function if it is reserved.
4620b57cec5SDimitry Andric     // The reason it's reserved is that it's the TOC pointer (and the function
4630b57cec5SDimitry Andric     // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
4640b57cec5SDimitry Andric     // with no TOC access), we can't claim that it is preserved.
465*fe6060f1SDimitry Andric     return (getReservedRegs(MF).test(PhysReg));
466*fe6060f1SDimitry Andric   if (StackPtrConst && PhysReg == Subtarget.getStackPointerRegister() &&
467*fe6060f1SDimitry Andric       !MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment())
4680b57cec5SDimitry Andric     // The value of the stack pointer does not change within a function after
4690b57cec5SDimitry Andric     // the prologue and before the epilogue if there are no dynamic allocations
470*fe6060f1SDimitry Andric     // and no inline asm which clobbers X1/R1.
4710b57cec5SDimitry Andric     return true;
4720b57cec5SDimitry Andric   return false;
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric 
475*fe6060f1SDimitry Andric bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
476*fe6060f1SDimitry Andric                                             ArrayRef<MCPhysReg> Order,
477*fe6060f1SDimitry Andric                                             SmallVectorImpl<MCPhysReg> &Hints,
478*fe6060f1SDimitry Andric                                             const MachineFunction &MF,
479*fe6060f1SDimitry Andric                                             const VirtRegMap *VRM,
480*fe6060f1SDimitry Andric                                             const LiveRegMatrix *Matrix) const {
481*fe6060f1SDimitry Andric   const MachineRegisterInfo *MRI = &MF.getRegInfo();
482*fe6060f1SDimitry Andric 
483*fe6060f1SDimitry Andric   // Call the base implementation first to set any hints based on the usual
484*fe6060f1SDimitry Andric   // heuristics and decide what the return value should be. We want to return
485*fe6060f1SDimitry Andric   // the same value returned by the base implementation. If the base
486*fe6060f1SDimitry Andric   // implementation decides to return true and force the allocation then we
487*fe6060f1SDimitry Andric   // will leave it as such. On the other hand if the base implementation
488*fe6060f1SDimitry Andric   // decides to return false the following code will not force the allocation
489*fe6060f1SDimitry Andric   // as we are just looking to provide a hint.
490*fe6060f1SDimitry Andric   bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
491*fe6060f1SDimitry Andric       VirtReg, Order, Hints, MF, VRM, Matrix);
492*fe6060f1SDimitry Andric   // We are interested in instructions that copy values to ACC/UACC.
493*fe6060f1SDimitry Andric   // The copy into UACC will be simply a COPY to a subreg so we
494*fe6060f1SDimitry Andric   // want to allocate the corresponding physical subreg for the source.
495*fe6060f1SDimitry Andric   // The copy into ACC will be a BUILD_UACC so we want to allocate
496*fe6060f1SDimitry Andric   // the same number UACC for the source.
497*fe6060f1SDimitry Andric   for (MachineInstr &Use : MRI->reg_nodbg_instructions(VirtReg)) {
498*fe6060f1SDimitry Andric     const MachineOperand *ResultOp = nullptr;
499*fe6060f1SDimitry Andric     Register ResultReg;
500*fe6060f1SDimitry Andric     switch (Use.getOpcode()) {
501*fe6060f1SDimitry Andric     case TargetOpcode::COPY: {
502*fe6060f1SDimitry Andric       ResultOp = &Use.getOperand(0);
503*fe6060f1SDimitry Andric       ResultReg = ResultOp->getReg();
504*fe6060f1SDimitry Andric       if (Register::isVirtualRegister(ResultReg) &&
505*fe6060f1SDimitry Andric           MRI->getRegClass(ResultReg)->contains(PPC::UACC0) &&
506*fe6060f1SDimitry Andric           VRM->hasPhys(ResultReg)) {
507*fe6060f1SDimitry Andric         Register UACCPhys = VRM->getPhys(ResultReg);
508*fe6060f1SDimitry Andric         Register HintReg = getSubReg(UACCPhys, ResultOp->getSubReg());
509*fe6060f1SDimitry Andric         Hints.push_back(HintReg);
510*fe6060f1SDimitry Andric       }
511*fe6060f1SDimitry Andric       break;
512*fe6060f1SDimitry Andric     }
513*fe6060f1SDimitry Andric     case PPC::BUILD_UACC: {
514*fe6060f1SDimitry Andric       ResultOp = &Use.getOperand(0);
515*fe6060f1SDimitry Andric       ResultReg = ResultOp->getReg();
516*fe6060f1SDimitry Andric       if (MRI->getRegClass(ResultReg)->contains(PPC::ACC0) &&
517*fe6060f1SDimitry Andric           VRM->hasPhys(ResultReg)) {
518*fe6060f1SDimitry Andric         Register ACCPhys = VRM->getPhys(ResultReg);
519*fe6060f1SDimitry Andric         assert((ACCPhys >= PPC::ACC0 && ACCPhys <= PPC::ACC7) &&
520*fe6060f1SDimitry Andric                "Expecting an ACC register for BUILD_UACC.");
521*fe6060f1SDimitry Andric         Register HintReg = PPC::UACC0 + (ACCPhys - PPC::ACC0);
522*fe6060f1SDimitry Andric         Hints.push_back(HintReg);
523*fe6060f1SDimitry Andric       }
524*fe6060f1SDimitry Andric       break;
525*fe6060f1SDimitry Andric     }
526*fe6060f1SDimitry Andric     }
527*fe6060f1SDimitry Andric   }
528*fe6060f1SDimitry Andric   return BaseImplRetVal;
529*fe6060f1SDimitry Andric }
530*fe6060f1SDimitry Andric 
5310b57cec5SDimitry Andric unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
5320b57cec5SDimitry Andric                                               MachineFunction &MF) const {
5330b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
5340b57cec5SDimitry Andric   const unsigned DefaultSafety = 1;
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   switch (RC->getID()) {
5370b57cec5SDimitry Andric   default:
5380b57cec5SDimitry Andric     return 0;
5390b57cec5SDimitry Andric   case PPC::G8RC_NOX0RegClassID:
5400b57cec5SDimitry Andric   case PPC::GPRC_NOR0RegClassID:
5410b57cec5SDimitry Andric   case PPC::SPERCRegClassID:
5420b57cec5SDimitry Andric   case PPC::G8RCRegClassID:
5430b57cec5SDimitry Andric   case PPC::GPRCRegClassID: {
5440b57cec5SDimitry Andric     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
5450b57cec5SDimitry Andric     return 32 - FP - DefaultSafety;
5460b57cec5SDimitry Andric   }
5470b57cec5SDimitry Andric   case PPC::F4RCRegClassID:
548*fe6060f1SDimitry Andric   case PPC::F8RCRegClassID:
5490b57cec5SDimitry Andric   case PPC::VSLRCRegClassID:
5500b57cec5SDimitry Andric     return 32 - DefaultSafety;
551*fe6060f1SDimitry Andric   case PPC::VFRCRegClassID:
552*fe6060f1SDimitry Andric   case PPC::VRRCRegClassID: {
553*fe6060f1SDimitry Andric     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
554*fe6060f1SDimitry Andric     // Vector registers VR20-VR31 are reserved and cannot be used in the default
555*fe6060f1SDimitry Andric     // Altivec ABI on AIX.
556*fe6060f1SDimitry Andric     if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
557*fe6060f1SDimitry Andric       return 20 - DefaultSafety;
558*fe6060f1SDimitry Andric   }
559*fe6060f1SDimitry Andric     return 32 - DefaultSafety;
5600b57cec5SDimitry Andric   case PPC::VSFRCRegClassID:
5610b57cec5SDimitry Andric   case PPC::VSSRCRegClassID:
562*fe6060f1SDimitry Andric   case PPC::VSRCRegClassID: {
563*fe6060f1SDimitry Andric     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
564*fe6060f1SDimitry Andric     if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
565*fe6060f1SDimitry Andric       // Vector registers VR20-VR31 are reserved and cannot be used in the
566*fe6060f1SDimitry Andric       // default Altivec ABI on AIX.
567*fe6060f1SDimitry Andric       return 52 - DefaultSafety;
568*fe6060f1SDimitry Andric   }
5690b57cec5SDimitry Andric     return 64 - DefaultSafety;
5700b57cec5SDimitry Andric   case PPC::CRRCRegClassID:
5710b57cec5SDimitry Andric     return 8 - DefaultSafety;
5720b57cec5SDimitry Andric   }
5730b57cec5SDimitry Andric }
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric const TargetRegisterClass *
5760b57cec5SDimitry Andric PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
5770b57cec5SDimitry Andric                                            const MachineFunction &MF) const {
5780b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
579*fe6060f1SDimitry Andric   const auto *DefaultSuperclass =
580*fe6060f1SDimitry Andric       TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
5810b57cec5SDimitry Andric   if (Subtarget.hasVSX()) {
5820b57cec5SDimitry Andric     // With VSX, we can inflate various sub-register classes to the full VSX
5830b57cec5SDimitry Andric     // register set.
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric     // For Power9 we allow the user to enable GPR to vector spills.
5860b57cec5SDimitry Andric     // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
5870b57cec5SDimitry Andric     // support to spill GPRC.
588*fe6060f1SDimitry Andric     if (TM.isELFv2ABI() || Subtarget.isAIXABI()) {
5890b57cec5SDimitry Andric       if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
5900b57cec5SDimitry Andric           RC == &PPC::G8RCRegClass) {
5910b57cec5SDimitry Andric         InflateGP8RC++;
5920b57cec5SDimitry Andric         return &PPC::SPILLTOVSRRCRegClass;
5930b57cec5SDimitry Andric       }
5940b57cec5SDimitry Andric       if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
5950b57cec5SDimitry Andric         InflateGPRC++;
5960b57cec5SDimitry Andric     }
597*fe6060f1SDimitry Andric 
598*fe6060f1SDimitry Andric     for (const auto *I = RC->getSuperClasses(); *I; ++I) {
599*fe6060f1SDimitry Andric       if (getRegSizeInBits(**I) != getRegSizeInBits(*RC))
600*fe6060f1SDimitry Andric         continue;
601*fe6060f1SDimitry Andric 
602*fe6060f1SDimitry Andric       switch ((*I)->getID()) {
603*fe6060f1SDimitry Andric       case PPC::VSSRCRegClassID:
604*fe6060f1SDimitry Andric         return Subtarget.hasP8Vector() ? *I : DefaultSuperclass;
605*fe6060f1SDimitry Andric       case PPC::VSFRCRegClassID:
606*fe6060f1SDimitry Andric       case PPC::VSRCRegClassID:
607*fe6060f1SDimitry Andric         return *I;
608*fe6060f1SDimitry Andric       case PPC::VSRpRCRegClassID:
609*fe6060f1SDimitry Andric         return Subtarget.pairedVectorMemops() ? *I : DefaultSuperclass;
610*fe6060f1SDimitry Andric       case PPC::ACCRCRegClassID:
611*fe6060f1SDimitry Andric       case PPC::UACCRCRegClassID:
612*fe6060f1SDimitry Andric         return Subtarget.hasMMA() ? *I : DefaultSuperclass;
613*fe6060f1SDimitry Andric       }
614*fe6060f1SDimitry Andric     }
6150b57cec5SDimitry Andric   }
6160b57cec5SDimitry Andric 
617*fe6060f1SDimitry Andric   return DefaultSuperclass;
6180b57cec5SDimitry Andric }
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6210b57cec5SDimitry Andric // Stack Frame Processing methods
6220b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric /// lowerDynamicAlloc - Generate the code for allocating an object in the
6250b57cec5SDimitry Andric /// current frame.  The sequence of code will be in the general form
6260b57cec5SDimitry Andric ///
6270b57cec5SDimitry Andric ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
6280b57cec5SDimitry Andric ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
6290b57cec5SDimitry Andric ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
6300b57cec5SDimitry Andric ///
6310b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
6320b57cec5SDimitry Andric   // Get the instruction.
6330b57cec5SDimitry Andric   MachineInstr &MI = *II;
6340b57cec5SDimitry Andric   // Get the instruction's basic block.
6350b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
6360b57cec5SDimitry Andric   // Get the basic block's function.
6370b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
6380b57cec5SDimitry Andric   // Get the frame info.
6390b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
6400b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
6410b57cec5SDimitry Andric   // Get the instruction info.
6420b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
6430b57cec5SDimitry Andric   // Determine whether 64-bit pointers are used.
6440b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
6450b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric   // Get the maximum call stack size.
6480b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
6495ffd83dbSDimitry Andric   Align MaxAlign = MFI.getMaxAlign();
6505ffd83dbSDimitry Andric   assert(isAligned(MaxAlign, maxCallFrameSize) &&
6510b57cec5SDimitry Andric          "Maximum call-frame size not sufficiently aligned");
6525ffd83dbSDimitry Andric   (void)MaxAlign;
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
6550b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
6568bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
6570b57cec5SDimitry Andric   bool KillNegSizeReg = MI.getOperand(1).isKill();
6588bcb0991SDimitry Andric   Register NegSizeReg = MI.getOperand(1).getReg();
6590b57cec5SDimitry Andric 
6605ffd83dbSDimitry Andric   prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg);
6610b57cec5SDimitry Andric   // Grow the stack and update the stack pointer link, then determine the
6620b57cec5SDimitry Andric   // address of new allocated space.
6630b57cec5SDimitry Andric   if (LP64) {
6640b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
6650b57cec5SDimitry Andric         .addReg(Reg, RegState::Kill)
6660b57cec5SDimitry Andric         .addReg(PPC::X1)
6670b57cec5SDimitry Andric         .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
6680b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
6690b57cec5SDimitry Andric         .addReg(PPC::X1)
6700b57cec5SDimitry Andric         .addImm(maxCallFrameSize);
6710b57cec5SDimitry Andric   } else {
6720b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
6730b57cec5SDimitry Andric         .addReg(Reg, RegState::Kill)
6740b57cec5SDimitry Andric         .addReg(PPC::R1)
6750b57cec5SDimitry Andric         .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
6760b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
6770b57cec5SDimitry Andric         .addReg(PPC::R1)
6780b57cec5SDimitry Andric         .addImm(maxCallFrameSize);
6790b57cec5SDimitry Andric   }
6800b57cec5SDimitry Andric 
6810b57cec5SDimitry Andric   // Discard the DYNALLOC instruction.
6820b57cec5SDimitry Andric   MBB.erase(II);
6830b57cec5SDimitry Andric }
6840b57cec5SDimitry Andric 
6855ffd83dbSDimitry Andric /// To accomplish dynamic stack allocation, we have to calculate exact size
6865ffd83dbSDimitry Andric /// subtracted from the stack pointer according alignment information and get
6875ffd83dbSDimitry Andric /// previous frame pointer.
6885ffd83dbSDimitry Andric void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II,
6895ffd83dbSDimitry Andric                                            Register &NegSizeReg,
6905ffd83dbSDimitry Andric                                            bool &KillNegSizeReg,
6915ffd83dbSDimitry Andric                                            Register &FramePointer) const {
6925ffd83dbSDimitry Andric   // Get the instruction.
6935ffd83dbSDimitry Andric   MachineInstr &MI = *II;
6945ffd83dbSDimitry Andric   // Get the instruction's basic block.
6955ffd83dbSDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
6965ffd83dbSDimitry Andric   // Get the basic block's function.
6975ffd83dbSDimitry Andric   MachineFunction &MF = *MBB.getParent();
6985ffd83dbSDimitry Andric   // Get the frame info.
6995ffd83dbSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
7005ffd83dbSDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
7015ffd83dbSDimitry Andric   // Get the instruction info.
7025ffd83dbSDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
7035ffd83dbSDimitry Andric   // Determine whether 64-bit pointers are used.
7045ffd83dbSDimitry Andric   bool LP64 = TM.isPPC64();
7055ffd83dbSDimitry Andric   DebugLoc dl = MI.getDebugLoc();
7065ffd83dbSDimitry Andric   // Get the total frame size.
7075ffd83dbSDimitry Andric   unsigned FrameSize = MFI.getStackSize();
7085ffd83dbSDimitry Andric 
7095ffd83dbSDimitry Andric   // Get stack alignments.
7105ffd83dbSDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
7115ffd83dbSDimitry Andric   Align TargetAlign = TFI->getStackAlign();
7125ffd83dbSDimitry Andric   Align MaxAlign = MFI.getMaxAlign();
7135ffd83dbSDimitry Andric 
7145ffd83dbSDimitry Andric   // Determine the previous frame's address.  If FrameSize can't be
7155ffd83dbSDimitry Andric   // represented as 16 bits or we need special alignment, then we load the
7165ffd83dbSDimitry Andric   // previous frame's address from 0(SP).  Why not do an addis of the hi?
7175ffd83dbSDimitry Andric   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
7185ffd83dbSDimitry Andric   // Constructing the constant and adding would take 3 instructions.
7195ffd83dbSDimitry Andric   // Fortunately, a frame greater than 32K is rare.
7205ffd83dbSDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
7215ffd83dbSDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
7225ffd83dbSDimitry Andric 
7235ffd83dbSDimitry Andric   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
7245ffd83dbSDimitry Andric     if (LP64)
7255ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer)
7265ffd83dbSDimitry Andric           .addReg(PPC::X31)
7275ffd83dbSDimitry Andric           .addImm(FrameSize);
7285ffd83dbSDimitry Andric     else
7295ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer)
7305ffd83dbSDimitry Andric           .addReg(PPC::R31)
7315ffd83dbSDimitry Andric           .addImm(FrameSize);
7325ffd83dbSDimitry Andric   } else if (LP64) {
7335ffd83dbSDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer)
7345ffd83dbSDimitry Andric         .addImm(0)
7355ffd83dbSDimitry Andric         .addReg(PPC::X1);
7365ffd83dbSDimitry Andric   } else {
7375ffd83dbSDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer)
7385ffd83dbSDimitry Andric         .addImm(0)
7395ffd83dbSDimitry Andric         .addReg(PPC::R1);
7405ffd83dbSDimitry Andric   }
7415ffd83dbSDimitry Andric   // Determine the actual NegSizeReg according to alignment info.
7425ffd83dbSDimitry Andric   if (LP64) {
7435ffd83dbSDimitry Andric     if (MaxAlign > TargetAlign) {
7445ffd83dbSDimitry Andric       unsigned UnalNegSizeReg = NegSizeReg;
7455ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
7465ffd83dbSDimitry Andric 
7475ffd83dbSDimitry Andric       // Unfortunately, there is no andi, only andi., and we can't insert that
7485ffd83dbSDimitry Andric       // here because we might clobber cr0 while it is live.
7495ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
7505ffd83dbSDimitry Andric           .addImm(~(MaxAlign.value() - 1));
7515ffd83dbSDimitry Andric 
7525ffd83dbSDimitry Andric       unsigned NegSizeReg1 = NegSizeReg;
7535ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
7545ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
7555ffd83dbSDimitry Andric           .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
7565ffd83dbSDimitry Andric           .addReg(NegSizeReg1, RegState::Kill);
7575ffd83dbSDimitry Andric       KillNegSizeReg = true;
7585ffd83dbSDimitry Andric     }
7595ffd83dbSDimitry Andric   } else {
7605ffd83dbSDimitry Andric     if (MaxAlign > TargetAlign) {
7615ffd83dbSDimitry Andric       unsigned UnalNegSizeReg = NegSizeReg;
7625ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
7635ffd83dbSDimitry Andric 
7645ffd83dbSDimitry Andric       // Unfortunately, there is no andi, only andi., and we can't insert that
7655ffd83dbSDimitry Andric       // here because we might clobber cr0 while it is live.
7665ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
7675ffd83dbSDimitry Andric           .addImm(~(MaxAlign.value() - 1));
7685ffd83dbSDimitry Andric 
7695ffd83dbSDimitry Andric       unsigned NegSizeReg1 = NegSizeReg;
7705ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
7715ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
7725ffd83dbSDimitry Andric           .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
7735ffd83dbSDimitry Andric           .addReg(NegSizeReg1, RegState::Kill);
7745ffd83dbSDimitry Andric       KillNegSizeReg = true;
7755ffd83dbSDimitry Andric     }
7765ffd83dbSDimitry Andric   }
7775ffd83dbSDimitry Andric }
7785ffd83dbSDimitry Andric 
7795ffd83dbSDimitry Andric void PPCRegisterInfo::lowerPrepareProbedAlloca(
7805ffd83dbSDimitry Andric     MachineBasicBlock::iterator II) const {
7815ffd83dbSDimitry Andric   MachineInstr &MI = *II;
7825ffd83dbSDimitry Andric   // Get the instruction's basic block.
7835ffd83dbSDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
7845ffd83dbSDimitry Andric   // Get the basic block's function.
7855ffd83dbSDimitry Andric   MachineFunction &MF = *MBB.getParent();
7865ffd83dbSDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
7875ffd83dbSDimitry Andric   // Get the instruction info.
7885ffd83dbSDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
7895ffd83dbSDimitry Andric   // Determine whether 64-bit pointers are used.
7905ffd83dbSDimitry Andric   bool LP64 = TM.isPPC64();
7915ffd83dbSDimitry Andric   DebugLoc dl = MI.getDebugLoc();
7925ffd83dbSDimitry Andric   Register FramePointer = MI.getOperand(0).getReg();
793590d96feSDimitry Andric   const Register ActualNegSizeReg = MI.getOperand(1).getReg();
7945ffd83dbSDimitry Andric   bool KillNegSizeReg = MI.getOperand(2).isKill();
7955ffd83dbSDimitry Andric   Register NegSizeReg = MI.getOperand(2).getReg();
796590d96feSDimitry Andric   const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR);
797590d96feSDimitry Andric   // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg.
798590d96feSDimitry Andric   if (FramePointer == NegSizeReg) {
799590d96feSDimitry Andric     assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, "
800590d96feSDimitry Andric                              "NegSizeReg should be killed");
801590d96feSDimitry Andric     // FramePointer is clobbered earlier than the use of NegSizeReg in
802590d96feSDimitry Andric     // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid
803590d96feSDimitry Andric     // misuse.
804590d96feSDimitry Andric     BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
805590d96feSDimitry Andric         .addReg(NegSizeReg)
806590d96feSDimitry Andric         .addReg(NegSizeReg);
807590d96feSDimitry Andric     NegSizeReg = ActualNegSizeReg;
808590d96feSDimitry Andric     KillNegSizeReg = false;
8095ffd83dbSDimitry Andric   }
810590d96feSDimitry Andric   prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer);
811590d96feSDimitry Andric   // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign >
812590d96feSDimitry Andric   // TargetAlign.
813590d96feSDimitry Andric   if (NegSizeReg != ActualNegSizeReg)
814590d96feSDimitry Andric     BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
815590d96feSDimitry Andric         .addReg(NegSizeReg)
816590d96feSDimitry Andric         .addReg(NegSizeReg);
8175ffd83dbSDimitry Andric   MBB.erase(II);
8185ffd83dbSDimitry Andric }
8195ffd83dbSDimitry Andric 
8200b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAreaOffset(
8210b57cec5SDimitry Andric     MachineBasicBlock::iterator II) const {
8220b57cec5SDimitry Andric   // Get the instruction.
8230b57cec5SDimitry Andric   MachineInstr &MI = *II;
8240b57cec5SDimitry Andric   // Get the instruction's basic block.
8250b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8260b57cec5SDimitry Andric   // Get the basic block's function.
8270b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
8280b57cec5SDimitry Andric   // Get the frame info.
8290b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
8300b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
8310b57cec5SDimitry Andric   // Get the instruction info.
8320b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
8350b57cec5SDimitry Andric   bool is64Bit = TM.isPPC64();
8360b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
8370b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
8380b57cec5SDimitry Andric           MI.getOperand(0).getReg())
8390b57cec5SDimitry Andric       .addImm(maxCallFrameSize);
8400b57cec5SDimitry Andric   MBB.erase(II);
8410b57cec5SDimitry Andric }
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
8440b57cec5SDimitry Andric /// reserving a whole register (R0), we scrounge for one here. This generates
8450b57cec5SDimitry Andric /// code like this:
8460b57cec5SDimitry Andric ///
8470b57cec5SDimitry Andric ///   mfcr rA                  ; Move the conditional register into GPR rA.
8480b57cec5SDimitry Andric ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
8490b57cec5SDimitry Andric ///   stw rA, FI               ; Store rA to the frame.
8500b57cec5SDimitry Andric ///
8510b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
8520b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
8530b57cec5SDimitry Andric   // Get the instruction.
8540b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
8550b57cec5SDimitry Andric   // Get the instruction's basic block.
8560b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8570b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
8580b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
8590b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
8600b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
8630b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
8640b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
8650b57cec5SDimitry Andric 
8668bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
8678bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   // We need to store the CR in the low 4-bits of the saved value. First, issue
8700b57cec5SDimitry Andric   // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
8710b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
8720b57cec5SDimitry Andric       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
8730b57cec5SDimitry Andric 
8740b57cec5SDimitry Andric   // If the saved register wasn't CR0, shift the bits left so that they are in
8750b57cec5SDimitry Andric   // CR0's slot.
8760b57cec5SDimitry Andric   if (SrcReg != PPC::CR0) {
8775ffd83dbSDimitry Andric     Register Reg1 = Reg;
8780b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
8790b57cec5SDimitry Andric 
8800b57cec5SDimitry Andric     // rlwinm rA, rA, ShiftBits, 0, 31.
8810b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
8820b57cec5SDimitry Andric       .addReg(Reg1, RegState::Kill)
8830b57cec5SDimitry Andric       .addImm(getEncodingValue(SrcReg) * 4)
8840b57cec5SDimitry Andric       .addImm(0)
8850b57cec5SDimitry Andric       .addImm(31);
8860b57cec5SDimitry Andric   }
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
8890b57cec5SDimitry Andric                     .addReg(Reg, RegState::Kill),
8900b57cec5SDimitry Andric                     FrameIndex);
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric   // Discard the pseudo instruction.
8930b57cec5SDimitry Andric   MBB.erase(II);
8940b57cec5SDimitry Andric }
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
8970b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
8980b57cec5SDimitry Andric   // Get the instruction.
8990b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
9000b57cec5SDimitry Andric   // Get the instruction's basic block.
9010b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
9020b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
9030b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9040b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
9050b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
9080b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
9090b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
9100b57cec5SDimitry Andric 
9118bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9128bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
9130b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
9140b57cec5SDimitry Andric     "RESTORE_CR does not define its destination");
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
9170b57cec5SDimitry Andric                               Reg), FrameIndex);
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric   // If the reloaded register isn't CR0, shift the bits right so that they are
9200b57cec5SDimitry Andric   // in the right CR's slot.
9210b57cec5SDimitry Andric   if (DestReg != PPC::CR0) {
9225ffd83dbSDimitry Andric     Register Reg1 = Reg;
9230b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9240b57cec5SDimitry Andric 
9250b57cec5SDimitry Andric     unsigned ShiftBits = getEncodingValue(DestReg)*4;
9260b57cec5SDimitry Andric     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
9270b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
9280b57cec5SDimitry Andric              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
9290b57cec5SDimitry Andric              .addImm(31);
9300b57cec5SDimitry Andric   }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
9330b57cec5SDimitry Andric              .addReg(Reg, RegState::Kill);
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric   // Discard the pseudo instruction.
9360b57cec5SDimitry Andric   MBB.erase(II);
9370b57cec5SDimitry Andric }
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
9400b57cec5SDimitry Andric                                          unsigned FrameIndex) const {
9410b57cec5SDimitry Andric   // Get the instruction.
9420b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_CRBIT <SrcReg>, <offset>
9430b57cec5SDimitry Andric   // Get the instruction's basic block.
9440b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
9450b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
9460b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9470b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
9480b57cec5SDimitry Andric   const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
9490b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
9500b57cec5SDimitry Andric 
9510b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
9520b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
9530b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
9540b57cec5SDimitry Andric 
9558bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9568bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric   // Search up the BB to find the definition of the CR bit.
959480093f4SDimitry Andric   MachineBasicBlock::reverse_iterator Ins = MI;
960480093f4SDimitry Andric   MachineBasicBlock::reverse_iterator Rend = MBB.rend();
961480093f4SDimitry Andric   ++Ins;
9620b57cec5SDimitry Andric   unsigned CRBitSpillDistance = 0;
963480093f4SDimitry Andric   bool SeenUse = false;
964480093f4SDimitry Andric   for (; Ins != Rend; ++Ins) {
9650b57cec5SDimitry Andric     // Definition found.
9660b57cec5SDimitry Andric     if (Ins->modifiesRegister(SrcReg, TRI))
9670b57cec5SDimitry Andric       break;
968480093f4SDimitry Andric     // Use found.
969480093f4SDimitry Andric     if (Ins->readsRegister(SrcReg, TRI))
970480093f4SDimitry Andric       SeenUse = true;
9710b57cec5SDimitry Andric     // Unable to find CR bit definition within maximum search distance.
9720b57cec5SDimitry Andric     if (CRBitSpillDistance == MaxCRBitSpillDist) {
9730b57cec5SDimitry Andric       Ins = MI;
9740b57cec5SDimitry Andric       break;
9750b57cec5SDimitry Andric     }
9760b57cec5SDimitry Andric     // Skip debug instructions when counting CR bit spill distance.
9770b57cec5SDimitry Andric     if (!Ins->isDebugInstr())
9780b57cec5SDimitry Andric       CRBitSpillDistance++;
9790b57cec5SDimitry Andric   }
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric   // Unable to find the definition of the CR bit in the MBB.
9820b57cec5SDimitry Andric   if (Ins == MBB.rend())
9830b57cec5SDimitry Andric     Ins = MI;
9840b57cec5SDimitry Andric 
985480093f4SDimitry Andric   bool SpillsKnownBit = false;
9860b57cec5SDimitry Andric   // There is no need to extract the CR bit if its value is already known.
9870b57cec5SDimitry Andric   switch (Ins->getOpcode()) {
9880b57cec5SDimitry Andric   case PPC::CRUNSET:
9890b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
9900b57cec5SDimitry Andric       .addImm(0);
991480093f4SDimitry Andric     SpillsKnownBit = true;
9920b57cec5SDimitry Andric     break;
9930b57cec5SDimitry Andric   case PPC::CRSET:
9940b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
9950b57cec5SDimitry Andric       .addImm(-32768);
996480093f4SDimitry Andric     SpillsKnownBit = true;
9970b57cec5SDimitry Andric     break;
9980b57cec5SDimitry Andric   default:
999e8d8bef9SDimitry Andric     // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all
1000e8d8bef9SDimitry Andric     // bits (specifically, it produces a -1 if the CR bit is set). Ultimately,
1001e8d8bef9SDimitry Andric     // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit
1002e8d8bef9SDimitry Andric     // register), and SETNBC will set this.
1003e8d8bef9SDimitry Andric     if (Subtarget.isISA3_1()) {
1004e8d8bef9SDimitry Andric       BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg)
1005e8d8bef9SDimitry Andric           .addReg(SrcReg, RegState::Undef);
1006e8d8bef9SDimitry Andric       break;
1007e8d8bef9SDimitry Andric     }
1008e8d8bef9SDimitry Andric 
1009480093f4SDimitry Andric     // On Power9, we can use SETB to extract the LT bit. This only works for
1010480093f4SDimitry Andric     // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value
1011480093f4SDimitry Andric     // of the bit we care about (32-bit sign bit) will be set to the value of
1012480093f4SDimitry Andric     // the LT bit (regardless of the other bits in the CR field).
1013480093f4SDimitry Andric     if (Subtarget.isISA3_0()) {
1014480093f4SDimitry Andric       if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT ||
1015480093f4SDimitry Andric           SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT ||
1016480093f4SDimitry Andric           SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT ||
1017480093f4SDimitry Andric           SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) {
1018480093f4SDimitry Andric         BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg)
1019480093f4SDimitry Andric           .addReg(getCRFromCRBit(SrcReg), RegState::Undef);
1020480093f4SDimitry Andric         break;
1021480093f4SDimitry Andric       }
1022480093f4SDimitry Andric     }
1023480093f4SDimitry Andric 
10240b57cec5SDimitry Andric     // We need to move the CR field that contains the CR bit we are spilling.
10250b57cec5SDimitry Andric     // The super register may not be explicitly defined (i.e. it can be defined
10260b57cec5SDimitry Andric     // by a CR-logical that only defines the subreg) so we state that the CR
10270b57cec5SDimitry Andric     // field is undef. Also, in order to preserve the kill flag on the CR bit,
10280b57cec5SDimitry Andric     // we add it as an implicit use.
10290b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
10300b57cec5SDimitry Andric       .addReg(getCRFromCRBit(SrcReg), RegState::Undef)
10310b57cec5SDimitry Andric       .addReg(SrcReg,
10320b57cec5SDimitry Andric               RegState::Implicit | getKillRegState(MI.getOperand(0).isKill()));
10330b57cec5SDimitry Andric 
10340b57cec5SDimitry Andric     // If the saved register wasn't CR0LT, shift the bits left so that the bit
10350b57cec5SDimitry Andric     // to store is the first one. Mask all but that bit.
10365ffd83dbSDimitry Andric     Register Reg1 = Reg;
10370b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
10380b57cec5SDimitry Andric 
10390b57cec5SDimitry Andric     // rlwinm rA, rA, ShiftBits, 0, 0.
10400b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
10410b57cec5SDimitry Andric       .addReg(Reg1, RegState::Kill)
10420b57cec5SDimitry Andric       .addImm(getEncodingValue(SrcReg))
10430b57cec5SDimitry Andric       .addImm(0).addImm(0);
10440b57cec5SDimitry Andric   }
10450b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
10460b57cec5SDimitry Andric                     .addReg(Reg, RegState::Kill),
10470b57cec5SDimitry Andric                     FrameIndex);
10480b57cec5SDimitry Andric 
1049480093f4SDimitry Andric   bool KillsCRBit = MI.killsRegister(SrcReg, TRI);
10500b57cec5SDimitry Andric   // Discard the pseudo instruction.
10510b57cec5SDimitry Andric   MBB.erase(II);
1052480093f4SDimitry Andric   if (SpillsKnownBit && KillsCRBit && !SeenUse) {
1053480093f4SDimitry Andric     Ins->setDesc(TII.get(PPC::UNENCODED_NOP));
1054480093f4SDimitry Andric     Ins->RemoveOperand(0);
1055480093f4SDimitry Andric   }
10560b57cec5SDimitry Andric }
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
10590b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
10600b57cec5SDimitry Andric   // Get the instruction.
10610b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CRBIT <offset>
10620b57cec5SDimitry Andric   // Get the instruction's basic block.
10630b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
10640b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
10650b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
10660b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
10670b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
10700b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
10710b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
10720b57cec5SDimitry Andric 
10738bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
10748bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
10750b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
10760b57cec5SDimitry Andric     "RESTORE_CRBIT does not define its destination");
10770b57cec5SDimitry Andric 
10780b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
10790b57cec5SDimitry Andric                               Reg), FrameIndex);
10800b57cec5SDimitry Andric 
10810b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
10820b57cec5SDimitry Andric 
10838bcb0991SDimitry Andric   Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
10840b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
10850b57cec5SDimitry Andric           .addReg(getCRFromCRBit(DestReg));
10860b57cec5SDimitry Andric 
10870b57cec5SDimitry Andric   unsigned ShiftBits = getEncodingValue(DestReg);
10880b57cec5SDimitry Andric   // rlwimi r11, r10, 32-ShiftBits, ..., ...
10890b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
10900b57cec5SDimitry Andric       .addReg(RegO, RegState::Kill)
10910b57cec5SDimitry Andric       .addReg(Reg, RegState::Kill)
10920b57cec5SDimitry Andric       .addImm(ShiftBits ? 32 - ShiftBits : 0)
10930b57cec5SDimitry Andric       .addImm(ShiftBits)
10940b57cec5SDimitry Andric       .addImm(ShiftBits);
10950b57cec5SDimitry Andric 
10960b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
10970b57cec5SDimitry Andric           getCRFromCRBit(DestReg))
10980b57cec5SDimitry Andric       .addReg(RegO, RegState::Kill)
10990b57cec5SDimitry Andric       // Make sure we have a use dependency all the way through this
11000b57cec5SDimitry Andric       // sequence of instructions. We can't have the other bits in the CR
11010b57cec5SDimitry Andric       // modified in between the mfocrf and the mtocrf.
11020b57cec5SDimitry Andric       .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
11030b57cec5SDimitry Andric 
11040b57cec5SDimitry Andric   // Discard the pseudo instruction.
11050b57cec5SDimitry Andric   MBB.erase(II);
11060b57cec5SDimitry Andric }
11070b57cec5SDimitry Andric 
1108e8d8bef9SDimitry Andric void PPCRegisterInfo::emitAccCopyInfo(MachineBasicBlock &MBB,
1109e8d8bef9SDimitry Andric                                       MCRegister DestReg, MCRegister SrcReg) {
1110e8d8bef9SDimitry Andric #ifdef NDEBUG
1111e8d8bef9SDimitry Andric   return;
1112e8d8bef9SDimitry Andric #else
1113e8d8bef9SDimitry Andric   if (ReportAccMoves) {
1114e8d8bef9SDimitry Andric     std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc";
1115e8d8bef9SDimitry Andric     std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc";
1116e8d8bef9SDimitry Andric     dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n";
1117e8d8bef9SDimitry Andric     MBB.dump();
1118e8d8bef9SDimitry Andric   }
1119e8d8bef9SDimitry Andric #endif
1120e8d8bef9SDimitry Andric }
1121e8d8bef9SDimitry Andric 
1122e8d8bef9SDimitry Andric static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed,
1123e8d8bef9SDimitry Andric                                     bool IsRestore) {
1124e8d8bef9SDimitry Andric #ifdef NDEBUG
1125e8d8bef9SDimitry Andric   return;
1126e8d8bef9SDimitry Andric #else
1127e8d8bef9SDimitry Andric   if (ReportAccMoves) {
1128e8d8bef9SDimitry Andric     dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register "
1129e8d8bef9SDimitry Andric            << (IsRestore ? "restore" : "spill") << ":\n";
1130e8d8bef9SDimitry Andric     MBB.dump();
1131e8d8bef9SDimitry Andric   }
1132e8d8bef9SDimitry Andric #endif
1133e8d8bef9SDimitry Andric }
1134e8d8bef9SDimitry Andric 
1135e8d8bef9SDimitry Andric /// lowerACCSpilling - Generate the code for spilling the accumulator register.
1136e8d8bef9SDimitry Andric /// Similarly to other spills/reloads that use pseudo-ops, we do not actually
1137e8d8bef9SDimitry Andric /// eliminate the FrameIndex here nor compute the stack offset. We simply
1138e8d8bef9SDimitry Andric /// create a real instruction with an FI and rely on eliminateFrameIndex to
1139e8d8bef9SDimitry Andric /// handle the FI elimination.
1140e8d8bef9SDimitry Andric void PPCRegisterInfo::lowerACCSpilling(MachineBasicBlock::iterator II,
11410b57cec5SDimitry Andric                                        unsigned FrameIndex) const {
1142e8d8bef9SDimitry Andric   MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset>
11430b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
11440b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
11450b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
11460b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1147e8d8bef9SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
11488bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
1149e8d8bef9SDimitry Andric   bool IsKilled = MI.getOperand(0).isKill();
11500b57cec5SDimitry Andric 
1151e8d8bef9SDimitry Andric   bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg);
1152e8d8bef9SDimitry Andric   Register Reg =
1153e8d8bef9SDimitry Andric       PPC::VSRp0 + (SrcReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1154e8d8bef9SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
11550b57cec5SDimitry Andric 
1156e8d8bef9SDimitry Andric   emitAccSpillRestoreInfo(MBB, IsPrimed, false);
1157e8d8bef9SDimitry Andric 
1158e8d8bef9SDimitry Andric   // De-prime the register being spilled, create two stores for the pair
1159e8d8bef9SDimitry Andric   // subregisters accounting for endianness and then re-prime the register if
1160e8d8bef9SDimitry Andric   // it isn't killed.  This uses the Offset parameter to addFrameReference() to
1161e8d8bef9SDimitry Andric   // adjust the offset of the store that is within the 64-byte stack slot.
1162e8d8bef9SDimitry Andric   if (IsPrimed)
1163e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg);
1164e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1165e8d8bef9SDimitry Andric                         .addReg(Reg, getKillRegState(IsKilled)),
1166e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 32 : 0);
1167e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1168e8d8bef9SDimitry Andric                         .addReg(Reg + 1, getKillRegState(IsKilled)),
1169e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 0 : 32);
1170e8d8bef9SDimitry Andric   if (IsPrimed && !IsKilled)
1171e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg);
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric   // Discard the pseudo instruction.
11740b57cec5SDimitry Andric   MBB.erase(II);
11750b57cec5SDimitry Andric }
11760b57cec5SDimitry Andric 
1177e8d8bef9SDimitry Andric /// lowerACCRestore - Generate the code to restore the accumulator register.
1178e8d8bef9SDimitry Andric void PPCRegisterInfo::lowerACCRestore(MachineBasicBlock::iterator II,
11790b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
1180e8d8bef9SDimitry Andric   MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset>
11810b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
11820b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
11830b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
11840b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1185e8d8bef9SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
11860b57cec5SDimitry Andric 
11878bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
11880b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
1189e8d8bef9SDimitry Andric          "RESTORE_ACC does not define its destination");
11900b57cec5SDimitry Andric 
1191e8d8bef9SDimitry Andric   bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg);
1192e8d8bef9SDimitry Andric   Register Reg =
1193e8d8bef9SDimitry Andric       PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1194e8d8bef9SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
11950b57cec5SDimitry Andric 
1196e8d8bef9SDimitry Andric   emitAccSpillRestoreInfo(MBB, IsPrimed, true);
1197e8d8bef9SDimitry Andric 
1198e8d8bef9SDimitry Andric   // Create two loads for the pair subregisters accounting for endianness and
1199e8d8bef9SDimitry Andric   // then prime the accumulator register being restored.
1200e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg),
1201e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 32 : 0);
1202e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1),
1203e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 0 : 32);
1204e8d8bef9SDimitry Andric   if (IsPrimed)
1205e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg);
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric   // Discard the pseudo instruction.
12080b57cec5SDimitry Andric   MBB.erase(II);
12090b57cec5SDimitry Andric }
12100b57cec5SDimitry Andric 
1211*fe6060f1SDimitry Andric /// lowerQuadwordSpilling - Generate code to spill paired general register.
1212*fe6060f1SDimitry Andric void PPCRegisterInfo::lowerQuadwordSpilling(MachineBasicBlock::iterator II,
1213*fe6060f1SDimitry Andric                                             unsigned FrameIndex) const {
1214*fe6060f1SDimitry Andric   MachineInstr &MI = *II;
1215*fe6060f1SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
1216*fe6060f1SDimitry Andric   MachineFunction &MF = *MBB.getParent();
1217*fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1218*fe6060f1SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1219*fe6060f1SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
1220*fe6060f1SDimitry Andric 
1221*fe6060f1SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
1222*fe6060f1SDimitry Andric   bool IsKilled = MI.getOperand(0).isKill();
1223*fe6060f1SDimitry Andric 
1224*fe6060f1SDimitry Andric   Register Reg = PPC::X0 + (SrcReg - PPC::G8p0) * 2;
1225*fe6060f1SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
1226*fe6060f1SDimitry Andric 
1227*fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
1228*fe6060f1SDimitry Andric                         .addReg(Reg, getKillRegState(IsKilled)),
1229*fe6060f1SDimitry Andric                     FrameIndex, IsLittleEndian ? 8 : 0);
1230*fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
1231*fe6060f1SDimitry Andric                         .addReg(Reg + 1, getKillRegState(IsKilled)),
1232*fe6060f1SDimitry Andric                     FrameIndex, IsLittleEndian ? 0 : 8);
1233*fe6060f1SDimitry Andric 
1234*fe6060f1SDimitry Andric   // Discard the pseudo instruction.
1235*fe6060f1SDimitry Andric   MBB.erase(II);
1236*fe6060f1SDimitry Andric }
1237*fe6060f1SDimitry Andric 
1238*fe6060f1SDimitry Andric /// lowerQuadwordRestore - Generate code to restore paired general register.
1239*fe6060f1SDimitry Andric void PPCRegisterInfo::lowerQuadwordRestore(MachineBasicBlock::iterator II,
1240*fe6060f1SDimitry Andric                                            unsigned FrameIndex) const {
1241*fe6060f1SDimitry Andric   MachineInstr &MI = *II;
1242*fe6060f1SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
1243*fe6060f1SDimitry Andric   MachineFunction &MF = *MBB.getParent();
1244*fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1245*fe6060f1SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1246*fe6060f1SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
1247*fe6060f1SDimitry Andric 
1248*fe6060f1SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
1249*fe6060f1SDimitry Andric   assert(MI.definesRegister(DestReg) &&
1250*fe6060f1SDimitry Andric          "RESTORE_QUADWORD does not define its destination");
1251*fe6060f1SDimitry Andric 
1252*fe6060f1SDimitry Andric   Register Reg = PPC::X0 + (DestReg - PPC::G8p0) * 2;
1253*fe6060f1SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
1254*fe6060f1SDimitry Andric 
1255*fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg), FrameIndex,
1256*fe6060f1SDimitry Andric                     IsLittleEndian ? 8 : 0);
1257*fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg + 1), FrameIndex,
1258*fe6060f1SDimitry Andric                     IsLittleEndian ? 0 : 8);
1259*fe6060f1SDimitry Andric 
1260*fe6060f1SDimitry Andric   // Discard the pseudo instruction.
1261*fe6060f1SDimitry Andric   MBB.erase(II);
1262*fe6060f1SDimitry Andric }
1263*fe6060f1SDimitry Andric 
12640b57cec5SDimitry Andric bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
12655ffd83dbSDimitry Andric                                            Register Reg, int &FrameIdx) const {
12665ffd83dbSDimitry Andric   // For the nonvolatile condition registers (CR2, CR3, CR4) return true to
12675ffd83dbSDimitry Andric   // prevent allocating an additional frame slot.
12685ffd83dbSDimitry Andric   // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8,
12695ffd83dbSDimitry Andric   // for 32-bit AIX the CR save area is in the linkage area at SP+4.
12705ffd83dbSDimitry Andric   // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos
12715ffd83dbSDimitry Andric   // valid.
12725ffd83dbSDimitry Andric   // For 32-bit ELF, we have previously created the stack slot if needed, so
12735ffd83dbSDimitry Andric   // return its FrameIdx.
12745ffd83dbSDimitry Andric   if (PPC::CR2 <= Reg && Reg <= PPC::CR4) {
12755ffd83dbSDimitry Andric     FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex();
12760b57cec5SDimitry Andric     return true;
12770b57cec5SDimitry Andric   }
12780b57cec5SDimitry Andric   return false;
12790b57cec5SDimitry Andric }
12800b57cec5SDimitry Andric 
12810b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is.
12820b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC) {
12830b57cec5SDimitry Andric   switch (OpC) {
12840b57cec5SDimitry Andric   default:
12850b57cec5SDimitry Andric     return 1;
12860b57cec5SDimitry Andric   case PPC::LWA:
12870b57cec5SDimitry Andric   case PPC::LWA_32:
12880b57cec5SDimitry Andric   case PPC::LD:
12890b57cec5SDimitry Andric   case PPC::LDU:
12900b57cec5SDimitry Andric   case PPC::STD:
12910b57cec5SDimitry Andric   case PPC::STDU:
12920b57cec5SDimitry Andric   case PPC::DFLOADf32:
12930b57cec5SDimitry Andric   case PPC::DFLOADf64:
12940b57cec5SDimitry Andric   case PPC::DFSTOREf32:
12950b57cec5SDimitry Andric   case PPC::DFSTOREf64:
12960b57cec5SDimitry Andric   case PPC::LXSD:
12970b57cec5SDimitry Andric   case PPC::LXSSP:
12980b57cec5SDimitry Andric   case PPC::STXSD:
12990b57cec5SDimitry Andric   case PPC::STXSSP:
1300*fe6060f1SDimitry Andric   case PPC::STQ:
13010b57cec5SDimitry Andric     return 4;
13020b57cec5SDimitry Andric   case PPC::EVLDD:
13030b57cec5SDimitry Andric   case PPC::EVSTDD:
13040b57cec5SDimitry Andric     return 8;
13050b57cec5SDimitry Andric   case PPC::LXV:
13060b57cec5SDimitry Andric   case PPC::STXV:
1307*fe6060f1SDimitry Andric   case PPC::LQ:
1308*fe6060f1SDimitry Andric   case PPC::LXVP:
1309*fe6060f1SDimitry Andric   case PPC::STXVP:
13100b57cec5SDimitry Andric     return 16;
13110b57cec5SDimitry Andric   }
13120b57cec5SDimitry Andric }
13130b57cec5SDimitry Andric 
13140b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is.
13150b57cec5SDimitry Andric static unsigned offsetMinAlign(const MachineInstr &MI) {
13160b57cec5SDimitry Andric   unsigned OpC = MI.getOpcode();
13170b57cec5SDimitry Andric   return offsetMinAlignForOpcode(OpC);
13180b57cec5SDimitry Andric }
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
13210b57cec5SDimitry Andric static unsigned getOffsetONFromFION(const MachineInstr &MI,
13220b57cec5SDimitry Andric                                     unsigned FIOperandNum) {
13230b57cec5SDimitry Andric   // Take into account whether it's an add or mem instruction
13240b57cec5SDimitry Andric   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
13250b57cec5SDimitry Andric   if (MI.isInlineAsm())
13260b57cec5SDimitry Andric     OffsetOperandNo = FIOperandNum - 1;
13270b57cec5SDimitry Andric   else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
13280b57cec5SDimitry Andric            MI.getOpcode() == TargetOpcode::PATCHPOINT)
13290b57cec5SDimitry Andric     OffsetOperandNo = FIOperandNum + 1;
13300b57cec5SDimitry Andric 
13310b57cec5SDimitry Andric   return OffsetOperandNo;
13320b57cec5SDimitry Andric }
13330b57cec5SDimitry Andric 
13340b57cec5SDimitry Andric void
13350b57cec5SDimitry Andric PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
13360b57cec5SDimitry Andric                                      int SPAdj, unsigned FIOperandNum,
13370b57cec5SDimitry Andric                                      RegScavenger *RS) const {
13380b57cec5SDimitry Andric   assert(SPAdj == 0 && "Unexpected");
13390b57cec5SDimitry Andric 
13400b57cec5SDimitry Andric   // Get the instruction.
13410b57cec5SDimitry Andric   MachineInstr &MI = *II;
13420b57cec5SDimitry Andric   // Get the instruction's basic block.
13430b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
13440b57cec5SDimitry Andric   // Get the basic block's function.
13450b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
13460b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
13470b57cec5SDimitry Andric   // Get the instruction info.
13480b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
13490b57cec5SDimitry Andric   // Get the frame info.
13500b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
13510b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
13520b57cec5SDimitry Andric 
13530b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
13540b57cec5SDimitry Andric 
13550b57cec5SDimitry Andric   // Get the frame index.
13560b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
13570b57cec5SDimitry Andric 
13580b57cec5SDimitry Andric   // Get the frame pointer save index.  Users of this index are primarily
13590b57cec5SDimitry Andric   // DYNALLOC instructions.
13600b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
13610b57cec5SDimitry Andric   int FPSI = FI->getFramePointerSaveIndex();
13620b57cec5SDimitry Andric   // Get the instruction opcode.
13630b57cec5SDimitry Andric   unsigned OpC = MI.getOpcode();
13640b57cec5SDimitry Andric 
13650b57cec5SDimitry Andric   if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
13660b57cec5SDimitry Andric     lowerDynamicAreaOffset(II);
13670b57cec5SDimitry Andric     return;
13680b57cec5SDimitry Andric   }
13690b57cec5SDimitry Andric 
13700b57cec5SDimitry Andric   // Special case for dynamic alloca.
13710b57cec5SDimitry Andric   if (FPSI && FrameIndex == FPSI &&
13720b57cec5SDimitry Andric       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
13730b57cec5SDimitry Andric     lowerDynamicAlloc(II);
13740b57cec5SDimitry Andric     return;
13750b57cec5SDimitry Andric   }
13760b57cec5SDimitry Andric 
13775ffd83dbSDimitry Andric   if (FPSI && FrameIndex == FPSI &&
13785ffd83dbSDimitry Andric       (OpC == PPC::PREPARE_PROBED_ALLOCA_64 ||
1379590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_32 ||
1380590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64 ||
1381590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32)) {
13825ffd83dbSDimitry Andric     lowerPrepareProbedAlloca(II);
13835ffd83dbSDimitry Andric     return;
13845ffd83dbSDimitry Andric   }
13855ffd83dbSDimitry Andric 
13860b57cec5SDimitry Andric   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
13870b57cec5SDimitry Andric   if (OpC == PPC::SPILL_CR) {
13880b57cec5SDimitry Andric     lowerCRSpilling(II, FrameIndex);
13890b57cec5SDimitry Andric     return;
13900b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_CR) {
13910b57cec5SDimitry Andric     lowerCRRestore(II, FrameIndex);
13920b57cec5SDimitry Andric     return;
13930b57cec5SDimitry Andric   } else if (OpC == PPC::SPILL_CRBIT) {
13940b57cec5SDimitry Andric     lowerCRBitSpilling(II, FrameIndex);
13950b57cec5SDimitry Andric     return;
13960b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_CRBIT) {
13970b57cec5SDimitry Andric     lowerCRBitRestore(II, FrameIndex);
13980b57cec5SDimitry Andric     return;
1399e8d8bef9SDimitry Andric   } else if (OpC == PPC::SPILL_ACC || OpC == PPC::SPILL_UACC) {
1400e8d8bef9SDimitry Andric     lowerACCSpilling(II, FrameIndex);
14010b57cec5SDimitry Andric     return;
1402e8d8bef9SDimitry Andric   } else if (OpC == PPC::RESTORE_ACC || OpC == PPC::RESTORE_UACC) {
1403e8d8bef9SDimitry Andric     lowerACCRestore(II, FrameIndex);
14040b57cec5SDimitry Andric     return;
1405*fe6060f1SDimitry Andric   } else if (OpC == PPC::SPILL_QUADWORD) {
1406*fe6060f1SDimitry Andric     lowerQuadwordSpilling(II, FrameIndex);
1407*fe6060f1SDimitry Andric     return;
1408*fe6060f1SDimitry Andric   } else if (OpC == PPC::RESTORE_QUADWORD) {
1409*fe6060f1SDimitry Andric     lowerQuadwordRestore(II, FrameIndex);
1410*fe6060f1SDimitry Andric     return;
14110b57cec5SDimitry Andric   }
14120b57cec5SDimitry Andric 
14130b57cec5SDimitry Andric   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
14140b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(
14150b57cec5SDimitry Andric     FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
14160b57cec5SDimitry Andric 
14170b57cec5SDimitry Andric   // If the instruction is not present in ImmToIdxMap, then it has no immediate
14180b57cec5SDimitry Andric   // form (and must be r+r).
14190b57cec5SDimitry Andric   bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
14200b57cec5SDimitry Andric                    OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
14210b57cec5SDimitry Andric 
14220b57cec5SDimitry Andric   // Now add the frame object offset to the offset from r1.
14230b57cec5SDimitry Andric   int Offset = MFI.getObjectOffset(FrameIndex);
14240b57cec5SDimitry Andric   Offset += MI.getOperand(OffsetOperandNo).getImm();
14250b57cec5SDimitry Andric 
14260b57cec5SDimitry Andric   // If we're not using a Frame Pointer that has been set to the value of the
14270b57cec5SDimitry Andric   // SP before having the stack size subtracted from it, then add the stack size
14280b57cec5SDimitry Andric   // to Offset to get the correct offset.
14290b57cec5SDimitry Andric   // Naked functions have stack size 0, although getStackSize may not reflect
14300b57cec5SDimitry Andric   // that because we didn't call all the pieces that compute it for naked
14310b57cec5SDimitry Andric   // functions.
14320b57cec5SDimitry Andric   if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
14330b57cec5SDimitry Andric     if (!(hasBasePointer(MF) && FrameIndex < 0))
14340b57cec5SDimitry Andric       Offset += MFI.getStackSize();
14350b57cec5SDimitry Andric   }
14360b57cec5SDimitry Andric 
1437*fe6060f1SDimitry Andric   // If we encounter an LXVP/STXVP with an offset that doesn't fit, we can
1438*fe6060f1SDimitry Andric   // transform it to the prefixed version so we don't have to use the XForm.
1439*fe6060f1SDimitry Andric   if ((OpC == PPC::LXVP || OpC == PPC::STXVP) &&
1440*fe6060f1SDimitry Andric       (!isInt<16>(Offset) || (Offset % offsetMinAlign(MI)) != 0) &&
1441*fe6060f1SDimitry Andric       Subtarget.hasPrefixInstrs()) {
1442*fe6060f1SDimitry Andric     unsigned NewOpc = OpC == PPC::LXVP ? PPC::PLXVP : PPC::PSTXVP;
1443*fe6060f1SDimitry Andric     MI.setDesc(TII.get(NewOpc));
1444*fe6060f1SDimitry Andric     OpC = NewOpc;
1445*fe6060f1SDimitry Andric   }
1446*fe6060f1SDimitry Andric 
14470b57cec5SDimitry Andric   // If we can, encode the offset directly into the instruction.  If this is a
14480b57cec5SDimitry Andric   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
14490b57cec5SDimitry Andric   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
14500b57cec5SDimitry Andric   // clear can be encoded.  This is extremely uncommon, because normally you
14510b57cec5SDimitry Andric   // only "std" to a stack slot that is at least 4-byte aligned, but it can
14520b57cec5SDimitry Andric   // happen in invalid code.
14530b57cec5SDimitry Andric   assert(OpC != PPC::DBG_VALUE &&
14540b57cec5SDimitry Andric          "This should be handled in a target-independent way");
1455*fe6060f1SDimitry Andric   // FIXME: This should be factored out to a separate function as prefixed
1456*fe6060f1SDimitry Andric   // instructions add a number of opcodes for which we can use 34-bit imm.
14570b57cec5SDimitry Andric   bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ?
14580b57cec5SDimitry Andric                             isUInt<8>(Offset) :
14590b57cec5SDimitry Andric                             isInt<16>(Offset);
1460*fe6060f1SDimitry Andric   if (OpC == PPC::PLXVP || OpC == PPC::PSTXVP)
1461*fe6060f1SDimitry Andric     OffsetFitsMnemonic = isInt<34>(Offset);
14620b57cec5SDimitry Andric   if (!noImmForm && ((OffsetFitsMnemonic &&
14630b57cec5SDimitry Andric                       ((Offset % offsetMinAlign(MI)) == 0)) ||
14640b57cec5SDimitry Andric                      OpC == TargetOpcode::STACKMAP ||
14650b57cec5SDimitry Andric                      OpC == TargetOpcode::PATCHPOINT)) {
14660b57cec5SDimitry Andric     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
14670b57cec5SDimitry Andric     return;
14680b57cec5SDimitry Andric   }
14690b57cec5SDimitry Andric 
14700b57cec5SDimitry Andric   // The offset doesn't fit into a single register, scavenge one to build the
14710b57cec5SDimitry Andric   // offset in.
14720b57cec5SDimitry Andric 
14730b57cec5SDimitry Andric   bool is64Bit = TM.isPPC64();
14740b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
14750b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
14760b57cec5SDimitry Andric   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
14775ffd83dbSDimitry Andric   Register SRegHi = MF.getRegInfo().createVirtualRegister(RC),
14780b57cec5SDimitry Andric            SReg = MF.getRegInfo().createVirtualRegister(RC);
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric   // Insert a set of rA with the full offset value before the ld, st, or add
14810b57cec5SDimitry Andric   if (isInt<16>(Offset))
14820b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
14830b57cec5SDimitry Andric       .addImm(Offset);
14840b57cec5SDimitry Andric   else {
14850b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
14860b57cec5SDimitry Andric       .addImm(Offset >> 16);
14870b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
14880b57cec5SDimitry Andric       .addReg(SRegHi, RegState::Kill)
14890b57cec5SDimitry Andric       .addImm(Offset);
14900b57cec5SDimitry Andric   }
14910b57cec5SDimitry Andric 
14920b57cec5SDimitry Andric   // Convert into indexed form of the instruction:
14930b57cec5SDimitry Andric   //
14940b57cec5SDimitry Andric   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
14950b57cec5SDimitry Andric   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
14960b57cec5SDimitry Andric   unsigned OperandBase;
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric   if (noImmForm)
14990b57cec5SDimitry Andric     OperandBase = 1;
15000b57cec5SDimitry Andric   else if (OpC != TargetOpcode::INLINEASM &&
15010b57cec5SDimitry Andric            OpC != TargetOpcode::INLINEASM_BR) {
15020b57cec5SDimitry Andric     assert(ImmToIdxMap.count(OpC) &&
15030b57cec5SDimitry Andric            "No indexed form of load or store available!");
15040b57cec5SDimitry Andric     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
15050b57cec5SDimitry Andric     MI.setDesc(TII.get(NewOpcode));
15060b57cec5SDimitry Andric     OperandBase = 1;
15070b57cec5SDimitry Andric   } else {
15080b57cec5SDimitry Andric     OperandBase = OffsetOperandNo;
15090b57cec5SDimitry Andric   }
15100b57cec5SDimitry Andric 
15118bcb0991SDimitry Andric   Register StackReg = MI.getOperand(FIOperandNum).getReg();
15120b57cec5SDimitry Andric   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
15130b57cec5SDimitry Andric   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
15140b57cec5SDimitry Andric }
15150b57cec5SDimitry Andric 
15160b57cec5SDimitry Andric Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
15170b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
15180b57cec5SDimitry Andric 
15190b57cec5SDimitry Andric   if (!TM.isPPC64())
15200b57cec5SDimitry Andric     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
15210b57cec5SDimitry Andric   else
15220b57cec5SDimitry Andric     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
15230b57cec5SDimitry Andric }
15240b57cec5SDimitry Andric 
15250b57cec5SDimitry Andric Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
15260b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
15270b57cec5SDimitry Andric   if (!hasBasePointer(MF))
15280b57cec5SDimitry Andric     return getFrameRegister(MF);
15290b57cec5SDimitry Andric 
15300b57cec5SDimitry Andric   if (TM.isPPC64())
15310b57cec5SDimitry Andric     return PPC::X30;
15320b57cec5SDimitry Andric 
15330b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
15340b57cec5SDimitry Andric     return PPC::R29;
15350b57cec5SDimitry Andric 
15360b57cec5SDimitry Andric   return PPC::R30;
15370b57cec5SDimitry Andric }
15380b57cec5SDimitry Andric 
15390b57cec5SDimitry Andric bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
15400b57cec5SDimitry Andric   if (!EnableBasePointer)
15410b57cec5SDimitry Andric     return false;
15420b57cec5SDimitry Andric   if (AlwaysBasePointer)
15430b57cec5SDimitry Andric     return true;
15440b57cec5SDimitry Andric 
15450b57cec5SDimitry Andric   // If we need to realign the stack, then the stack pointer can no longer
15460b57cec5SDimitry Andric   // serve as an offset into the caller's stack space. As a result, we need a
15470b57cec5SDimitry Andric   // base pointer.
1548*fe6060f1SDimitry Andric   return hasStackRealignment(MF);
15490b57cec5SDimitry Andric }
15500b57cec5SDimitry Andric 
15510b57cec5SDimitry Andric /// Returns true if the instruction's frame index
15520b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
15530b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
15540b57cec5SDimitry Andric /// references it should create new base registers for.
15550b57cec5SDimitry Andric bool PPCRegisterInfo::
15560b57cec5SDimitry Andric needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
15570b57cec5SDimitry Andric   assert(Offset < 0 && "Local offset must be negative");
15580b57cec5SDimitry Andric 
15590b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
15600b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
15610b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
15620b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
15630b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
15640b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
15650b57cec5SDimitry Andric 
15660b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores that have
15670b57cec5SDimitry Andric   // an r+i form. Return false for everything else.
15680b57cec5SDimitry Andric   unsigned OpC = MI->getOpcode();
15690b57cec5SDimitry Andric   if (!ImmToIdxMap.count(OpC))
15700b57cec5SDimitry Andric     return false;
15710b57cec5SDimitry Andric 
15720b57cec5SDimitry Andric   // Don't generate a new virtual base register just to add zero to it.
15730b57cec5SDimitry Andric   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
15740b57cec5SDimitry Andric       MI->getOperand(2).getImm() == 0)
15750b57cec5SDimitry Andric     return false;
15760b57cec5SDimitry Andric 
15770b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI->getParent();
15780b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
15790b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
15800b57cec5SDimitry Andric   unsigned StackEst = TFI->determineFrameLayout(MF, true);
15810b57cec5SDimitry Andric 
15820b57cec5SDimitry Andric   // If we likely don't need a stack frame, then we probably don't need a
15830b57cec5SDimitry Andric   // virtual base register either.
15840b57cec5SDimitry Andric   if (!StackEst)
15850b57cec5SDimitry Andric     return false;
15860b57cec5SDimitry Andric 
15870b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
15880b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
15890b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
15900b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
15910b57cec5SDimitry Andric   Offset += StackEst;
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric   // The frame pointer will point to the end of the stack, so estimate the
15940b57cec5SDimitry Andric   // offset as the difference between the object offset and the FP location.
15950b57cec5SDimitry Andric   return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
15960b57cec5SDimitry Andric }
15970b57cec5SDimitry Andric 
15980b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to
15990b57cec5SDimitry Andric /// be a pointer to FrameIdx at the beginning of the basic block.
1600e8d8bef9SDimitry Andric Register PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
16015ffd83dbSDimitry Andric                                                        int FrameIdx,
16020b57cec5SDimitry Andric                                                        int64_t Offset) const {
16030b57cec5SDimitry Andric   unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
16060b57cec5SDimitry Andric   DebugLoc DL;                  // Defaults to "unknown"
16070b57cec5SDimitry Andric   if (Ins != MBB->end())
16080b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
16090b57cec5SDimitry Andric 
16100b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
16110b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
16120b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
16130b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII.get(ADDriOpc);
16140b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1615e8d8bef9SDimitry Andric   const TargetRegisterClass *RC = getPointerRegClass(MF);
1616e8d8bef9SDimitry Andric   Register BaseReg = MRI.createVirtualRegister(RC);
16170b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
16180b57cec5SDimitry Andric 
16190b57cec5SDimitry Andric   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
16200b57cec5SDimitry Andric     .addFrameIndex(FrameIdx).addImm(Offset);
1621e8d8bef9SDimitry Andric 
1622e8d8bef9SDimitry Andric   return BaseReg;
16230b57cec5SDimitry Andric }
16240b57cec5SDimitry Andric 
16255ffd83dbSDimitry Andric void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
16260b57cec5SDimitry Andric                                         int64_t Offset) const {
16270b57cec5SDimitry Andric   unsigned FIOperandNum = 0;
16280b57cec5SDimitry Andric   while (!MI.getOperand(FIOperandNum).isFI()) {
16290b57cec5SDimitry Andric     ++FIOperandNum;
16300b57cec5SDimitry Andric     assert(FIOperandNum < MI.getNumOperands() &&
16310b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
16320b57cec5SDimitry Andric   }
16330b57cec5SDimitry Andric 
16340b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
16350b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
16360b57cec5SDimitry Andric   Offset += MI.getOperand(OffsetOperandNo).getImm();
16370b57cec5SDimitry Andric   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
16380b57cec5SDimitry Andric 
16390b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
16400b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
16410b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
16420b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
16430b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
16440b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
16450b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg,
16460b57cec5SDimitry Andric                         TII.getRegClass(MCID, FIOperandNum, this, MF));
16470b57cec5SDimitry Andric }
16480b57cec5SDimitry Andric 
16490b57cec5SDimitry Andric bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
16505ffd83dbSDimitry Andric                                          Register BaseReg,
16510b57cec5SDimitry Andric                                          int64_t Offset) const {
16520b57cec5SDimitry Andric   unsigned FIOperandNum = 0;
16530b57cec5SDimitry Andric   while (!MI->getOperand(FIOperandNum).isFI()) {
16540b57cec5SDimitry Andric     ++FIOperandNum;
16550b57cec5SDimitry Andric     assert(FIOperandNum < MI->getNumOperands() &&
16560b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
16570b57cec5SDimitry Andric   }
16580b57cec5SDimitry Andric 
16590b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
16600b57cec5SDimitry Andric   Offset += MI->getOperand(OffsetOperandNo).getImm();
16610b57cec5SDimitry Andric 
16620b57cec5SDimitry Andric   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
16630b57cec5SDimitry Andric          MI->getOpcode() == TargetOpcode::STACKMAP ||
16640b57cec5SDimitry Andric          MI->getOpcode() == TargetOpcode::PATCHPOINT ||
16650b57cec5SDimitry Andric          (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
16660b57cec5SDimitry Andric }
1667