Path: blob/main/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCFastISel.cpp
35266 views
//===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines the PowerPC-specific support for the FastISel class. Some9// of the target-specific code is generated by tablegen in the file10// PPCGenFastISel.inc, which is #included here.11//12//===----------------------------------------------------------------------===//1314#include "MCTargetDesc/PPCPredicates.h"15#include "PPC.h"16#include "PPCCCState.h"17#include "PPCCallingConv.h"18#include "PPCISelLowering.h"19#include "PPCMachineFunctionInfo.h"20#include "PPCSubtarget.h"21#include "PPCTargetMachine.h"22#include "llvm/CodeGen/CallingConvLower.h"23#include "llvm/CodeGen/FastISel.h"24#include "llvm/CodeGen/FunctionLoweringInfo.h"25#include "llvm/CodeGen/MachineConstantPool.h"26#include "llvm/CodeGen/MachineFrameInfo.h"27#include "llvm/CodeGen/MachineInstrBuilder.h"28#include "llvm/CodeGen/MachineRegisterInfo.h"29#include "llvm/CodeGen/TargetLowering.h"30#include "llvm/IR/CallingConv.h"31#include "llvm/IR/GetElementPtrTypeIterator.h"32#include "llvm/IR/GlobalAlias.h"33#include "llvm/IR/GlobalVariable.h"34#include "llvm/IR/IntrinsicInst.h"35#include "llvm/IR/Operator.h"36#include "llvm/Support/Debug.h"37#include "llvm/Target/TargetMachine.h"3839//===----------------------------------------------------------------------===//40//41// TBD:42// fastLowerArguments: Handle simple cases.43// PPCMaterializeGV: Handle TLS.44// SelectCall: Handle function pointers.45// SelectCall: Handle multi-register return values.46// SelectCall: Optimize away nops for local calls.47// processCallArgs: Handle bit-converted arguments.48// finishCall: Handle multi-register return values.49// PPCComputeAddress: Handle parameter references as FrameIndex's.50// PPCEmitCmp: Handle immediate as operand 1.51// SelectCall: Handle small byval arguments.52// SelectIntrinsicCall: Implement.53// SelectSelect: Implement.54// Consider factoring isTypeLegal into the base class.55// Implement switches and jump tables.56//57//===----------------------------------------------------------------------===//58using namespace llvm;5960#define DEBUG_TYPE "ppcfastisel"6162namespace {6364struct Address {65enum {66RegBase,67FrameIndexBase68} BaseType;6970union {71unsigned Reg;72int FI;73} Base;7475int64_t Offset;7677// Innocuous defaults for our address.78Address()79: BaseType(RegBase), Offset(0) {80Base.Reg = 0;81}82};8384class PPCFastISel final : public FastISel {8586const TargetMachine &TM;87const PPCSubtarget *Subtarget;88PPCFunctionInfo *PPCFuncInfo;89const TargetInstrInfo &TII;90const TargetLowering &TLI;91LLVMContext *Context;9293public:94explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,95const TargetLibraryInfo *LibInfo)96: FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),97Subtarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),98PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),99TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()),100Context(&FuncInfo.Fn->getContext()) {}101102// Backend specific FastISel code.103private:104bool fastSelectInstruction(const Instruction *I) override;105unsigned fastMaterializeConstant(const Constant *C) override;106unsigned fastMaterializeAlloca(const AllocaInst *AI) override;107bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,108const LoadInst *LI) override;109bool fastLowerArguments() override;110unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;111unsigned fastEmitInst_ri(unsigned MachineInstOpcode,112const TargetRegisterClass *RC,113unsigned Op0, uint64_t Imm);114unsigned fastEmitInst_r(unsigned MachineInstOpcode,115const TargetRegisterClass *RC, unsigned Op0);116unsigned fastEmitInst_rr(unsigned MachineInstOpcode,117const TargetRegisterClass *RC,118unsigned Op0, unsigned Op1);119120bool fastLowerCall(CallLoweringInfo &CLI) override;121122// Instruction selection routines.123private:124bool SelectLoad(const Instruction *I);125bool SelectStore(const Instruction *I);126bool SelectBranch(const Instruction *I);127bool SelectIndirectBr(const Instruction *I);128bool SelectFPExt(const Instruction *I);129bool SelectFPTrunc(const Instruction *I);130bool SelectIToFP(const Instruction *I, bool IsSigned);131bool SelectFPToI(const Instruction *I, bool IsSigned);132bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);133bool SelectRet(const Instruction *I);134bool SelectTrunc(const Instruction *I);135bool SelectIntExt(const Instruction *I);136137// Utility routines.138private:139bool isTypeLegal(Type *Ty, MVT &VT);140bool isLoadTypeLegal(Type *Ty, MVT &VT);141bool isValueAvailable(const Value *V) const;142bool isVSFRCRegClass(const TargetRegisterClass *RC) const {143return RC->getID() == PPC::VSFRCRegClassID;144}145bool isVSSRCRegClass(const TargetRegisterClass *RC) const {146return RC->getID() == PPC::VSSRCRegClassID;147}148unsigned copyRegToRegClass(const TargetRegisterClass *ToRC,149unsigned SrcReg, unsigned Flag = 0,150unsigned SubReg = 0) {151Register TmpReg = createResultReg(ToRC);152BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,153TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg, Flag, SubReg);154return TmpReg;155}156bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,157bool isZExt, unsigned DestReg,158const PPC::Predicate Pred);159bool PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,160const TargetRegisterClass *RC, bool IsZExt = true,161unsigned FP64LoadOpc = PPC::LFD);162bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);163bool PPCComputeAddress(const Value *Obj, Address &Addr);164void PPCSimplifyAddress(Address &Addr, bool &UseOffset,165unsigned &IndexReg);166bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,167unsigned DestReg, bool IsZExt);168unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);169unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);170unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,171bool UseSExt = true);172unsigned PPCMaterialize32BitInt(int64_t Imm,173const TargetRegisterClass *RC);174unsigned PPCMaterialize64BitInt(int64_t Imm,175const TargetRegisterClass *RC);176unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,177unsigned SrcReg, bool IsSigned);178unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);179180// Call handling routines.181private:182bool processCallArgs(SmallVectorImpl<Value*> &Args,183SmallVectorImpl<unsigned> &ArgRegs,184SmallVectorImpl<MVT> &ArgVTs,185SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,186SmallVectorImpl<unsigned> &RegArgs,187CallingConv::ID CC,188unsigned &NumBytes,189bool IsVarArg);190bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);191192private:193#include "PPCGenFastISel.inc"194195};196197} // end anonymous namespace198199static std::optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {200switch (Pred) {201// These are not representable with any single compare.202case CmpInst::FCMP_FALSE:203case CmpInst::FCMP_TRUE:204// Major concern about the following 6 cases is NaN result. The comparison205// result consists of 4 bits, indicating lt, eq, gt and un (unordered),206// only one of which will be set. The result is generated by fcmpu207// instruction. However, bc instruction only inspects one of the first 3208// bits, so when un is set, bc instruction may jump to an undesired209// place.210//211// More specifically, if we expect an unordered comparison and un is set, we212// expect to always go to true branch; in such case UEQ, UGT and ULT still213// give false, which are undesired; but UNE, UGE, ULE happen to give true,214// since they are tested by inspecting !eq, !lt, !gt, respectively.215//216// Similarly, for ordered comparison, when un is set, we always expect the217// result to be false. In such case OGT, OLT and OEQ is good, since they are218// actually testing GT, LT, and EQ respectively, which are false. OGE, OLE219// and ONE are tested through !lt, !gt and !eq, and these are true.220case CmpInst::FCMP_UEQ:221case CmpInst::FCMP_UGT:222case CmpInst::FCMP_ULT:223case CmpInst::FCMP_OGE:224case CmpInst::FCMP_OLE:225case CmpInst::FCMP_ONE:226default:227return std::nullopt;228229case CmpInst::FCMP_OEQ:230case CmpInst::ICMP_EQ:231return PPC::PRED_EQ;232233case CmpInst::FCMP_OGT:234case CmpInst::ICMP_UGT:235case CmpInst::ICMP_SGT:236return PPC::PRED_GT;237238case CmpInst::FCMP_UGE:239case CmpInst::ICMP_UGE:240case CmpInst::ICMP_SGE:241return PPC::PRED_GE;242243case CmpInst::FCMP_OLT:244case CmpInst::ICMP_ULT:245case CmpInst::ICMP_SLT:246return PPC::PRED_LT;247248case CmpInst::FCMP_ULE:249case CmpInst::ICMP_ULE:250case CmpInst::ICMP_SLE:251return PPC::PRED_LE;252253case CmpInst::FCMP_UNE:254case CmpInst::ICMP_NE:255return PPC::PRED_NE;256257case CmpInst::FCMP_ORD:258return PPC::PRED_NU;259260case CmpInst::FCMP_UNO:261return PPC::PRED_UN;262}263}264265// Determine whether the type Ty is simple enough to be handled by266// fast-isel, and return its equivalent machine type in VT.267// FIXME: Copied directly from ARM -- factor into base class?268bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {269EVT Evt = TLI.getValueType(DL, Ty, true);270271// Only handle simple types.272if (Evt == MVT::Other || !Evt.isSimple()) return false;273VT = Evt.getSimpleVT();274275// Handle all legal types, i.e. a register that will directly hold this276// value.277return TLI.isTypeLegal(VT);278}279280// Determine whether the type Ty is simple enough to be handled by281// fast-isel as a load target, and return its equivalent machine type in VT.282bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {283if (isTypeLegal(Ty, VT)) return true;284285// If this is a type than can be sign or zero-extended to a basic operation286// go ahead and accept it now.287if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {288return true;289}290291return false;292}293294bool PPCFastISel::isValueAvailable(const Value *V) const {295if (!isa<Instruction>(V))296return true;297298const auto *I = cast<Instruction>(V);299return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;300}301302// Given a value Obj, create an Address object Addr that represents its303// address. Return false if we can't handle it.304bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {305const User *U = nullptr;306unsigned Opcode = Instruction::UserOp1;307if (const Instruction *I = dyn_cast<Instruction>(Obj)) {308// Don't walk into other basic blocks unless the object is an alloca from309// another block, otherwise it may not have a virtual register assigned.310if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||311FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {312Opcode = I->getOpcode();313U = I;314}315} else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {316Opcode = C->getOpcode();317U = C;318}319320switch (Opcode) {321default:322break;323case Instruction::BitCast:324// Look through bitcasts.325return PPCComputeAddress(U->getOperand(0), Addr);326case Instruction::IntToPtr:327// Look past no-op inttoptrs.328if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==329TLI.getPointerTy(DL))330return PPCComputeAddress(U->getOperand(0), Addr);331break;332case Instruction::PtrToInt:333// Look past no-op ptrtoints.334if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))335return PPCComputeAddress(U->getOperand(0), Addr);336break;337case Instruction::GetElementPtr: {338Address SavedAddr = Addr;339int64_t TmpOffset = Addr.Offset;340341// Iterate through the GEP folding the constants into offsets where342// we can.343gep_type_iterator GTI = gep_type_begin(U);344for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();345II != IE; ++II, ++GTI) {346const Value *Op = *II;347if (StructType *STy = GTI.getStructTypeOrNull()) {348const StructLayout *SL = DL.getStructLayout(STy);349unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();350TmpOffset += SL->getElementOffset(Idx);351} else {352uint64_t S = GTI.getSequentialElementStride(DL);353for (;;) {354if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {355// Constant-offset addressing.356TmpOffset += CI->getSExtValue() * S;357break;358}359if (canFoldAddIntoGEP(U, Op)) {360// A compatible add with a constant operand. Fold the constant.361ConstantInt *CI =362cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));363TmpOffset += CI->getSExtValue() * S;364// Iterate on the other operand.365Op = cast<AddOperator>(Op)->getOperand(0);366continue;367}368// Unsupported369goto unsupported_gep;370}371}372}373374// Try to grab the base operand now.375Addr.Offset = TmpOffset;376if (PPCComputeAddress(U->getOperand(0), Addr)) return true;377378// We failed, restore everything and try the other options.379Addr = SavedAddr;380381unsupported_gep:382break;383}384case Instruction::Alloca: {385const AllocaInst *AI = cast<AllocaInst>(Obj);386DenseMap<const AllocaInst*, int>::iterator SI =387FuncInfo.StaticAllocaMap.find(AI);388if (SI != FuncInfo.StaticAllocaMap.end()) {389Addr.BaseType = Address::FrameIndexBase;390Addr.Base.FI = SI->second;391return true;392}393break;394}395}396397// FIXME: References to parameters fall through to the behavior398// below. They should be able to reference a frame index since399// they are stored to the stack, so we can get "ld rx, offset(r1)"400// instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will401// just contain the parameter. Try to handle this with a FI.402403// Try to get this in a register if nothing else has worked.404if (Addr.Base.Reg == 0)405Addr.Base.Reg = getRegForValue(Obj);406407// Prevent assignment of base register to X0, which is inappropriate408// for loads and stores alike.409if (Addr.Base.Reg != 0)410MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);411412return Addr.Base.Reg != 0;413}414415// Fix up some addresses that can't be used directly. For example, if416// an offset won't fit in an instruction field, we may need to move it417// into an index register.418void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,419unsigned &IndexReg) {420421// Check whether the offset fits in the instruction field.422if (!isInt<16>(Addr.Offset))423UseOffset = false;424425// If this is a stack pointer and the offset needs to be simplified then426// put the alloca address into a register, set the base type back to427// register and continue. This should almost never happen.428if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {429Register ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);430BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDI8),431ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);432Addr.Base.Reg = ResultReg;433Addr.BaseType = Address::RegBase;434}435436if (!UseOffset) {437IntegerType *OffsetTy = Type::getInt64Ty(*Context);438const ConstantInt *Offset = ConstantInt::getSigned(OffsetTy, Addr.Offset);439IndexReg = PPCMaterializeInt(Offset, MVT::i64);440assert(IndexReg && "Unexpected error in PPCMaterializeInt!");441}442}443444// Emit a load instruction if possible, returning true if we succeeded,445// otherwise false. See commentary below for how the register class of446// the load is determined.447bool PPCFastISel::PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,448const TargetRegisterClass *RC,449bool IsZExt, unsigned FP64LoadOpc) {450unsigned Opc;451bool UseOffset = true;452bool HasSPE = Subtarget->hasSPE();453454// If ResultReg is given, it determines the register class of the load.455// Otherwise, RC is the register class to use. If the result of the456// load isn't anticipated in this block, both may be zero, in which457// case we must make a conservative guess. In particular, don't assign458// R0 or X0 to the result register, as the result may be used in a load,459// store, add-immediate, or isel that won't permit this. (Though460// perhaps the spill and reload of live-exit values would handle this?)461const TargetRegisterClass *UseRC =462(ResultReg ? MRI.getRegClass(ResultReg) :463(RC ? RC :464(VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :465(VT == MVT::f32 ? (HasSPE ? &PPC::GPRCRegClass : &PPC::F4RCRegClass) :466(VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :467&PPC::GPRC_and_GPRC_NOR0RegClass)))));468469bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);470471switch (VT.SimpleTy) {472default: // e.g., vector types not handled473return false;474case MVT::i8:475Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;476break;477case MVT::i16:478Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)479: (Is32BitInt ? PPC::LHA : PPC::LHA8));480break;481case MVT::i32:482Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)483: (Is32BitInt ? PPC::LWA_32 : PPC::LWA));484if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))485UseOffset = false;486break;487case MVT::i64:488Opc = PPC::LD;489assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&490"64-bit load with 32-bit target??");491UseOffset = ((Addr.Offset & 3) == 0);492break;493case MVT::f32:494Opc = Subtarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;495break;496case MVT::f64:497Opc = FP64LoadOpc;498break;499}500501// If necessary, materialize the offset into a register and use502// the indexed form. Also handle stack pointers with special needs.503unsigned IndexReg = 0;504PPCSimplifyAddress(Addr, UseOffset, IndexReg);505506// If this is a potential VSX load with an offset of 0, a VSX indexed load can507// be used.508bool IsVSSRC = isVSSRCRegClass(UseRC);509bool IsVSFRC = isVSFRCRegClass(UseRC);510bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;511bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;512if ((Is32VSXLoad || Is64VSXLoad) &&513(Addr.BaseType != Address::FrameIndexBase) && UseOffset &&514(Addr.Offset == 0)) {515UseOffset = false;516}517518if (ResultReg == 0)519ResultReg = createResultReg(UseRC);520521// Note: If we still have a frame index here, we know the offset is522// in range, as otherwise PPCSimplifyAddress would have converted it523// into a RegBase.524if (Addr.BaseType == Address::FrameIndexBase) {525// VSX only provides an indexed load.526if (Is32VSXLoad || Is64VSXLoad) return false;527528MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(529MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,530Addr.Offset),531MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),532MFI.getObjectAlign(Addr.Base.FI));533534BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)535.addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);536537// Base reg with offset in range.538} else if (UseOffset) {539// VSX only provides an indexed load.540if (Is32VSXLoad || Is64VSXLoad) return false;541542BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)543.addImm(Addr.Offset).addReg(Addr.Base.Reg);544545// Indexed form.546} else {547// Get the RR opcode corresponding to the RI one. FIXME: It would be548// preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it549// is hard to get at.550switch (Opc) {551default: llvm_unreachable("Unexpected opcode!");552case PPC::LBZ: Opc = PPC::LBZX; break;553case PPC::LBZ8: Opc = PPC::LBZX8; break;554case PPC::LHZ: Opc = PPC::LHZX; break;555case PPC::LHZ8: Opc = PPC::LHZX8; break;556case PPC::LHA: Opc = PPC::LHAX; break;557case PPC::LHA8: Opc = PPC::LHAX8; break;558case PPC::LWZ: Opc = PPC::LWZX; break;559case PPC::LWZ8: Opc = PPC::LWZX8; break;560case PPC::LWA: Opc = PPC::LWAX; break;561case PPC::LWA_32: Opc = PPC::LWAX_32; break;562case PPC::LD: Opc = PPC::LDX; break;563case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;564case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;565case PPC::EVLDD: Opc = PPC::EVLDDX; break;566case PPC::SPELWZ: Opc = PPC::SPELWZX; break;567}568569auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc),570ResultReg);571572// If we have an index register defined we use it in the store inst,573// otherwise we use X0 as base as it makes the vector instructions to574// use zero in the computation of the effective address regardless the575// content of the register.576if (IndexReg)577MIB.addReg(Addr.Base.Reg).addReg(IndexReg);578else579MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);580}581582return true;583}584585// Attempt to fast-select a load instruction.586bool PPCFastISel::SelectLoad(const Instruction *I) {587// FIXME: No atomic loads are supported.588if (cast<LoadInst>(I)->isAtomic())589return false;590591// Verify we have a legal type before going any further.592MVT VT;593if (!isLoadTypeLegal(I->getType(), VT))594return false;595596// See if we can handle this address.597Address Addr;598if (!PPCComputeAddress(I->getOperand(0), Addr))599return false;600601// Look at the currently assigned register for this instruction602// to determine the required register class. This is necessary603// to constrain RA from using R0/X0 when this is not legal.604Register AssignedReg = FuncInfo.ValueMap[I];605const TargetRegisterClass *RC =606AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;607608Register ResultReg = 0;609if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,610Subtarget->hasSPE() ? PPC::EVLDD : PPC::LFD))611return false;612updateValueMap(I, ResultReg);613return true;614}615616// Emit a store instruction to store SrcReg at Addr.617bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {618assert(SrcReg && "Nothing to store!");619unsigned Opc;620bool UseOffset = true;621622const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);623bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);624625switch (VT.SimpleTy) {626default: // e.g., vector types not handled627return false;628case MVT::i8:629Opc = Is32BitInt ? PPC::STB : PPC::STB8;630break;631case MVT::i16:632Opc = Is32BitInt ? PPC::STH : PPC::STH8;633break;634case MVT::i32:635assert(Is32BitInt && "Not GPRC for i32??");636Opc = PPC::STW;637break;638case MVT::i64:639Opc = PPC::STD;640UseOffset = ((Addr.Offset & 3) == 0);641break;642case MVT::f32:643Opc = Subtarget->hasSPE() ? PPC::SPESTW : PPC::STFS;644break;645case MVT::f64:646Opc = Subtarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;647break;648}649650// If necessary, materialize the offset into a register and use651// the indexed form. Also handle stack pointers with special needs.652unsigned IndexReg = 0;653PPCSimplifyAddress(Addr, UseOffset, IndexReg);654655// If this is a potential VSX store with an offset of 0, a VSX indexed store656// can be used.657bool IsVSSRC = isVSSRCRegClass(RC);658bool IsVSFRC = isVSFRCRegClass(RC);659bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;660bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;661if ((Is32VSXStore || Is64VSXStore) &&662(Addr.BaseType != Address::FrameIndexBase) && UseOffset &&663(Addr.Offset == 0)) {664UseOffset = false;665}666667// Note: If we still have a frame index here, we know the offset is668// in range, as otherwise PPCSimplifyAddress would have converted it669// into a RegBase.670if (Addr.BaseType == Address::FrameIndexBase) {671// VSX only provides an indexed store.672if (Is32VSXStore || Is64VSXStore) return false;673674MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(675MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,676Addr.Offset),677MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),678MFI.getObjectAlign(Addr.Base.FI));679680BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc))681.addReg(SrcReg)682.addImm(Addr.Offset)683.addFrameIndex(Addr.Base.FI)684.addMemOperand(MMO);685686// Base reg with offset in range.687} else if (UseOffset) {688// VSX only provides an indexed store.689if (Is32VSXStore || Is64VSXStore)690return false;691692BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc))693.addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);694695// Indexed form.696} else {697// Get the RR opcode corresponding to the RI one. FIXME: It would be698// preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it699// is hard to get at.700switch (Opc) {701default: llvm_unreachable("Unexpected opcode!");702case PPC::STB: Opc = PPC::STBX; break;703case PPC::STH : Opc = PPC::STHX; break;704case PPC::STW : Opc = PPC::STWX; break;705case PPC::STB8: Opc = PPC::STBX8; break;706case PPC::STH8: Opc = PPC::STHX8; break;707case PPC::STW8: Opc = PPC::STWX8; break;708case PPC::STD: Opc = PPC::STDX; break;709case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;710case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;711case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;712case PPC::SPESTW: Opc = PPC::SPESTWX; break;713}714715auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc))716.addReg(SrcReg);717718// If we have an index register defined we use it in the store inst,719// otherwise we use X0 as base as it makes the vector instructions to720// use zero in the computation of the effective address regardless the721// content of the register.722if (IndexReg)723MIB.addReg(Addr.Base.Reg).addReg(IndexReg);724else725MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);726}727728return true;729}730731// Attempt to fast-select a store instruction.732bool PPCFastISel::SelectStore(const Instruction *I) {733Value *Op0 = I->getOperand(0);734unsigned SrcReg = 0;735736// FIXME: No atomics loads are supported.737if (cast<StoreInst>(I)->isAtomic())738return false;739740// Verify we have a legal type before going any further.741MVT VT;742if (!isLoadTypeLegal(Op0->getType(), VT))743return false;744745// Get the value to be stored into a register.746SrcReg = getRegForValue(Op0);747if (SrcReg == 0)748return false;749750// See if we can handle this address.751Address Addr;752if (!PPCComputeAddress(I->getOperand(1), Addr))753return false;754755if (!PPCEmitStore(VT, SrcReg, Addr))756return false;757758return true;759}760761// Attempt to fast-select a branch instruction.762bool PPCFastISel::SelectBranch(const Instruction *I) {763const BranchInst *BI = cast<BranchInst>(I);764MachineBasicBlock *BrBB = FuncInfo.MBB;765MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];766MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];767768// For now, just try the simplest case where it's fed by a compare.769if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {770if (isValueAvailable(CI)) {771std::optional<PPC::Predicate> OptPPCPred =772getComparePred(CI->getPredicate());773if (!OptPPCPred)774return false;775776PPC::Predicate PPCPred = *OptPPCPred;777778// Take advantage of fall-through opportunities.779if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {780std::swap(TBB, FBB);781PPCPred = PPC::InvertPredicate(PPCPred);782}783784Register CondReg = createResultReg(&PPC::CRRCRegClass);785786if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),787CondReg, PPCPred))788return false;789790BuildMI(*BrBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::BCC))791.addImm(Subtarget->hasSPE() ? PPC::PRED_SPE : PPCPred)792.addReg(CondReg)793.addMBB(TBB);794finishCondBranch(BI->getParent(), TBB, FBB);795return true;796}797} else if (const ConstantInt *CI =798dyn_cast<ConstantInt>(BI->getCondition())) {799uint64_t Imm = CI->getZExtValue();800MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;801fastEmitBranch(Target, MIMD.getDL());802return true;803}804805// FIXME: ARM looks for a case where the block containing the compare806// has been split from the block containing the branch. If this happens,807// there is a vreg available containing the result of the compare. I'm808// not sure we can do much, as we've lost the predicate information with809// the compare instruction -- we have a 4-bit CR but don't know which bit810// to test here.811return false;812}813814// Attempt to emit a compare of the two source values. Signed and unsigned815// comparisons are supported. Return false if we can't handle it.816bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,817bool IsZExt, unsigned DestReg,818const PPC::Predicate Pred) {819Type *Ty = SrcValue1->getType();820EVT SrcEVT = TLI.getValueType(DL, Ty, true);821if (!SrcEVT.isSimple())822return false;823MVT SrcVT = SrcEVT.getSimpleVT();824825if (SrcVT == MVT::i1 && Subtarget->useCRBits())826return false;827828// See if operand 2 is an immediate encodeable in the compare.829// FIXME: Operands are not in canonical order at -O0, so an immediate830// operand in position 1 is a lost opportunity for now. We are831// similar to ARM in this regard.832int64_t Imm = 0;833bool UseImm = false;834const bool HasSPE = Subtarget->hasSPE();835836// Only 16-bit integer constants can be represented in compares for837// PowerPC. Others will be materialized into a register.838if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {839if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||840SrcVT == MVT::i8 || SrcVT == MVT::i1) {841const APInt &CIVal = ConstInt->getValue();842Imm = (IsZExt) ? (int64_t)CIVal.getZExtValue() :843(int64_t)CIVal.getSExtValue();844if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))845UseImm = true;846}847}848849Register SrcReg1 = getRegForValue(SrcValue1);850if (SrcReg1 == 0)851return false;852853unsigned SrcReg2 = 0;854if (!UseImm) {855SrcReg2 = getRegForValue(SrcValue2);856if (SrcReg2 == 0)857return false;858}859860unsigned CmpOpc;861bool NeedsExt = false;862863auto RC1 = MRI.getRegClass(SrcReg1);864auto RC2 = SrcReg2 != 0 ? MRI.getRegClass(SrcReg2) : nullptr;865866switch (SrcVT.SimpleTy) {867default: return false;868case MVT::f32:869if (HasSPE) {870switch (Pred) {871default: return false;872case PPC::PRED_EQ:873CmpOpc = PPC::EFSCMPEQ;874break;875case PPC::PRED_LT:876CmpOpc = PPC::EFSCMPLT;877break;878case PPC::PRED_GT:879CmpOpc = PPC::EFSCMPGT;880break;881}882} else {883CmpOpc = PPC::FCMPUS;884if (isVSSRCRegClass(RC1))885SrcReg1 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg1);886if (RC2 && isVSSRCRegClass(RC2))887SrcReg2 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg2);888}889break;890case MVT::f64:891if (HasSPE) {892switch (Pred) {893default: return false;894case PPC::PRED_EQ:895CmpOpc = PPC::EFDCMPEQ;896break;897case PPC::PRED_LT:898CmpOpc = PPC::EFDCMPLT;899break;900case PPC::PRED_GT:901CmpOpc = PPC::EFDCMPGT;902break;903}904} else if (isVSFRCRegClass(RC1) || (RC2 && isVSFRCRegClass(RC2))) {905CmpOpc = PPC::XSCMPUDP;906} else {907CmpOpc = PPC::FCMPUD;908}909break;910case MVT::i1:911case MVT::i8:912case MVT::i16:913NeedsExt = true;914[[fallthrough]];915case MVT::i32:916if (!UseImm)917CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;918else919CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;920break;921case MVT::i64:922if (!UseImm)923CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;924else925CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;926break;927}928929if (NeedsExt) {930Register ExtReg = createResultReg(&PPC::GPRCRegClass);931if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))932return false;933SrcReg1 = ExtReg;934935if (!UseImm) {936Register ExtReg = createResultReg(&PPC::GPRCRegClass);937if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))938return false;939SrcReg2 = ExtReg;940}941}942943if (!UseImm)944BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(CmpOpc), DestReg)945.addReg(SrcReg1).addReg(SrcReg2);946else947BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(CmpOpc), DestReg)948.addReg(SrcReg1).addImm(Imm);949950return true;951}952953// Attempt to fast-select a floating-point extend instruction.954bool PPCFastISel::SelectFPExt(const Instruction *I) {955Value *Src = I->getOperand(0);956EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);957EVT DestVT = TLI.getValueType(DL, I->getType(), true);958959if (SrcVT != MVT::f32 || DestVT != MVT::f64)960return false;961962Register SrcReg = getRegForValue(Src);963if (!SrcReg)964return false;965966// No code is generated for a FP extend.967updateValueMap(I, SrcReg);968return true;969}970971// Attempt to fast-select a floating-point truncate instruction.972bool PPCFastISel::SelectFPTrunc(const Instruction *I) {973Value *Src = I->getOperand(0);974EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);975EVT DestVT = TLI.getValueType(DL, I->getType(), true);976977if (SrcVT != MVT::f64 || DestVT != MVT::f32)978return false;979980Register SrcReg = getRegForValue(Src);981if (!SrcReg)982return false;983984// Round the result to single precision.985unsigned DestReg;986auto RC = MRI.getRegClass(SrcReg);987if (Subtarget->hasSPE()) {988DestReg = createResultReg(&PPC::GPRCRegClass);989BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::EFSCFD),990DestReg)991.addReg(SrcReg);992} else if (Subtarget->hasP8Vector() && isVSFRCRegClass(RC)) {993DestReg = createResultReg(&PPC::VSSRCRegClass);994BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::XSRSP),995DestReg)996.addReg(SrcReg);997} else {998SrcReg = copyRegToRegClass(&PPC::F8RCRegClass, SrcReg);999DestReg = createResultReg(&PPC::F4RCRegClass);1000BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1001TII.get(PPC::FRSP), DestReg)1002.addReg(SrcReg);1003}10041005updateValueMap(I, DestReg);1006return true;1007}10081009// Move an i32 or i64 value in a GPR to an f64 value in an FPR.1010// FIXME: When direct register moves are implemented (see PowerISA 2.07),1011// those should be used instead of moving via a stack slot when the1012// subtarget permits.1013// FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte1014// stack slot and 4-byte store/load sequence. Or just sext the 4-byte1015// case to 8 bytes which produces tighter code but wastes stack space.1016unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,1017bool IsSigned) {10181019// If necessary, extend 32-bit int to 64-bit.1020if (SrcVT == MVT::i32) {1021Register TmpReg = createResultReg(&PPC::G8RCRegClass);1022if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))1023return 0;1024SrcReg = TmpReg;1025}10261027// Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.1028Address Addr;1029Addr.BaseType = Address::FrameIndexBase;1030Addr.Base.FI = MFI.CreateStackObject(8, Align(8), false);10311032// Store the value from the GPR.1033if (!PPCEmitStore(MVT::i64, SrcReg, Addr))1034return 0;10351036// Load the integer value into an FPR. The kind of load used depends1037// on a number of conditions.1038unsigned LoadOpc = PPC::LFD;10391040if (SrcVT == MVT::i32) {1041if (!IsSigned) {1042LoadOpc = PPC::LFIWZX;1043Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;1044} else if (Subtarget->hasLFIWAX()) {1045LoadOpc = PPC::LFIWAX;1046Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;1047}1048}10491050const TargetRegisterClass *RC = &PPC::F8RCRegClass;1051Register ResultReg = 0;1052if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))1053return 0;10541055return ResultReg;1056}10571058// Attempt to fast-select an integer-to-floating-point conversion.1059// FIXME: Once fast-isel has better support for VSX, conversions using1060// direct moves should be implemented.1061bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {1062MVT DstVT;1063Type *DstTy = I->getType();1064if (!isTypeLegal(DstTy, DstVT))1065return false;10661067if (DstVT != MVT::f32 && DstVT != MVT::f64)1068return false;10691070Value *Src = I->getOperand(0);1071EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);1072if (!SrcEVT.isSimple())1073return false;10741075MVT SrcVT = SrcEVT.getSimpleVT();10761077if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&1078SrcVT != MVT::i32 && SrcVT != MVT::i64)1079return false;10801081Register SrcReg = getRegForValue(Src);1082if (SrcReg == 0)1083return false;10841085// Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs1086if (Subtarget->hasSPE()) {1087unsigned Opc;1088if (DstVT == MVT::f32)1089Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;1090else1091Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;10921093Register DestReg = createResultReg(&PPC::SPERCRegClass);1094// Generate the convert.1095BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)1096.addReg(SrcReg);1097updateValueMap(I, DestReg);1098return true;1099}11001101// We can only lower an unsigned convert if we have the newer1102// floating-point conversion operations.1103if (!IsSigned && !Subtarget->hasFPCVT())1104return false;11051106// FIXME: For now we require the newer floating-point conversion operations1107// (which are present only on P7 and A2 server models) when converting1108// to single-precision float. Otherwise we have to generate a lot of1109// fiddly code to avoid double rounding. If necessary, the fiddly code1110// can be found in PPCTargetLowering::LowerINT_TO_FP().1111if (DstVT == MVT::f32 && !Subtarget->hasFPCVT())1112return false;11131114// Extend the input if necessary.1115if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {1116Register TmpReg = createResultReg(&PPC::G8RCRegClass);1117if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))1118return false;1119SrcVT = MVT::i64;1120SrcReg = TmpReg;1121}11221123// Move the integer value to an FPR.1124unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);1125if (FPReg == 0)1126return false;11271128// Determine the opcode for the conversion.1129const TargetRegisterClass *RC = &PPC::F8RCRegClass;1130Register DestReg = createResultReg(RC);1131unsigned Opc;11321133if (DstVT == MVT::f32)1134Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;1135else1136Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;11371138// Generate the convert.1139BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)1140.addReg(FPReg);11411142updateValueMap(I, DestReg);1143return true;1144}11451146// Move the floating-point value in SrcReg into an integer destination1147// register, and return the register (or zero if we can't handle it).1148// FIXME: When direct register moves are implemented (see PowerISA 2.07),1149// those should be used instead of moving via a stack slot when the1150// subtarget permits.1151unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,1152unsigned SrcReg, bool IsSigned) {1153// Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.1154// Note that if have STFIWX available, we could use a 4-byte stack1155// slot for i32, but this being fast-isel we'll just go with the1156// easiest code gen possible.1157Address Addr;1158Addr.BaseType = Address::FrameIndexBase;1159Addr.Base.FI = MFI.CreateStackObject(8, Align(8), false);11601161// Store the value from the FPR.1162if (!PPCEmitStore(MVT::f64, SrcReg, Addr))1163return 0;11641165// Reload it into a GPR. If we want an i32 on big endian, modify the1166// address to have a 4-byte offset so we load from the right place.1167if (VT == MVT::i32)1168Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;11691170// Look at the currently assigned register for this instruction1171// to determine the required register class.1172Register AssignedReg = FuncInfo.ValueMap[I];1173const TargetRegisterClass *RC =1174AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;11751176Register ResultReg = 0;1177if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))1178return 0;11791180return ResultReg;1181}11821183// Attempt to fast-select a floating-point-to-integer conversion.1184// FIXME: Once fast-isel has better support for VSX, conversions using1185// direct moves should be implemented.1186bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {1187MVT DstVT, SrcVT;1188Type *DstTy = I->getType();1189if (!isTypeLegal(DstTy, DstVT))1190return false;11911192if (DstVT != MVT::i32 && DstVT != MVT::i64)1193return false;11941195// If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.1196if (DstVT == MVT::i64 && !IsSigned && !Subtarget->hasFPCVT() &&1197!Subtarget->hasSPE())1198return false;11991200Value *Src = I->getOperand(0);1201Type *SrcTy = Src->getType();1202if (!isTypeLegal(SrcTy, SrcVT))1203return false;12041205if (SrcVT != MVT::f32 && SrcVT != MVT::f64)1206return false;12071208Register SrcReg = getRegForValue(Src);1209if (SrcReg == 0)1210return false;12111212// Convert f32 to f64 or convert VSSRC to VSFRC if necessary. This is just a1213// meaningless copy to get the register class right.1214const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);1215if (InRC == &PPC::F4RCRegClass)1216SrcReg = copyRegToRegClass(&PPC::F8RCRegClass, SrcReg);1217else if (InRC == &PPC::VSSRCRegClass)1218SrcReg = copyRegToRegClass(&PPC::VSFRCRegClass, SrcReg);12191220// Determine the opcode for the conversion, which takes place1221// entirely within FPRs or VSRs.1222unsigned DestReg;1223unsigned Opc;1224auto RC = MRI.getRegClass(SrcReg);12251226if (Subtarget->hasSPE()) {1227DestReg = createResultReg(&PPC::GPRCRegClass);1228if (IsSigned)1229Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;1230else1231Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;1232} else if (isVSFRCRegClass(RC)) {1233DestReg = createResultReg(&PPC::VSFRCRegClass);1234if (DstVT == MVT::i32)1235Opc = IsSigned ? PPC::XSCVDPSXWS : PPC::XSCVDPUXWS;1236else1237Opc = IsSigned ? PPC::XSCVDPSXDS : PPC::XSCVDPUXDS;1238} else {1239DestReg = createResultReg(&PPC::F8RCRegClass);1240if (DstVT == MVT::i32)1241if (IsSigned)1242Opc = PPC::FCTIWZ;1243else1244Opc = Subtarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;1245else1246Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;1247}12481249// Generate the convert.1250BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)1251.addReg(SrcReg);12521253// Now move the integer value from a float register to an integer register.1254unsigned IntReg = Subtarget->hasSPE()1255? DestReg1256: PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);12571258if (IntReg == 0)1259return false;12601261updateValueMap(I, IntReg);1262return true;1263}12641265// Attempt to fast-select a binary integer operation that isn't already1266// handled automatically.1267bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {1268EVT DestVT = TLI.getValueType(DL, I->getType(), true);12691270// We can get here in the case when we have a binary operation on a non-legal1271// type and the target independent selector doesn't know how to handle it.1272if (DestVT != MVT::i16 && DestVT != MVT::i8)1273return false;12741275// Look at the currently assigned register for this instruction1276// to determine the required register class. If there is no register,1277// make a conservative choice (don't assign R0).1278Register AssignedReg = FuncInfo.ValueMap[I];1279const TargetRegisterClass *RC =1280(AssignedReg ? MRI.getRegClass(AssignedReg) :1281&PPC::GPRC_and_GPRC_NOR0RegClass);1282bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);12831284unsigned Opc;1285switch (ISDOpcode) {1286default: return false;1287case ISD::ADD:1288Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;1289break;1290case ISD::OR:1291Opc = IsGPRC ? PPC::OR : PPC::OR8;1292break;1293case ISD::SUB:1294Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;1295break;1296}12971298Register ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);1299Register SrcReg1 = getRegForValue(I->getOperand(0));1300if (SrcReg1 == 0) return false;13011302// Handle case of small immediate operand.1303if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {1304const APInt &CIVal = ConstInt->getValue();1305int Imm = (int)CIVal.getSExtValue();1306bool UseImm = true;1307if (isInt<16>(Imm)) {1308switch (Opc) {1309default:1310llvm_unreachable("Missing case!");1311case PPC::ADD4:1312Opc = PPC::ADDI;1313MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);1314break;1315case PPC::ADD8:1316Opc = PPC::ADDI8;1317MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);1318break;1319case PPC::OR:1320Opc = PPC::ORI;1321break;1322case PPC::OR8:1323Opc = PPC::ORI8;1324break;1325case PPC::SUBF:1326if (Imm == -32768)1327UseImm = false;1328else {1329Opc = PPC::ADDI;1330MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);1331Imm = -Imm;1332}1333break;1334case PPC::SUBF8:1335if (Imm == -32768)1336UseImm = false;1337else {1338Opc = PPC::ADDI8;1339MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);1340Imm = -Imm;1341}1342break;1343}13441345if (UseImm) {1346BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc),1347ResultReg)1348.addReg(SrcReg1)1349.addImm(Imm);1350updateValueMap(I, ResultReg);1351return true;1352}1353}1354}13551356// Reg-reg case.1357Register SrcReg2 = getRegForValue(I->getOperand(1));1358if (SrcReg2 == 0) return false;13591360// Reverse operands for subtract-from.1361if (ISDOpcode == ISD::SUB)1362std::swap(SrcReg1, SrcReg2);13631364BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)1365.addReg(SrcReg1).addReg(SrcReg2);1366updateValueMap(I, ResultReg);1367return true;1368}13691370// Handle arguments to a call that we're attempting to fast-select.1371// Return false if the arguments are too complex for us at the moment.1372bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,1373SmallVectorImpl<unsigned> &ArgRegs,1374SmallVectorImpl<MVT> &ArgVTs,1375SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,1376SmallVectorImpl<unsigned> &RegArgs,1377CallingConv::ID CC,1378unsigned &NumBytes,1379bool IsVarArg) {1380SmallVector<CCValAssign, 16> ArgLocs;1381CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);13821383// Reserve space for the linkage area on the stack.1384unsigned LinkageSize = Subtarget->getFrameLowering()->getLinkageSize();1385CCInfo.AllocateStack(LinkageSize, Align(8));13861387CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);13881389// Bail out if we can't handle any of the arguments.1390for (const CCValAssign &VA : ArgLocs) {1391MVT ArgVT = ArgVTs[VA.getValNo()];13921393// Skip vector arguments for now, as well as long double and1394// uint128_t, and anything that isn't passed in a register.1395if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||1396!VA.isRegLoc() || VA.needsCustom())1397return false;13981399// Skip bit-converted arguments for now.1400if (VA.getLocInfo() == CCValAssign::BCvt)1401return false;1402}14031404// Get a count of how many bytes are to be pushed onto the stack.1405NumBytes = CCInfo.getStackSize();14061407// The prolog code of the callee may store up to 8 GPR argument registers to1408// the stack, allowing va_start to index over them in memory if its varargs.1409// Because we cannot tell if this is needed on the caller side, we have to1410// conservatively assume that it is needed. As such, make sure we have at1411// least enough stack space for the caller to store the 8 GPRs.1412// FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.1413NumBytes = std::max(NumBytes, LinkageSize + 64);14141415// Issue CALLSEQ_START.1416BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1417TII.get(TII.getCallFrameSetupOpcode()))1418.addImm(NumBytes).addImm(0);14191420// Prepare to assign register arguments. Every argument uses up a1421// GPR protocol register even if it's passed in a floating-point1422// register (unless we're using the fast calling convention).1423unsigned NextGPR = PPC::X3;1424unsigned NextFPR = PPC::F1;14251426// Process arguments.1427for (const CCValAssign &VA : ArgLocs) {1428unsigned Arg = ArgRegs[VA.getValNo()];1429MVT ArgVT = ArgVTs[VA.getValNo()];14301431// Handle argument promotion and bitcasts.1432switch (VA.getLocInfo()) {1433default:1434llvm_unreachable("Unknown loc info!");1435case CCValAssign::Full:1436break;1437case CCValAssign::SExt: {1438MVT DestVT = VA.getLocVT();1439const TargetRegisterClass *RC =1440(DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;1441Register TmpReg = createResultReg(RC);1442if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))1443llvm_unreachable("Failed to emit a sext!");1444ArgVT = DestVT;1445Arg = TmpReg;1446break;1447}1448case CCValAssign::AExt:1449case CCValAssign::ZExt: {1450MVT DestVT = VA.getLocVT();1451const TargetRegisterClass *RC =1452(DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;1453Register TmpReg = createResultReg(RC);1454if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))1455llvm_unreachable("Failed to emit a zext!");1456ArgVT = DestVT;1457Arg = TmpReg;1458break;1459}1460case CCValAssign::BCvt: {1461// FIXME: Not yet handled.1462llvm_unreachable("Should have bailed before getting here!");1463break;1464}1465}14661467// Copy this argument to the appropriate register.1468unsigned ArgReg;1469if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {1470ArgReg = NextFPR++;1471if (CC != CallingConv::Fast)1472++NextGPR;1473} else1474ArgReg = NextGPR++;14751476BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1477TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);1478RegArgs.push_back(ArgReg);1479}14801481return true;1482}14831484// For a call that we've determined we can fast-select, finish the1485// call sequence and generate a copy to obtain the return value (if any).1486bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {1487CallingConv::ID CC = CLI.CallConv;14881489// Issue CallSEQ_END.1490BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1491TII.get(TII.getCallFrameDestroyOpcode()))1492.addImm(NumBytes).addImm(0);14931494// Next, generate a copy to obtain the return value.1495// FIXME: No multi-register return values yet, though I don't foresee1496// any real difficulties there.1497if (RetVT != MVT::isVoid) {1498SmallVector<CCValAssign, 16> RVLocs;1499CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);1500CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);1501CCValAssign &VA = RVLocs[0];1502assert(RVLocs.size() == 1 && "No support for multi-reg return values!");1503assert(VA.isRegLoc() && "Can only return in registers!");15041505MVT DestVT = VA.getValVT();1506MVT CopyVT = DestVT;15071508// Ints smaller than a register still arrive in a full 64-bit1509// register, so make sure we recognize this.1510if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)1511CopyVT = MVT::i64;15121513unsigned SourcePhysReg = VA.getLocReg();1514unsigned ResultReg = 0;15151516if (RetVT == CopyVT) {1517const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);1518ResultReg = copyRegToRegClass(CpyRC, SourcePhysReg);15191520// If necessary, round the floating result to single precision.1521} else if (CopyVT == MVT::f64) {1522ResultReg = createResultReg(TLI.getRegClassFor(RetVT));1523BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::FRSP),1524ResultReg).addReg(SourcePhysReg);15251526// If only the low half of a general register is needed, generate1527// a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be1528// used along the fast-isel path (not lowered), and downstream logic1529// also doesn't like a direct subreg copy on a physical reg.)1530} else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {1531// Convert physical register from G8RC to GPRC.1532SourcePhysReg -= PPC::X0 - PPC::R0;1533ResultReg = copyRegToRegClass(&PPC::GPRCRegClass, SourcePhysReg);1534}15351536assert(ResultReg && "ResultReg unset!");1537CLI.InRegs.push_back(SourcePhysReg);1538CLI.ResultReg = ResultReg;1539CLI.NumResultRegs = 1;1540}15411542return true;1543}15441545bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {1546CallingConv::ID CC = CLI.CallConv;1547bool IsTailCall = CLI.IsTailCall;1548bool IsVarArg = CLI.IsVarArg;1549const Value *Callee = CLI.Callee;1550const MCSymbol *Symbol = CLI.Symbol;15511552if (!Callee && !Symbol)1553return false;15541555// Allow SelectionDAG isel to handle tail calls and long calls.1556if (IsTailCall || Subtarget->useLongCalls())1557return false;15581559// Let SDISel handle vararg functions.1560if (IsVarArg)1561return false;15621563// If this is a PC-Rel function, let SDISel handle the call.1564if (Subtarget->isUsingPCRelativeCalls())1565return false;15661567// Handle simple calls for now, with legal return types and1568// those that can be extended.1569Type *RetTy = CLI.RetTy;1570MVT RetVT;1571if (RetTy->isVoidTy())1572RetVT = MVT::isVoid;1573else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&1574RetVT != MVT::i8)1575return false;1576else if (RetVT == MVT::i1 && Subtarget->useCRBits())1577// We can't handle boolean returns when CR bits are in use.1578return false;15791580// FIXME: No multi-register return values yet.1581if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&1582RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&1583RetVT != MVT::f64) {1584SmallVector<CCValAssign, 16> RVLocs;1585CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);1586CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);1587if (RVLocs.size() > 1)1588return false;1589}15901591// Bail early if more than 8 arguments, as we only currently1592// handle arguments passed in registers.1593unsigned NumArgs = CLI.OutVals.size();1594if (NumArgs > 8)1595return false;15961597// Set up the argument vectors.1598SmallVector<Value*, 8> Args;1599SmallVector<unsigned, 8> ArgRegs;1600SmallVector<MVT, 8> ArgVTs;1601SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;16021603Args.reserve(NumArgs);1604ArgRegs.reserve(NumArgs);1605ArgVTs.reserve(NumArgs);1606ArgFlags.reserve(NumArgs);16071608for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {1609// Only handle easy calls for now. It would be reasonably easy1610// to handle <= 8-byte structures passed ByVal in registers, but we1611// have to ensure they are right-justified in the register.1612ISD::ArgFlagsTy Flags = CLI.OutFlags[i];1613if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())1614return false;16151616Value *ArgValue = CLI.OutVals[i];1617Type *ArgTy = ArgValue->getType();1618MVT ArgVT;1619if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)1620return false;16211622// FIXME: FastISel cannot handle non-simple types yet, including 128-bit FP1623// types, which is passed through vector register. Skip these types and1624// fallback to default SelectionDAG based selection.1625if (ArgVT.isVector() || ArgVT == MVT::f128)1626return false;16271628Register Arg = getRegForValue(ArgValue);1629if (Arg == 0)1630return false;16311632Args.push_back(ArgValue);1633ArgRegs.push_back(Arg);1634ArgVTs.push_back(ArgVT);1635ArgFlags.push_back(Flags);1636}16371638// Process the arguments.1639SmallVector<unsigned, 8> RegArgs;1640unsigned NumBytes;16411642if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,1643RegArgs, CC, NumBytes, IsVarArg))1644return false;16451646MachineInstrBuilder MIB;1647// FIXME: No handling for function pointers yet. This requires1648// implementing the function descriptor (OPD) setup.1649const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);1650if (!GV) {1651// patchpoints are a special case; they always dispatch to a pointer value.1652// However, we don't actually want to generate the indirect call sequence1653// here (that will be generated, as necessary, during asm printing), and1654// the call we generate here will be erased by FastISel::selectPatchpoint,1655// so don't try very hard...1656if (CLI.IsPatchPoint)1657MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::NOP));1658else1659return false;1660} else {1661// Build direct call with NOP for TOC restore.1662// FIXME: We can and should optimize away the NOP for local calls.1663MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1664TII.get(PPC::BL8_NOP));1665// Add callee.1666MIB.addGlobalAddress(GV);1667}16681669// Add implicit physical register uses to the call.1670for (unsigned Reg : RegArgs)1671MIB.addReg(Reg, RegState::Implicit);16721673// Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live1674// into the call.1675PPCFuncInfo->setUsesTOCBasePtr();1676MIB.addReg(PPC::X2, RegState::Implicit);16771678// Add a register mask with the call-preserved registers. Proper1679// defs for return values will be added by setPhysRegsDeadExcept().1680MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));16811682CLI.Call = MIB;16831684// Finish off the call including any return values.1685return finishCall(RetVT, CLI, NumBytes);1686}16871688// Attempt to fast-select a return instruction.1689bool PPCFastISel::SelectRet(const Instruction *I) {16901691if (!FuncInfo.CanLowerReturn)1692return false;16931694const ReturnInst *Ret = cast<ReturnInst>(I);1695const Function &F = *I->getParent()->getParent();16961697// Build a list of return value registers.1698SmallVector<unsigned, 4> RetRegs;1699CallingConv::ID CC = F.getCallingConv();17001701if (Ret->getNumOperands() > 0) {1702SmallVector<ISD::OutputArg, 4> Outs;1703GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);17041705// Analyze operands of the call, assigning locations to each operand.1706SmallVector<CCValAssign, 16> ValLocs;1707CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);1708CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);1709const Value *RV = Ret->getOperand(0);17101711// FIXME: Only one output register for now.1712if (ValLocs.size() > 1)1713return false;17141715// Special case for returning a constant integer of any size - materialize1716// the constant as an i64 and copy it to the return register.1717if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {1718CCValAssign &VA = ValLocs[0];17191720Register RetReg = VA.getLocReg();1721// We still need to worry about properly extending the sign. For example,1722// we could have only a single bit or a constant that needs zero1723// extension rather than sign extension. Make sure we pass the return1724// value extension property to integer materialization.1725unsigned SrcReg =1726PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);17271728BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1729TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);17301731RetRegs.push_back(RetReg);17321733} else {1734Register Reg = getRegForValue(RV);17351736if (Reg == 0)1737return false;17381739// Copy the result values into the output registers.1740for (unsigned i = 0; i < ValLocs.size(); ++i) {17411742CCValAssign &VA = ValLocs[i];1743assert(VA.isRegLoc() && "Can only return in registers!");1744RetRegs.push_back(VA.getLocReg());1745unsigned SrcReg = Reg + VA.getValNo();17461747EVT RVEVT = TLI.getValueType(DL, RV->getType());1748if (!RVEVT.isSimple())1749return false;1750MVT RVVT = RVEVT.getSimpleVT();1751MVT DestVT = VA.getLocVT();17521753if (RVVT != DestVT && RVVT != MVT::i8 &&1754RVVT != MVT::i16 && RVVT != MVT::i32)1755return false;17561757if (RVVT != DestVT) {1758switch (VA.getLocInfo()) {1759default:1760llvm_unreachable("Unknown loc info!");1761case CCValAssign::Full:1762llvm_unreachable("Full value assign but types don't match?");1763case CCValAssign::AExt:1764case CCValAssign::ZExt: {1765const TargetRegisterClass *RC =1766(DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;1767Register TmpReg = createResultReg(RC);1768if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))1769return false;1770SrcReg = TmpReg;1771break;1772}1773case CCValAssign::SExt: {1774const TargetRegisterClass *RC =1775(DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;1776Register TmpReg = createResultReg(RC);1777if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))1778return false;1779SrcReg = TmpReg;1780break;1781}1782}1783}17841785BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1786TII.get(TargetOpcode::COPY), RetRegs[i])1787.addReg(SrcReg);1788}1789}1790}17911792MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1793TII.get(PPC::BLR8));17941795for (unsigned Reg : RetRegs)1796MIB.addReg(Reg, RegState::Implicit);17971798return true;1799}18001801// Attempt to emit an integer extend of SrcReg into DestReg. Both1802// signed and zero extensions are supported. Return false if we1803// can't handle it.1804bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,1805unsigned DestReg, bool IsZExt) {1806if (DestVT != MVT::i32 && DestVT != MVT::i64)1807return false;1808if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)1809return false;18101811// Signed extensions use EXTSB, EXTSH, EXTSW.1812if (!IsZExt) {1813unsigned Opc;1814if (SrcVT == MVT::i8)1815Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;1816else if (SrcVT == MVT::i16)1817Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;1818else {1819assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");1820Opc = PPC::EXTSW_32_64;1821}1822BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)1823.addReg(SrcReg);18241825// Unsigned 32-bit extensions use RLWINM.1826} else if (DestVT == MVT::i32) {1827unsigned MB;1828if (SrcVT == MVT::i8)1829MB = 24;1830else {1831assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");1832MB = 16;1833}1834BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::RLWINM),1835DestReg)1836.addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);18371838// Unsigned 64-bit extensions use RLDICL (with a 32-bit source).1839} else {1840unsigned MB;1841if (SrcVT == MVT::i8)1842MB = 56;1843else if (SrcVT == MVT::i16)1844MB = 48;1845else1846MB = 32;1847BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,1848TII.get(PPC::RLDICL_32_64), DestReg)1849.addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);1850}18511852return true;1853}18541855// Attempt to fast-select an indirect branch instruction.1856bool PPCFastISel::SelectIndirectBr(const Instruction *I) {1857Register AddrReg = getRegForValue(I->getOperand(0));1858if (AddrReg == 0)1859return false;18601861BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::MTCTR8))1862.addReg(AddrReg);1863BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::BCTR8));18641865const IndirectBrInst *IB = cast<IndirectBrInst>(I);1866for (const BasicBlock *SuccBB : IB->successors())1867FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);18681869return true;1870}18711872// Attempt to fast-select an integer truncate instruction.1873bool PPCFastISel::SelectTrunc(const Instruction *I) {1874Value *Src = I->getOperand(0);1875EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);1876EVT DestVT = TLI.getValueType(DL, I->getType(), true);18771878if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)1879return false;18801881if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)1882return false;18831884Register SrcReg = getRegForValue(Src);1885if (!SrcReg)1886return false;18871888// The only interesting case is when we need to switch register classes.1889if (SrcVT == MVT::i64)1890SrcReg = copyRegToRegClass(&PPC::GPRCRegClass, SrcReg, 0, PPC::sub_32);18911892updateValueMap(I, SrcReg);1893return true;1894}18951896// Attempt to fast-select an integer extend instruction.1897bool PPCFastISel::SelectIntExt(const Instruction *I) {1898Type *DestTy = I->getType();1899Value *Src = I->getOperand(0);1900Type *SrcTy = Src->getType();19011902bool IsZExt = isa<ZExtInst>(I);1903Register SrcReg = getRegForValue(Src);1904if (!SrcReg) return false;19051906EVT SrcEVT, DestEVT;1907SrcEVT = TLI.getValueType(DL, SrcTy, true);1908DestEVT = TLI.getValueType(DL, DestTy, true);1909if (!SrcEVT.isSimple())1910return false;1911if (!DestEVT.isSimple())1912return false;19131914MVT SrcVT = SrcEVT.getSimpleVT();1915MVT DestVT = DestEVT.getSimpleVT();19161917// If we know the register class needed for the result of this1918// instruction, use it. Otherwise pick the register class of the1919// correct size that does not contain X0/R0, since we don't know1920// whether downstream uses permit that assignment.1921Register AssignedReg = FuncInfo.ValueMap[I];1922const TargetRegisterClass *RC =1923(AssignedReg ? MRI.getRegClass(AssignedReg) :1924(DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :1925&PPC::GPRC_and_GPRC_NOR0RegClass));1926Register ResultReg = createResultReg(RC);19271928if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))1929return false;19301931updateValueMap(I, ResultReg);1932return true;1933}19341935// Attempt to fast-select an instruction that wasn't handled by1936// the table-generated machinery.1937bool PPCFastISel::fastSelectInstruction(const Instruction *I) {19381939switch (I->getOpcode()) {1940case Instruction::Load:1941return SelectLoad(I);1942case Instruction::Store:1943return SelectStore(I);1944case Instruction::Br:1945return SelectBranch(I);1946case Instruction::IndirectBr:1947return SelectIndirectBr(I);1948case Instruction::FPExt:1949return SelectFPExt(I);1950case Instruction::FPTrunc:1951return SelectFPTrunc(I);1952case Instruction::SIToFP:1953return SelectIToFP(I, /*IsSigned*/ true);1954case Instruction::UIToFP:1955return SelectIToFP(I, /*IsSigned*/ false);1956case Instruction::FPToSI:1957return SelectFPToI(I, /*IsSigned*/ true);1958case Instruction::FPToUI:1959return SelectFPToI(I, /*IsSigned*/ false);1960case Instruction::Add:1961return SelectBinaryIntOp(I, ISD::ADD);1962case Instruction::Or:1963return SelectBinaryIntOp(I, ISD::OR);1964case Instruction::Sub:1965return SelectBinaryIntOp(I, ISD::SUB);1966case Instruction::Ret:1967return SelectRet(I);1968case Instruction::Trunc:1969return SelectTrunc(I);1970case Instruction::ZExt:1971case Instruction::SExt:1972return SelectIntExt(I);1973// Here add other flavors of Instruction::XXX that automated1974// cases don't catch. For example, switches are terminators1975// that aren't yet handled.1976default:1977break;1978}1979return false;1980}19811982// Materialize a floating-point constant into a register, and return1983// the register number (or zero if we failed to handle it).1984unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {1985// If this is a PC-Rel function, let SDISel handle constant pool.1986if (Subtarget->isUsingPCRelativeCalls())1987return false;19881989// No plans to handle long double here.1990if (VT != MVT::f32 && VT != MVT::f64)1991return 0;19921993// All FP constants are loaded from the constant pool.1994Align Alignment = DL.getPrefTypeAlign(CFP->getType());1995unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Alignment);1996const bool HasSPE = Subtarget->hasSPE();1997const TargetRegisterClass *RC;1998if (HasSPE)1999RC = ((VT == MVT::f32) ? &PPC::GPRCRegClass : &PPC::SPERCRegClass);2000else2001RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);20022003Register DestReg = createResultReg(RC);2004CodeModel::Model CModel = TM.getCodeModel();20052006MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(2007MachinePointerInfo::getConstantPool(*FuncInfo.MF),2008MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Alignment);20092010unsigned Opc;20112012if (HasSPE)2013Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);2014else2015Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);20162017Register TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);20182019PPCFuncInfo->setUsesTOCBasePtr();2020// For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).2021if (CModel == CodeModel::Small) {2022BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::LDtocCPT),2023TmpReg)2024.addConstantPoolIndex(Idx).addReg(PPC::X2);2025BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)2026.addImm(0).addReg(TmpReg).addMemOperand(MMO);2027} else {2028// Otherwise we generate LF[SD](Idx[lo], ADDIStocHA8(X2, Idx)).2029BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDIStocHA8),2030TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);2031// But for large code model, we must generate a LDtocL followed2032// by the LF[SD].2033if (CModel == CodeModel::Large) {2034Register TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);2035BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::LDtocL),2036TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);2037BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)2038.addImm(0)2039.addReg(TmpReg2);2040} else2041BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)2042.addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)2043.addReg(TmpReg)2044.addMemOperand(MMO);2045}20462047return DestReg;2048}20492050// Materialize the address of a global value into a register, and return2051// the register number (or zero if we failed to handle it).2052unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {2053// If this is a PC-Rel function, let SDISel handle GV materialization.2054if (Subtarget->isUsingPCRelativeCalls())2055return false;20562057assert(VT == MVT::i64 && "Non-address!");2058const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;2059Register DestReg = createResultReg(RC);20602061// Global values may be plain old object addresses, TLS object2062// addresses, constant pool entries, or jump tables. How we generate2063// code for these may depend on small, medium, or large code model.2064CodeModel::Model CModel = TM.getCodeModel();20652066// FIXME: Jump tables are not yet required because fast-isel doesn't2067// handle switches; if that changes, we need them as well. For now,2068// what follows assumes everything's a generic (or TLS) global address.20692070// FIXME: We don't yet handle the complexity of TLS.2071if (GV->isThreadLocal())2072return 0;20732074PPCFuncInfo->setUsesTOCBasePtr();2075bool IsAIXTocData = TM.getTargetTriple().isOSAIX() &&2076isa<GlobalVariable>(GV) &&2077cast<GlobalVariable>(GV)->hasAttribute("toc-data");20782079// For small code model, generate a simple TOC load.2080if (CModel == CodeModel::Small) {2081auto MIB = BuildMI(2082*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,2083IsAIXTocData ? TII.get(PPC::ADDItoc8) : TII.get(PPC::LDtoc), DestReg);2084if (IsAIXTocData)2085MIB.addReg(PPC::X2).addGlobalAddress(GV);2086else2087MIB.addGlobalAddress(GV).addReg(PPC::X2);2088} else {2089// If the address is an externally defined symbol, a symbol with common2090// or externally available linkage, a non-local function address, or a2091// jump table address (not yet needed), or if we are generating code2092// for large code model, we generate:2093// LDtocL(GV, ADDIStocHA8(%x2, GV))2094// Otherwise we generate:2095// ADDItocL8(ADDIStocHA8(%x2, GV), GV)2096// Either way, start with the ADDIStocHA8:2097Register HighPartReg = createResultReg(RC);2098BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDIStocHA8),2099HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);21002101if (Subtarget->isGVIndirectSymbol(GV)) {2102assert(!IsAIXTocData && "TOC data should always be direct.");2103BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::LDtocL),2104DestReg).addGlobalAddress(GV).addReg(HighPartReg);2105} else {2106// Otherwise generate the ADDItocL8.2107BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDItocL8),2108DestReg)2109.addReg(HighPartReg)2110.addGlobalAddress(GV);2111}2112}21132114return DestReg;2115}21162117// Materialize a 32-bit integer constant into a register, and return2118// the register number (or zero if we failed to handle it).2119unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,2120const TargetRegisterClass *RC) {2121unsigned Lo = Imm & 0xFFFF;2122unsigned Hi = (Imm >> 16) & 0xFFFF;21232124Register ResultReg = createResultReg(RC);2125bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);21262127if (isInt<16>(Imm))2128BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,2129TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)2130.addImm(Imm);2131else if (Lo) {2132// Both Lo and Hi have nonzero bits.2133Register TmpReg = createResultReg(RC);2134BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,2135TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)2136.addImm(Hi);2137BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,2138TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)2139.addReg(TmpReg).addImm(Lo);2140} else2141// Just Hi bits.2142BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,2143TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)2144.addImm(Hi);21452146return ResultReg;2147}21482149// Materialize a 64-bit integer constant into a register, and return2150// the register number (or zero if we failed to handle it).2151unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,2152const TargetRegisterClass *RC) {2153unsigned Remainder = 0;2154unsigned Shift = 0;21552156// If the value doesn't fit in 32 bits, see if we can shift it2157// so that it fits in 32 bits.2158if (!isInt<32>(Imm)) {2159Shift = llvm::countr_zero<uint64_t>(Imm);2160int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;21612162if (isInt<32>(ImmSh))2163Imm = ImmSh;2164else {2165Remainder = Imm;2166Shift = 32;2167Imm >>= 32;2168}2169}21702171// Handle the high-order 32 bits (if shifted) or the whole 32 bits2172// (if not shifted).2173unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);2174if (!Shift)2175return TmpReg1;21762177// If upper 32 bits were not zero, we've built them and need to shift2178// them into place.2179unsigned TmpReg2;2180if (Imm) {2181TmpReg2 = createResultReg(RC);2182BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::RLDICR),2183TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);2184} else2185TmpReg2 = TmpReg1;21862187unsigned TmpReg3, Hi, Lo;2188if ((Hi = (Remainder >> 16) & 0xFFFF)) {2189TmpReg3 = createResultReg(RC);2190BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ORIS8),2191TmpReg3).addReg(TmpReg2).addImm(Hi);2192} else2193TmpReg3 = TmpReg2;21942195if ((Lo = Remainder & 0xFFFF)) {2196Register ResultReg = createResultReg(RC);2197BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ORI8),2198ResultReg).addReg(TmpReg3).addImm(Lo);2199return ResultReg;2200}22012202return TmpReg3;2203}22042205// Materialize an integer constant into a register, and return2206// the register number (or zero if we failed to handle it).2207unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,2208bool UseSExt) {2209// If we're using CR bit registers for i1 values, handle that as a special2210// case first.2211if (VT == MVT::i1 && Subtarget->useCRBits()) {2212Register ImmReg = createResultReg(&PPC::CRBITRCRegClass);2213BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,2214TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);2215return ImmReg;2216}22172218if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&2219VT != MVT::i1)2220return 0;22212222const TargetRegisterClass *RC =2223((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);2224int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();22252226// If the constant is in range, use a load-immediate.2227// Since LI will sign extend the constant we need to make sure that for2228// our zeroext constants that the sign extended constant fits into 16-bits -2229// a range of 0..0x7fff.2230if (isInt<16>(Imm)) {2231unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;2232Register ImmReg = createResultReg(RC);2233BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ImmReg)2234.addImm(Imm);2235return ImmReg;2236}22372238// Construct the constant piecewise.2239if (VT == MVT::i64)2240return PPCMaterialize64BitInt(Imm, RC);2241else if (VT == MVT::i32)2242return PPCMaterialize32BitInt(Imm, RC);22432244return 0;2245}22462247// Materialize a constant into a register, and return the register2248// number (or zero if we failed to handle it).2249unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {2250EVT CEVT = TLI.getValueType(DL, C->getType(), true);22512252// Only handle simple types.2253if (!CEVT.isSimple()) return 0;2254MVT VT = CEVT.getSimpleVT();22552256if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))2257return PPCMaterializeFP(CFP, VT);2258else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))2259return PPCMaterializeGV(GV, VT);2260else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))2261// Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo2262// assumes that constant PHI operands will be zero extended, and failure to2263// match that assumption will cause problems if we sign extend here but2264// some user of a PHI is in a block for which we fall back to full SDAG2265// instruction selection.2266return PPCMaterializeInt(CI, VT, false);22672268return 0;2269}22702271// Materialize the address created by an alloca into a register, and2272// return the register number (or zero if we failed to handle it).2273unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {2274// Don't handle dynamic allocas.2275if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;22762277MVT VT;2278if (!isLoadTypeLegal(AI->getType(), VT)) return 0;22792280DenseMap<const AllocaInst*, int>::iterator SI =2281FuncInfo.StaticAllocaMap.find(AI);22822283if (SI != FuncInfo.StaticAllocaMap.end()) {2284Register ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);2285BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDI8),2286ResultReg).addFrameIndex(SI->second).addImm(0);2287return ResultReg;2288}22892290return 0;2291}22922293// Fold loads into extends when possible.2294// FIXME: We can have multiple redundant extend/trunc instructions2295// following a load. The folding only picks up one. Extend this2296// to check subsequent instructions for the same pattern and remove2297// them. Thus ResultReg should be the def reg for the last redundant2298// instruction in a chain, and all intervening instructions can be2299// removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll2300// to add ELF64-NOT: rldicl to the appropriate tests when this works.2301bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,2302const LoadInst *LI) {2303// Verify we have a legal type before going any further.2304MVT VT;2305if (!isLoadTypeLegal(LI->getType(), VT))2306return false;23072308// Combine load followed by zero- or sign-extend.2309bool IsZExt = false;2310switch(MI->getOpcode()) {2311default:2312return false;23132314case PPC::RLDICL:2315case PPC::RLDICL_32_64: {2316IsZExt = true;2317unsigned MB = MI->getOperand(3).getImm();2318if ((VT == MVT::i8 && MB <= 56) ||2319(VT == MVT::i16 && MB <= 48) ||2320(VT == MVT::i32 && MB <= 32))2321break;2322return false;2323}23242325case PPC::RLWINM:2326case PPC::RLWINM8: {2327IsZExt = true;2328unsigned MB = MI->getOperand(3).getImm();2329if ((VT == MVT::i8 && MB <= 24) ||2330(VT == MVT::i16 && MB <= 16))2331break;2332return false;2333}23342335case PPC::EXTSB:2336case PPC::EXTSB8:2337case PPC::EXTSB8_32_64:2338/* There is no sign-extending load-byte instruction. */2339return false;23402341case PPC::EXTSH:2342case PPC::EXTSH8:2343case PPC::EXTSH8_32_64: {2344if (VT != MVT::i16 && VT != MVT::i8)2345return false;2346break;2347}23482349case PPC::EXTSW:2350case PPC::EXTSW_32:2351case PPC::EXTSW_32_64: {2352if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)2353return false;2354break;2355}2356}23572358// See if we can handle this address.2359Address Addr;2360if (!PPCComputeAddress(LI->getOperand(0), Addr))2361return false;23622363Register ResultReg = MI->getOperand(0).getReg();23642365if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,2366Subtarget->hasSPE() ? PPC::EVLDD : PPC::LFD))2367return false;23682369MachineBasicBlock::iterator I(MI);2370removeDeadCode(I, std::next(I));2371return true;2372}23732374// Attempt to lower call arguments in a faster way than done by2375// the selection DAG code.2376bool PPCFastISel::fastLowerArguments() {2377// Defer to normal argument lowering for now. It's reasonably2378// efficient. Consider doing something like ARM to handle the2379// case where all args fit in registers, no varargs, no float2380// or vector args.2381return false;2382}23832384// Handle materializing integer constants into a register. This is not2385// automatically generated for PowerPC, so must be explicitly created here.2386unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {23872388if (Opc != ISD::Constant)2389return 0;23902391// If we're using CR bit registers for i1 values, handle that as a special2392// case first.2393if (VT == MVT::i1 && Subtarget->useCRBits()) {2394Register ImmReg = createResultReg(&PPC::CRBITRCRegClass);2395BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,2396TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);2397return ImmReg;2398}23992400if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&2401VT != MVT::i1)2402return 0;24032404const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :2405&PPC::GPRCRegClass);2406if (VT == MVT::i64)2407return PPCMaterialize64BitInt(Imm, RC);2408else2409return PPCMaterialize32BitInt(Imm, RC);2410}24112412// Override for ADDI and ADDI8 to set the correct register class2413// on RHS operand 0. The automatic infrastructure naively assumes2414// GPRC for i32 and G8RC for i64; the concept of "no R0" is lost2415// for these cases. At the moment, none of the other automatically2416// generated RI instructions require special treatment. However, once2417// SelectSelect is implemented, "isel" requires similar handling.2418//2419// Also be conservative about the output register class. Avoid2420// assigning R0 or X0 to the output register for GPRC and G8RC2421// register classes, as any such result could be used in ADDI, etc.,2422// where those regs have another meaning.2423unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,2424const TargetRegisterClass *RC,2425unsigned Op0,2426uint64_t Imm) {2427if (MachineInstOpcode == PPC::ADDI)2428MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);2429else if (MachineInstOpcode == PPC::ADDI8)2430MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);24312432const TargetRegisterClass *UseRC =2433(RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :2434(RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));24352436return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC, Op0, Imm);2437}24382439// Override for instructions with one register operand to avoid use of2440// R0/X0. The automatic infrastructure isn't aware of the context so2441// we must be conservative.2442unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,2443const TargetRegisterClass* RC,2444unsigned Op0) {2445const TargetRegisterClass *UseRC =2446(RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :2447(RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));24482449return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0);2450}24512452// Override for instructions with two register operands to avoid use2453// of R0/X0. The automatic infrastructure isn't aware of the context2454// so we must be conservative.2455unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,2456const TargetRegisterClass* RC,2457unsigned Op0, unsigned Op1) {2458const TargetRegisterClass *UseRC =2459(RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :2460(RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));24612462return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op1);2463}24642465namespace llvm {2466// Create the fast instruction selector for PowerPC64 ELF.2467FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,2468const TargetLibraryInfo *LibInfo) {2469// Only available on 64-bit for now.2470const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();2471if (Subtarget.isPPC64())2472return new PPCFastISel(FuncInfo, LibInfo);2473return nullptr;2474}2475}247624772478