10cf702faSBill Schmidt //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
20cf702faSBill Schmidt //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60cf702faSBill Schmidt //
70cf702faSBill Schmidt //===----------------------------------------------------------------------===//
80cf702faSBill Schmidt //
90cf702faSBill Schmidt // This file defines the PowerPC-specific support for the FastISel class. Some
100cf702faSBill Schmidt // of the target-specific code is generated by tablegen in the file
110cf702faSBill Schmidt // PPCGenFastISel.inc, which is #included here.
120cf702faSBill Schmidt //
130cf702faSBill Schmidt //===----------------------------------------------------------------------===//
140cf702faSBill Schmidt 
158a8cd2baSChandler Carruth #include "MCTargetDesc/PPCPredicates.h"
166bda14b3SChandler Carruth #include "PPC.h"
17e682b80bSStrahinja Petrovic #include "PPCCCState.h"
186bda14b3SChandler Carruth #include "PPCCallingConv.h"
190cf702faSBill Schmidt #include "PPCISelLowering.h"
20e6698d53SHal Finkel #include "PPCMachineFunctionInfo.h"
210cf702faSBill Schmidt #include "PPCSubtarget.h"
220cf702faSBill Schmidt #include "PPCTargetMachine.h"
230cf702faSBill Schmidt #include "llvm/ADT/Optional.h"
240cf702faSBill Schmidt #include "llvm/CodeGen/CallingConvLower.h"
250cf702faSBill Schmidt #include "llvm/CodeGen/FastISel.h"
260cf702faSBill Schmidt #include "llvm/CodeGen/FunctionLoweringInfo.h"
270cf702faSBill Schmidt #include "llvm/CodeGen/MachineConstantPool.h"
280cf702faSBill Schmidt #include "llvm/CodeGen/MachineFrameInfo.h"
290cf702faSBill Schmidt #include "llvm/CodeGen/MachineInstrBuilder.h"
300cf702faSBill Schmidt #include "llvm/CodeGen/MachineRegisterInfo.h"
31b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetLowering.h"
320cf702faSBill Schmidt #include "llvm/IR/CallingConv.h"
3303eb0de9SChandler Carruth #include "llvm/IR/GetElementPtrTypeIterator.h"
340cf702faSBill Schmidt #include "llvm/IR/GlobalAlias.h"
350cf702faSBill Schmidt #include "llvm/IR/GlobalVariable.h"
360cf702faSBill Schmidt #include "llvm/IR/IntrinsicInst.h"
370cf702faSBill Schmidt #include "llvm/IR/Operator.h"
380cf702faSBill Schmidt #include "llvm/Support/Debug.h"
390cf702faSBill Schmidt #include "llvm/Target/TargetMachine.h"
400cf702faSBill Schmidt 
41eb8d6f7dSBill Schmidt //===----------------------------------------------------------------------===//
42eb8d6f7dSBill Schmidt //
43eb8d6f7dSBill Schmidt // TBD:
445b8bb4d7SJuergen Ributzka //   fastLowerArguments: Handle simple cases.
45eb8d6f7dSBill Schmidt //   PPCMaterializeGV: Handle TLS.
46eb8d6f7dSBill Schmidt //   SelectCall: Handle function pointers.
47eb8d6f7dSBill Schmidt //   SelectCall: Handle multi-register return values.
48eb8d6f7dSBill Schmidt //   SelectCall: Optimize away nops for local calls.
49eb8d6f7dSBill Schmidt //   processCallArgs: Handle bit-converted arguments.
50eb8d6f7dSBill Schmidt //   finishCall: Handle multi-register return values.
51eb8d6f7dSBill Schmidt //   PPCComputeAddress: Handle parameter references as FrameIndex's.
52eb8d6f7dSBill Schmidt //   PPCEmitCmp: Handle immediate as operand 1.
53eb8d6f7dSBill Schmidt //   SelectCall: Handle small byval arguments.
54eb8d6f7dSBill Schmidt //   SelectIntrinsicCall: Implement.
55eb8d6f7dSBill Schmidt //   SelectSelect: Implement.
56eb8d6f7dSBill Schmidt //   Consider factoring isTypeLegal into the base class.
57eb8d6f7dSBill Schmidt //   Implement switches and jump tables.
58eb8d6f7dSBill Schmidt //
59eb8d6f7dSBill Schmidt //===----------------------------------------------------------------------===//
600cf702faSBill Schmidt using namespace llvm;
610cf702faSBill Schmidt 
6284e68b29SChandler Carruth #define DEBUG_TYPE "ppcfastisel"
6384e68b29SChandler Carruth 
640cf702faSBill Schmidt namespace {
650cf702faSBill Schmidt 
6659d90fe8SFangrui Song struct Address {
670cf702faSBill Schmidt   enum {
680cf702faSBill Schmidt     RegBase,
690cf702faSBill Schmidt     FrameIndexBase
700cf702faSBill Schmidt   } BaseType;
710cf702faSBill Schmidt 
720cf702faSBill Schmidt   union {
730cf702faSBill Schmidt     unsigned Reg;
740cf702faSBill Schmidt     int FI;
750cf702faSBill Schmidt   } Base;
760cf702faSBill Schmidt 
77ccecf261SBill Schmidt   long Offset;
780cf702faSBill Schmidt 
790cf702faSBill Schmidt   // Innocuous defaults for our address.
Address__anonad57e5630111::Address800cf702faSBill Schmidt   Address()
810cf702faSBill Schmidt    : BaseType(RegBase), Offset(0) {
820cf702faSBill Schmidt      Base.Reg = 0;
830cf702faSBill Schmidt    }
8459d90fe8SFangrui Song };
850cf702faSBill Schmidt 
8626696314SCraig Topper class PPCFastISel final : public FastISel {
870cf702faSBill Schmidt 
880cf702faSBill Schmidt   const TargetMachine &TM;
895ca75130SKit Barton   const PPCSubtarget *Subtarget;
90e6698d53SHal Finkel   PPCFunctionInfo *PPCFuncInfo;
910cf702faSBill Schmidt   const TargetInstrInfo &TII;
920cf702faSBill Schmidt   const TargetLowering &TLI;
930cf702faSBill Schmidt   LLVMContext *Context;
940cf702faSBill Schmidt 
950cf702faSBill Schmidt   public:
PPCFastISel(FunctionLoweringInfo & FuncInfo,const TargetLibraryInfo * LibInfo)960cf702faSBill Schmidt     explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
970cf702faSBill Schmidt                          const TargetLibraryInfo *LibInfo)
98d913448bSEric Christopher         : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
995ca75130SKit Barton           Subtarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
100e6698d53SHal Finkel           PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
1015ca75130SKit Barton           TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()),
1020cf702faSBill Schmidt           Context(&FuncInfo.Fn->getContext()) {}
1030cf702faSBill Schmidt 
1040cf702faSBill Schmidt     // Backend specific FastISel code.
1050cf702faSBill Schmidt   private:
1065b8bb4d7SJuergen Ributzka     bool fastSelectInstruction(const Instruction *I) override;
1075b8bb4d7SJuergen Ributzka     unsigned fastMaterializeConstant(const Constant *C) override;
1085b8bb4d7SJuergen Ributzka     unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
1090d3fa925SCraig Topper     bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
1100d3fa925SCraig Topper                              const LoadInst *LI) override;
1115b8bb4d7SJuergen Ributzka     bool fastLowerArguments() override;
11288e32517SJuergen Ributzka     unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
11388e32517SJuergen Ributzka     unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
114ccecf261SBill Schmidt                              const TargetRegisterClass *RC,
11566506582SNikita Popov                              unsigned Op0, uint64_t Imm);
11688e32517SJuergen Ributzka     unsigned fastEmitInst_r(unsigned MachineInstOpcode,
11766506582SNikita Popov                             const TargetRegisterClass *RC, unsigned Op0);
11888e32517SJuergen Ributzka     unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
119ccecf261SBill Schmidt                              const TargetRegisterClass *RC,
12066506582SNikita Popov                              unsigned Op0, unsigned Op1);
1210300813dSBill Schmidt 
122934361a4SHal Finkel     bool fastLowerCall(CallLoweringInfo &CLI) override;
123934361a4SHal Finkel 
1240300813dSBill Schmidt   // Instruction selection routines.
1250300813dSBill Schmidt   private:
126ccecf261SBill Schmidt     bool SelectLoad(const Instruction *I);
127ccecf261SBill Schmidt     bool SelectStore(const Instruction *I);
1280300813dSBill Schmidt     bool SelectBranch(const Instruction *I);
1290300813dSBill Schmidt     bool SelectIndirectBr(const Instruction *I);
1308d86fe7dSBill Schmidt     bool SelectFPExt(const Instruction *I);
1318d86fe7dSBill Schmidt     bool SelectFPTrunc(const Instruction *I);
1328d86fe7dSBill Schmidt     bool SelectIToFP(const Instruction *I, bool IsSigned);
1338d86fe7dSBill Schmidt     bool SelectFPToI(const Instruction *I, bool IsSigned);
134ccecf261SBill Schmidt     bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
135d89f678cSBill Schmidt     bool SelectRet(const Instruction *I);
1369d9510d8SBill Schmidt     bool SelectTrunc(const Instruction *I);
137d89f678cSBill Schmidt     bool SelectIntExt(const Instruction *I);
1380cf702faSBill Schmidt 
1390cf702faSBill Schmidt   // Utility routines.
1400cf702faSBill Schmidt   private:
141ccecf261SBill Schmidt     bool isTypeLegal(Type *Ty, MVT &VT);
142ccecf261SBill Schmidt     bool isLoadTypeLegal(Type *Ty, MVT &VT);
1435f2a1379SHal Finkel     bool isValueAvailable(const Value *V) const;
isVSFRCRegClass(const TargetRegisterClass * RC) const144c3b495a6SUlrich Weigand     bool isVSFRCRegClass(const TargetRegisterClass *RC) const {
145c3b495a6SUlrich Weigand       return RC->getID() == PPC::VSFRCRegClassID;
1468c728ae9SBill Seurer     }
isVSSRCRegClass(const TargetRegisterClass * RC) const147c3b495a6SUlrich Weigand     bool isVSSRCRegClass(const TargetRegisterClass *RC) const {
148c3b495a6SUlrich Weigand       return RC->getID() == PPC::VSSRCRegClassID;
149376e1736SNemanja Ivanovic     }
copyRegToRegClass(const TargetRegisterClass * ToRC,unsigned SrcReg,unsigned Flag=0,unsigned SubReg=0)150fec749ffSZi Xuan Wu     unsigned copyRegToRegClass(const TargetRegisterClass *ToRC,
151fec749ffSZi Xuan Wu                                unsigned SrcReg, unsigned Flag = 0,
152fec749ffSZi Xuan Wu                                unsigned SubReg = 0) {
153d6b07348SJim Lin       Register TmpReg = createResultReg(ToRC);
154fec749ffSZi Xuan Wu       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
155fec749ffSZi Xuan Wu               TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg, Flag, SubReg);
156fec749ffSZi Xuan Wu       return TmpReg;
157fec749ffSZi Xuan Wu     }
1580300813dSBill Schmidt     bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
159d52990c7SJustin Hibbits                     bool isZExt, unsigned DestReg,
160d52990c7SJustin Hibbits                     const PPC::Predicate Pred);
1610c476111SDaniel Sanders     bool PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
162ccecf261SBill Schmidt                      const TargetRegisterClass *RC, bool IsZExt = true,
163ccecf261SBill Schmidt                      unsigned FP64LoadOpc = PPC::LFD);
164ccecf261SBill Schmidt     bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
165ccecf261SBill Schmidt     bool PPCComputeAddress(const Value *Obj, Address &Addr);
1663707ba80SUlrich Weigand     void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
167ccecf261SBill Schmidt                             unsigned &IndexReg);
1680300813dSBill Schmidt     bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1690300813dSBill Schmidt                            unsigned DestReg, bool IsZExt);
1700cf702faSBill Schmidt     unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
171ccecf261SBill Schmidt     unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
17203df7ac8SEric Christopher     unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
17303df7ac8SEric Christopher                                bool UseSExt = true);
1740cf702faSBill Schmidt     unsigned PPCMaterialize32BitInt(int64_t Imm,
1750cf702faSBill Schmidt                                     const TargetRegisterClass *RC);
1760cf702faSBill Schmidt     unsigned PPCMaterialize64BitInt(int64_t Imm,
1770cf702faSBill Schmidt                                     const TargetRegisterClass *RC);
1788d86fe7dSBill Schmidt     unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
1798d86fe7dSBill Schmidt                              unsigned SrcReg, bool IsSigned);
1808d86fe7dSBill Schmidt     unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
1810cf702faSBill Schmidt 
182d89f678cSBill Schmidt   // Call handling routines.
183d89f678cSBill Schmidt   private:
1848470b0f9SBill Schmidt     bool processCallArgs(SmallVectorImpl<Value*> &Args,
1858470b0f9SBill Schmidt                          SmallVectorImpl<unsigned> &ArgRegs,
1868470b0f9SBill Schmidt                          SmallVectorImpl<MVT> &ArgVTs,
1878470b0f9SBill Schmidt                          SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1888470b0f9SBill Schmidt                          SmallVectorImpl<unsigned> &RegArgs,
1898470b0f9SBill Schmidt                          CallingConv::ID CC,
1908470b0f9SBill Schmidt                          unsigned &NumBytes,
1918470b0f9SBill Schmidt                          bool IsVarArg);
192934361a4SHal Finkel     bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
193d89f678cSBill Schmidt 
1940cf702faSBill Schmidt   private:
1950cf702faSBill Schmidt   #include "PPCGenFastISel.inc"
1960cf702faSBill Schmidt 
1970cf702faSBill Schmidt };
1980cf702faSBill Schmidt 
1990cf702faSBill Schmidt } // end anonymous namespace
2000cf702faSBill Schmidt 
getComparePred(CmpInst::Predicate Pred)2010300813dSBill Schmidt static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
2020300813dSBill Schmidt   switch (Pred) {
2030300813dSBill Schmidt     // These are not representable with any single compare.
2040300813dSBill Schmidt     case CmpInst::FCMP_FALSE:
2055cdf7508STim Shen     case CmpInst::FCMP_TRUE:
2065cdf7508STim Shen     // Major concern about the following 6 cases is NaN result. The comparison
2075cdf7508STim Shen     // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
2085cdf7508STim Shen     // only one of which will be set. The result is generated by fcmpu
2095cdf7508STim Shen     // instruction. However, bc instruction only inspects one of the first 3
210c8e92458SHiroshi Inoue     // bits, so when un is set, bc instruction may jump to an undesired
2115cdf7508STim Shen     // place.
2125cdf7508STim Shen     //
2135cdf7508STim Shen     // More specifically, if we expect an unordered comparison and un is set, we
2145cdf7508STim Shen     // expect to always go to true branch; in such case UEQ, UGT and ULT still
2155cdf7508STim Shen     // give false, which are undesired; but UNE, UGE, ULE happen to give true,
2165cdf7508STim Shen     // since they are tested by inspecting !eq, !lt, !gt, respectively.
2175cdf7508STim Shen     //
2185cdf7508STim Shen     // Similarly, for ordered comparison, when un is set, we always expect the
2195cdf7508STim Shen     // result to be false. In such case OGT, OLT and OEQ is good, since they are
2205cdf7508STim Shen     // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
2215cdf7508STim Shen     // and ONE are tested through !lt, !gt and !eq, and these are true.
2220300813dSBill Schmidt     case CmpInst::FCMP_UEQ:
2230300813dSBill Schmidt     case CmpInst::FCMP_UGT:
2240300813dSBill Schmidt     case CmpInst::FCMP_ULT:
2255cdf7508STim Shen     case CmpInst::FCMP_OGE:
2265cdf7508STim Shen     case CmpInst::FCMP_OLE:
2275cdf7508STim Shen     case CmpInst::FCMP_ONE:
2280300813dSBill Schmidt     default:
2290300813dSBill Schmidt       return Optional<PPC::Predicate>();
2300300813dSBill Schmidt 
2310300813dSBill Schmidt     case CmpInst::FCMP_OEQ:
2320300813dSBill Schmidt     case CmpInst::ICMP_EQ:
2330300813dSBill Schmidt       return PPC::PRED_EQ;
2340300813dSBill Schmidt 
2350300813dSBill Schmidt     case CmpInst::FCMP_OGT:
2360300813dSBill Schmidt     case CmpInst::ICMP_UGT:
2370300813dSBill Schmidt     case CmpInst::ICMP_SGT:
2380300813dSBill Schmidt       return PPC::PRED_GT;
2390300813dSBill Schmidt 
2405cdf7508STim Shen     case CmpInst::FCMP_UGE:
2410300813dSBill Schmidt     case CmpInst::ICMP_UGE:
2420300813dSBill Schmidt     case CmpInst::ICMP_SGE:
2430300813dSBill Schmidt       return PPC::PRED_GE;
2440300813dSBill Schmidt 
2450300813dSBill Schmidt     case CmpInst::FCMP_OLT:
2460300813dSBill Schmidt     case CmpInst::ICMP_ULT:
2470300813dSBill Schmidt     case CmpInst::ICMP_SLT:
2480300813dSBill Schmidt       return PPC::PRED_LT;
2490300813dSBill Schmidt 
2505cdf7508STim Shen     case CmpInst::FCMP_ULE:
2510300813dSBill Schmidt     case CmpInst::ICMP_ULE:
2520300813dSBill Schmidt     case CmpInst::ICMP_SLE:
2530300813dSBill Schmidt       return PPC::PRED_LE;
2540300813dSBill Schmidt 
2555cdf7508STim Shen     case CmpInst::FCMP_UNE:
2560300813dSBill Schmidt     case CmpInst::ICMP_NE:
2570300813dSBill Schmidt       return PPC::PRED_NE;
2580300813dSBill Schmidt 
2590300813dSBill Schmidt     case CmpInst::FCMP_ORD:
2600300813dSBill Schmidt       return PPC::PRED_NU;
2610300813dSBill Schmidt 
2620300813dSBill Schmidt     case CmpInst::FCMP_UNO:
2630300813dSBill Schmidt       return PPC::PRED_UN;
2640300813dSBill Schmidt   }
2650300813dSBill Schmidt }
2660300813dSBill Schmidt 
267ccecf261SBill Schmidt // Determine whether the type Ty is simple enough to be handled by
268ccecf261SBill Schmidt // fast-isel, and return its equivalent machine type in VT.
269ccecf261SBill Schmidt // FIXME: Copied directly from ARM -- factor into base class?
isTypeLegal(Type * Ty,MVT & VT)270ccecf261SBill Schmidt bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
27144ede33aSMehdi Amini   EVT Evt = TLI.getValueType(DL, Ty, true);
272ccecf261SBill Schmidt 
273ccecf261SBill Schmidt   // Only handle simple types.
274ccecf261SBill Schmidt   if (Evt == MVT::Other || !Evt.isSimple()) return false;
275ccecf261SBill Schmidt   VT = Evt.getSimpleVT();
276ccecf261SBill Schmidt 
277ccecf261SBill Schmidt   // Handle all legal types, i.e. a register that will directly hold this
278ccecf261SBill Schmidt   // value.
279ccecf261SBill Schmidt   return TLI.isTypeLegal(VT);
280ccecf261SBill Schmidt }
281ccecf261SBill Schmidt 
282ccecf261SBill Schmidt // Determine whether the type Ty is simple enough to be handled by
283ccecf261SBill Schmidt // fast-isel as a load target, and return its equivalent machine type in VT.
isLoadTypeLegal(Type * Ty,MVT & VT)284ccecf261SBill Schmidt bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
285ccecf261SBill Schmidt   if (isTypeLegal(Ty, VT)) return true;
286ccecf261SBill Schmidt 
287ccecf261SBill Schmidt   // If this is a type than can be sign or zero-extended to a basic operation
288ccecf261SBill Schmidt   // go ahead and accept it now.
289ccecf261SBill Schmidt   if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
290ccecf261SBill Schmidt     return true;
291ccecf261SBill Schmidt   }
292ccecf261SBill Schmidt 
293ccecf261SBill Schmidt   return false;
294ccecf261SBill Schmidt }
295ccecf261SBill Schmidt 
isValueAvailable(const Value * V) const2965f2a1379SHal Finkel bool PPCFastISel::isValueAvailable(const Value *V) const {
2975f2a1379SHal Finkel   if (!isa<Instruction>(V))
2985f2a1379SHal Finkel     return true;
2995f2a1379SHal Finkel 
3005f2a1379SHal Finkel   const auto *I = cast<Instruction>(V);
301175a7cbfSAlexander Kornienko   return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
3025f2a1379SHal Finkel }
3035f2a1379SHal Finkel 
304ccecf261SBill Schmidt // Given a value Obj, create an Address object Addr that represents its
305ccecf261SBill Schmidt // address.  Return false if we can't handle it.
PPCComputeAddress(const Value * Obj,Address & Addr)306ccecf261SBill Schmidt bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
307062a2baeSCraig Topper   const User *U = nullptr;
308ccecf261SBill Schmidt   unsigned Opcode = Instruction::UserOp1;
309ccecf261SBill Schmidt   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
310ccecf261SBill Schmidt     // Don't walk into other basic blocks unless the object is an alloca from
311ccecf261SBill Schmidt     // another block, otherwise it may not have a virtual register assigned.
312ccecf261SBill Schmidt     if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
313ccecf261SBill Schmidt         FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
314ccecf261SBill Schmidt       Opcode = I->getOpcode();
315ccecf261SBill Schmidt       U = I;
316ccecf261SBill Schmidt     }
317ccecf261SBill Schmidt   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
318ccecf261SBill Schmidt     Opcode = C->getOpcode();
319ccecf261SBill Schmidt     U = C;
320ccecf261SBill Schmidt   }
321ccecf261SBill Schmidt 
322ccecf261SBill Schmidt   switch (Opcode) {
323ccecf261SBill Schmidt     default:
324ccecf261SBill Schmidt       break;
325ccecf261SBill Schmidt     case Instruction::BitCast:
326ccecf261SBill Schmidt       // Look through bitcasts.
327ccecf261SBill Schmidt       return PPCComputeAddress(U->getOperand(0), Addr);
328ccecf261SBill Schmidt     case Instruction::IntToPtr:
329ccecf261SBill Schmidt       // Look past no-op inttoptrs.
33044ede33aSMehdi Amini       if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
33144ede33aSMehdi Amini           TLI.getPointerTy(DL))
332ccecf261SBill Schmidt         return PPCComputeAddress(U->getOperand(0), Addr);
333ccecf261SBill Schmidt       break;
334ccecf261SBill Schmidt     case Instruction::PtrToInt:
335ccecf261SBill Schmidt       // Look past no-op ptrtoints.
33644ede33aSMehdi Amini       if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
337ccecf261SBill Schmidt         return PPCComputeAddress(U->getOperand(0), Addr);
338ccecf261SBill Schmidt       break;
339ccecf261SBill Schmidt     case Instruction::GetElementPtr: {
340ccecf261SBill Schmidt       Address SavedAddr = Addr;
341ccecf261SBill Schmidt       long TmpOffset = Addr.Offset;
342ccecf261SBill Schmidt 
343ccecf261SBill Schmidt       // Iterate through the GEP folding the constants into offsets where
344ccecf261SBill Schmidt       // we can.
345ccecf261SBill Schmidt       gep_type_iterator GTI = gep_type_begin(U);
346ccecf261SBill Schmidt       for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
347ccecf261SBill Schmidt            II != IE; ++II, ++GTI) {
348ccecf261SBill Schmidt         const Value *Op = *II;
349ab85225bSPeter Collingbourne         if (StructType *STy = GTI.getStructTypeOrNull()) {
350ea09c595SRafael Espindola           const StructLayout *SL = DL.getStructLayout(STy);
351ccecf261SBill Schmidt           unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
352ccecf261SBill Schmidt           TmpOffset += SL->getElementOffset(Idx);
353ccecf261SBill Schmidt         } else {
354ea09c595SRafael Espindola           uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
355ccecf261SBill Schmidt           for (;;) {
356ccecf261SBill Schmidt             if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
357ccecf261SBill Schmidt               // Constant-offset addressing.
358ccecf261SBill Schmidt               TmpOffset += CI->getSExtValue() * S;
359ccecf261SBill Schmidt               break;
360ccecf261SBill Schmidt             }
3619f3e6b25SBob Wilson             if (canFoldAddIntoGEP(U, Op)) {
3629f3e6b25SBob Wilson               // A compatible add with a constant operand. Fold the constant.
363ccecf261SBill Schmidt               ConstantInt *CI =
364ccecf261SBill Schmidt               cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
365ccecf261SBill Schmidt               TmpOffset += CI->getSExtValue() * S;
366ccecf261SBill Schmidt               // Iterate on the other operand.
367ccecf261SBill Schmidt               Op = cast<AddOperator>(Op)->getOperand(0);
368ccecf261SBill Schmidt               continue;
369ccecf261SBill Schmidt             }
370ccecf261SBill Schmidt             // Unsupported
371ccecf261SBill Schmidt             goto unsupported_gep;
372ccecf261SBill Schmidt           }
373ccecf261SBill Schmidt         }
374ccecf261SBill Schmidt       }
375ccecf261SBill Schmidt 
376ccecf261SBill Schmidt       // Try to grab the base operand now.
377ccecf261SBill Schmidt       Addr.Offset = TmpOffset;
378ccecf261SBill Schmidt       if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
379ccecf261SBill Schmidt 
380ccecf261SBill Schmidt       // We failed, restore everything and try the other options.
381ccecf261SBill Schmidt       Addr = SavedAddr;
382ccecf261SBill Schmidt 
383ccecf261SBill Schmidt       unsupported_gep:
384ccecf261SBill Schmidt       break;
385ccecf261SBill Schmidt     }
386ccecf261SBill Schmidt     case Instruction::Alloca: {
387ccecf261SBill Schmidt       const AllocaInst *AI = cast<AllocaInst>(Obj);
388ccecf261SBill Schmidt       DenseMap<const AllocaInst*, int>::iterator SI =
389ccecf261SBill Schmidt         FuncInfo.StaticAllocaMap.find(AI);
390ccecf261SBill Schmidt       if (SI != FuncInfo.StaticAllocaMap.end()) {
391ccecf261SBill Schmidt         Addr.BaseType = Address::FrameIndexBase;
392ccecf261SBill Schmidt         Addr.Base.FI = SI->second;
393ccecf261SBill Schmidt         return true;
394ccecf261SBill Schmidt       }
395ccecf261SBill Schmidt       break;
396ccecf261SBill Schmidt     }
397ccecf261SBill Schmidt   }
398ccecf261SBill Schmidt 
399ccecf261SBill Schmidt   // FIXME: References to parameters fall through to the behavior
400ccecf261SBill Schmidt   // below.  They should be able to reference a frame index since
401ccecf261SBill Schmidt   // they are stored to the stack, so we can get "ld rx, offset(r1)"
402ccecf261SBill Schmidt   // instead of "addi ry, r1, offset / ld rx, 0(ry)".  Obj will
403ccecf261SBill Schmidt   // just contain the parameter.  Try to handle this with a FI.
404ccecf261SBill Schmidt 
405ccecf261SBill Schmidt   // Try to get this in a register if nothing else has worked.
406ccecf261SBill Schmidt   if (Addr.Base.Reg == 0)
407ccecf261SBill Schmidt     Addr.Base.Reg = getRegForValue(Obj);
408ccecf261SBill Schmidt 
409ccecf261SBill Schmidt   // Prevent assignment of base register to X0, which is inappropriate
410ccecf261SBill Schmidt   // for loads and stores alike.
411ccecf261SBill Schmidt   if (Addr.Base.Reg != 0)
412ccecf261SBill Schmidt     MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
413ccecf261SBill Schmidt 
414ccecf261SBill Schmidt   return Addr.Base.Reg != 0;
415ccecf261SBill Schmidt }
416ccecf261SBill Schmidt 
417ccecf261SBill Schmidt // Fix up some addresses that can't be used directly.  For example, if
418ccecf261SBill Schmidt // an offset won't fit in an instruction field, we may need to move it
419ccecf261SBill Schmidt // into an index register.
PPCSimplifyAddress(Address & Addr,bool & UseOffset,unsigned & IndexReg)4203707ba80SUlrich Weigand void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
421ccecf261SBill Schmidt                                      unsigned &IndexReg) {
422ccecf261SBill Schmidt 
423ccecf261SBill Schmidt   // Check whether the offset fits in the instruction field.
424ccecf261SBill Schmidt   if (!isInt<16>(Addr.Offset))
425ccecf261SBill Schmidt     UseOffset = false;
426ccecf261SBill Schmidt 
427ccecf261SBill Schmidt   // If this is a stack pointer and the offset needs to be simplified then
428ccecf261SBill Schmidt   // put the alloca address into a register, set the base type back to
429ccecf261SBill Schmidt   // register and continue. This should almost never happen.
430ccecf261SBill Schmidt   if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
431d6b07348SJim Lin     Register ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
432ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
433ccecf261SBill Schmidt             ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
434ccecf261SBill Schmidt     Addr.Base.Reg = ResultReg;
435ccecf261SBill Schmidt     Addr.BaseType = Address::RegBase;
436ccecf261SBill Schmidt   }
437ccecf261SBill Schmidt 
438ccecf261SBill Schmidt   if (!UseOffset) {
4393707ba80SUlrich Weigand     IntegerType *OffsetTy = Type::getInt64Ty(*Context);
440ccecf261SBill Schmidt     const ConstantInt *Offset =
441ccecf261SBill Schmidt       ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
442ccecf261SBill Schmidt     IndexReg = PPCMaterializeInt(Offset, MVT::i64);
443ccecf261SBill Schmidt     assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
444ccecf261SBill Schmidt   }
445ccecf261SBill Schmidt }
446ccecf261SBill Schmidt 
447ccecf261SBill Schmidt // Emit a load instruction if possible, returning true if we succeeded,
448ccecf261SBill Schmidt // otherwise false.  See commentary below for how the register class of
449ccecf261SBill Schmidt // the load is determined.
PPCEmitLoad(MVT VT,Register & ResultReg,Address & Addr,const TargetRegisterClass * RC,bool IsZExt,unsigned FP64LoadOpc)4500c476111SDaniel Sanders bool PPCFastISel::PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
451ccecf261SBill Schmidt                               const TargetRegisterClass *RC,
452ccecf261SBill Schmidt                               bool IsZExt, unsigned FP64LoadOpc) {
453ccecf261SBill Schmidt   unsigned Opc;
454ccecf261SBill Schmidt   bool UseOffset = true;
4555ca75130SKit Barton   bool HasSPE = Subtarget->hasSPE();
456ccecf261SBill Schmidt 
457ccecf261SBill Schmidt   // If ResultReg is given, it determines the register class of the load.
458ccecf261SBill Schmidt   // Otherwise, RC is the register class to use.  If the result of the
459ccecf261SBill Schmidt   // load isn't anticipated in this block, both may be zero, in which
460ccecf261SBill Schmidt   // case we must make a conservative guess.  In particular, don't assign
461ccecf261SBill Schmidt   // R0 or X0 to the result register, as the result may be used in a load,
462ccecf261SBill Schmidt   // store, add-immediate, or isel that won't permit this.  (Though
463ccecf261SBill Schmidt   // perhaps the spill and reload of live-exit values would handle this?)
464ccecf261SBill Schmidt   const TargetRegisterClass *UseRC =
465ccecf261SBill Schmidt     (ResultReg ? MRI.getRegClass(ResultReg) :
466ccecf261SBill Schmidt      (RC ? RC :
467d52990c7SJustin Hibbits       (VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :
46836e04d14SCraig Topper        (VT == MVT::f32 ? (HasSPE ? &PPC::GPRCRegClass : &PPC::F4RCRegClass) :
469ccecf261SBill Schmidt         (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
470ccecf261SBill Schmidt          &PPC::GPRC_and_GPRC_NOR0RegClass)))));
471ccecf261SBill Schmidt 
472ccecf261SBill Schmidt   bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
473ccecf261SBill Schmidt 
474ccecf261SBill Schmidt   switch (VT.SimpleTy) {
475ccecf261SBill Schmidt     default: // e.g., vector types not handled
476ccecf261SBill Schmidt       return false;
477ccecf261SBill Schmidt     case MVT::i8:
478ccecf261SBill Schmidt       Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
479ccecf261SBill Schmidt       break;
480ccecf261SBill Schmidt     case MVT::i16:
4819d0b5312SNAKAMURA Takumi       Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
4829d0b5312SNAKAMURA Takumi                     : (Is32BitInt ? PPC::LHA : PPC::LHA8));
483ccecf261SBill Schmidt       break;
484ccecf261SBill Schmidt     case MVT::i32:
4859d0b5312SNAKAMURA Takumi       Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
4869d0b5312SNAKAMURA Takumi                     : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
487ccecf261SBill Schmidt       if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
488ccecf261SBill Schmidt         UseOffset = false;
489ccecf261SBill Schmidt       break;
490ccecf261SBill Schmidt     case MVT::i64:
491ccecf261SBill Schmidt       Opc = PPC::LD;
492ccecf261SBill Schmidt       assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
493ccecf261SBill Schmidt              "64-bit load with 32-bit target??");
494ccecf261SBill Schmidt       UseOffset = ((Addr.Offset & 3) == 0);
495ccecf261SBill Schmidt       break;
496ccecf261SBill Schmidt     case MVT::f32:
4975ca75130SKit Barton       Opc = Subtarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
498ccecf261SBill Schmidt       break;
499ccecf261SBill Schmidt     case MVT::f64:
500ccecf261SBill Schmidt       Opc = FP64LoadOpc;
501ccecf261SBill Schmidt       break;
502ccecf261SBill Schmidt   }
503ccecf261SBill Schmidt 
504ccecf261SBill Schmidt   // If necessary, materialize the offset into a register and use
505ccecf261SBill Schmidt   // the indexed form.  Also handle stack pointers with special needs.
506ccecf261SBill Schmidt   unsigned IndexReg = 0;
5073707ba80SUlrich Weigand   PPCSimplifyAddress(Addr, UseOffset, IndexReg);
5088c728ae9SBill Seurer 
5098c728ae9SBill Seurer   // If this is a potential VSX load with an offset of 0, a VSX indexed load can
5108c728ae9SBill Seurer   // be used.
511c3b495a6SUlrich Weigand   bool IsVSSRC = isVSSRCRegClass(UseRC);
512c3b495a6SUlrich Weigand   bool IsVSFRC = isVSFRCRegClass(UseRC);
513376e1736SNemanja Ivanovic   bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
514c3b495a6SUlrich Weigand   bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
515376e1736SNemanja Ivanovic   if ((Is32VSXLoad || Is64VSXLoad) &&
5168c728ae9SBill Seurer       (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
5178c728ae9SBill Seurer       (Addr.Offset == 0)) {
5188c728ae9SBill Seurer     UseOffset = false;
5198c728ae9SBill Seurer   }
5208c728ae9SBill Seurer 
521ccecf261SBill Schmidt   if (ResultReg == 0)
522ccecf261SBill Schmidt     ResultReg = createResultReg(UseRC);
523ccecf261SBill Schmidt 
524ccecf261SBill Schmidt   // Note: If we still have a frame index here, we know the offset is
525ccecf261SBill Schmidt   // in range, as otherwise PPCSimplifyAddress would have converted it
526ccecf261SBill Schmidt   // into a RegBase.
527ccecf261SBill Schmidt   if (Addr.BaseType == Address::FrameIndexBase) {
5288c728ae9SBill Seurer     // VSX only provides an indexed load.
529376e1736SNemanja Ivanovic     if (Is32VSXLoad || Is64VSXLoad) return false;
530ccecf261SBill Schmidt 
531e40c8a2bSAlex Lorenz     MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
532e40c8a2bSAlex Lorenz         MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
533e40c8a2bSAlex Lorenz                                           Addr.Offset),
534ccecf261SBill Schmidt         MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
535c9d5c195SGuillaume Chatelet         MFI.getObjectAlign(Addr.Base.FI));
536ccecf261SBill Schmidt 
537ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
538ccecf261SBill Schmidt       .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
539ccecf261SBill Schmidt 
540ccecf261SBill Schmidt   // Base reg with offset in range.
541ccecf261SBill Schmidt   } else if (UseOffset) {
5428c728ae9SBill Seurer     // VSX only provides an indexed load.
543376e1736SNemanja Ivanovic     if (Is32VSXLoad || Is64VSXLoad) return false;
544ccecf261SBill Schmidt 
545ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
546ccecf261SBill Schmidt       .addImm(Addr.Offset).addReg(Addr.Base.Reg);
547ccecf261SBill Schmidt 
548ccecf261SBill Schmidt   // Indexed form.
549ccecf261SBill Schmidt   } else {
550ccecf261SBill Schmidt     // Get the RR opcode corresponding to the RI one.  FIXME: It would be
551ccecf261SBill Schmidt     // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
552ccecf261SBill Schmidt     // is hard to get at.
553ccecf261SBill Schmidt     switch (Opc) {
554ccecf261SBill Schmidt       default:        llvm_unreachable("Unexpected opcode!");
555ccecf261SBill Schmidt       case PPC::LBZ:    Opc = PPC::LBZX;    break;
556ccecf261SBill Schmidt       case PPC::LBZ8:   Opc = PPC::LBZX8;   break;
557ccecf261SBill Schmidt       case PPC::LHZ:    Opc = PPC::LHZX;    break;
558ccecf261SBill Schmidt       case PPC::LHZ8:   Opc = PPC::LHZX8;   break;
559ccecf261SBill Schmidt       case PPC::LHA:    Opc = PPC::LHAX;    break;
560ccecf261SBill Schmidt       case PPC::LHA8:   Opc = PPC::LHAX8;   break;
561ccecf261SBill Schmidt       case PPC::LWZ:    Opc = PPC::LWZX;    break;
562ccecf261SBill Schmidt       case PPC::LWZ8:   Opc = PPC::LWZX8;   break;
563ccecf261SBill Schmidt       case PPC::LWA:    Opc = PPC::LWAX;    break;
564ccecf261SBill Schmidt       case PPC::LWA_32: Opc = PPC::LWAX_32; break;
565ccecf261SBill Schmidt       case PPC::LD:     Opc = PPC::LDX;     break;
566376e1736SNemanja Ivanovic       case PPC::LFS:    Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
5678c728ae9SBill Seurer       case PPC::LFD:    Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
568d52990c7SJustin Hibbits       case PPC::EVLDD:  Opc = PPC::EVLDDX;  break;
569d52990c7SJustin Hibbits       case PPC::SPELWZ: Opc = PPC::SPELWZX;    break;
570ccecf261SBill Schmidt     }
571c3b495a6SUlrich Weigand 
57259a20649SNAKAMURA Takumi     auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
57359a20649SNAKAMURA Takumi                        ResultReg);
574c3b495a6SUlrich Weigand 
575c3b495a6SUlrich Weigand     // If we have an index register defined we use it in the store inst,
576c3b495a6SUlrich Weigand     // otherwise we use X0 as base as it makes the vector instructions to
577c3b495a6SUlrich Weigand     // use zero in the computation of the effective address regardless the
578c3b495a6SUlrich Weigand     // content of the register.
579c3b495a6SUlrich Weigand     if (IndexReg)
580c3b495a6SUlrich Weigand       MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
581c3b495a6SUlrich Weigand     else
582c3b495a6SUlrich Weigand       MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
583ccecf261SBill Schmidt   }
584ccecf261SBill Schmidt 
585ccecf261SBill Schmidt   return true;
586ccecf261SBill Schmidt }
587ccecf261SBill Schmidt 
588ccecf261SBill Schmidt // Attempt to fast-select a load instruction.
SelectLoad(const Instruction * I)589ccecf261SBill Schmidt bool PPCFastISel::SelectLoad(const Instruction *I) {
590ccecf261SBill Schmidt   // FIXME: No atomic loads are supported.
591ccecf261SBill Schmidt   if (cast<LoadInst>(I)->isAtomic())
592ccecf261SBill Schmidt     return false;
593ccecf261SBill Schmidt 
594ccecf261SBill Schmidt   // Verify we have a legal type before going any further.
595ccecf261SBill Schmidt   MVT VT;
596ccecf261SBill Schmidt   if (!isLoadTypeLegal(I->getType(), VT))
597ccecf261SBill Schmidt     return false;
598ccecf261SBill Schmidt 
599ccecf261SBill Schmidt   // See if we can handle this address.
600ccecf261SBill Schmidt   Address Addr;
601ccecf261SBill Schmidt   if (!PPCComputeAddress(I->getOperand(0), Addr))
602ccecf261SBill Schmidt     return false;
603ccecf261SBill Schmidt 
604ccecf261SBill Schmidt   // Look at the currently assigned register for this instruction
605ccecf261SBill Schmidt   // to determine the required register class.  This is necessary
606ccecf261SBill Schmidt   // to constrain RA from using R0/X0 when this is not legal.
607d6b07348SJim Lin   Register AssignedReg = FuncInfo.ValueMap[I];
608ccecf261SBill Schmidt   const TargetRegisterClass *RC =
609062a2baeSCraig Topper     AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
610ccecf261SBill Schmidt 
6110c476111SDaniel Sanders   Register ResultReg = 0;
612d52990c7SJustin Hibbits   if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
6135ca75130SKit Barton                    Subtarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
614ccecf261SBill Schmidt     return false;
6155b8bb4d7SJuergen Ributzka   updateValueMap(I, ResultReg);
616ccecf261SBill Schmidt   return true;
617ccecf261SBill Schmidt }
618ccecf261SBill Schmidt 
619ccecf261SBill Schmidt // Emit a store instruction to store SrcReg at Addr.
PPCEmitStore(MVT VT,unsigned SrcReg,Address & Addr)620ccecf261SBill Schmidt bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
621ccecf261SBill Schmidt   assert(SrcReg && "Nothing to store!");
622ccecf261SBill Schmidt   unsigned Opc;
623ccecf261SBill Schmidt   bool UseOffset = true;
624ccecf261SBill Schmidt 
625ccecf261SBill Schmidt   const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
626ccecf261SBill Schmidt   bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
627ccecf261SBill Schmidt 
628ccecf261SBill Schmidt   switch (VT.SimpleTy) {
629ccecf261SBill Schmidt     default: // e.g., vector types not handled
630ccecf261SBill Schmidt       return false;
631ccecf261SBill Schmidt     case MVT::i8:
632ccecf261SBill Schmidt       Opc = Is32BitInt ? PPC::STB : PPC::STB8;
633ccecf261SBill Schmidt       break;
634ccecf261SBill Schmidt     case MVT::i16:
635ccecf261SBill Schmidt       Opc = Is32BitInt ? PPC::STH : PPC::STH8;
636ccecf261SBill Schmidt       break;
637ccecf261SBill Schmidt     case MVT::i32:
638ccecf261SBill Schmidt       assert(Is32BitInt && "Not GPRC for i32??");
639ccecf261SBill Schmidt       Opc = PPC::STW;
640ccecf261SBill Schmidt       break;
641ccecf261SBill Schmidt     case MVT::i64:
642ccecf261SBill Schmidt       Opc = PPC::STD;
643ccecf261SBill Schmidt       UseOffset = ((Addr.Offset & 3) == 0);
644ccecf261SBill Schmidt       break;
645ccecf261SBill Schmidt     case MVT::f32:
6465ca75130SKit Barton       Opc = Subtarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
647ccecf261SBill Schmidt       break;
648ccecf261SBill Schmidt     case MVT::f64:
6495ca75130SKit Barton       Opc = Subtarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
650ccecf261SBill Schmidt       break;
651ccecf261SBill Schmidt   }
652ccecf261SBill Schmidt 
653ccecf261SBill Schmidt   // If necessary, materialize the offset into a register and use
654ccecf261SBill Schmidt   // the indexed form.  Also handle stack pointers with special needs.
655ccecf261SBill Schmidt   unsigned IndexReg = 0;
6563707ba80SUlrich Weigand   PPCSimplifyAddress(Addr, UseOffset, IndexReg);
657ccecf261SBill Schmidt 
6588c728ae9SBill Seurer   // If this is a potential VSX store with an offset of 0, a VSX indexed store
6598c728ae9SBill Seurer   // can be used.
660c3b495a6SUlrich Weigand   bool IsVSSRC = isVSSRCRegClass(RC);
661c3b495a6SUlrich Weigand   bool IsVSFRC = isVSFRCRegClass(RC);
662376e1736SNemanja Ivanovic   bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
663376e1736SNemanja Ivanovic   bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
664376e1736SNemanja Ivanovic   if ((Is32VSXStore || Is64VSXStore) &&
6658c728ae9SBill Seurer       (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
6668c728ae9SBill Seurer       (Addr.Offset == 0)) {
6678c728ae9SBill Seurer     UseOffset = false;
6688c728ae9SBill Seurer   }
6698c728ae9SBill Seurer 
670ccecf261SBill Schmidt   // Note: If we still have a frame index here, we know the offset is
671ccecf261SBill Schmidt   // in range, as otherwise PPCSimplifyAddress would have converted it
672ccecf261SBill Schmidt   // into a RegBase.
673ccecf261SBill Schmidt   if (Addr.BaseType == Address::FrameIndexBase) {
6748c728ae9SBill Seurer     // VSX only provides an indexed store.
675376e1736SNemanja Ivanovic     if (Is32VSXStore || Is64VSXStore) return false;
6768c728ae9SBill Seurer 
677e40c8a2bSAlex Lorenz     MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
678e40c8a2bSAlex Lorenz         MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
679e40c8a2bSAlex Lorenz                                           Addr.Offset),
680ccecf261SBill Schmidt         MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
681c9d5c195SGuillaume Chatelet         MFI.getObjectAlign(Addr.Base.FI));
682ccecf261SBill Schmidt 
683ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
684ea09c595SRafael Espindola         .addReg(SrcReg)
685ea09c595SRafael Espindola         .addImm(Addr.Offset)
686ea09c595SRafael Espindola         .addFrameIndex(Addr.Base.FI)
687ea09c595SRafael Espindola         .addMemOperand(MMO);
688ccecf261SBill Schmidt 
689ccecf261SBill Schmidt   // Base reg with offset in range.
6908c728ae9SBill Seurer   } else if (UseOffset) {
6918c728ae9SBill Seurer     // VSX only provides an indexed store.
6929d0b5312SNAKAMURA Takumi     if (Is32VSXStore || Is64VSXStore)
6939d0b5312SNAKAMURA Takumi       return false;
6948c728ae9SBill Seurer 
695ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
696ccecf261SBill Schmidt       .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
697ccecf261SBill Schmidt 
698ccecf261SBill Schmidt   // Indexed form.
6998c728ae9SBill Seurer   } else {
700ccecf261SBill Schmidt     // Get the RR opcode corresponding to the RI one.  FIXME: It would be
701ccecf261SBill Schmidt     // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
702ccecf261SBill Schmidt     // is hard to get at.
703ccecf261SBill Schmidt     switch (Opc) {
704ccecf261SBill Schmidt       default:        llvm_unreachable("Unexpected opcode!");
705ccecf261SBill Schmidt       case PPC::STB:  Opc = PPC::STBX;  break;
706ccecf261SBill Schmidt       case PPC::STH : Opc = PPC::STHX;  break;
707ccecf261SBill Schmidt       case PPC::STW : Opc = PPC::STWX;  break;
708ccecf261SBill Schmidt       case PPC::STB8: Opc = PPC::STBX8; break;
709ccecf261SBill Schmidt       case PPC::STH8: Opc = PPC::STHX8; break;
710ccecf261SBill Schmidt       case PPC::STW8: Opc = PPC::STWX8; break;
711ccecf261SBill Schmidt       case PPC::STD:  Opc = PPC::STDX;  break;
712376e1736SNemanja Ivanovic       case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
7138c728ae9SBill Seurer       case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
714d52990c7SJustin Hibbits       case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;
715d52990c7SJustin Hibbits       case PPC::SPESTW: Opc = PPC::SPESTWX; break;
716ccecf261SBill Schmidt     }
717f6815601SSamuel Antao 
718f6815601SSamuel Antao     auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
719f6815601SSamuel Antao         .addReg(SrcReg);
720f6815601SSamuel Antao 
721f6815601SSamuel Antao     // If we have an index register defined we use it in the store inst,
722f6815601SSamuel Antao     // otherwise we use X0 as base as it makes the vector instructions to
723f6815601SSamuel Antao     // use zero in the computation of the effective address regardless the
724f6815601SSamuel Antao     // content of the register.
725f6815601SSamuel Antao     if (IndexReg)
726f6815601SSamuel Antao       MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
727f6815601SSamuel Antao     else
728f6815601SSamuel Antao       MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
729ccecf261SBill Schmidt   }
730ccecf261SBill Schmidt 
731ccecf261SBill Schmidt   return true;
732ccecf261SBill Schmidt }
733ccecf261SBill Schmidt 
734ccecf261SBill Schmidt // Attempt to fast-select a store instruction.
SelectStore(const Instruction * I)735ccecf261SBill Schmidt bool PPCFastISel::SelectStore(const Instruction *I) {
736ccecf261SBill Schmidt   Value *Op0 = I->getOperand(0);
737ccecf261SBill Schmidt   unsigned SrcReg = 0;
738ccecf261SBill Schmidt 
739ccecf261SBill Schmidt   // FIXME: No atomics loads are supported.
740ccecf261SBill Schmidt   if (cast<StoreInst>(I)->isAtomic())
741ccecf261SBill Schmidt     return false;
742ccecf261SBill Schmidt 
743ccecf261SBill Schmidt   // Verify we have a legal type before going any further.
744ccecf261SBill Schmidt   MVT VT;
745ccecf261SBill Schmidt   if (!isLoadTypeLegal(Op0->getType(), VT))
746ccecf261SBill Schmidt     return false;
747ccecf261SBill Schmidt 
748ccecf261SBill Schmidt   // Get the value to be stored into a register.
749ccecf261SBill Schmidt   SrcReg = getRegForValue(Op0);
750ccecf261SBill Schmidt   if (SrcReg == 0)
751ccecf261SBill Schmidt     return false;
752ccecf261SBill Schmidt 
753ccecf261SBill Schmidt   // See if we can handle this address.
754ccecf261SBill Schmidt   Address Addr;
755ccecf261SBill Schmidt   if (!PPCComputeAddress(I->getOperand(1), Addr))
756ccecf261SBill Schmidt     return false;
757ccecf261SBill Schmidt 
758ccecf261SBill Schmidt   if (!PPCEmitStore(VT, SrcReg, Addr))
759ccecf261SBill Schmidt     return false;
760ccecf261SBill Schmidt 
761ccecf261SBill Schmidt   return true;
762ccecf261SBill Schmidt }
763ccecf261SBill Schmidt 
7640300813dSBill Schmidt // Attempt to fast-select a branch instruction.
SelectBranch(const Instruction * I)7650300813dSBill Schmidt bool PPCFastISel::SelectBranch(const Instruction *I) {
7660300813dSBill Schmidt   const BranchInst *BI = cast<BranchInst>(I);
7670300813dSBill Schmidt   MachineBasicBlock *BrBB = FuncInfo.MBB;
7680300813dSBill Schmidt   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
7690300813dSBill Schmidt   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
7700300813dSBill Schmidt 
7710300813dSBill Schmidt   // For now, just try the simplest case where it's fed by a compare.
7720300813dSBill Schmidt   if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
7735f2a1379SHal Finkel     if (isValueAvailable(CI)) {
7740300813dSBill Schmidt       Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
7750300813dSBill Schmidt       if (!OptPPCPred)
7760300813dSBill Schmidt         return false;
7770300813dSBill Schmidt 
7787a47ee51SKazu Hirata       PPC::Predicate PPCPred = *OptPPCPred;
7790300813dSBill Schmidt 
7800300813dSBill Schmidt       // Take advantage of fall-through opportunities.
7810300813dSBill Schmidt       if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
7820300813dSBill Schmidt         std::swap(TBB, FBB);
7830300813dSBill Schmidt         PPCPred = PPC::InvertPredicate(PPCPred);
7840300813dSBill Schmidt       }
7850300813dSBill Schmidt 
786d6b07348SJim Lin       Register CondReg = createResultReg(&PPC::CRRCRegClass);
7870300813dSBill Schmidt 
7880300813dSBill Schmidt       if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
789d52990c7SJustin Hibbits                       CondReg, PPCPred))
7900300813dSBill Schmidt         return false;
7910300813dSBill Schmidt 
792ea09c595SRafael Espindola       BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
7935ca75130SKit Barton           .addImm(Subtarget->hasSPE() ? PPC::PRED_SPE : PPCPred)
7945ca75130SKit Barton           .addReg(CondReg)
7955ca75130SKit Barton           .addMBB(TBB);
796ccfc9c8dSMatthias Braun       finishCondBranch(BI->getParent(), TBB, FBB);
7970300813dSBill Schmidt       return true;
7985f2a1379SHal Finkel     }
7990300813dSBill Schmidt   } else if (const ConstantInt *CI =
8000300813dSBill Schmidt              dyn_cast<ConstantInt>(BI->getCondition())) {
8010300813dSBill Schmidt     uint64_t Imm = CI->getZExtValue();
8020300813dSBill Schmidt     MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
8035b8bb4d7SJuergen Ributzka     fastEmitBranch(Target, DbgLoc);
8040300813dSBill Schmidt     return true;
8050300813dSBill Schmidt   }
8060300813dSBill Schmidt 
8070300813dSBill Schmidt   // FIXME: ARM looks for a case where the block containing the compare
8080300813dSBill Schmidt   // has been split from the block containing the branch.  If this happens,
8090300813dSBill Schmidt   // there is a vreg available containing the result of the compare.  I'm
8100300813dSBill Schmidt   // not sure we can do much, as we've lost the predicate information with
8110300813dSBill Schmidt   // the compare instruction -- we have a 4-bit CR but don't know which bit
8120300813dSBill Schmidt   // to test here.
8130300813dSBill Schmidt   return false;
8140300813dSBill Schmidt }
8150300813dSBill Schmidt 
8160300813dSBill Schmidt // Attempt to emit a compare of the two source values.  Signed and unsigned
8170300813dSBill Schmidt // comparisons are supported.  Return false if we can't handle it.
PPCEmitCmp(const Value * SrcValue1,const Value * SrcValue2,bool IsZExt,unsigned DestReg,const PPC::Predicate Pred)8180300813dSBill Schmidt bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
819d52990c7SJustin Hibbits                              bool IsZExt, unsigned DestReg,
820d52990c7SJustin Hibbits                              const PPC::Predicate Pred) {
8210300813dSBill Schmidt   Type *Ty = SrcValue1->getType();
82244ede33aSMehdi Amini   EVT SrcEVT = TLI.getValueType(DL, Ty, true);
8230300813dSBill Schmidt   if (!SrcEVT.isSimple())
8240300813dSBill Schmidt     return false;
8250300813dSBill Schmidt   MVT SrcVT = SrcEVT.getSimpleVT();
8260300813dSBill Schmidt 
8275ca75130SKit Barton   if (SrcVT == MVT::i1 && Subtarget->useCRBits())
828940ab934SHal Finkel     return false;
829940ab934SHal Finkel 
8300300813dSBill Schmidt   // See if operand 2 is an immediate encodeable in the compare.
8310300813dSBill Schmidt   // FIXME: Operands are not in canonical order at -O0, so an immediate
8320300813dSBill Schmidt   // operand in position 1 is a lost opportunity for now.  We are
8330300813dSBill Schmidt   // similar to ARM in this regard.
834*01548868SUmesh Kalappa   int64_t Imm = 0;
8350300813dSBill Schmidt   bool UseImm = false;
8365ca75130SKit Barton   const bool HasSPE = Subtarget->hasSPE();
8370300813dSBill Schmidt 
8380300813dSBill Schmidt   // Only 16-bit integer constants can be represented in compares for
8390300813dSBill Schmidt   // PowerPC.  Others will be materialized into a register.
8400300813dSBill Schmidt   if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
8410300813dSBill Schmidt     if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
8420300813dSBill Schmidt         SrcVT == MVT::i8 || SrcVT == MVT::i1) {
8430300813dSBill Schmidt       const APInt &CIVal = ConstInt->getValue();
844*01548868SUmesh Kalappa       Imm = (IsZExt) ? (int64_t)CIVal.getZExtValue() :
845*01548868SUmesh Kalappa                        (int64_t)CIVal.getSExtValue();
8460300813dSBill Schmidt       if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
8470300813dSBill Schmidt         UseImm = true;
8480300813dSBill Schmidt     }
8490300813dSBill Schmidt   }
8500300813dSBill Schmidt 
851d6b07348SJim Lin   Register SrcReg1 = getRegForValue(SrcValue1);
85264c956eeSZi Xuan Wu   if (SrcReg1 == 0)
85364c956eeSZi Xuan Wu     return false;
85464c956eeSZi Xuan Wu 
85564c956eeSZi Xuan Wu   unsigned SrcReg2 = 0;
85664c956eeSZi Xuan Wu   if (!UseImm) {
85764c956eeSZi Xuan Wu     SrcReg2 = getRegForValue(SrcValue2);
85864c956eeSZi Xuan Wu     if (SrcReg2 == 0)
85964c956eeSZi Xuan Wu       return false;
86064c956eeSZi Xuan Wu   }
86164c956eeSZi Xuan Wu 
8620300813dSBill Schmidt   unsigned CmpOpc;
8630300813dSBill Schmidt   bool NeedsExt = false;
864308a609cSZi Xuan Wu 
865308a609cSZi Xuan Wu   auto RC1 = MRI.getRegClass(SrcReg1);
866308a609cSZi Xuan Wu   auto RC2 = SrcReg2 != 0 ? MRI.getRegClass(SrcReg2) : nullptr;
867308a609cSZi Xuan Wu 
8680300813dSBill Schmidt   switch (SrcVT.SimpleTy) {
8690300813dSBill Schmidt     default: return false;
8700300813dSBill Schmidt     case MVT::f32:
871d52990c7SJustin Hibbits       if (HasSPE) {
872d52990c7SJustin Hibbits         switch (Pred) {
873d52990c7SJustin Hibbits           default: return false;
874d52990c7SJustin Hibbits           case PPC::PRED_EQ:
875d52990c7SJustin Hibbits             CmpOpc = PPC::EFSCMPEQ;
876d52990c7SJustin Hibbits             break;
877d52990c7SJustin Hibbits           case PPC::PRED_LT:
878d52990c7SJustin Hibbits             CmpOpc = PPC::EFSCMPLT;
879d52990c7SJustin Hibbits             break;
880d52990c7SJustin Hibbits           case PPC::PRED_GT:
881d52990c7SJustin Hibbits             CmpOpc = PPC::EFSCMPGT;
882d52990c7SJustin Hibbits             break;
883d52990c7SJustin Hibbits         }
88464c956eeSZi Xuan Wu       } else {
8850300813dSBill Schmidt         CmpOpc = PPC::FCMPUS;
886fec749ffSZi Xuan Wu         if (isVSSRCRegClass(RC1))
887fec749ffSZi Xuan Wu           SrcReg1 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg1);
888fec749ffSZi Xuan Wu         if (RC2 && isVSSRCRegClass(RC2))
889fec749ffSZi Xuan Wu           SrcReg2 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg2);
89064c956eeSZi Xuan Wu       }
8910300813dSBill Schmidt       break;
8920300813dSBill Schmidt     case MVT::f64:
893d52990c7SJustin Hibbits       if (HasSPE) {
894d52990c7SJustin Hibbits         switch (Pred) {
895d52990c7SJustin Hibbits           default: return false;
896d52990c7SJustin Hibbits           case PPC::PRED_EQ:
897d52990c7SJustin Hibbits             CmpOpc = PPC::EFDCMPEQ;
898d52990c7SJustin Hibbits             break;
899d52990c7SJustin Hibbits           case PPC::PRED_LT:
900d52990c7SJustin Hibbits             CmpOpc = PPC::EFDCMPLT;
901d52990c7SJustin Hibbits             break;
902d52990c7SJustin Hibbits           case PPC::PRED_GT:
903d52990c7SJustin Hibbits             CmpOpc = PPC::EFDCMPGT;
904d52990c7SJustin Hibbits             break;
905d52990c7SJustin Hibbits         }
906308a609cSZi Xuan Wu       } else if (isVSFRCRegClass(RC1) || (RC2 && isVSFRCRegClass(RC2))) {
90764c956eeSZi Xuan Wu         CmpOpc = PPC::XSCMPUDP;
90864c956eeSZi Xuan Wu       } else {
9090300813dSBill Schmidt         CmpOpc = PPC::FCMPUD;
91064c956eeSZi Xuan Wu       }
9110300813dSBill Schmidt       break;
9120300813dSBill Schmidt     case MVT::i1:
9130300813dSBill Schmidt     case MVT::i8:
9140300813dSBill Schmidt     case MVT::i16:
9150300813dSBill Schmidt       NeedsExt = true;
9164dc0b1acSReid Kleckner       LLVM_FALLTHROUGH;
9170300813dSBill Schmidt     case MVT::i32:
9180300813dSBill Schmidt       if (!UseImm)
9190300813dSBill Schmidt         CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
9200300813dSBill Schmidt       else
9210300813dSBill Schmidt         CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
9220300813dSBill Schmidt       break;
9230300813dSBill Schmidt     case MVT::i64:
9240300813dSBill Schmidt       if (!UseImm)
9250300813dSBill Schmidt         CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
9260300813dSBill Schmidt       else
9270300813dSBill Schmidt         CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
9280300813dSBill Schmidt       break;
9290300813dSBill Schmidt   }
9300300813dSBill Schmidt 
9310300813dSBill Schmidt   if (NeedsExt) {
932d6b07348SJim Lin     Register ExtReg = createResultReg(&PPC::GPRCRegClass);
9330300813dSBill Schmidt     if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
9340300813dSBill Schmidt       return false;
9350300813dSBill Schmidt     SrcReg1 = ExtReg;
9360300813dSBill Schmidt 
9370300813dSBill Schmidt     if (!UseImm) {
938d6b07348SJim Lin       Register ExtReg = createResultReg(&PPC::GPRCRegClass);
9390300813dSBill Schmidt       if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
9400300813dSBill Schmidt         return false;
9410300813dSBill Schmidt       SrcReg2 = ExtReg;
9420300813dSBill Schmidt     }
9430300813dSBill Schmidt   }
9440300813dSBill Schmidt 
9450300813dSBill Schmidt   if (!UseImm)
946ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
9470300813dSBill Schmidt       .addReg(SrcReg1).addReg(SrcReg2);
9480300813dSBill Schmidt   else
949ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
9500300813dSBill Schmidt       .addReg(SrcReg1).addImm(Imm);
9510300813dSBill Schmidt 
9520300813dSBill Schmidt   return true;
9530300813dSBill Schmidt }
9540300813dSBill Schmidt 
9558d86fe7dSBill Schmidt // Attempt to fast-select a floating-point extend instruction.
SelectFPExt(const Instruction * I)9568d86fe7dSBill Schmidt bool PPCFastISel::SelectFPExt(const Instruction *I) {
9578d86fe7dSBill Schmidt   Value *Src  = I->getOperand(0);
95844ede33aSMehdi Amini   EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
95944ede33aSMehdi Amini   EVT DestVT = TLI.getValueType(DL, I->getType(), true);
9608d86fe7dSBill Schmidt 
9618d86fe7dSBill Schmidt   if (SrcVT != MVT::f32 || DestVT != MVT::f64)
9628d86fe7dSBill Schmidt     return false;
9638d86fe7dSBill Schmidt 
964d6b07348SJim Lin   Register SrcReg = getRegForValue(Src);
9658d86fe7dSBill Schmidt   if (!SrcReg)
9668d86fe7dSBill Schmidt     return false;
9678d86fe7dSBill Schmidt 
9688d86fe7dSBill Schmidt   // No code is generated for a FP extend.
9695b8bb4d7SJuergen Ributzka   updateValueMap(I, SrcReg);
9708d86fe7dSBill Schmidt   return true;
9718d86fe7dSBill Schmidt }
9728d86fe7dSBill Schmidt 
9738d86fe7dSBill Schmidt // Attempt to fast-select a floating-point truncate instruction.
SelectFPTrunc(const Instruction * I)9748d86fe7dSBill Schmidt bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
9758d86fe7dSBill Schmidt   Value *Src  = I->getOperand(0);
97644ede33aSMehdi Amini   EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
97744ede33aSMehdi Amini   EVT DestVT = TLI.getValueType(DL, I->getType(), true);
9788d86fe7dSBill Schmidt 
9798d86fe7dSBill Schmidt   if (SrcVT != MVT::f64 || DestVT != MVT::f32)
9808d86fe7dSBill Schmidt     return false;
9818d86fe7dSBill Schmidt 
982d6b07348SJim Lin   Register SrcReg = getRegForValue(Src);
9838d86fe7dSBill Schmidt   if (!SrcReg)
9848d86fe7dSBill Schmidt     return false;
9858d86fe7dSBill Schmidt 
9868d86fe7dSBill Schmidt   // Round the result to single precision.
987d52990c7SJustin Hibbits   unsigned DestReg;
9884faa4090SKang Zhang   auto RC = MRI.getRegClass(SrcReg);
9895ca75130SKit Barton   if (Subtarget->hasSPE()) {
99036e04d14SCraig Topper     DestReg = createResultReg(&PPC::GPRCRegClass);
991042a6564SJinsong Ji     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::EFSCFD),
992042a6564SJinsong Ji             DestReg)
9938d86fe7dSBill Schmidt         .addReg(SrcReg);
994042a6564SJinsong Ji   } else if (Subtarget->hasP8Vector() && isVSFRCRegClass(RC)) {
9954faa4090SKang Zhang     DestReg = createResultReg(&PPC::VSSRCRegClass);
996042a6564SJinsong Ji     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::XSRSP),
997042a6564SJinsong Ji             DestReg)
9984faa4090SKang Zhang         .addReg(SrcReg);
999d52990c7SJustin Hibbits   } else {
1000042a6564SJinsong Ji     SrcReg = copyRegToRegClass(&PPC::F8RCRegClass, SrcReg);
1001d52990c7SJustin Hibbits     DestReg = createResultReg(&PPC::F4RCRegClass);
1002d52990c7SJustin Hibbits     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1003d52990c7SJustin Hibbits       TII.get(PPC::FRSP), DestReg)
1004d52990c7SJustin Hibbits       .addReg(SrcReg);
1005d52990c7SJustin Hibbits   }
10068d86fe7dSBill Schmidt 
10075b8bb4d7SJuergen Ributzka   updateValueMap(I, DestReg);
10088d86fe7dSBill Schmidt   return true;
10098d86fe7dSBill Schmidt }
10108d86fe7dSBill Schmidt 
10118d86fe7dSBill Schmidt // Move an i32 or i64 value in a GPR to an f64 value in an FPR.
10121194b8fdSSamuel Antao // FIXME: When direct register moves are implemented (see PowerISA 2.07),
10138d86fe7dSBill Schmidt // those should be used instead of moving via a stack slot when the
10148d86fe7dSBill Schmidt // subtarget permits.
10158d86fe7dSBill Schmidt // FIXME: The code here is sloppy for the 4-byte case.  Can use a 4-byte
10168d86fe7dSBill Schmidt // stack slot and 4-byte store/load sequence.  Or just sext the 4-byte
10178d86fe7dSBill Schmidt // case to 8 bytes which produces tighter code but wastes stack space.
PPCMoveToFPReg(MVT SrcVT,unsigned SrcReg,bool IsSigned)10188d86fe7dSBill Schmidt unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
10198d86fe7dSBill Schmidt                                      bool IsSigned) {
10208d86fe7dSBill Schmidt 
10218d86fe7dSBill Schmidt   // If necessary, extend 32-bit int to 64-bit.
10228d86fe7dSBill Schmidt   if (SrcVT == MVT::i32) {
1023d6b07348SJim Lin     Register TmpReg = createResultReg(&PPC::G8RCRegClass);
10248d86fe7dSBill Schmidt     if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
10258d86fe7dSBill Schmidt       return 0;
10268d86fe7dSBill Schmidt     SrcReg = TmpReg;
10278d86fe7dSBill Schmidt   }
10288d86fe7dSBill Schmidt 
10298d86fe7dSBill Schmidt   // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
10308d86fe7dSBill Schmidt   Address Addr;
10318d86fe7dSBill Schmidt   Addr.BaseType = Address::FrameIndexBase;
103228de229bSGuillaume Chatelet   Addr.Base.FI = MFI.CreateStackObject(8, Align(8), false);
10338d86fe7dSBill Schmidt 
10348d86fe7dSBill Schmidt   // Store the value from the GPR.
10358d86fe7dSBill Schmidt   if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
10368d86fe7dSBill Schmidt     return 0;
10378d86fe7dSBill Schmidt 
10388d86fe7dSBill Schmidt   // Load the integer value into an FPR.  The kind of load used depends
10398d86fe7dSBill Schmidt   // on a number of conditions.
10408d86fe7dSBill Schmidt   unsigned LoadOpc = PPC::LFD;
10418d86fe7dSBill Schmidt 
10428d86fe7dSBill Schmidt   if (SrcVT == MVT::i32) {
1043ff9622efSBill Schmidt     if (!IsSigned) {
10448d86fe7dSBill Schmidt       LoadOpc = PPC::LFIWZX;
10455ca75130SKit Barton       Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
10465ca75130SKit Barton     } else if (Subtarget->hasLFIWAX()) {
10478d86fe7dSBill Schmidt       LoadOpc = PPC::LFIWAX;
10485ca75130SKit Barton       Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
1049ff9622efSBill Schmidt     }
10508d86fe7dSBill Schmidt   }
10518d86fe7dSBill Schmidt 
10528d86fe7dSBill Schmidt   const TargetRegisterClass *RC = &PPC::F8RCRegClass;
10530c476111SDaniel Sanders   Register ResultReg = 0;
10548d86fe7dSBill Schmidt   if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
10558d86fe7dSBill Schmidt     return 0;
10568d86fe7dSBill Schmidt 
10578d86fe7dSBill Schmidt   return ResultReg;
10588d86fe7dSBill Schmidt }
10598d86fe7dSBill Schmidt 
10608d86fe7dSBill Schmidt // Attempt to fast-select an integer-to-floating-point conversion.
1061c38b5311SNemanja Ivanovic // FIXME: Once fast-isel has better support for VSX, conversions using
1062c38b5311SNemanja Ivanovic //        direct moves should be implemented.
SelectIToFP(const Instruction * I,bool IsSigned)10638d86fe7dSBill Schmidt bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
10648d86fe7dSBill Schmidt   MVT DstVT;
10658d86fe7dSBill Schmidt   Type *DstTy = I->getType();
10668d86fe7dSBill Schmidt   if (!isTypeLegal(DstTy, DstVT))
10678d86fe7dSBill Schmidt     return false;
10688d86fe7dSBill Schmidt 
10698d86fe7dSBill Schmidt   if (DstVT != MVT::f32 && DstVT != MVT::f64)
10708d86fe7dSBill Schmidt     return false;
10718d86fe7dSBill Schmidt 
10728d86fe7dSBill Schmidt   Value *Src = I->getOperand(0);
107344ede33aSMehdi Amini   EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
10748d86fe7dSBill Schmidt   if (!SrcEVT.isSimple())
10758d86fe7dSBill Schmidt     return false;
10768d86fe7dSBill Schmidt 
10778d86fe7dSBill Schmidt   MVT SrcVT = SrcEVT.getSimpleVT();
10788d86fe7dSBill Schmidt 
10798d86fe7dSBill Schmidt   if (SrcVT != MVT::i8  && SrcVT != MVT::i16 &&
10808d86fe7dSBill Schmidt       SrcVT != MVT::i32 && SrcVT != MVT::i64)
10818d86fe7dSBill Schmidt     return false;
10828d86fe7dSBill Schmidt 
1083d6b07348SJim Lin   Register SrcReg = getRegForValue(Src);
10848d86fe7dSBill Schmidt   if (SrcReg == 0)
10858d86fe7dSBill Schmidt     return false;
10868d86fe7dSBill Schmidt 
1087d52990c7SJustin Hibbits   // Shortcut for SPE.  Doesn't need to store/load, since it's all in the GPRs
10885ca75130SKit Barton   if (Subtarget->hasSPE()) {
1089d52990c7SJustin Hibbits     unsigned Opc;
1090d52990c7SJustin Hibbits     if (DstVT == MVT::f32)
1091d52990c7SJustin Hibbits       Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
1092d52990c7SJustin Hibbits     else
1093d52990c7SJustin Hibbits       Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
1094d52990c7SJustin Hibbits 
1095d6b07348SJim Lin     Register DestReg = createResultReg(&PPC::SPERCRegClass);
1096d52990c7SJustin Hibbits     // Generate the convert.
1097d52990c7SJustin Hibbits     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1098d52990c7SJustin Hibbits       .addReg(SrcReg);
1099d52990c7SJustin Hibbits     updateValueMap(I, DestReg);
1100d52990c7SJustin Hibbits     return true;
1101d52990c7SJustin Hibbits   }
1102d52990c7SJustin Hibbits 
11038d86fe7dSBill Schmidt   // We can only lower an unsigned convert if we have the newer
11048d86fe7dSBill Schmidt   // floating-point conversion operations.
11055ca75130SKit Barton   if (!IsSigned && !Subtarget->hasFPCVT())
11068d86fe7dSBill Schmidt     return false;
11078d86fe7dSBill Schmidt 
11088d86fe7dSBill Schmidt   // FIXME: For now we require the newer floating-point conversion operations
11098d86fe7dSBill Schmidt   // (which are present only on P7 and A2 server models) when converting
11108d86fe7dSBill Schmidt   // to single-precision float.  Otherwise we have to generate a lot of
11118d86fe7dSBill Schmidt   // fiddly code to avoid double rounding.  If necessary, the fiddly code
11128d86fe7dSBill Schmidt   // can be found in PPCTargetLowering::LowerINT_TO_FP().
11135ca75130SKit Barton   if (DstVT == MVT::f32 && !Subtarget->hasFPCVT())
11148d86fe7dSBill Schmidt     return false;
11158d86fe7dSBill Schmidt 
11168d86fe7dSBill Schmidt   // Extend the input if necessary.
11178d86fe7dSBill Schmidt   if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
1118d6b07348SJim Lin     Register TmpReg = createResultReg(&PPC::G8RCRegClass);
11198d86fe7dSBill Schmidt     if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
11208d86fe7dSBill Schmidt       return false;
11218d86fe7dSBill Schmidt     SrcVT = MVT::i64;
11228d86fe7dSBill Schmidt     SrcReg = TmpReg;
11238d86fe7dSBill Schmidt   }
11248d86fe7dSBill Schmidt 
11258d86fe7dSBill Schmidt   // Move the integer value to an FPR.
11268d86fe7dSBill Schmidt   unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
11278d86fe7dSBill Schmidt   if (FPReg == 0)
11288d86fe7dSBill Schmidt     return false;
11298d86fe7dSBill Schmidt 
11308d86fe7dSBill Schmidt   // Determine the opcode for the conversion.
11318d86fe7dSBill Schmidt   const TargetRegisterClass *RC = &PPC::F8RCRegClass;
1132d6b07348SJim Lin   Register DestReg = createResultReg(RC);
11338d86fe7dSBill Schmidt   unsigned Opc;
11348d86fe7dSBill Schmidt 
11358d86fe7dSBill Schmidt   if (DstVT == MVT::f32)
11368d86fe7dSBill Schmidt     Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
11378d86fe7dSBill Schmidt   else
11388d86fe7dSBill Schmidt     Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
11398d86fe7dSBill Schmidt 
11408d86fe7dSBill Schmidt   // Generate the convert.
1141ea09c595SRafael Espindola   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
11428d86fe7dSBill Schmidt     .addReg(FPReg);
11438d86fe7dSBill Schmidt 
11445b8bb4d7SJuergen Ributzka   updateValueMap(I, DestReg);
11458d86fe7dSBill Schmidt   return true;
11468d86fe7dSBill Schmidt }
11478d86fe7dSBill Schmidt 
11488d86fe7dSBill Schmidt // Move the floating-point value in SrcReg into an integer destination
11498d86fe7dSBill Schmidt // register, and return the register (or zero if we can't handle it).
11501194b8fdSSamuel Antao // FIXME: When direct register moves are implemented (see PowerISA 2.07),
11518d86fe7dSBill Schmidt // those should be used instead of moving via a stack slot when the
11528d86fe7dSBill Schmidt // subtarget permits.
PPCMoveToIntReg(const Instruction * I,MVT VT,unsigned SrcReg,bool IsSigned)11538d86fe7dSBill Schmidt unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
11548d86fe7dSBill Schmidt                                       unsigned SrcReg, bool IsSigned) {
11558d86fe7dSBill Schmidt   // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
11568d86fe7dSBill Schmidt   // Note that if have STFIWX available, we could use a 4-byte stack
11578d86fe7dSBill Schmidt   // slot for i32, but this being fast-isel we'll just go with the
11588d86fe7dSBill Schmidt   // easiest code gen possible.
11598d86fe7dSBill Schmidt   Address Addr;
11608d86fe7dSBill Schmidt   Addr.BaseType = Address::FrameIndexBase;
116128de229bSGuillaume Chatelet   Addr.Base.FI = MFI.CreateStackObject(8, Align(8), false);
11628d86fe7dSBill Schmidt 
11638d86fe7dSBill Schmidt   // Store the value from the FPR.
11648d86fe7dSBill Schmidt   if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
11658d86fe7dSBill Schmidt     return 0;
11668d86fe7dSBill Schmidt 
11671a5706caSNemanja Ivanovic   // Reload it into a GPR.  If we want an i32 on big endian, modify the
11681a5706caSNemanja Ivanovic   // address to have a 4-byte offset so we load from the right place.
11698d86fe7dSBill Schmidt   if (VT == MVT::i32)
11705ca75130SKit Barton     Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
11718d86fe7dSBill Schmidt 
11728d86fe7dSBill Schmidt   // Look at the currently assigned register for this instruction
11738d86fe7dSBill Schmidt   // to determine the required register class.
1174d6b07348SJim Lin   Register AssignedReg = FuncInfo.ValueMap[I];
11758d86fe7dSBill Schmidt   const TargetRegisterClass *RC =
1176062a2baeSCraig Topper     AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
11778d86fe7dSBill Schmidt 
11780c476111SDaniel Sanders   Register ResultReg = 0;
11798d86fe7dSBill Schmidt   if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
11808d86fe7dSBill Schmidt     return 0;
11818d86fe7dSBill Schmidt 
11828d86fe7dSBill Schmidt   return ResultReg;
11838d86fe7dSBill Schmidt }
11848d86fe7dSBill Schmidt 
11858d86fe7dSBill Schmidt // Attempt to fast-select a floating-point-to-integer conversion.
1186c38b5311SNemanja Ivanovic // FIXME: Once fast-isel has better support for VSX, conversions using
1187c38b5311SNemanja Ivanovic //        direct moves should be implemented.
SelectFPToI(const Instruction * I,bool IsSigned)11888d86fe7dSBill Schmidt bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
11898d86fe7dSBill Schmidt   MVT DstVT, SrcVT;
11908d86fe7dSBill Schmidt   Type *DstTy = I->getType();
11918d86fe7dSBill Schmidt   if (!isTypeLegal(DstTy, DstVT))
11928d86fe7dSBill Schmidt     return false;
11938d86fe7dSBill Schmidt 
11948d86fe7dSBill Schmidt   if (DstVT != MVT::i32 && DstVT != MVT::i64)
11958d86fe7dSBill Schmidt     return false;
11968d86fe7dSBill Schmidt 
1197d52990c7SJustin Hibbits   // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
11985ca75130SKit Barton   if (DstVT == MVT::i64 && !IsSigned && !Subtarget->hasFPCVT() &&
11995ca75130SKit Barton       !Subtarget->hasSPE())
120083973ef2SBill Schmidt     return false;
120183973ef2SBill Schmidt 
12028d86fe7dSBill Schmidt   Value *Src = I->getOperand(0);
12038d86fe7dSBill Schmidt   Type *SrcTy = Src->getType();
12048d86fe7dSBill Schmidt   if (!isTypeLegal(SrcTy, SrcVT))
12058d86fe7dSBill Schmidt     return false;
12068d86fe7dSBill Schmidt 
12078d86fe7dSBill Schmidt   if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
12088d86fe7dSBill Schmidt     return false;
12098d86fe7dSBill Schmidt 
1210d6b07348SJim Lin   Register SrcReg = getRegForValue(Src);
12118d86fe7dSBill Schmidt   if (SrcReg == 0)
12128d86fe7dSBill Schmidt     return false;
12138d86fe7dSBill Schmidt 
12144faa4090SKang Zhang   // Convert f32 to f64 or convert VSSRC to VSFRC if necessary. This is just a
12154faa4090SKang Zhang   // meaningless copy to get the register class right.
12168d86fe7dSBill Schmidt   const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
1217fec749ffSZi Xuan Wu   if (InRC == &PPC::F4RCRegClass)
1218fec749ffSZi Xuan Wu     SrcReg = copyRegToRegClass(&PPC::F8RCRegClass, SrcReg);
12194faa4090SKang Zhang   else if (InRC == &PPC::VSSRCRegClass)
12204faa4090SKang Zhang     SrcReg = copyRegToRegClass(&PPC::VSFRCRegClass, SrcReg);
12218d86fe7dSBill Schmidt 
12228d86fe7dSBill Schmidt   // Determine the opcode for the conversion, which takes place
12234faa4090SKang Zhang   // entirely within FPRs or VSRs.
1224d52990c7SJustin Hibbits   unsigned DestReg;
12258d86fe7dSBill Schmidt   unsigned Opc;
12264faa4090SKang Zhang   auto RC = MRI.getRegClass(SrcReg);
12278d86fe7dSBill Schmidt 
12285ca75130SKit Barton   if (Subtarget->hasSPE()) {
1229d52990c7SJustin Hibbits     DestReg = createResultReg(&PPC::GPRCRegClass);
1230d52990c7SJustin Hibbits     if (IsSigned)
123136e04d14SCraig Topper       Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
1232d52990c7SJustin Hibbits     else
123336e04d14SCraig Topper       Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
12344faa4090SKang Zhang   } else if (isVSFRCRegClass(RC)) {
12354faa4090SKang Zhang     DestReg = createResultReg(&PPC::VSFRCRegClass);
12364faa4090SKang Zhang     if (DstVT == MVT::i32)
12374faa4090SKang Zhang       Opc = IsSigned ? PPC::XSCVDPSXWS : PPC::XSCVDPUXWS;
12384faa4090SKang Zhang     else
12394faa4090SKang Zhang       Opc = IsSigned ? PPC::XSCVDPSXDS : PPC::XSCVDPUXDS;
1240d52990c7SJustin Hibbits   } else {
1241d52990c7SJustin Hibbits     DestReg = createResultReg(&PPC::F8RCRegClass);
12428d86fe7dSBill Schmidt     if (DstVT == MVT::i32)
12438d86fe7dSBill Schmidt       if (IsSigned)
12448d86fe7dSBill Schmidt         Opc = PPC::FCTIWZ;
12458d86fe7dSBill Schmidt       else
12465ca75130SKit Barton         Opc = Subtarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
12478d86fe7dSBill Schmidt     else
12488d86fe7dSBill Schmidt       Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
1249d52990c7SJustin Hibbits   }
12508d86fe7dSBill Schmidt 
12518d86fe7dSBill Schmidt   // Generate the convert.
1252ea09c595SRafael Espindola   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
12538d86fe7dSBill Schmidt     .addReg(SrcReg);
12548d86fe7dSBill Schmidt 
12558d86fe7dSBill Schmidt   // Now move the integer value from a float register to an integer register.
12565ca75130SKit Barton   unsigned IntReg = Subtarget->hasSPE()
12575ca75130SKit Barton                         ? DestReg
12585ca75130SKit Barton                         : PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
1259d52990c7SJustin Hibbits 
12608d86fe7dSBill Schmidt   if (IntReg == 0)
12618d86fe7dSBill Schmidt     return false;
12628d86fe7dSBill Schmidt 
12635b8bb4d7SJuergen Ributzka   updateValueMap(I, IntReg);
12648d86fe7dSBill Schmidt   return true;
12658d86fe7dSBill Schmidt }
12668d86fe7dSBill Schmidt 
1267ccecf261SBill Schmidt // Attempt to fast-select a binary integer operation that isn't already
1268ccecf261SBill Schmidt // handled automatically.
SelectBinaryIntOp(const Instruction * I,unsigned ISDOpcode)1269ccecf261SBill Schmidt bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
127044ede33aSMehdi Amini   EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1271ccecf261SBill Schmidt 
1272ccecf261SBill Schmidt   // We can get here in the case when we have a binary operation on a non-legal
1273ccecf261SBill Schmidt   // type and the target independent selector doesn't know how to handle it.
1274ccecf261SBill Schmidt   if (DestVT != MVT::i16 && DestVT != MVT::i8)
1275ccecf261SBill Schmidt     return false;
1276ccecf261SBill Schmidt 
1277ccecf261SBill Schmidt   // Look at the currently assigned register for this instruction
1278ccecf261SBill Schmidt   // to determine the required register class.  If there is no register,
1279ccecf261SBill Schmidt   // make a conservative choice (don't assign R0).
1280d6b07348SJim Lin   Register AssignedReg = FuncInfo.ValueMap[I];
1281ccecf261SBill Schmidt   const TargetRegisterClass *RC =
1282ccecf261SBill Schmidt     (AssignedReg ? MRI.getRegClass(AssignedReg) :
1283ccecf261SBill Schmidt      &PPC::GPRC_and_GPRC_NOR0RegClass);
1284ccecf261SBill Schmidt   bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
1285ccecf261SBill Schmidt 
1286ccecf261SBill Schmidt   unsigned Opc;
1287ccecf261SBill Schmidt   switch (ISDOpcode) {
1288ccecf261SBill Schmidt     default: return false;
1289ccecf261SBill Schmidt     case ISD::ADD:
1290ccecf261SBill Schmidt       Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
1291ccecf261SBill Schmidt       break;
1292ccecf261SBill Schmidt     case ISD::OR:
1293ccecf261SBill Schmidt       Opc = IsGPRC ? PPC::OR : PPC::OR8;
1294ccecf261SBill Schmidt       break;
1295ccecf261SBill Schmidt     case ISD::SUB:
1296ccecf261SBill Schmidt       Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
1297ccecf261SBill Schmidt       break;
1298ccecf261SBill Schmidt   }
1299ccecf261SBill Schmidt 
1300d6b07348SJim Lin   Register ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
1301d6b07348SJim Lin   Register SrcReg1 = getRegForValue(I->getOperand(0));
1302ccecf261SBill Schmidt   if (SrcReg1 == 0) return false;
1303ccecf261SBill Schmidt 
1304ccecf261SBill Schmidt   // Handle case of small immediate operand.
1305ccecf261SBill Schmidt   if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
1306ccecf261SBill Schmidt     const APInt &CIVal = ConstInt->getValue();
1307ccecf261SBill Schmidt     int Imm = (int)CIVal.getSExtValue();
1308ccecf261SBill Schmidt     bool UseImm = true;
1309ccecf261SBill Schmidt     if (isInt<16>(Imm)) {
1310ccecf261SBill Schmidt       switch (Opc) {
1311ccecf261SBill Schmidt         default:
1312ccecf261SBill Schmidt           llvm_unreachable("Missing case!");
1313ccecf261SBill Schmidt         case PPC::ADD4:
1314ccecf261SBill Schmidt           Opc = PPC::ADDI;
1315ccecf261SBill Schmidt           MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1316ccecf261SBill Schmidt           break;
1317ccecf261SBill Schmidt         case PPC::ADD8:
1318ccecf261SBill Schmidt           Opc = PPC::ADDI8;
1319ccecf261SBill Schmidt           MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1320ccecf261SBill Schmidt           break;
1321ccecf261SBill Schmidt         case PPC::OR:
1322ccecf261SBill Schmidt           Opc = PPC::ORI;
1323ccecf261SBill Schmidt           break;
1324ccecf261SBill Schmidt         case PPC::OR8:
1325ccecf261SBill Schmidt           Opc = PPC::ORI8;
1326ccecf261SBill Schmidt           break;
1327ccecf261SBill Schmidt         case PPC::SUBF:
1328ccecf261SBill Schmidt           if (Imm == -32768)
1329ccecf261SBill Schmidt             UseImm = false;
1330ccecf261SBill Schmidt           else {
1331ccecf261SBill Schmidt             Opc = PPC::ADDI;
1332ccecf261SBill Schmidt             MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
1333ccecf261SBill Schmidt             Imm = -Imm;
1334ccecf261SBill Schmidt           }
1335ccecf261SBill Schmidt           break;
1336ccecf261SBill Schmidt         case PPC::SUBF8:
1337ccecf261SBill Schmidt           if (Imm == -32768)
1338ccecf261SBill Schmidt             UseImm = false;
1339ccecf261SBill Schmidt           else {
1340ccecf261SBill Schmidt             Opc = PPC::ADDI8;
1341ccecf261SBill Schmidt             MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
1342ccecf261SBill Schmidt             Imm = -Imm;
1343ccecf261SBill Schmidt           }
1344ccecf261SBill Schmidt           break;
1345ccecf261SBill Schmidt       }
1346ccecf261SBill Schmidt 
1347ccecf261SBill Schmidt       if (UseImm) {
1348ea09c595SRafael Espindola         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1349ea09c595SRafael Espindola                 ResultReg)
1350ea09c595SRafael Espindola             .addReg(SrcReg1)
1351ea09c595SRafael Espindola             .addImm(Imm);
13525b8bb4d7SJuergen Ributzka         updateValueMap(I, ResultReg);
1353ccecf261SBill Schmidt         return true;
1354ccecf261SBill Schmidt       }
1355ccecf261SBill Schmidt     }
1356ccecf261SBill Schmidt   }
1357ccecf261SBill Schmidt 
1358ccecf261SBill Schmidt   // Reg-reg case.
1359d6b07348SJim Lin   Register SrcReg2 = getRegForValue(I->getOperand(1));
1360ccecf261SBill Schmidt   if (SrcReg2 == 0) return false;
1361ccecf261SBill Schmidt 
1362ccecf261SBill Schmidt   // Reverse operands for subtract-from.
1363ccecf261SBill Schmidt   if (ISDOpcode == ISD::SUB)
1364ccecf261SBill Schmidt     std::swap(SrcReg1, SrcReg2);
1365ccecf261SBill Schmidt 
1366ea09c595SRafael Espindola   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1367ccecf261SBill Schmidt     .addReg(SrcReg1).addReg(SrcReg2);
13685b8bb4d7SJuergen Ributzka   updateValueMap(I, ResultReg);
1369ccecf261SBill Schmidt   return true;
1370ccecf261SBill Schmidt }
1371ccecf261SBill Schmidt 
13728470b0f9SBill Schmidt // Handle arguments to a call that we're attempting to fast-select.
13738470b0f9SBill Schmidt // Return false if the arguments are too complex for us at the moment.
processCallArgs(SmallVectorImpl<Value * > & Args,SmallVectorImpl<unsigned> & ArgRegs,SmallVectorImpl<MVT> & ArgVTs,SmallVectorImpl<ISD::ArgFlagsTy> & ArgFlags,SmallVectorImpl<unsigned> & RegArgs,CallingConv::ID CC,unsigned & NumBytes,bool IsVarArg)13748470b0f9SBill Schmidt bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
13758470b0f9SBill Schmidt                                   SmallVectorImpl<unsigned> &ArgRegs,
13768470b0f9SBill Schmidt                                   SmallVectorImpl<MVT> &ArgVTs,
13778470b0f9SBill Schmidt                                   SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
13788470b0f9SBill Schmidt                                   SmallVectorImpl<unsigned> &RegArgs,
13798470b0f9SBill Schmidt                                   CallingConv::ID CC,
13808470b0f9SBill Schmidt                                   unsigned &NumBytes,
13818470b0f9SBill Schmidt                                   bool IsVarArg) {
13828470b0f9SBill Schmidt   SmallVector<CCValAssign, 16> ArgLocs;
1383b5217507SEric Christopher   CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
1384f316e1dbSUlrich Weigand 
1385f316e1dbSUlrich Weigand   // Reserve space for the linkage area on the stack.
13865ca75130SKit Barton   unsigned LinkageSize = Subtarget->getFrameLowering()->getLinkageSize();
13871778564fSGuillaume Chatelet   CCInfo.AllocateStack(LinkageSize, Align(8));
1388f316e1dbSUlrich Weigand 
13898470b0f9SBill Schmidt   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
13908470b0f9SBill Schmidt 
13918470b0f9SBill Schmidt   // Bail out if we can't handle any of the arguments.
13928470b0f9SBill Schmidt   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
13938470b0f9SBill Schmidt     CCValAssign &VA = ArgLocs[I];
13948470b0f9SBill Schmidt     MVT ArgVT = ArgVTs[VA.getValNo()];
13958470b0f9SBill Schmidt 
13968470b0f9SBill Schmidt     // Skip vector arguments for now, as well as long double and
13978470b0f9SBill Schmidt     // uint128_t, and anything that isn't passed in a register.
1398940ab934SHal Finkel     if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
13998470b0f9SBill Schmidt         !VA.isRegLoc() || VA.needsCustom())
14008470b0f9SBill Schmidt       return false;
14018470b0f9SBill Schmidt 
14028470b0f9SBill Schmidt     // Skip bit-converted arguments for now.
14038470b0f9SBill Schmidt     if (VA.getLocInfo() == CCValAssign::BCvt)
14048470b0f9SBill Schmidt       return false;
14058470b0f9SBill Schmidt   }
14068470b0f9SBill Schmidt 
14078470b0f9SBill Schmidt   // Get a count of how many bytes are to be pushed onto the stack.
14088470b0f9SBill Schmidt   NumBytes = CCInfo.getNextStackOffset();
14098470b0f9SBill Schmidt 
1410f316e1dbSUlrich Weigand   // The prolog code of the callee may store up to 8 GPR argument registers to
1411f316e1dbSUlrich Weigand   // the stack, allowing va_start to index over them in memory if its varargs.
1412f316e1dbSUlrich Weigand   // Because we cannot tell if this is needed on the caller side, we have to
1413f316e1dbSUlrich Weigand   // conservatively assume that it is needed.  As such, make sure we have at
1414f316e1dbSUlrich Weigand   // least enough stack space for the caller to store the 8 GPRs.
14158658f17eSUlrich Weigand   // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
14168ca988f3SUlrich Weigand   NumBytes = std::max(NumBytes, LinkageSize + 64);
1417f316e1dbSUlrich Weigand 
14188470b0f9SBill Schmidt   // Issue CALLSEQ_START.
1419ea09c595SRafael Espindola   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
14208470b0f9SBill Schmidt           TII.get(TII.getCallFrameSetupOpcode()))
1421d526b13eSSerge Pavlov     .addImm(NumBytes).addImm(0);
14228470b0f9SBill Schmidt 
14238470b0f9SBill Schmidt   // Prepare to assign register arguments.  Every argument uses up a
14248470b0f9SBill Schmidt   // GPR protocol register even if it's passed in a floating-point
1425f81b6dd7SHal Finkel   // register (unless we're using the fast calling convention).
14268470b0f9SBill Schmidt   unsigned NextGPR = PPC::X3;
14278470b0f9SBill Schmidt   unsigned NextFPR = PPC::F1;
14288470b0f9SBill Schmidt 
14298470b0f9SBill Schmidt   // Process arguments.
14308470b0f9SBill Schmidt   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
14318470b0f9SBill Schmidt     CCValAssign &VA = ArgLocs[I];
14328470b0f9SBill Schmidt     unsigned Arg = ArgRegs[VA.getValNo()];
14338470b0f9SBill Schmidt     MVT ArgVT = ArgVTs[VA.getValNo()];
14348470b0f9SBill Schmidt 
14358470b0f9SBill Schmidt     // Handle argument promotion and bitcasts.
14368470b0f9SBill Schmidt     switch (VA.getLocInfo()) {
14378470b0f9SBill Schmidt       default:
14388470b0f9SBill Schmidt         llvm_unreachable("Unknown loc info!");
14398470b0f9SBill Schmidt       case CCValAssign::Full:
14408470b0f9SBill Schmidt         break;
14418470b0f9SBill Schmidt       case CCValAssign::SExt: {
14428470b0f9SBill Schmidt         MVT DestVT = VA.getLocVT();
14438470b0f9SBill Schmidt         const TargetRegisterClass *RC =
14448470b0f9SBill Schmidt           (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1445d6b07348SJim Lin         Register TmpReg = createResultReg(RC);
14468470b0f9SBill Schmidt         if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
14478470b0f9SBill Schmidt           llvm_unreachable("Failed to emit a sext!");
14488470b0f9SBill Schmidt         ArgVT = DestVT;
14498470b0f9SBill Schmidt         Arg = TmpReg;
14508470b0f9SBill Schmidt         break;
14518470b0f9SBill Schmidt       }
14528470b0f9SBill Schmidt       case CCValAssign::AExt:
14538470b0f9SBill Schmidt       case CCValAssign::ZExt: {
14548470b0f9SBill Schmidt         MVT DestVT = VA.getLocVT();
14558470b0f9SBill Schmidt         const TargetRegisterClass *RC =
14568470b0f9SBill Schmidt           (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1457d6b07348SJim Lin         Register TmpReg = createResultReg(RC);
14588470b0f9SBill Schmidt         if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
14598470b0f9SBill Schmidt           llvm_unreachable("Failed to emit a zext!");
14608470b0f9SBill Schmidt         ArgVT = DestVT;
14618470b0f9SBill Schmidt         Arg = TmpReg;
14628470b0f9SBill Schmidt         break;
14638470b0f9SBill Schmidt       }
14648470b0f9SBill Schmidt       case CCValAssign::BCvt: {
14658470b0f9SBill Schmidt         // FIXME: Not yet handled.
14668470b0f9SBill Schmidt         llvm_unreachable("Should have bailed before getting here!");
14678470b0f9SBill Schmidt         break;
14688470b0f9SBill Schmidt       }
14698470b0f9SBill Schmidt     }
14708470b0f9SBill Schmidt 
14718470b0f9SBill Schmidt     // Copy this argument to the appropriate register.
14728470b0f9SBill Schmidt     unsigned ArgReg;
14738470b0f9SBill Schmidt     if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
14748470b0f9SBill Schmidt       ArgReg = NextFPR++;
1475f81b6dd7SHal Finkel       if (CC != CallingConv::Fast)
14768470b0f9SBill Schmidt         ++NextGPR;
14778470b0f9SBill Schmidt     } else
14788470b0f9SBill Schmidt       ArgReg = NextGPR++;
14798470b0f9SBill Schmidt 
1480ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1481ea09c595SRafael Espindola             TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
14828470b0f9SBill Schmidt     RegArgs.push_back(ArgReg);
14838470b0f9SBill Schmidt   }
14848470b0f9SBill Schmidt 
14858470b0f9SBill Schmidt   return true;
14868470b0f9SBill Schmidt }
14878470b0f9SBill Schmidt 
14888470b0f9SBill Schmidt // For a call that we've determined we can fast-select, finish the
14898470b0f9SBill Schmidt // call sequence and generate a copy to obtain the return value (if any).
finishCall(MVT RetVT,CallLoweringInfo & CLI,unsigned & NumBytes)1490934361a4SHal Finkel bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
1491934361a4SHal Finkel   CallingConv::ID CC = CLI.CallConv;
1492934361a4SHal Finkel 
14938470b0f9SBill Schmidt   // Issue CallSEQ_END.
1494ea09c595SRafael Espindola   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
14958470b0f9SBill Schmidt           TII.get(TII.getCallFrameDestroyOpcode()))
14968470b0f9SBill Schmidt     .addImm(NumBytes).addImm(0);
14978470b0f9SBill Schmidt 
14988470b0f9SBill Schmidt   // Next, generate a copy to obtain the return value.
14998470b0f9SBill Schmidt   // FIXME: No multi-register return values yet, though I don't foresee
15008470b0f9SBill Schmidt   // any real difficulties there.
15018470b0f9SBill Schmidt   if (RetVT != MVT::isVoid) {
15028470b0f9SBill Schmidt     SmallVector<CCValAssign, 16> RVLocs;
1503934361a4SHal Finkel     CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
15048470b0f9SBill Schmidt     CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
15058470b0f9SBill Schmidt     CCValAssign &VA = RVLocs[0];
15068470b0f9SBill Schmidt     assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
15078470b0f9SBill Schmidt     assert(VA.isRegLoc() && "Can only return in registers!");
15088470b0f9SBill Schmidt 
15098470b0f9SBill Schmidt     MVT DestVT = VA.getValVT();
15108470b0f9SBill Schmidt     MVT CopyVT = DestVT;
15118470b0f9SBill Schmidt 
15128470b0f9SBill Schmidt     // Ints smaller than a register still arrive in a full 64-bit
15138470b0f9SBill Schmidt     // register, so make sure we recognize this.
15148470b0f9SBill Schmidt     if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
15158470b0f9SBill Schmidt       CopyVT = MVT::i64;
15168470b0f9SBill Schmidt 
15178470b0f9SBill Schmidt     unsigned SourcePhysReg = VA.getLocReg();
15180954ea1bSBill Schmidt     unsigned ResultReg = 0;
15198470b0f9SBill Schmidt 
15208470b0f9SBill Schmidt     if (RetVT == CopyVT) {
15218470b0f9SBill Schmidt       const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
1522fec749ffSZi Xuan Wu       ResultReg = copyRegToRegClass(CpyRC, SourcePhysReg);
15238470b0f9SBill Schmidt 
15248470b0f9SBill Schmidt     // If necessary, round the floating result to single precision.
15258470b0f9SBill Schmidt     } else if (CopyVT == MVT::f64) {
15268470b0f9SBill Schmidt       ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
1527ea09c595SRafael Espindola       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
15288470b0f9SBill Schmidt               ResultReg).addReg(SourcePhysReg);
15298470b0f9SBill Schmidt 
15308470b0f9SBill Schmidt     // If only the low half of a general register is needed, generate
15318470b0f9SBill Schmidt     // a GPRC copy instead of a G8RC copy.  (EXTRACT_SUBREG can't be
15328470b0f9SBill Schmidt     // used along the fast-isel path (not lowered), and downstream logic
15338470b0f9SBill Schmidt     // also doesn't like a direct subreg copy on a physical reg.)
15348470b0f9SBill Schmidt     } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
15358470b0f9SBill Schmidt       // Convert physical register from G8RC to GPRC.
15368470b0f9SBill Schmidt       SourcePhysReg -= PPC::X0 - PPC::R0;
1537fec749ffSZi Xuan Wu       ResultReg = copyRegToRegClass(&PPC::GPRCRegClass, SourcePhysReg);
15388470b0f9SBill Schmidt     }
15398470b0f9SBill Schmidt 
15400954ea1bSBill Schmidt     assert(ResultReg && "ResultReg unset!");
1541934361a4SHal Finkel     CLI.InRegs.push_back(SourcePhysReg);
1542934361a4SHal Finkel     CLI.ResultReg = ResultReg;
1543934361a4SHal Finkel     CLI.NumResultRegs = 1;
15448470b0f9SBill Schmidt   }
15458470b0f9SBill Schmidt 
1546934361a4SHal Finkel   return true;
1547934361a4SHal Finkel }
15488470b0f9SBill Schmidt 
fastLowerCall(CallLoweringInfo & CLI)1549934361a4SHal Finkel bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1550934361a4SHal Finkel   CallingConv::ID CC  = CLI.CallConv;
1551934361a4SHal Finkel   bool IsTailCall     = CLI.IsTailCall;
1552934361a4SHal Finkel   bool IsVarArg       = CLI.IsVarArg;
1553934361a4SHal Finkel   const Value *Callee = CLI.Callee;
1554ce4c2bc1SRafael Espindola   const MCSymbol *Symbol = CLI.Symbol;
1555934361a4SHal Finkel 
1556ce4c2bc1SRafael Espindola   if (!Callee && !Symbol)
15578470b0f9SBill Schmidt     return false;
15588470b0f9SBill Schmidt 
15598470b0f9SBill Schmidt   // Allow SelectionDAG isel to handle tail calls.
1560934361a4SHal Finkel   if (IsTailCall)
15618470b0f9SBill Schmidt     return false;
15628470b0f9SBill Schmidt 
1563934361a4SHal Finkel   // Let SDISel handle vararg functions.
15648470b0f9SBill Schmidt   if (IsVarArg)
15658470b0f9SBill Schmidt     return false;
15668470b0f9SBill Schmidt 
1567075a92deSNemanja Ivanovic   // If this is a PC-Rel function, let SDISel handle the call.
1568075a92deSNemanja Ivanovic   if (Subtarget->isUsingPCRelativeCalls())
1569075a92deSNemanja Ivanovic     return false;
1570075a92deSNemanja Ivanovic 
15718470b0f9SBill Schmidt   // Handle simple calls for now, with legal return types and
15728470b0f9SBill Schmidt   // those that can be extended.
1573934361a4SHal Finkel   Type *RetTy = CLI.RetTy;
15748470b0f9SBill Schmidt   MVT RetVT;
15758470b0f9SBill Schmidt   if (RetTy->isVoidTy())
15768470b0f9SBill Schmidt     RetVT = MVT::isVoid;
15778470b0f9SBill Schmidt   else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
15788470b0f9SBill Schmidt            RetVT != MVT::i8)
15798470b0f9SBill Schmidt     return false;
15805ca75130SKit Barton   else if (RetVT == MVT::i1 && Subtarget->useCRBits())
158150271aaeSHal Finkel     // We can't handle boolean returns when CR bits are in use.
158250271aaeSHal Finkel     return false;
15838470b0f9SBill Schmidt 
15848470b0f9SBill Schmidt   // FIXME: No multi-register return values yet.
15858470b0f9SBill Schmidt   if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
15868470b0f9SBill Schmidt       RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
15878470b0f9SBill Schmidt       RetVT != MVT::f64) {
15888470b0f9SBill Schmidt     SmallVector<CCValAssign, 16> RVLocs;
1589b5217507SEric Christopher     CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
15908470b0f9SBill Schmidt     CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
15918470b0f9SBill Schmidt     if (RVLocs.size() > 1)
15928470b0f9SBill Schmidt       return false;
15938470b0f9SBill Schmidt   }
15948470b0f9SBill Schmidt 
15958470b0f9SBill Schmidt   // Bail early if more than 8 arguments, as we only currently
15968470b0f9SBill Schmidt   // handle arguments passed in registers.
1597934361a4SHal Finkel   unsigned NumArgs = CLI.OutVals.size();
15988470b0f9SBill Schmidt   if (NumArgs > 8)
15998470b0f9SBill Schmidt     return false;
16008470b0f9SBill Schmidt 
16018470b0f9SBill Schmidt   // Set up the argument vectors.
16028470b0f9SBill Schmidt   SmallVector<Value*, 8> Args;
16038470b0f9SBill Schmidt   SmallVector<unsigned, 8> ArgRegs;
16048470b0f9SBill Schmidt   SmallVector<MVT, 8> ArgVTs;
16058470b0f9SBill Schmidt   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
16068470b0f9SBill Schmidt 
16078470b0f9SBill Schmidt   Args.reserve(NumArgs);
16088470b0f9SBill Schmidt   ArgRegs.reserve(NumArgs);
16098470b0f9SBill Schmidt   ArgVTs.reserve(NumArgs);
16108470b0f9SBill Schmidt   ArgFlags.reserve(NumArgs);
16118470b0f9SBill Schmidt 
1612934361a4SHal Finkel   for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
16138470b0f9SBill Schmidt     // Only handle easy calls for now.  It would be reasonably easy
16148470b0f9SBill Schmidt     // to handle <= 8-byte structures passed ByVal in registers, but we
16158470b0f9SBill Schmidt     // have to ensure they are right-justified in the register.
1616934361a4SHal Finkel     ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
1617934361a4SHal Finkel     if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
16188470b0f9SBill Schmidt       return false;
16198470b0f9SBill Schmidt 
1620934361a4SHal Finkel     Value *ArgValue = CLI.OutVals[i];
1621934361a4SHal Finkel     Type *ArgTy = ArgValue->getType();
16228470b0f9SBill Schmidt     MVT ArgVT;
16238470b0f9SBill Schmidt     if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
16248470b0f9SBill Schmidt       return false;
16258470b0f9SBill Schmidt 
1626d14e5180SQiu Chaofan     // FIXME: FastISel cannot handle non-simple types yet, including 128-bit FP
1627d14e5180SQiu Chaofan     // types, which is passed through vector register. Skip these types and
1628d14e5180SQiu Chaofan     // fallback to default SelectionDAG based selection.
1629d14e5180SQiu Chaofan     if (ArgVT.isVector() || ArgVT == MVT::f128)
16308470b0f9SBill Schmidt       return false;
16318470b0f9SBill Schmidt 
1632d6b07348SJim Lin     Register Arg = getRegForValue(ArgValue);
16338470b0f9SBill Schmidt     if (Arg == 0)
16348470b0f9SBill Schmidt       return false;
16358470b0f9SBill Schmidt 
1636934361a4SHal Finkel     Args.push_back(ArgValue);
16378470b0f9SBill Schmidt     ArgRegs.push_back(Arg);
16388470b0f9SBill Schmidt     ArgVTs.push_back(ArgVT);
16398470b0f9SBill Schmidt     ArgFlags.push_back(Flags);
16408470b0f9SBill Schmidt   }
16418470b0f9SBill Schmidt 
16428470b0f9SBill Schmidt   // Process the arguments.
16438470b0f9SBill Schmidt   SmallVector<unsigned, 8> RegArgs;
16448470b0f9SBill Schmidt   unsigned NumBytes;
16458470b0f9SBill Schmidt 
16468470b0f9SBill Schmidt   if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
16478470b0f9SBill Schmidt                        RegArgs, CC, NumBytes, IsVarArg))
16488470b0f9SBill Schmidt     return false;
16498470b0f9SBill Schmidt 
1650934361a4SHal Finkel   MachineInstrBuilder MIB;
16518470b0f9SBill Schmidt   // FIXME: No handling for function pointers yet.  This requires
16528470b0f9SBill Schmidt   // implementing the function descriptor (OPD) setup.
16538470b0f9SBill Schmidt   const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1654934361a4SHal Finkel   if (!GV) {
1655934361a4SHal Finkel     // patchpoints are a special case; they always dispatch to a pointer value.
1656934361a4SHal Finkel     // However, we don't actually want to generate the indirect call sequence
1657934361a4SHal Finkel     // here (that will be generated, as necessary, during asm printing), and
1658934361a4SHal Finkel     // the call we generate here will be erased by FastISel::selectPatchpoint,
1659934361a4SHal Finkel     // so don't try very hard...
1660934361a4SHal Finkel     if (CLI.IsPatchPoint)
1661934361a4SHal Finkel       MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
1662934361a4SHal Finkel     else
16638470b0f9SBill Schmidt       return false;
1664934361a4SHal Finkel   } else {
16658470b0f9SBill Schmidt     // Build direct call with NOP for TOC restore.
16668470b0f9SBill Schmidt     // FIXME: We can and should optimize away the NOP for local calls.
1667934361a4SHal Finkel     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
16688470b0f9SBill Schmidt                   TII.get(PPC::BL8_NOP));
16698470b0f9SBill Schmidt     // Add callee.
16708470b0f9SBill Schmidt     MIB.addGlobalAddress(GV);
1671934361a4SHal Finkel   }
16728470b0f9SBill Schmidt 
16738470b0f9SBill Schmidt   // Add implicit physical register uses to the call.
16748470b0f9SBill Schmidt   for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
16758470b0f9SBill Schmidt     MIB.addReg(RegArgs[II], RegState::Implicit);
16768470b0f9SBill Schmidt 
1677af51993eSHal Finkel   // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
1678af51993eSHal Finkel   // into the call.
1679e6698d53SHal Finkel   PPCFuncInfo->setUsesTOCBasePtr();
1680aa0ac4f1SUlrich Weigand   MIB.addReg(PPC::X2, RegState::Implicit);
1681aa0ac4f1SUlrich Weigand 
16828470b0f9SBill Schmidt   // Add a register mask with the call-preserved registers.  Proper
16838470b0f9SBill Schmidt   // defs for return values will be added by setPhysRegsDeadExcept().
16849deb75d1SEric Christopher   MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
16858470b0f9SBill Schmidt 
1686934361a4SHal Finkel   CLI.Call = MIB;
1687934361a4SHal Finkel 
16888470b0f9SBill Schmidt   // Finish off the call including any return values.
1689934361a4SHal Finkel   return finishCall(RetVT, CLI, NumBytes);
16908470b0f9SBill Schmidt }
16918470b0f9SBill Schmidt 
1692d89f678cSBill Schmidt // Attempt to fast-select a return instruction.
SelectRet(const Instruction * I)1693d89f678cSBill Schmidt bool PPCFastISel::SelectRet(const Instruction *I) {
1694d89f678cSBill Schmidt 
1695d89f678cSBill Schmidt   if (!FuncInfo.CanLowerReturn)
1696d89f678cSBill Schmidt     return false;
1697d89f678cSBill Schmidt 
1698d89f678cSBill Schmidt   const ReturnInst *Ret = cast<ReturnInst>(I);
1699d89f678cSBill Schmidt   const Function &F = *I->getParent()->getParent();
1700d89f678cSBill Schmidt 
1701d89f678cSBill Schmidt   // Build a list of return value registers.
1702d89f678cSBill Schmidt   SmallVector<unsigned, 4> RetRegs;
1703d89f678cSBill Schmidt   CallingConv::ID CC = F.getCallingConv();
1704d89f678cSBill Schmidt 
1705d89f678cSBill Schmidt   if (Ret->getNumOperands() > 0) {
1706d89f678cSBill Schmidt     SmallVector<ISD::OutputArg, 4> Outs;
170781920b0aSMatt Arsenault     GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1708d89f678cSBill Schmidt 
1709d89f678cSBill Schmidt     // Analyze operands of the call, assigning locations to each operand.
1710d89f678cSBill Schmidt     SmallVector<CCValAssign, 16> ValLocs;
1711b5217507SEric Christopher     CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
1712d89f678cSBill Schmidt     CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1713d89f678cSBill Schmidt     const Value *RV = Ret->getOperand(0);
1714d89f678cSBill Schmidt 
1715d89f678cSBill Schmidt     // FIXME: Only one output register for now.
1716d89f678cSBill Schmidt     if (ValLocs.size() > 1)
1717d89f678cSBill Schmidt       return false;
1718d89f678cSBill Schmidt 
1719f0024d14SEric Christopher     // Special case for returning a constant integer of any size - materialize
1720f0024d14SEric Christopher     // the constant as an i64 and copy it to the return register.
172103df7ac8SEric Christopher     if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
172261570df7SSamuel Antao       CCValAssign &VA = ValLocs[0];
172361570df7SSamuel Antao 
17240c476111SDaniel Sanders       Register RetReg = VA.getLocReg();
1725f0024d14SEric Christopher       // We still need to worry about properly extending the sign. For example,
1726f0024d14SEric Christopher       // we could have only a single bit or a constant that needs zero
1727f0024d14SEric Christopher       // extension rather than sign extension. Make sure we pass the return
1728f0024d14SEric Christopher       // value extension property to integer materialization.
172903df7ac8SEric Christopher       unsigned SrcReg =
1730b6fdce4cSNemanja Ivanovic           PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
173161570df7SSamuel Antao 
1732ea09c595SRafael Espindola       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1733ea09c595SRafael Espindola             TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
173461570df7SSamuel Antao 
1735d89f678cSBill Schmidt       RetRegs.push_back(RetReg);
1736d89f678cSBill Schmidt 
1737d89f678cSBill Schmidt     } else {
1738d6b07348SJim Lin       Register Reg = getRegForValue(RV);
1739d89f678cSBill Schmidt 
1740d89f678cSBill Schmidt       if (Reg == 0)
1741d89f678cSBill Schmidt         return false;
1742d89f678cSBill Schmidt 
1743d89f678cSBill Schmidt       // Copy the result values into the output registers.
1744d89f678cSBill Schmidt       for (unsigned i = 0; i < ValLocs.size(); ++i) {
1745d89f678cSBill Schmidt 
1746d89f678cSBill Schmidt         CCValAssign &VA = ValLocs[i];
1747d89f678cSBill Schmidt         assert(VA.isRegLoc() && "Can only return in registers!");
1748d89f678cSBill Schmidt         RetRegs.push_back(VA.getLocReg());
1749d89f678cSBill Schmidt         unsigned SrcReg = Reg + VA.getValNo();
1750d89f678cSBill Schmidt 
175144ede33aSMehdi Amini         EVT RVEVT = TLI.getValueType(DL, RV->getType());
1752d89f678cSBill Schmidt         if (!RVEVT.isSimple())
1753d89f678cSBill Schmidt           return false;
1754d89f678cSBill Schmidt         MVT RVVT = RVEVT.getSimpleVT();
1755d89f678cSBill Schmidt         MVT DestVT = VA.getLocVT();
1756d89f678cSBill Schmidt 
1757d89f678cSBill Schmidt         if (RVVT != DestVT && RVVT != MVT::i8 &&
1758d89f678cSBill Schmidt             RVVT != MVT::i16 && RVVT != MVT::i32)
1759d89f678cSBill Schmidt           return false;
1760d89f678cSBill Schmidt 
1761d89f678cSBill Schmidt         if (RVVT != DestVT) {
1762d89f678cSBill Schmidt           switch (VA.getLocInfo()) {
1763d89f678cSBill Schmidt             default:
1764d89f678cSBill Schmidt               llvm_unreachable("Unknown loc info!");
1765d89f678cSBill Schmidt             case CCValAssign::Full:
1766d89f678cSBill Schmidt               llvm_unreachable("Full value assign but types don't match?");
1767d89f678cSBill Schmidt             case CCValAssign::AExt:
1768d89f678cSBill Schmidt             case CCValAssign::ZExt: {
1769d89f678cSBill Schmidt               const TargetRegisterClass *RC =
1770d89f678cSBill Schmidt                 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1771d6b07348SJim Lin               Register TmpReg = createResultReg(RC);
1772d89f678cSBill Schmidt               if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
1773d89f678cSBill Schmidt                 return false;
1774d89f678cSBill Schmidt               SrcReg = TmpReg;
1775d89f678cSBill Schmidt               break;
1776d89f678cSBill Schmidt             }
1777d89f678cSBill Schmidt             case CCValAssign::SExt: {
1778d89f678cSBill Schmidt               const TargetRegisterClass *RC =
1779d89f678cSBill Schmidt                 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
1780d6b07348SJim Lin               Register TmpReg = createResultReg(RC);
1781d89f678cSBill Schmidt               if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
1782d89f678cSBill Schmidt                 return false;
1783d89f678cSBill Schmidt               SrcReg = TmpReg;
1784d89f678cSBill Schmidt               break;
1785d89f678cSBill Schmidt             }
1786d89f678cSBill Schmidt           }
1787d89f678cSBill Schmidt         }
1788d89f678cSBill Schmidt 
1789ea09c595SRafael Espindola         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1790d89f678cSBill Schmidt                 TII.get(TargetOpcode::COPY), RetRegs[i])
1791d89f678cSBill Schmidt           .addReg(SrcReg);
1792d89f678cSBill Schmidt       }
1793d89f678cSBill Schmidt     }
1794d89f678cSBill Schmidt   }
1795d89f678cSBill Schmidt 
1796ea09c595SRafael Espindola   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1797f4a22c0dSHal Finkel                                     TII.get(PPC::BLR8));
1798d89f678cSBill Schmidt 
1799d89f678cSBill Schmidt   for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1800d89f678cSBill Schmidt     MIB.addReg(RetRegs[i], RegState::Implicit);
1801d89f678cSBill Schmidt 
1802d89f678cSBill Schmidt   return true;
1803d89f678cSBill Schmidt }
1804d89f678cSBill Schmidt 
18050300813dSBill Schmidt // Attempt to emit an integer extend of SrcReg into DestReg.  Both
18060300813dSBill Schmidt // signed and zero extensions are supported.  Return false if we
1807d89f678cSBill Schmidt // can't handle it.
PPCEmitIntExt(MVT SrcVT,unsigned SrcReg,MVT DestVT,unsigned DestReg,bool IsZExt)18080300813dSBill Schmidt bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
18090300813dSBill Schmidt                                 unsigned DestReg, bool IsZExt) {
1810d89f678cSBill Schmidt   if (DestVT != MVT::i32 && DestVT != MVT::i64)
1811d89f678cSBill Schmidt     return false;
1812d89f678cSBill Schmidt   if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
1813d89f678cSBill Schmidt     return false;
1814d89f678cSBill Schmidt 
1815d89f678cSBill Schmidt   // Signed extensions use EXTSB, EXTSH, EXTSW.
1816d89f678cSBill Schmidt   if (!IsZExt) {
1817d89f678cSBill Schmidt     unsigned Opc;
1818d89f678cSBill Schmidt     if (SrcVT == MVT::i8)
1819d89f678cSBill Schmidt       Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
1820d89f678cSBill Schmidt     else if (SrcVT == MVT::i16)
1821d89f678cSBill Schmidt       Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
1822d89f678cSBill Schmidt     else {
1823d89f678cSBill Schmidt       assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
1824d89f678cSBill Schmidt       Opc = PPC::EXTSW_32_64;
1825d89f678cSBill Schmidt     }
1826ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
1827d89f678cSBill Schmidt       .addReg(SrcReg);
1828d89f678cSBill Schmidt 
1829d89f678cSBill Schmidt   // Unsigned 32-bit extensions use RLWINM.
1830d89f678cSBill Schmidt   } else if (DestVT == MVT::i32) {
1831d89f678cSBill Schmidt     unsigned MB;
1832d89f678cSBill Schmidt     if (SrcVT == MVT::i8)
1833d89f678cSBill Schmidt       MB = 24;
1834d89f678cSBill Schmidt     else {
1835d89f678cSBill Schmidt       assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
1836d89f678cSBill Schmidt       MB = 16;
1837d89f678cSBill Schmidt     }
1838ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
1839d89f678cSBill Schmidt             DestReg)
1840d89f678cSBill Schmidt       .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
1841d89f678cSBill Schmidt 
1842d89f678cSBill Schmidt   // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
1843d89f678cSBill Schmidt   } else {
1844d89f678cSBill Schmidt     unsigned MB;
1845d89f678cSBill Schmidt     if (SrcVT == MVT::i8)
1846d89f678cSBill Schmidt       MB = 56;
1847d89f678cSBill Schmidt     else if (SrcVT == MVT::i16)
1848d89f678cSBill Schmidt       MB = 48;
1849d89f678cSBill Schmidt     else
1850d89f678cSBill Schmidt       MB = 32;
1851ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1852d89f678cSBill Schmidt             TII.get(PPC::RLDICL_32_64), DestReg)
1853d89f678cSBill Schmidt       .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
1854d89f678cSBill Schmidt   }
1855d89f678cSBill Schmidt 
1856d89f678cSBill Schmidt   return true;
18570300813dSBill Schmidt }
18580300813dSBill Schmidt 
18590300813dSBill Schmidt // Attempt to fast-select an indirect branch instruction.
SelectIndirectBr(const Instruction * I)18600300813dSBill Schmidt bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
1861d6b07348SJim Lin   Register AddrReg = getRegForValue(I->getOperand(0));
18620300813dSBill Schmidt   if (AddrReg == 0)
18630300813dSBill Schmidt     return false;
18640300813dSBill Schmidt 
1865ea09c595SRafael Espindola   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
18660300813dSBill Schmidt     .addReg(AddrReg);
1867ea09c595SRafael Espindola   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
18680300813dSBill Schmidt 
18690300813dSBill Schmidt   const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1870ebcd7489SPete Cooper   for (const BasicBlock *SuccBB : IB->successors())
1871ebcd7489SPete Cooper     FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
18720300813dSBill Schmidt 
18730300813dSBill Schmidt   return true;
18740300813dSBill Schmidt }
18750300813dSBill Schmidt 
18769d9510d8SBill Schmidt // Attempt to fast-select an integer truncate instruction.
SelectTrunc(const Instruction * I)18779d9510d8SBill Schmidt bool PPCFastISel::SelectTrunc(const Instruction *I) {
18789d9510d8SBill Schmidt   Value *Src  = I->getOperand(0);
187944ede33aSMehdi Amini   EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
188044ede33aSMehdi Amini   EVT DestVT = TLI.getValueType(DL, I->getType(), true);
18819d9510d8SBill Schmidt 
18829d9510d8SBill Schmidt   if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
18839d9510d8SBill Schmidt     return false;
18849d9510d8SBill Schmidt 
18859d9510d8SBill Schmidt   if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
18869d9510d8SBill Schmidt     return false;
18879d9510d8SBill Schmidt 
1888d6b07348SJim Lin   Register SrcReg = getRegForValue(Src);
18899d9510d8SBill Schmidt   if (!SrcReg)
18909d9510d8SBill Schmidt     return false;
18919d9510d8SBill Schmidt 
18929d9510d8SBill Schmidt   // The only interesting case is when we need to switch register classes.
1893fec749ffSZi Xuan Wu   if (SrcVT == MVT::i64)
1894fec749ffSZi Xuan Wu     SrcReg = copyRegToRegClass(&PPC::GPRCRegClass, SrcReg, 0, PPC::sub_32);
18959d9510d8SBill Schmidt 
18965b8bb4d7SJuergen Ributzka   updateValueMap(I, SrcReg);
18979d9510d8SBill Schmidt   return true;
18989d9510d8SBill Schmidt }
18999d9510d8SBill Schmidt 
1900d89f678cSBill Schmidt // Attempt to fast-select an integer extend instruction.
SelectIntExt(const Instruction * I)1901d89f678cSBill Schmidt bool PPCFastISel::SelectIntExt(const Instruction *I) {
1902d89f678cSBill Schmidt   Type *DestTy = I->getType();
1903d89f678cSBill Schmidt   Value *Src = I->getOperand(0);
1904d89f678cSBill Schmidt   Type *SrcTy = Src->getType();
1905d89f678cSBill Schmidt 
1906d89f678cSBill Schmidt   bool IsZExt = isa<ZExtInst>(I);
1907d6b07348SJim Lin   Register SrcReg = getRegForValue(Src);
1908d89f678cSBill Schmidt   if (!SrcReg) return false;
1909d89f678cSBill Schmidt 
1910d89f678cSBill Schmidt   EVT SrcEVT, DestEVT;
191144ede33aSMehdi Amini   SrcEVT = TLI.getValueType(DL, SrcTy, true);
191244ede33aSMehdi Amini   DestEVT = TLI.getValueType(DL, DestTy, true);
1913d89f678cSBill Schmidt   if (!SrcEVT.isSimple())
1914d89f678cSBill Schmidt     return false;
1915d89f678cSBill Schmidt   if (!DestEVT.isSimple())
1916d89f678cSBill Schmidt     return false;
1917d89f678cSBill Schmidt 
1918d89f678cSBill Schmidt   MVT SrcVT = SrcEVT.getSimpleVT();
1919d89f678cSBill Schmidt   MVT DestVT = DestEVT.getSimpleVT();
1920d89f678cSBill Schmidt 
1921d89f678cSBill Schmidt   // If we know the register class needed for the result of this
1922d89f678cSBill Schmidt   // instruction, use it.  Otherwise pick the register class of the
1923d89f678cSBill Schmidt   // correct size that does not contain X0/R0, since we don't know
1924d89f678cSBill Schmidt   // whether downstream uses permit that assignment.
1925d6b07348SJim Lin   Register AssignedReg = FuncInfo.ValueMap[I];
1926d89f678cSBill Schmidt   const TargetRegisterClass *RC =
1927d89f678cSBill Schmidt     (AssignedReg ? MRI.getRegClass(AssignedReg) :
1928d89f678cSBill Schmidt      (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
1929d89f678cSBill Schmidt       &PPC::GPRC_and_GPRC_NOR0RegClass));
1930d6b07348SJim Lin   Register ResultReg = createResultReg(RC);
1931d89f678cSBill Schmidt 
1932d89f678cSBill Schmidt   if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
1933d89f678cSBill Schmidt     return false;
1934d89f678cSBill Schmidt 
19355b8bb4d7SJuergen Ributzka   updateValueMap(I, ResultReg);
1936d89f678cSBill Schmidt   return true;
1937d89f678cSBill Schmidt }
1938d89f678cSBill Schmidt 
19390cf702faSBill Schmidt // Attempt to fast-select an instruction that wasn't handled by
19400300813dSBill Schmidt // the table-generated machinery.
fastSelectInstruction(const Instruction * I)19415b8bb4d7SJuergen Ributzka bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
19420300813dSBill Schmidt 
19430300813dSBill Schmidt   switch (I->getOpcode()) {
1944ccecf261SBill Schmidt     case Instruction::Load:
1945ccecf261SBill Schmidt       return SelectLoad(I);
1946ccecf261SBill Schmidt     case Instruction::Store:
1947ccecf261SBill Schmidt       return SelectStore(I);
19480300813dSBill Schmidt     case Instruction::Br:
19490300813dSBill Schmidt       return SelectBranch(I);
19500300813dSBill Schmidt     case Instruction::IndirectBr:
19510300813dSBill Schmidt       return SelectIndirectBr(I);
19528d86fe7dSBill Schmidt     case Instruction::FPExt:
19538d86fe7dSBill Schmidt       return SelectFPExt(I);
19548d86fe7dSBill Schmidt     case Instruction::FPTrunc:
19558d86fe7dSBill Schmidt       return SelectFPTrunc(I);
19568d86fe7dSBill Schmidt     case Instruction::SIToFP:
19578d86fe7dSBill Schmidt       return SelectIToFP(I, /*IsSigned*/ true);
19588d86fe7dSBill Schmidt     case Instruction::UIToFP:
19598d86fe7dSBill Schmidt       return SelectIToFP(I, /*IsSigned*/ false);
19608d86fe7dSBill Schmidt     case Instruction::FPToSI:
19618d86fe7dSBill Schmidt       return SelectFPToI(I, /*IsSigned*/ true);
19628d86fe7dSBill Schmidt     case Instruction::FPToUI:
19638d86fe7dSBill Schmidt       return SelectFPToI(I, /*IsSigned*/ false);
1964ccecf261SBill Schmidt     case Instruction::Add:
1965ccecf261SBill Schmidt       return SelectBinaryIntOp(I, ISD::ADD);
1966ccecf261SBill Schmidt     case Instruction::Or:
1967ccecf261SBill Schmidt       return SelectBinaryIntOp(I, ISD::OR);
1968ccecf261SBill Schmidt     case Instruction::Sub:
1969ccecf261SBill Schmidt       return SelectBinaryIntOp(I, ISD::SUB);
1970d89f678cSBill Schmidt     case Instruction::Ret:
1971d89f678cSBill Schmidt       return SelectRet(I);
19729d9510d8SBill Schmidt     case Instruction::Trunc:
19739d9510d8SBill Schmidt       return SelectTrunc(I);
1974d89f678cSBill Schmidt     case Instruction::ZExt:
1975d89f678cSBill Schmidt     case Instruction::SExt:
1976d89f678cSBill Schmidt       return SelectIntExt(I);
19770300813dSBill Schmidt     // Here add other flavors of Instruction::XXX that automated
19780300813dSBill Schmidt     // cases don't catch.  For example, switches are terminators
19790300813dSBill Schmidt     // that aren't yet handled.
19800300813dSBill Schmidt     default:
19810300813dSBill Schmidt       break;
19820300813dSBill Schmidt   }
19830300813dSBill Schmidt   return false;
19840cf702faSBill Schmidt }
19850cf702faSBill Schmidt 
19860cf702faSBill Schmidt // Materialize a floating-point constant into a register, and return
19870cf702faSBill Schmidt // the register number (or zero if we failed to handle it).
PPCMaterializeFP(const ConstantFP * CFP,MVT VT)19880cf702faSBill Schmidt unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
1989075a92deSNemanja Ivanovic   // If this is a PC-Rel function, let SDISel handle constant pool.
1990075a92deSNemanja Ivanovic   if (Subtarget->isUsingPCRelativeCalls())
1991075a92deSNemanja Ivanovic     return false;
1992075a92deSNemanja Ivanovic 
19930cf702faSBill Schmidt   // No plans to handle long double here.
19940cf702faSBill Schmidt   if (VT != MVT::f32 && VT != MVT::f64)
19950cf702faSBill Schmidt     return 0;
19960cf702faSBill Schmidt 
19970cf702faSBill Schmidt   // All FP constants are loaded from the constant pool.
1998c9d5c195SGuillaume Chatelet   Align Alignment = DL.getPrefTypeAlign(CFP->getType());
19998c72b027SCraig Topper   unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Alignment);
20005ca75130SKit Barton   const bool HasSPE = Subtarget->hasSPE();
2001d52990c7SJustin Hibbits   const TargetRegisterClass *RC;
2002d52990c7SJustin Hibbits   if (HasSPE)
200336e04d14SCraig Topper     RC = ((VT == MVT::f32) ? &PPC::GPRCRegClass : &PPC::SPERCRegClass);
2004d52990c7SJustin Hibbits   else
2005d52990c7SJustin Hibbits     RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
2006d52990c7SJustin Hibbits 
2007d6b07348SJim Lin   Register DestReg = createResultReg(RC);
20080cf702faSBill Schmidt   CodeModel::Model CModel = TM.getCodeModel();
20090cf702faSBill Schmidt 
2010e40c8a2bSAlex Lorenz   MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2011e40c8a2bSAlex Lorenz       MachinePointerInfo::getConstantPool(*FuncInfo.MF),
2012c9d5c195SGuillaume Chatelet       MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Alignment);
20130cf702faSBill Schmidt 
2014d52990c7SJustin Hibbits   unsigned Opc;
2015d52990c7SJustin Hibbits 
2016d52990c7SJustin Hibbits   if (HasSPE)
2017d52990c7SJustin Hibbits     Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
2018d52990c7SJustin Hibbits   else
2019d52990c7SJustin Hibbits     Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
2020d52990c7SJustin Hibbits 
2021d6b07348SJim Lin   Register TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
20220300813dSBill Schmidt 
2023e6698d53SHal Finkel   PPCFuncInfo->setUsesTOCBasePtr();
20240300813dSBill Schmidt   // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
202579e238afSRafael Espindola   if (CModel == CodeModel::Small) {
2026ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
20270300813dSBill Schmidt             TmpReg)
20280300813dSBill Schmidt       .addConstantPoolIndex(Idx).addReg(PPC::X2);
2029ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
20300300813dSBill Schmidt       .addImm(0).addReg(TmpReg).addMemOperand(MMO);
20310300813dSBill Schmidt   } else {
20328dd563efSJason Liu     // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA8(X2, Idx)).
20338dd563efSJason Liu     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA8),
20340cf702faSBill Schmidt             TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
2035bb381d70SBill Schmidt     // But for large code model, we must generate a LDtocL followed
2036bb381d70SBill Schmidt     // by the LF[SD].
2037bb381d70SBill Schmidt     if (CModel == CodeModel::Large) {
2038d6b07348SJim Lin       Register TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2039ea09c595SRafael Espindola       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
2040bb381d70SBill Schmidt               TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
2041ea09c595SRafael Espindola       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
20429d0b5312SNAKAMURA Takumi           .addImm(0)
20439d0b5312SNAKAMURA Takumi           .addReg(TmpReg2);
2044bb381d70SBill Schmidt     } else
2045ea09c595SRafael Espindola       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
20460cf702faSBill Schmidt         .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
20470cf702faSBill Schmidt         .addReg(TmpReg)
20480cf702faSBill Schmidt         .addMemOperand(MMO);
20490cf702faSBill Schmidt   }
20500cf702faSBill Schmidt 
20510cf702faSBill Schmidt   return DestReg;
20520cf702faSBill Schmidt }
20530cf702faSBill Schmidt 
2054ccecf261SBill Schmidt // Materialize the address of a global value into a register, and return
2055ccecf261SBill Schmidt // the register number (or zero if we failed to handle it).
PPCMaterializeGV(const GlobalValue * GV,MVT VT)2056ccecf261SBill Schmidt unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
2057075a92deSNemanja Ivanovic   // If this is a PC-Rel function, let SDISel handle GV materialization.
2058075a92deSNemanja Ivanovic   if (Subtarget->isUsingPCRelativeCalls())
2059075a92deSNemanja Ivanovic     return false;
2060075a92deSNemanja Ivanovic 
2061ccecf261SBill Schmidt   assert(VT == MVT::i64 && "Non-address!");
2062ccecf261SBill Schmidt   const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
2063d6b07348SJim Lin   Register DestReg = createResultReg(RC);
2064ccecf261SBill Schmidt 
2065ccecf261SBill Schmidt   // Global values may be plain old object addresses, TLS object
2066ccecf261SBill Schmidt   // addresses, constant pool entries, or jump tables.  How we generate
2067ccecf261SBill Schmidt   // code for these may depend on small, medium, or large code model.
2068ccecf261SBill Schmidt   CodeModel::Model CModel = TM.getCodeModel();
2069ccecf261SBill Schmidt 
2070ccecf261SBill Schmidt   // FIXME: Jump tables are not yet required because fast-isel doesn't
2071ccecf261SBill Schmidt   // handle switches; if that changes, we need them as well.  For now,
2072ccecf261SBill Schmidt   // what follows assumes everything's a generic (or TLS) global address.
2073ccecf261SBill Schmidt 
2074ccecf261SBill Schmidt   // FIXME: We don't yet handle the complexity of TLS.
207559f7eba2SRafael Espindola   if (GV->isThreadLocal())
2076ccecf261SBill Schmidt     return 0;
2077ccecf261SBill Schmidt 
207810d3bf95SSean Fertile   // If the global has the toc-data attribute then fallback to DAG-ISEL.
207910d3bf95SSean Fertile   if (TM.getTargetTriple().isOSAIX())
208010d3bf95SSean Fertile     if (const GlobalVariable *Var = dyn_cast_or_null<GlobalVariable>(GV))
208110d3bf95SSean Fertile       if (Var->hasAttribute("toc-data"))
208210d3bf95SSean Fertile         return false;
208310d3bf95SSean Fertile 
2084e6698d53SHal Finkel   PPCFuncInfo->setUsesTOCBasePtr();
2085ccecf261SBill Schmidt   // For small code model, generate a simple TOC load.
208679e238afSRafael Espindola   if (CModel == CodeModel::Small)
2087ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
2088ea09c595SRafael Espindola             DestReg)
2089ea09c595SRafael Espindola         .addGlobalAddress(GV)
2090ea09c595SRafael Espindola         .addReg(PPC::X2);
2091ccecf261SBill Schmidt   else {
20925d82f09bSBill Schmidt     // If the address is an externally defined symbol, a symbol with common
20935d82f09bSBill Schmidt     // or externally available linkage, a non-local function address, or a
2094ccecf261SBill Schmidt     // jump table address (not yet needed), or if we are generating code
2095ccecf261SBill Schmidt     // for large code model, we generate:
20968dd563efSJason Liu     //       LDtocL(GV, ADDIStocHA8(%x2, GV))
2097ccecf261SBill Schmidt     // Otherwise we generate:
20988dd563efSJason Liu     //       ADDItocL(ADDIStocHA8(%x2, GV), GV)
20998dd563efSJason Liu     // Either way, start with the ADDIStocHA8:
2100d6b07348SJim Lin     Register HighPartReg = createResultReg(RC);
21018dd563efSJason Liu     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA8),
2102ccecf261SBill Schmidt             HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
2103ccecf261SBill Schmidt 
21045ca75130SKit Barton     if (Subtarget->isGVIndirectSymbol(GV)) {
2105ea09c595SRafael Espindola       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
2106ccecf261SBill Schmidt               DestReg).addGlobalAddress(GV).addReg(HighPartReg);
2107c1808367SEric Christopher     } else {
2108ccecf261SBill Schmidt       // Otherwise generate the ADDItocL.
2109ea09c595SRafael Espindola       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
2110ccecf261SBill Schmidt               DestReg).addReg(HighPartReg).addGlobalAddress(GV);
2111ccecf261SBill Schmidt     }
2112c1808367SEric Christopher   }
2113ccecf261SBill Schmidt 
2114ccecf261SBill Schmidt   return DestReg;
2115ccecf261SBill Schmidt }
2116ccecf261SBill Schmidt 
21170cf702faSBill Schmidt // Materialize a 32-bit integer constant into a register, and return
21180cf702faSBill Schmidt // the register number (or zero if we failed to handle it).
PPCMaterialize32BitInt(int64_t Imm,const TargetRegisterClass * RC)21190cf702faSBill Schmidt unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
21200cf702faSBill Schmidt                                              const TargetRegisterClass *RC) {
21210cf702faSBill Schmidt   unsigned Lo = Imm & 0xFFFF;
21220cf702faSBill Schmidt   unsigned Hi = (Imm >> 16) & 0xFFFF;
21230cf702faSBill Schmidt 
2124d6b07348SJim Lin   Register ResultReg = createResultReg(RC);
21250cf702faSBill Schmidt   bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
21260cf702faSBill Schmidt 
21270cf702faSBill Schmidt   if (isInt<16>(Imm))
2128ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
21290cf702faSBill Schmidt             TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
21300cf702faSBill Schmidt       .addImm(Imm);
21310cf702faSBill Schmidt   else if (Lo) {
21320cf702faSBill Schmidt     // Both Lo and Hi have nonzero bits.
2133d6b07348SJim Lin     Register TmpReg = createResultReg(RC);
2134ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
21350cf702faSBill Schmidt             TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
21360cf702faSBill Schmidt       .addImm(Hi);
2137ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
21380cf702faSBill Schmidt             TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
21390cf702faSBill Schmidt       .addReg(TmpReg).addImm(Lo);
21400cf702faSBill Schmidt   } else
21410cf702faSBill Schmidt     // Just Hi bits.
2142ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
21430cf702faSBill Schmidt             TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
21440cf702faSBill Schmidt         .addImm(Hi);
21450cf702faSBill Schmidt 
21460cf702faSBill Schmidt   return ResultReg;
21470cf702faSBill Schmidt }
21480cf702faSBill Schmidt 
21490cf702faSBill Schmidt // Materialize a 64-bit integer constant into a register, and return
21500cf702faSBill Schmidt // the register number (or zero if we failed to handle it).
PPCMaterialize64BitInt(int64_t Imm,const TargetRegisterClass * RC)21510cf702faSBill Schmidt unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
21520cf702faSBill Schmidt                                              const TargetRegisterClass *RC) {
21530cf702faSBill Schmidt   unsigned Remainder = 0;
21540cf702faSBill Schmidt   unsigned Shift = 0;
21550cf702faSBill Schmidt 
21560cf702faSBill Schmidt   // If the value doesn't fit in 32 bits, see if we can shift it
21570cf702faSBill Schmidt   // so that it fits in 32 bits.
21580cf702faSBill Schmidt   if (!isInt<32>(Imm)) {
21590cf702faSBill Schmidt     Shift = countTrailingZeros<uint64_t>(Imm);
21600cf702faSBill Schmidt     int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
21610cf702faSBill Schmidt 
21620cf702faSBill Schmidt     if (isInt<32>(ImmSh))
21630cf702faSBill Schmidt       Imm = ImmSh;
21640cf702faSBill Schmidt     else {
21650cf702faSBill Schmidt       Remainder = Imm;
21660cf702faSBill Schmidt       Shift = 32;
21670cf702faSBill Schmidt       Imm >>= 32;
21680cf702faSBill Schmidt     }
21690cf702faSBill Schmidt   }
21700cf702faSBill Schmidt 
21710cf702faSBill Schmidt   // Handle the high-order 32 bits (if shifted) or the whole 32 bits
21720cf702faSBill Schmidt   // (if not shifted).
21730cf702faSBill Schmidt   unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
21740cf702faSBill Schmidt   if (!Shift)
21750cf702faSBill Schmidt     return TmpReg1;
21760cf702faSBill Schmidt 
21770cf702faSBill Schmidt   // If upper 32 bits were not zero, we've built them and need to shift
21780cf702faSBill Schmidt   // them into place.
21790cf702faSBill Schmidt   unsigned TmpReg2;
21800cf702faSBill Schmidt   if (Imm) {
21810cf702faSBill Schmidt     TmpReg2 = createResultReg(RC);
2182ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
21830cf702faSBill Schmidt             TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
21840cf702faSBill Schmidt   } else
21850cf702faSBill Schmidt     TmpReg2 = TmpReg1;
21860cf702faSBill Schmidt 
21870cf702faSBill Schmidt   unsigned TmpReg3, Hi, Lo;
21880cf702faSBill Schmidt   if ((Hi = (Remainder >> 16) & 0xFFFF)) {
21890cf702faSBill Schmidt     TmpReg3 = createResultReg(RC);
2190ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
21910cf702faSBill Schmidt             TmpReg3).addReg(TmpReg2).addImm(Hi);
21920cf702faSBill Schmidt   } else
21930cf702faSBill Schmidt     TmpReg3 = TmpReg2;
21940cf702faSBill Schmidt 
21950cf702faSBill Schmidt   if ((Lo = Remainder & 0xFFFF)) {
2196d6b07348SJim Lin     Register ResultReg = createResultReg(RC);
2197ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
21980cf702faSBill Schmidt             ResultReg).addReg(TmpReg3).addImm(Lo);
21990cf702faSBill Schmidt     return ResultReg;
22000cf702faSBill Schmidt   }
22010cf702faSBill Schmidt 
22020cf702faSBill Schmidt   return TmpReg3;
22030cf702faSBill Schmidt }
22040cf702faSBill Schmidt 
22050cf702faSBill Schmidt // Materialize an integer constant into a register, and return
22060cf702faSBill Schmidt // the register number (or zero if we failed to handle it).
PPCMaterializeInt(const ConstantInt * CI,MVT VT,bool UseSExt)220703df7ac8SEric Christopher unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
220861570df7SSamuel Antao                                         bool UseSExt) {
2209940ab934SHal Finkel   // If we're using CR bit registers for i1 values, handle that as a special
2210940ab934SHal Finkel   // case first.
22115ca75130SKit Barton   if (VT == MVT::i1 && Subtarget->useCRBits()) {
2212d6b07348SJim Lin     Register ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2213940ab934SHal Finkel     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2214940ab934SHal Finkel             TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2215940ab934SHal Finkel     return ImmReg;
2216940ab934SHal Finkel   }
22170cf702faSBill Schmidt 
221880ba58a1SEric Christopher   if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
221980ba58a1SEric Christopher       VT != MVT::i1)
22200cf702faSBill Schmidt     return 0;
22210cf702faSBill Schmidt 
222280ba58a1SEric Christopher   const TargetRegisterClass *RC =
222380ba58a1SEric Christopher       ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
2224b6fdce4cSNemanja Ivanovic   int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
22250cf702faSBill Schmidt 
22260cf702faSBill Schmidt   // If the constant is in range, use a load-immediate.
22277d9b9b2dSEric Christopher   // Since LI will sign extend the constant we need to make sure that for
22287d9b9b2dSEric Christopher   // our zeroext constants that the sign extended constant fits into 16-bits -
22297d9b9b2dSEric Christopher   // a range of 0..0x7fff.
2230b6fdce4cSNemanja Ivanovic   if (isInt<16>(Imm)) {
22310cf702faSBill Schmidt     unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2232d6b07348SJim Lin     Register ImmReg = createResultReg(RC);
2233ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
2234b6fdce4cSNemanja Ivanovic         .addImm(Imm);
2235f0024d14SEric Christopher     return ImmReg;
22360cf702faSBill Schmidt   }
22370cf702faSBill Schmidt 
22380cf702faSBill Schmidt   // Construct the constant piecewise.
22390cf702faSBill Schmidt   if (VT == MVT::i64)
22400cf702faSBill Schmidt     return PPCMaterialize64BitInt(Imm, RC);
22410cf702faSBill Schmidt   else if (VT == MVT::i32)
22420cf702faSBill Schmidt     return PPCMaterialize32BitInt(Imm, RC);
22430cf702faSBill Schmidt 
22440cf702faSBill Schmidt   return 0;
22450cf702faSBill Schmidt }
22460cf702faSBill Schmidt 
22470cf702faSBill Schmidt // Materialize a constant into a register, and return the register
22480cf702faSBill Schmidt // number (or zero if we failed to handle it).
fastMaterializeConstant(const Constant * C)22495b8bb4d7SJuergen Ributzka unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
225044ede33aSMehdi Amini   EVT CEVT = TLI.getValueType(DL, C->getType(), true);
22510cf702faSBill Schmidt 
22520cf702faSBill Schmidt   // Only handle simple types.
22530cf702faSBill Schmidt   if (!CEVT.isSimple()) return 0;
22540cf702faSBill Schmidt   MVT VT = CEVT.getSimpleVT();
22550cf702faSBill Schmidt 
22560cf702faSBill Schmidt   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
22570cf702faSBill Schmidt     return PPCMaterializeFP(CFP, VT);
2258ccecf261SBill Schmidt   else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2259ccecf261SBill Schmidt     return PPCMaterializeGV(GV, VT);
226003df7ac8SEric Christopher   else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
226173390c7aSHal Finkel     // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
226273390c7aSHal Finkel     // assumes that constant PHI operands will be zero extended, and failure to
226373390c7aSHal Finkel     // match that assumption will cause problems if we sign extend here but
226473390c7aSHal Finkel     // some user of a PHI is in a block for which we fall back to full SDAG
226573390c7aSHal Finkel     // instruction selection.
226673390c7aSHal Finkel     return PPCMaterializeInt(CI, VT, false);
22670cf702faSBill Schmidt 
22680cf702faSBill Schmidt   return 0;
22690cf702faSBill Schmidt }
22700cf702faSBill Schmidt 
22710cf702faSBill Schmidt // Materialize the address created by an alloca into a register, and
2272eb8d6f7dSBill Schmidt // return the register number (or zero if we failed to handle it).
fastMaterializeAlloca(const AllocaInst * AI)22735b8bb4d7SJuergen Ributzka unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
2274eb8d6f7dSBill Schmidt   // Don't handle dynamic allocas.
2275eb8d6f7dSBill Schmidt   if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
2276eb8d6f7dSBill Schmidt 
2277eb8d6f7dSBill Schmidt   MVT VT;
2278eb8d6f7dSBill Schmidt   if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
2279eb8d6f7dSBill Schmidt 
2280eb8d6f7dSBill Schmidt   DenseMap<const AllocaInst*, int>::iterator SI =
2281eb8d6f7dSBill Schmidt     FuncInfo.StaticAllocaMap.find(AI);
2282eb8d6f7dSBill Schmidt 
2283eb8d6f7dSBill Schmidt   if (SI != FuncInfo.StaticAllocaMap.end()) {
2284d6b07348SJim Lin     Register ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
2285ea09c595SRafael Espindola     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
2286eb8d6f7dSBill Schmidt             ResultReg).addFrameIndex(SI->second).addImm(0);
2287eb8d6f7dSBill Schmidt     return ResultReg;
2288eb8d6f7dSBill Schmidt   }
2289eb8d6f7dSBill Schmidt 
2290eb8d6f7dSBill Schmidt   return 0;
22910cf702faSBill Schmidt }
22920cf702faSBill Schmidt 
2293ccecf261SBill Schmidt // Fold loads into extends when possible.
2294ccecf261SBill Schmidt // FIXME: We can have multiple redundant extend/trunc instructions
2295ccecf261SBill Schmidt // following a load.  The folding only picks up one.  Extend this
2296ccecf261SBill Schmidt // to check subsequent instructions for the same pattern and remove
2297ccecf261SBill Schmidt // them.  Thus ResultReg should be the def reg for the last redundant
2298ccecf261SBill Schmidt // instruction in a chain, and all intervening instructions can be
2299ccecf261SBill Schmidt // removed from parent.  Change test/CodeGen/PowerPC/fast-isel-fold.ll
2300ccecf261SBill Schmidt // to add ELF64-NOT: rldicl to the appropriate tests when this works.
tryToFoldLoadIntoMI(MachineInstr * MI,unsigned OpNo,const LoadInst * LI)23010cf702faSBill Schmidt bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
23020cf702faSBill Schmidt                                       const LoadInst *LI) {
2303ccecf261SBill Schmidt   // Verify we have a legal type before going any further.
2304ccecf261SBill Schmidt   MVT VT;
2305ccecf261SBill Schmidt   if (!isLoadTypeLegal(LI->getType(), VT))
2306ccecf261SBill Schmidt     return false;
2307ccecf261SBill Schmidt 
2308ccecf261SBill Schmidt   // Combine load followed by zero- or sign-extend.
2309ccecf261SBill Schmidt   bool IsZExt = false;
2310ccecf261SBill Schmidt   switch(MI->getOpcode()) {
2311ccecf261SBill Schmidt     default:
2312ccecf261SBill Schmidt       return false;
2313ccecf261SBill Schmidt 
2314ccecf261SBill Schmidt     case PPC::RLDICL:
2315ccecf261SBill Schmidt     case PPC::RLDICL_32_64: {
2316ccecf261SBill Schmidt       IsZExt = true;
2317ccecf261SBill Schmidt       unsigned MB = MI->getOperand(3).getImm();
2318ccecf261SBill Schmidt       if ((VT == MVT::i8 && MB <= 56) ||
2319ccecf261SBill Schmidt           (VT == MVT::i16 && MB <= 48) ||
2320ccecf261SBill Schmidt           (VT == MVT::i32 && MB <= 32))
2321ccecf261SBill Schmidt         break;
2322ccecf261SBill Schmidt       return false;
2323ccecf261SBill Schmidt     }
2324ccecf261SBill Schmidt 
2325ccecf261SBill Schmidt     case PPC::RLWINM:
2326ccecf261SBill Schmidt     case PPC::RLWINM8: {
2327ccecf261SBill Schmidt       IsZExt = true;
2328ccecf261SBill Schmidt       unsigned MB = MI->getOperand(3).getImm();
2329ccecf261SBill Schmidt       if ((VT == MVT::i8 && MB <= 24) ||
2330ccecf261SBill Schmidt           (VT == MVT::i16 && MB <= 16))
2331ccecf261SBill Schmidt         break;
2332ccecf261SBill Schmidt       return false;
2333ccecf261SBill Schmidt     }
2334ccecf261SBill Schmidt 
2335ccecf261SBill Schmidt     case PPC::EXTSB:
2336ccecf261SBill Schmidt     case PPC::EXTSB8:
2337ccecf261SBill Schmidt     case PPC::EXTSB8_32_64:
2338ccecf261SBill Schmidt       /* There is no sign-extending load-byte instruction. */
2339ccecf261SBill Schmidt       return false;
2340ccecf261SBill Schmidt 
2341ccecf261SBill Schmidt     case PPC::EXTSH:
2342ccecf261SBill Schmidt     case PPC::EXTSH8:
2343ccecf261SBill Schmidt     case PPC::EXTSH8_32_64: {
2344ccecf261SBill Schmidt       if (VT != MVT::i16 && VT != MVT::i8)
2345ccecf261SBill Schmidt         return false;
2346ccecf261SBill Schmidt       break;
2347ccecf261SBill Schmidt     }
2348ccecf261SBill Schmidt 
2349ccecf261SBill Schmidt     case PPC::EXTSW:
235096c3d626SNemanja Ivanovic     case PPC::EXTSW_32:
2351ccecf261SBill Schmidt     case PPC::EXTSW_32_64: {
2352ccecf261SBill Schmidt       if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
2353ccecf261SBill Schmidt         return false;
2354ccecf261SBill Schmidt       break;
2355ccecf261SBill Schmidt     }
2356ccecf261SBill Schmidt   }
2357ccecf261SBill Schmidt 
2358ccecf261SBill Schmidt   // See if we can handle this address.
2359ccecf261SBill Schmidt   Address Addr;
2360ccecf261SBill Schmidt   if (!PPCComputeAddress(LI->getOperand(0), Addr))
2361ccecf261SBill Schmidt     return false;
2362ccecf261SBill Schmidt 
23630c476111SDaniel Sanders   Register ResultReg = MI->getOperand(0).getReg();
2364ccecf261SBill Schmidt 
2365d52990c7SJustin Hibbits   if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
23665ca75130SKit Barton                    Subtarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
2367ccecf261SBill Schmidt     return false;
2368ccecf261SBill Schmidt 
2369256a16d0STim Northover   MachineBasicBlock::iterator I(MI);
2370256a16d0STim Northover   removeDeadCode(I, std::next(I));
2371ccecf261SBill Schmidt   return true;
23720cf702faSBill Schmidt }
23730cf702faSBill Schmidt 
23740cf702faSBill Schmidt // Attempt to lower call arguments in a faster way than done by
23750cf702faSBill Schmidt // the selection DAG code.
fastLowerArguments()23765b8bb4d7SJuergen Ributzka bool PPCFastISel::fastLowerArguments() {
23770cf702faSBill Schmidt   // Defer to normal argument lowering for now.  It's reasonably
23780cf702faSBill Schmidt   // efficient.  Consider doing something like ARM to handle the
23790cf702faSBill Schmidt   // case where all args fit in registers, no varargs, no float
23800cf702faSBill Schmidt   // or vector args.
23810cf702faSBill Schmidt   return false;
23820cf702faSBill Schmidt }
23830cf702faSBill Schmidt 
23840300813dSBill Schmidt // Handle materializing integer constants into a register.  This is not
23850300813dSBill Schmidt // automatically generated for PowerPC, so must be explicitly created here.
fastEmit_i(MVT Ty,MVT VT,unsigned Opc,uint64_t Imm)238688e32517SJuergen Ributzka unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
23870300813dSBill Schmidt 
23880300813dSBill Schmidt   if (Opc != ISD::Constant)
23890300813dSBill Schmidt     return 0;
23900300813dSBill Schmidt 
2391940ab934SHal Finkel   // If we're using CR bit registers for i1 values, handle that as a special
2392940ab934SHal Finkel   // case first.
23935ca75130SKit Barton   if (VT == MVT::i1 && Subtarget->useCRBits()) {
2394d6b07348SJim Lin     Register ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2395940ab934SHal Finkel     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2396940ab934SHal Finkel             TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2397940ab934SHal Finkel     return ImmReg;
2398940ab934SHal Finkel   }
2399940ab934SHal Finkel 
24009d0b5312SNAKAMURA Takumi   if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
24019d0b5312SNAKAMURA Takumi       VT != MVT::i1)
24020300813dSBill Schmidt     return 0;
24030300813dSBill Schmidt 
24040300813dSBill Schmidt   const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
24050300813dSBill Schmidt                                    &PPC::GPRCRegClass);
24060300813dSBill Schmidt   if (VT == MVT::i64)
24070300813dSBill Schmidt     return PPCMaterialize64BitInt(Imm, RC);
24080300813dSBill Schmidt   else
24090300813dSBill Schmidt     return PPCMaterialize32BitInt(Imm, RC);
24100300813dSBill Schmidt }
24110300813dSBill Schmidt 
2412ccecf261SBill Schmidt // Override for ADDI and ADDI8 to set the correct register class
2413ccecf261SBill Schmidt // on RHS operand 0.  The automatic infrastructure naively assumes
2414ccecf261SBill Schmidt // GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
2415ccecf261SBill Schmidt // for these cases.  At the moment, none of the other automatically
2416ccecf261SBill Schmidt // generated RI instructions require special treatment.  However, once
2417ccecf261SBill Schmidt // SelectSelect is implemented, "isel" requires similar handling.
2418ccecf261SBill Schmidt //
2419ccecf261SBill Schmidt // Also be conservative about the output register class.  Avoid
2420ccecf261SBill Schmidt // assigning R0 or X0 to the output register for GPRC and G8RC
2421ccecf261SBill Schmidt // register classes, as any such result could be used in ADDI, etc.,
2422ccecf261SBill Schmidt // where those regs have another meaning.
fastEmitInst_ri(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0,uint64_t Imm)242388e32517SJuergen Ributzka unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
2424ccecf261SBill Schmidt                                       const TargetRegisterClass *RC,
242566506582SNikita Popov                                       unsigned Op0,
2426ccecf261SBill Schmidt                                       uint64_t Imm) {
2427ccecf261SBill Schmidt   if (MachineInstOpcode == PPC::ADDI)
2428ccecf261SBill Schmidt     MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
2429ccecf261SBill Schmidt   else if (MachineInstOpcode == PPC::ADDI8)
2430ccecf261SBill Schmidt     MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
2431ccecf261SBill Schmidt 
2432ccecf261SBill Schmidt   const TargetRegisterClass *UseRC =
2433ccecf261SBill Schmidt     (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2434ccecf261SBill Schmidt      (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2435ccecf261SBill Schmidt 
243666506582SNikita Popov   return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC, Op0, Imm);
2437ccecf261SBill Schmidt }
2438ccecf261SBill Schmidt 
2439ccecf261SBill Schmidt // Override for instructions with one register operand to avoid use of
2440ccecf261SBill Schmidt // R0/X0.  The automatic infrastructure isn't aware of the context so
2441ccecf261SBill Schmidt // we must be conservative.
fastEmitInst_r(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0)244288e32517SJuergen Ributzka unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
2443ccecf261SBill Schmidt                                      const TargetRegisterClass* RC,
244466506582SNikita Popov                                      unsigned Op0) {
2445ccecf261SBill Schmidt   const TargetRegisterClass *UseRC =
2446ccecf261SBill Schmidt     (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2447ccecf261SBill Schmidt      (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2448ccecf261SBill Schmidt 
244966506582SNikita Popov   return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0);
2450ccecf261SBill Schmidt }
2451ccecf261SBill Schmidt 
2452ccecf261SBill Schmidt // Override for instructions with two register operands to avoid use
2453ccecf261SBill Schmidt // of R0/X0.  The automatic infrastructure isn't aware of the context
2454ccecf261SBill Schmidt // so we must be conservative.
fastEmitInst_rr(unsigned MachineInstOpcode,const TargetRegisterClass * RC,unsigned Op0,unsigned Op1)245588e32517SJuergen Ributzka unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2456ccecf261SBill Schmidt                                       const TargetRegisterClass* RC,
245766506582SNikita Popov                                       unsigned Op0, unsigned Op1) {
2458ccecf261SBill Schmidt   const TargetRegisterClass *UseRC =
2459ccecf261SBill Schmidt     (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
2460ccecf261SBill Schmidt      (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
2461ccecf261SBill Schmidt 
246266506582SNikita Popov   return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op1);
2463ccecf261SBill Schmidt }
2464ccecf261SBill Schmidt 
24650cf702faSBill Schmidt namespace llvm {
24660cf702faSBill Schmidt   // Create the fast instruction selector for PowerPC64 ELF.
createFastISel(FunctionLoweringInfo & FuncInfo,const TargetLibraryInfo * LibInfo)24670cf702faSBill Schmidt   FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
24680cf702faSBill Schmidt                                 const TargetLibraryInfo *LibInfo) {
2469d0f9553eSQiu Chaofan     // Only available on 64-bit for now.
2470cccae795SEric Christopher     const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
2471d0f9553eSQiu Chaofan     if (Subtarget.isPPC64())
24720cf702faSBill Schmidt       return new PPCFastISel(FuncInfo, LibInfo);
2473062a2baeSCraig Topper     return nullptr;
24740cf702faSBill Schmidt   }
2475f00654e3SAlexander Kornienko }
2476