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 78*e8d8bef9SDimitry Andric // Copies/moves of physical accumulators are expensive operations 79*e8d8bef9SDimitry Andric // that should be avoided whenever possible. MMA instructions are 80*e8d8bef9SDimitry Andric // meant to be used in performance-sensitive computational kernels. 81*e8d8bef9SDimitry Andric // This option is provided, at least for the time being, to give the 82*e8d8bef9SDimitry Andric // user a tool to detect this expensive operation and either rework 83*e8d8bef9SDimitry Andric // their code or report a compiler bug if that turns out to be the 84*e8d8bef9SDimitry Andric // cause. 85*e8d8bef9SDimitry Andric #ifndef NDEBUG 86*e8d8bef9SDimitry Andric static cl::opt<bool> 87*e8d8bef9SDimitry Andric ReportAccMoves("ppc-report-acc-moves", 88*e8d8bef9SDimitry Andric cl::desc("Emit information about accumulator register spills " 89*e8d8bef9SDimitry Andric "and copies"), 90*e8d8bef9SDimitry Andric cl::Hidden, cl::init(false)); 91*e8d8bef9SDimitry Andric #endif 92*e8d8bef9SDimitry 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; 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric /// getPointerRegClass - Return the register class to use to hold pointers. 1390b57cec5SDimitry Andric /// This is used for addressing modes. 1400b57cec5SDimitry Andric const TargetRegisterClass * 1410b57cec5SDimitry Andric PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) 1420b57cec5SDimitry Andric const { 1430b57cec5SDimitry Andric // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value 1440b57cec5SDimitry Andric // when it checks for ZERO folding. 1450b57cec5SDimitry Andric if (Kind == 1) { 1460b57cec5SDimitry Andric if (TM.isPPC64()) 1470b57cec5SDimitry Andric return &PPC::G8RC_NOX0RegClass; 1480b57cec5SDimitry Andric return &PPC::GPRC_NOR0RegClass; 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric if (TM.isPPC64()) 1520b57cec5SDimitry Andric return &PPC::G8RCRegClass; 1530b57cec5SDimitry Andric return &PPC::GPRCRegClass; 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric const MCPhysReg* 1570b57cec5SDimitry Andric PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 1580b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>(); 159*e8d8bef9SDimitry Andric if (Subtarget.isAIXABI() && 160*e8d8bef9SDimitry Andric (Subtarget.hasAltivec() && !TM.getAIXExtendedAltivecABI())) 161*e8d8bef9SDimitry Andric report_fatal_error("the default AIX Altivec ABI is not yet " 162*e8d8bef9SDimitry Andric "supported."); 1630b57cec5SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) { 1645ffd83dbSDimitry Andric if (!TM.isPPC64() && Subtarget.isAIXABI()) 1655ffd83dbSDimitry Andric report_fatal_error("AnyReg unimplemented on 32-bit AIX."); 1660b57cec5SDimitry Andric if (Subtarget.hasVSX()) 1670b57cec5SDimitry Andric return CSR_64_AllRegs_VSX_SaveList; 1680b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 1690b57cec5SDimitry Andric return CSR_64_AllRegs_Altivec_SaveList; 1700b57cec5SDimitry Andric return CSR_64_AllRegs_SaveList; 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric // On PPC64, we might need to save r2 (but only if it is not reserved). 1745ffd83dbSDimitry Andric // We do not need to treat R2 as callee-saved when using PC-Relative calls 1755ffd83dbSDimitry Andric // because any direct uses of R2 will cause it to be reserved. If the function 1765ffd83dbSDimitry Andric // is a leaf or the only uses of R2 are implicit uses for calls, the calls 1775ffd83dbSDimitry Andric // will use the @notoc relocation which will cause this function to set the 1785ffd83dbSDimitry Andric // st_other bit to 1, thereby communicating to its caller that it arbitrarily 1795ffd83dbSDimitry Andric // clobbers the TOC. 1805ffd83dbSDimitry Andric bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) && 1815ffd83dbSDimitry Andric !Subtarget.isUsingPCRelativeCalls(); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric // Cold calling convention CSRs. 1840b57cec5SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::Cold) { 1855ffd83dbSDimitry Andric if (Subtarget.isAIXABI()) 1865ffd83dbSDimitry Andric report_fatal_error("Cold calling unimplemented on AIX."); 1870b57cec5SDimitry Andric if (TM.isPPC64()) { 1880b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 1890b57cec5SDimitry Andric return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList 1900b57cec5SDimitry Andric : CSR_SVR64_ColdCC_Altivec_SaveList; 1910b57cec5SDimitry Andric return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList 1920b57cec5SDimitry Andric : CSR_SVR64_ColdCC_SaveList; 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric // 32-bit targets. 1950b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 1960b57cec5SDimitry Andric return CSR_SVR32_ColdCC_Altivec_SaveList; 1970b57cec5SDimitry Andric else if (Subtarget.hasSPE()) 1980b57cec5SDimitry Andric return CSR_SVR32_ColdCC_SPE_SaveList; 1990b57cec5SDimitry Andric return CSR_SVR32_ColdCC_SaveList; 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric // Standard calling convention CSRs. 2020b57cec5SDimitry Andric if (TM.isPPC64()) { 2030b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 2045ffd83dbSDimitry Andric return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList 2055ffd83dbSDimitry Andric : CSR_PPC64_Altivec_SaveList; 2065ffd83dbSDimitry Andric return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric // 32-bit targets. 209*e8d8bef9SDimitry Andric if (Subtarget.isAIXABI()) { 210*e8d8bef9SDimitry Andric if (Subtarget.hasAltivec()) 211*e8d8bef9SDimitry Andric return CSR_AIX32_Altivec_SaveList; 2125ffd83dbSDimitry Andric return CSR_AIX32_SaveList; 213*e8d8bef9SDimitry Andric } 2140b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 2150b57cec5SDimitry Andric return CSR_SVR432_Altivec_SaveList; 2160b57cec5SDimitry Andric else if (Subtarget.hasSPE()) 2170b57cec5SDimitry Andric return CSR_SVR432_SPE_SaveList; 2180b57cec5SDimitry Andric return CSR_SVR432_SaveList; 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric const uint32_t * 2220b57cec5SDimitry Andric PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF, 2230b57cec5SDimitry Andric CallingConv::ID CC) const { 2240b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 2250b57cec5SDimitry Andric if (CC == CallingConv::AnyReg) { 2260b57cec5SDimitry Andric if (Subtarget.hasVSX()) 2270b57cec5SDimitry Andric return CSR_64_AllRegs_VSX_RegMask; 2280b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 2290b57cec5SDimitry Andric return CSR_64_AllRegs_Altivec_RegMask; 2300b57cec5SDimitry Andric return CSR_64_AllRegs_RegMask; 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric if (Subtarget.isAIXABI()) { 234*e8d8bef9SDimitry Andric return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask 235*e8d8bef9SDimitry Andric : CSR_PPC64_RegMask) 236*e8d8bef9SDimitry Andric : (Subtarget.hasAltivec() ? CSR_AIX32_Altivec_RegMask 237*e8d8bef9SDimitry Andric : CSR_AIX32_RegMask); 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric if (CC == CallingConv::Cold) { 2410b57cec5SDimitry Andric return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask 2420b57cec5SDimitry Andric : CSR_SVR64_ColdCC_RegMask) 2430b57cec5SDimitry Andric : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask 2440b57cec5SDimitry Andric : (Subtarget.hasSPE() 2450b57cec5SDimitry Andric ? CSR_SVR32_ColdCC_SPE_RegMask 2460b57cec5SDimitry Andric : CSR_SVR32_ColdCC_RegMask)); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2495ffd83dbSDimitry Andric return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask 2505ffd83dbSDimitry Andric : CSR_PPC64_RegMask) 2515ffd83dbSDimitry Andric : (Subtarget.hasAltivec() 2525ffd83dbSDimitry Andric ? CSR_SVR432_Altivec_RegMask 2535ffd83dbSDimitry Andric : (Subtarget.hasSPE() ? CSR_SVR432_SPE_RegMask 2540b57cec5SDimitry Andric : CSR_SVR432_RegMask)); 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric const uint32_t* 2580b57cec5SDimitry Andric PPCRegisterInfo::getNoPreservedMask() const { 2590b57cec5SDimitry Andric return CSR_NoRegs_RegMask; 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const { 2630b57cec5SDimitry Andric for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM}) 2640b57cec5SDimitry Andric Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32)); 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 2680b57cec5SDimitry Andric BitVector Reserved(getNumRegs()); 2690b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 2700b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric // The ZERO register is not really a register, but the representation of r0 2730b57cec5SDimitry Andric // when used in instructions that treat r0 as the constant 0. 2740b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::ZERO); 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric // The FP register is also not really a register, but is the representation 2770b57cec5SDimitry Andric // of the frame pointer register used by ISD::FRAMEADDR. 2780b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::FP); 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric // The BP register is also not really a register, but is the representation 2810b57cec5SDimitry Andric // of the base pointer register used by setjmp. 2820b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::BP); 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric // The counter registers must be reserved so that counter-based loops can 2850b57cec5SDimitry Andric // be correctly formed (and the mtctr instructions are not DCE'd). 2860b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::CTR); 2870b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::CTR8); 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R1); 2900b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::LR); 2910b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::LR8); 2920b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::RM); 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::VRSAVE); 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // The SVR4 ABI reserves r2 and r13 2970b57cec5SDimitry Andric if (Subtarget.isSVR4ABI()) { 2980b57cec5SDimitry Andric // We only reserve r2 if we need to use the TOC pointer. If we have no 2990b57cec5SDimitry Andric // explicit uses of the TOC pointer (meaning we're a leaf function with 3000b57cec5SDimitry Andric // no constant-pool loads, etc.) and we have no potential uses inside an 3010b57cec5SDimitry Andric // inline asm block, then we can treat r2 has an ordinary callee-saved 3020b57cec5SDimitry Andric // register. 3030b57cec5SDimitry Andric const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 3040b57cec5SDimitry Andric if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm()) 3050b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R2); // System-reserved register 3060b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric // Always reserve r2 on AIX for now. 3100b57cec5SDimitry Andric // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions. 3110b57cec5SDimitry Andric if (Subtarget.isAIXABI()) 3120b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R2); // System-reserved register 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric // On PPC64, r13 is the thread pointer. Never allocate this register. 3150b57cec5SDimitry Andric if (TM.isPPC64()) 3160b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R13); 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric if (TFI->needsFP(MF)) 3190b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R31); 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric bool IsPositionIndependent = TM.isPositionIndependent(); 3220b57cec5SDimitry Andric if (hasBasePointer(MF)) { 3238bcb0991SDimitry Andric if (Subtarget.is32BitELFABI() && IsPositionIndependent) 3240b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R29); 3250b57cec5SDimitry Andric else 3260b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R30); 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3298bcb0991SDimitry Andric if (Subtarget.is32BitELFABI() && IsPositionIndependent) 3300b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R30); 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric // Reserve Altivec registers when Altivec is unavailable. 3330b57cec5SDimitry Andric if (!Subtarget.hasAltivec()) 3340b57cec5SDimitry Andric for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(), 3350b57cec5SDimitry Andric IE = PPC::VRRCRegClass.end(); I != IE; ++I) 3360b57cec5SDimitry Andric markSuperRegs(Reserved, *I); 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric assert(checkAllSuperRegsMarked(Reserved)); 3390b57cec5SDimitry Andric return Reserved; 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const { 3430b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 3440b57cec5SDimitry Andric const PPCInstrInfo *InstrInfo = Subtarget.getInstrInfo(); 3450b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 3460b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo(); 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric // If the callee saved info is invalid we have to default to true for safety. 3490b57cec5SDimitry Andric if (!MFI.isCalleeSavedInfoValid()) 3500b57cec5SDimitry Andric return true; 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric // We will require the use of X-Forms because the frame is larger than what 3530b57cec5SDimitry Andric // can be represented in signed 16 bits that fit in the immediate of a D-Form. 3540b57cec5SDimitry Andric // If we need an X-Form then we need a register to store the address offset. 3550b57cec5SDimitry Andric unsigned FrameSize = MFI.getStackSize(); 3560b57cec5SDimitry Andric // Signed 16 bits means that the FrameSize cannot be more than 15 bits. 3570b57cec5SDimitry Andric if (FrameSize & ~0x7FFF) 3580b57cec5SDimitry Andric return true; 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric // The callee saved info is valid so it can be traversed. 3610b57cec5SDimitry Andric // Checking for registers that need saving that do not have load or store 3620b57cec5SDimitry Andric // forms where the address offset is an immediate. 3630b57cec5SDimitry Andric for (unsigned i = 0; i < Info.size(); i++) { 3640b57cec5SDimitry Andric int FrIdx = Info[i].getFrameIdx(); 3650b57cec5SDimitry Andric unsigned Reg = Info[i].getReg(); 3660b57cec5SDimitry Andric 3675ffd83dbSDimitry Andric const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg); 3685ffd83dbSDimitry Andric unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC); 3690b57cec5SDimitry Andric if (!MFI.isFixedObjectIndex(FrIdx)) { 3700b57cec5SDimitry Andric // This is not a fixed object. If it requires alignment then we may still 3710b57cec5SDimitry Andric // need to use the XForm. 3720b57cec5SDimitry Andric if (offsetMinAlignForOpcode(Opcode) > 1) 3730b57cec5SDimitry Andric return true; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric // This is eiher: 3770b57cec5SDimitry Andric // 1) A fixed frame index object which we know are aligned so 3780b57cec5SDimitry Andric // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't 379480093f4SDimitry Andric // need to consider the alignment here. 3800b57cec5SDimitry Andric // 2) A not fixed object but in that case we now know that the min required 3810b57cec5SDimitry Andric // alignment is no more than 1 based on the previous check. 3820b57cec5SDimitry Andric if (InstrInfo->isXFormMemOp(Opcode)) 3830b57cec5SDimitry Andric return true; 3840b57cec5SDimitry Andric } 3850b57cec5SDimitry Andric return false; 3860b57cec5SDimitry Andric } 3870b57cec5SDimitry Andric 3885ffd83dbSDimitry Andric bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg, 3890b57cec5SDimitry Andric const MachineFunction &MF) const { 3908bcb0991SDimitry Andric assert(Register::isPhysicalRegister(PhysReg)); 3910b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 3920b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 3930b57cec5SDimitry Andric if (!TM.isPPC64()) 3940b57cec5SDimitry Andric return false; 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric if (!Subtarget.isSVR4ABI()) 3970b57cec5SDimitry Andric return false; 3980b57cec5SDimitry Andric if (PhysReg == PPC::X2) 3990b57cec5SDimitry Andric // X2 is guaranteed to be preserved within a function if it is reserved. 4000b57cec5SDimitry Andric // The reason it's reserved is that it's the TOC pointer (and the function 4010b57cec5SDimitry Andric // uses the TOC). In functions where it isn't reserved (i.e. leaf functions 4020b57cec5SDimitry Andric // with no TOC access), we can't claim that it is preserved. 4030b57cec5SDimitry Andric return (getReservedRegs(MF).test(PPC::X2)); 4040b57cec5SDimitry Andric if (StackPtrConst && (PhysReg == PPC::X1) && !MFI.hasVarSizedObjects() 4050b57cec5SDimitry Andric && !MFI.hasOpaqueSPAdjustment()) 4060b57cec5SDimitry Andric // The value of the stack pointer does not change within a function after 4070b57cec5SDimitry Andric // the prologue and before the epilogue if there are no dynamic allocations 4080b57cec5SDimitry Andric // and no inline asm which clobbers X1. 4090b57cec5SDimitry Andric return true; 4100b57cec5SDimitry Andric return false; 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 4140b57cec5SDimitry Andric MachineFunction &MF) const { 4150b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 4160b57cec5SDimitry Andric const unsigned DefaultSafety = 1; 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric switch (RC->getID()) { 4190b57cec5SDimitry Andric default: 4200b57cec5SDimitry Andric return 0; 4210b57cec5SDimitry Andric case PPC::G8RC_NOX0RegClassID: 4220b57cec5SDimitry Andric case PPC::GPRC_NOR0RegClassID: 4230b57cec5SDimitry Andric case PPC::SPERCRegClassID: 4240b57cec5SDimitry Andric case PPC::G8RCRegClassID: 4250b57cec5SDimitry Andric case PPC::GPRCRegClassID: { 4260b57cec5SDimitry Andric unsigned FP = TFI->hasFP(MF) ? 1 : 0; 4270b57cec5SDimitry Andric return 32 - FP - DefaultSafety; 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric case PPC::F8RCRegClassID: 4300b57cec5SDimitry Andric case PPC::F4RCRegClassID: 4310b57cec5SDimitry Andric case PPC::VRRCRegClassID: 4320b57cec5SDimitry Andric case PPC::VFRCRegClassID: 4330b57cec5SDimitry Andric case PPC::VSLRCRegClassID: 4340b57cec5SDimitry Andric return 32 - DefaultSafety; 4350b57cec5SDimitry Andric case PPC::VSRCRegClassID: 4360b57cec5SDimitry Andric case PPC::VSFRCRegClassID: 4370b57cec5SDimitry Andric case PPC::VSSRCRegClassID: 4380b57cec5SDimitry Andric return 64 - DefaultSafety; 4390b57cec5SDimitry Andric case PPC::CRRCRegClassID: 4400b57cec5SDimitry Andric return 8 - DefaultSafety; 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric const TargetRegisterClass * 4450b57cec5SDimitry Andric PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 4460b57cec5SDimitry Andric const MachineFunction &MF) const { 4470b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 4480b57cec5SDimitry Andric if (Subtarget.hasVSX()) { 4490b57cec5SDimitry Andric // With VSX, we can inflate various sub-register classes to the full VSX 4500b57cec5SDimitry Andric // register set. 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric // For Power9 we allow the user to enable GPR to vector spills. 4530b57cec5SDimitry Andric // FIXME: Currently limited to spilling GP8RC. A follow on patch will add 4540b57cec5SDimitry Andric // support to spill GPRC. 4550b57cec5SDimitry Andric if (TM.isELFv2ABI()) { 4560b57cec5SDimitry Andric if (Subtarget.hasP9Vector() && EnableGPRToVecSpills && 4570b57cec5SDimitry Andric RC == &PPC::G8RCRegClass) { 4580b57cec5SDimitry Andric InflateGP8RC++; 4590b57cec5SDimitry Andric return &PPC::SPILLTOVSRRCRegClass; 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills) 4620b57cec5SDimitry Andric InflateGPRC++; 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric if (RC == &PPC::F8RCRegClass) 4650b57cec5SDimitry Andric return &PPC::VSFRCRegClass; 4660b57cec5SDimitry Andric else if (RC == &PPC::VRRCRegClass) 4670b57cec5SDimitry Andric return &PPC::VSRCRegClass; 4680b57cec5SDimitry Andric else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector()) 4690b57cec5SDimitry Andric return &PPC::VSSRCRegClass; 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF); 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4760b57cec5SDimitry Andric // Stack Frame Processing methods 4770b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric /// lowerDynamicAlloc - Generate the code for allocating an object in the 4800b57cec5SDimitry Andric /// current frame. The sequence of code will be in the general form 4810b57cec5SDimitry Andric /// 4820b57cec5SDimitry Andric /// addi R0, SP, \#frameSize ; get the address of the previous frame 4830b57cec5SDimitry Andric /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size 4840b57cec5SDimitry Andric /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation 4850b57cec5SDimitry Andric /// 4860b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const { 4870b57cec5SDimitry Andric // Get the instruction. 4880b57cec5SDimitry Andric MachineInstr &MI = *II; 4890b57cec5SDimitry Andric // Get the instruction's basic block. 4900b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 4910b57cec5SDimitry Andric // Get the basic block's function. 4920b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 4930b57cec5SDimitry Andric // Get the frame info. 4940b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 4950b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 4960b57cec5SDimitry Andric // Get the instruction info. 4970b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 4980b57cec5SDimitry Andric // Determine whether 64-bit pointers are used. 4990b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 5000b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric // Get the maximum call stack size. 5030b57cec5SDimitry Andric unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 5045ffd83dbSDimitry Andric Align MaxAlign = MFI.getMaxAlign(); 5055ffd83dbSDimitry Andric assert(isAligned(MaxAlign, maxCallFrameSize) && 5060b57cec5SDimitry Andric "Maximum call-frame size not sufficiently aligned"); 5075ffd83dbSDimitry Andric (void)MaxAlign; 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 5100b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 5118bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 5120b57cec5SDimitry Andric bool KillNegSizeReg = MI.getOperand(1).isKill(); 5138bcb0991SDimitry Andric Register NegSizeReg = MI.getOperand(1).getReg(); 5140b57cec5SDimitry Andric 5155ffd83dbSDimitry Andric prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg); 5160b57cec5SDimitry Andric // Grow the stack and update the stack pointer link, then determine the 5170b57cec5SDimitry Andric // address of new allocated space. 5180b57cec5SDimitry Andric if (LP64) { 5190b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1) 5200b57cec5SDimitry Andric .addReg(Reg, RegState::Kill) 5210b57cec5SDimitry Andric .addReg(PPC::X1) 5220b57cec5SDimitry Andric .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 5230b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 5240b57cec5SDimitry Andric .addReg(PPC::X1) 5250b57cec5SDimitry Andric .addImm(maxCallFrameSize); 5260b57cec5SDimitry Andric } else { 5270b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1) 5280b57cec5SDimitry Andric .addReg(Reg, RegState::Kill) 5290b57cec5SDimitry Andric .addReg(PPC::R1) 5300b57cec5SDimitry Andric .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 5310b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 5320b57cec5SDimitry Andric .addReg(PPC::R1) 5330b57cec5SDimitry Andric .addImm(maxCallFrameSize); 5340b57cec5SDimitry Andric } 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric // Discard the DYNALLOC instruction. 5370b57cec5SDimitry Andric MBB.erase(II); 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5405ffd83dbSDimitry Andric /// To accomplish dynamic stack allocation, we have to calculate exact size 5415ffd83dbSDimitry Andric /// subtracted from the stack pointer according alignment information and get 5425ffd83dbSDimitry Andric /// previous frame pointer. 5435ffd83dbSDimitry Andric void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II, 5445ffd83dbSDimitry Andric Register &NegSizeReg, 5455ffd83dbSDimitry Andric bool &KillNegSizeReg, 5465ffd83dbSDimitry Andric Register &FramePointer) const { 5475ffd83dbSDimitry Andric // Get the instruction. 5485ffd83dbSDimitry Andric MachineInstr &MI = *II; 5495ffd83dbSDimitry Andric // Get the instruction's basic block. 5505ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 5515ffd83dbSDimitry Andric // Get the basic block's function. 5525ffd83dbSDimitry Andric MachineFunction &MF = *MBB.getParent(); 5535ffd83dbSDimitry Andric // Get the frame info. 5545ffd83dbSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 5555ffd83dbSDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 5565ffd83dbSDimitry Andric // Get the instruction info. 5575ffd83dbSDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 5585ffd83dbSDimitry Andric // Determine whether 64-bit pointers are used. 5595ffd83dbSDimitry Andric bool LP64 = TM.isPPC64(); 5605ffd83dbSDimitry Andric DebugLoc dl = MI.getDebugLoc(); 5615ffd83dbSDimitry Andric // Get the total frame size. 5625ffd83dbSDimitry Andric unsigned FrameSize = MFI.getStackSize(); 5635ffd83dbSDimitry Andric 5645ffd83dbSDimitry Andric // Get stack alignments. 5655ffd83dbSDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 5665ffd83dbSDimitry Andric Align TargetAlign = TFI->getStackAlign(); 5675ffd83dbSDimitry Andric Align MaxAlign = MFI.getMaxAlign(); 5685ffd83dbSDimitry Andric 5695ffd83dbSDimitry Andric // Determine the previous frame's address. If FrameSize can't be 5705ffd83dbSDimitry Andric // represented as 16 bits or we need special alignment, then we load the 5715ffd83dbSDimitry Andric // previous frame's address from 0(SP). Why not do an addis of the hi? 5725ffd83dbSDimitry Andric // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 5735ffd83dbSDimitry Andric // Constructing the constant and adding would take 3 instructions. 5745ffd83dbSDimitry Andric // Fortunately, a frame greater than 32K is rare. 5755ffd83dbSDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 5765ffd83dbSDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 5775ffd83dbSDimitry Andric 5785ffd83dbSDimitry Andric if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 5795ffd83dbSDimitry Andric if (LP64) 5805ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer) 5815ffd83dbSDimitry Andric .addReg(PPC::X31) 5825ffd83dbSDimitry Andric .addImm(FrameSize); 5835ffd83dbSDimitry Andric else 5845ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer) 5855ffd83dbSDimitry Andric .addReg(PPC::R31) 5865ffd83dbSDimitry Andric .addImm(FrameSize); 5875ffd83dbSDimitry Andric } else if (LP64) { 5885ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer) 5895ffd83dbSDimitry Andric .addImm(0) 5905ffd83dbSDimitry Andric .addReg(PPC::X1); 5915ffd83dbSDimitry Andric } else { 5925ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer) 5935ffd83dbSDimitry Andric .addImm(0) 5945ffd83dbSDimitry Andric .addReg(PPC::R1); 5955ffd83dbSDimitry Andric } 5965ffd83dbSDimitry Andric // Determine the actual NegSizeReg according to alignment info. 5975ffd83dbSDimitry Andric if (LP64) { 5985ffd83dbSDimitry Andric if (MaxAlign > TargetAlign) { 5995ffd83dbSDimitry Andric unsigned UnalNegSizeReg = NegSizeReg; 6005ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 6015ffd83dbSDimitry Andric 6025ffd83dbSDimitry Andric // Unfortunately, there is no andi, only andi., and we can't insert that 6035ffd83dbSDimitry Andric // here because we might clobber cr0 while it is live. 6045ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg) 6055ffd83dbSDimitry Andric .addImm(~(MaxAlign.value() - 1)); 6065ffd83dbSDimitry Andric 6075ffd83dbSDimitry Andric unsigned NegSizeReg1 = NegSizeReg; 6085ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 6095ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg) 6105ffd83dbSDimitry Andric .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 6115ffd83dbSDimitry Andric .addReg(NegSizeReg1, RegState::Kill); 6125ffd83dbSDimitry Andric KillNegSizeReg = true; 6135ffd83dbSDimitry Andric } 6145ffd83dbSDimitry Andric } else { 6155ffd83dbSDimitry Andric if (MaxAlign > TargetAlign) { 6165ffd83dbSDimitry Andric unsigned UnalNegSizeReg = NegSizeReg; 6175ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 6185ffd83dbSDimitry Andric 6195ffd83dbSDimitry Andric // Unfortunately, there is no andi, only andi., and we can't insert that 6205ffd83dbSDimitry Andric // here because we might clobber cr0 while it is live. 6215ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg) 6225ffd83dbSDimitry Andric .addImm(~(MaxAlign.value() - 1)); 6235ffd83dbSDimitry Andric 6245ffd83dbSDimitry Andric unsigned NegSizeReg1 = NegSizeReg; 6255ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 6265ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg) 6275ffd83dbSDimitry Andric .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 6285ffd83dbSDimitry Andric .addReg(NegSizeReg1, RegState::Kill); 6295ffd83dbSDimitry Andric KillNegSizeReg = true; 6305ffd83dbSDimitry Andric } 6315ffd83dbSDimitry Andric } 6325ffd83dbSDimitry Andric } 6335ffd83dbSDimitry Andric 6345ffd83dbSDimitry Andric void PPCRegisterInfo::lowerPrepareProbedAlloca( 6355ffd83dbSDimitry Andric MachineBasicBlock::iterator II) const { 6365ffd83dbSDimitry Andric MachineInstr &MI = *II; 6375ffd83dbSDimitry Andric // Get the instruction's basic block. 6385ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 6395ffd83dbSDimitry Andric // Get the basic block's function. 6405ffd83dbSDimitry Andric MachineFunction &MF = *MBB.getParent(); 6415ffd83dbSDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 6425ffd83dbSDimitry Andric // Get the instruction info. 6435ffd83dbSDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 6445ffd83dbSDimitry Andric // Determine whether 64-bit pointers are used. 6455ffd83dbSDimitry Andric bool LP64 = TM.isPPC64(); 6465ffd83dbSDimitry Andric DebugLoc dl = MI.getDebugLoc(); 6475ffd83dbSDimitry Andric Register FramePointer = MI.getOperand(0).getReg(); 648590d96feSDimitry Andric const Register ActualNegSizeReg = MI.getOperand(1).getReg(); 6495ffd83dbSDimitry Andric bool KillNegSizeReg = MI.getOperand(2).isKill(); 6505ffd83dbSDimitry Andric Register NegSizeReg = MI.getOperand(2).getReg(); 651590d96feSDimitry Andric const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR); 652590d96feSDimitry Andric // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg. 653590d96feSDimitry Andric if (FramePointer == NegSizeReg) { 654590d96feSDimitry Andric assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, " 655590d96feSDimitry Andric "NegSizeReg should be killed"); 656590d96feSDimitry Andric // FramePointer is clobbered earlier than the use of NegSizeReg in 657590d96feSDimitry Andric // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid 658590d96feSDimitry Andric // misuse. 659590d96feSDimitry Andric BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg) 660590d96feSDimitry Andric .addReg(NegSizeReg) 661590d96feSDimitry Andric .addReg(NegSizeReg); 662590d96feSDimitry Andric NegSizeReg = ActualNegSizeReg; 663590d96feSDimitry Andric KillNegSizeReg = false; 6645ffd83dbSDimitry Andric } 665590d96feSDimitry Andric prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer); 666590d96feSDimitry Andric // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign > 667590d96feSDimitry Andric // TargetAlign. 668590d96feSDimitry Andric if (NegSizeReg != ActualNegSizeReg) 669590d96feSDimitry Andric BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg) 670590d96feSDimitry Andric .addReg(NegSizeReg) 671590d96feSDimitry Andric .addReg(NegSizeReg); 6725ffd83dbSDimitry Andric MBB.erase(II); 6735ffd83dbSDimitry Andric } 6745ffd83dbSDimitry Andric 6750b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAreaOffset( 6760b57cec5SDimitry Andric MachineBasicBlock::iterator II) const { 6770b57cec5SDimitry Andric // Get the instruction. 6780b57cec5SDimitry Andric MachineInstr &MI = *II; 6790b57cec5SDimitry Andric // Get the instruction's basic block. 6800b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 6810b57cec5SDimitry Andric // Get the basic block's function. 6820b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6830b57cec5SDimitry Andric // Get the frame info. 6840b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 6850b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 6860b57cec5SDimitry Andric // Get the instruction info. 6870b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 6900b57cec5SDimitry Andric bool is64Bit = TM.isPPC64(); 6910b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 6920b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), 6930b57cec5SDimitry Andric MI.getOperand(0).getReg()) 6940b57cec5SDimitry Andric .addImm(maxCallFrameSize); 6950b57cec5SDimitry Andric MBB.erase(II); 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 6990b57cec5SDimitry Andric /// reserving a whole register (R0), we scrounge for one here. This generates 7000b57cec5SDimitry Andric /// code like this: 7010b57cec5SDimitry Andric /// 7020b57cec5SDimitry Andric /// mfcr rA ; Move the conditional register into GPR rA. 7030b57cec5SDimitry Andric /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 7040b57cec5SDimitry Andric /// stw rA, FI ; Store rA to the frame. 7050b57cec5SDimitry Andric /// 7060b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 7070b57cec5SDimitry Andric unsigned FrameIndex) const { 7080b57cec5SDimitry Andric // Get the instruction. 7090b57cec5SDimitry Andric MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 7100b57cec5SDimitry Andric // Get the instruction's basic block. 7110b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 7120b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 7130b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 7140b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 7150b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 7180b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 7190b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 7200b57cec5SDimitry Andric 7218bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7228bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric // We need to store the CR in the low 4-bits of the saved value. First, issue 7250b57cec5SDimitry Andric // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg. 7260b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 7270b57cec5SDimitry Andric .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric // If the saved register wasn't CR0, shift the bits left so that they are in 7300b57cec5SDimitry Andric // CR0's slot. 7310b57cec5SDimitry Andric if (SrcReg != PPC::CR0) { 7325ffd83dbSDimitry Andric Register Reg1 = Reg; 7330b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric // rlwinm rA, rA, ShiftBits, 0, 31. 7360b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 7370b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill) 7380b57cec5SDimitry Andric .addImm(getEncodingValue(SrcReg) * 4) 7390b57cec5SDimitry Andric .addImm(0) 7400b57cec5SDimitry Andric .addImm(31); 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 7440b57cec5SDimitry Andric .addReg(Reg, RegState::Kill), 7450b57cec5SDimitry Andric FrameIndex); 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric // Discard the pseudo instruction. 7480b57cec5SDimitry Andric MBB.erase(II); 7490b57cec5SDimitry Andric } 7500b57cec5SDimitry Andric 7510b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 7520b57cec5SDimitry Andric unsigned FrameIndex) const { 7530b57cec5SDimitry Andric // Get the instruction. 7540b57cec5SDimitry Andric MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 7550b57cec5SDimitry Andric // Get the instruction's basic block. 7560b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 7570b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 7580b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 7590b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 7600b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 7630b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 7640b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 7650b57cec5SDimitry Andric 7668bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7678bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 7680b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 7690b57cec5SDimitry Andric "RESTORE_CR does not define its destination"); 7700b57cec5SDimitry Andric 7710b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 7720b57cec5SDimitry Andric Reg), FrameIndex); 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric // If the reloaded register isn't CR0, shift the bits right so that they are 7750b57cec5SDimitry Andric // in the right CR's slot. 7760b57cec5SDimitry Andric if (DestReg != PPC::CR0) { 7775ffd83dbSDimitry Andric Register Reg1 = Reg; 7780b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric unsigned ShiftBits = getEncodingValue(DestReg)*4; 7810b57cec5SDimitry Andric // rlwinm r11, r11, 32-ShiftBits, 0, 31. 7820b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 7830b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0) 7840b57cec5SDimitry Andric .addImm(31); 7850b57cec5SDimitry Andric } 7860b57cec5SDimitry Andric 7870b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg) 7880b57cec5SDimitry Andric .addReg(Reg, RegState::Kill); 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric // Discard the pseudo instruction. 7910b57cec5SDimitry Andric MBB.erase(II); 7920b57cec5SDimitry Andric } 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II, 7950b57cec5SDimitry Andric unsigned FrameIndex) const { 7960b57cec5SDimitry Andric // Get the instruction. 7970b57cec5SDimitry Andric MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset> 7980b57cec5SDimitry Andric // Get the instruction's basic block. 7990b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 8000b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 8010b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 8020b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 8030b57cec5SDimitry Andric const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo(); 8040b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 8070b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 8080b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 8090b57cec5SDimitry Andric 8108bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 8118bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric // Search up the BB to find the definition of the CR bit. 814480093f4SDimitry Andric MachineBasicBlock::reverse_iterator Ins = MI; 815480093f4SDimitry Andric MachineBasicBlock::reverse_iterator Rend = MBB.rend(); 816480093f4SDimitry Andric ++Ins; 8170b57cec5SDimitry Andric unsigned CRBitSpillDistance = 0; 818480093f4SDimitry Andric bool SeenUse = false; 819480093f4SDimitry Andric for (; Ins != Rend; ++Ins) { 8200b57cec5SDimitry Andric // Definition found. 8210b57cec5SDimitry Andric if (Ins->modifiesRegister(SrcReg, TRI)) 8220b57cec5SDimitry Andric break; 823480093f4SDimitry Andric // Use found. 824480093f4SDimitry Andric if (Ins->readsRegister(SrcReg, TRI)) 825480093f4SDimitry Andric SeenUse = true; 8260b57cec5SDimitry Andric // Unable to find CR bit definition within maximum search distance. 8270b57cec5SDimitry Andric if (CRBitSpillDistance == MaxCRBitSpillDist) { 8280b57cec5SDimitry Andric Ins = MI; 8290b57cec5SDimitry Andric break; 8300b57cec5SDimitry Andric } 8310b57cec5SDimitry Andric // Skip debug instructions when counting CR bit spill distance. 8320b57cec5SDimitry Andric if (!Ins->isDebugInstr()) 8330b57cec5SDimitry Andric CRBitSpillDistance++; 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric // Unable to find the definition of the CR bit in the MBB. 8370b57cec5SDimitry Andric if (Ins == MBB.rend()) 8380b57cec5SDimitry Andric Ins = MI; 8390b57cec5SDimitry Andric 840480093f4SDimitry Andric bool SpillsKnownBit = false; 8410b57cec5SDimitry Andric // There is no need to extract the CR bit if its value is already known. 8420b57cec5SDimitry Andric switch (Ins->getOpcode()) { 8430b57cec5SDimitry Andric case PPC::CRUNSET: 8440b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg) 8450b57cec5SDimitry Andric .addImm(0); 846480093f4SDimitry Andric SpillsKnownBit = true; 8470b57cec5SDimitry Andric break; 8480b57cec5SDimitry Andric case PPC::CRSET: 8490b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg) 8500b57cec5SDimitry Andric .addImm(-32768); 851480093f4SDimitry Andric SpillsKnownBit = true; 8520b57cec5SDimitry Andric break; 8530b57cec5SDimitry Andric default: 854*e8d8bef9SDimitry Andric // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all 855*e8d8bef9SDimitry Andric // bits (specifically, it produces a -1 if the CR bit is set). Ultimately, 856*e8d8bef9SDimitry Andric // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit 857*e8d8bef9SDimitry Andric // register), and SETNBC will set this. 858*e8d8bef9SDimitry Andric if (Subtarget.isISA3_1()) { 859*e8d8bef9SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg) 860*e8d8bef9SDimitry Andric .addReg(SrcReg, RegState::Undef); 861*e8d8bef9SDimitry Andric break; 862*e8d8bef9SDimitry Andric } 863*e8d8bef9SDimitry Andric 864480093f4SDimitry Andric // On Power9, we can use SETB to extract the LT bit. This only works for 865480093f4SDimitry Andric // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value 866480093f4SDimitry Andric // of the bit we care about (32-bit sign bit) will be set to the value of 867480093f4SDimitry Andric // the LT bit (regardless of the other bits in the CR field). 868480093f4SDimitry Andric if (Subtarget.isISA3_0()) { 869480093f4SDimitry Andric if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT || 870480093f4SDimitry Andric SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT || 871480093f4SDimitry Andric SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT || 872480093f4SDimitry Andric SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) { 873480093f4SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg) 874480093f4SDimitry Andric .addReg(getCRFromCRBit(SrcReg), RegState::Undef); 875480093f4SDimitry Andric break; 876480093f4SDimitry Andric } 877480093f4SDimitry Andric } 878480093f4SDimitry Andric 8790b57cec5SDimitry Andric // We need to move the CR field that contains the CR bit we are spilling. 8800b57cec5SDimitry Andric // The super register may not be explicitly defined (i.e. it can be defined 8810b57cec5SDimitry Andric // by a CR-logical that only defines the subreg) so we state that the CR 8820b57cec5SDimitry Andric // field is undef. Also, in order to preserve the kill flag on the CR bit, 8830b57cec5SDimitry Andric // we add it as an implicit use. 8840b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 8850b57cec5SDimitry Andric .addReg(getCRFromCRBit(SrcReg), RegState::Undef) 8860b57cec5SDimitry Andric .addReg(SrcReg, 8870b57cec5SDimitry Andric RegState::Implicit | getKillRegState(MI.getOperand(0).isKill())); 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric // If the saved register wasn't CR0LT, shift the bits left so that the bit 8900b57cec5SDimitry Andric // to store is the first one. Mask all but that bit. 8915ffd83dbSDimitry Andric Register Reg1 = Reg; 8920b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric // rlwinm rA, rA, ShiftBits, 0, 0. 8950b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 8960b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill) 8970b57cec5SDimitry Andric .addImm(getEncodingValue(SrcReg)) 8980b57cec5SDimitry Andric .addImm(0).addImm(0); 8990b57cec5SDimitry Andric } 9000b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 9010b57cec5SDimitry Andric .addReg(Reg, RegState::Kill), 9020b57cec5SDimitry Andric FrameIndex); 9030b57cec5SDimitry Andric 904480093f4SDimitry Andric bool KillsCRBit = MI.killsRegister(SrcReg, TRI); 9050b57cec5SDimitry Andric // Discard the pseudo instruction. 9060b57cec5SDimitry Andric MBB.erase(II); 907480093f4SDimitry Andric if (SpillsKnownBit && KillsCRBit && !SeenUse) { 908480093f4SDimitry Andric Ins->setDesc(TII.get(PPC::UNENCODED_NOP)); 909480093f4SDimitry Andric Ins->RemoveOperand(0); 910480093f4SDimitry Andric } 9110b57cec5SDimitry Andric } 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, 9140b57cec5SDimitry Andric unsigned FrameIndex) const { 9150b57cec5SDimitry Andric // Get the instruction. 9160b57cec5SDimitry Andric MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset> 9170b57cec5SDimitry Andric // Get the instruction's basic block. 9180b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 9190b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 9200b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 9210b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 9220b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 9250b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 9260b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 9270b57cec5SDimitry Andric 9288bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 9298bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 9300b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 9310b57cec5SDimitry Andric "RESTORE_CRBIT does not define its destination"); 9320b57cec5SDimitry Andric 9330b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 9340b57cec5SDimitry Andric Reg), FrameIndex); 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg); 9370b57cec5SDimitry Andric 9388bcb0991SDimitry Andric Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 9390b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO) 9400b57cec5SDimitry Andric .addReg(getCRFromCRBit(DestReg)); 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric unsigned ShiftBits = getEncodingValue(DestReg); 9430b57cec5SDimitry Andric // rlwimi r11, r10, 32-ShiftBits, ..., ... 9440b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO) 9450b57cec5SDimitry Andric .addReg(RegO, RegState::Kill) 9460b57cec5SDimitry Andric .addReg(Reg, RegState::Kill) 9470b57cec5SDimitry Andric .addImm(ShiftBits ? 32 - ShiftBits : 0) 9480b57cec5SDimitry Andric .addImm(ShiftBits) 9490b57cec5SDimitry Andric .addImm(ShiftBits); 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), 9520b57cec5SDimitry Andric getCRFromCRBit(DestReg)) 9530b57cec5SDimitry Andric .addReg(RegO, RegState::Kill) 9540b57cec5SDimitry Andric // Make sure we have a use dependency all the way through this 9550b57cec5SDimitry Andric // sequence of instructions. We can't have the other bits in the CR 9560b57cec5SDimitry Andric // modified in between the mfocrf and the mtocrf. 9570b57cec5SDimitry Andric .addReg(getCRFromCRBit(DestReg), RegState::Implicit); 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric // Discard the pseudo instruction. 9600b57cec5SDimitry Andric MBB.erase(II); 9610b57cec5SDimitry Andric } 9620b57cec5SDimitry Andric 963*e8d8bef9SDimitry Andric void PPCRegisterInfo::emitAccCopyInfo(MachineBasicBlock &MBB, 964*e8d8bef9SDimitry Andric MCRegister DestReg, MCRegister SrcReg) { 965*e8d8bef9SDimitry Andric #ifdef NDEBUG 966*e8d8bef9SDimitry Andric return; 967*e8d8bef9SDimitry Andric #else 968*e8d8bef9SDimitry Andric if (ReportAccMoves) { 969*e8d8bef9SDimitry Andric std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc"; 970*e8d8bef9SDimitry Andric std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc"; 971*e8d8bef9SDimitry Andric dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n"; 972*e8d8bef9SDimitry Andric MBB.dump(); 973*e8d8bef9SDimitry Andric } 974*e8d8bef9SDimitry Andric #endif 975*e8d8bef9SDimitry Andric } 976*e8d8bef9SDimitry Andric 977*e8d8bef9SDimitry Andric static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed, 978*e8d8bef9SDimitry Andric bool IsRestore) { 979*e8d8bef9SDimitry Andric #ifdef NDEBUG 980*e8d8bef9SDimitry Andric return; 981*e8d8bef9SDimitry Andric #else 982*e8d8bef9SDimitry Andric if (ReportAccMoves) { 983*e8d8bef9SDimitry Andric dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register " 984*e8d8bef9SDimitry Andric << (IsRestore ? "restore" : "spill") << ":\n"; 985*e8d8bef9SDimitry Andric MBB.dump(); 986*e8d8bef9SDimitry Andric } 987*e8d8bef9SDimitry Andric #endif 988*e8d8bef9SDimitry Andric } 989*e8d8bef9SDimitry Andric 990*e8d8bef9SDimitry Andric /// lowerACCSpilling - Generate the code for spilling the accumulator register. 991*e8d8bef9SDimitry Andric /// Similarly to other spills/reloads that use pseudo-ops, we do not actually 992*e8d8bef9SDimitry Andric /// eliminate the FrameIndex here nor compute the stack offset. We simply 993*e8d8bef9SDimitry Andric /// create a real instruction with an FI and rely on eliminateFrameIndex to 994*e8d8bef9SDimitry Andric /// handle the FI elimination. 995*e8d8bef9SDimitry Andric void PPCRegisterInfo::lowerACCSpilling(MachineBasicBlock::iterator II, 9960b57cec5SDimitry Andric unsigned FrameIndex) const { 997*e8d8bef9SDimitry Andric MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset> 9980b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 9990b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 10000b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 10010b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1002*e8d8bef9SDimitry Andric DebugLoc DL = MI.getDebugLoc(); 10038bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 1004*e8d8bef9SDimitry Andric bool IsKilled = MI.getOperand(0).isKill(); 10050b57cec5SDimitry Andric 1006*e8d8bef9SDimitry Andric bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg); 1007*e8d8bef9SDimitry Andric Register Reg = 1008*e8d8bef9SDimitry Andric PPC::VSRp0 + (SrcReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2; 1009*e8d8bef9SDimitry Andric bool IsLittleEndian = Subtarget.isLittleEndian(); 10100b57cec5SDimitry Andric 1011*e8d8bef9SDimitry Andric emitAccSpillRestoreInfo(MBB, IsPrimed, false); 1012*e8d8bef9SDimitry Andric 1013*e8d8bef9SDimitry Andric // De-prime the register being spilled, create two stores for the pair 1014*e8d8bef9SDimitry Andric // subregisters accounting for endianness and then re-prime the register if 1015*e8d8bef9SDimitry Andric // it isn't killed. This uses the Offset parameter to addFrameReference() to 1016*e8d8bef9SDimitry Andric // adjust the offset of the store that is within the 64-byte stack slot. 1017*e8d8bef9SDimitry Andric if (IsPrimed) 1018*e8d8bef9SDimitry Andric BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg); 1019*e8d8bef9SDimitry Andric addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1020*e8d8bef9SDimitry Andric .addReg(Reg, getKillRegState(IsKilled)), 1021*e8d8bef9SDimitry Andric FrameIndex, IsLittleEndian ? 32 : 0); 1022*e8d8bef9SDimitry Andric addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1023*e8d8bef9SDimitry Andric .addReg(Reg + 1, getKillRegState(IsKilled)), 1024*e8d8bef9SDimitry Andric FrameIndex, IsLittleEndian ? 0 : 32); 1025*e8d8bef9SDimitry Andric if (IsPrimed && !IsKilled) 1026*e8d8bef9SDimitry Andric BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg); 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric // Discard the pseudo instruction. 10290b57cec5SDimitry Andric MBB.erase(II); 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric 1032*e8d8bef9SDimitry Andric /// lowerACCRestore - Generate the code to restore the accumulator register. 1033*e8d8bef9SDimitry Andric void PPCRegisterInfo::lowerACCRestore(MachineBasicBlock::iterator II, 10340b57cec5SDimitry Andric unsigned FrameIndex) const { 1035*e8d8bef9SDimitry Andric MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset> 10360b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 10370b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 10380b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 10390b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1040*e8d8bef9SDimitry Andric DebugLoc DL = MI.getDebugLoc(); 10410b57cec5SDimitry Andric 10428bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 10430b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 1044*e8d8bef9SDimitry Andric "RESTORE_ACC does not define its destination"); 10450b57cec5SDimitry Andric 1046*e8d8bef9SDimitry Andric bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg); 1047*e8d8bef9SDimitry Andric Register Reg = 1048*e8d8bef9SDimitry Andric PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2; 1049*e8d8bef9SDimitry Andric bool IsLittleEndian = Subtarget.isLittleEndian(); 10500b57cec5SDimitry Andric 1051*e8d8bef9SDimitry Andric emitAccSpillRestoreInfo(MBB, IsPrimed, true); 1052*e8d8bef9SDimitry Andric 1053*e8d8bef9SDimitry Andric // Create two loads for the pair subregisters accounting for endianness and 1054*e8d8bef9SDimitry Andric // then prime the accumulator register being restored. 1055*e8d8bef9SDimitry Andric addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg), 1056*e8d8bef9SDimitry Andric FrameIndex, IsLittleEndian ? 32 : 0); 1057*e8d8bef9SDimitry Andric addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1), 1058*e8d8bef9SDimitry Andric FrameIndex, IsLittleEndian ? 0 : 32); 1059*e8d8bef9SDimitry Andric if (IsPrimed) 1060*e8d8bef9SDimitry Andric BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg); 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric // Discard the pseudo instruction. 10630b57cec5SDimitry Andric MBB.erase(II); 10640b57cec5SDimitry Andric } 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 10675ffd83dbSDimitry Andric Register Reg, int &FrameIdx) const { 10685ffd83dbSDimitry Andric // For the nonvolatile condition registers (CR2, CR3, CR4) return true to 10695ffd83dbSDimitry Andric // prevent allocating an additional frame slot. 10705ffd83dbSDimitry Andric // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8, 10715ffd83dbSDimitry Andric // for 32-bit AIX the CR save area is in the linkage area at SP+4. 10725ffd83dbSDimitry Andric // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos 10735ffd83dbSDimitry Andric // valid. 10745ffd83dbSDimitry Andric // For 32-bit ELF, we have previously created the stack slot if needed, so 10755ffd83dbSDimitry Andric // return its FrameIdx. 10765ffd83dbSDimitry Andric if (PPC::CR2 <= Reg && Reg <= PPC::CR4) { 10775ffd83dbSDimitry Andric FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex(); 10780b57cec5SDimitry Andric return true; 10790b57cec5SDimitry Andric } 10800b57cec5SDimitry Andric return false; 10810b57cec5SDimitry Andric } 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is. 10840b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC) { 10850b57cec5SDimitry Andric switch (OpC) { 10860b57cec5SDimitry Andric default: 10870b57cec5SDimitry Andric return 1; 10880b57cec5SDimitry Andric case PPC::LWA: 10890b57cec5SDimitry Andric case PPC::LWA_32: 10900b57cec5SDimitry Andric case PPC::LD: 10910b57cec5SDimitry Andric case PPC::LDU: 10920b57cec5SDimitry Andric case PPC::STD: 10930b57cec5SDimitry Andric case PPC::STDU: 10940b57cec5SDimitry Andric case PPC::DFLOADf32: 10950b57cec5SDimitry Andric case PPC::DFLOADf64: 10960b57cec5SDimitry Andric case PPC::DFSTOREf32: 10970b57cec5SDimitry Andric case PPC::DFSTOREf64: 10980b57cec5SDimitry Andric case PPC::LXSD: 10990b57cec5SDimitry Andric case PPC::LXSSP: 11000b57cec5SDimitry Andric case PPC::STXSD: 11010b57cec5SDimitry Andric case PPC::STXSSP: 11020b57cec5SDimitry Andric return 4; 11030b57cec5SDimitry Andric case PPC::EVLDD: 11040b57cec5SDimitry Andric case PPC::EVSTDD: 11050b57cec5SDimitry Andric return 8; 11060b57cec5SDimitry Andric case PPC::LXV: 11070b57cec5SDimitry Andric case PPC::STXV: 11080b57cec5SDimitry Andric return 16; 11090b57cec5SDimitry Andric } 11100b57cec5SDimitry Andric } 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is. 11130b57cec5SDimitry Andric static unsigned offsetMinAlign(const MachineInstr &MI) { 11140b57cec5SDimitry Andric unsigned OpC = MI.getOpcode(); 11150b57cec5SDimitry Andric return offsetMinAlignForOpcode(OpC); 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric // Return the OffsetOperandNo given the FIOperandNum (and the instruction). 11190b57cec5SDimitry Andric static unsigned getOffsetONFromFION(const MachineInstr &MI, 11200b57cec5SDimitry Andric unsigned FIOperandNum) { 11210b57cec5SDimitry Andric // Take into account whether it's an add or mem instruction 11220b57cec5SDimitry Andric unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2; 11230b57cec5SDimitry Andric if (MI.isInlineAsm()) 11240b57cec5SDimitry Andric OffsetOperandNo = FIOperandNum - 1; 11250b57cec5SDimitry Andric else if (MI.getOpcode() == TargetOpcode::STACKMAP || 11260b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::PATCHPOINT) 11270b57cec5SDimitry Andric OffsetOperandNo = FIOperandNum + 1; 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andric return OffsetOperandNo; 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric void 11330b57cec5SDimitry Andric PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 11340b57cec5SDimitry Andric int SPAdj, unsigned FIOperandNum, 11350b57cec5SDimitry Andric RegScavenger *RS) const { 11360b57cec5SDimitry Andric assert(SPAdj == 0 && "Unexpected"); 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andric // Get the instruction. 11390b57cec5SDimitry Andric MachineInstr &MI = *II; 11400b57cec5SDimitry Andric // Get the instruction's basic block. 11410b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 11420b57cec5SDimitry Andric // Get the basic block's function. 11430b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 11440b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 11450b57cec5SDimitry Andric // Get the instruction info. 11460b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 11470b57cec5SDimitry Andric // Get the frame info. 11480b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 11490b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 11500b57cec5SDimitry Andric 11510b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric // Get the frame index. 11540b57cec5SDimitry Andric int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 11550b57cec5SDimitry Andric 11560b57cec5SDimitry Andric // Get the frame pointer save index. Users of this index are primarily 11570b57cec5SDimitry Andric // DYNALLOC instructions. 11580b57cec5SDimitry Andric PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 11590b57cec5SDimitry Andric int FPSI = FI->getFramePointerSaveIndex(); 11600b57cec5SDimitry Andric // Get the instruction opcode. 11610b57cec5SDimitry Andric unsigned OpC = MI.getOpcode(); 11620b57cec5SDimitry Andric 11630b57cec5SDimitry Andric if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) { 11640b57cec5SDimitry Andric lowerDynamicAreaOffset(II); 11650b57cec5SDimitry Andric return; 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric // Special case for dynamic alloca. 11690b57cec5SDimitry Andric if (FPSI && FrameIndex == FPSI && 11700b57cec5SDimitry Andric (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) { 11710b57cec5SDimitry Andric lowerDynamicAlloc(II); 11720b57cec5SDimitry Andric return; 11730b57cec5SDimitry Andric } 11740b57cec5SDimitry Andric 11755ffd83dbSDimitry Andric if (FPSI && FrameIndex == FPSI && 11765ffd83dbSDimitry Andric (OpC == PPC::PREPARE_PROBED_ALLOCA_64 || 1177590d96feSDimitry Andric OpC == PPC::PREPARE_PROBED_ALLOCA_32 || 1178590d96feSDimitry Andric OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64 || 1179590d96feSDimitry Andric OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32)) { 11805ffd83dbSDimitry Andric lowerPrepareProbedAlloca(II); 11815ffd83dbSDimitry Andric return; 11825ffd83dbSDimitry Andric } 11835ffd83dbSDimitry Andric 11840b57cec5SDimitry Andric // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc. 11850b57cec5SDimitry Andric if (OpC == PPC::SPILL_CR) { 11860b57cec5SDimitry Andric lowerCRSpilling(II, FrameIndex); 11870b57cec5SDimitry Andric return; 11880b57cec5SDimitry Andric } else if (OpC == PPC::RESTORE_CR) { 11890b57cec5SDimitry Andric lowerCRRestore(II, FrameIndex); 11900b57cec5SDimitry Andric return; 11910b57cec5SDimitry Andric } else if (OpC == PPC::SPILL_CRBIT) { 11920b57cec5SDimitry Andric lowerCRBitSpilling(II, FrameIndex); 11930b57cec5SDimitry Andric return; 11940b57cec5SDimitry Andric } else if (OpC == PPC::RESTORE_CRBIT) { 11950b57cec5SDimitry Andric lowerCRBitRestore(II, FrameIndex); 11960b57cec5SDimitry Andric return; 1197*e8d8bef9SDimitry Andric } else if (OpC == PPC::SPILL_ACC || OpC == PPC::SPILL_UACC) { 1198*e8d8bef9SDimitry Andric lowerACCSpilling(II, FrameIndex); 11990b57cec5SDimitry Andric return; 1200*e8d8bef9SDimitry Andric } else if (OpC == PPC::RESTORE_ACC || OpC == PPC::RESTORE_UACC) { 1201*e8d8bef9SDimitry Andric lowerACCRestore(II, FrameIndex); 12020b57cec5SDimitry Andric return; 12030b57cec5SDimitry Andric } 12040b57cec5SDimitry Andric 12050b57cec5SDimitry Andric // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). 12060b57cec5SDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister( 12070b57cec5SDimitry Andric FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false); 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric // If the instruction is not present in ImmToIdxMap, then it has no immediate 12100b57cec5SDimitry Andric // form (and must be r+r). 12110b57cec5SDimitry Andric bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP && 12120b57cec5SDimitry Andric OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC); 12130b57cec5SDimitry Andric 12140b57cec5SDimitry Andric // Now add the frame object offset to the offset from r1. 12150b57cec5SDimitry Andric int Offset = MFI.getObjectOffset(FrameIndex); 12160b57cec5SDimitry Andric Offset += MI.getOperand(OffsetOperandNo).getImm(); 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric // If we're not using a Frame Pointer that has been set to the value of the 12190b57cec5SDimitry Andric // SP before having the stack size subtracted from it, then add the stack size 12200b57cec5SDimitry Andric // to Offset to get the correct offset. 12210b57cec5SDimitry Andric // Naked functions have stack size 0, although getStackSize may not reflect 12220b57cec5SDimitry Andric // that because we didn't call all the pieces that compute it for naked 12230b57cec5SDimitry Andric // functions. 12240b57cec5SDimitry Andric if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) { 12250b57cec5SDimitry Andric if (!(hasBasePointer(MF) && FrameIndex < 0)) 12260b57cec5SDimitry Andric Offset += MFI.getStackSize(); 12270b57cec5SDimitry Andric } 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric // If we can, encode the offset directly into the instruction. If this is a 12300b57cec5SDimitry Andric // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If 12310b57cec5SDimitry Andric // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits 12320b57cec5SDimitry Andric // clear can be encoded. This is extremely uncommon, because normally you 12330b57cec5SDimitry Andric // only "std" to a stack slot that is at least 4-byte aligned, but it can 12340b57cec5SDimitry Andric // happen in invalid code. 12350b57cec5SDimitry Andric assert(OpC != PPC::DBG_VALUE && 12360b57cec5SDimitry Andric "This should be handled in a target-independent way"); 12370b57cec5SDimitry Andric bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ? 12380b57cec5SDimitry Andric isUInt<8>(Offset) : 12390b57cec5SDimitry Andric isInt<16>(Offset); 12400b57cec5SDimitry Andric if (!noImmForm && ((OffsetFitsMnemonic && 12410b57cec5SDimitry Andric ((Offset % offsetMinAlign(MI)) == 0)) || 12420b57cec5SDimitry Andric OpC == TargetOpcode::STACKMAP || 12430b57cec5SDimitry Andric OpC == TargetOpcode::PATCHPOINT)) { 12440b57cec5SDimitry Andric MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 12450b57cec5SDimitry Andric return; 12460b57cec5SDimitry Andric } 12470b57cec5SDimitry Andric 12480b57cec5SDimitry Andric // The offset doesn't fit into a single register, scavenge one to build the 12490b57cec5SDimitry Andric // offset in. 12500b57cec5SDimitry Andric 12510b57cec5SDimitry Andric bool is64Bit = TM.isPPC64(); 12520b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 12530b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 12540b57cec5SDimitry Andric const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC; 12555ffd83dbSDimitry Andric Register SRegHi = MF.getRegInfo().createVirtualRegister(RC), 12560b57cec5SDimitry Andric SReg = MF.getRegInfo().createVirtualRegister(RC); 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric // Insert a set of rA with the full offset value before the ld, st, or add 12590b57cec5SDimitry Andric if (isInt<16>(Offset)) 12600b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg) 12610b57cec5SDimitry Andric .addImm(Offset); 12620b57cec5SDimitry Andric else { 12630b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi) 12640b57cec5SDimitry Andric .addImm(Offset >> 16); 12650b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg) 12660b57cec5SDimitry Andric .addReg(SRegHi, RegState::Kill) 12670b57cec5SDimitry Andric .addImm(Offset); 12680b57cec5SDimitry Andric } 12690b57cec5SDimitry Andric 12700b57cec5SDimitry Andric // Convert into indexed form of the instruction: 12710b57cec5SDimitry Andric // 12720b57cec5SDimitry Andric // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 12730b57cec5SDimitry Andric // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 12740b57cec5SDimitry Andric unsigned OperandBase; 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric if (noImmForm) 12770b57cec5SDimitry Andric OperandBase = 1; 12780b57cec5SDimitry Andric else if (OpC != TargetOpcode::INLINEASM && 12790b57cec5SDimitry Andric OpC != TargetOpcode::INLINEASM_BR) { 12800b57cec5SDimitry Andric assert(ImmToIdxMap.count(OpC) && 12810b57cec5SDimitry Andric "No indexed form of load or store available!"); 12820b57cec5SDimitry Andric unsigned NewOpcode = ImmToIdxMap.find(OpC)->second; 12830b57cec5SDimitry Andric MI.setDesc(TII.get(NewOpcode)); 12840b57cec5SDimitry Andric OperandBase = 1; 12850b57cec5SDimitry Andric } else { 12860b57cec5SDimitry Andric OperandBase = OffsetOperandNo; 12870b57cec5SDimitry Andric } 12880b57cec5SDimitry Andric 12898bcb0991SDimitry Andric Register StackReg = MI.getOperand(FIOperandNum).getReg(); 12900b57cec5SDimitry Andric MI.getOperand(OperandBase).ChangeToRegister(StackReg, false); 12910b57cec5SDimitry Andric MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true); 12920b57cec5SDimitry Andric } 12930b57cec5SDimitry Andric 12940b57cec5SDimitry Andric Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 12950b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 12960b57cec5SDimitry Andric 12970b57cec5SDimitry Andric if (!TM.isPPC64()) 12980b57cec5SDimitry Andric return TFI->hasFP(MF) ? PPC::R31 : PPC::R1; 12990b57cec5SDimitry Andric else 13000b57cec5SDimitry Andric return TFI->hasFP(MF) ? PPC::X31 : PPC::X1; 13010b57cec5SDimitry Andric } 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const { 13040b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 13050b57cec5SDimitry Andric if (!hasBasePointer(MF)) 13060b57cec5SDimitry Andric return getFrameRegister(MF); 13070b57cec5SDimitry Andric 13080b57cec5SDimitry Andric if (TM.isPPC64()) 13090b57cec5SDimitry Andric return PPC::X30; 13100b57cec5SDimitry Andric 13110b57cec5SDimitry Andric if (Subtarget.isSVR4ABI() && TM.isPositionIndependent()) 13120b57cec5SDimitry Andric return PPC::R29; 13130b57cec5SDimitry Andric 13140b57cec5SDimitry Andric return PPC::R30; 13150b57cec5SDimitry Andric } 13160b57cec5SDimitry Andric 13170b57cec5SDimitry Andric bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const { 13180b57cec5SDimitry Andric if (!EnableBasePointer) 13190b57cec5SDimitry Andric return false; 13200b57cec5SDimitry Andric if (AlwaysBasePointer) 13210b57cec5SDimitry Andric return true; 13220b57cec5SDimitry Andric 13230b57cec5SDimitry Andric // If we need to realign the stack, then the stack pointer can no longer 13240b57cec5SDimitry Andric // serve as an offset into the caller's stack space. As a result, we need a 13250b57cec5SDimitry Andric // base pointer. 13260b57cec5SDimitry Andric return needsStackRealignment(MF); 13270b57cec5SDimitry Andric } 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric /// Returns true if the instruction's frame index 13300b57cec5SDimitry Andric /// reference would be better served by a base register other than FP 13310b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index 13320b57cec5SDimitry Andric /// references it should create new base registers for. 13330b57cec5SDimitry Andric bool PPCRegisterInfo:: 13340b57cec5SDimitry Andric needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 13350b57cec5SDimitry Andric assert(Offset < 0 && "Local offset must be negative"); 13360b57cec5SDimitry Andric 13370b57cec5SDimitry Andric // It's the load/store FI references that cause issues, as it can be difficult 13380b57cec5SDimitry Andric // to materialize the offset if it won't fit in the literal field. Estimate 13390b57cec5SDimitry Andric // based on the size of the local frame and some conservative assumptions 13400b57cec5SDimitry Andric // about the rest of the stack frame (note, this is pre-regalloc, so 13410b57cec5SDimitry Andric // we don't know everything for certain yet) whether this offset is likely 13420b57cec5SDimitry Andric // to be out of range of the immediate. Return true if so. 13430b57cec5SDimitry Andric 13440b57cec5SDimitry Andric // We only generate virtual base registers for loads and stores that have 13450b57cec5SDimitry Andric // an r+i form. Return false for everything else. 13460b57cec5SDimitry Andric unsigned OpC = MI->getOpcode(); 13470b57cec5SDimitry Andric if (!ImmToIdxMap.count(OpC)) 13480b57cec5SDimitry Andric return false; 13490b57cec5SDimitry Andric 13500b57cec5SDimitry Andric // Don't generate a new virtual base register just to add zero to it. 13510b57cec5SDimitry Andric if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) && 13520b57cec5SDimitry Andric MI->getOperand(2).getImm() == 0) 13530b57cec5SDimitry Andric return false; 13540b57cec5SDimitry Andric 13550b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI->getParent(); 13560b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 13570b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 13580b57cec5SDimitry Andric unsigned StackEst = TFI->determineFrameLayout(MF, true); 13590b57cec5SDimitry Andric 13600b57cec5SDimitry Andric // If we likely don't need a stack frame, then we probably don't need a 13610b57cec5SDimitry Andric // virtual base register either. 13620b57cec5SDimitry Andric if (!StackEst) 13630b57cec5SDimitry Andric return false; 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric // Estimate an offset from the stack pointer. 13660b57cec5SDimitry Andric // The incoming offset is relating to the SP at the start of the function, 13670b57cec5SDimitry Andric // but when we access the local it'll be relative to the SP after local 13680b57cec5SDimitry Andric // allocation, so adjust our SP-relative offset by that allocation size. 13690b57cec5SDimitry Andric Offset += StackEst; 13700b57cec5SDimitry Andric 13710b57cec5SDimitry Andric // The frame pointer will point to the end of the stack, so estimate the 13720b57cec5SDimitry Andric // offset as the difference between the object offset and the FP location. 13730b57cec5SDimitry Andric return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset); 13740b57cec5SDimitry Andric } 13750b57cec5SDimitry Andric 13760b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to 13770b57cec5SDimitry Andric /// be a pointer to FrameIdx at the beginning of the basic block. 1378*e8d8bef9SDimitry Andric Register PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 13795ffd83dbSDimitry Andric int FrameIdx, 13800b57cec5SDimitry Andric int64_t Offset) const { 13810b57cec5SDimitry Andric unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI; 13820b57cec5SDimitry Andric 13830b57cec5SDimitry Andric MachineBasicBlock::iterator Ins = MBB->begin(); 13840b57cec5SDimitry Andric DebugLoc DL; // Defaults to "unknown" 13850b57cec5SDimitry Andric if (Ins != MBB->end()) 13860b57cec5SDimitry Andric DL = Ins->getDebugLoc(); 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric const MachineFunction &MF = *MBB->getParent(); 13890b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 13900b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 13910b57cec5SDimitry Andric const MCInstrDesc &MCID = TII.get(ADDriOpc); 13920b57cec5SDimitry Andric MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 1393*e8d8bef9SDimitry Andric const TargetRegisterClass *RC = getPointerRegClass(MF); 1394*e8d8bef9SDimitry Andric Register BaseReg = MRI.createVirtualRegister(RC); 13950b57cec5SDimitry Andric MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); 13960b57cec5SDimitry Andric 13970b57cec5SDimitry Andric BuildMI(*MBB, Ins, DL, MCID, BaseReg) 13980b57cec5SDimitry Andric .addFrameIndex(FrameIdx).addImm(Offset); 1399*e8d8bef9SDimitry Andric 1400*e8d8bef9SDimitry Andric return BaseReg; 14010b57cec5SDimitry Andric } 14020b57cec5SDimitry Andric 14035ffd83dbSDimitry Andric void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 14040b57cec5SDimitry Andric int64_t Offset) const { 14050b57cec5SDimitry Andric unsigned FIOperandNum = 0; 14060b57cec5SDimitry Andric while (!MI.getOperand(FIOperandNum).isFI()) { 14070b57cec5SDimitry Andric ++FIOperandNum; 14080b57cec5SDimitry Andric assert(FIOperandNum < MI.getNumOperands() && 14090b57cec5SDimitry Andric "Instr doesn't have FrameIndex operand!"); 14100b57cec5SDimitry Andric } 14110b57cec5SDimitry Andric 14120b57cec5SDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false); 14130b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 14140b57cec5SDimitry Andric Offset += MI.getOperand(OffsetOperandNo).getImm(); 14150b57cec5SDimitry Andric MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 14160b57cec5SDimitry Andric 14170b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 14180b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 14190b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 14200b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 14210b57cec5SDimitry Andric const MCInstrDesc &MCID = MI.getDesc(); 14220b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 14230b57cec5SDimitry Andric MRI.constrainRegClass(BaseReg, 14240b57cec5SDimitry Andric TII.getRegClass(MCID, FIOperandNum, this, MF)); 14250b57cec5SDimitry Andric } 14260b57cec5SDimitry Andric 14270b57cec5SDimitry Andric bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 14285ffd83dbSDimitry Andric Register BaseReg, 14290b57cec5SDimitry Andric int64_t Offset) const { 14300b57cec5SDimitry Andric unsigned FIOperandNum = 0; 14310b57cec5SDimitry Andric while (!MI->getOperand(FIOperandNum).isFI()) { 14320b57cec5SDimitry Andric ++FIOperandNum; 14330b57cec5SDimitry Andric assert(FIOperandNum < MI->getNumOperands() && 14340b57cec5SDimitry Andric "Instr doesn't have FrameIndex operand!"); 14350b57cec5SDimitry Andric } 14360b57cec5SDimitry Andric 14370b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum); 14380b57cec5SDimitry Andric Offset += MI->getOperand(OffsetOperandNo).getImm(); 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm 14410b57cec5SDimitry Andric MI->getOpcode() == TargetOpcode::STACKMAP || 14420b57cec5SDimitry Andric MI->getOpcode() == TargetOpcode::PATCHPOINT || 14430b57cec5SDimitry Andric (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0); 14440b57cec5SDimitry Andric } 1445