10b57cec5SDimitry Andric //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file contains the PowerPC implementation of the TargetRegisterInfo 100b57cec5SDimitry Andric // class. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "PPCRegisterInfo.h" 150b57cec5SDimitry Andric #include "PPCFrameLowering.h" 160b57cec5SDimitry Andric #include "PPCInstrBuilder.h" 170b57cec5SDimitry Andric #include "PPCMachineFunctionInfo.h" 180b57cec5SDimitry Andric #include "PPCSubtarget.h" 190b57cec5SDimitry Andric #include "PPCTargetMachine.h" 200b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 210b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 220b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 310b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h" 320b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 330b57cec5SDimitry Andric #include "llvm/IR/Function.h" 340b57cec5SDimitry Andric #include "llvm/IR/Type.h" 350b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 360b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 370b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 380b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 390b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 400b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 410b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 420b57cec5SDimitry Andric #include <cstdlib> 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric using namespace llvm; 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric #define DEBUG_TYPE "reginfo" 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC 490b57cec5SDimitry Andric #include "PPCGenRegisterInfo.inc" 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass"); 520b57cec5SDimitry Andric STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass"); 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric static cl::opt<bool> 550b57cec5SDimitry Andric EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true), 560b57cec5SDimitry Andric cl::desc("Enable use of a base pointer for complex stack frames")); 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric static cl::opt<bool> 590b57cec5SDimitry Andric AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false), 600b57cec5SDimitry Andric cl::desc("Force the use of a base pointer in every function")); 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric static cl::opt<bool> 630b57cec5SDimitry Andric EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false), 640b57cec5SDimitry Andric cl::desc("Enable spills from gpr to vsr rather than stack")); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric static cl::opt<bool> 670b57cec5SDimitry Andric StackPtrConst("ppc-stack-ptr-caller-preserved", 680b57cec5SDimitry Andric cl::desc("Consider R1 caller preserved so stack saves of " 690b57cec5SDimitry Andric "caller preserved registers can be LICM candidates"), 700b57cec5SDimitry Andric cl::init(true), cl::Hidden); 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric static cl::opt<unsigned> 730b57cec5SDimitry Andric MaxCRBitSpillDist("ppc-max-crbit-spill-dist", 740b57cec5SDimitry Andric cl::desc("Maximum search distance for definition of CR bit " 750b57cec5SDimitry Andric "spill on ppc"), 760b57cec5SDimitry Andric cl::Hidden, cl::init(100)); 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM) 810b57cec5SDimitry Andric : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR, 820b57cec5SDimitry Andric TM.isPPC64() ? 0 : 1, 830b57cec5SDimitry Andric TM.isPPC64() ? 0 : 1), 840b57cec5SDimitry Andric TM(TM) { 850b57cec5SDimitry Andric ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX; 860b57cec5SDimitry Andric ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX; 870b57cec5SDimitry Andric ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX; 880b57cec5SDimitry Andric ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX; 890b57cec5SDimitry Andric ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX; 900b57cec5SDimitry Andric ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX; 910b57cec5SDimitry Andric ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX; 920b57cec5SDimitry Andric ImmToIdxMap[PPC::ADDI] = PPC::ADD4; 930b57cec5SDimitry Andric ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric // 64-bit 960b57cec5SDimitry Andric ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8; 970b57cec5SDimitry Andric ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8; 980b57cec5SDimitry Andric ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8; 990b57cec5SDimitry Andric ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX; 1000b57cec5SDimitry Andric ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric // VSX 1030b57cec5SDimitry Andric ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX; 1040b57cec5SDimitry Andric ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX; 1050b57cec5SDimitry Andric ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX; 1060b57cec5SDimitry Andric ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX; 1070b57cec5SDimitry Andric ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX; 1080b57cec5SDimitry Andric ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX; 1090b57cec5SDimitry Andric ImmToIdxMap[PPC::LXV] = PPC::LXVX; 1100b57cec5SDimitry Andric ImmToIdxMap[PPC::LXSD] = PPC::LXSDX; 1110b57cec5SDimitry Andric ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX; 1120b57cec5SDimitry Andric ImmToIdxMap[PPC::STXV] = PPC::STXVX; 1130b57cec5SDimitry Andric ImmToIdxMap[PPC::STXSD] = PPC::STXSDX; 1140b57cec5SDimitry Andric ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric // SPE 1170b57cec5SDimitry Andric ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX; 1180b57cec5SDimitry Andric ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX; 1190b57cec5SDimitry Andric ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX; 1200b57cec5SDimitry Andric ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX; 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric /// getPointerRegClass - Return the register class to use to hold pointers. 1240b57cec5SDimitry Andric /// This is used for addressing modes. 1250b57cec5SDimitry Andric const TargetRegisterClass * 1260b57cec5SDimitry Andric PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) 1270b57cec5SDimitry Andric const { 1280b57cec5SDimitry Andric // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value 1290b57cec5SDimitry Andric // when it checks for ZERO folding. 1300b57cec5SDimitry Andric if (Kind == 1) { 1310b57cec5SDimitry Andric if (TM.isPPC64()) 1320b57cec5SDimitry Andric return &PPC::G8RC_NOX0RegClass; 1330b57cec5SDimitry Andric return &PPC::GPRC_NOR0RegClass; 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric if (TM.isPPC64()) 1370b57cec5SDimitry Andric return &PPC::G8RCRegClass; 1380b57cec5SDimitry Andric return &PPC::GPRCRegClass; 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric const MCPhysReg* 1420b57cec5SDimitry Andric PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 1430b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>(); 1440b57cec5SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) { 145*5ffd83dbSDimitry Andric if (!TM.isPPC64() && Subtarget.isAIXABI()) 146*5ffd83dbSDimitry Andric report_fatal_error("AnyReg unimplemented on 32-bit AIX."); 1470b57cec5SDimitry Andric if (Subtarget.hasVSX()) 1480b57cec5SDimitry Andric return CSR_64_AllRegs_VSX_SaveList; 1490b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 1500b57cec5SDimitry Andric return CSR_64_AllRegs_Altivec_SaveList; 1510b57cec5SDimitry Andric return CSR_64_AllRegs_SaveList; 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric // On PPC64, we might need to save r2 (but only if it is not reserved). 155*5ffd83dbSDimitry Andric // We do not need to treat R2 as callee-saved when using PC-Relative calls 156*5ffd83dbSDimitry Andric // because any direct uses of R2 will cause it to be reserved. If the function 157*5ffd83dbSDimitry Andric // is a leaf or the only uses of R2 are implicit uses for calls, the calls 158*5ffd83dbSDimitry Andric // will use the @notoc relocation which will cause this function to set the 159*5ffd83dbSDimitry Andric // st_other bit to 1, thereby communicating to its caller that it arbitrarily 160*5ffd83dbSDimitry Andric // clobbers the TOC. 161*5ffd83dbSDimitry Andric bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) && 162*5ffd83dbSDimitry Andric !Subtarget.isUsingPCRelativeCalls(); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // Cold calling convention CSRs. 1650b57cec5SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::Cold) { 166*5ffd83dbSDimitry Andric if (Subtarget.isAIXABI()) 167*5ffd83dbSDimitry Andric report_fatal_error("Cold calling unimplemented on AIX."); 1680b57cec5SDimitry Andric if (TM.isPPC64()) { 1690b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 1700b57cec5SDimitry Andric return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList 1710b57cec5SDimitry Andric : CSR_SVR64_ColdCC_Altivec_SaveList; 1720b57cec5SDimitry Andric return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList 1730b57cec5SDimitry Andric : CSR_SVR64_ColdCC_SaveList; 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric // 32-bit targets. 1760b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 1770b57cec5SDimitry Andric return CSR_SVR32_ColdCC_Altivec_SaveList; 1780b57cec5SDimitry Andric else if (Subtarget.hasSPE()) 1790b57cec5SDimitry Andric return CSR_SVR32_ColdCC_SPE_SaveList; 1800b57cec5SDimitry Andric return CSR_SVR32_ColdCC_SaveList; 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric // Standard calling convention CSRs. 1830b57cec5SDimitry Andric if (TM.isPPC64()) { 1840b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 185*5ffd83dbSDimitry Andric return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList 186*5ffd83dbSDimitry Andric : CSR_PPC64_Altivec_SaveList; 187*5ffd83dbSDimitry Andric return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList; 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric // 32-bit targets. 190*5ffd83dbSDimitry Andric if (Subtarget.isAIXABI()) 191*5ffd83dbSDimitry Andric return CSR_AIX32_SaveList; 1920b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 1930b57cec5SDimitry Andric return CSR_SVR432_Altivec_SaveList; 1940b57cec5SDimitry Andric else if (Subtarget.hasSPE()) 1950b57cec5SDimitry Andric return CSR_SVR432_SPE_SaveList; 1960b57cec5SDimitry Andric return CSR_SVR432_SaveList; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric const uint32_t * 2000b57cec5SDimitry Andric PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF, 2010b57cec5SDimitry Andric CallingConv::ID CC) const { 2020b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 2030b57cec5SDimitry Andric if (CC == CallingConv::AnyReg) { 2040b57cec5SDimitry Andric if (Subtarget.hasVSX()) 2050b57cec5SDimitry Andric return CSR_64_AllRegs_VSX_RegMask; 2060b57cec5SDimitry Andric if (Subtarget.hasAltivec()) 2070b57cec5SDimitry Andric return CSR_64_AllRegs_Altivec_RegMask; 2080b57cec5SDimitry Andric return CSR_64_AllRegs_RegMask; 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric if (Subtarget.isAIXABI()) { 2120b57cec5SDimitry Andric assert(!Subtarget.hasAltivec() && "Altivec is not implemented on AIX yet."); 213*5ffd83dbSDimitry Andric return TM.isPPC64() ? CSR_PPC64_RegMask : CSR_AIX32_RegMask; 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric if (CC == CallingConv::Cold) { 2170b57cec5SDimitry Andric return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask 2180b57cec5SDimitry Andric : CSR_SVR64_ColdCC_RegMask) 2190b57cec5SDimitry Andric : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask 2200b57cec5SDimitry Andric : (Subtarget.hasSPE() 2210b57cec5SDimitry Andric ? CSR_SVR32_ColdCC_SPE_RegMask 2220b57cec5SDimitry Andric : CSR_SVR32_ColdCC_RegMask)); 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric 225*5ffd83dbSDimitry Andric return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask 226*5ffd83dbSDimitry Andric : CSR_PPC64_RegMask) 227*5ffd83dbSDimitry Andric : (Subtarget.hasAltivec() 228*5ffd83dbSDimitry Andric ? CSR_SVR432_Altivec_RegMask 229*5ffd83dbSDimitry Andric : (Subtarget.hasSPE() ? CSR_SVR432_SPE_RegMask 2300b57cec5SDimitry Andric : CSR_SVR432_RegMask)); 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric const uint32_t* 2340b57cec5SDimitry Andric PPCRegisterInfo::getNoPreservedMask() const { 2350b57cec5SDimitry Andric return CSR_NoRegs_RegMask; 2360b57cec5SDimitry Andric } 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const { 2390b57cec5SDimitry Andric for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM}) 2400b57cec5SDimitry Andric Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32)); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 2440b57cec5SDimitry Andric BitVector Reserved(getNumRegs()); 2450b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 2460b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric // The ZERO register is not really a register, but the representation of r0 2490b57cec5SDimitry Andric // when used in instructions that treat r0 as the constant 0. 2500b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::ZERO); 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric // The FP register is also not really a register, but is the representation 2530b57cec5SDimitry Andric // of the frame pointer register used by ISD::FRAMEADDR. 2540b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::FP); 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric // The BP register is also not really a register, but is the representation 2570b57cec5SDimitry Andric // of the base pointer register used by setjmp. 2580b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::BP); 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric // The counter registers must be reserved so that counter-based loops can 2610b57cec5SDimitry Andric // be correctly formed (and the mtctr instructions are not DCE'd). 2620b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::CTR); 2630b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::CTR8); 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R1); 2660b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::LR); 2670b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::LR8); 2680b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::RM); 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::VRSAVE); 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric // The SVR4 ABI reserves r2 and r13 2730b57cec5SDimitry Andric if (Subtarget.isSVR4ABI()) { 2740b57cec5SDimitry Andric // We only reserve r2 if we need to use the TOC pointer. If we have no 2750b57cec5SDimitry Andric // explicit uses of the TOC pointer (meaning we're a leaf function with 2760b57cec5SDimitry Andric // no constant-pool loads, etc.) and we have no potential uses inside an 2770b57cec5SDimitry Andric // inline asm block, then we can treat r2 has an ordinary callee-saved 2780b57cec5SDimitry Andric // register. 2790b57cec5SDimitry Andric const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 2800b57cec5SDimitry Andric if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm()) 2810b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R2); // System-reserved register 2820b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // Always reserve r2 on AIX for now. 2860b57cec5SDimitry Andric // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions. 2870b57cec5SDimitry Andric if (Subtarget.isAIXABI()) 2880b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R2); // System-reserved register 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric // On PPC64, r13 is the thread pointer. Never allocate this register. 2910b57cec5SDimitry Andric if (TM.isPPC64()) 2920b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R13); 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric if (TFI->needsFP(MF)) 2950b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R31); 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric bool IsPositionIndependent = TM.isPositionIndependent(); 2980b57cec5SDimitry Andric if (hasBasePointer(MF)) { 2998bcb0991SDimitry Andric if (Subtarget.is32BitELFABI() && IsPositionIndependent) 3000b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R29); 3010b57cec5SDimitry Andric else 3020b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R30); 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric 3058bcb0991SDimitry Andric if (Subtarget.is32BitELFABI() && IsPositionIndependent) 3060b57cec5SDimitry Andric markSuperRegs(Reserved, PPC::R30); 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric // Reserve Altivec registers when Altivec is unavailable. 3090b57cec5SDimitry Andric if (!Subtarget.hasAltivec()) 3100b57cec5SDimitry Andric for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(), 3110b57cec5SDimitry Andric IE = PPC::VRRCRegClass.end(); I != IE; ++I) 3120b57cec5SDimitry Andric markSuperRegs(Reserved, *I); 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric assert(checkAllSuperRegsMarked(Reserved)); 3150b57cec5SDimitry Andric return Reserved; 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const { 3190b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 3200b57cec5SDimitry Andric const PPCInstrInfo *InstrInfo = Subtarget.getInstrInfo(); 3210b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 3220b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo(); 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric // If the callee saved info is invalid we have to default to true for safety. 3250b57cec5SDimitry Andric if (!MFI.isCalleeSavedInfoValid()) 3260b57cec5SDimitry Andric return true; 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric // We will require the use of X-Forms because the frame is larger than what 3290b57cec5SDimitry Andric // can be represented in signed 16 bits that fit in the immediate of a D-Form. 3300b57cec5SDimitry Andric // If we need an X-Form then we need a register to store the address offset. 3310b57cec5SDimitry Andric unsigned FrameSize = MFI.getStackSize(); 3320b57cec5SDimitry Andric // Signed 16 bits means that the FrameSize cannot be more than 15 bits. 3330b57cec5SDimitry Andric if (FrameSize & ~0x7FFF) 3340b57cec5SDimitry Andric return true; 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // The callee saved info is valid so it can be traversed. 3370b57cec5SDimitry Andric // Checking for registers that need saving that do not have load or store 3380b57cec5SDimitry Andric // forms where the address offset is an immediate. 3390b57cec5SDimitry Andric for (unsigned i = 0; i < Info.size(); i++) { 3400b57cec5SDimitry Andric int FrIdx = Info[i].getFrameIdx(); 3410b57cec5SDimitry Andric unsigned Reg = Info[i].getReg(); 3420b57cec5SDimitry Andric 343*5ffd83dbSDimitry Andric const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg); 344*5ffd83dbSDimitry Andric unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC); 3450b57cec5SDimitry Andric if (!MFI.isFixedObjectIndex(FrIdx)) { 3460b57cec5SDimitry Andric // This is not a fixed object. If it requires alignment then we may still 3470b57cec5SDimitry Andric // need to use the XForm. 3480b57cec5SDimitry Andric if (offsetMinAlignForOpcode(Opcode) > 1) 3490b57cec5SDimitry Andric return true; 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric // This is eiher: 3530b57cec5SDimitry Andric // 1) A fixed frame index object which we know are aligned so 3540b57cec5SDimitry Andric // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't 355480093f4SDimitry Andric // need to consider the alignment here. 3560b57cec5SDimitry Andric // 2) A not fixed object but in that case we now know that the min required 3570b57cec5SDimitry Andric // alignment is no more than 1 based on the previous check. 3580b57cec5SDimitry Andric if (InstrInfo->isXFormMemOp(Opcode)) 3590b57cec5SDimitry Andric return true; 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric return false; 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric 364*5ffd83dbSDimitry Andric bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg, 3650b57cec5SDimitry Andric const MachineFunction &MF) const { 3668bcb0991SDimitry Andric assert(Register::isPhysicalRegister(PhysReg)); 3670b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 3680b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 3690b57cec5SDimitry Andric if (!TM.isPPC64()) 3700b57cec5SDimitry Andric return false; 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric if (!Subtarget.isSVR4ABI()) 3730b57cec5SDimitry Andric return false; 3740b57cec5SDimitry Andric if (PhysReg == PPC::X2) 3750b57cec5SDimitry Andric // X2 is guaranteed to be preserved within a function if it is reserved. 3760b57cec5SDimitry Andric // The reason it's reserved is that it's the TOC pointer (and the function 3770b57cec5SDimitry Andric // uses the TOC). In functions where it isn't reserved (i.e. leaf functions 3780b57cec5SDimitry Andric // with no TOC access), we can't claim that it is preserved. 3790b57cec5SDimitry Andric return (getReservedRegs(MF).test(PPC::X2)); 3800b57cec5SDimitry Andric if (StackPtrConst && (PhysReg == PPC::X1) && !MFI.hasVarSizedObjects() 3810b57cec5SDimitry Andric && !MFI.hasOpaqueSPAdjustment()) 3820b57cec5SDimitry Andric // The value of the stack pointer does not change within a function after 3830b57cec5SDimitry Andric // the prologue and before the epilogue if there are no dynamic allocations 3840b57cec5SDimitry Andric // and no inline asm which clobbers X1. 3850b57cec5SDimitry Andric return true; 3860b57cec5SDimitry Andric return false; 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 3900b57cec5SDimitry Andric MachineFunction &MF) const { 3910b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 3920b57cec5SDimitry Andric const unsigned DefaultSafety = 1; 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric switch (RC->getID()) { 3950b57cec5SDimitry Andric default: 3960b57cec5SDimitry Andric return 0; 3970b57cec5SDimitry Andric case PPC::G8RC_NOX0RegClassID: 3980b57cec5SDimitry Andric case PPC::GPRC_NOR0RegClassID: 3990b57cec5SDimitry Andric case PPC::SPERCRegClassID: 4000b57cec5SDimitry Andric case PPC::G8RCRegClassID: 4010b57cec5SDimitry Andric case PPC::GPRCRegClassID: { 4020b57cec5SDimitry Andric unsigned FP = TFI->hasFP(MF) ? 1 : 0; 4030b57cec5SDimitry Andric return 32 - FP - DefaultSafety; 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric case PPC::F8RCRegClassID: 4060b57cec5SDimitry Andric case PPC::F4RCRegClassID: 4070b57cec5SDimitry Andric case PPC::QFRCRegClassID: 4080b57cec5SDimitry Andric case PPC::QSRCRegClassID: 4090b57cec5SDimitry Andric case PPC::QBRCRegClassID: 4100b57cec5SDimitry Andric case PPC::VRRCRegClassID: 4110b57cec5SDimitry Andric case PPC::VFRCRegClassID: 4120b57cec5SDimitry Andric case PPC::VSLRCRegClassID: 4130b57cec5SDimitry Andric return 32 - DefaultSafety; 4140b57cec5SDimitry Andric case PPC::VSRCRegClassID: 4150b57cec5SDimitry Andric case PPC::VSFRCRegClassID: 4160b57cec5SDimitry Andric case PPC::VSSRCRegClassID: 4170b57cec5SDimitry Andric return 64 - DefaultSafety; 4180b57cec5SDimitry Andric case PPC::CRRCRegClassID: 4190b57cec5SDimitry Andric return 8 - DefaultSafety; 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric const TargetRegisterClass * 4240b57cec5SDimitry Andric PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 4250b57cec5SDimitry Andric const MachineFunction &MF) const { 4260b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 4270b57cec5SDimitry Andric if (Subtarget.hasVSX()) { 4280b57cec5SDimitry Andric // With VSX, we can inflate various sub-register classes to the full VSX 4290b57cec5SDimitry Andric // register set. 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric // For Power9 we allow the user to enable GPR to vector spills. 4320b57cec5SDimitry Andric // FIXME: Currently limited to spilling GP8RC. A follow on patch will add 4330b57cec5SDimitry Andric // support to spill GPRC. 4340b57cec5SDimitry Andric if (TM.isELFv2ABI()) { 4350b57cec5SDimitry Andric if (Subtarget.hasP9Vector() && EnableGPRToVecSpills && 4360b57cec5SDimitry Andric RC == &PPC::G8RCRegClass) { 4370b57cec5SDimitry Andric InflateGP8RC++; 4380b57cec5SDimitry Andric return &PPC::SPILLTOVSRRCRegClass; 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills) 4410b57cec5SDimitry Andric InflateGPRC++; 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric if (RC == &PPC::F8RCRegClass) 4440b57cec5SDimitry Andric return &PPC::VSFRCRegClass; 4450b57cec5SDimitry Andric else if (RC == &PPC::VRRCRegClass) 4460b57cec5SDimitry Andric return &PPC::VSRCRegClass; 4470b57cec5SDimitry Andric else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector()) 4480b57cec5SDimitry Andric return &PPC::VSSRCRegClass; 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF); 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4550b57cec5SDimitry Andric // Stack Frame Processing methods 4560b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric /// lowerDynamicAlloc - Generate the code for allocating an object in the 4590b57cec5SDimitry Andric /// current frame. The sequence of code will be in the general form 4600b57cec5SDimitry Andric /// 4610b57cec5SDimitry Andric /// addi R0, SP, \#frameSize ; get the address of the previous frame 4620b57cec5SDimitry Andric /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size 4630b57cec5SDimitry Andric /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation 4640b57cec5SDimitry Andric /// 4650b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const { 4660b57cec5SDimitry Andric // Get the instruction. 4670b57cec5SDimitry Andric MachineInstr &MI = *II; 4680b57cec5SDimitry Andric // Get the instruction's basic block. 4690b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 4700b57cec5SDimitry Andric // Get the basic block's function. 4710b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 4720b57cec5SDimitry Andric // Get the frame info. 4730b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 4740b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 4750b57cec5SDimitry Andric // Get the instruction info. 4760b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 4770b57cec5SDimitry Andric // Determine whether 64-bit pointers are used. 4780b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 4790b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric // Get the maximum call stack size. 4820b57cec5SDimitry Andric unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 483*5ffd83dbSDimitry Andric Align MaxAlign = MFI.getMaxAlign(); 484*5ffd83dbSDimitry Andric assert(isAligned(MaxAlign, maxCallFrameSize) && 4850b57cec5SDimitry Andric "Maximum call-frame size not sufficiently aligned"); 486*5ffd83dbSDimitry Andric (void)MaxAlign; 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 4890b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 4908bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 4910b57cec5SDimitry Andric bool KillNegSizeReg = MI.getOperand(1).isKill(); 4928bcb0991SDimitry Andric Register NegSizeReg = MI.getOperand(1).getReg(); 4930b57cec5SDimitry Andric 494*5ffd83dbSDimitry Andric prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg); 4950b57cec5SDimitry Andric // Grow the stack and update the stack pointer link, then determine the 4960b57cec5SDimitry Andric // address of new allocated space. 4970b57cec5SDimitry Andric if (LP64) { 4980b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1) 4990b57cec5SDimitry Andric .addReg(Reg, RegState::Kill) 5000b57cec5SDimitry Andric .addReg(PPC::X1) 5010b57cec5SDimitry Andric .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 5020b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 5030b57cec5SDimitry Andric .addReg(PPC::X1) 5040b57cec5SDimitry Andric .addImm(maxCallFrameSize); 5050b57cec5SDimitry Andric } else { 5060b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1) 5070b57cec5SDimitry Andric .addReg(Reg, RegState::Kill) 5080b57cec5SDimitry Andric .addReg(PPC::R1) 5090b57cec5SDimitry Andric .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 5100b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 5110b57cec5SDimitry Andric .addReg(PPC::R1) 5120b57cec5SDimitry Andric .addImm(maxCallFrameSize); 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric // Discard the DYNALLOC instruction. 5160b57cec5SDimitry Andric MBB.erase(II); 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric 519*5ffd83dbSDimitry Andric /// To accomplish dynamic stack allocation, we have to calculate exact size 520*5ffd83dbSDimitry Andric /// subtracted from the stack pointer according alignment information and get 521*5ffd83dbSDimitry Andric /// previous frame pointer. 522*5ffd83dbSDimitry Andric void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II, 523*5ffd83dbSDimitry Andric Register &NegSizeReg, 524*5ffd83dbSDimitry Andric bool &KillNegSizeReg, 525*5ffd83dbSDimitry Andric Register &FramePointer) const { 526*5ffd83dbSDimitry Andric // Get the instruction. 527*5ffd83dbSDimitry Andric MachineInstr &MI = *II; 528*5ffd83dbSDimitry Andric // Get the instruction's basic block. 529*5ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 530*5ffd83dbSDimitry Andric // Get the basic block's function. 531*5ffd83dbSDimitry Andric MachineFunction &MF = *MBB.getParent(); 532*5ffd83dbSDimitry Andric // Get the frame info. 533*5ffd83dbSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 534*5ffd83dbSDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 535*5ffd83dbSDimitry Andric // Get the instruction info. 536*5ffd83dbSDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 537*5ffd83dbSDimitry Andric // Determine whether 64-bit pointers are used. 538*5ffd83dbSDimitry Andric bool LP64 = TM.isPPC64(); 539*5ffd83dbSDimitry Andric DebugLoc dl = MI.getDebugLoc(); 540*5ffd83dbSDimitry Andric // Get the total frame size. 541*5ffd83dbSDimitry Andric unsigned FrameSize = MFI.getStackSize(); 542*5ffd83dbSDimitry Andric 543*5ffd83dbSDimitry Andric // Get stack alignments. 544*5ffd83dbSDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 545*5ffd83dbSDimitry Andric Align TargetAlign = TFI->getStackAlign(); 546*5ffd83dbSDimitry Andric Align MaxAlign = MFI.getMaxAlign(); 547*5ffd83dbSDimitry Andric 548*5ffd83dbSDimitry Andric // Determine the previous frame's address. If FrameSize can't be 549*5ffd83dbSDimitry Andric // represented as 16 bits or we need special alignment, then we load the 550*5ffd83dbSDimitry Andric // previous frame's address from 0(SP). Why not do an addis of the hi? 551*5ffd83dbSDimitry Andric // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 552*5ffd83dbSDimitry Andric // Constructing the constant and adding would take 3 instructions. 553*5ffd83dbSDimitry Andric // Fortunately, a frame greater than 32K is rare. 554*5ffd83dbSDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 555*5ffd83dbSDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 556*5ffd83dbSDimitry Andric 557*5ffd83dbSDimitry Andric if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 558*5ffd83dbSDimitry Andric if (LP64) 559*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer) 560*5ffd83dbSDimitry Andric .addReg(PPC::X31) 561*5ffd83dbSDimitry Andric .addImm(FrameSize); 562*5ffd83dbSDimitry Andric else 563*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer) 564*5ffd83dbSDimitry Andric .addReg(PPC::R31) 565*5ffd83dbSDimitry Andric .addImm(FrameSize); 566*5ffd83dbSDimitry Andric } else if (LP64) { 567*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer) 568*5ffd83dbSDimitry Andric .addImm(0) 569*5ffd83dbSDimitry Andric .addReg(PPC::X1); 570*5ffd83dbSDimitry Andric } else { 571*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer) 572*5ffd83dbSDimitry Andric .addImm(0) 573*5ffd83dbSDimitry Andric .addReg(PPC::R1); 574*5ffd83dbSDimitry Andric } 575*5ffd83dbSDimitry Andric // Determine the actual NegSizeReg according to alignment info. 576*5ffd83dbSDimitry Andric if (LP64) { 577*5ffd83dbSDimitry Andric if (MaxAlign > TargetAlign) { 578*5ffd83dbSDimitry Andric unsigned UnalNegSizeReg = NegSizeReg; 579*5ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 580*5ffd83dbSDimitry Andric 581*5ffd83dbSDimitry Andric // Unfortunately, there is no andi, only andi., and we can't insert that 582*5ffd83dbSDimitry Andric // here because we might clobber cr0 while it is live. 583*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg) 584*5ffd83dbSDimitry Andric .addImm(~(MaxAlign.value() - 1)); 585*5ffd83dbSDimitry Andric 586*5ffd83dbSDimitry Andric unsigned NegSizeReg1 = NegSizeReg; 587*5ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 588*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg) 589*5ffd83dbSDimitry Andric .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 590*5ffd83dbSDimitry Andric .addReg(NegSizeReg1, RegState::Kill); 591*5ffd83dbSDimitry Andric KillNegSizeReg = true; 592*5ffd83dbSDimitry Andric } 593*5ffd83dbSDimitry Andric } else { 594*5ffd83dbSDimitry Andric if (MaxAlign > TargetAlign) { 595*5ffd83dbSDimitry Andric unsigned UnalNegSizeReg = NegSizeReg; 596*5ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 597*5ffd83dbSDimitry Andric 598*5ffd83dbSDimitry Andric // Unfortunately, there is no andi, only andi., and we can't insert that 599*5ffd83dbSDimitry Andric // here because we might clobber cr0 while it is live. 600*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg) 601*5ffd83dbSDimitry Andric .addImm(~(MaxAlign.value() - 1)); 602*5ffd83dbSDimitry Andric 603*5ffd83dbSDimitry Andric unsigned NegSizeReg1 = NegSizeReg; 604*5ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 605*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg) 606*5ffd83dbSDimitry Andric .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 607*5ffd83dbSDimitry Andric .addReg(NegSizeReg1, RegState::Kill); 608*5ffd83dbSDimitry Andric KillNegSizeReg = true; 609*5ffd83dbSDimitry Andric } 610*5ffd83dbSDimitry Andric } 611*5ffd83dbSDimitry Andric } 612*5ffd83dbSDimitry Andric 613*5ffd83dbSDimitry Andric void PPCRegisterInfo::lowerPrepareProbedAlloca( 614*5ffd83dbSDimitry Andric MachineBasicBlock::iterator II) const { 615*5ffd83dbSDimitry Andric MachineInstr &MI = *II; 616*5ffd83dbSDimitry Andric // Get the instruction's basic block. 617*5ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 618*5ffd83dbSDimitry Andric // Get the basic block's function. 619*5ffd83dbSDimitry Andric MachineFunction &MF = *MBB.getParent(); 620*5ffd83dbSDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 621*5ffd83dbSDimitry Andric // Get the instruction info. 622*5ffd83dbSDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 623*5ffd83dbSDimitry Andric // Determine whether 64-bit pointers are used. 624*5ffd83dbSDimitry Andric bool LP64 = TM.isPPC64(); 625*5ffd83dbSDimitry Andric DebugLoc dl = MI.getDebugLoc(); 626*5ffd83dbSDimitry Andric Register FramePointer = MI.getOperand(0).getReg(); 627*5ffd83dbSDimitry Andric Register FinalStackPtr = MI.getOperand(1).getReg(); 628*5ffd83dbSDimitry Andric bool KillNegSizeReg = MI.getOperand(2).isKill(); 629*5ffd83dbSDimitry Andric Register NegSizeReg = MI.getOperand(2).getReg(); 630*5ffd83dbSDimitry Andric prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer); 631*5ffd83dbSDimitry Andric if (LP64) { 632*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADD8), FinalStackPtr) 633*5ffd83dbSDimitry Andric .addReg(PPC::X1) 634*5ffd83dbSDimitry Andric .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 635*5ffd83dbSDimitry Andric 636*5ffd83dbSDimitry Andric } else { 637*5ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADD4), FinalStackPtr) 638*5ffd83dbSDimitry Andric .addReg(PPC::R1) 639*5ffd83dbSDimitry Andric .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 640*5ffd83dbSDimitry Andric } 641*5ffd83dbSDimitry Andric 642*5ffd83dbSDimitry Andric MBB.erase(II); 643*5ffd83dbSDimitry Andric } 644*5ffd83dbSDimitry Andric 6450b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAreaOffset( 6460b57cec5SDimitry Andric MachineBasicBlock::iterator II) const { 6470b57cec5SDimitry Andric // Get the instruction. 6480b57cec5SDimitry Andric MachineInstr &MI = *II; 6490b57cec5SDimitry Andric // Get the instruction's basic block. 6500b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 6510b57cec5SDimitry Andric // Get the basic block's function. 6520b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6530b57cec5SDimitry Andric // Get the frame info. 6540b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 6550b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 6560b57cec5SDimitry Andric // Get the instruction info. 6570b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 6600b57cec5SDimitry Andric bool is64Bit = TM.isPPC64(); 6610b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 6620b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), 6630b57cec5SDimitry Andric MI.getOperand(0).getReg()) 6640b57cec5SDimitry Andric .addImm(maxCallFrameSize); 6650b57cec5SDimitry Andric MBB.erase(II); 6660b57cec5SDimitry Andric } 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 6690b57cec5SDimitry Andric /// reserving a whole register (R0), we scrounge for one here. This generates 6700b57cec5SDimitry Andric /// code like this: 6710b57cec5SDimitry Andric /// 6720b57cec5SDimitry Andric /// mfcr rA ; Move the conditional register into GPR rA. 6730b57cec5SDimitry Andric /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 6740b57cec5SDimitry Andric /// stw rA, FI ; Store rA to the frame. 6750b57cec5SDimitry Andric /// 6760b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 6770b57cec5SDimitry Andric unsigned FrameIndex) const { 6780b57cec5SDimitry Andric // Get the instruction. 6790b57cec5SDimitry Andric MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 6800b57cec5SDimitry Andric // Get the instruction's basic block. 6810b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 6820b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6830b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 6840b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 6850b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 6880b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 6890b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 6900b57cec5SDimitry Andric 6918bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 6928bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric // We need to store the CR in the low 4-bits of the saved value. First, issue 6950b57cec5SDimitry Andric // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg. 6960b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 6970b57cec5SDimitry Andric .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric // If the saved register wasn't CR0, shift the bits left so that they are in 7000b57cec5SDimitry Andric // CR0's slot. 7010b57cec5SDimitry Andric if (SrcReg != PPC::CR0) { 702*5ffd83dbSDimitry Andric Register Reg1 = Reg; 7030b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric // rlwinm rA, rA, ShiftBits, 0, 31. 7060b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 7070b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill) 7080b57cec5SDimitry Andric .addImm(getEncodingValue(SrcReg) * 4) 7090b57cec5SDimitry Andric .addImm(0) 7100b57cec5SDimitry Andric .addImm(31); 7110b57cec5SDimitry Andric } 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 7140b57cec5SDimitry Andric .addReg(Reg, RegState::Kill), 7150b57cec5SDimitry Andric FrameIndex); 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric // Discard the pseudo instruction. 7180b57cec5SDimitry Andric MBB.erase(II); 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 7220b57cec5SDimitry Andric unsigned FrameIndex) const { 7230b57cec5SDimitry Andric // Get the instruction. 7240b57cec5SDimitry Andric MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 7250b57cec5SDimitry Andric // Get the instruction's basic block. 7260b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 7270b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 7280b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 7290b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 7300b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 7330b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 7340b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 7350b57cec5SDimitry Andric 7368bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7378bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 7380b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 7390b57cec5SDimitry Andric "RESTORE_CR does not define its destination"); 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 7420b57cec5SDimitry Andric Reg), FrameIndex); 7430b57cec5SDimitry Andric 7440b57cec5SDimitry Andric // If the reloaded register isn't CR0, shift the bits right so that they are 7450b57cec5SDimitry Andric // in the right CR's slot. 7460b57cec5SDimitry Andric if (DestReg != PPC::CR0) { 747*5ffd83dbSDimitry Andric Register Reg1 = Reg; 7480b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric unsigned ShiftBits = getEncodingValue(DestReg)*4; 7510b57cec5SDimitry Andric // rlwinm r11, r11, 32-ShiftBits, 0, 31. 7520b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 7530b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0) 7540b57cec5SDimitry Andric .addImm(31); 7550b57cec5SDimitry Andric } 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg) 7580b57cec5SDimitry Andric .addReg(Reg, RegState::Kill); 7590b57cec5SDimitry Andric 7600b57cec5SDimitry Andric // Discard the pseudo instruction. 7610b57cec5SDimitry Andric MBB.erase(II); 7620b57cec5SDimitry Andric } 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II, 7650b57cec5SDimitry Andric unsigned FrameIndex) const { 7660b57cec5SDimitry Andric // Get the instruction. 7670b57cec5SDimitry Andric MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset> 7680b57cec5SDimitry Andric // Get the instruction's basic block. 7690b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 7700b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 7710b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 7720b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 7730b57cec5SDimitry Andric const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo(); 7740b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 7770b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 7780b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 7790b57cec5SDimitry Andric 7808bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7818bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 7820b57cec5SDimitry Andric 7830b57cec5SDimitry Andric // Search up the BB to find the definition of the CR bit. 784480093f4SDimitry Andric MachineBasicBlock::reverse_iterator Ins = MI; 785480093f4SDimitry Andric MachineBasicBlock::reverse_iterator Rend = MBB.rend(); 786480093f4SDimitry Andric ++Ins; 7870b57cec5SDimitry Andric unsigned CRBitSpillDistance = 0; 788480093f4SDimitry Andric bool SeenUse = false; 789480093f4SDimitry Andric for (; Ins != Rend; ++Ins) { 7900b57cec5SDimitry Andric // Definition found. 7910b57cec5SDimitry Andric if (Ins->modifiesRegister(SrcReg, TRI)) 7920b57cec5SDimitry Andric break; 793480093f4SDimitry Andric // Use found. 794480093f4SDimitry Andric if (Ins->readsRegister(SrcReg, TRI)) 795480093f4SDimitry Andric SeenUse = true; 7960b57cec5SDimitry Andric // Unable to find CR bit definition within maximum search distance. 7970b57cec5SDimitry Andric if (CRBitSpillDistance == MaxCRBitSpillDist) { 7980b57cec5SDimitry Andric Ins = MI; 7990b57cec5SDimitry Andric break; 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric // Skip debug instructions when counting CR bit spill distance. 8020b57cec5SDimitry Andric if (!Ins->isDebugInstr()) 8030b57cec5SDimitry Andric CRBitSpillDistance++; 8040b57cec5SDimitry Andric } 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric // Unable to find the definition of the CR bit in the MBB. 8070b57cec5SDimitry Andric if (Ins == MBB.rend()) 8080b57cec5SDimitry Andric Ins = MI; 8090b57cec5SDimitry Andric 810480093f4SDimitry Andric bool SpillsKnownBit = false; 8110b57cec5SDimitry Andric // There is no need to extract the CR bit if its value is already known. 8120b57cec5SDimitry Andric switch (Ins->getOpcode()) { 8130b57cec5SDimitry Andric case PPC::CRUNSET: 8140b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg) 8150b57cec5SDimitry Andric .addImm(0); 816480093f4SDimitry Andric SpillsKnownBit = true; 8170b57cec5SDimitry Andric break; 8180b57cec5SDimitry Andric case PPC::CRSET: 8190b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg) 8200b57cec5SDimitry Andric .addImm(-32768); 821480093f4SDimitry Andric SpillsKnownBit = true; 8220b57cec5SDimitry Andric break; 8230b57cec5SDimitry Andric default: 824480093f4SDimitry Andric // On Power9, we can use SETB to extract the LT bit. This only works for 825480093f4SDimitry Andric // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value 826480093f4SDimitry Andric // of the bit we care about (32-bit sign bit) will be set to the value of 827480093f4SDimitry Andric // the LT bit (regardless of the other bits in the CR field). 828480093f4SDimitry Andric if (Subtarget.isISA3_0()) { 829480093f4SDimitry Andric if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT || 830480093f4SDimitry Andric SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT || 831480093f4SDimitry Andric SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT || 832480093f4SDimitry Andric SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) { 833480093f4SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg) 834480093f4SDimitry Andric .addReg(getCRFromCRBit(SrcReg), RegState::Undef); 835480093f4SDimitry Andric break; 836480093f4SDimitry Andric } 837480093f4SDimitry Andric } 838480093f4SDimitry Andric 8390b57cec5SDimitry Andric // We need to move the CR field that contains the CR bit we are spilling. 8400b57cec5SDimitry Andric // The super register may not be explicitly defined (i.e. it can be defined 8410b57cec5SDimitry Andric // by a CR-logical that only defines the subreg) so we state that the CR 8420b57cec5SDimitry Andric // field is undef. Also, in order to preserve the kill flag on the CR bit, 8430b57cec5SDimitry Andric // we add it as an implicit use. 8440b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 8450b57cec5SDimitry Andric .addReg(getCRFromCRBit(SrcReg), RegState::Undef) 8460b57cec5SDimitry Andric .addReg(SrcReg, 8470b57cec5SDimitry Andric RegState::Implicit | getKillRegState(MI.getOperand(0).isKill())); 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric // If the saved register wasn't CR0LT, shift the bits left so that the bit 8500b57cec5SDimitry Andric // to store is the first one. Mask all but that bit. 851*5ffd83dbSDimitry Andric Register Reg1 = Reg; 8520b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 8530b57cec5SDimitry Andric 8540b57cec5SDimitry Andric // rlwinm rA, rA, ShiftBits, 0, 0. 8550b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 8560b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill) 8570b57cec5SDimitry Andric .addImm(getEncodingValue(SrcReg)) 8580b57cec5SDimitry Andric .addImm(0).addImm(0); 8590b57cec5SDimitry Andric } 8600b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 8610b57cec5SDimitry Andric .addReg(Reg, RegState::Kill), 8620b57cec5SDimitry Andric FrameIndex); 8630b57cec5SDimitry Andric 864480093f4SDimitry Andric bool KillsCRBit = MI.killsRegister(SrcReg, TRI); 8650b57cec5SDimitry Andric // Discard the pseudo instruction. 8660b57cec5SDimitry Andric MBB.erase(II); 867480093f4SDimitry Andric if (SpillsKnownBit && KillsCRBit && !SeenUse) { 868480093f4SDimitry Andric Ins->setDesc(TII.get(PPC::UNENCODED_NOP)); 869480093f4SDimitry Andric Ins->RemoveOperand(0); 870480093f4SDimitry Andric } 8710b57cec5SDimitry Andric } 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, 8740b57cec5SDimitry Andric unsigned FrameIndex) const { 8750b57cec5SDimitry Andric // Get the instruction. 8760b57cec5SDimitry Andric MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset> 8770b57cec5SDimitry Andric // Get the instruction's basic block. 8780b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 8790b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 8800b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 8810b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 8820b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 8850b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 8860b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 8870b57cec5SDimitry Andric 8888bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 8898bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 8900b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 8910b57cec5SDimitry Andric "RESTORE_CRBIT does not define its destination"); 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 8940b57cec5SDimitry Andric Reg), FrameIndex); 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg); 8970b57cec5SDimitry Andric 8988bcb0991SDimitry Andric Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 8990b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO) 9000b57cec5SDimitry Andric .addReg(getCRFromCRBit(DestReg)); 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric unsigned ShiftBits = getEncodingValue(DestReg); 9030b57cec5SDimitry Andric // rlwimi r11, r10, 32-ShiftBits, ..., ... 9040b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO) 9050b57cec5SDimitry Andric .addReg(RegO, RegState::Kill) 9060b57cec5SDimitry Andric .addReg(Reg, RegState::Kill) 9070b57cec5SDimitry Andric .addImm(ShiftBits ? 32 - ShiftBits : 0) 9080b57cec5SDimitry Andric .addImm(ShiftBits) 9090b57cec5SDimitry Andric .addImm(ShiftBits); 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), 9120b57cec5SDimitry Andric getCRFromCRBit(DestReg)) 9130b57cec5SDimitry Andric .addReg(RegO, RegState::Kill) 9140b57cec5SDimitry Andric // Make sure we have a use dependency all the way through this 9150b57cec5SDimitry Andric // sequence of instructions. We can't have the other bits in the CR 9160b57cec5SDimitry Andric // modified in between the mfocrf and the mtocrf. 9170b57cec5SDimitry Andric .addReg(getCRFromCRBit(DestReg), RegState::Implicit); 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric // Discard the pseudo instruction. 9200b57cec5SDimitry Andric MBB.erase(II); 9210b57cec5SDimitry Andric } 9220b57cec5SDimitry Andric 9230b57cec5SDimitry Andric void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II, 9240b57cec5SDimitry Andric unsigned FrameIndex) const { 9250b57cec5SDimitry Andric // Get the instruction. 9260b57cec5SDimitry Andric MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset> 9270b57cec5SDimitry Andric // Get the instruction's basic block. 9280b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 9290b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 9300b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 9310b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 9320b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 9358bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(GPRC); 9368bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 9370b57cec5SDimitry Andric 9380b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg) 9390b57cec5SDimitry Andric .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric addFrameReference( 9420b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill), 9430b57cec5SDimitry Andric FrameIndex); 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric // Discard the pseudo instruction. 9460b57cec5SDimitry Andric MBB.erase(II); 9470b57cec5SDimitry Andric } 9480b57cec5SDimitry Andric 9490b57cec5SDimitry Andric void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II, 9500b57cec5SDimitry Andric unsigned FrameIndex) const { 9510b57cec5SDimitry Andric // Get the instruction. 9520b57cec5SDimitry Andric MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset> 9530b57cec5SDimitry Andric // Get the instruction's basic block. 9540b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 9550b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 9560b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 9570b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 9580b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 9590b57cec5SDimitry Andric 9600b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 9618bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(GPRC); 9628bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 9630b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 9640b57cec5SDimitry Andric "RESTORE_VRSAVE does not define its destination"); 9650b57cec5SDimitry Andric 9660b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ), 9670b57cec5SDimitry Andric Reg), FrameIndex); 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg) 9700b57cec5SDimitry Andric .addReg(Reg, RegState::Kill); 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric // Discard the pseudo instruction. 9730b57cec5SDimitry Andric MBB.erase(II); 9740b57cec5SDimitry Andric } 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 977*5ffd83dbSDimitry Andric Register Reg, int &FrameIdx) const { 978*5ffd83dbSDimitry Andric // For the nonvolatile condition registers (CR2, CR3, CR4) return true to 979*5ffd83dbSDimitry Andric // prevent allocating an additional frame slot. 980*5ffd83dbSDimitry Andric // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8, 981*5ffd83dbSDimitry Andric // for 32-bit AIX the CR save area is in the linkage area at SP+4. 982*5ffd83dbSDimitry Andric // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos 983*5ffd83dbSDimitry Andric // valid. 984*5ffd83dbSDimitry Andric // For 32-bit ELF, we have previously created the stack slot if needed, so 985*5ffd83dbSDimitry Andric // return its FrameIdx. 986*5ffd83dbSDimitry Andric if (PPC::CR2 <= Reg && Reg <= PPC::CR4) { 987*5ffd83dbSDimitry Andric FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex(); 9880b57cec5SDimitry Andric return true; 9890b57cec5SDimitry Andric } 9900b57cec5SDimitry Andric return false; 9910b57cec5SDimitry Andric } 9920b57cec5SDimitry Andric 9930b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is. 9940b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC) { 9950b57cec5SDimitry Andric switch (OpC) { 9960b57cec5SDimitry Andric default: 9970b57cec5SDimitry Andric return 1; 9980b57cec5SDimitry Andric case PPC::LWA: 9990b57cec5SDimitry Andric case PPC::LWA_32: 10000b57cec5SDimitry Andric case PPC::LD: 10010b57cec5SDimitry Andric case PPC::LDU: 10020b57cec5SDimitry Andric case PPC::STD: 10030b57cec5SDimitry Andric case PPC::STDU: 10040b57cec5SDimitry Andric case PPC::DFLOADf32: 10050b57cec5SDimitry Andric case PPC::DFLOADf64: 10060b57cec5SDimitry Andric case PPC::DFSTOREf32: 10070b57cec5SDimitry Andric case PPC::DFSTOREf64: 10080b57cec5SDimitry Andric case PPC::LXSD: 10090b57cec5SDimitry Andric case PPC::LXSSP: 10100b57cec5SDimitry Andric case PPC::STXSD: 10110b57cec5SDimitry Andric case PPC::STXSSP: 10120b57cec5SDimitry Andric return 4; 10130b57cec5SDimitry Andric case PPC::EVLDD: 10140b57cec5SDimitry Andric case PPC::EVSTDD: 10150b57cec5SDimitry Andric return 8; 10160b57cec5SDimitry Andric case PPC::LXV: 10170b57cec5SDimitry Andric case PPC::STXV: 10180b57cec5SDimitry Andric return 16; 10190b57cec5SDimitry Andric } 10200b57cec5SDimitry Andric } 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is. 10230b57cec5SDimitry Andric static unsigned offsetMinAlign(const MachineInstr &MI) { 10240b57cec5SDimitry Andric unsigned OpC = MI.getOpcode(); 10250b57cec5SDimitry Andric return offsetMinAlignForOpcode(OpC); 10260b57cec5SDimitry Andric } 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric // Return the OffsetOperandNo given the FIOperandNum (and the instruction). 10290b57cec5SDimitry Andric static unsigned getOffsetONFromFION(const MachineInstr &MI, 10300b57cec5SDimitry Andric unsigned FIOperandNum) { 10310b57cec5SDimitry Andric // Take into account whether it's an add or mem instruction 10320b57cec5SDimitry Andric unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2; 10330b57cec5SDimitry Andric if (MI.isInlineAsm()) 10340b57cec5SDimitry Andric OffsetOperandNo = FIOperandNum - 1; 10350b57cec5SDimitry Andric else if (MI.getOpcode() == TargetOpcode::STACKMAP || 10360b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::PATCHPOINT) 10370b57cec5SDimitry Andric OffsetOperandNo = FIOperandNum + 1; 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric return OffsetOperandNo; 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric void 10430b57cec5SDimitry Andric PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 10440b57cec5SDimitry Andric int SPAdj, unsigned FIOperandNum, 10450b57cec5SDimitry Andric RegScavenger *RS) const { 10460b57cec5SDimitry Andric assert(SPAdj == 0 && "Unexpected"); 10470b57cec5SDimitry Andric 10480b57cec5SDimitry Andric // Get the instruction. 10490b57cec5SDimitry Andric MachineInstr &MI = *II; 10500b57cec5SDimitry Andric // Get the instruction's basic block. 10510b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 10520b57cec5SDimitry Andric // Get the basic block's function. 10530b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 10540b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 10550b57cec5SDimitry Andric // Get the instruction info. 10560b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 10570b57cec5SDimitry Andric // Get the frame info. 10580b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 10590b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 10620b57cec5SDimitry Andric 10630b57cec5SDimitry Andric // Get the frame index. 10640b57cec5SDimitry Andric int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric // Get the frame pointer save index. Users of this index are primarily 10670b57cec5SDimitry Andric // DYNALLOC instructions. 10680b57cec5SDimitry Andric PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 10690b57cec5SDimitry Andric int FPSI = FI->getFramePointerSaveIndex(); 10700b57cec5SDimitry Andric // Get the instruction opcode. 10710b57cec5SDimitry Andric unsigned OpC = MI.getOpcode(); 10720b57cec5SDimitry Andric 10730b57cec5SDimitry Andric if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) { 10740b57cec5SDimitry Andric lowerDynamicAreaOffset(II); 10750b57cec5SDimitry Andric return; 10760b57cec5SDimitry Andric } 10770b57cec5SDimitry Andric 10780b57cec5SDimitry Andric // Special case for dynamic alloca. 10790b57cec5SDimitry Andric if (FPSI && FrameIndex == FPSI && 10800b57cec5SDimitry Andric (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) { 10810b57cec5SDimitry Andric lowerDynamicAlloc(II); 10820b57cec5SDimitry Andric return; 10830b57cec5SDimitry Andric } 10840b57cec5SDimitry Andric 1085*5ffd83dbSDimitry Andric if (FPSI && FrameIndex == FPSI && 1086*5ffd83dbSDimitry Andric (OpC == PPC::PREPARE_PROBED_ALLOCA_64 || 1087*5ffd83dbSDimitry Andric OpC == PPC::PREPARE_PROBED_ALLOCA_32)) { 1088*5ffd83dbSDimitry Andric lowerPrepareProbedAlloca(II); 1089*5ffd83dbSDimitry Andric return; 1090*5ffd83dbSDimitry Andric } 1091*5ffd83dbSDimitry Andric 10920b57cec5SDimitry Andric // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc. 10930b57cec5SDimitry Andric if (OpC == PPC::SPILL_CR) { 10940b57cec5SDimitry Andric lowerCRSpilling(II, FrameIndex); 10950b57cec5SDimitry Andric return; 10960b57cec5SDimitry Andric } else if (OpC == PPC::RESTORE_CR) { 10970b57cec5SDimitry Andric lowerCRRestore(II, FrameIndex); 10980b57cec5SDimitry Andric return; 10990b57cec5SDimitry Andric } else if (OpC == PPC::SPILL_CRBIT) { 11000b57cec5SDimitry Andric lowerCRBitSpilling(II, FrameIndex); 11010b57cec5SDimitry Andric return; 11020b57cec5SDimitry Andric } else if (OpC == PPC::RESTORE_CRBIT) { 11030b57cec5SDimitry Andric lowerCRBitRestore(II, FrameIndex); 11040b57cec5SDimitry Andric return; 11050b57cec5SDimitry Andric } else if (OpC == PPC::SPILL_VRSAVE) { 11060b57cec5SDimitry Andric lowerVRSAVESpilling(II, FrameIndex); 11070b57cec5SDimitry Andric return; 11080b57cec5SDimitry Andric } else if (OpC == PPC::RESTORE_VRSAVE) { 11090b57cec5SDimitry Andric lowerVRSAVERestore(II, FrameIndex); 11100b57cec5SDimitry Andric return; 11110b57cec5SDimitry Andric } 11120b57cec5SDimitry Andric 11130b57cec5SDimitry Andric // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). 11140b57cec5SDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister( 11150b57cec5SDimitry Andric FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false); 11160b57cec5SDimitry Andric 11170b57cec5SDimitry Andric // If the instruction is not present in ImmToIdxMap, then it has no immediate 11180b57cec5SDimitry Andric // form (and must be r+r). 11190b57cec5SDimitry Andric bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP && 11200b57cec5SDimitry Andric OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC); 11210b57cec5SDimitry Andric 11220b57cec5SDimitry Andric // Now add the frame object offset to the offset from r1. 11230b57cec5SDimitry Andric int Offset = MFI.getObjectOffset(FrameIndex); 11240b57cec5SDimitry Andric Offset += MI.getOperand(OffsetOperandNo).getImm(); 11250b57cec5SDimitry Andric 11260b57cec5SDimitry Andric // If we're not using a Frame Pointer that has been set to the value of the 11270b57cec5SDimitry Andric // SP before having the stack size subtracted from it, then add the stack size 11280b57cec5SDimitry Andric // to Offset to get the correct offset. 11290b57cec5SDimitry Andric // Naked functions have stack size 0, although getStackSize may not reflect 11300b57cec5SDimitry Andric // that because we didn't call all the pieces that compute it for naked 11310b57cec5SDimitry Andric // functions. 11320b57cec5SDimitry Andric if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) { 11330b57cec5SDimitry Andric if (!(hasBasePointer(MF) && FrameIndex < 0)) 11340b57cec5SDimitry Andric Offset += MFI.getStackSize(); 11350b57cec5SDimitry Andric } 11360b57cec5SDimitry Andric 11370b57cec5SDimitry Andric // If we can, encode the offset directly into the instruction. If this is a 11380b57cec5SDimitry Andric // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If 11390b57cec5SDimitry Andric // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits 11400b57cec5SDimitry Andric // clear can be encoded. This is extremely uncommon, because normally you 11410b57cec5SDimitry Andric // only "std" to a stack slot that is at least 4-byte aligned, but it can 11420b57cec5SDimitry Andric // happen in invalid code. 11430b57cec5SDimitry Andric assert(OpC != PPC::DBG_VALUE && 11440b57cec5SDimitry Andric "This should be handled in a target-independent way"); 11450b57cec5SDimitry Andric bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ? 11460b57cec5SDimitry Andric isUInt<8>(Offset) : 11470b57cec5SDimitry Andric isInt<16>(Offset); 11480b57cec5SDimitry Andric if (!noImmForm && ((OffsetFitsMnemonic && 11490b57cec5SDimitry Andric ((Offset % offsetMinAlign(MI)) == 0)) || 11500b57cec5SDimitry Andric OpC == TargetOpcode::STACKMAP || 11510b57cec5SDimitry Andric OpC == TargetOpcode::PATCHPOINT)) { 11520b57cec5SDimitry Andric MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 11530b57cec5SDimitry Andric return; 11540b57cec5SDimitry Andric } 11550b57cec5SDimitry Andric 11560b57cec5SDimitry Andric // The offset doesn't fit into a single register, scavenge one to build the 11570b57cec5SDimitry Andric // offset in. 11580b57cec5SDimitry Andric 11590b57cec5SDimitry Andric bool is64Bit = TM.isPPC64(); 11600b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 11610b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 11620b57cec5SDimitry Andric const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC; 1163*5ffd83dbSDimitry Andric Register SRegHi = MF.getRegInfo().createVirtualRegister(RC), 11640b57cec5SDimitry Andric SReg = MF.getRegInfo().createVirtualRegister(RC); 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric // Insert a set of rA with the full offset value before the ld, st, or add 11670b57cec5SDimitry Andric if (isInt<16>(Offset)) 11680b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg) 11690b57cec5SDimitry Andric .addImm(Offset); 11700b57cec5SDimitry Andric else { 11710b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi) 11720b57cec5SDimitry Andric .addImm(Offset >> 16); 11730b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg) 11740b57cec5SDimitry Andric .addReg(SRegHi, RegState::Kill) 11750b57cec5SDimitry Andric .addImm(Offset); 11760b57cec5SDimitry Andric } 11770b57cec5SDimitry Andric 11780b57cec5SDimitry Andric // Convert into indexed form of the instruction: 11790b57cec5SDimitry Andric // 11800b57cec5SDimitry Andric // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 11810b57cec5SDimitry Andric // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 11820b57cec5SDimitry Andric unsigned OperandBase; 11830b57cec5SDimitry Andric 11840b57cec5SDimitry Andric if (noImmForm) 11850b57cec5SDimitry Andric OperandBase = 1; 11860b57cec5SDimitry Andric else if (OpC != TargetOpcode::INLINEASM && 11870b57cec5SDimitry Andric OpC != TargetOpcode::INLINEASM_BR) { 11880b57cec5SDimitry Andric assert(ImmToIdxMap.count(OpC) && 11890b57cec5SDimitry Andric "No indexed form of load or store available!"); 11900b57cec5SDimitry Andric unsigned NewOpcode = ImmToIdxMap.find(OpC)->second; 11910b57cec5SDimitry Andric MI.setDesc(TII.get(NewOpcode)); 11920b57cec5SDimitry Andric OperandBase = 1; 11930b57cec5SDimitry Andric } else { 11940b57cec5SDimitry Andric OperandBase = OffsetOperandNo; 11950b57cec5SDimitry Andric } 11960b57cec5SDimitry Andric 11978bcb0991SDimitry Andric Register StackReg = MI.getOperand(FIOperandNum).getReg(); 11980b57cec5SDimitry Andric MI.getOperand(OperandBase).ChangeToRegister(StackReg, false); 11990b57cec5SDimitry Andric MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true); 12000b57cec5SDimitry Andric } 12010b57cec5SDimitry Andric 12020b57cec5SDimitry Andric Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 12030b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 12040b57cec5SDimitry Andric 12050b57cec5SDimitry Andric if (!TM.isPPC64()) 12060b57cec5SDimitry Andric return TFI->hasFP(MF) ? PPC::R31 : PPC::R1; 12070b57cec5SDimitry Andric else 12080b57cec5SDimitry Andric return TFI->hasFP(MF) ? PPC::X31 : PPC::X1; 12090b57cec5SDimitry Andric } 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andric Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const { 12120b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 12130b57cec5SDimitry Andric if (!hasBasePointer(MF)) 12140b57cec5SDimitry Andric return getFrameRegister(MF); 12150b57cec5SDimitry Andric 12160b57cec5SDimitry Andric if (TM.isPPC64()) 12170b57cec5SDimitry Andric return PPC::X30; 12180b57cec5SDimitry Andric 12190b57cec5SDimitry Andric if (Subtarget.isSVR4ABI() && TM.isPositionIndependent()) 12200b57cec5SDimitry Andric return PPC::R29; 12210b57cec5SDimitry Andric 12220b57cec5SDimitry Andric return PPC::R30; 12230b57cec5SDimitry Andric } 12240b57cec5SDimitry Andric 12250b57cec5SDimitry Andric bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const { 12260b57cec5SDimitry Andric if (!EnableBasePointer) 12270b57cec5SDimitry Andric return false; 12280b57cec5SDimitry Andric if (AlwaysBasePointer) 12290b57cec5SDimitry Andric return true; 12300b57cec5SDimitry Andric 12310b57cec5SDimitry Andric // If we need to realign the stack, then the stack pointer can no longer 12320b57cec5SDimitry Andric // serve as an offset into the caller's stack space. As a result, we need a 12330b57cec5SDimitry Andric // base pointer. 12340b57cec5SDimitry Andric return needsStackRealignment(MF); 12350b57cec5SDimitry Andric } 12360b57cec5SDimitry Andric 12370b57cec5SDimitry Andric /// Returns true if the instruction's frame index 12380b57cec5SDimitry Andric /// reference would be better served by a base register other than FP 12390b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index 12400b57cec5SDimitry Andric /// references it should create new base registers for. 12410b57cec5SDimitry Andric bool PPCRegisterInfo:: 12420b57cec5SDimitry Andric needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 12430b57cec5SDimitry Andric assert(Offset < 0 && "Local offset must be negative"); 12440b57cec5SDimitry Andric 12450b57cec5SDimitry Andric // It's the load/store FI references that cause issues, as it can be difficult 12460b57cec5SDimitry Andric // to materialize the offset if it won't fit in the literal field. Estimate 12470b57cec5SDimitry Andric // based on the size of the local frame and some conservative assumptions 12480b57cec5SDimitry Andric // about the rest of the stack frame (note, this is pre-regalloc, so 12490b57cec5SDimitry Andric // we don't know everything for certain yet) whether this offset is likely 12500b57cec5SDimitry Andric // to be out of range of the immediate. Return true if so. 12510b57cec5SDimitry Andric 12520b57cec5SDimitry Andric // We only generate virtual base registers for loads and stores that have 12530b57cec5SDimitry Andric // an r+i form. Return false for everything else. 12540b57cec5SDimitry Andric unsigned OpC = MI->getOpcode(); 12550b57cec5SDimitry Andric if (!ImmToIdxMap.count(OpC)) 12560b57cec5SDimitry Andric return false; 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric // Don't generate a new virtual base register just to add zero to it. 12590b57cec5SDimitry Andric if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) && 12600b57cec5SDimitry Andric MI->getOperand(2).getImm() == 0) 12610b57cec5SDimitry Andric return false; 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI->getParent(); 12640b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 12650b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 12660b57cec5SDimitry Andric unsigned StackEst = TFI->determineFrameLayout(MF, true); 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric // If we likely don't need a stack frame, then we probably don't need a 12690b57cec5SDimitry Andric // virtual base register either. 12700b57cec5SDimitry Andric if (!StackEst) 12710b57cec5SDimitry Andric return false; 12720b57cec5SDimitry Andric 12730b57cec5SDimitry Andric // Estimate an offset from the stack pointer. 12740b57cec5SDimitry Andric // The incoming offset is relating to the SP at the start of the function, 12750b57cec5SDimitry Andric // but when we access the local it'll be relative to the SP after local 12760b57cec5SDimitry Andric // allocation, so adjust our SP-relative offset by that allocation size. 12770b57cec5SDimitry Andric Offset += StackEst; 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric // The frame pointer will point to the end of the stack, so estimate the 12800b57cec5SDimitry Andric // offset as the difference between the object offset and the FP location. 12810b57cec5SDimitry Andric return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset); 12820b57cec5SDimitry Andric } 12830b57cec5SDimitry Andric 12840b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to 12850b57cec5SDimitry Andric /// be a pointer to FrameIdx at the beginning of the basic block. 1286*5ffd83dbSDimitry Andric void PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 1287*5ffd83dbSDimitry Andric Register BaseReg, 1288*5ffd83dbSDimitry Andric int FrameIdx, 12890b57cec5SDimitry Andric int64_t Offset) const { 12900b57cec5SDimitry Andric unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI; 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric MachineBasicBlock::iterator Ins = MBB->begin(); 12930b57cec5SDimitry Andric DebugLoc DL; // Defaults to "unknown" 12940b57cec5SDimitry Andric if (Ins != MBB->end()) 12950b57cec5SDimitry Andric DL = Ins->getDebugLoc(); 12960b57cec5SDimitry Andric 12970b57cec5SDimitry Andric const MachineFunction &MF = *MBB->getParent(); 12980b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 12990b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 13000b57cec5SDimitry Andric const MCInstrDesc &MCID = TII.get(ADDriOpc); 13010b57cec5SDimitry Andric MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 13020b57cec5SDimitry Andric MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); 13030b57cec5SDimitry Andric 13040b57cec5SDimitry Andric BuildMI(*MBB, Ins, DL, MCID, BaseReg) 13050b57cec5SDimitry Andric .addFrameIndex(FrameIdx).addImm(Offset); 13060b57cec5SDimitry Andric } 13070b57cec5SDimitry Andric 1308*5ffd83dbSDimitry Andric void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 13090b57cec5SDimitry Andric int64_t Offset) const { 13100b57cec5SDimitry Andric unsigned FIOperandNum = 0; 13110b57cec5SDimitry Andric while (!MI.getOperand(FIOperandNum).isFI()) { 13120b57cec5SDimitry Andric ++FIOperandNum; 13130b57cec5SDimitry Andric assert(FIOperandNum < MI.getNumOperands() && 13140b57cec5SDimitry Andric "Instr doesn't have FrameIndex operand!"); 13150b57cec5SDimitry Andric } 13160b57cec5SDimitry Andric 13170b57cec5SDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false); 13180b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 13190b57cec5SDimitry Andric Offset += MI.getOperand(OffsetOperandNo).getImm(); 13200b57cec5SDimitry Andric MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 13210b57cec5SDimitry Andric 13220b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 13230b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 13240b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 13250b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 13260b57cec5SDimitry Andric const MCInstrDesc &MCID = MI.getDesc(); 13270b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 13280b57cec5SDimitry Andric MRI.constrainRegClass(BaseReg, 13290b57cec5SDimitry Andric TII.getRegClass(MCID, FIOperandNum, this, MF)); 13300b57cec5SDimitry Andric } 13310b57cec5SDimitry Andric 13320b57cec5SDimitry Andric bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 1333*5ffd83dbSDimitry Andric Register BaseReg, 13340b57cec5SDimitry Andric int64_t Offset) const { 13350b57cec5SDimitry Andric unsigned FIOperandNum = 0; 13360b57cec5SDimitry Andric while (!MI->getOperand(FIOperandNum).isFI()) { 13370b57cec5SDimitry Andric ++FIOperandNum; 13380b57cec5SDimitry Andric assert(FIOperandNum < MI->getNumOperands() && 13390b57cec5SDimitry Andric "Instr doesn't have FrameIndex operand!"); 13400b57cec5SDimitry Andric } 13410b57cec5SDimitry Andric 13420b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum); 13430b57cec5SDimitry Andric Offset += MI->getOperand(OffsetOperandNo).getImm(); 13440b57cec5SDimitry Andric 13450b57cec5SDimitry Andric return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm 13460b57cec5SDimitry Andric MI->getOpcode() == TargetOpcode::STACKMAP || 13470b57cec5SDimitry Andric MI->getOpcode() == TargetOpcode::PATCHPOINT || 13480b57cec5SDimitry Andric (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0); 13490b57cec5SDimitry Andric } 1350