xref: /freebsd/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
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 
780b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC);
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
810b57cec5SDimitry Andric   : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
820b57cec5SDimitry Andric                        TM.isPPC64() ? 0 : 1,
830b57cec5SDimitry Andric                        TM.isPPC64() ? 0 : 1),
840b57cec5SDimitry Andric     TM(TM) {
850b57cec5SDimitry Andric   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
860b57cec5SDimitry Andric   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
870b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
880b57cec5SDimitry Andric   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
890b57cec5SDimitry Andric   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
900b57cec5SDimitry Andric   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
910b57cec5SDimitry Andric   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
920b57cec5SDimitry Andric   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
930b57cec5SDimitry Andric   ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   // 64-bit
960b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
970b57cec5SDimitry Andric   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
980b57cec5SDimitry Andric   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
990b57cec5SDimitry Andric   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
1000b57cec5SDimitry Andric   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   // VSX
1030b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX;
1040b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX;
1050b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX;
1060b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX;
1070b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX;
1080b57cec5SDimitry Andric   ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX;
1090b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXV] = PPC::LXVX;
1100b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXSD] = PPC::LXSDX;
1110b57cec5SDimitry Andric   ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX;
1120b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXV] = PPC::STXVX;
1130b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXSD] = PPC::STXSDX;
1140b57cec5SDimitry Andric   ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX;
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   // SPE
1170b57cec5SDimitry Andric   ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX;
1180b57cec5SDimitry Andric   ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX;
1190b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX;
1200b57cec5SDimitry Andric   ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric /// getPointerRegClass - Return the register class to use to hold pointers.
1240b57cec5SDimitry Andric /// This is used for addressing modes.
1250b57cec5SDimitry Andric const TargetRegisterClass *
1260b57cec5SDimitry Andric PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
1270b57cec5SDimitry Andric                                                                        const {
1280b57cec5SDimitry Andric   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
1290b57cec5SDimitry Andric   // when it checks for ZERO folding.
1300b57cec5SDimitry Andric   if (Kind == 1) {
1310b57cec5SDimitry Andric     if (TM.isPPC64())
1320b57cec5SDimitry Andric       return &PPC::G8RC_NOX0RegClass;
1330b57cec5SDimitry Andric     return &PPC::GPRC_NOR0RegClass;
1340b57cec5SDimitry Andric   }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   if (TM.isPPC64())
1370b57cec5SDimitry Andric     return &PPC::G8RCRegClass;
1380b57cec5SDimitry Andric   return &PPC::GPRCRegClass;
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric const MCPhysReg*
1420b57cec5SDimitry Andric PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
1430b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
1440b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) {
1450b57cec5SDimitry Andric     if (Subtarget.hasVSX())
1460b57cec5SDimitry Andric       return CSR_64_AllRegs_VSX_SaveList;
1470b57cec5SDimitry Andric     if (Subtarget.hasAltivec())
1480b57cec5SDimitry Andric       return CSR_64_AllRegs_Altivec_SaveList;
1490b57cec5SDimitry Andric     return CSR_64_AllRegs_SaveList;
1500b57cec5SDimitry Andric   }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   if (Subtarget.isDarwinABI())
1530b57cec5SDimitry Andric     return TM.isPPC64()
1540b57cec5SDimitry Andric                ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_SaveList
1550b57cec5SDimitry Andric                                          : CSR_Darwin64_SaveList)
1560b57cec5SDimitry Andric                : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_SaveList
1570b57cec5SDimitry Andric                                          : CSR_Darwin32_SaveList);
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   if (TM.isPPC64() && MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
1600b57cec5SDimitry Andric     return CSR_SRV464_TLS_PE_SaveList;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   // On PPC64, we might need to save r2 (but only if it is not reserved).
1630b57cec5SDimitry Andric   bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2);
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   // Cold calling convention CSRs.
1660b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::Cold) {
1670b57cec5SDimitry Andric     if (TM.isPPC64()) {
1680b57cec5SDimitry Andric       if (Subtarget.hasAltivec())
1690b57cec5SDimitry Andric         return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
1700b57cec5SDimitry Andric                       : CSR_SVR64_ColdCC_Altivec_SaveList;
1710b57cec5SDimitry Andric       return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
1720b57cec5SDimitry Andric                     : CSR_SVR64_ColdCC_SaveList;
1730b57cec5SDimitry Andric     }
1740b57cec5SDimitry Andric     // 32-bit targets.
1750b57cec5SDimitry Andric     if (Subtarget.hasAltivec())
1760b57cec5SDimitry Andric       return CSR_SVR32_ColdCC_Altivec_SaveList;
1770b57cec5SDimitry Andric     else if (Subtarget.hasSPE())
1780b57cec5SDimitry Andric       return CSR_SVR32_ColdCC_SPE_SaveList;
1790b57cec5SDimitry Andric     return CSR_SVR32_ColdCC_SaveList;
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric   // Standard calling convention CSRs.
1820b57cec5SDimitry Andric   if (TM.isPPC64()) {
1830b57cec5SDimitry Andric     if (Subtarget.hasAltivec())
1840b57cec5SDimitry Andric       return SaveR2 ? CSR_SVR464_R2_Altivec_SaveList
1850b57cec5SDimitry Andric                     : CSR_SVR464_Altivec_SaveList;
1860b57cec5SDimitry Andric     return SaveR2 ? CSR_SVR464_R2_SaveList
1870b57cec5SDimitry Andric                   : CSR_SVR464_SaveList;
1880b57cec5SDimitry Andric   }
1890b57cec5SDimitry Andric   // 32-bit targets.
1900b57cec5SDimitry Andric   if (Subtarget.hasAltivec())
1910b57cec5SDimitry Andric     return CSR_SVR432_Altivec_SaveList;
1920b57cec5SDimitry Andric   else if (Subtarget.hasSPE())
1930b57cec5SDimitry Andric     return CSR_SVR432_SPE_SaveList;
1940b57cec5SDimitry Andric   return CSR_SVR432_SaveList;
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric const MCPhysReg *
1980b57cec5SDimitry Andric PPCRegisterInfo::getCalleeSavedRegsViaCopy(const MachineFunction *MF) const {
1990b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
2000b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
2010b57cec5SDimitry Andric   if (Subtarget.isDarwinABI())
2020b57cec5SDimitry Andric     return nullptr;
2030b57cec5SDimitry Andric   if (!TM.isPPC64())
2040b57cec5SDimitry Andric     return nullptr;
2050b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() != CallingConv::CXX_FAST_TLS)
2060b57cec5SDimitry Andric     return nullptr;
2070b57cec5SDimitry Andric   if (!MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
2080b57cec5SDimitry Andric     return nullptr;
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   // On PPC64, we might need to save r2 (but only if it is not reserved).
2110b57cec5SDimitry Andric   bool SaveR2 = !getReservedRegs(*MF).test(PPC::X2);
2120b57cec5SDimitry Andric   if (Subtarget.hasAltivec())
2130b57cec5SDimitry Andric     return SaveR2
2140b57cec5SDimitry Andric       ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList
2150b57cec5SDimitry Andric       : CSR_SVR464_Altivec_ViaCopy_SaveList;
2160b57cec5SDimitry Andric   else
2170b57cec5SDimitry Andric     return SaveR2
2180b57cec5SDimitry Andric       ? CSR_SVR464_R2_ViaCopy_SaveList
2190b57cec5SDimitry Andric       : CSR_SVR464_ViaCopy_SaveList;
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric const uint32_t *
2230b57cec5SDimitry Andric PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
2240b57cec5SDimitry Andric                                       CallingConv::ID CC) const {
2250b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
2260b57cec5SDimitry Andric   if (CC == CallingConv::AnyReg) {
2270b57cec5SDimitry Andric     if (Subtarget.hasVSX())
2280b57cec5SDimitry Andric       return CSR_64_AllRegs_VSX_RegMask;
2290b57cec5SDimitry Andric     if (Subtarget.hasAltivec())
2300b57cec5SDimitry Andric       return CSR_64_AllRegs_Altivec_RegMask;
2310b57cec5SDimitry Andric     return CSR_64_AllRegs_RegMask;
2320b57cec5SDimitry Andric   }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   if (Subtarget.isDarwinABI())
2350b57cec5SDimitry Andric     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_RegMask
2360b57cec5SDimitry Andric                                                   : CSR_Darwin64_RegMask)
2370b57cec5SDimitry Andric                         : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_RegMask
2380b57cec5SDimitry Andric                                                   : CSR_Darwin32_RegMask);
2390b57cec5SDimitry Andric   if (Subtarget.isAIXABI()) {
2400b57cec5SDimitry Andric     assert(!Subtarget.hasAltivec() && "Altivec is not implemented on AIX yet.");
2410b57cec5SDimitry Andric     return TM.isPPC64() ? CSR_AIX64_RegMask : CSR_AIX32_RegMask;
2420b57cec5SDimitry Andric   }
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric   if (CC == CallingConv::Cold) {
2450b57cec5SDimitry Andric     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
2460b57cec5SDimitry Andric                                                   : CSR_SVR64_ColdCC_RegMask)
2470b57cec5SDimitry Andric                         : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
2480b57cec5SDimitry Andric                                                   : (Subtarget.hasSPE()
2490b57cec5SDimitry Andric                                                   ? CSR_SVR32_ColdCC_SPE_RegMask
2500b57cec5SDimitry Andric                                                   : CSR_SVR32_ColdCC_RegMask));
2510b57cec5SDimitry Andric   }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR464_Altivec_RegMask
2540b57cec5SDimitry Andric                                                 : CSR_SVR464_RegMask)
2550b57cec5SDimitry Andric                       : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_RegMask
2560b57cec5SDimitry Andric                                                 : (Subtarget.hasSPE()
2570b57cec5SDimitry Andric                                                   ? CSR_SVR432_SPE_RegMask
2580b57cec5SDimitry Andric                                                   : CSR_SVR432_RegMask));
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric const uint32_t*
2620b57cec5SDimitry Andric PPCRegisterInfo::getNoPreservedMask() const {
2630b57cec5SDimitry Andric   return CSR_NoRegs_RegMask;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
2670b57cec5SDimitry Andric   for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
2680b57cec5SDimitry Andric     Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
2720b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
2730b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
2740b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   // The ZERO register is not really a register, but the representation of r0
2770b57cec5SDimitry Andric   // when used in instructions that treat r0 as the constant 0.
2780b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::ZERO);
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   // The FP register is also not really a register, but is the representation
2810b57cec5SDimitry Andric   // of the frame pointer register used by ISD::FRAMEADDR.
2820b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::FP);
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   // The BP register is also not really a register, but is the representation
2850b57cec5SDimitry Andric   // of the base pointer register used by setjmp.
2860b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::BP);
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   // The counter registers must be reserved so that counter-based loops can
2890b57cec5SDimitry Andric   // be correctly formed (and the mtctr instructions are not DCE'd).
2900b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::CTR);
2910b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::CTR8);
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::R1);
2940b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::LR);
2950b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::LR8);
2960b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::RM);
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec())
2990b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::VRSAVE);
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   // The SVR4 ABI reserves r2 and r13
3020b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI()) {
3030b57cec5SDimitry Andric     // We only reserve r2 if we need to use the TOC pointer. If we have no
3040b57cec5SDimitry Andric     // explicit uses of the TOC pointer (meaning we're a leaf function with
3050b57cec5SDimitry Andric     // no constant-pool loads, etc.) and we have no potential uses inside an
3060b57cec5SDimitry Andric     // inline asm block, then we can treat r2 has an ordinary callee-saved
3070b57cec5SDimitry Andric     // register.
3080b57cec5SDimitry Andric     const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3090b57cec5SDimitry Andric     if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
3100b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R2);  // System-reserved register
3110b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
3120b57cec5SDimitry Andric   }
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   // Always reserve r2 on AIX for now.
3150b57cec5SDimitry Andric   // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions.
3160b57cec5SDimitry Andric   if (Subtarget.isAIXABI())
3170b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R2);  // System-reserved register
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   // On PPC64, r13 is the thread pointer. Never allocate this register.
3200b57cec5SDimitry Andric   if (TM.isPPC64())
3210b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R13);
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   if (TFI->needsFP(MF))
3240b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R31);
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   bool IsPositionIndependent = TM.isPositionIndependent();
3270b57cec5SDimitry Andric   if (hasBasePointer(MF)) {
3288bcb0991SDimitry Andric     if (Subtarget.is32BitELFABI() && IsPositionIndependent)
3290b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R29);
3300b57cec5SDimitry Andric     else
3310b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R30);
3320b57cec5SDimitry Andric   }
3330b57cec5SDimitry Andric 
3348bcb0991SDimitry Andric   if (Subtarget.is32BitELFABI() && IsPositionIndependent)
3350b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R30);
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   // Reserve Altivec registers when Altivec is unavailable.
3380b57cec5SDimitry Andric   if (!Subtarget.hasAltivec())
3390b57cec5SDimitry Andric     for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
3400b57cec5SDimitry Andric          IE = PPC::VRRCRegClass.end(); I != IE; ++I)
3410b57cec5SDimitry Andric       markSuperRegs(Reserved, *I);
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
3440b57cec5SDimitry Andric   return Reserved;
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const {
3480b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
3490b57cec5SDimitry Andric   const PPCInstrInfo *InstrInfo =  Subtarget.getInstrInfo();
3500b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
3510b57cec5SDimitry Andric   const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   // If the callee saved info is invalid we have to default to true for safety.
3540b57cec5SDimitry Andric   if (!MFI.isCalleeSavedInfoValid())
3550b57cec5SDimitry Andric     return true;
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   // We will require the use of X-Forms because the frame is larger than what
3580b57cec5SDimitry Andric   // can be represented in signed 16 bits that fit in the immediate of a D-Form.
3590b57cec5SDimitry Andric   // If we need an X-Form then we need a register to store the address offset.
3600b57cec5SDimitry Andric   unsigned FrameSize = MFI.getStackSize();
3610b57cec5SDimitry Andric   // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
3620b57cec5SDimitry Andric   if (FrameSize & ~0x7FFF)
3630b57cec5SDimitry Andric     return true;
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   // The callee saved info is valid so it can be traversed.
3660b57cec5SDimitry Andric   // Checking for registers that need saving that do not have load or store
3670b57cec5SDimitry Andric   // forms where the address offset is an immediate.
3680b57cec5SDimitry Andric   for (unsigned i = 0; i < Info.size(); i++) {
3690b57cec5SDimitry Andric     int FrIdx = Info[i].getFrameIdx();
3700b57cec5SDimitry Andric     unsigned Reg = Info[i].getReg();
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric     unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(Reg);
3730b57cec5SDimitry Andric     if (!MFI.isFixedObjectIndex(FrIdx)) {
3740b57cec5SDimitry Andric       // This is not a fixed object. If it requires alignment then we may still
3750b57cec5SDimitry Andric       // need to use the XForm.
3760b57cec5SDimitry Andric       if (offsetMinAlignForOpcode(Opcode) > 1)
3770b57cec5SDimitry Andric         return true;
3780b57cec5SDimitry Andric     }
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric     // This is eiher:
3810b57cec5SDimitry Andric     // 1) A fixed frame index object which we know are aligned so
3820b57cec5SDimitry Andric     // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
383*480093f4SDimitry Andric     // need to consider the alignment here.
3840b57cec5SDimitry Andric     // 2) A not fixed object but in that case we now know that the min required
3850b57cec5SDimitry Andric     // alignment is no more than 1 based on the previous check.
3860b57cec5SDimitry Andric     if (InstrInfo->isXFormMemOp(Opcode))
3870b57cec5SDimitry Andric       return true;
3880b57cec5SDimitry Andric   }
3890b57cec5SDimitry Andric   return false;
3900b57cec5SDimitry Andric }
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric bool PPCRegisterInfo::isCallerPreservedPhysReg(unsigned PhysReg,
3930b57cec5SDimitry Andric                                                const MachineFunction &MF) const {
3948bcb0991SDimitry Andric   assert(Register::isPhysicalRegister(PhysReg));
3950b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
3960b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
3970b57cec5SDimitry Andric   if (!TM.isPPC64())
3980b57cec5SDimitry Andric     return false;
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric   if (!Subtarget.isSVR4ABI())
4010b57cec5SDimitry Andric     return false;
4020b57cec5SDimitry Andric   if (PhysReg == PPC::X2)
4030b57cec5SDimitry Andric     // X2 is guaranteed to be preserved within a function if it is reserved.
4040b57cec5SDimitry Andric     // The reason it's reserved is that it's the TOC pointer (and the function
4050b57cec5SDimitry Andric     // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
4060b57cec5SDimitry Andric     // with no TOC access), we can't claim that it is preserved.
4070b57cec5SDimitry Andric     return (getReservedRegs(MF).test(PPC::X2));
4080b57cec5SDimitry Andric   if (StackPtrConst && (PhysReg == PPC::X1) && !MFI.hasVarSizedObjects()
4090b57cec5SDimitry Andric       && !MFI.hasOpaqueSPAdjustment())
4100b57cec5SDimitry Andric     // The value of the stack pointer does not change within a function after
4110b57cec5SDimitry Andric     // the prologue and before the epilogue if there are no dynamic allocations
4120b57cec5SDimitry Andric     // and no inline asm which clobbers X1.
4130b57cec5SDimitry Andric     return true;
4140b57cec5SDimitry Andric   return false;
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
4180b57cec5SDimitry Andric                                               MachineFunction &MF) const {
4190b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
4200b57cec5SDimitry Andric   const unsigned DefaultSafety = 1;
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric   switch (RC->getID()) {
4230b57cec5SDimitry Andric   default:
4240b57cec5SDimitry Andric     return 0;
4250b57cec5SDimitry Andric   case PPC::G8RC_NOX0RegClassID:
4260b57cec5SDimitry Andric   case PPC::GPRC_NOR0RegClassID:
4270b57cec5SDimitry Andric   case PPC::SPERCRegClassID:
4280b57cec5SDimitry Andric   case PPC::G8RCRegClassID:
4290b57cec5SDimitry Andric   case PPC::GPRCRegClassID: {
4300b57cec5SDimitry Andric     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
4310b57cec5SDimitry Andric     return 32 - FP - DefaultSafety;
4320b57cec5SDimitry Andric   }
4330b57cec5SDimitry Andric   case PPC::F8RCRegClassID:
4340b57cec5SDimitry Andric   case PPC::F4RCRegClassID:
4350b57cec5SDimitry Andric   case PPC::QFRCRegClassID:
4360b57cec5SDimitry Andric   case PPC::QSRCRegClassID:
4370b57cec5SDimitry Andric   case PPC::QBRCRegClassID:
4380b57cec5SDimitry Andric   case PPC::VRRCRegClassID:
4390b57cec5SDimitry Andric   case PPC::VFRCRegClassID:
4400b57cec5SDimitry Andric   case PPC::VSLRCRegClassID:
4410b57cec5SDimitry Andric     return 32 - DefaultSafety;
4420b57cec5SDimitry Andric   case PPC::VSRCRegClassID:
4430b57cec5SDimitry Andric   case PPC::VSFRCRegClassID:
4440b57cec5SDimitry Andric   case PPC::VSSRCRegClassID:
4450b57cec5SDimitry Andric     return 64 - DefaultSafety;
4460b57cec5SDimitry Andric   case PPC::CRRCRegClassID:
4470b57cec5SDimitry Andric     return 8 - DefaultSafety;
4480b57cec5SDimitry Andric   }
4490b57cec5SDimitry Andric }
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric const TargetRegisterClass *
4520b57cec5SDimitry Andric PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
4530b57cec5SDimitry Andric                                            const MachineFunction &MF) const {
4540b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
4550b57cec5SDimitry Andric   if (Subtarget.hasVSX()) {
4560b57cec5SDimitry Andric     // With VSX, we can inflate various sub-register classes to the full VSX
4570b57cec5SDimitry Andric     // register set.
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric     // For Power9 we allow the user to enable GPR to vector spills.
4600b57cec5SDimitry Andric     // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
4610b57cec5SDimitry Andric     // support to spill GPRC.
4620b57cec5SDimitry Andric     if (TM.isELFv2ABI()) {
4630b57cec5SDimitry Andric       if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
4640b57cec5SDimitry Andric           RC == &PPC::G8RCRegClass) {
4650b57cec5SDimitry Andric         InflateGP8RC++;
4660b57cec5SDimitry Andric         return &PPC::SPILLTOVSRRCRegClass;
4670b57cec5SDimitry Andric       }
4680b57cec5SDimitry Andric       if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
4690b57cec5SDimitry Andric         InflateGPRC++;
4700b57cec5SDimitry Andric     }
4710b57cec5SDimitry Andric     if (RC == &PPC::F8RCRegClass)
4720b57cec5SDimitry Andric       return &PPC::VSFRCRegClass;
4730b57cec5SDimitry Andric     else if (RC == &PPC::VRRCRegClass)
4740b57cec5SDimitry Andric       return &PPC::VSRCRegClass;
4750b57cec5SDimitry Andric     else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector())
4760b57cec5SDimitry Andric       return &PPC::VSSRCRegClass;
4770b57cec5SDimitry Andric   }
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4830b57cec5SDimitry Andric // Stack Frame Processing methods
4840b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric /// lowerDynamicAlloc - Generate the code for allocating an object in the
4870b57cec5SDimitry Andric /// current frame.  The sequence of code will be in the general form
4880b57cec5SDimitry Andric ///
4890b57cec5SDimitry Andric ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
4900b57cec5SDimitry Andric ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
4910b57cec5SDimitry Andric ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
4920b57cec5SDimitry Andric ///
4930b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
4940b57cec5SDimitry Andric   // Get the instruction.
4950b57cec5SDimitry Andric   MachineInstr &MI = *II;
4960b57cec5SDimitry Andric   // Get the instruction's basic block.
4970b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
4980b57cec5SDimitry Andric   // Get the basic block's function.
4990b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
5000b57cec5SDimitry Andric   // Get the frame info.
5010b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
5020b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
5030b57cec5SDimitry Andric   // Get the instruction info.
5040b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
5050b57cec5SDimitry Andric   // Determine whether 64-bit pointers are used.
5060b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
5070b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   // Get the maximum call stack size.
5100b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
5110b57cec5SDimitry Andric   // Get the total frame size.
5120b57cec5SDimitry Andric   unsigned FrameSize = MFI.getStackSize();
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric   // Get stack alignments.
5150b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
5160b57cec5SDimitry Andric   unsigned TargetAlign = TFI->getStackAlignment();
5170b57cec5SDimitry Andric   unsigned MaxAlign = MFI.getMaxAlignment();
5180b57cec5SDimitry Andric   assert((maxCallFrameSize & (MaxAlign-1)) == 0 &&
5190b57cec5SDimitry Andric          "Maximum call-frame size not sufficiently aligned");
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric   // Determine the previous frame's address.  If FrameSize can't be
5220b57cec5SDimitry Andric   // represented as 16 bits or we need special alignment, then we load the
5230b57cec5SDimitry Andric   // previous frame's address from 0(SP).  Why not do an addis of the hi?
5240b57cec5SDimitry Andric   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
5250b57cec5SDimitry Andric   // Constructing the constant and adding would take 3 instructions.
5260b57cec5SDimitry Andric   // Fortunately, a frame greater than 32K is rare.
5270b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
5280b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
5298bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
5320b57cec5SDimitry Andric     if (LP64)
5330b57cec5SDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), Reg)
5340b57cec5SDimitry Andric         .addReg(PPC::X31)
5350b57cec5SDimitry Andric         .addImm(FrameSize);
5360b57cec5SDimitry Andric     else
5370b57cec5SDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
5380b57cec5SDimitry Andric         .addReg(PPC::R31)
5390b57cec5SDimitry Andric         .addImm(FrameSize);
5400b57cec5SDimitry Andric   } else if (LP64) {
5410b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
5420b57cec5SDimitry Andric       .addImm(0)
5430b57cec5SDimitry Andric       .addReg(PPC::X1);
5440b57cec5SDimitry Andric   } else {
5450b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
5460b57cec5SDimitry Andric       .addImm(0)
5470b57cec5SDimitry Andric       .addReg(PPC::R1);
5480b57cec5SDimitry Andric   }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric   bool KillNegSizeReg = MI.getOperand(1).isKill();
5518bcb0991SDimitry Andric   Register NegSizeReg = MI.getOperand(1).getReg();
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric   // Grow the stack and update the stack pointer link, then determine the
5540b57cec5SDimitry Andric   // address of new allocated space.
5550b57cec5SDimitry Andric   if (LP64) {
5560b57cec5SDimitry Andric     if (MaxAlign > TargetAlign) {
5570b57cec5SDimitry Andric       unsigned UnalNegSizeReg = NegSizeReg;
5580b57cec5SDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric       // Unfortunately, there is no andi, only andi., and we can't insert that
5610b57cec5SDimitry Andric       // here because we might clobber cr0 while it is live.
5620b57cec5SDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
5630b57cec5SDimitry Andric         .addImm(~(MaxAlign-1));
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric       unsigned NegSizeReg1 = NegSizeReg;
5660b57cec5SDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
5670b57cec5SDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
5680b57cec5SDimitry Andric         .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
5690b57cec5SDimitry Andric         .addReg(NegSizeReg1, RegState::Kill);
5700b57cec5SDimitry Andric       KillNegSizeReg = true;
5710b57cec5SDimitry Andric     }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
5740b57cec5SDimitry Andric       .addReg(Reg, RegState::Kill)
5750b57cec5SDimitry Andric       .addReg(PPC::X1)
5760b57cec5SDimitry Andric       .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
5770b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
5780b57cec5SDimitry Andric       .addReg(PPC::X1)
5790b57cec5SDimitry Andric       .addImm(maxCallFrameSize);
5800b57cec5SDimitry Andric   } else {
5810b57cec5SDimitry Andric     if (MaxAlign > TargetAlign) {
5820b57cec5SDimitry Andric       unsigned UnalNegSizeReg = NegSizeReg;
5830b57cec5SDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric       // Unfortunately, there is no andi, only andi., and we can't insert that
5860b57cec5SDimitry Andric       // here because we might clobber cr0 while it is live.
5870b57cec5SDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
5880b57cec5SDimitry Andric         .addImm(~(MaxAlign-1));
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric       unsigned NegSizeReg1 = NegSizeReg;
5910b57cec5SDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
5920b57cec5SDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
5930b57cec5SDimitry Andric         .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
5940b57cec5SDimitry Andric         .addReg(NegSizeReg1, RegState::Kill);
5950b57cec5SDimitry Andric       KillNegSizeReg = true;
5960b57cec5SDimitry Andric     }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
5990b57cec5SDimitry Andric       .addReg(Reg, RegState::Kill)
6000b57cec5SDimitry Andric       .addReg(PPC::R1)
6010b57cec5SDimitry Andric       .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
6020b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
6030b57cec5SDimitry Andric       .addReg(PPC::R1)
6040b57cec5SDimitry Andric       .addImm(maxCallFrameSize);
6050b57cec5SDimitry Andric   }
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric   // Discard the DYNALLOC instruction.
6080b57cec5SDimitry Andric   MBB.erase(II);
6090b57cec5SDimitry Andric }
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAreaOffset(
6120b57cec5SDimitry Andric     MachineBasicBlock::iterator II) const {
6130b57cec5SDimitry Andric   // Get the instruction.
6140b57cec5SDimitry Andric   MachineInstr &MI = *II;
6150b57cec5SDimitry Andric   // Get the instruction's basic block.
6160b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
6170b57cec5SDimitry Andric   // Get the basic block's function.
6180b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
6190b57cec5SDimitry Andric   // Get the frame info.
6200b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
6210b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
6220b57cec5SDimitry Andric   // Get the instruction info.
6230b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
6260b57cec5SDimitry Andric   bool is64Bit = TM.isPPC64();
6270b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
6280b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
6290b57cec5SDimitry Andric           MI.getOperand(0).getReg())
6300b57cec5SDimitry Andric       .addImm(maxCallFrameSize);
6310b57cec5SDimitry Andric   MBB.erase(II);
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
6350b57cec5SDimitry Andric /// reserving a whole register (R0), we scrounge for one here. This generates
6360b57cec5SDimitry Andric /// code like this:
6370b57cec5SDimitry Andric ///
6380b57cec5SDimitry Andric ///   mfcr rA                  ; Move the conditional register into GPR rA.
6390b57cec5SDimitry Andric ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
6400b57cec5SDimitry Andric ///   stw rA, FI               ; Store rA to the frame.
6410b57cec5SDimitry Andric ///
6420b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
6430b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
6440b57cec5SDimitry Andric   // Get the instruction.
6450b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
6460b57cec5SDimitry Andric   // Get the instruction's basic block.
6470b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
6480b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
6490b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
6500b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
6510b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
6520b57cec5SDimitry Andric 
6530b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
6540b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
6550b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
6560b57cec5SDimitry Andric 
6578bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
6588bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric   // We need to store the CR in the low 4-bits of the saved value. First, issue
6610b57cec5SDimitry Andric   // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
6620b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
6630b57cec5SDimitry Andric       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
6640b57cec5SDimitry Andric 
6650b57cec5SDimitry Andric   // If the saved register wasn't CR0, shift the bits left so that they are in
6660b57cec5SDimitry Andric   // CR0's slot.
6670b57cec5SDimitry Andric   if (SrcReg != PPC::CR0) {
6680b57cec5SDimitry Andric     unsigned Reg1 = Reg;
6690b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric     // rlwinm rA, rA, ShiftBits, 0, 31.
6720b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
6730b57cec5SDimitry Andric       .addReg(Reg1, RegState::Kill)
6740b57cec5SDimitry Andric       .addImm(getEncodingValue(SrcReg) * 4)
6750b57cec5SDimitry Andric       .addImm(0)
6760b57cec5SDimitry Andric       .addImm(31);
6770b57cec5SDimitry Andric   }
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
6800b57cec5SDimitry Andric                     .addReg(Reg, RegState::Kill),
6810b57cec5SDimitry Andric                     FrameIndex);
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   // Discard the pseudo instruction.
6840b57cec5SDimitry Andric   MBB.erase(II);
6850b57cec5SDimitry Andric }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
6880b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
6890b57cec5SDimitry Andric   // Get the instruction.
6900b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
6910b57cec5SDimitry Andric   // Get the instruction's basic block.
6920b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
6930b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
6940b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
6950b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
6960b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
6990b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
7000b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
7010b57cec5SDimitry Andric 
7028bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
7038bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
7040b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
7050b57cec5SDimitry Andric     "RESTORE_CR does not define its destination");
7060b57cec5SDimitry Andric 
7070b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
7080b57cec5SDimitry Andric                               Reg), FrameIndex);
7090b57cec5SDimitry Andric 
7100b57cec5SDimitry Andric   // If the reloaded register isn't CR0, shift the bits right so that they are
7110b57cec5SDimitry Andric   // in the right CR's slot.
7120b57cec5SDimitry Andric   if (DestReg != PPC::CR0) {
7130b57cec5SDimitry Andric     unsigned Reg1 = Reg;
7140b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric     unsigned ShiftBits = getEncodingValue(DestReg)*4;
7170b57cec5SDimitry Andric     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
7180b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
7190b57cec5SDimitry Andric              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
7200b57cec5SDimitry Andric              .addImm(31);
7210b57cec5SDimitry Andric   }
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
7240b57cec5SDimitry Andric              .addReg(Reg, RegState::Kill);
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric   // Discard the pseudo instruction.
7270b57cec5SDimitry Andric   MBB.erase(II);
7280b57cec5SDimitry Andric }
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
7310b57cec5SDimitry Andric                                          unsigned FrameIndex) const {
7320b57cec5SDimitry Andric   // Get the instruction.
7330b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_CRBIT <SrcReg>, <offset>
7340b57cec5SDimitry Andric   // Get the instruction's basic block.
7350b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
7360b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
7370b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
7380b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
7390b57cec5SDimitry Andric   const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
7400b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
7410b57cec5SDimitry Andric 
7420b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
7430b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
7440b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
7450b57cec5SDimitry Andric 
7468bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
7478bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric   // Search up the BB to find the definition of the CR bit.
750*480093f4SDimitry Andric   MachineBasicBlock::reverse_iterator Ins = MI;
751*480093f4SDimitry Andric   MachineBasicBlock::reverse_iterator Rend = MBB.rend();
752*480093f4SDimitry Andric   ++Ins;
7530b57cec5SDimitry Andric   unsigned CRBitSpillDistance = 0;
754*480093f4SDimitry Andric   bool SeenUse = false;
755*480093f4SDimitry Andric   for (; Ins != Rend; ++Ins) {
7560b57cec5SDimitry Andric     // Definition found.
7570b57cec5SDimitry Andric     if (Ins->modifiesRegister(SrcReg, TRI))
7580b57cec5SDimitry Andric       break;
759*480093f4SDimitry Andric     // Use found.
760*480093f4SDimitry Andric     if (Ins->readsRegister(SrcReg, TRI))
761*480093f4SDimitry Andric       SeenUse = true;
7620b57cec5SDimitry Andric     // Unable to find CR bit definition within maximum search distance.
7630b57cec5SDimitry Andric     if (CRBitSpillDistance == MaxCRBitSpillDist) {
7640b57cec5SDimitry Andric       Ins = MI;
7650b57cec5SDimitry Andric       break;
7660b57cec5SDimitry Andric     }
7670b57cec5SDimitry Andric     // Skip debug instructions when counting CR bit spill distance.
7680b57cec5SDimitry Andric     if (!Ins->isDebugInstr())
7690b57cec5SDimitry Andric       CRBitSpillDistance++;
7700b57cec5SDimitry Andric   }
7710b57cec5SDimitry Andric 
7720b57cec5SDimitry Andric   // Unable to find the definition of the CR bit in the MBB.
7730b57cec5SDimitry Andric   if (Ins == MBB.rend())
7740b57cec5SDimitry Andric     Ins = MI;
7750b57cec5SDimitry Andric 
776*480093f4SDimitry Andric   bool SpillsKnownBit = false;
7770b57cec5SDimitry Andric   // There is no need to extract the CR bit if its value is already known.
7780b57cec5SDimitry Andric   switch (Ins->getOpcode()) {
7790b57cec5SDimitry Andric   case PPC::CRUNSET:
7800b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
7810b57cec5SDimitry Andric       .addImm(0);
782*480093f4SDimitry Andric     SpillsKnownBit = true;
7830b57cec5SDimitry Andric     break;
7840b57cec5SDimitry Andric   case PPC::CRSET:
7850b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
7860b57cec5SDimitry Andric       .addImm(-32768);
787*480093f4SDimitry Andric     SpillsKnownBit = true;
7880b57cec5SDimitry Andric     break;
7890b57cec5SDimitry Andric   default:
790*480093f4SDimitry Andric     // On Power9, we can use SETB to extract the LT bit. This only works for
791*480093f4SDimitry Andric     // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value
792*480093f4SDimitry Andric     // of the bit we care about (32-bit sign bit) will be set to the value of
793*480093f4SDimitry Andric     // the LT bit (regardless of the other bits in the CR field).
794*480093f4SDimitry Andric     if (Subtarget.isISA3_0()) {
795*480093f4SDimitry Andric       if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT ||
796*480093f4SDimitry Andric           SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT ||
797*480093f4SDimitry Andric           SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT ||
798*480093f4SDimitry Andric           SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) {
799*480093f4SDimitry Andric         BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg)
800*480093f4SDimitry Andric           .addReg(getCRFromCRBit(SrcReg), RegState::Undef);
801*480093f4SDimitry Andric         break;
802*480093f4SDimitry Andric       }
803*480093f4SDimitry Andric     }
804*480093f4SDimitry Andric 
8050b57cec5SDimitry Andric     // We need to move the CR field that contains the CR bit we are spilling.
8060b57cec5SDimitry Andric     // The super register may not be explicitly defined (i.e. it can be defined
8070b57cec5SDimitry Andric     // by a CR-logical that only defines the subreg) so we state that the CR
8080b57cec5SDimitry Andric     // field is undef. Also, in order to preserve the kill flag on the CR bit,
8090b57cec5SDimitry Andric     // we add it as an implicit use.
8100b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
8110b57cec5SDimitry Andric       .addReg(getCRFromCRBit(SrcReg), RegState::Undef)
8120b57cec5SDimitry Andric       .addReg(SrcReg,
8130b57cec5SDimitry Andric               RegState::Implicit | getKillRegState(MI.getOperand(0).isKill()));
8140b57cec5SDimitry Andric 
8150b57cec5SDimitry Andric     // If the saved register wasn't CR0LT, shift the bits left so that the bit
8160b57cec5SDimitry Andric     // to store is the first one. Mask all but that bit.
8170b57cec5SDimitry Andric     unsigned Reg1 = Reg;
8180b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric     // rlwinm rA, rA, ShiftBits, 0, 0.
8210b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
8220b57cec5SDimitry Andric       .addReg(Reg1, RegState::Kill)
8230b57cec5SDimitry Andric       .addImm(getEncodingValue(SrcReg))
8240b57cec5SDimitry Andric       .addImm(0).addImm(0);
8250b57cec5SDimitry Andric   }
8260b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
8270b57cec5SDimitry Andric                     .addReg(Reg, RegState::Kill),
8280b57cec5SDimitry Andric                     FrameIndex);
8290b57cec5SDimitry Andric 
830*480093f4SDimitry Andric   bool KillsCRBit = MI.killsRegister(SrcReg, TRI);
8310b57cec5SDimitry Andric   // Discard the pseudo instruction.
8320b57cec5SDimitry Andric   MBB.erase(II);
833*480093f4SDimitry Andric   if (SpillsKnownBit && KillsCRBit && !SeenUse) {
834*480093f4SDimitry Andric     Ins->setDesc(TII.get(PPC::UNENCODED_NOP));
835*480093f4SDimitry Andric     Ins->RemoveOperand(0);
836*480093f4SDimitry Andric   }
8370b57cec5SDimitry Andric }
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
8400b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
8410b57cec5SDimitry Andric   // Get the instruction.
8420b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CRBIT <offset>
8430b57cec5SDimitry Andric   // Get the instruction's basic block.
8440b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8450b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
8460b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
8470b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
8480b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
8490b57cec5SDimitry Andric 
8500b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
8510b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
8520b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
8530b57cec5SDimitry Andric 
8548bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
8558bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
8560b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
8570b57cec5SDimitry Andric     "RESTORE_CRBIT does not define its destination");
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
8600b57cec5SDimitry Andric                               Reg), FrameIndex);
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
8630b57cec5SDimitry Andric 
8648bcb0991SDimitry Andric   Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
8650b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
8660b57cec5SDimitry Andric           .addReg(getCRFromCRBit(DestReg));
8670b57cec5SDimitry Andric 
8680b57cec5SDimitry Andric   unsigned ShiftBits = getEncodingValue(DestReg);
8690b57cec5SDimitry Andric   // rlwimi r11, r10, 32-ShiftBits, ..., ...
8700b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
8710b57cec5SDimitry Andric       .addReg(RegO, RegState::Kill)
8720b57cec5SDimitry Andric       .addReg(Reg, RegState::Kill)
8730b57cec5SDimitry Andric       .addImm(ShiftBits ? 32 - ShiftBits : 0)
8740b57cec5SDimitry Andric       .addImm(ShiftBits)
8750b57cec5SDimitry Andric       .addImm(ShiftBits);
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
8780b57cec5SDimitry Andric           getCRFromCRBit(DestReg))
8790b57cec5SDimitry Andric       .addReg(RegO, RegState::Kill)
8800b57cec5SDimitry Andric       // Make sure we have a use dependency all the way through this
8810b57cec5SDimitry Andric       // sequence of instructions. We can't have the other bits in the CR
8820b57cec5SDimitry Andric       // modified in between the mfocrf and the mtocrf.
8830b57cec5SDimitry Andric       .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric   // Discard the pseudo instruction.
8860b57cec5SDimitry Andric   MBB.erase(II);
8870b57cec5SDimitry Andric }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II,
8900b57cec5SDimitry Andric                                           unsigned FrameIndex) const {
8910b57cec5SDimitry Andric   // Get the instruction.
8920b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_VRSAVE <SrcReg>, <offset>
8930b57cec5SDimitry Andric   // Get the instruction's basic block.
8940b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8950b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
8960b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
8970b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
8980b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
9018bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(GPRC);
9028bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
9050b57cec5SDimitry Andric       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric   addFrameReference(
9080b57cec5SDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill),
9090b57cec5SDimitry Andric       FrameIndex);
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric   // Discard the pseudo instruction.
9120b57cec5SDimitry Andric   MBB.erase(II);
9130b57cec5SDimitry Andric }
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II,
9160b57cec5SDimitry Andric                                          unsigned FrameIndex) const {
9170b57cec5SDimitry Andric   // Get the instruction.
9180b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_VRSAVE <offset>
9190b57cec5SDimitry Andric   // Get the instruction's basic block.
9200b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
9210b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
9220b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9230b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
9240b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
9278bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(GPRC);
9288bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
9290b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
9300b57cec5SDimitry Andric     "RESTORE_VRSAVE does not define its destination");
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
9330b57cec5SDimitry Andric                               Reg), FrameIndex);
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
9360b57cec5SDimitry Andric              .addReg(Reg, RegState::Kill);
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric   // Discard the pseudo instruction.
9390b57cec5SDimitry Andric   MBB.erase(II);
9400b57cec5SDimitry Andric }
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
9430b57cec5SDimitry Andric                                            unsigned Reg, int &FrameIdx) const {
9440b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9450b57cec5SDimitry Andric   // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
9460b57cec5SDimitry Andric   // ABI, return true to prevent allocating an additional frame slot.
9470b57cec5SDimitry Andric   // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
9480b57cec5SDimitry Andric   // is arbitrary and will be subsequently ignored.  For 32-bit, we have
9490b57cec5SDimitry Andric   // previously created the stack slot if needed, so return its FrameIdx.
9500b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
9510b57cec5SDimitry Andric     if (TM.isPPC64())
9520b57cec5SDimitry Andric       FrameIdx = 0;
9530b57cec5SDimitry Andric     else {
9540b57cec5SDimitry Andric       const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
9550b57cec5SDimitry Andric       FrameIdx = FI->getCRSpillFrameIndex();
9560b57cec5SDimitry Andric     }
9570b57cec5SDimitry Andric     return true;
9580b57cec5SDimitry Andric   }
9590b57cec5SDimitry Andric   return false;
9600b57cec5SDimitry Andric }
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is.
9630b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC) {
9640b57cec5SDimitry Andric   switch (OpC) {
9650b57cec5SDimitry Andric   default:
9660b57cec5SDimitry Andric     return 1;
9670b57cec5SDimitry Andric   case PPC::LWA:
9680b57cec5SDimitry Andric   case PPC::LWA_32:
9690b57cec5SDimitry Andric   case PPC::LD:
9700b57cec5SDimitry Andric   case PPC::LDU:
9710b57cec5SDimitry Andric   case PPC::STD:
9720b57cec5SDimitry Andric   case PPC::STDU:
9730b57cec5SDimitry Andric   case PPC::DFLOADf32:
9740b57cec5SDimitry Andric   case PPC::DFLOADf64:
9750b57cec5SDimitry Andric   case PPC::DFSTOREf32:
9760b57cec5SDimitry Andric   case PPC::DFSTOREf64:
9770b57cec5SDimitry Andric   case PPC::LXSD:
9780b57cec5SDimitry Andric   case PPC::LXSSP:
9790b57cec5SDimitry Andric   case PPC::STXSD:
9800b57cec5SDimitry Andric   case PPC::STXSSP:
9810b57cec5SDimitry Andric     return 4;
9820b57cec5SDimitry Andric   case PPC::EVLDD:
9830b57cec5SDimitry Andric   case PPC::EVSTDD:
9840b57cec5SDimitry Andric     return 8;
9850b57cec5SDimitry Andric   case PPC::LXV:
9860b57cec5SDimitry Andric   case PPC::STXV:
9870b57cec5SDimitry Andric     return 16;
9880b57cec5SDimitry Andric   }
9890b57cec5SDimitry Andric }
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is.
9920b57cec5SDimitry Andric static unsigned offsetMinAlign(const MachineInstr &MI) {
9930b57cec5SDimitry Andric   unsigned OpC = MI.getOpcode();
9940b57cec5SDimitry Andric   return offsetMinAlignForOpcode(OpC);
9950b57cec5SDimitry Andric }
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
9980b57cec5SDimitry Andric static unsigned getOffsetONFromFION(const MachineInstr &MI,
9990b57cec5SDimitry Andric                                     unsigned FIOperandNum) {
10000b57cec5SDimitry Andric   // Take into account whether it's an add or mem instruction
10010b57cec5SDimitry Andric   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
10020b57cec5SDimitry Andric   if (MI.isInlineAsm())
10030b57cec5SDimitry Andric     OffsetOperandNo = FIOperandNum - 1;
10040b57cec5SDimitry Andric   else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
10050b57cec5SDimitry Andric            MI.getOpcode() == TargetOpcode::PATCHPOINT)
10060b57cec5SDimitry Andric     OffsetOperandNo = FIOperandNum + 1;
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric   return OffsetOperandNo;
10090b57cec5SDimitry Andric }
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric void
10120b57cec5SDimitry Andric PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
10130b57cec5SDimitry Andric                                      int SPAdj, unsigned FIOperandNum,
10140b57cec5SDimitry Andric                                      RegScavenger *RS) const {
10150b57cec5SDimitry Andric   assert(SPAdj == 0 && "Unexpected");
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric   // Get the instruction.
10180b57cec5SDimitry Andric   MachineInstr &MI = *II;
10190b57cec5SDimitry Andric   // Get the instruction's basic block.
10200b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
10210b57cec5SDimitry Andric   // Get the basic block's function.
10220b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
10230b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
10240b57cec5SDimitry Andric   // Get the instruction info.
10250b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
10260b57cec5SDimitry Andric   // Get the frame info.
10270b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
10280b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
10290b57cec5SDimitry Andric 
10300b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric   // Get the frame index.
10330b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
10340b57cec5SDimitry Andric 
10350b57cec5SDimitry Andric   // Get the frame pointer save index.  Users of this index are primarily
10360b57cec5SDimitry Andric   // DYNALLOC instructions.
10370b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
10380b57cec5SDimitry Andric   int FPSI = FI->getFramePointerSaveIndex();
10390b57cec5SDimitry Andric   // Get the instruction opcode.
10400b57cec5SDimitry Andric   unsigned OpC = MI.getOpcode();
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric   if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
10430b57cec5SDimitry Andric     lowerDynamicAreaOffset(II);
10440b57cec5SDimitry Andric     return;
10450b57cec5SDimitry Andric   }
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric   // Special case for dynamic alloca.
10480b57cec5SDimitry Andric   if (FPSI && FrameIndex == FPSI &&
10490b57cec5SDimitry Andric       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
10500b57cec5SDimitry Andric     lowerDynamicAlloc(II);
10510b57cec5SDimitry Andric     return;
10520b57cec5SDimitry Andric   }
10530b57cec5SDimitry Andric 
10540b57cec5SDimitry Andric   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
10550b57cec5SDimitry Andric   if (OpC == PPC::SPILL_CR) {
10560b57cec5SDimitry Andric     lowerCRSpilling(II, FrameIndex);
10570b57cec5SDimitry Andric     return;
10580b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_CR) {
10590b57cec5SDimitry Andric     lowerCRRestore(II, FrameIndex);
10600b57cec5SDimitry Andric     return;
10610b57cec5SDimitry Andric   } else if (OpC == PPC::SPILL_CRBIT) {
10620b57cec5SDimitry Andric     lowerCRBitSpilling(II, FrameIndex);
10630b57cec5SDimitry Andric     return;
10640b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_CRBIT) {
10650b57cec5SDimitry Andric     lowerCRBitRestore(II, FrameIndex);
10660b57cec5SDimitry Andric     return;
10670b57cec5SDimitry Andric   } else if (OpC == PPC::SPILL_VRSAVE) {
10680b57cec5SDimitry Andric     lowerVRSAVESpilling(II, FrameIndex);
10690b57cec5SDimitry Andric     return;
10700b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_VRSAVE) {
10710b57cec5SDimitry Andric     lowerVRSAVERestore(II, FrameIndex);
10720b57cec5SDimitry Andric     return;
10730b57cec5SDimitry Andric   }
10740b57cec5SDimitry Andric 
10750b57cec5SDimitry Andric   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
10760b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(
10770b57cec5SDimitry Andric     FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   // If the instruction is not present in ImmToIdxMap, then it has no immediate
10800b57cec5SDimitry Andric   // form (and must be r+r).
10810b57cec5SDimitry Andric   bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
10820b57cec5SDimitry Andric                    OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
10830b57cec5SDimitry Andric 
10840b57cec5SDimitry Andric   // Now add the frame object offset to the offset from r1.
10850b57cec5SDimitry Andric   int Offset = MFI.getObjectOffset(FrameIndex);
10860b57cec5SDimitry Andric   Offset += MI.getOperand(OffsetOperandNo).getImm();
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric   // If we're not using a Frame Pointer that has been set to the value of the
10890b57cec5SDimitry Andric   // SP before having the stack size subtracted from it, then add the stack size
10900b57cec5SDimitry Andric   // to Offset to get the correct offset.
10910b57cec5SDimitry Andric   // Naked functions have stack size 0, although getStackSize may not reflect
10920b57cec5SDimitry Andric   // that because we didn't call all the pieces that compute it for naked
10930b57cec5SDimitry Andric   // functions.
10940b57cec5SDimitry Andric   if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
10950b57cec5SDimitry Andric     if (!(hasBasePointer(MF) && FrameIndex < 0))
10960b57cec5SDimitry Andric       Offset += MFI.getStackSize();
10970b57cec5SDimitry Andric   }
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric   // If we can, encode the offset directly into the instruction.  If this is a
11000b57cec5SDimitry Andric   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
11010b57cec5SDimitry Andric   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
11020b57cec5SDimitry Andric   // clear can be encoded.  This is extremely uncommon, because normally you
11030b57cec5SDimitry Andric   // only "std" to a stack slot that is at least 4-byte aligned, but it can
11040b57cec5SDimitry Andric   // happen in invalid code.
11050b57cec5SDimitry Andric   assert(OpC != PPC::DBG_VALUE &&
11060b57cec5SDimitry Andric          "This should be handled in a target-independent way");
11070b57cec5SDimitry Andric   bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ?
11080b57cec5SDimitry Andric                             isUInt<8>(Offset) :
11090b57cec5SDimitry Andric                             isInt<16>(Offset);
11100b57cec5SDimitry Andric   if (!noImmForm && ((OffsetFitsMnemonic &&
11110b57cec5SDimitry Andric                       ((Offset % offsetMinAlign(MI)) == 0)) ||
11120b57cec5SDimitry Andric                      OpC == TargetOpcode::STACKMAP ||
11130b57cec5SDimitry Andric                      OpC == TargetOpcode::PATCHPOINT)) {
11140b57cec5SDimitry Andric     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
11150b57cec5SDimitry Andric     return;
11160b57cec5SDimitry Andric   }
11170b57cec5SDimitry Andric 
11180b57cec5SDimitry Andric   // The offset doesn't fit into a single register, scavenge one to build the
11190b57cec5SDimitry Andric   // offset in.
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric   bool is64Bit = TM.isPPC64();
11220b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
11230b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
11240b57cec5SDimitry Andric   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
11250b57cec5SDimitry Andric   unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
11260b57cec5SDimitry Andric            SReg = MF.getRegInfo().createVirtualRegister(RC);
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric   // Insert a set of rA with the full offset value before the ld, st, or add
11290b57cec5SDimitry Andric   if (isInt<16>(Offset))
11300b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
11310b57cec5SDimitry Andric       .addImm(Offset);
11320b57cec5SDimitry Andric   else {
11330b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
11340b57cec5SDimitry Andric       .addImm(Offset >> 16);
11350b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
11360b57cec5SDimitry Andric       .addReg(SRegHi, RegState::Kill)
11370b57cec5SDimitry Andric       .addImm(Offset);
11380b57cec5SDimitry Andric   }
11390b57cec5SDimitry Andric 
11400b57cec5SDimitry Andric   // Convert into indexed form of the instruction:
11410b57cec5SDimitry Andric   //
11420b57cec5SDimitry Andric   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
11430b57cec5SDimitry Andric   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
11440b57cec5SDimitry Andric   unsigned OperandBase;
11450b57cec5SDimitry Andric 
11460b57cec5SDimitry Andric   if (noImmForm)
11470b57cec5SDimitry Andric     OperandBase = 1;
11480b57cec5SDimitry Andric   else if (OpC != TargetOpcode::INLINEASM &&
11490b57cec5SDimitry Andric            OpC != TargetOpcode::INLINEASM_BR) {
11500b57cec5SDimitry Andric     assert(ImmToIdxMap.count(OpC) &&
11510b57cec5SDimitry Andric            "No indexed form of load or store available!");
11520b57cec5SDimitry Andric     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
11530b57cec5SDimitry Andric     MI.setDesc(TII.get(NewOpcode));
11540b57cec5SDimitry Andric     OperandBase = 1;
11550b57cec5SDimitry Andric   } else {
11560b57cec5SDimitry Andric     OperandBase = OffsetOperandNo;
11570b57cec5SDimitry Andric   }
11580b57cec5SDimitry Andric 
11598bcb0991SDimitry Andric   Register StackReg = MI.getOperand(FIOperandNum).getReg();
11600b57cec5SDimitry Andric   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
11610b57cec5SDimitry Andric   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
11620b57cec5SDimitry Andric }
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
11650b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
11660b57cec5SDimitry Andric 
11670b57cec5SDimitry Andric   if (!TM.isPPC64())
11680b57cec5SDimitry Andric     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
11690b57cec5SDimitry Andric   else
11700b57cec5SDimitry Andric     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
11710b57cec5SDimitry Andric }
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
11740b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
11750b57cec5SDimitry Andric   if (!hasBasePointer(MF))
11760b57cec5SDimitry Andric     return getFrameRegister(MF);
11770b57cec5SDimitry Andric 
11780b57cec5SDimitry Andric   if (TM.isPPC64())
11790b57cec5SDimitry Andric     return PPC::X30;
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
11820b57cec5SDimitry Andric     return PPC::R29;
11830b57cec5SDimitry Andric 
11840b57cec5SDimitry Andric   return PPC::R30;
11850b57cec5SDimitry Andric }
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
11880b57cec5SDimitry Andric   if (!EnableBasePointer)
11890b57cec5SDimitry Andric     return false;
11900b57cec5SDimitry Andric   if (AlwaysBasePointer)
11910b57cec5SDimitry Andric     return true;
11920b57cec5SDimitry Andric 
11930b57cec5SDimitry Andric   // If we need to realign the stack, then the stack pointer can no longer
11940b57cec5SDimitry Andric   // serve as an offset into the caller's stack space. As a result, we need a
11950b57cec5SDimitry Andric   // base pointer.
11960b57cec5SDimitry Andric   return needsStackRealignment(MF);
11970b57cec5SDimitry Andric }
11980b57cec5SDimitry Andric 
11990b57cec5SDimitry Andric /// Returns true if the instruction's frame index
12000b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
12010b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
12020b57cec5SDimitry Andric /// references it should create new base registers for.
12030b57cec5SDimitry Andric bool PPCRegisterInfo::
12040b57cec5SDimitry Andric needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
12050b57cec5SDimitry Andric   assert(Offset < 0 && "Local offset must be negative");
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
12080b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
12090b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
12100b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
12110b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
12120b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores that have
12150b57cec5SDimitry Andric   // an r+i form. Return false for everything else.
12160b57cec5SDimitry Andric   unsigned OpC = MI->getOpcode();
12170b57cec5SDimitry Andric   if (!ImmToIdxMap.count(OpC))
12180b57cec5SDimitry Andric     return false;
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric   // Don't generate a new virtual base register just to add zero to it.
12210b57cec5SDimitry Andric   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
12220b57cec5SDimitry Andric       MI->getOperand(2).getImm() == 0)
12230b57cec5SDimitry Andric     return false;
12240b57cec5SDimitry Andric 
12250b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI->getParent();
12260b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
12270b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
12280b57cec5SDimitry Andric   unsigned StackEst = TFI->determineFrameLayout(MF, true);
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric   // If we likely don't need a stack frame, then we probably don't need a
12310b57cec5SDimitry Andric   // virtual base register either.
12320b57cec5SDimitry Andric   if (!StackEst)
12330b57cec5SDimitry Andric     return false;
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
12360b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
12370b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
12380b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
12390b57cec5SDimitry Andric   Offset += StackEst;
12400b57cec5SDimitry Andric 
12410b57cec5SDimitry Andric   // The frame pointer will point to the end of the stack, so estimate the
12420b57cec5SDimitry Andric   // offset as the difference between the object offset and the FP location.
12430b57cec5SDimitry Andric   return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
12440b57cec5SDimitry Andric }
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to
12470b57cec5SDimitry Andric /// be a pointer to FrameIdx at the beginning of the basic block.
12480b57cec5SDimitry Andric void PPCRegisterInfo::
12490b57cec5SDimitry Andric materializeFrameBaseRegister(MachineBasicBlock *MBB,
12500b57cec5SDimitry Andric                              unsigned BaseReg, int FrameIdx,
12510b57cec5SDimitry Andric                              int64_t Offset) const {
12520b57cec5SDimitry Andric   unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
12530b57cec5SDimitry Andric 
12540b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
12550b57cec5SDimitry Andric   DebugLoc DL;                  // Defaults to "unknown"
12560b57cec5SDimitry Andric   if (Ins != MBB->end())
12570b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
12580b57cec5SDimitry Andric 
12590b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
12600b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
12610b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
12620b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII.get(ADDriOpc);
12630b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12640b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
12650b57cec5SDimitry Andric 
12660b57cec5SDimitry Andric   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
12670b57cec5SDimitry Andric     .addFrameIndex(FrameIdx).addImm(Offset);
12680b57cec5SDimitry Andric }
12690b57cec5SDimitry Andric 
12700b57cec5SDimitry Andric void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
12710b57cec5SDimitry Andric                                         int64_t Offset) const {
12720b57cec5SDimitry Andric   unsigned FIOperandNum = 0;
12730b57cec5SDimitry Andric   while (!MI.getOperand(FIOperandNum).isFI()) {
12740b57cec5SDimitry Andric     ++FIOperandNum;
12750b57cec5SDimitry Andric     assert(FIOperandNum < MI.getNumOperands() &&
12760b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
12770b57cec5SDimitry Andric   }
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
12800b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
12810b57cec5SDimitry Andric   Offset += MI.getOperand(OffsetOperandNo).getImm();
12820b57cec5SDimitry Andric   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
12850b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
12860b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
12870b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
12880b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
12890b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
12900b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg,
12910b57cec5SDimitry Andric                         TII.getRegClass(MCID, FIOperandNum, this, MF));
12920b57cec5SDimitry Andric }
12930b57cec5SDimitry Andric 
12940b57cec5SDimitry Andric bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
12950b57cec5SDimitry Andric                                          unsigned BaseReg,
12960b57cec5SDimitry Andric                                          int64_t Offset) const {
12970b57cec5SDimitry Andric   unsigned FIOperandNum = 0;
12980b57cec5SDimitry Andric   while (!MI->getOperand(FIOperandNum).isFI()) {
12990b57cec5SDimitry Andric     ++FIOperandNum;
13000b57cec5SDimitry Andric     assert(FIOperandNum < MI->getNumOperands() &&
13010b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
13020b57cec5SDimitry Andric   }
13030b57cec5SDimitry Andric 
13040b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
13050b57cec5SDimitry Andric   Offset += MI->getOperand(OffsetOperandNo).getImm();
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
13080b57cec5SDimitry Andric          MI->getOpcode() == TargetOpcode::STACKMAP ||
13090b57cec5SDimitry Andric          MI->getOpcode() == TargetOpcode::PATCHPOINT ||
13100b57cec5SDimitry Andric          (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
13110b57cec5SDimitry Andric }
1312