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) { 1455ffd83dbSDimitry Andric if (!TM.isPPC64() && Subtarget.isAIXABI()) 1465ffd83dbSDimitry 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). 1555ffd83dbSDimitry Andric // We do not need to treat R2 as callee-saved when using PC-Relative calls 1565ffd83dbSDimitry Andric // because any direct uses of R2 will cause it to be reserved. If the function 1575ffd83dbSDimitry Andric // is a leaf or the only uses of R2 are implicit uses for calls, the calls 1585ffd83dbSDimitry Andric // will use the @notoc relocation which will cause this function to set the 1595ffd83dbSDimitry Andric // st_other bit to 1, thereby communicating to its caller that it arbitrarily 1605ffd83dbSDimitry Andric // clobbers the TOC. 1615ffd83dbSDimitry Andric bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) && 1625ffd83dbSDimitry Andric !Subtarget.isUsingPCRelativeCalls(); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // Cold calling convention CSRs. 1650b57cec5SDimitry Andric if (MF->getFunction().getCallingConv() == CallingConv::Cold) { 1665ffd83dbSDimitry Andric if (Subtarget.isAIXABI()) 1675ffd83dbSDimitry 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()) 1855ffd83dbSDimitry Andric return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList 1865ffd83dbSDimitry Andric : CSR_PPC64_Altivec_SaveList; 1875ffd83dbSDimitry Andric return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList; 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric // 32-bit targets. 1905ffd83dbSDimitry Andric if (Subtarget.isAIXABI()) 1915ffd83dbSDimitry 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."); 2135ffd83dbSDimitry 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 2255ffd83dbSDimitry Andric return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask 2265ffd83dbSDimitry Andric : CSR_PPC64_RegMask) 2275ffd83dbSDimitry Andric : (Subtarget.hasAltivec() 2285ffd83dbSDimitry Andric ? CSR_SVR432_Altivec_RegMask 2295ffd83dbSDimitry 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 3435ffd83dbSDimitry Andric const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg); 3445ffd83dbSDimitry 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 3645ffd83dbSDimitry 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(); 4835ffd83dbSDimitry Andric Align MaxAlign = MFI.getMaxAlign(); 4845ffd83dbSDimitry Andric assert(isAligned(MaxAlign, maxCallFrameSize) && 4850b57cec5SDimitry Andric "Maximum call-frame size not sufficiently aligned"); 4865ffd83dbSDimitry 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 4945ffd83dbSDimitry 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 5195ffd83dbSDimitry Andric /// To accomplish dynamic stack allocation, we have to calculate exact size 5205ffd83dbSDimitry Andric /// subtracted from the stack pointer according alignment information and get 5215ffd83dbSDimitry Andric /// previous frame pointer. 5225ffd83dbSDimitry Andric void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II, 5235ffd83dbSDimitry Andric Register &NegSizeReg, 5245ffd83dbSDimitry Andric bool &KillNegSizeReg, 5255ffd83dbSDimitry Andric Register &FramePointer) const { 5265ffd83dbSDimitry Andric // Get the instruction. 5275ffd83dbSDimitry Andric MachineInstr &MI = *II; 5285ffd83dbSDimitry Andric // Get the instruction's basic block. 5295ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 5305ffd83dbSDimitry Andric // Get the basic block's function. 5315ffd83dbSDimitry Andric MachineFunction &MF = *MBB.getParent(); 5325ffd83dbSDimitry Andric // Get the frame info. 5335ffd83dbSDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 5345ffd83dbSDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 5355ffd83dbSDimitry Andric // Get the instruction info. 5365ffd83dbSDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 5375ffd83dbSDimitry Andric // Determine whether 64-bit pointers are used. 5385ffd83dbSDimitry Andric bool LP64 = TM.isPPC64(); 5395ffd83dbSDimitry Andric DebugLoc dl = MI.getDebugLoc(); 5405ffd83dbSDimitry Andric // Get the total frame size. 5415ffd83dbSDimitry Andric unsigned FrameSize = MFI.getStackSize(); 5425ffd83dbSDimitry Andric 5435ffd83dbSDimitry Andric // Get stack alignments. 5445ffd83dbSDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 5455ffd83dbSDimitry Andric Align TargetAlign = TFI->getStackAlign(); 5465ffd83dbSDimitry Andric Align MaxAlign = MFI.getMaxAlign(); 5475ffd83dbSDimitry Andric 5485ffd83dbSDimitry Andric // Determine the previous frame's address. If FrameSize can't be 5495ffd83dbSDimitry Andric // represented as 16 bits or we need special alignment, then we load the 5505ffd83dbSDimitry Andric // previous frame's address from 0(SP). Why not do an addis of the hi? 5515ffd83dbSDimitry Andric // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 5525ffd83dbSDimitry Andric // Constructing the constant and adding would take 3 instructions. 5535ffd83dbSDimitry Andric // Fortunately, a frame greater than 32K is rare. 5545ffd83dbSDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 5555ffd83dbSDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 5565ffd83dbSDimitry Andric 5575ffd83dbSDimitry Andric if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 5585ffd83dbSDimitry Andric if (LP64) 5595ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer) 5605ffd83dbSDimitry Andric .addReg(PPC::X31) 5615ffd83dbSDimitry Andric .addImm(FrameSize); 5625ffd83dbSDimitry Andric else 5635ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer) 5645ffd83dbSDimitry Andric .addReg(PPC::R31) 5655ffd83dbSDimitry Andric .addImm(FrameSize); 5665ffd83dbSDimitry Andric } else if (LP64) { 5675ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer) 5685ffd83dbSDimitry Andric .addImm(0) 5695ffd83dbSDimitry Andric .addReg(PPC::X1); 5705ffd83dbSDimitry Andric } else { 5715ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer) 5725ffd83dbSDimitry Andric .addImm(0) 5735ffd83dbSDimitry Andric .addReg(PPC::R1); 5745ffd83dbSDimitry Andric } 5755ffd83dbSDimitry Andric // Determine the actual NegSizeReg according to alignment info. 5765ffd83dbSDimitry Andric if (LP64) { 5775ffd83dbSDimitry Andric if (MaxAlign > TargetAlign) { 5785ffd83dbSDimitry Andric unsigned UnalNegSizeReg = NegSizeReg; 5795ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 5805ffd83dbSDimitry Andric 5815ffd83dbSDimitry Andric // Unfortunately, there is no andi, only andi., and we can't insert that 5825ffd83dbSDimitry Andric // here because we might clobber cr0 while it is live. 5835ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg) 5845ffd83dbSDimitry Andric .addImm(~(MaxAlign.value() - 1)); 5855ffd83dbSDimitry Andric 5865ffd83dbSDimitry Andric unsigned NegSizeReg1 = NegSizeReg; 5875ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 5885ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg) 5895ffd83dbSDimitry Andric .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 5905ffd83dbSDimitry Andric .addReg(NegSizeReg1, RegState::Kill); 5915ffd83dbSDimitry Andric KillNegSizeReg = true; 5925ffd83dbSDimitry Andric } 5935ffd83dbSDimitry Andric } else { 5945ffd83dbSDimitry Andric if (MaxAlign > TargetAlign) { 5955ffd83dbSDimitry Andric unsigned UnalNegSizeReg = NegSizeReg; 5965ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 5975ffd83dbSDimitry Andric 5985ffd83dbSDimitry Andric // Unfortunately, there is no andi, only andi., and we can't insert that 5995ffd83dbSDimitry Andric // here because we might clobber cr0 while it is live. 6005ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg) 6015ffd83dbSDimitry Andric .addImm(~(MaxAlign.value() - 1)); 6025ffd83dbSDimitry Andric 6035ffd83dbSDimitry Andric unsigned NegSizeReg1 = NegSizeReg; 6045ffd83dbSDimitry Andric NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 6055ffd83dbSDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg) 6065ffd83dbSDimitry Andric .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 6075ffd83dbSDimitry Andric .addReg(NegSizeReg1, RegState::Kill); 6085ffd83dbSDimitry Andric KillNegSizeReg = true; 6095ffd83dbSDimitry Andric } 6105ffd83dbSDimitry Andric } 6115ffd83dbSDimitry Andric } 6125ffd83dbSDimitry Andric 6135ffd83dbSDimitry Andric void PPCRegisterInfo::lowerPrepareProbedAlloca( 6145ffd83dbSDimitry Andric MachineBasicBlock::iterator II) const { 6155ffd83dbSDimitry Andric MachineInstr &MI = *II; 6165ffd83dbSDimitry Andric // Get the instruction's basic block. 6175ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 6185ffd83dbSDimitry Andric // Get the basic block's function. 6195ffd83dbSDimitry Andric MachineFunction &MF = *MBB.getParent(); 6205ffd83dbSDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 6215ffd83dbSDimitry Andric // Get the instruction info. 6225ffd83dbSDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 6235ffd83dbSDimitry Andric // Determine whether 64-bit pointers are used. 6245ffd83dbSDimitry Andric bool LP64 = TM.isPPC64(); 6255ffd83dbSDimitry Andric DebugLoc dl = MI.getDebugLoc(); 6265ffd83dbSDimitry Andric Register FramePointer = MI.getOperand(0).getReg(); 627*590d96feSDimitry Andric const Register ActualNegSizeReg = MI.getOperand(1).getReg(); 6285ffd83dbSDimitry Andric bool KillNegSizeReg = MI.getOperand(2).isKill(); 6295ffd83dbSDimitry Andric Register NegSizeReg = MI.getOperand(2).getReg(); 630*590d96feSDimitry Andric const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR); 631*590d96feSDimitry Andric // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg. 632*590d96feSDimitry Andric if (FramePointer == NegSizeReg) { 633*590d96feSDimitry Andric assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, " 634*590d96feSDimitry Andric "NegSizeReg should be killed"); 635*590d96feSDimitry Andric // FramePointer is clobbered earlier than the use of NegSizeReg in 636*590d96feSDimitry Andric // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid 637*590d96feSDimitry Andric // misuse. 638*590d96feSDimitry Andric BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg) 639*590d96feSDimitry Andric .addReg(NegSizeReg) 640*590d96feSDimitry Andric .addReg(NegSizeReg); 641*590d96feSDimitry Andric NegSizeReg = ActualNegSizeReg; 642*590d96feSDimitry Andric KillNegSizeReg = false; 6435ffd83dbSDimitry Andric } 644*590d96feSDimitry Andric prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer); 645*590d96feSDimitry Andric // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign > 646*590d96feSDimitry Andric // TargetAlign. 647*590d96feSDimitry Andric if (NegSizeReg != ActualNegSizeReg) 648*590d96feSDimitry Andric BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg) 649*590d96feSDimitry Andric .addReg(NegSizeReg) 650*590d96feSDimitry Andric .addReg(NegSizeReg); 6515ffd83dbSDimitry Andric MBB.erase(II); 6525ffd83dbSDimitry Andric } 6535ffd83dbSDimitry Andric 6540b57cec5SDimitry Andric void PPCRegisterInfo::lowerDynamicAreaOffset( 6550b57cec5SDimitry Andric MachineBasicBlock::iterator II) const { 6560b57cec5SDimitry Andric // Get the instruction. 6570b57cec5SDimitry Andric MachineInstr &MI = *II; 6580b57cec5SDimitry Andric // Get the instruction's basic block. 6590b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 6600b57cec5SDimitry Andric // Get the basic block's function. 6610b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6620b57cec5SDimitry Andric // Get the frame info. 6630b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 6640b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 6650b57cec5SDimitry Andric // Get the instruction info. 6660b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 6690b57cec5SDimitry Andric bool is64Bit = TM.isPPC64(); 6700b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 6710b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), 6720b57cec5SDimitry Andric MI.getOperand(0).getReg()) 6730b57cec5SDimitry Andric .addImm(maxCallFrameSize); 6740b57cec5SDimitry Andric MBB.erase(II); 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 6780b57cec5SDimitry Andric /// reserving a whole register (R0), we scrounge for one here. This generates 6790b57cec5SDimitry Andric /// code like this: 6800b57cec5SDimitry Andric /// 6810b57cec5SDimitry Andric /// mfcr rA ; Move the conditional register into GPR rA. 6820b57cec5SDimitry Andric /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 6830b57cec5SDimitry Andric /// stw rA, FI ; Store rA to the frame. 6840b57cec5SDimitry Andric /// 6850b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 6860b57cec5SDimitry Andric unsigned FrameIndex) const { 6870b57cec5SDimitry Andric // Get the instruction. 6880b57cec5SDimitry Andric MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 6890b57cec5SDimitry Andric // Get the instruction's basic block. 6900b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 6910b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6920b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 6930b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 6940b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 6950b57cec5SDimitry Andric 6960b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 6970b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 6980b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 6990b57cec5SDimitry Andric 7008bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7018bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric // We need to store the CR in the low 4-bits of the saved value. First, issue 7040b57cec5SDimitry Andric // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg. 7050b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 7060b57cec5SDimitry Andric .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric // If the saved register wasn't CR0, shift the bits left so that they are in 7090b57cec5SDimitry Andric // CR0's slot. 7100b57cec5SDimitry Andric if (SrcReg != PPC::CR0) { 7115ffd83dbSDimitry Andric Register Reg1 = Reg; 7120b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7130b57cec5SDimitry Andric 7140b57cec5SDimitry Andric // rlwinm rA, rA, ShiftBits, 0, 31. 7150b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 7160b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill) 7170b57cec5SDimitry Andric .addImm(getEncodingValue(SrcReg) * 4) 7180b57cec5SDimitry Andric .addImm(0) 7190b57cec5SDimitry Andric .addImm(31); 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 7230b57cec5SDimitry Andric .addReg(Reg, RegState::Kill), 7240b57cec5SDimitry Andric FrameIndex); 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric // Discard the pseudo instruction. 7270b57cec5SDimitry Andric MBB.erase(II); 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 7310b57cec5SDimitry Andric unsigned FrameIndex) const { 7320b57cec5SDimitry Andric // Get the instruction. 7330b57cec5SDimitry Andric MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 7340b57cec5SDimitry Andric // Get the instruction's basic block. 7350b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 7360b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 7370b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 7380b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 7390b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 7420b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 7430b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 7440b57cec5SDimitry Andric 7458bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7468bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 7470b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 7480b57cec5SDimitry Andric "RESTORE_CR does not define its destination"); 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 7510b57cec5SDimitry Andric Reg), FrameIndex); 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric // If the reloaded register isn't CR0, shift the bits right so that they are 7540b57cec5SDimitry Andric // in the right CR's slot. 7550b57cec5SDimitry Andric if (DestReg != PPC::CR0) { 7565ffd83dbSDimitry Andric Register Reg1 = Reg; 7570b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric unsigned ShiftBits = getEncodingValue(DestReg)*4; 7600b57cec5SDimitry Andric // rlwinm r11, r11, 32-ShiftBits, 0, 31. 7610b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 7620b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0) 7630b57cec5SDimitry Andric .addImm(31); 7640b57cec5SDimitry Andric } 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg) 7670b57cec5SDimitry Andric .addReg(Reg, RegState::Kill); 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric // Discard the pseudo instruction. 7700b57cec5SDimitry Andric MBB.erase(II); 7710b57cec5SDimitry Andric } 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II, 7740b57cec5SDimitry Andric unsigned FrameIndex) const { 7750b57cec5SDimitry Andric // Get the instruction. 7760b57cec5SDimitry Andric MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset> 7770b57cec5SDimitry Andric // Get the instruction's basic block. 7780b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 7790b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 7800b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 7810b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 7820b57cec5SDimitry Andric const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo(); 7830b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 7840b57cec5SDimitry Andric 7850b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 7860b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 7870b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 7880b57cec5SDimitry Andric 7898bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 7908bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric // Search up the BB to find the definition of the CR bit. 793480093f4SDimitry Andric MachineBasicBlock::reverse_iterator Ins = MI; 794480093f4SDimitry Andric MachineBasicBlock::reverse_iterator Rend = MBB.rend(); 795480093f4SDimitry Andric ++Ins; 7960b57cec5SDimitry Andric unsigned CRBitSpillDistance = 0; 797480093f4SDimitry Andric bool SeenUse = false; 798480093f4SDimitry Andric for (; Ins != Rend; ++Ins) { 7990b57cec5SDimitry Andric // Definition found. 8000b57cec5SDimitry Andric if (Ins->modifiesRegister(SrcReg, TRI)) 8010b57cec5SDimitry Andric break; 802480093f4SDimitry Andric // Use found. 803480093f4SDimitry Andric if (Ins->readsRegister(SrcReg, TRI)) 804480093f4SDimitry Andric SeenUse = true; 8050b57cec5SDimitry Andric // Unable to find CR bit definition within maximum search distance. 8060b57cec5SDimitry Andric if (CRBitSpillDistance == MaxCRBitSpillDist) { 8070b57cec5SDimitry Andric Ins = MI; 8080b57cec5SDimitry Andric break; 8090b57cec5SDimitry Andric } 8100b57cec5SDimitry Andric // Skip debug instructions when counting CR bit spill distance. 8110b57cec5SDimitry Andric if (!Ins->isDebugInstr()) 8120b57cec5SDimitry Andric CRBitSpillDistance++; 8130b57cec5SDimitry Andric } 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric // Unable to find the definition of the CR bit in the MBB. 8160b57cec5SDimitry Andric if (Ins == MBB.rend()) 8170b57cec5SDimitry Andric Ins = MI; 8180b57cec5SDimitry Andric 819480093f4SDimitry Andric bool SpillsKnownBit = false; 8200b57cec5SDimitry Andric // There is no need to extract the CR bit if its value is already known. 8210b57cec5SDimitry Andric switch (Ins->getOpcode()) { 8220b57cec5SDimitry Andric case PPC::CRUNSET: 8230b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg) 8240b57cec5SDimitry Andric .addImm(0); 825480093f4SDimitry Andric SpillsKnownBit = true; 8260b57cec5SDimitry Andric break; 8270b57cec5SDimitry Andric case PPC::CRSET: 8280b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg) 8290b57cec5SDimitry Andric .addImm(-32768); 830480093f4SDimitry Andric SpillsKnownBit = true; 8310b57cec5SDimitry Andric break; 8320b57cec5SDimitry Andric default: 833480093f4SDimitry Andric // On Power9, we can use SETB to extract the LT bit. This only works for 834480093f4SDimitry Andric // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value 835480093f4SDimitry Andric // of the bit we care about (32-bit sign bit) will be set to the value of 836480093f4SDimitry Andric // the LT bit (regardless of the other bits in the CR field). 837480093f4SDimitry Andric if (Subtarget.isISA3_0()) { 838480093f4SDimitry Andric if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT || 839480093f4SDimitry Andric SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT || 840480093f4SDimitry Andric SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT || 841480093f4SDimitry Andric SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) { 842480093f4SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg) 843480093f4SDimitry Andric .addReg(getCRFromCRBit(SrcReg), RegState::Undef); 844480093f4SDimitry Andric break; 845480093f4SDimitry Andric } 846480093f4SDimitry Andric } 847480093f4SDimitry Andric 8480b57cec5SDimitry Andric // We need to move the CR field that contains the CR bit we are spilling. 8490b57cec5SDimitry Andric // The super register may not be explicitly defined (i.e. it can be defined 8500b57cec5SDimitry Andric // by a CR-logical that only defines the subreg) so we state that the CR 8510b57cec5SDimitry Andric // field is undef. Also, in order to preserve the kill flag on the CR bit, 8520b57cec5SDimitry Andric // we add it as an implicit use. 8530b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 8540b57cec5SDimitry Andric .addReg(getCRFromCRBit(SrcReg), RegState::Undef) 8550b57cec5SDimitry Andric .addReg(SrcReg, 8560b57cec5SDimitry Andric RegState::Implicit | getKillRegState(MI.getOperand(0).isKill())); 8570b57cec5SDimitry Andric 8580b57cec5SDimitry Andric // If the saved register wasn't CR0LT, shift the bits left so that the bit 8590b57cec5SDimitry Andric // to store is the first one. Mask all but that bit. 8605ffd83dbSDimitry Andric Register Reg1 = Reg; 8610b57cec5SDimitry Andric Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric // rlwinm rA, rA, ShiftBits, 0, 0. 8640b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 8650b57cec5SDimitry Andric .addReg(Reg1, RegState::Kill) 8660b57cec5SDimitry Andric .addImm(getEncodingValue(SrcReg)) 8670b57cec5SDimitry Andric .addImm(0).addImm(0); 8680b57cec5SDimitry Andric } 8690b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 8700b57cec5SDimitry Andric .addReg(Reg, RegState::Kill), 8710b57cec5SDimitry Andric FrameIndex); 8720b57cec5SDimitry Andric 873480093f4SDimitry Andric bool KillsCRBit = MI.killsRegister(SrcReg, TRI); 8740b57cec5SDimitry Andric // Discard the pseudo instruction. 8750b57cec5SDimitry Andric MBB.erase(II); 876480093f4SDimitry Andric if (SpillsKnownBit && KillsCRBit && !SeenUse) { 877480093f4SDimitry Andric Ins->setDesc(TII.get(PPC::UNENCODED_NOP)); 878480093f4SDimitry Andric Ins->RemoveOperand(0); 879480093f4SDimitry Andric } 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric 8820b57cec5SDimitry Andric void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, 8830b57cec5SDimitry Andric unsigned FrameIndex) const { 8840b57cec5SDimitry Andric // Get the instruction. 8850b57cec5SDimitry Andric MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset> 8860b57cec5SDimitry Andric // Get the instruction's basic block. 8870b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 8880b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 8890b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 8900b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 8910b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric bool LP64 = TM.isPPC64(); 8940b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 8950b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 8960b57cec5SDimitry Andric 8978bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 8988bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 8990b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 9000b57cec5SDimitry Andric "RESTORE_CRBIT does not define its destination"); 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 9030b57cec5SDimitry Andric Reg), FrameIndex); 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg); 9060b57cec5SDimitry Andric 9078bcb0991SDimitry Andric Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 9080b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO) 9090b57cec5SDimitry Andric .addReg(getCRFromCRBit(DestReg)); 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric unsigned ShiftBits = getEncodingValue(DestReg); 9120b57cec5SDimitry Andric // rlwimi r11, r10, 32-ShiftBits, ..., ... 9130b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO) 9140b57cec5SDimitry Andric .addReg(RegO, RegState::Kill) 9150b57cec5SDimitry Andric .addReg(Reg, RegState::Kill) 9160b57cec5SDimitry Andric .addImm(ShiftBits ? 32 - ShiftBits : 0) 9170b57cec5SDimitry Andric .addImm(ShiftBits) 9180b57cec5SDimitry Andric .addImm(ShiftBits); 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), 9210b57cec5SDimitry Andric getCRFromCRBit(DestReg)) 9220b57cec5SDimitry Andric .addReg(RegO, RegState::Kill) 9230b57cec5SDimitry Andric // Make sure we have a use dependency all the way through this 9240b57cec5SDimitry Andric // sequence of instructions. We can't have the other bits in the CR 9250b57cec5SDimitry Andric // modified in between the mfocrf and the mtocrf. 9260b57cec5SDimitry Andric .addReg(getCRFromCRBit(DestReg), RegState::Implicit); 9270b57cec5SDimitry Andric 9280b57cec5SDimitry Andric // Discard the pseudo instruction. 9290b57cec5SDimitry Andric MBB.erase(II); 9300b57cec5SDimitry Andric } 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II, 9330b57cec5SDimitry Andric unsigned FrameIndex) const { 9340b57cec5SDimitry Andric // Get the instruction. 9350b57cec5SDimitry Andric MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset> 9360b57cec5SDimitry Andric // Get the instruction's basic block. 9370b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 9380b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 9390b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 9400b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 9410b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 9448bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(GPRC); 9458bcb0991SDimitry Andric Register SrcReg = MI.getOperand(0).getReg(); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg) 9480b57cec5SDimitry Andric .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric addFrameReference( 9510b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill), 9520b57cec5SDimitry Andric FrameIndex); 9530b57cec5SDimitry Andric 9540b57cec5SDimitry Andric // Discard the pseudo instruction. 9550b57cec5SDimitry Andric MBB.erase(II); 9560b57cec5SDimitry Andric } 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II, 9590b57cec5SDimitry Andric unsigned FrameIndex) const { 9600b57cec5SDimitry Andric // Get the instruction. 9610b57cec5SDimitry Andric MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset> 9620b57cec5SDimitry Andric // Get the instruction's basic block. 9630b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 9640b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 9650b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 9660b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 9670b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 9708bcb0991SDimitry Andric Register Reg = MF.getRegInfo().createVirtualRegister(GPRC); 9718bcb0991SDimitry Andric Register DestReg = MI.getOperand(0).getReg(); 9720b57cec5SDimitry Andric assert(MI.definesRegister(DestReg) && 9730b57cec5SDimitry Andric "RESTORE_VRSAVE does not define its destination"); 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ), 9760b57cec5SDimitry Andric Reg), FrameIndex); 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg) 9790b57cec5SDimitry Andric .addReg(Reg, RegState::Kill); 9800b57cec5SDimitry Andric 9810b57cec5SDimitry Andric // Discard the pseudo instruction. 9820b57cec5SDimitry Andric MBB.erase(II); 9830b57cec5SDimitry Andric } 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 9865ffd83dbSDimitry Andric Register Reg, int &FrameIdx) const { 9875ffd83dbSDimitry Andric // For the nonvolatile condition registers (CR2, CR3, CR4) return true to 9885ffd83dbSDimitry Andric // prevent allocating an additional frame slot. 9895ffd83dbSDimitry Andric // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8, 9905ffd83dbSDimitry Andric // for 32-bit AIX the CR save area is in the linkage area at SP+4. 9915ffd83dbSDimitry Andric // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos 9925ffd83dbSDimitry Andric // valid. 9935ffd83dbSDimitry Andric // For 32-bit ELF, we have previously created the stack slot if needed, so 9945ffd83dbSDimitry Andric // return its FrameIdx. 9955ffd83dbSDimitry Andric if (PPC::CR2 <= Reg && Reg <= PPC::CR4) { 9965ffd83dbSDimitry Andric FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex(); 9970b57cec5SDimitry Andric return true; 9980b57cec5SDimitry Andric } 9990b57cec5SDimitry Andric return false; 10000b57cec5SDimitry Andric } 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is. 10030b57cec5SDimitry Andric static unsigned offsetMinAlignForOpcode(unsigned OpC) { 10040b57cec5SDimitry Andric switch (OpC) { 10050b57cec5SDimitry Andric default: 10060b57cec5SDimitry Andric return 1; 10070b57cec5SDimitry Andric case PPC::LWA: 10080b57cec5SDimitry Andric case PPC::LWA_32: 10090b57cec5SDimitry Andric case PPC::LD: 10100b57cec5SDimitry Andric case PPC::LDU: 10110b57cec5SDimitry Andric case PPC::STD: 10120b57cec5SDimitry Andric case PPC::STDU: 10130b57cec5SDimitry Andric case PPC::DFLOADf32: 10140b57cec5SDimitry Andric case PPC::DFLOADf64: 10150b57cec5SDimitry Andric case PPC::DFSTOREf32: 10160b57cec5SDimitry Andric case PPC::DFSTOREf64: 10170b57cec5SDimitry Andric case PPC::LXSD: 10180b57cec5SDimitry Andric case PPC::LXSSP: 10190b57cec5SDimitry Andric case PPC::STXSD: 10200b57cec5SDimitry Andric case PPC::STXSSP: 10210b57cec5SDimitry Andric return 4; 10220b57cec5SDimitry Andric case PPC::EVLDD: 10230b57cec5SDimitry Andric case PPC::EVSTDD: 10240b57cec5SDimitry Andric return 8; 10250b57cec5SDimitry Andric case PPC::LXV: 10260b57cec5SDimitry Andric case PPC::STXV: 10270b57cec5SDimitry Andric return 16; 10280b57cec5SDimitry Andric } 10290b57cec5SDimitry Andric } 10300b57cec5SDimitry Andric 10310b57cec5SDimitry Andric // If the offset must be a multiple of some value, return what that value is. 10320b57cec5SDimitry Andric static unsigned offsetMinAlign(const MachineInstr &MI) { 10330b57cec5SDimitry Andric unsigned OpC = MI.getOpcode(); 10340b57cec5SDimitry Andric return offsetMinAlignForOpcode(OpC); 10350b57cec5SDimitry Andric } 10360b57cec5SDimitry Andric 10370b57cec5SDimitry Andric // Return the OffsetOperandNo given the FIOperandNum (and the instruction). 10380b57cec5SDimitry Andric static unsigned getOffsetONFromFION(const MachineInstr &MI, 10390b57cec5SDimitry Andric unsigned FIOperandNum) { 10400b57cec5SDimitry Andric // Take into account whether it's an add or mem instruction 10410b57cec5SDimitry Andric unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2; 10420b57cec5SDimitry Andric if (MI.isInlineAsm()) 10430b57cec5SDimitry Andric OffsetOperandNo = FIOperandNum - 1; 10440b57cec5SDimitry Andric else if (MI.getOpcode() == TargetOpcode::STACKMAP || 10450b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::PATCHPOINT) 10460b57cec5SDimitry Andric OffsetOperandNo = FIOperandNum + 1; 10470b57cec5SDimitry Andric 10480b57cec5SDimitry Andric return OffsetOperandNo; 10490b57cec5SDimitry Andric } 10500b57cec5SDimitry Andric 10510b57cec5SDimitry Andric void 10520b57cec5SDimitry Andric PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 10530b57cec5SDimitry Andric int SPAdj, unsigned FIOperandNum, 10540b57cec5SDimitry Andric RegScavenger *RS) const { 10550b57cec5SDimitry Andric assert(SPAdj == 0 && "Unexpected"); 10560b57cec5SDimitry Andric 10570b57cec5SDimitry Andric // Get the instruction. 10580b57cec5SDimitry Andric MachineInstr &MI = *II; 10590b57cec5SDimitry Andric // Get the instruction's basic block. 10600b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 10610b57cec5SDimitry Andric // Get the basic block's function. 10620b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 10630b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 10640b57cec5SDimitry Andric // Get the instruction info. 10650b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 10660b57cec5SDimitry Andric // Get the frame info. 10670b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 10680b57cec5SDimitry Andric DebugLoc dl = MI.getDebugLoc(); 10690b57cec5SDimitry Andric 10700b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 10710b57cec5SDimitry Andric 10720b57cec5SDimitry Andric // Get the frame index. 10730b57cec5SDimitry Andric int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 10740b57cec5SDimitry Andric 10750b57cec5SDimitry Andric // Get the frame pointer save index. Users of this index are primarily 10760b57cec5SDimitry Andric // DYNALLOC instructions. 10770b57cec5SDimitry Andric PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 10780b57cec5SDimitry Andric int FPSI = FI->getFramePointerSaveIndex(); 10790b57cec5SDimitry Andric // Get the instruction opcode. 10800b57cec5SDimitry Andric unsigned OpC = MI.getOpcode(); 10810b57cec5SDimitry Andric 10820b57cec5SDimitry Andric if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) { 10830b57cec5SDimitry Andric lowerDynamicAreaOffset(II); 10840b57cec5SDimitry Andric return; 10850b57cec5SDimitry Andric } 10860b57cec5SDimitry Andric 10870b57cec5SDimitry Andric // Special case for dynamic alloca. 10880b57cec5SDimitry Andric if (FPSI && FrameIndex == FPSI && 10890b57cec5SDimitry Andric (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) { 10900b57cec5SDimitry Andric lowerDynamicAlloc(II); 10910b57cec5SDimitry Andric return; 10920b57cec5SDimitry Andric } 10930b57cec5SDimitry Andric 10945ffd83dbSDimitry Andric if (FPSI && FrameIndex == FPSI && 10955ffd83dbSDimitry Andric (OpC == PPC::PREPARE_PROBED_ALLOCA_64 || 1096*590d96feSDimitry Andric OpC == PPC::PREPARE_PROBED_ALLOCA_32 || 1097*590d96feSDimitry Andric OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64 || 1098*590d96feSDimitry Andric OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32)) { 10995ffd83dbSDimitry Andric lowerPrepareProbedAlloca(II); 11005ffd83dbSDimitry Andric return; 11015ffd83dbSDimitry Andric } 11025ffd83dbSDimitry Andric 11030b57cec5SDimitry Andric // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc. 11040b57cec5SDimitry Andric if (OpC == PPC::SPILL_CR) { 11050b57cec5SDimitry Andric lowerCRSpilling(II, FrameIndex); 11060b57cec5SDimitry Andric return; 11070b57cec5SDimitry Andric } else if (OpC == PPC::RESTORE_CR) { 11080b57cec5SDimitry Andric lowerCRRestore(II, FrameIndex); 11090b57cec5SDimitry Andric return; 11100b57cec5SDimitry Andric } else if (OpC == PPC::SPILL_CRBIT) { 11110b57cec5SDimitry Andric lowerCRBitSpilling(II, FrameIndex); 11120b57cec5SDimitry Andric return; 11130b57cec5SDimitry Andric } else if (OpC == PPC::RESTORE_CRBIT) { 11140b57cec5SDimitry Andric lowerCRBitRestore(II, FrameIndex); 11150b57cec5SDimitry Andric return; 11160b57cec5SDimitry Andric } else if (OpC == PPC::SPILL_VRSAVE) { 11170b57cec5SDimitry Andric lowerVRSAVESpilling(II, FrameIndex); 11180b57cec5SDimitry Andric return; 11190b57cec5SDimitry Andric } else if (OpC == PPC::RESTORE_VRSAVE) { 11200b57cec5SDimitry Andric lowerVRSAVERestore(II, FrameIndex); 11210b57cec5SDimitry Andric return; 11220b57cec5SDimitry Andric } 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). 11250b57cec5SDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister( 11260b57cec5SDimitry Andric FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false); 11270b57cec5SDimitry Andric 11280b57cec5SDimitry Andric // If the instruction is not present in ImmToIdxMap, then it has no immediate 11290b57cec5SDimitry Andric // form (and must be r+r). 11300b57cec5SDimitry Andric bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP && 11310b57cec5SDimitry Andric OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC); 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric // Now add the frame object offset to the offset from r1. 11340b57cec5SDimitry Andric int Offset = MFI.getObjectOffset(FrameIndex); 11350b57cec5SDimitry Andric Offset += MI.getOperand(OffsetOperandNo).getImm(); 11360b57cec5SDimitry Andric 11370b57cec5SDimitry Andric // If we're not using a Frame Pointer that has been set to the value of the 11380b57cec5SDimitry Andric // SP before having the stack size subtracted from it, then add the stack size 11390b57cec5SDimitry Andric // to Offset to get the correct offset. 11400b57cec5SDimitry Andric // Naked functions have stack size 0, although getStackSize may not reflect 11410b57cec5SDimitry Andric // that because we didn't call all the pieces that compute it for naked 11420b57cec5SDimitry Andric // functions. 11430b57cec5SDimitry Andric if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) { 11440b57cec5SDimitry Andric if (!(hasBasePointer(MF) && FrameIndex < 0)) 11450b57cec5SDimitry Andric Offset += MFI.getStackSize(); 11460b57cec5SDimitry Andric } 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric // If we can, encode the offset directly into the instruction. If this is a 11490b57cec5SDimitry Andric // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If 11500b57cec5SDimitry Andric // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits 11510b57cec5SDimitry Andric // clear can be encoded. This is extremely uncommon, because normally you 11520b57cec5SDimitry Andric // only "std" to a stack slot that is at least 4-byte aligned, but it can 11530b57cec5SDimitry Andric // happen in invalid code. 11540b57cec5SDimitry Andric assert(OpC != PPC::DBG_VALUE && 11550b57cec5SDimitry Andric "This should be handled in a target-independent way"); 11560b57cec5SDimitry Andric bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ? 11570b57cec5SDimitry Andric isUInt<8>(Offset) : 11580b57cec5SDimitry Andric isInt<16>(Offset); 11590b57cec5SDimitry Andric if (!noImmForm && ((OffsetFitsMnemonic && 11600b57cec5SDimitry Andric ((Offset % offsetMinAlign(MI)) == 0)) || 11610b57cec5SDimitry Andric OpC == TargetOpcode::STACKMAP || 11620b57cec5SDimitry Andric OpC == TargetOpcode::PATCHPOINT)) { 11630b57cec5SDimitry Andric MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 11640b57cec5SDimitry Andric return; 11650b57cec5SDimitry Andric } 11660b57cec5SDimitry Andric 11670b57cec5SDimitry Andric // The offset doesn't fit into a single register, scavenge one to build the 11680b57cec5SDimitry Andric // offset in. 11690b57cec5SDimitry Andric 11700b57cec5SDimitry Andric bool is64Bit = TM.isPPC64(); 11710b57cec5SDimitry Andric const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 11720b57cec5SDimitry Andric const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 11730b57cec5SDimitry Andric const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC; 11745ffd83dbSDimitry Andric Register SRegHi = MF.getRegInfo().createVirtualRegister(RC), 11750b57cec5SDimitry Andric SReg = MF.getRegInfo().createVirtualRegister(RC); 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric // Insert a set of rA with the full offset value before the ld, st, or add 11780b57cec5SDimitry Andric if (isInt<16>(Offset)) 11790b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg) 11800b57cec5SDimitry Andric .addImm(Offset); 11810b57cec5SDimitry Andric else { 11820b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi) 11830b57cec5SDimitry Andric .addImm(Offset >> 16); 11840b57cec5SDimitry Andric BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg) 11850b57cec5SDimitry Andric .addReg(SRegHi, RegState::Kill) 11860b57cec5SDimitry Andric .addImm(Offset); 11870b57cec5SDimitry Andric } 11880b57cec5SDimitry Andric 11890b57cec5SDimitry Andric // Convert into indexed form of the instruction: 11900b57cec5SDimitry Andric // 11910b57cec5SDimitry Andric // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 11920b57cec5SDimitry Andric // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 11930b57cec5SDimitry Andric unsigned OperandBase; 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric if (noImmForm) 11960b57cec5SDimitry Andric OperandBase = 1; 11970b57cec5SDimitry Andric else if (OpC != TargetOpcode::INLINEASM && 11980b57cec5SDimitry Andric OpC != TargetOpcode::INLINEASM_BR) { 11990b57cec5SDimitry Andric assert(ImmToIdxMap.count(OpC) && 12000b57cec5SDimitry Andric "No indexed form of load or store available!"); 12010b57cec5SDimitry Andric unsigned NewOpcode = ImmToIdxMap.find(OpC)->second; 12020b57cec5SDimitry Andric MI.setDesc(TII.get(NewOpcode)); 12030b57cec5SDimitry Andric OperandBase = 1; 12040b57cec5SDimitry Andric } else { 12050b57cec5SDimitry Andric OperandBase = OffsetOperandNo; 12060b57cec5SDimitry Andric } 12070b57cec5SDimitry Andric 12088bcb0991SDimitry Andric Register StackReg = MI.getOperand(FIOperandNum).getReg(); 12090b57cec5SDimitry Andric MI.getOperand(OperandBase).ChangeToRegister(StackReg, false); 12100b57cec5SDimitry Andric MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true); 12110b57cec5SDimitry Andric } 12120b57cec5SDimitry Andric 12130b57cec5SDimitry Andric Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 12140b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 12150b57cec5SDimitry Andric 12160b57cec5SDimitry Andric if (!TM.isPPC64()) 12170b57cec5SDimitry Andric return TFI->hasFP(MF) ? PPC::R31 : PPC::R1; 12180b57cec5SDimitry Andric else 12190b57cec5SDimitry Andric return TFI->hasFP(MF) ? PPC::X31 : PPC::X1; 12200b57cec5SDimitry Andric } 12210b57cec5SDimitry Andric 12220b57cec5SDimitry Andric Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const { 12230b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 12240b57cec5SDimitry Andric if (!hasBasePointer(MF)) 12250b57cec5SDimitry Andric return getFrameRegister(MF); 12260b57cec5SDimitry Andric 12270b57cec5SDimitry Andric if (TM.isPPC64()) 12280b57cec5SDimitry Andric return PPC::X30; 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric if (Subtarget.isSVR4ABI() && TM.isPositionIndependent()) 12310b57cec5SDimitry Andric return PPC::R29; 12320b57cec5SDimitry Andric 12330b57cec5SDimitry Andric return PPC::R30; 12340b57cec5SDimitry Andric } 12350b57cec5SDimitry Andric 12360b57cec5SDimitry Andric bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const { 12370b57cec5SDimitry Andric if (!EnableBasePointer) 12380b57cec5SDimitry Andric return false; 12390b57cec5SDimitry Andric if (AlwaysBasePointer) 12400b57cec5SDimitry Andric return true; 12410b57cec5SDimitry Andric 12420b57cec5SDimitry Andric // If we need to realign the stack, then the stack pointer can no longer 12430b57cec5SDimitry Andric // serve as an offset into the caller's stack space. As a result, we need a 12440b57cec5SDimitry Andric // base pointer. 12450b57cec5SDimitry Andric return needsStackRealignment(MF); 12460b57cec5SDimitry Andric } 12470b57cec5SDimitry Andric 12480b57cec5SDimitry Andric /// Returns true if the instruction's frame index 12490b57cec5SDimitry Andric /// reference would be better served by a base register other than FP 12500b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index 12510b57cec5SDimitry Andric /// references it should create new base registers for. 12520b57cec5SDimitry Andric bool PPCRegisterInfo:: 12530b57cec5SDimitry Andric needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 12540b57cec5SDimitry Andric assert(Offset < 0 && "Local offset must be negative"); 12550b57cec5SDimitry Andric 12560b57cec5SDimitry Andric // It's the load/store FI references that cause issues, as it can be difficult 12570b57cec5SDimitry Andric // to materialize the offset if it won't fit in the literal field. Estimate 12580b57cec5SDimitry Andric // based on the size of the local frame and some conservative assumptions 12590b57cec5SDimitry Andric // about the rest of the stack frame (note, this is pre-regalloc, so 12600b57cec5SDimitry Andric // we don't know everything for certain yet) whether this offset is likely 12610b57cec5SDimitry Andric // to be out of range of the immediate. Return true if so. 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric // We only generate virtual base registers for loads and stores that have 12640b57cec5SDimitry Andric // an r+i form. Return false for everything else. 12650b57cec5SDimitry Andric unsigned OpC = MI->getOpcode(); 12660b57cec5SDimitry Andric if (!ImmToIdxMap.count(OpC)) 12670b57cec5SDimitry Andric return false; 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric // Don't generate a new virtual base register just to add zero to it. 12700b57cec5SDimitry Andric if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) && 12710b57cec5SDimitry Andric MI->getOperand(2).getImm() == 0) 12720b57cec5SDimitry Andric return false; 12730b57cec5SDimitry Andric 12740b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI->getParent(); 12750b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 12760b57cec5SDimitry Andric const PPCFrameLowering *TFI = getFrameLowering(MF); 12770b57cec5SDimitry Andric unsigned StackEst = TFI->determineFrameLayout(MF, true); 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric // If we likely don't need a stack frame, then we probably don't need a 12800b57cec5SDimitry Andric // virtual base register either. 12810b57cec5SDimitry Andric if (!StackEst) 12820b57cec5SDimitry Andric return false; 12830b57cec5SDimitry Andric 12840b57cec5SDimitry Andric // Estimate an offset from the stack pointer. 12850b57cec5SDimitry Andric // The incoming offset is relating to the SP at the start of the function, 12860b57cec5SDimitry Andric // but when we access the local it'll be relative to the SP after local 12870b57cec5SDimitry Andric // allocation, so adjust our SP-relative offset by that allocation size. 12880b57cec5SDimitry Andric Offset += StackEst; 12890b57cec5SDimitry Andric 12900b57cec5SDimitry Andric // The frame pointer will point to the end of the stack, so estimate the 12910b57cec5SDimitry Andric // offset as the difference between the object offset and the FP location. 12920b57cec5SDimitry Andric return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset); 12930b57cec5SDimitry Andric } 12940b57cec5SDimitry Andric 12950b57cec5SDimitry Andric /// Insert defining instruction(s) for BaseReg to 12960b57cec5SDimitry Andric /// be a pointer to FrameIdx at the beginning of the basic block. 12975ffd83dbSDimitry Andric void PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 12985ffd83dbSDimitry Andric Register BaseReg, 12995ffd83dbSDimitry Andric int FrameIdx, 13000b57cec5SDimitry Andric int64_t Offset) const { 13010b57cec5SDimitry Andric unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI; 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric MachineBasicBlock::iterator Ins = MBB->begin(); 13040b57cec5SDimitry Andric DebugLoc DL; // Defaults to "unknown" 13050b57cec5SDimitry Andric if (Ins != MBB->end()) 13060b57cec5SDimitry Andric DL = Ins->getDebugLoc(); 13070b57cec5SDimitry Andric 13080b57cec5SDimitry Andric const MachineFunction &MF = *MBB->getParent(); 13090b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 13100b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 13110b57cec5SDimitry Andric const MCInstrDesc &MCID = TII.get(ADDriOpc); 13120b57cec5SDimitry Andric MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 13130b57cec5SDimitry Andric MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); 13140b57cec5SDimitry Andric 13150b57cec5SDimitry Andric BuildMI(*MBB, Ins, DL, MCID, BaseReg) 13160b57cec5SDimitry Andric .addFrameIndex(FrameIdx).addImm(Offset); 13170b57cec5SDimitry Andric } 13180b57cec5SDimitry Andric 13195ffd83dbSDimitry Andric void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 13200b57cec5SDimitry Andric int64_t Offset) const { 13210b57cec5SDimitry Andric unsigned FIOperandNum = 0; 13220b57cec5SDimitry Andric while (!MI.getOperand(FIOperandNum).isFI()) { 13230b57cec5SDimitry Andric ++FIOperandNum; 13240b57cec5SDimitry Andric assert(FIOperandNum < MI.getNumOperands() && 13250b57cec5SDimitry Andric "Instr doesn't have FrameIndex operand!"); 13260b57cec5SDimitry Andric } 13270b57cec5SDimitry Andric 13280b57cec5SDimitry Andric MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false); 13290b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 13300b57cec5SDimitry Andric Offset += MI.getOperand(OffsetOperandNo).getImm(); 13310b57cec5SDimitry Andric MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 13320b57cec5SDimitry Andric 13330b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 13340b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 13350b57cec5SDimitry Andric const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 13360b57cec5SDimitry Andric const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 13370b57cec5SDimitry Andric const MCInstrDesc &MCID = MI.getDesc(); 13380b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 13390b57cec5SDimitry Andric MRI.constrainRegClass(BaseReg, 13400b57cec5SDimitry Andric TII.getRegClass(MCID, FIOperandNum, this, MF)); 13410b57cec5SDimitry Andric } 13420b57cec5SDimitry Andric 13430b57cec5SDimitry Andric bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 13445ffd83dbSDimitry Andric Register BaseReg, 13450b57cec5SDimitry Andric int64_t Offset) const { 13460b57cec5SDimitry Andric unsigned FIOperandNum = 0; 13470b57cec5SDimitry Andric while (!MI->getOperand(FIOperandNum).isFI()) { 13480b57cec5SDimitry Andric ++FIOperandNum; 13490b57cec5SDimitry Andric assert(FIOperandNum < MI->getNumOperands() && 13500b57cec5SDimitry Andric "Instr doesn't have FrameIndex operand!"); 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum); 13540b57cec5SDimitry Andric Offset += MI->getOperand(OffsetOperandNo).getImm(); 13550b57cec5SDimitry Andric 13560b57cec5SDimitry Andric return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm 13570b57cec5SDimitry Andric MI->getOpcode() == TargetOpcode::STACKMAP || 13580b57cec5SDimitry Andric MI->getOpcode() == TargetOpcode::PATCHPOINT || 13590b57cec5SDimitry Andric (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0); 13600b57cec5SDimitry Andric } 1361