xref: /freebsd/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
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;
136fe6060f1SDimitry Andric 
137fe6060f1SDimitry Andric   // Power10
138349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLBZ]   = PPC::LBZX; ImmToIdxMap[PPC::PLBZ8]   = PPC::LBZX8;
139349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLHZ]   = PPC::LHZX; ImmToIdxMap[PPC::PLHZ8]   = PPC::LHZX8;
140349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLHA]   = PPC::LHAX; ImmToIdxMap[PPC::PLHA8]   = PPC::LHAX8;
141349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLWZ]   = PPC::LWZX; ImmToIdxMap[PPC::PLWZ8]   = PPC::LWZX8;
142349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLWA]   = PPC::LWAX; ImmToIdxMap[PPC::PLWA8]   = PPC::LWAX;
143349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLD]    = PPC::LDX;  ImmToIdxMap[PPC::PSTD]   = PPC::STDX;
144349cc55cSDimitry Andric 
145349cc55cSDimitry Andric   ImmToIdxMap[PPC::PSTB]   = PPC::STBX; ImmToIdxMap[PPC::PSTB8]   = PPC::STBX8;
146349cc55cSDimitry Andric   ImmToIdxMap[PPC::PSTH]   = PPC::STHX; ImmToIdxMap[PPC::PSTH8]   = PPC::STHX8;
147349cc55cSDimitry Andric   ImmToIdxMap[PPC::PSTW]   = PPC::STWX; ImmToIdxMap[PPC::PSTW8]   = PPC::STWX8;
148349cc55cSDimitry Andric 
149349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLFS]   = PPC::LFSX; ImmToIdxMap[PPC::PSTFS]   = PPC::STFSX;
150349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLFD]   = PPC::LFDX; ImmToIdxMap[PPC::PSTFD]   = PPC::STFDX;
151349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLXSSP] = PPC::LXSSPX; ImmToIdxMap[PPC::PSTXSSP] = PPC::STXSSPX;
152349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLXSD]  = PPC::LXSDX; ImmToIdxMap[PPC::PSTXSD]  = PPC::STXSDX;
153349cc55cSDimitry Andric   ImmToIdxMap[PPC::PLXV]   = PPC::LXVX; ImmToIdxMap[PPC::PSTXV]  = PPC::STXVX;
154349cc55cSDimitry Andric 
155fe6060f1SDimitry Andric   ImmToIdxMap[PPC::LXVP]   = PPC::LXVPX;
156fe6060f1SDimitry Andric   ImmToIdxMap[PPC::STXVP]  = PPC::STXVPX;
157fe6060f1SDimitry Andric   ImmToIdxMap[PPC::PLXVP]  = PPC::LXVPX;
158fe6060f1SDimitry Andric   ImmToIdxMap[PPC::PSTXVP] = PPC::STXVPX;
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric /// getPointerRegClass - Return the register class to use to hold pointers.
1620b57cec5SDimitry Andric /// This is used for addressing modes.
1630b57cec5SDimitry Andric const TargetRegisterClass *
1640b57cec5SDimitry Andric PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
1650b57cec5SDimitry Andric                                                                        const {
1660b57cec5SDimitry Andric   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
1670b57cec5SDimitry Andric   // when it checks for ZERO folding.
1680b57cec5SDimitry Andric   if (Kind == 1) {
1690b57cec5SDimitry Andric     if (TM.isPPC64())
1700b57cec5SDimitry Andric       return &PPC::G8RC_NOX0RegClass;
1710b57cec5SDimitry Andric     return &PPC::GPRC_NOR0RegClass;
1720b57cec5SDimitry Andric   }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   if (TM.isPPC64())
1750b57cec5SDimitry Andric     return &PPC::G8RCRegClass;
1760b57cec5SDimitry Andric   return &PPC::GPRCRegClass;
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric const MCPhysReg*
1800b57cec5SDimitry Andric PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
1810b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
1820b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) {
1835ffd83dbSDimitry Andric     if (!TM.isPPC64() && Subtarget.isAIXABI())
1845ffd83dbSDimitry Andric       report_fatal_error("AnyReg unimplemented on 32-bit AIX.");
185fe6060f1SDimitry Andric     if (Subtarget.hasVSX()) {
186fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
187fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_VSX_SaveList;
1880b57cec5SDimitry Andric       return CSR_64_AllRegs_VSX_SaveList;
189fe6060f1SDimitry Andric     }
190fe6060f1SDimitry Andric     if (Subtarget.hasAltivec()) {
191fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
192fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_Altivec_SaveList;
1930b57cec5SDimitry Andric       return CSR_64_AllRegs_Altivec_SaveList;
194fe6060f1SDimitry Andric     }
1950b57cec5SDimitry Andric     return CSR_64_AllRegs_SaveList;
1960b57cec5SDimitry Andric   }
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   // On PPC64, we might need to save r2 (but only if it is not reserved).
1995ffd83dbSDimitry Andric   // We do not need to treat R2 as callee-saved when using PC-Relative calls
2005ffd83dbSDimitry Andric   // because any direct uses of R2 will cause it to be reserved. If the function
2015ffd83dbSDimitry Andric   // is a leaf or the only uses of R2 are implicit uses for calls, the calls
2025ffd83dbSDimitry Andric   // will use the @notoc relocation which will cause this function to set the
2035ffd83dbSDimitry Andric   // st_other bit to 1, thereby communicating to its caller that it arbitrarily
2045ffd83dbSDimitry Andric   // clobbers the TOC.
2055ffd83dbSDimitry Andric   bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) &&
2065ffd83dbSDimitry Andric                 !Subtarget.isUsingPCRelativeCalls();
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric   // Cold calling convention CSRs.
2090b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::Cold) {
2105ffd83dbSDimitry Andric     if (Subtarget.isAIXABI())
2115ffd83dbSDimitry Andric       report_fatal_error("Cold calling unimplemented on AIX.");
2120b57cec5SDimitry Andric     if (TM.isPPC64()) {
2130b57cec5SDimitry Andric       if (Subtarget.hasAltivec())
2140b57cec5SDimitry Andric         return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
2150b57cec5SDimitry Andric                       : CSR_SVR64_ColdCC_Altivec_SaveList;
2160b57cec5SDimitry Andric       return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
2170b57cec5SDimitry Andric                     : CSR_SVR64_ColdCC_SaveList;
2180b57cec5SDimitry Andric     }
2190b57cec5SDimitry Andric     // 32-bit targets.
2200b57cec5SDimitry Andric     if (Subtarget.hasAltivec())
2210b57cec5SDimitry Andric       return CSR_SVR32_ColdCC_Altivec_SaveList;
2220b57cec5SDimitry Andric     else if (Subtarget.hasSPE())
2230b57cec5SDimitry Andric       return CSR_SVR32_ColdCC_SPE_SaveList;
2240b57cec5SDimitry Andric     return CSR_SVR32_ColdCC_SaveList;
2250b57cec5SDimitry Andric   }
2260b57cec5SDimitry Andric   // Standard calling convention CSRs.
2270b57cec5SDimitry Andric   if (TM.isPPC64()) {
228fe6060f1SDimitry Andric     if (Subtarget.hasAltivec() &&
229fe6060f1SDimitry Andric         (!Subtarget.isAIXABI() || TM.getAIXExtendedAltivecABI())) {
2305ffd83dbSDimitry Andric       return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList
2315ffd83dbSDimitry Andric                     : CSR_PPC64_Altivec_SaveList;
232fe6060f1SDimitry Andric     }
2335ffd83dbSDimitry Andric     return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList;
2340b57cec5SDimitry Andric   }
2350b57cec5SDimitry Andric   // 32-bit targets.
236e8d8bef9SDimitry Andric   if (Subtarget.isAIXABI()) {
237e8d8bef9SDimitry Andric     if (Subtarget.hasAltivec())
238fe6060f1SDimitry Andric       return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_Altivec_SaveList
239fe6060f1SDimitry Andric                                            : CSR_AIX32_SaveList;
2405ffd83dbSDimitry Andric     return CSR_AIX32_SaveList;
241e8d8bef9SDimitry Andric   }
2420b57cec5SDimitry Andric   if (Subtarget.hasAltivec())
2430b57cec5SDimitry Andric     return CSR_SVR432_Altivec_SaveList;
2440b57cec5SDimitry Andric   else if (Subtarget.hasSPE())
2450b57cec5SDimitry Andric     return CSR_SVR432_SPE_SaveList;
2460b57cec5SDimitry Andric   return CSR_SVR432_SaveList;
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric const uint32_t *
2500b57cec5SDimitry Andric PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
2510b57cec5SDimitry Andric                                       CallingConv::ID CC) const {
2520b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
2530b57cec5SDimitry Andric   if (CC == CallingConv::AnyReg) {
254fe6060f1SDimitry Andric     if (Subtarget.hasVSX()) {
255fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
256fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_VSX_RegMask;
2570b57cec5SDimitry Andric       return CSR_64_AllRegs_VSX_RegMask;
258fe6060f1SDimitry Andric     }
259fe6060f1SDimitry Andric     if (Subtarget.hasAltivec()) {
260fe6060f1SDimitry Andric       if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
261fe6060f1SDimitry Andric         return CSR_64_AllRegs_AIX_Dflt_Altivec_RegMask;
2620b57cec5SDimitry Andric       return CSR_64_AllRegs_Altivec_RegMask;
263fe6060f1SDimitry Andric     }
2640b57cec5SDimitry Andric     return CSR_64_AllRegs_RegMask;
2650b57cec5SDimitry Andric   }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   if (Subtarget.isAIXABI()) {
268fe6060f1SDimitry Andric     return TM.isPPC64()
269fe6060f1SDimitry Andric                ? ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
270fe6060f1SDimitry Andric                       ? CSR_PPC64_Altivec_RegMask
271e8d8bef9SDimitry Andric                       : CSR_PPC64_RegMask)
272fe6060f1SDimitry Andric                : ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
273fe6060f1SDimitry Andric                       ? CSR_AIX32_Altivec_RegMask
274e8d8bef9SDimitry Andric                       : CSR_AIX32_RegMask);
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   if (CC == CallingConv::Cold) {
2780b57cec5SDimitry Andric     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
2790b57cec5SDimitry Andric                                                   : CSR_SVR64_ColdCC_RegMask)
2800b57cec5SDimitry Andric                         : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
2810b57cec5SDimitry Andric                                                   : (Subtarget.hasSPE()
2820b57cec5SDimitry Andric                                                   ? CSR_SVR32_ColdCC_SPE_RegMask
2830b57cec5SDimitry Andric                                                   : CSR_SVR32_ColdCC_RegMask));
2840b57cec5SDimitry Andric   }
2850b57cec5SDimitry Andric 
2865ffd83dbSDimitry Andric   return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask
2875ffd83dbSDimitry Andric                                                 : CSR_PPC64_RegMask)
2885ffd83dbSDimitry Andric                       : (Subtarget.hasAltivec()
2895ffd83dbSDimitry Andric                              ? CSR_SVR432_Altivec_RegMask
2905ffd83dbSDimitry Andric                              : (Subtarget.hasSPE() ? CSR_SVR432_SPE_RegMask
2910b57cec5SDimitry Andric                                                    : CSR_SVR432_RegMask));
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric const uint32_t*
2950b57cec5SDimitry Andric PPCRegisterInfo::getNoPreservedMask() const {
2960b57cec5SDimitry Andric   return CSR_NoRegs_RegMask;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
3000b57cec5SDimitry Andric   for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
3010b57cec5SDimitry Andric     Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
3050b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
3060b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
3070b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   // The ZERO register is not really a register, but the representation of r0
3100b57cec5SDimitry Andric   // when used in instructions that treat r0 as the constant 0.
3110b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::ZERO);
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   // The FP register is also not really a register, but is the representation
3140b57cec5SDimitry Andric   // of the frame pointer register used by ISD::FRAMEADDR.
3150b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::FP);
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   // The BP register is also not really a register, but is the representation
3180b57cec5SDimitry Andric   // of the base pointer register used by setjmp.
3190b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::BP);
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   // The counter registers must be reserved so that counter-based loops can
3220b57cec5SDimitry Andric   // be correctly formed (and the mtctr instructions are not DCE'd).
3230b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::CTR);
3240b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::CTR8);
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::R1);
3270b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::LR);
3280b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::LR8);
3290b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::RM);
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   markSuperRegs(Reserved, PPC::VRSAVE);
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   // The SVR4 ABI reserves r2 and r13
3340b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI()) {
3350b57cec5SDimitry Andric     // We only reserve r2 if we need to use the TOC pointer. If we have no
3360b57cec5SDimitry Andric     // explicit uses of the TOC pointer (meaning we're a leaf function with
3370b57cec5SDimitry Andric     // no constant-pool loads, etc.) and we have no potential uses inside an
3380b57cec5SDimitry Andric     // inline asm block, then we can treat r2 has an ordinary callee-saved
3390b57cec5SDimitry Andric     // register.
3400b57cec5SDimitry Andric     const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
3410b57cec5SDimitry Andric     if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
3420b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R2);  // System-reserved register
3430b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
3440b57cec5SDimitry Andric   }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   // Always reserve r2 on AIX for now.
3470b57cec5SDimitry Andric   // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions.
3480b57cec5SDimitry Andric   if (Subtarget.isAIXABI())
3490b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R2);  // System-reserved register
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   // On PPC64, r13 is the thread pointer. Never allocate this register.
3520b57cec5SDimitry Andric   if (TM.isPPC64())
3530b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R13);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   if (TFI->needsFP(MF))
3560b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R31);
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric   bool IsPositionIndependent = TM.isPositionIndependent();
3590b57cec5SDimitry Andric   if (hasBasePointer(MF)) {
3608bcb0991SDimitry Andric     if (Subtarget.is32BitELFABI() && IsPositionIndependent)
3610b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R29);
3620b57cec5SDimitry Andric     else
3630b57cec5SDimitry Andric       markSuperRegs(Reserved, PPC::R30);
3640b57cec5SDimitry Andric   }
3650b57cec5SDimitry Andric 
3668bcb0991SDimitry Andric   if (Subtarget.is32BitELFABI() && IsPositionIndependent)
3670b57cec5SDimitry Andric     markSuperRegs(Reserved, PPC::R30);
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   // Reserve Altivec registers when Altivec is unavailable.
3700b57cec5SDimitry Andric   if (!Subtarget.hasAltivec())
3710b57cec5SDimitry Andric     for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
3720b57cec5SDimitry Andric          IE = PPC::VRRCRegClass.end(); I != IE; ++I)
3730b57cec5SDimitry Andric       markSuperRegs(Reserved, *I);
3740b57cec5SDimitry Andric 
375fe6060f1SDimitry Andric   if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
376fe6060f1SDimitry Andric       !TM.getAIXExtendedAltivecABI()) {
377fe6060f1SDimitry Andric     //  In the AIX default Altivec ABI, vector registers VR20-VR31 are reserved
378fe6060f1SDimitry Andric     //  and cannot be used.
379fe6060f1SDimitry Andric     for (auto Reg : CSR_Altivec_SaveList) {
380fe6060f1SDimitry Andric       if (Reg == 0)
381fe6060f1SDimitry Andric         break;
382fe6060f1SDimitry Andric       markSuperRegs(Reserved, Reg);
383fe6060f1SDimitry Andric       for (MCRegAliasIterator AS(Reg, this, true); AS.isValid(); ++AS) {
384fe6060f1SDimitry Andric         Reserved.set(*AS);
385fe6060f1SDimitry Andric       }
386fe6060f1SDimitry Andric     }
387fe6060f1SDimitry Andric   }
388fe6060f1SDimitry Andric 
3890b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
3900b57cec5SDimitry Andric   return Reserved;
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric 
393*04eeddc0SDimitry Andric bool PPCRegisterInfo::isAsmClobberable(const MachineFunction &MF,
394*04eeddc0SDimitry Andric                                        MCRegister PhysReg) const {
395*04eeddc0SDimitry Andric   // We cannot use getReservedRegs() to find the registers that are not asm
396*04eeddc0SDimitry Andric   // clobberable because there are some reserved registers which can be
397*04eeddc0SDimitry Andric   // clobbered by inline asm. For example, when LR is clobbered, the register is
398*04eeddc0SDimitry Andric   // saved and restored. We will hardcode the registers that are not asm
399*04eeddc0SDimitry Andric   // cloberable in this function.
400*04eeddc0SDimitry Andric 
401*04eeddc0SDimitry Andric   // The stack pointer (R1/X1) is not clobberable by inline asm
402*04eeddc0SDimitry Andric   return PhysReg != PPC::R1 && PhysReg != PPC::X1;
403*04eeddc0SDimitry Andric }
404*04eeddc0SDimitry Andric 
4050b57cec5SDimitry Andric bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const {
4060b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
4070b57cec5SDimitry Andric   const PPCInstrInfo *InstrInfo =  Subtarget.getInstrInfo();
4080b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4090b57cec5SDimitry Andric   const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
4100b57cec5SDimitry Andric 
411fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "requiresFrameIndexScavenging for " << MF.getName()
412fe6060f1SDimitry Andric                     << ".\n");
4130b57cec5SDimitry Andric   // If the callee saved info is invalid we have to default to true for safety.
414fe6060f1SDimitry Andric   if (!MFI.isCalleeSavedInfoValid()) {
415fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "TRUE - Invalid callee saved info.\n");
4160b57cec5SDimitry Andric     return true;
417fe6060f1SDimitry Andric   }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   // We will require the use of X-Forms because the frame is larger than what
4200b57cec5SDimitry Andric   // can be represented in signed 16 bits that fit in the immediate of a D-Form.
4210b57cec5SDimitry Andric   // If we need an X-Form then we need a register to store the address offset.
4220b57cec5SDimitry Andric   unsigned FrameSize = MFI.getStackSize();
4230b57cec5SDimitry Andric   // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
424fe6060f1SDimitry Andric   if (FrameSize & ~0x7FFF) {
425fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "TRUE - Frame size is too large for D-Form.\n");
4260b57cec5SDimitry Andric     return true;
427fe6060f1SDimitry Andric   }
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric   // The callee saved info is valid so it can be traversed.
4300b57cec5SDimitry Andric   // Checking for registers that need saving that do not have load or store
4310b57cec5SDimitry Andric   // forms where the address offset is an immediate.
4320b57cec5SDimitry Andric   for (unsigned i = 0; i < Info.size(); i++) {
433fe6060f1SDimitry Andric     // If the spill is to a register no scavenging is required.
434fe6060f1SDimitry Andric     if (Info[i].isSpilledToReg())
435fe6060f1SDimitry Andric       continue;
436fe6060f1SDimitry Andric 
4370b57cec5SDimitry Andric     int FrIdx = Info[i].getFrameIdx();
438*04eeddc0SDimitry Andric     Register Reg = Info[i].getReg();
4390b57cec5SDimitry Andric 
4405ffd83dbSDimitry Andric     const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg);
4415ffd83dbSDimitry Andric     unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC);
4420b57cec5SDimitry Andric     if (!MFI.isFixedObjectIndex(FrIdx)) {
4430b57cec5SDimitry Andric       // This is not a fixed object. If it requires alignment then we may still
4440b57cec5SDimitry Andric       // need to use the XForm.
445fe6060f1SDimitry Andric       if (offsetMinAlignForOpcode(Opcode) > 1) {
446fe6060f1SDimitry Andric         LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
447fe6060f1SDimitry Andric                           << " for register " << printReg(Reg, this) << ".\n");
448fe6060f1SDimitry Andric         LLVM_DEBUG(dbgs() << "TRUE - Not fixed frame object that requires "
449fe6060f1SDimitry Andric                           << "alignment.\n");
4500b57cec5SDimitry Andric         return true;
4510b57cec5SDimitry Andric       }
452fe6060f1SDimitry Andric     }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric     // This is eiher:
4550b57cec5SDimitry Andric     // 1) A fixed frame index object which we know are aligned so
4560b57cec5SDimitry Andric     // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
457480093f4SDimitry Andric     // need to consider the alignment here.
4580b57cec5SDimitry Andric     // 2) A not fixed object but in that case we now know that the min required
4590b57cec5SDimitry Andric     // alignment is no more than 1 based on the previous check.
460fe6060f1SDimitry Andric     if (InstrInfo->isXFormMemOp(Opcode)) {
461fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
462fe6060f1SDimitry Andric                         << " for register " << printReg(Reg, this) << ".\n");
463fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "TRUE - Memory operand is X-Form.\n");
4640b57cec5SDimitry Andric       return true;
4650b57cec5SDimitry Andric     }
466fe6060f1SDimitry Andric   }
467fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "FALSE - Scavenging is not required.\n");
4680b57cec5SDimitry Andric   return false;
4690b57cec5SDimitry Andric }
4700b57cec5SDimitry Andric 
471fe6060f1SDimitry Andric bool PPCRegisterInfo::requiresVirtualBaseRegisters(
472fe6060f1SDimitry Andric     const MachineFunction &MF) const {
473fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
474fe6060f1SDimitry Andric   // Do not use virtual base registers when ROP protection is turned on.
475fe6060f1SDimitry Andric   // Virtual base registers break the layout of the local variable space and may
476fe6060f1SDimitry Andric   // push the ROP Hash location past the 512 byte range of the ROP store
477fe6060f1SDimitry Andric   // instruction.
478fe6060f1SDimitry Andric   return !Subtarget.hasROPProtect();
479fe6060f1SDimitry Andric }
480fe6060f1SDimitry Andric 
4815ffd83dbSDimitry Andric bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg,
4820b57cec5SDimitry Andric                                                const MachineFunction &MF) const {
4838bcb0991SDimitry Andric   assert(Register::isPhysicalRegister(PhysReg));
4840b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
4850b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4860b57cec5SDimitry Andric 
487fe6060f1SDimitry Andric   if (!Subtarget.is64BitELFABI() && !Subtarget.isAIXABI())
4880b57cec5SDimitry Andric     return false;
489fe6060f1SDimitry Andric   if (PhysReg == Subtarget.getTOCPointerRegister())
490fe6060f1SDimitry Andric     // X2/R2 is guaranteed to be preserved within a function if it is reserved.
4910b57cec5SDimitry Andric     // The reason it's reserved is that it's the TOC pointer (and the function
4920b57cec5SDimitry Andric     // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
4930b57cec5SDimitry Andric     // with no TOC access), we can't claim that it is preserved.
494fe6060f1SDimitry Andric     return (getReservedRegs(MF).test(PhysReg));
495fe6060f1SDimitry Andric   if (StackPtrConst && PhysReg == Subtarget.getStackPointerRegister() &&
496fe6060f1SDimitry Andric       !MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment())
4970b57cec5SDimitry Andric     // The value of the stack pointer does not change within a function after
4980b57cec5SDimitry Andric     // the prologue and before the epilogue if there are no dynamic allocations
499fe6060f1SDimitry Andric     // and no inline asm which clobbers X1/R1.
5000b57cec5SDimitry Andric     return true;
5010b57cec5SDimitry Andric   return false;
5020b57cec5SDimitry Andric }
5030b57cec5SDimitry Andric 
504fe6060f1SDimitry Andric bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
505fe6060f1SDimitry Andric                                             ArrayRef<MCPhysReg> Order,
506fe6060f1SDimitry Andric                                             SmallVectorImpl<MCPhysReg> &Hints,
507fe6060f1SDimitry Andric                                             const MachineFunction &MF,
508fe6060f1SDimitry Andric                                             const VirtRegMap *VRM,
509fe6060f1SDimitry Andric                                             const LiveRegMatrix *Matrix) const {
510fe6060f1SDimitry Andric   const MachineRegisterInfo *MRI = &MF.getRegInfo();
511fe6060f1SDimitry Andric 
512fe6060f1SDimitry Andric   // Call the base implementation first to set any hints based on the usual
513fe6060f1SDimitry Andric   // heuristics and decide what the return value should be. We want to return
514fe6060f1SDimitry Andric   // the same value returned by the base implementation. If the base
515fe6060f1SDimitry Andric   // implementation decides to return true and force the allocation then we
516fe6060f1SDimitry Andric   // will leave it as such. On the other hand if the base implementation
517fe6060f1SDimitry Andric   // decides to return false the following code will not force the allocation
518fe6060f1SDimitry Andric   // as we are just looking to provide a hint.
519fe6060f1SDimitry Andric   bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
520fe6060f1SDimitry Andric       VirtReg, Order, Hints, MF, VRM, Matrix);
521fe6060f1SDimitry Andric   // We are interested in instructions that copy values to ACC/UACC.
522fe6060f1SDimitry Andric   // The copy into UACC will be simply a COPY to a subreg so we
523fe6060f1SDimitry Andric   // want to allocate the corresponding physical subreg for the source.
524fe6060f1SDimitry Andric   // The copy into ACC will be a BUILD_UACC so we want to allocate
525fe6060f1SDimitry Andric   // the same number UACC for the source.
526fe6060f1SDimitry Andric   for (MachineInstr &Use : MRI->reg_nodbg_instructions(VirtReg)) {
527fe6060f1SDimitry Andric     const MachineOperand *ResultOp = nullptr;
528fe6060f1SDimitry Andric     Register ResultReg;
529fe6060f1SDimitry Andric     switch (Use.getOpcode()) {
530fe6060f1SDimitry Andric     case TargetOpcode::COPY: {
531fe6060f1SDimitry Andric       ResultOp = &Use.getOperand(0);
532fe6060f1SDimitry Andric       ResultReg = ResultOp->getReg();
533fe6060f1SDimitry Andric       if (Register::isVirtualRegister(ResultReg) &&
534fe6060f1SDimitry Andric           MRI->getRegClass(ResultReg)->contains(PPC::UACC0) &&
535fe6060f1SDimitry Andric           VRM->hasPhys(ResultReg)) {
536fe6060f1SDimitry Andric         Register UACCPhys = VRM->getPhys(ResultReg);
537fe6060f1SDimitry Andric         Register HintReg = getSubReg(UACCPhys, ResultOp->getSubReg());
538349cc55cSDimitry Andric         // Ensure that the hint is a VSRp register.
539349cc55cSDimitry Andric         if (HintReg >= PPC::VSRp0 && HintReg <= PPC::VSRp31)
540fe6060f1SDimitry Andric           Hints.push_back(HintReg);
541fe6060f1SDimitry Andric       }
542fe6060f1SDimitry Andric       break;
543fe6060f1SDimitry Andric     }
544fe6060f1SDimitry Andric     case PPC::BUILD_UACC: {
545fe6060f1SDimitry Andric       ResultOp = &Use.getOperand(0);
546fe6060f1SDimitry Andric       ResultReg = ResultOp->getReg();
547fe6060f1SDimitry Andric       if (MRI->getRegClass(ResultReg)->contains(PPC::ACC0) &&
548fe6060f1SDimitry Andric           VRM->hasPhys(ResultReg)) {
549fe6060f1SDimitry Andric         Register ACCPhys = VRM->getPhys(ResultReg);
550fe6060f1SDimitry Andric         assert((ACCPhys >= PPC::ACC0 && ACCPhys <= PPC::ACC7) &&
551fe6060f1SDimitry Andric                "Expecting an ACC register for BUILD_UACC.");
552fe6060f1SDimitry Andric         Register HintReg = PPC::UACC0 + (ACCPhys - PPC::ACC0);
553fe6060f1SDimitry Andric         Hints.push_back(HintReg);
554fe6060f1SDimitry Andric       }
555fe6060f1SDimitry Andric       break;
556fe6060f1SDimitry Andric     }
557fe6060f1SDimitry Andric     }
558fe6060f1SDimitry Andric   }
559fe6060f1SDimitry Andric   return BaseImplRetVal;
560fe6060f1SDimitry Andric }
561fe6060f1SDimitry Andric 
5620b57cec5SDimitry Andric unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
5630b57cec5SDimitry Andric                                               MachineFunction &MF) const {
5640b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
5650b57cec5SDimitry Andric   const unsigned DefaultSafety = 1;
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric   switch (RC->getID()) {
5680b57cec5SDimitry Andric   default:
5690b57cec5SDimitry Andric     return 0;
5700b57cec5SDimitry Andric   case PPC::G8RC_NOX0RegClassID:
5710b57cec5SDimitry Andric   case PPC::GPRC_NOR0RegClassID:
5720b57cec5SDimitry Andric   case PPC::SPERCRegClassID:
5730b57cec5SDimitry Andric   case PPC::G8RCRegClassID:
5740b57cec5SDimitry Andric   case PPC::GPRCRegClassID: {
5750b57cec5SDimitry Andric     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
5760b57cec5SDimitry Andric     return 32 - FP - DefaultSafety;
5770b57cec5SDimitry Andric   }
5780b57cec5SDimitry Andric   case PPC::F4RCRegClassID:
579fe6060f1SDimitry Andric   case PPC::F8RCRegClassID:
5800b57cec5SDimitry Andric   case PPC::VSLRCRegClassID:
5810b57cec5SDimitry Andric     return 32 - DefaultSafety;
582fe6060f1SDimitry Andric   case PPC::VFRCRegClassID:
583fe6060f1SDimitry Andric   case PPC::VRRCRegClassID: {
584fe6060f1SDimitry Andric     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
585fe6060f1SDimitry Andric     // Vector registers VR20-VR31 are reserved and cannot be used in the default
586fe6060f1SDimitry Andric     // Altivec ABI on AIX.
587fe6060f1SDimitry Andric     if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
588fe6060f1SDimitry Andric       return 20 - DefaultSafety;
589fe6060f1SDimitry Andric   }
590fe6060f1SDimitry Andric     return 32 - DefaultSafety;
5910b57cec5SDimitry Andric   case PPC::VSFRCRegClassID:
5920b57cec5SDimitry Andric   case PPC::VSSRCRegClassID:
593fe6060f1SDimitry Andric   case PPC::VSRCRegClassID: {
594fe6060f1SDimitry Andric     const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
595fe6060f1SDimitry Andric     if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
596fe6060f1SDimitry Andric       // Vector registers VR20-VR31 are reserved and cannot be used in the
597fe6060f1SDimitry Andric       // default Altivec ABI on AIX.
598fe6060f1SDimitry Andric       return 52 - DefaultSafety;
599fe6060f1SDimitry Andric   }
6000b57cec5SDimitry Andric     return 64 - DefaultSafety;
6010b57cec5SDimitry Andric   case PPC::CRRCRegClassID:
6020b57cec5SDimitry Andric     return 8 - DefaultSafety;
6030b57cec5SDimitry Andric   }
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric const TargetRegisterClass *
6070b57cec5SDimitry Andric PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
6080b57cec5SDimitry Andric                                            const MachineFunction &MF) const {
6090b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
610fe6060f1SDimitry Andric   const auto *DefaultSuperclass =
611fe6060f1SDimitry Andric       TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
6120b57cec5SDimitry Andric   if (Subtarget.hasVSX()) {
6130b57cec5SDimitry Andric     // With VSX, we can inflate various sub-register classes to the full VSX
6140b57cec5SDimitry Andric     // register set.
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric     // For Power9 we allow the user to enable GPR to vector spills.
6170b57cec5SDimitry Andric     // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
6180b57cec5SDimitry Andric     // support to spill GPRC.
619fe6060f1SDimitry Andric     if (TM.isELFv2ABI() || Subtarget.isAIXABI()) {
6200b57cec5SDimitry Andric       if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
6210b57cec5SDimitry Andric           RC == &PPC::G8RCRegClass) {
6220b57cec5SDimitry Andric         InflateGP8RC++;
6230b57cec5SDimitry Andric         return &PPC::SPILLTOVSRRCRegClass;
6240b57cec5SDimitry Andric       }
6250b57cec5SDimitry Andric       if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
6260b57cec5SDimitry Andric         InflateGPRC++;
6270b57cec5SDimitry Andric     }
628fe6060f1SDimitry Andric 
629fe6060f1SDimitry Andric     for (const auto *I = RC->getSuperClasses(); *I; ++I) {
630fe6060f1SDimitry Andric       if (getRegSizeInBits(**I) != getRegSizeInBits(*RC))
631fe6060f1SDimitry Andric         continue;
632fe6060f1SDimitry Andric 
633fe6060f1SDimitry Andric       switch ((*I)->getID()) {
634fe6060f1SDimitry Andric       case PPC::VSSRCRegClassID:
635fe6060f1SDimitry Andric         return Subtarget.hasP8Vector() ? *I : DefaultSuperclass;
636fe6060f1SDimitry Andric       case PPC::VSFRCRegClassID:
637fe6060f1SDimitry Andric       case PPC::VSRCRegClassID:
638fe6060f1SDimitry Andric         return *I;
639fe6060f1SDimitry Andric       case PPC::VSRpRCRegClassID:
640fe6060f1SDimitry Andric         return Subtarget.pairedVectorMemops() ? *I : DefaultSuperclass;
641fe6060f1SDimitry Andric       case PPC::ACCRCRegClassID:
642fe6060f1SDimitry Andric       case PPC::UACCRCRegClassID:
643fe6060f1SDimitry Andric         return Subtarget.hasMMA() ? *I : DefaultSuperclass;
644fe6060f1SDimitry Andric       }
645fe6060f1SDimitry Andric     }
6460b57cec5SDimitry Andric   }
6470b57cec5SDimitry Andric 
648fe6060f1SDimitry Andric   return DefaultSuperclass;
6490b57cec5SDimitry Andric }
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6520b57cec5SDimitry Andric // Stack Frame Processing methods
6530b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6540b57cec5SDimitry Andric 
6550b57cec5SDimitry Andric /// lowerDynamicAlloc - Generate the code for allocating an object in the
6560b57cec5SDimitry Andric /// current frame.  The sequence of code will be in the general form
6570b57cec5SDimitry Andric ///
6580b57cec5SDimitry Andric ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
6590b57cec5SDimitry Andric ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
6600b57cec5SDimitry Andric ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
6610b57cec5SDimitry Andric ///
6620b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
6630b57cec5SDimitry Andric   // Get the instruction.
6640b57cec5SDimitry Andric   MachineInstr &MI = *II;
6650b57cec5SDimitry Andric   // Get the instruction's basic block.
6660b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
6670b57cec5SDimitry Andric   // Get the basic block's function.
6680b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
6690b57cec5SDimitry Andric   // Get the frame info.
6700b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
6710b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
6720b57cec5SDimitry Andric   // Get the instruction info.
6730b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
6740b57cec5SDimitry Andric   // Determine whether 64-bit pointers are used.
6750b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
6760b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   // Get the maximum call stack size.
6790b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
6805ffd83dbSDimitry Andric   Align MaxAlign = MFI.getMaxAlign();
6815ffd83dbSDimitry Andric   assert(isAligned(MaxAlign, maxCallFrameSize) &&
6820b57cec5SDimitry Andric          "Maximum call-frame size not sufficiently aligned");
6835ffd83dbSDimitry Andric   (void)MaxAlign;
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
6860b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
6878bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
6880b57cec5SDimitry Andric   bool KillNegSizeReg = MI.getOperand(1).isKill();
6898bcb0991SDimitry Andric   Register NegSizeReg = MI.getOperand(1).getReg();
6900b57cec5SDimitry Andric 
6915ffd83dbSDimitry Andric   prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg);
6920b57cec5SDimitry Andric   // Grow the stack and update the stack pointer link, then determine the
6930b57cec5SDimitry Andric   // address of new allocated space.
6940b57cec5SDimitry Andric   if (LP64) {
6950b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
6960b57cec5SDimitry Andric         .addReg(Reg, RegState::Kill)
6970b57cec5SDimitry Andric         .addReg(PPC::X1)
6980b57cec5SDimitry Andric         .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
6990b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
7000b57cec5SDimitry Andric         .addReg(PPC::X1)
7010b57cec5SDimitry Andric         .addImm(maxCallFrameSize);
7020b57cec5SDimitry Andric   } else {
7030b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
7040b57cec5SDimitry Andric         .addReg(Reg, RegState::Kill)
7050b57cec5SDimitry Andric         .addReg(PPC::R1)
7060b57cec5SDimitry Andric         .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
7070b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
7080b57cec5SDimitry Andric         .addReg(PPC::R1)
7090b57cec5SDimitry Andric         .addImm(maxCallFrameSize);
7100b57cec5SDimitry Andric   }
7110b57cec5SDimitry Andric 
7120b57cec5SDimitry Andric   // Discard the DYNALLOC instruction.
7130b57cec5SDimitry Andric   MBB.erase(II);
7140b57cec5SDimitry Andric }
7150b57cec5SDimitry Andric 
7165ffd83dbSDimitry Andric /// To accomplish dynamic stack allocation, we have to calculate exact size
7175ffd83dbSDimitry Andric /// subtracted from the stack pointer according alignment information and get
7185ffd83dbSDimitry Andric /// previous frame pointer.
7195ffd83dbSDimitry Andric void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II,
7205ffd83dbSDimitry Andric                                            Register &NegSizeReg,
7215ffd83dbSDimitry Andric                                            bool &KillNegSizeReg,
7225ffd83dbSDimitry Andric                                            Register &FramePointer) const {
7235ffd83dbSDimitry Andric   // Get the instruction.
7245ffd83dbSDimitry Andric   MachineInstr &MI = *II;
7255ffd83dbSDimitry Andric   // Get the instruction's basic block.
7265ffd83dbSDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
7275ffd83dbSDimitry Andric   // Get the basic block's function.
7285ffd83dbSDimitry Andric   MachineFunction &MF = *MBB.getParent();
7295ffd83dbSDimitry Andric   // Get the frame info.
7305ffd83dbSDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
7315ffd83dbSDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
7325ffd83dbSDimitry Andric   // Get the instruction info.
7335ffd83dbSDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
7345ffd83dbSDimitry Andric   // Determine whether 64-bit pointers are used.
7355ffd83dbSDimitry Andric   bool LP64 = TM.isPPC64();
7365ffd83dbSDimitry Andric   DebugLoc dl = MI.getDebugLoc();
7375ffd83dbSDimitry Andric   // Get the total frame size.
7385ffd83dbSDimitry Andric   unsigned FrameSize = MFI.getStackSize();
7395ffd83dbSDimitry Andric 
7405ffd83dbSDimitry Andric   // Get stack alignments.
7415ffd83dbSDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
7425ffd83dbSDimitry Andric   Align TargetAlign = TFI->getStackAlign();
7435ffd83dbSDimitry Andric   Align MaxAlign = MFI.getMaxAlign();
7445ffd83dbSDimitry Andric 
7455ffd83dbSDimitry Andric   // Determine the previous frame's address.  If FrameSize can't be
7465ffd83dbSDimitry Andric   // represented as 16 bits or we need special alignment, then we load the
7475ffd83dbSDimitry Andric   // previous frame's address from 0(SP).  Why not do an addis of the hi?
7485ffd83dbSDimitry Andric   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
7495ffd83dbSDimitry Andric   // Constructing the constant and adding would take 3 instructions.
7505ffd83dbSDimitry Andric   // Fortunately, a frame greater than 32K is rare.
7515ffd83dbSDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
7525ffd83dbSDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
7535ffd83dbSDimitry Andric 
7545ffd83dbSDimitry Andric   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
7555ffd83dbSDimitry Andric     if (LP64)
7565ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer)
7575ffd83dbSDimitry Andric           .addReg(PPC::X31)
7585ffd83dbSDimitry Andric           .addImm(FrameSize);
7595ffd83dbSDimitry Andric     else
7605ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer)
7615ffd83dbSDimitry Andric           .addReg(PPC::R31)
7625ffd83dbSDimitry Andric           .addImm(FrameSize);
7635ffd83dbSDimitry Andric   } else if (LP64) {
7645ffd83dbSDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer)
7655ffd83dbSDimitry Andric         .addImm(0)
7665ffd83dbSDimitry Andric         .addReg(PPC::X1);
7675ffd83dbSDimitry Andric   } else {
7685ffd83dbSDimitry Andric     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer)
7695ffd83dbSDimitry Andric         .addImm(0)
7705ffd83dbSDimitry Andric         .addReg(PPC::R1);
7715ffd83dbSDimitry Andric   }
7725ffd83dbSDimitry Andric   // Determine the actual NegSizeReg according to alignment info.
7735ffd83dbSDimitry Andric   if (LP64) {
7745ffd83dbSDimitry Andric     if (MaxAlign > TargetAlign) {
7755ffd83dbSDimitry Andric       unsigned UnalNegSizeReg = NegSizeReg;
7765ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
7775ffd83dbSDimitry Andric 
7785ffd83dbSDimitry Andric       // Unfortunately, there is no andi, only andi., and we can't insert that
7795ffd83dbSDimitry Andric       // here because we might clobber cr0 while it is live.
7805ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
7815ffd83dbSDimitry Andric           .addImm(~(MaxAlign.value() - 1));
7825ffd83dbSDimitry Andric 
7835ffd83dbSDimitry Andric       unsigned NegSizeReg1 = NegSizeReg;
7845ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
7855ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
7865ffd83dbSDimitry Andric           .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
7875ffd83dbSDimitry Andric           .addReg(NegSizeReg1, RegState::Kill);
7885ffd83dbSDimitry Andric       KillNegSizeReg = true;
7895ffd83dbSDimitry Andric     }
7905ffd83dbSDimitry Andric   } else {
7915ffd83dbSDimitry Andric     if (MaxAlign > TargetAlign) {
7925ffd83dbSDimitry Andric       unsigned UnalNegSizeReg = NegSizeReg;
7935ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
7945ffd83dbSDimitry Andric 
7955ffd83dbSDimitry Andric       // Unfortunately, there is no andi, only andi., and we can't insert that
7965ffd83dbSDimitry Andric       // here because we might clobber cr0 while it is live.
7975ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
7985ffd83dbSDimitry Andric           .addImm(~(MaxAlign.value() - 1));
7995ffd83dbSDimitry Andric 
8005ffd83dbSDimitry Andric       unsigned NegSizeReg1 = NegSizeReg;
8015ffd83dbSDimitry Andric       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
8025ffd83dbSDimitry Andric       BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
8035ffd83dbSDimitry Andric           .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
8045ffd83dbSDimitry Andric           .addReg(NegSizeReg1, RegState::Kill);
8055ffd83dbSDimitry Andric       KillNegSizeReg = true;
8065ffd83dbSDimitry Andric     }
8075ffd83dbSDimitry Andric   }
8085ffd83dbSDimitry Andric }
8095ffd83dbSDimitry Andric 
8105ffd83dbSDimitry Andric void PPCRegisterInfo::lowerPrepareProbedAlloca(
8115ffd83dbSDimitry Andric     MachineBasicBlock::iterator II) const {
8125ffd83dbSDimitry Andric   MachineInstr &MI = *II;
8135ffd83dbSDimitry Andric   // Get the instruction's basic block.
8145ffd83dbSDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8155ffd83dbSDimitry Andric   // Get the basic block's function.
8165ffd83dbSDimitry Andric   MachineFunction &MF = *MBB.getParent();
8175ffd83dbSDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
8185ffd83dbSDimitry Andric   // Get the instruction info.
8195ffd83dbSDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
8205ffd83dbSDimitry Andric   // Determine whether 64-bit pointers are used.
8215ffd83dbSDimitry Andric   bool LP64 = TM.isPPC64();
8225ffd83dbSDimitry Andric   DebugLoc dl = MI.getDebugLoc();
8235ffd83dbSDimitry Andric   Register FramePointer = MI.getOperand(0).getReg();
824590d96feSDimitry Andric   const Register ActualNegSizeReg = MI.getOperand(1).getReg();
8255ffd83dbSDimitry Andric   bool KillNegSizeReg = MI.getOperand(2).isKill();
8265ffd83dbSDimitry Andric   Register NegSizeReg = MI.getOperand(2).getReg();
827590d96feSDimitry Andric   const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR);
828590d96feSDimitry Andric   // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg.
829590d96feSDimitry Andric   if (FramePointer == NegSizeReg) {
830590d96feSDimitry Andric     assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, "
831590d96feSDimitry Andric                              "NegSizeReg should be killed");
832590d96feSDimitry Andric     // FramePointer is clobbered earlier than the use of NegSizeReg in
833590d96feSDimitry Andric     // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid
834590d96feSDimitry Andric     // misuse.
835590d96feSDimitry Andric     BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
836590d96feSDimitry Andric         .addReg(NegSizeReg)
837590d96feSDimitry Andric         .addReg(NegSizeReg);
838590d96feSDimitry Andric     NegSizeReg = ActualNegSizeReg;
839590d96feSDimitry Andric     KillNegSizeReg = false;
8405ffd83dbSDimitry Andric   }
841590d96feSDimitry Andric   prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer);
842590d96feSDimitry Andric   // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign >
843590d96feSDimitry Andric   // TargetAlign.
844590d96feSDimitry Andric   if (NegSizeReg != ActualNegSizeReg)
845590d96feSDimitry Andric     BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
846590d96feSDimitry Andric         .addReg(NegSizeReg)
847590d96feSDimitry Andric         .addReg(NegSizeReg);
8485ffd83dbSDimitry Andric   MBB.erase(II);
8495ffd83dbSDimitry Andric }
8505ffd83dbSDimitry Andric 
8510b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAreaOffset(
8520b57cec5SDimitry Andric     MachineBasicBlock::iterator II) const {
8530b57cec5SDimitry Andric   // Get the instruction.
8540b57cec5SDimitry Andric   MachineInstr &MI = *II;
8550b57cec5SDimitry Andric   // Get the instruction's basic block.
8560b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8570b57cec5SDimitry Andric   // Get the basic block's function.
8580b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
8590b57cec5SDimitry Andric   // Get the frame info.
8600b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
8610b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
8620b57cec5SDimitry Andric   // Get the instruction info.
8630b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
8660b57cec5SDimitry Andric   bool is64Bit = TM.isPPC64();
8670b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
8680b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
8690b57cec5SDimitry Andric           MI.getOperand(0).getReg())
8700b57cec5SDimitry Andric       .addImm(maxCallFrameSize);
8710b57cec5SDimitry Andric   MBB.erase(II);
8720b57cec5SDimitry Andric }
8730b57cec5SDimitry Andric 
8740b57cec5SDimitry Andric /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
8750b57cec5SDimitry Andric /// reserving a whole register (R0), we scrounge for one here. This generates
8760b57cec5SDimitry Andric /// code like this:
8770b57cec5SDimitry Andric ///
8780b57cec5SDimitry Andric ///   mfcr rA                  ; Move the conditional register into GPR rA.
8790b57cec5SDimitry Andric ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
8800b57cec5SDimitry Andric ///   stw rA, FI               ; Store rA to the frame.
8810b57cec5SDimitry Andric ///
8820b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
8830b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
8840b57cec5SDimitry Andric   // Get the instruction.
8850b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
8860b57cec5SDimitry Andric   // Get the instruction's basic block.
8870b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8880b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
8890b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
8900b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
8910b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
8940b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
8950b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
8960b57cec5SDimitry Andric 
8978bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
8988bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric   // We need to store the CR in the low 4-bits of the saved value. First, issue
9010b57cec5SDimitry Andric   // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
9020b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
9030b57cec5SDimitry Andric       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
9040b57cec5SDimitry Andric 
9050b57cec5SDimitry Andric   // If the saved register wasn't CR0, shift the bits left so that they are in
9060b57cec5SDimitry Andric   // CR0's slot.
9070b57cec5SDimitry Andric   if (SrcReg != PPC::CR0) {
9085ffd83dbSDimitry Andric     Register Reg1 = Reg;
9090b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric     // rlwinm rA, rA, ShiftBits, 0, 31.
9120b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
9130b57cec5SDimitry Andric       .addReg(Reg1, RegState::Kill)
9140b57cec5SDimitry Andric       .addImm(getEncodingValue(SrcReg) * 4)
9150b57cec5SDimitry Andric       .addImm(0)
9160b57cec5SDimitry Andric       .addImm(31);
9170b57cec5SDimitry Andric   }
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
9200b57cec5SDimitry Andric                     .addReg(Reg, RegState::Kill),
9210b57cec5SDimitry Andric                     FrameIndex);
9220b57cec5SDimitry Andric 
9230b57cec5SDimitry Andric   // Discard the pseudo instruction.
9240b57cec5SDimitry Andric   MBB.erase(II);
9250b57cec5SDimitry Andric }
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
9280b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
9290b57cec5SDimitry Andric   // Get the instruction.
9300b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
9310b57cec5SDimitry Andric   // Get the instruction's basic block.
9320b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
9330b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
9340b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9350b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
9360b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
9390b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
9400b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
9410b57cec5SDimitry Andric 
9428bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9438bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
9440b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
9450b57cec5SDimitry Andric     "RESTORE_CR does not define its destination");
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
9480b57cec5SDimitry Andric                               Reg), FrameIndex);
9490b57cec5SDimitry Andric 
9500b57cec5SDimitry Andric   // If the reloaded register isn't CR0, shift the bits right so that they are
9510b57cec5SDimitry Andric   // in the right CR's slot.
9520b57cec5SDimitry Andric   if (DestReg != PPC::CR0) {
9535ffd83dbSDimitry Andric     Register Reg1 = Reg;
9540b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric     unsigned ShiftBits = getEncodingValue(DestReg)*4;
9570b57cec5SDimitry Andric     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
9580b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
9590b57cec5SDimitry Andric              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
9600b57cec5SDimitry Andric              .addImm(31);
9610b57cec5SDimitry Andric   }
9620b57cec5SDimitry Andric 
9630b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
9640b57cec5SDimitry Andric              .addReg(Reg, RegState::Kill);
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric   // Discard the pseudo instruction.
9670b57cec5SDimitry Andric   MBB.erase(II);
9680b57cec5SDimitry Andric }
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
9710b57cec5SDimitry Andric                                          unsigned FrameIndex) const {
9720b57cec5SDimitry Andric   // Get the instruction.
9730b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; SPILL_CRBIT <SrcReg>, <offset>
9740b57cec5SDimitry Andric   // Get the instruction's basic block.
9750b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
9760b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
9770b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
9780b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
9790b57cec5SDimitry Andric   const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
9800b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
9810b57cec5SDimitry Andric 
9820b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
9830b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
9840b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
9850b57cec5SDimitry Andric 
9868bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
9878bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric   // Search up the BB to find the definition of the CR bit.
990480093f4SDimitry Andric   MachineBasicBlock::reverse_iterator Ins = MI;
991480093f4SDimitry Andric   MachineBasicBlock::reverse_iterator Rend = MBB.rend();
992480093f4SDimitry Andric   ++Ins;
9930b57cec5SDimitry Andric   unsigned CRBitSpillDistance = 0;
994480093f4SDimitry Andric   bool SeenUse = false;
995480093f4SDimitry Andric   for (; Ins != Rend; ++Ins) {
9960b57cec5SDimitry Andric     // Definition found.
9970b57cec5SDimitry Andric     if (Ins->modifiesRegister(SrcReg, TRI))
9980b57cec5SDimitry Andric       break;
999480093f4SDimitry Andric     // Use found.
1000480093f4SDimitry Andric     if (Ins->readsRegister(SrcReg, TRI))
1001480093f4SDimitry Andric       SeenUse = true;
10020b57cec5SDimitry Andric     // Unable to find CR bit definition within maximum search distance.
10030b57cec5SDimitry Andric     if (CRBitSpillDistance == MaxCRBitSpillDist) {
10040b57cec5SDimitry Andric       Ins = MI;
10050b57cec5SDimitry Andric       break;
10060b57cec5SDimitry Andric     }
10070b57cec5SDimitry Andric     // Skip debug instructions when counting CR bit spill distance.
10080b57cec5SDimitry Andric     if (!Ins->isDebugInstr())
10090b57cec5SDimitry Andric       CRBitSpillDistance++;
10100b57cec5SDimitry Andric   }
10110b57cec5SDimitry Andric 
10120b57cec5SDimitry Andric   // Unable to find the definition of the CR bit in the MBB.
10130b57cec5SDimitry Andric   if (Ins == MBB.rend())
10140b57cec5SDimitry Andric     Ins = MI;
10150b57cec5SDimitry Andric 
1016480093f4SDimitry Andric   bool SpillsKnownBit = false;
10170b57cec5SDimitry Andric   // There is no need to extract the CR bit if its value is already known.
10180b57cec5SDimitry Andric   switch (Ins->getOpcode()) {
10190b57cec5SDimitry Andric   case PPC::CRUNSET:
10200b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
10210b57cec5SDimitry Andric       .addImm(0);
1022480093f4SDimitry Andric     SpillsKnownBit = true;
10230b57cec5SDimitry Andric     break;
10240b57cec5SDimitry Andric   case PPC::CRSET:
10250b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
10260b57cec5SDimitry Andric       .addImm(-32768);
1027480093f4SDimitry Andric     SpillsKnownBit = true;
10280b57cec5SDimitry Andric     break;
10290b57cec5SDimitry Andric   default:
1030e8d8bef9SDimitry Andric     // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all
1031e8d8bef9SDimitry Andric     // bits (specifically, it produces a -1 if the CR bit is set). Ultimately,
1032e8d8bef9SDimitry Andric     // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit
1033e8d8bef9SDimitry Andric     // register), and SETNBC will set this.
1034e8d8bef9SDimitry Andric     if (Subtarget.isISA3_1()) {
1035e8d8bef9SDimitry Andric       BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg)
1036e8d8bef9SDimitry Andric           .addReg(SrcReg, RegState::Undef);
1037e8d8bef9SDimitry Andric       break;
1038e8d8bef9SDimitry Andric     }
1039e8d8bef9SDimitry Andric 
1040480093f4SDimitry Andric     // On Power9, we can use SETB to extract the LT bit. This only works for
1041480093f4SDimitry Andric     // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value
1042480093f4SDimitry Andric     // of the bit we care about (32-bit sign bit) will be set to the value of
1043480093f4SDimitry Andric     // the LT bit (regardless of the other bits in the CR field).
1044480093f4SDimitry Andric     if (Subtarget.isISA3_0()) {
1045480093f4SDimitry Andric       if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT ||
1046480093f4SDimitry Andric           SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT ||
1047480093f4SDimitry Andric           SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT ||
1048480093f4SDimitry Andric           SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) {
1049480093f4SDimitry Andric         BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg)
1050480093f4SDimitry Andric           .addReg(getCRFromCRBit(SrcReg), RegState::Undef);
1051480093f4SDimitry Andric         break;
1052480093f4SDimitry Andric       }
1053480093f4SDimitry Andric     }
1054480093f4SDimitry Andric 
10550b57cec5SDimitry Andric     // We need to move the CR field that contains the CR bit we are spilling.
10560b57cec5SDimitry Andric     // The super register may not be explicitly defined (i.e. it can be defined
10570b57cec5SDimitry Andric     // by a CR-logical that only defines the subreg) so we state that the CR
10580b57cec5SDimitry Andric     // field is undef. Also, in order to preserve the kill flag on the CR bit,
10590b57cec5SDimitry Andric     // we add it as an implicit use.
10600b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
10610b57cec5SDimitry Andric       .addReg(getCRFromCRBit(SrcReg), RegState::Undef)
10620b57cec5SDimitry Andric       .addReg(SrcReg,
10630b57cec5SDimitry Andric               RegState::Implicit | getKillRegState(MI.getOperand(0).isKill()));
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric     // If the saved register wasn't CR0LT, shift the bits left so that the bit
10660b57cec5SDimitry Andric     // to store is the first one. Mask all but that bit.
10675ffd83dbSDimitry Andric     Register Reg1 = Reg;
10680b57cec5SDimitry Andric     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
10690b57cec5SDimitry Andric 
10700b57cec5SDimitry Andric     // rlwinm rA, rA, ShiftBits, 0, 0.
10710b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
10720b57cec5SDimitry Andric       .addReg(Reg1, RegState::Kill)
10730b57cec5SDimitry Andric       .addImm(getEncodingValue(SrcReg))
10740b57cec5SDimitry Andric       .addImm(0).addImm(0);
10750b57cec5SDimitry Andric   }
10760b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
10770b57cec5SDimitry Andric                     .addReg(Reg, RegState::Kill),
10780b57cec5SDimitry Andric                     FrameIndex);
10790b57cec5SDimitry Andric 
1080480093f4SDimitry Andric   bool KillsCRBit = MI.killsRegister(SrcReg, TRI);
10810b57cec5SDimitry Andric   // Discard the pseudo instruction.
10820b57cec5SDimitry Andric   MBB.erase(II);
1083480093f4SDimitry Andric   if (SpillsKnownBit && KillsCRBit && !SeenUse) {
1084480093f4SDimitry Andric     Ins->setDesc(TII.get(PPC::UNENCODED_NOP));
1085480093f4SDimitry Andric     Ins->RemoveOperand(0);
1086480093f4SDimitry Andric   }
10870b57cec5SDimitry Andric }
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
10900b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
10910b57cec5SDimitry Andric   // Get the instruction.
10920b57cec5SDimitry Andric   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CRBIT <offset>
10930b57cec5SDimitry Andric   // Get the instruction's basic block.
10940b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
10950b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
10960b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
10970b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
10980b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
10990b57cec5SDimitry Andric 
11000b57cec5SDimitry Andric   bool LP64 = TM.isPPC64();
11010b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
11020b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
11030b57cec5SDimitry Andric 
11048bcb0991SDimitry Andric   Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
11058bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
11060b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
11070b57cec5SDimitry Andric     "RESTORE_CRBIT does not define its destination");
11080b57cec5SDimitry Andric 
11090b57cec5SDimitry Andric   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
11100b57cec5SDimitry Andric                               Reg), FrameIndex);
11110b57cec5SDimitry Andric 
11120b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
11130b57cec5SDimitry Andric 
11148bcb0991SDimitry Andric   Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
11150b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
11160b57cec5SDimitry Andric           .addReg(getCRFromCRBit(DestReg));
11170b57cec5SDimitry Andric 
11180b57cec5SDimitry Andric   unsigned ShiftBits = getEncodingValue(DestReg);
11190b57cec5SDimitry Andric   // rlwimi r11, r10, 32-ShiftBits, ..., ...
11200b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
11210b57cec5SDimitry Andric       .addReg(RegO, RegState::Kill)
11220b57cec5SDimitry Andric       .addReg(Reg, RegState::Kill)
11230b57cec5SDimitry Andric       .addImm(ShiftBits ? 32 - ShiftBits : 0)
11240b57cec5SDimitry Andric       .addImm(ShiftBits)
11250b57cec5SDimitry Andric       .addImm(ShiftBits);
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
11280b57cec5SDimitry Andric           getCRFromCRBit(DestReg))
11290b57cec5SDimitry Andric       .addReg(RegO, RegState::Kill)
11300b57cec5SDimitry Andric       // Make sure we have a use dependency all the way through this
11310b57cec5SDimitry Andric       // sequence of instructions. We can't have the other bits in the CR
11320b57cec5SDimitry Andric       // modified in between the mfocrf and the mtocrf.
11330b57cec5SDimitry Andric       .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric   // Discard the pseudo instruction.
11360b57cec5SDimitry Andric   MBB.erase(II);
11370b57cec5SDimitry Andric }
11380b57cec5SDimitry Andric 
1139e8d8bef9SDimitry Andric void PPCRegisterInfo::emitAccCopyInfo(MachineBasicBlock &MBB,
1140e8d8bef9SDimitry Andric                                       MCRegister DestReg, MCRegister SrcReg) {
1141e8d8bef9SDimitry Andric #ifdef NDEBUG
1142e8d8bef9SDimitry Andric   return;
1143e8d8bef9SDimitry Andric #else
1144e8d8bef9SDimitry Andric   if (ReportAccMoves) {
1145e8d8bef9SDimitry Andric     std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc";
1146e8d8bef9SDimitry Andric     std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc";
1147e8d8bef9SDimitry Andric     dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n";
1148e8d8bef9SDimitry Andric     MBB.dump();
1149e8d8bef9SDimitry Andric   }
1150e8d8bef9SDimitry Andric #endif
1151e8d8bef9SDimitry Andric }
1152e8d8bef9SDimitry Andric 
1153e8d8bef9SDimitry Andric static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed,
1154e8d8bef9SDimitry Andric                                     bool IsRestore) {
1155e8d8bef9SDimitry Andric #ifdef NDEBUG
1156e8d8bef9SDimitry Andric   return;
1157e8d8bef9SDimitry Andric #else
1158e8d8bef9SDimitry Andric   if (ReportAccMoves) {
1159e8d8bef9SDimitry Andric     dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register "
1160e8d8bef9SDimitry Andric            << (IsRestore ? "restore" : "spill") << ":\n";
1161e8d8bef9SDimitry Andric     MBB.dump();
1162e8d8bef9SDimitry Andric   }
1163e8d8bef9SDimitry Andric #endif
1164e8d8bef9SDimitry Andric }
1165e8d8bef9SDimitry Andric 
1166e8d8bef9SDimitry Andric /// lowerACCSpilling - Generate the code for spilling the accumulator register.
1167e8d8bef9SDimitry Andric /// Similarly to other spills/reloads that use pseudo-ops, we do not actually
1168e8d8bef9SDimitry Andric /// eliminate the FrameIndex here nor compute the stack offset. We simply
1169e8d8bef9SDimitry Andric /// create a real instruction with an FI and rely on eliminateFrameIndex to
1170e8d8bef9SDimitry Andric /// handle the FI elimination.
1171e8d8bef9SDimitry Andric void PPCRegisterInfo::lowerACCSpilling(MachineBasicBlock::iterator II,
11720b57cec5SDimitry Andric                                        unsigned FrameIndex) const {
1173e8d8bef9SDimitry Andric   MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset>
11740b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
11750b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
11760b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
11770b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1178e8d8bef9SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
11798bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
1180e8d8bef9SDimitry Andric   bool IsKilled = MI.getOperand(0).isKill();
11810b57cec5SDimitry Andric 
1182e8d8bef9SDimitry Andric   bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg);
1183e8d8bef9SDimitry Andric   Register Reg =
1184e8d8bef9SDimitry Andric       PPC::VSRp0 + (SrcReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1185e8d8bef9SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
11860b57cec5SDimitry Andric 
1187e8d8bef9SDimitry Andric   emitAccSpillRestoreInfo(MBB, IsPrimed, false);
1188e8d8bef9SDimitry Andric 
1189e8d8bef9SDimitry Andric   // De-prime the register being spilled, create two stores for the pair
1190e8d8bef9SDimitry Andric   // subregisters accounting for endianness and then re-prime the register if
1191e8d8bef9SDimitry Andric   // it isn't killed.  This uses the Offset parameter to addFrameReference() to
1192e8d8bef9SDimitry Andric   // adjust the offset of the store that is within the 64-byte stack slot.
1193e8d8bef9SDimitry Andric   if (IsPrimed)
1194e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg);
1195e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1196e8d8bef9SDimitry Andric                         .addReg(Reg, getKillRegState(IsKilled)),
1197e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 32 : 0);
1198e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
1199e8d8bef9SDimitry Andric                         .addReg(Reg + 1, getKillRegState(IsKilled)),
1200e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 0 : 32);
1201e8d8bef9SDimitry Andric   if (IsPrimed && !IsKilled)
1202e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg);
12030b57cec5SDimitry Andric 
12040b57cec5SDimitry Andric   // Discard the pseudo instruction.
12050b57cec5SDimitry Andric   MBB.erase(II);
12060b57cec5SDimitry Andric }
12070b57cec5SDimitry Andric 
1208e8d8bef9SDimitry Andric /// lowerACCRestore - Generate the code to restore the accumulator register.
1209e8d8bef9SDimitry Andric void PPCRegisterInfo::lowerACCRestore(MachineBasicBlock::iterator II,
12100b57cec5SDimitry Andric                                       unsigned FrameIndex) const {
1211e8d8bef9SDimitry Andric   MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset>
12120b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
12130b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
12140b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
12150b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1216e8d8bef9SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
12170b57cec5SDimitry Andric 
12188bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
12190b57cec5SDimitry Andric   assert(MI.definesRegister(DestReg) &&
1220e8d8bef9SDimitry Andric          "RESTORE_ACC does not define its destination");
12210b57cec5SDimitry Andric 
1222e8d8bef9SDimitry Andric   bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg);
1223e8d8bef9SDimitry Andric   Register Reg =
1224e8d8bef9SDimitry Andric       PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
1225e8d8bef9SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
12260b57cec5SDimitry Andric 
1227e8d8bef9SDimitry Andric   emitAccSpillRestoreInfo(MBB, IsPrimed, true);
1228e8d8bef9SDimitry Andric 
1229e8d8bef9SDimitry Andric   // Create two loads for the pair subregisters accounting for endianness and
1230e8d8bef9SDimitry Andric   // then prime the accumulator register being restored.
1231e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg),
1232e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 32 : 0);
1233e8d8bef9SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1),
1234e8d8bef9SDimitry Andric                     FrameIndex, IsLittleEndian ? 0 : 32);
1235e8d8bef9SDimitry Andric   if (IsPrimed)
1236e8d8bef9SDimitry Andric     BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg);
12370b57cec5SDimitry Andric 
12380b57cec5SDimitry Andric   // Discard the pseudo instruction.
12390b57cec5SDimitry Andric   MBB.erase(II);
12400b57cec5SDimitry Andric }
12410b57cec5SDimitry Andric 
1242fe6060f1SDimitry Andric /// lowerQuadwordSpilling - Generate code to spill paired general register.
1243fe6060f1SDimitry Andric void PPCRegisterInfo::lowerQuadwordSpilling(MachineBasicBlock::iterator II,
1244fe6060f1SDimitry Andric                                             unsigned FrameIndex) const {
1245fe6060f1SDimitry Andric   MachineInstr &MI = *II;
1246fe6060f1SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
1247fe6060f1SDimitry Andric   MachineFunction &MF = *MBB.getParent();
1248fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1249fe6060f1SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1250fe6060f1SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
1251fe6060f1SDimitry Andric 
1252fe6060f1SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
1253fe6060f1SDimitry Andric   bool IsKilled = MI.getOperand(0).isKill();
1254fe6060f1SDimitry Andric 
1255fe6060f1SDimitry Andric   Register Reg = PPC::X0 + (SrcReg - PPC::G8p0) * 2;
1256fe6060f1SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
1257fe6060f1SDimitry Andric 
1258fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
1259fe6060f1SDimitry Andric                         .addReg(Reg, getKillRegState(IsKilled)),
1260fe6060f1SDimitry Andric                     FrameIndex, IsLittleEndian ? 8 : 0);
1261fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
1262fe6060f1SDimitry Andric                         .addReg(Reg + 1, getKillRegState(IsKilled)),
1263fe6060f1SDimitry Andric                     FrameIndex, IsLittleEndian ? 0 : 8);
1264fe6060f1SDimitry Andric 
1265fe6060f1SDimitry Andric   // Discard the pseudo instruction.
1266fe6060f1SDimitry Andric   MBB.erase(II);
1267fe6060f1SDimitry Andric }
1268fe6060f1SDimitry Andric 
1269fe6060f1SDimitry Andric /// lowerQuadwordRestore - Generate code to restore paired general register.
1270fe6060f1SDimitry Andric void PPCRegisterInfo::lowerQuadwordRestore(MachineBasicBlock::iterator II,
1271fe6060f1SDimitry Andric                                            unsigned FrameIndex) const {
1272fe6060f1SDimitry Andric   MachineInstr &MI = *II;
1273fe6060f1SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
1274fe6060f1SDimitry Andric   MachineFunction &MF = *MBB.getParent();
1275fe6060f1SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1276fe6060f1SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1277fe6060f1SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
1278fe6060f1SDimitry Andric 
1279fe6060f1SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
1280fe6060f1SDimitry Andric   assert(MI.definesRegister(DestReg) &&
1281fe6060f1SDimitry Andric          "RESTORE_QUADWORD does not define its destination");
1282fe6060f1SDimitry Andric 
1283fe6060f1SDimitry Andric   Register Reg = PPC::X0 + (DestReg - PPC::G8p0) * 2;
1284fe6060f1SDimitry Andric   bool IsLittleEndian = Subtarget.isLittleEndian();
1285fe6060f1SDimitry Andric 
1286fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg), FrameIndex,
1287fe6060f1SDimitry Andric                     IsLittleEndian ? 8 : 0);
1288fe6060f1SDimitry Andric   addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg + 1), FrameIndex,
1289fe6060f1SDimitry Andric                     IsLittleEndian ? 0 : 8);
1290fe6060f1SDimitry Andric 
1291fe6060f1SDimitry Andric   // Discard the pseudo instruction.
1292fe6060f1SDimitry Andric   MBB.erase(II);
1293fe6060f1SDimitry Andric }
1294fe6060f1SDimitry Andric 
12950b57cec5SDimitry Andric bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
12965ffd83dbSDimitry Andric                                            Register Reg, int &FrameIdx) const {
12975ffd83dbSDimitry Andric   // For the nonvolatile condition registers (CR2, CR3, CR4) return true to
12985ffd83dbSDimitry Andric   // prevent allocating an additional frame slot.
12995ffd83dbSDimitry Andric   // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8,
13005ffd83dbSDimitry Andric   // for 32-bit AIX the CR save area is in the linkage area at SP+4.
13015ffd83dbSDimitry Andric   // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos
13025ffd83dbSDimitry Andric   // valid.
13035ffd83dbSDimitry Andric   // For 32-bit ELF, we have previously created the stack slot if needed, so
13045ffd83dbSDimitry Andric   // return its FrameIdx.
13055ffd83dbSDimitry Andric   if (PPC::CR2 <= Reg && Reg <= PPC::CR4) {
13065ffd83dbSDimitry Andric     FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex();
13070b57cec5SDimitry Andric     return true;
13080b57cec5SDimitry Andric   }
13090b57cec5SDimitry Andric   return false;
13100b57cec5SDimitry Andric }
13110b57cec5SDimitry Andric 
13120b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is.
13130b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC) {
13140b57cec5SDimitry Andric   switch (OpC) {
13150b57cec5SDimitry Andric   default:
13160b57cec5SDimitry Andric     return 1;
13170b57cec5SDimitry Andric   case PPC::LWA:
13180b57cec5SDimitry Andric   case PPC::LWA_32:
13190b57cec5SDimitry Andric   case PPC::LD:
13200b57cec5SDimitry Andric   case PPC::LDU:
13210b57cec5SDimitry Andric   case PPC::STD:
13220b57cec5SDimitry Andric   case PPC::STDU:
13230b57cec5SDimitry Andric   case PPC::DFLOADf32:
13240b57cec5SDimitry Andric   case PPC::DFLOADf64:
13250b57cec5SDimitry Andric   case PPC::DFSTOREf32:
13260b57cec5SDimitry Andric   case PPC::DFSTOREf64:
13270b57cec5SDimitry Andric   case PPC::LXSD:
13280b57cec5SDimitry Andric   case PPC::LXSSP:
13290b57cec5SDimitry Andric   case PPC::STXSD:
13300b57cec5SDimitry Andric   case PPC::STXSSP:
1331fe6060f1SDimitry Andric   case PPC::STQ:
13320b57cec5SDimitry Andric     return 4;
13330b57cec5SDimitry Andric   case PPC::EVLDD:
13340b57cec5SDimitry Andric   case PPC::EVSTDD:
13350b57cec5SDimitry Andric     return 8;
13360b57cec5SDimitry Andric   case PPC::LXV:
13370b57cec5SDimitry Andric   case PPC::STXV:
1338fe6060f1SDimitry Andric   case PPC::LQ:
1339fe6060f1SDimitry Andric   case PPC::LXVP:
1340fe6060f1SDimitry Andric   case PPC::STXVP:
13410b57cec5SDimitry Andric     return 16;
13420b57cec5SDimitry Andric   }
13430b57cec5SDimitry Andric }
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is.
13460b57cec5SDimitry Andric static unsigned offsetMinAlign(const MachineInstr &MI) {
13470b57cec5SDimitry Andric   unsigned OpC = MI.getOpcode();
13480b57cec5SDimitry Andric   return offsetMinAlignForOpcode(OpC);
13490b57cec5SDimitry Andric }
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
13520b57cec5SDimitry Andric static unsigned getOffsetONFromFION(const MachineInstr &MI,
13530b57cec5SDimitry Andric                                     unsigned FIOperandNum) {
13540b57cec5SDimitry Andric   // Take into account whether it's an add or mem instruction
13550b57cec5SDimitry Andric   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
13560b57cec5SDimitry Andric   if (MI.isInlineAsm())
13570b57cec5SDimitry Andric     OffsetOperandNo = FIOperandNum - 1;
13580b57cec5SDimitry Andric   else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
13590b57cec5SDimitry Andric            MI.getOpcode() == TargetOpcode::PATCHPOINT)
13600b57cec5SDimitry Andric     OffsetOperandNo = FIOperandNum + 1;
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric   return OffsetOperandNo;
13630b57cec5SDimitry Andric }
13640b57cec5SDimitry Andric 
13650b57cec5SDimitry Andric void
13660b57cec5SDimitry Andric PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
13670b57cec5SDimitry Andric                                      int SPAdj, unsigned FIOperandNum,
13680b57cec5SDimitry Andric                                      RegScavenger *RS) const {
13690b57cec5SDimitry Andric   assert(SPAdj == 0 && "Unexpected");
13700b57cec5SDimitry Andric 
13710b57cec5SDimitry Andric   // Get the instruction.
13720b57cec5SDimitry Andric   MachineInstr &MI = *II;
13730b57cec5SDimitry Andric   // Get the instruction's basic block.
13740b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
13750b57cec5SDimitry Andric   // Get the basic block's function.
13760b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
13770b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
13780b57cec5SDimitry Andric   // Get the instruction info.
1379349cc55cSDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
13800b57cec5SDimitry Andric   // Get the frame info.
13810b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
13820b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
13830b57cec5SDimitry Andric 
13840b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
13850b57cec5SDimitry Andric 
13860b57cec5SDimitry Andric   // Get the frame index.
13870b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
13880b57cec5SDimitry Andric 
13890b57cec5SDimitry Andric   // Get the frame pointer save index.  Users of this index are primarily
13900b57cec5SDimitry Andric   // DYNALLOC instructions.
13910b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
13920b57cec5SDimitry Andric   int FPSI = FI->getFramePointerSaveIndex();
13930b57cec5SDimitry Andric   // Get the instruction opcode.
13940b57cec5SDimitry Andric   unsigned OpC = MI.getOpcode();
13950b57cec5SDimitry Andric 
13960b57cec5SDimitry Andric   if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
13970b57cec5SDimitry Andric     lowerDynamicAreaOffset(II);
13980b57cec5SDimitry Andric     return;
13990b57cec5SDimitry Andric   }
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric   // Special case for dynamic alloca.
14020b57cec5SDimitry Andric   if (FPSI && FrameIndex == FPSI &&
14030b57cec5SDimitry Andric       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
14040b57cec5SDimitry Andric     lowerDynamicAlloc(II);
14050b57cec5SDimitry Andric     return;
14060b57cec5SDimitry Andric   }
14070b57cec5SDimitry Andric 
14085ffd83dbSDimitry Andric   if (FPSI && FrameIndex == FPSI &&
14095ffd83dbSDimitry Andric       (OpC == PPC::PREPARE_PROBED_ALLOCA_64 ||
1410590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_32 ||
1411590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64 ||
1412590d96feSDimitry Andric        OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32)) {
14135ffd83dbSDimitry Andric     lowerPrepareProbedAlloca(II);
14145ffd83dbSDimitry Andric     return;
14155ffd83dbSDimitry Andric   }
14165ffd83dbSDimitry Andric 
14170b57cec5SDimitry Andric   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
14180b57cec5SDimitry Andric   if (OpC == PPC::SPILL_CR) {
14190b57cec5SDimitry Andric     lowerCRSpilling(II, FrameIndex);
14200b57cec5SDimitry Andric     return;
14210b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_CR) {
14220b57cec5SDimitry Andric     lowerCRRestore(II, FrameIndex);
14230b57cec5SDimitry Andric     return;
14240b57cec5SDimitry Andric   } else if (OpC == PPC::SPILL_CRBIT) {
14250b57cec5SDimitry Andric     lowerCRBitSpilling(II, FrameIndex);
14260b57cec5SDimitry Andric     return;
14270b57cec5SDimitry Andric   } else if (OpC == PPC::RESTORE_CRBIT) {
14280b57cec5SDimitry Andric     lowerCRBitRestore(II, FrameIndex);
14290b57cec5SDimitry Andric     return;
1430e8d8bef9SDimitry Andric   } else if (OpC == PPC::SPILL_ACC || OpC == PPC::SPILL_UACC) {
1431e8d8bef9SDimitry Andric     lowerACCSpilling(II, FrameIndex);
14320b57cec5SDimitry Andric     return;
1433e8d8bef9SDimitry Andric   } else if (OpC == PPC::RESTORE_ACC || OpC == PPC::RESTORE_UACC) {
1434e8d8bef9SDimitry Andric     lowerACCRestore(II, FrameIndex);
14350b57cec5SDimitry Andric     return;
1436fe6060f1SDimitry Andric   } else if (OpC == PPC::SPILL_QUADWORD) {
1437fe6060f1SDimitry Andric     lowerQuadwordSpilling(II, FrameIndex);
1438fe6060f1SDimitry Andric     return;
1439fe6060f1SDimitry Andric   } else if (OpC == PPC::RESTORE_QUADWORD) {
1440fe6060f1SDimitry Andric     lowerQuadwordRestore(II, FrameIndex);
1441fe6060f1SDimitry Andric     return;
14420b57cec5SDimitry Andric   }
14430b57cec5SDimitry Andric 
14440b57cec5SDimitry Andric   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
14450b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(
14460b57cec5SDimitry Andric     FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
14470b57cec5SDimitry Andric 
14480b57cec5SDimitry Andric   // If the instruction is not present in ImmToIdxMap, then it has no immediate
14490b57cec5SDimitry Andric   // form (and must be r+r).
14500b57cec5SDimitry Andric   bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
14510b57cec5SDimitry Andric                    OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
14520b57cec5SDimitry Andric 
14530b57cec5SDimitry Andric   // Now add the frame object offset to the offset from r1.
14540b57cec5SDimitry Andric   int Offset = MFI.getObjectOffset(FrameIndex);
14550b57cec5SDimitry Andric   Offset += MI.getOperand(OffsetOperandNo).getImm();
14560b57cec5SDimitry Andric 
14570b57cec5SDimitry Andric   // If we're not using a Frame Pointer that has been set to the value of the
14580b57cec5SDimitry Andric   // SP before having the stack size subtracted from it, then add the stack size
14590b57cec5SDimitry Andric   // to Offset to get the correct offset.
14600b57cec5SDimitry Andric   // Naked functions have stack size 0, although getStackSize may not reflect
14610b57cec5SDimitry Andric   // that because we didn't call all the pieces that compute it for naked
14620b57cec5SDimitry Andric   // functions.
14630b57cec5SDimitry Andric   if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
14640b57cec5SDimitry Andric     if (!(hasBasePointer(MF) && FrameIndex < 0))
14650b57cec5SDimitry Andric       Offset += MFI.getStackSize();
14660b57cec5SDimitry Andric   }
14670b57cec5SDimitry Andric 
1468fe6060f1SDimitry Andric   // If we encounter an LXVP/STXVP with an offset that doesn't fit, we can
1469fe6060f1SDimitry Andric   // transform it to the prefixed version so we don't have to use the XForm.
1470fe6060f1SDimitry Andric   if ((OpC == PPC::LXVP || OpC == PPC::STXVP) &&
1471fe6060f1SDimitry Andric       (!isInt<16>(Offset) || (Offset % offsetMinAlign(MI)) != 0) &&
1472fe6060f1SDimitry Andric       Subtarget.hasPrefixInstrs()) {
1473fe6060f1SDimitry Andric     unsigned NewOpc = OpC == PPC::LXVP ? PPC::PLXVP : PPC::PSTXVP;
1474fe6060f1SDimitry Andric     MI.setDesc(TII.get(NewOpc));
1475fe6060f1SDimitry Andric     OpC = NewOpc;
1476fe6060f1SDimitry Andric   }
1477fe6060f1SDimitry Andric 
14780b57cec5SDimitry Andric   // If we can, encode the offset directly into the instruction.  If this is a
14790b57cec5SDimitry Andric   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
14800b57cec5SDimitry Andric   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
14810b57cec5SDimitry Andric   // clear can be encoded.  This is extremely uncommon, because normally you
14820b57cec5SDimitry Andric   // only "std" to a stack slot that is at least 4-byte aligned, but it can
14830b57cec5SDimitry Andric   // happen in invalid code.
14840b57cec5SDimitry Andric   assert(OpC != PPC::DBG_VALUE &&
14850b57cec5SDimitry Andric          "This should be handled in a target-independent way");
1486fe6060f1SDimitry Andric   // FIXME: This should be factored out to a separate function as prefixed
1487fe6060f1SDimitry Andric   // instructions add a number of opcodes for which we can use 34-bit imm.
14880b57cec5SDimitry Andric   bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ?
14890b57cec5SDimitry Andric                             isUInt<8>(Offset) :
14900b57cec5SDimitry Andric                             isInt<16>(Offset);
1491349cc55cSDimitry Andric   if (TII.isPrefixed(MI.getOpcode()))
1492fe6060f1SDimitry Andric     OffsetFitsMnemonic = isInt<34>(Offset);
14930b57cec5SDimitry Andric   if (!noImmForm && ((OffsetFitsMnemonic &&
14940b57cec5SDimitry Andric                       ((Offset % offsetMinAlign(MI)) == 0)) ||
14950b57cec5SDimitry Andric                      OpC == TargetOpcode::STACKMAP ||
14960b57cec5SDimitry Andric                      OpC == TargetOpcode::PATCHPOINT)) {
14970b57cec5SDimitry Andric     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
14980b57cec5SDimitry Andric     return;
14990b57cec5SDimitry Andric   }
15000b57cec5SDimitry Andric 
15010b57cec5SDimitry Andric   // The offset doesn't fit into a single register, scavenge one to build the
15020b57cec5SDimitry Andric   // offset in.
15030b57cec5SDimitry Andric 
15040b57cec5SDimitry Andric   bool is64Bit = TM.isPPC64();
15050b57cec5SDimitry Andric   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
15060b57cec5SDimitry Andric   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
15070b57cec5SDimitry Andric   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
15085ffd83dbSDimitry Andric   Register SRegHi = MF.getRegInfo().createVirtualRegister(RC),
15090b57cec5SDimitry Andric            SReg = MF.getRegInfo().createVirtualRegister(RC);
15100b57cec5SDimitry Andric 
15110b57cec5SDimitry Andric   // Insert a set of rA with the full offset value before the ld, st, or add
15120b57cec5SDimitry Andric   if (isInt<16>(Offset))
15130b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
15140b57cec5SDimitry Andric       .addImm(Offset);
15150b57cec5SDimitry Andric   else {
15160b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
15170b57cec5SDimitry Andric       .addImm(Offset >> 16);
15180b57cec5SDimitry Andric     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
15190b57cec5SDimitry Andric       .addReg(SRegHi, RegState::Kill)
15200b57cec5SDimitry Andric       .addImm(Offset);
15210b57cec5SDimitry Andric   }
15220b57cec5SDimitry Andric 
15230b57cec5SDimitry Andric   // Convert into indexed form of the instruction:
15240b57cec5SDimitry Andric   //
15250b57cec5SDimitry Andric   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
15260b57cec5SDimitry Andric   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
15270b57cec5SDimitry Andric   unsigned OperandBase;
15280b57cec5SDimitry Andric 
15290b57cec5SDimitry Andric   if (noImmForm)
15300b57cec5SDimitry Andric     OperandBase = 1;
15310b57cec5SDimitry Andric   else if (OpC != TargetOpcode::INLINEASM &&
15320b57cec5SDimitry Andric            OpC != TargetOpcode::INLINEASM_BR) {
15330b57cec5SDimitry Andric     assert(ImmToIdxMap.count(OpC) &&
15340b57cec5SDimitry Andric            "No indexed form of load or store available!");
15350b57cec5SDimitry Andric     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
15360b57cec5SDimitry Andric     MI.setDesc(TII.get(NewOpcode));
15370b57cec5SDimitry Andric     OperandBase = 1;
15380b57cec5SDimitry Andric   } else {
15390b57cec5SDimitry Andric     OperandBase = OffsetOperandNo;
15400b57cec5SDimitry Andric   }
15410b57cec5SDimitry Andric 
15428bcb0991SDimitry Andric   Register StackReg = MI.getOperand(FIOperandNum).getReg();
15430b57cec5SDimitry Andric   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
15440b57cec5SDimitry Andric   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
15450b57cec5SDimitry Andric }
15460b57cec5SDimitry Andric 
15470b57cec5SDimitry Andric Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
15480b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
15490b57cec5SDimitry Andric 
15500b57cec5SDimitry Andric   if (!TM.isPPC64())
15510b57cec5SDimitry Andric     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
15520b57cec5SDimitry Andric   else
15530b57cec5SDimitry Andric     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
15540b57cec5SDimitry Andric }
15550b57cec5SDimitry Andric 
15560b57cec5SDimitry Andric Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
15570b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
15580b57cec5SDimitry Andric   if (!hasBasePointer(MF))
15590b57cec5SDimitry Andric     return getFrameRegister(MF);
15600b57cec5SDimitry Andric 
15610b57cec5SDimitry Andric   if (TM.isPPC64())
15620b57cec5SDimitry Andric     return PPC::X30;
15630b57cec5SDimitry Andric 
15640b57cec5SDimitry Andric   if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
15650b57cec5SDimitry Andric     return PPC::R29;
15660b57cec5SDimitry Andric 
15670b57cec5SDimitry Andric   return PPC::R30;
15680b57cec5SDimitry Andric }
15690b57cec5SDimitry Andric 
15700b57cec5SDimitry Andric bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
15710b57cec5SDimitry Andric   if (!EnableBasePointer)
15720b57cec5SDimitry Andric     return false;
15730b57cec5SDimitry Andric   if (AlwaysBasePointer)
15740b57cec5SDimitry Andric     return true;
15750b57cec5SDimitry Andric 
15760b57cec5SDimitry Andric   // If we need to realign the stack, then the stack pointer can no longer
15770b57cec5SDimitry Andric   // serve as an offset into the caller's stack space. As a result, we need a
15780b57cec5SDimitry Andric   // base pointer.
1579fe6060f1SDimitry Andric   return hasStackRealignment(MF);
15800b57cec5SDimitry Andric }
15810b57cec5SDimitry Andric 
15820b57cec5SDimitry Andric /// Returns true if the instruction's frame index
15830b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
15840b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
15850b57cec5SDimitry Andric /// references it should create new base registers for.
15860b57cec5SDimitry Andric bool PPCRegisterInfo::
15870b57cec5SDimitry Andric needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
15880b57cec5SDimitry Andric   assert(Offset < 0 && "Local offset must be negative");
15890b57cec5SDimitry Andric 
15900b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
15910b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
15920b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
15930b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
15940b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
15950b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
15960b57cec5SDimitry Andric 
15970b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores that have
15980b57cec5SDimitry Andric   // an r+i form. Return false for everything else.
15990b57cec5SDimitry Andric   unsigned OpC = MI->getOpcode();
16000b57cec5SDimitry Andric   if (!ImmToIdxMap.count(OpC))
16010b57cec5SDimitry Andric     return false;
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric   // Don't generate a new virtual base register just to add zero to it.
16040b57cec5SDimitry Andric   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
16050b57cec5SDimitry Andric       MI->getOperand(2).getImm() == 0)
16060b57cec5SDimitry Andric     return false;
16070b57cec5SDimitry Andric 
16080b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI->getParent();
16090b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
16100b57cec5SDimitry Andric   const PPCFrameLowering *TFI = getFrameLowering(MF);
16110b57cec5SDimitry Andric   unsigned StackEst = TFI->determineFrameLayout(MF, true);
16120b57cec5SDimitry Andric 
16130b57cec5SDimitry Andric   // If we likely don't need a stack frame, then we probably don't need a
16140b57cec5SDimitry Andric   // virtual base register either.
16150b57cec5SDimitry Andric   if (!StackEst)
16160b57cec5SDimitry Andric     return false;
16170b57cec5SDimitry Andric 
16180b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
16190b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
16200b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
16210b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
16220b57cec5SDimitry Andric   Offset += StackEst;
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric   // The frame pointer will point to the end of the stack, so estimate the
16250b57cec5SDimitry Andric   // offset as the difference between the object offset and the FP location.
16260b57cec5SDimitry Andric   return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
16270b57cec5SDimitry Andric }
16280b57cec5SDimitry Andric 
16290b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to
16300b57cec5SDimitry Andric /// be a pointer to FrameIdx at the beginning of the basic block.
1631e8d8bef9SDimitry Andric Register PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
16325ffd83dbSDimitry Andric                                                        int FrameIdx,
16330b57cec5SDimitry Andric                                                        int64_t Offset) const {
16340b57cec5SDimitry Andric   unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
16350b57cec5SDimitry Andric 
16360b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
16370b57cec5SDimitry Andric   DebugLoc DL;                  // Defaults to "unknown"
16380b57cec5SDimitry Andric   if (Ins != MBB->end())
16390b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
16400b57cec5SDimitry Andric 
16410b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
16420b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
16430b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
16440b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII.get(ADDriOpc);
16450b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1646e8d8bef9SDimitry Andric   const TargetRegisterClass *RC = getPointerRegClass(MF);
1647e8d8bef9SDimitry Andric   Register BaseReg = MRI.createVirtualRegister(RC);
16480b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
16490b57cec5SDimitry Andric 
16500b57cec5SDimitry Andric   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
16510b57cec5SDimitry Andric     .addFrameIndex(FrameIdx).addImm(Offset);
1652e8d8bef9SDimitry Andric 
1653e8d8bef9SDimitry Andric   return BaseReg;
16540b57cec5SDimitry Andric }
16550b57cec5SDimitry Andric 
16565ffd83dbSDimitry Andric void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
16570b57cec5SDimitry Andric                                         int64_t Offset) const {
16580b57cec5SDimitry Andric   unsigned FIOperandNum = 0;
16590b57cec5SDimitry Andric   while (!MI.getOperand(FIOperandNum).isFI()) {
16600b57cec5SDimitry Andric     ++FIOperandNum;
16610b57cec5SDimitry Andric     assert(FIOperandNum < MI.getNumOperands() &&
16620b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
16630b57cec5SDimitry Andric   }
16640b57cec5SDimitry Andric 
16650b57cec5SDimitry Andric   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
16660b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
16670b57cec5SDimitry Andric   Offset += MI.getOperand(OffsetOperandNo).getImm();
16680b57cec5SDimitry Andric   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
16710b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
16720b57cec5SDimitry Andric   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
16730b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
16740b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
16750b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
16760b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg,
16770b57cec5SDimitry Andric                         TII.getRegClass(MCID, FIOperandNum, this, MF));
16780b57cec5SDimitry Andric }
16790b57cec5SDimitry Andric 
16800b57cec5SDimitry Andric bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
16815ffd83dbSDimitry Andric                                          Register BaseReg,
16820b57cec5SDimitry Andric                                          int64_t Offset) const {
16830b57cec5SDimitry Andric   unsigned FIOperandNum = 0;
16840b57cec5SDimitry Andric   while (!MI->getOperand(FIOperandNum).isFI()) {
16850b57cec5SDimitry Andric     ++FIOperandNum;
16860b57cec5SDimitry Andric     assert(FIOperandNum < MI->getNumOperands() &&
16870b57cec5SDimitry Andric            "Instr doesn't have FrameIndex operand!");
16880b57cec5SDimitry Andric   }
16890b57cec5SDimitry Andric 
16900b57cec5SDimitry Andric   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
16910b57cec5SDimitry Andric   Offset += MI->getOperand(OffsetOperandNo).getImm();
16920b57cec5SDimitry Andric 
16930b57cec5SDimitry Andric   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
16940b57cec5SDimitry Andric          MI->getOpcode() == TargetOpcode::STACKMAP ||
16950b57cec5SDimitry Andric          MI->getOpcode() == TargetOpcode::PATCHPOINT ||
16960b57cec5SDimitry Andric          (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
16970b57cec5SDimitry Andric }
1698