Home | History | Annotate | Line # | Download | only in PowerPC
      1 //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 //
      9 // This file defines the PowerPC-specific support for the FastISel class. Some
     10 // of the target-specific code is generated by tablegen in the file
     11 // PPCGenFastISel.inc, which is #included here.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "MCTargetDesc/PPCPredicates.h"
     16 #include "PPC.h"
     17 #include "PPCCCState.h"
     18 #include "PPCCallingConv.h"
     19 #include "PPCISelLowering.h"
     20 #include "PPCMachineFunctionInfo.h"
     21 #include "PPCSubtarget.h"
     22 #include "PPCTargetMachine.h"
     23 #include "llvm/ADT/Optional.h"
     24 #include "llvm/CodeGen/CallingConvLower.h"
     25 #include "llvm/CodeGen/FastISel.h"
     26 #include "llvm/CodeGen/FunctionLoweringInfo.h"
     27 #include "llvm/CodeGen/MachineConstantPool.h"
     28 #include "llvm/CodeGen/MachineFrameInfo.h"
     29 #include "llvm/CodeGen/MachineInstrBuilder.h"
     30 #include "llvm/CodeGen/MachineRegisterInfo.h"
     31 #include "llvm/CodeGen/TargetLowering.h"
     32 #include "llvm/IR/CallingConv.h"
     33 #include "llvm/IR/GetElementPtrTypeIterator.h"
     34 #include "llvm/IR/GlobalAlias.h"
     35 #include "llvm/IR/GlobalVariable.h"
     36 #include "llvm/IR/IntrinsicInst.h"
     37 #include "llvm/IR/Operator.h"
     38 #include "llvm/Support/Debug.h"
     39 #include "llvm/Target/TargetMachine.h"
     40 
     41 //===----------------------------------------------------------------------===//
     42 //
     43 // TBD:
     44 //   fastLowerArguments: Handle simple cases.
     45 //   PPCMaterializeGV: Handle TLS.
     46 //   SelectCall: Handle function pointers.
     47 //   SelectCall: Handle multi-register return values.
     48 //   SelectCall: Optimize away nops for local calls.
     49 //   processCallArgs: Handle bit-converted arguments.
     50 //   finishCall: Handle multi-register return values.
     51 //   PPCComputeAddress: Handle parameter references as FrameIndex's.
     52 //   PPCEmitCmp: Handle immediate as operand 1.
     53 //   SelectCall: Handle small byval arguments.
     54 //   SelectIntrinsicCall: Implement.
     55 //   SelectSelect: Implement.
     56 //   Consider factoring isTypeLegal into the base class.
     57 //   Implement switches and jump tables.
     58 //
     59 //===----------------------------------------------------------------------===//
     60 using namespace llvm;
     61 
     62 #define DEBUG_TYPE "ppcfastisel"
     63 
     64 namespace {
     65 
     66 typedef struct Address {
     67   enum {
     68     RegBase,
     69     FrameIndexBase
     70   } BaseType;
     71 
     72   union {
     73     unsigned Reg;
     74     int FI;
     75   } Base;
     76 
     77   long Offset;
     78 
     79   // Innocuous defaults for our address.
     80   Address()
     81    : BaseType(RegBase), Offset(0) {
     82      Base.Reg = 0;
     83    }
     84 } Address;
     85 
     86 class PPCFastISel final : public FastISel {
     87 
     88   const TargetMachine &TM;
     89   const PPCSubtarget *Subtarget;
     90   PPCFunctionInfo *PPCFuncInfo;
     91   const TargetInstrInfo &TII;
     92   const TargetLowering &TLI;
     93   LLVMContext *Context;
     94 
     95   public:
     96     explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
     97                          const TargetLibraryInfo *LibInfo)
     98         : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
     99           Subtarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
    100           PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
    101           TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()),
    102           Context(&FuncInfo.Fn->getContext()) {}
    103 
    104     // Backend specific FastISel code.
    105   private:
    106     bool fastSelectInstruction(const Instruction *I) override;
    107     unsigned fastMaterializeConstant(const Constant *C) override;
    108     unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
    109     bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
    110                              const LoadInst *LI) override;
    111     bool fastLowerArguments() override;
    112     unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
    113     unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
    114                              const TargetRegisterClass *RC,
    115                              unsigned Op0, uint64_t Imm);
    116     unsigned fastEmitInst_r(unsigned MachineInstOpcode,
    117                             const TargetRegisterClass *RC, unsigned Op0);
    118     unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
    119                              const TargetRegisterClass *RC,
    120                              unsigned Op0, unsigned Op1);
    121 
    122     bool fastLowerCall(CallLoweringInfo &CLI) override;
    123 
    124   // Instruction selection routines.
    125   private:
    126     bool SelectLoad(const Instruction *I);
    127     bool SelectStore(const Instruction *I);
    128     bool SelectBranch(const Instruction *I);
    129     bool SelectIndirectBr(const Instruction *I);
    130     bool SelectFPExt(const Instruction *I);
    131     bool SelectFPTrunc(const Instruction *I);
    132     bool SelectIToFP(const Instruction *I, bool IsSigned);
    133     bool SelectFPToI(const Instruction *I, bool IsSigned);
    134     bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
    135     bool SelectRet(const Instruction *I);
    136     bool SelectTrunc(const Instruction *I);
    137     bool SelectIntExt(const Instruction *I);
    138 
    139   // Utility routines.
    140   private:
    141     bool isTypeLegal(Type *Ty, MVT &VT);
    142     bool isLoadTypeLegal(Type *Ty, MVT &VT);
    143     bool isValueAvailable(const Value *V) const;
    144     bool isVSFRCRegClass(const TargetRegisterClass *RC) const {
    145       return RC->getID() == PPC::VSFRCRegClassID;
    146     }
    147     bool isVSSRCRegClass(const TargetRegisterClass *RC) const {
    148       return RC->getID() == PPC::VSSRCRegClassID;
    149     }
    150     unsigned copyRegToRegClass(const TargetRegisterClass *ToRC,
    151                                unsigned SrcReg, unsigned Flag = 0,
    152                                unsigned SubReg = 0) {
    153       unsigned TmpReg = createResultReg(ToRC);
    154       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
    155               TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg, Flag, SubReg);
    156       return TmpReg;
    157     }
    158     bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
    159                     bool isZExt, unsigned DestReg,
    160                     const PPC::Predicate Pred);
    161     bool PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
    162                      const TargetRegisterClass *RC, bool IsZExt = true,
    163                      unsigned FP64LoadOpc = PPC::LFD);
    164     bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
    165     bool PPCComputeAddress(const Value *Obj, Address &Addr);
    166     void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
    167                             unsigned &IndexReg);
    168     bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
    169                            unsigned DestReg, bool IsZExt);
    170     unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
    171     unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
    172     unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
    173                                bool UseSExt = true);
    174     unsigned PPCMaterialize32BitInt(int64_t Imm,
    175                                     const TargetRegisterClass *RC);
    176     unsigned PPCMaterialize64BitInt(int64_t Imm,
    177                                     const TargetRegisterClass *RC);
    178     unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
    179                              unsigned SrcReg, bool IsSigned);
    180     unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
    181 
    182   // Call handling routines.
    183   private:
    184     bool processCallArgs(SmallVectorImpl<Value*> &Args,
    185                          SmallVectorImpl<unsigned> &ArgRegs,
    186                          SmallVectorImpl<MVT> &ArgVTs,
    187                          SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
    188                          SmallVectorImpl<unsigned> &RegArgs,
    189                          CallingConv::ID CC,
    190                          unsigned &NumBytes,
    191                          bool IsVarArg);
    192     bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
    193 
    194   private:
    195   #include "PPCGenFastISel.inc"
    196 
    197 };
    198 
    199 } // end anonymous namespace
    200 
    201 static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
    202   switch (Pred) {
    203     // These are not representable with any single compare.
    204     case CmpInst::FCMP_FALSE:
    205     case CmpInst::FCMP_TRUE:
    206     // Major concern about the following 6 cases is NaN result. The comparison
    207     // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
    208     // only one of which will be set. The result is generated by fcmpu
    209     // instruction. However, bc instruction only inspects one of the first 3
    210     // bits, so when un is set, bc instruction may jump to an undesired
    211     // place.
    212     //
    213     // More specifically, if we expect an unordered comparison and un is set, we
    214     // expect to always go to true branch; in such case UEQ, UGT and ULT still
    215     // give false, which are undesired; but UNE, UGE, ULE happen to give true,
    216     // since they are tested by inspecting !eq, !lt, !gt, respectively.
    217     //
    218     // Similarly, for ordered comparison, when un is set, we always expect the
    219     // result to be false. In such case OGT, OLT and OEQ is good, since they are
    220     // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
    221     // and ONE are tested through !lt, !gt and !eq, and these are true.
    222     case CmpInst::FCMP_UEQ:
    223     case CmpInst::FCMP_UGT:
    224     case CmpInst::FCMP_ULT:
    225     case CmpInst::FCMP_OGE:
    226     case CmpInst::FCMP_OLE:
    227     case CmpInst::FCMP_ONE:
    228     default:
    229       return Optional<PPC::Predicate>();
    230 
    231     case CmpInst::FCMP_OEQ:
    232     case CmpInst::ICMP_EQ:
    233       return PPC::PRED_EQ;
    234 
    235     case CmpInst::FCMP_OGT:
    236     case CmpInst::ICMP_UGT:
    237     case CmpInst::ICMP_SGT:
    238       return PPC::PRED_GT;
    239 
    240     case CmpInst::FCMP_UGE:
    241     case CmpInst::ICMP_UGE:
    242     case CmpInst::ICMP_SGE:
    243       return PPC::PRED_GE;
    244 
    245     case CmpInst::FCMP_OLT:
    246     case CmpInst::ICMP_ULT:
    247     case CmpInst::ICMP_SLT:
    248       return PPC::PRED_LT;
    249 
    250     case CmpInst::FCMP_ULE:
    251     case CmpInst::ICMP_ULE:
    252     case CmpInst::ICMP_SLE:
    253       return PPC::PRED_LE;
    254 
    255     case CmpInst::FCMP_UNE:
    256     case CmpInst::ICMP_NE:
    257       return PPC::PRED_NE;
    258 
    259     case CmpInst::FCMP_ORD:
    260       return PPC::PRED_NU;
    261 
    262     case CmpInst::FCMP_UNO:
    263       return PPC::PRED_UN;
    264   }
    265 }
    266 
    267 // Determine whether the type Ty is simple enough to be handled by
    268 // fast-isel, and return its equivalent machine type in VT.
    269 // FIXME: Copied directly from ARM -- factor into base class?
    270 bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
    271   EVT Evt = TLI.getValueType(DL, Ty, true);
    272 
    273   // Only handle simple types.
    274   if (Evt == MVT::Other || !Evt.isSimple()) return false;
    275   VT = Evt.getSimpleVT();
    276 
    277   // Handle all legal types, i.e. a register that will directly hold this
    278   // value.
    279   return TLI.isTypeLegal(VT);
    280 }
    281 
    282 // Determine whether the type Ty is simple enough to be handled by
    283 // fast-isel as a load target, and return its equivalent machine type in VT.
    284 bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
    285   if (isTypeLegal(Ty, VT)) return true;
    286 
    287   // If this is a type than can be sign or zero-extended to a basic operation
    288   // go ahead and accept it now.
    289   if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
    290     return true;
    291   }
    292 
    293   return false;
    294 }
    295 
    296 bool PPCFastISel::isValueAvailable(const Value *V) const {
    297   if (!isa<Instruction>(V))
    298     return true;
    299 
    300   const auto *I = cast<Instruction>(V);
    301   return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
    302 }
    303 
    304 // Given a value Obj, create an Address object Addr that represents its
    305 // address.  Return false if we can't handle it.
    306 bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
    307   const User *U = nullptr;
    308   unsigned Opcode = Instruction::UserOp1;
    309   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
    310     // Don't walk into other basic blocks unless the object is an alloca from
    311     // another block, otherwise it may not have a virtual register assigned.
    312     if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
    313         FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
    314       Opcode = I->getOpcode();
    315       U = I;
    316     }
    317   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
    318     Opcode = C->getOpcode();
    319     U = C;
    320   }
    321 
    322   switch (Opcode) {
    323     default:
    324       break;
    325     case Instruction::BitCast:
    326       // Look through bitcasts.
    327       return PPCComputeAddress(U->getOperand(0), Addr);
    328     case Instruction::IntToPtr:
    329       // Look past no-op inttoptrs.
    330       if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
    331           TLI.getPointerTy(DL))
    332         return PPCComputeAddress(U->getOperand(0), Addr);
    333       break;
    334     case Instruction::PtrToInt:
    335       // Look past no-op ptrtoints.
    336       if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
    337         return PPCComputeAddress(U->getOperand(0), Addr);
    338       break;
    339     case Instruction::GetElementPtr: {
    340       Address SavedAddr = Addr;
    341       long TmpOffset = Addr.Offset;
    342 
    343       // Iterate through the GEP folding the constants into offsets where
    344       // we can.
    345       gep_type_iterator GTI = gep_type_begin(U);
    346       for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
    347            II != IE; ++II, ++GTI) {
    348         const Value *Op = *II;
    349         if (StructType *STy = GTI.getStructTypeOrNull()) {
    350           const StructLayout *SL = DL.getStructLayout(STy);
    351           unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
    352           TmpOffset += SL->getElementOffset(Idx);
    353         } else {
    354           uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
    355           for (;;) {
    356             if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
    357               // Constant-offset addressing.
    358               TmpOffset += CI->getSExtValue() * S;
    359               break;
    360             }
    361             if (canFoldAddIntoGEP(U, Op)) {
    362               // A compatible add with a constant operand. Fold the constant.
    363               ConstantInt *CI =
    364               cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
    365               TmpOffset += CI->getSExtValue() * S;
    366               // Iterate on the other operand.
    367               Op = cast<AddOperator>(Op)->getOperand(0);
    368               continue;
    369             }
    370             // Unsupported
    371             goto unsupported_gep;
    372           }
    373         }
    374       }
    375 
    376       // Try to grab the base operand now.
    377       Addr.Offset = TmpOffset;
    378       if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
    379 
    380       // We failed, restore everything and try the other options.
    381       Addr = SavedAddr;
    382 
    383       unsupported_gep:
    384       break;
    385     }
    386     case Instruction::Alloca: {
    387       const AllocaInst *AI = cast<AllocaInst>(Obj);
    388       DenseMap<const AllocaInst*, int>::iterator SI =
    389         FuncInfo.StaticAllocaMap.find(AI);
    390       if (SI != FuncInfo.StaticAllocaMap.end()) {
    391         Addr.BaseType = Address::FrameIndexBase;
    392         Addr.Base.FI = SI->second;
    393         return true;
    394       }
    395       break;
    396     }
    397   }
    398 
    399   // FIXME: References to parameters fall through to the behavior
    400   // below.  They should be able to reference a frame index since
    401   // they are stored to the stack, so we can get "ld rx, offset(r1)"
    402   // instead of "addi ry, r1, offset / ld rx, 0(ry)".  Obj will
    403   // just contain the parameter.  Try to handle this with a FI.
    404 
    405   // Try to get this in a register if nothing else has worked.
    406   if (Addr.Base.Reg == 0)
    407     Addr.Base.Reg = getRegForValue(Obj);
    408 
    409   // Prevent assignment of base register to X0, which is inappropriate
    410   // for loads and stores alike.
    411   if (Addr.Base.Reg != 0)
    412     MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
    413 
    414   return Addr.Base.Reg != 0;
    415 }
    416 
    417 // Fix up some addresses that can't be used directly.  For example, if
    418 // an offset won't fit in an instruction field, we may need to move it
    419 // into an index register.
    420 void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
    421                                      unsigned &IndexReg) {
    422 
    423   // Check whether the offset fits in the instruction field.
    424   if (!isInt<16>(Addr.Offset))
    425     UseOffset = false;
    426 
    427   // If this is a stack pointer and the offset needs to be simplified then
    428   // put the alloca address into a register, set the base type back to
    429   // register and continue. This should almost never happen.
    430   if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
    431     unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
    432     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
    433             ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
    434     Addr.Base.Reg = ResultReg;
    435     Addr.BaseType = Address::RegBase;
    436   }
    437 
    438   if (!UseOffset) {
    439     IntegerType *OffsetTy = Type::getInt64Ty(*Context);
    440     const ConstantInt *Offset =
    441       ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset));
    442     IndexReg = PPCMaterializeInt(Offset, MVT::i64);
    443     assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
    444   }
    445 }
    446 
    447 // Emit a load instruction if possible, returning true if we succeeded,
    448 // otherwise false.  See commentary below for how the register class of
    449 // the load is determined.
    450 bool PPCFastISel::PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
    451                               const TargetRegisterClass *RC,
    452                               bool IsZExt, unsigned FP64LoadOpc) {
    453   unsigned Opc;
    454   bool UseOffset = true;
    455   bool HasSPE = Subtarget->hasSPE();
    456 
    457   // If ResultReg is given, it determines the register class of the load.
    458   // Otherwise, RC is the register class to use.  If the result of the
    459   // load isn't anticipated in this block, both may be zero, in which
    460   // case we must make a conservative guess.  In particular, don't assign
    461   // R0 or X0 to the result register, as the result may be used in a load,
    462   // store, add-immediate, or isel that won't permit this.  (Though
    463   // perhaps the spill and reload of live-exit values would handle this?)
    464   const TargetRegisterClass *UseRC =
    465     (ResultReg ? MRI.getRegClass(ResultReg) :
    466      (RC ? RC :
    467       (VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :
    468        (VT == MVT::f32 ? (HasSPE ? &PPC::GPRCRegClass : &PPC::F4RCRegClass) :
    469         (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
    470          &PPC::GPRC_and_GPRC_NOR0RegClass)))));
    471 
    472   bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
    473 
    474   switch (VT.SimpleTy) {
    475     default: // e.g., vector types not handled
    476       return false;
    477     case MVT::i8:
    478       Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
    479       break;
    480     case MVT::i16:
    481       Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
    482                     : (Is32BitInt ? PPC::LHA : PPC::LHA8));
    483       break;
    484     case MVT::i32:
    485       Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
    486                     : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
    487       if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
    488         UseOffset = false;
    489       break;
    490     case MVT::i64:
    491       Opc = PPC::LD;
    492       assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
    493              "64-bit load with 32-bit target??");
    494       UseOffset = ((Addr.Offset & 3) == 0);
    495       break;
    496     case MVT::f32:
    497       Opc = Subtarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
    498       break;
    499     case MVT::f64:
    500       Opc = FP64LoadOpc;
    501       break;
    502   }
    503 
    504   // If necessary, materialize the offset into a register and use
    505   // the indexed form.  Also handle stack pointers with special needs.
    506   unsigned IndexReg = 0;
    507   PPCSimplifyAddress(Addr, UseOffset, IndexReg);
    508 
    509   // If this is a potential VSX load with an offset of 0, a VSX indexed load can
    510   // be used.
    511   bool IsVSSRC = isVSSRCRegClass(UseRC);
    512   bool IsVSFRC = isVSFRCRegClass(UseRC);
    513   bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
    514   bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
    515   if ((Is32VSXLoad || Is64VSXLoad) &&
    516       (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
    517       (Addr.Offset == 0)) {
    518     UseOffset = false;
    519   }
    520 
    521   if (ResultReg == 0)
    522     ResultReg = createResultReg(UseRC);
    523 
    524   // Note: If we still have a frame index here, we know the offset is
    525   // in range, as otherwise PPCSimplifyAddress would have converted it
    526   // into a RegBase.
    527   if (Addr.BaseType == Address::FrameIndexBase) {
    528     // VSX only provides an indexed load.
    529     if (Is32VSXLoad || Is64VSXLoad) return false;
    530 
    531     MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
    532         MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
    533                                           Addr.Offset),
    534         MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
    535         MFI.getObjectAlign(Addr.Base.FI));
    536 
    537     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
    538       .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
    539 
    540   // Base reg with offset in range.
    541   } else if (UseOffset) {
    542     // VSX only provides an indexed load.
    543     if (Is32VSXLoad || Is64VSXLoad) return false;
    544 
    545     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
    546       .addImm(Addr.Offset).addReg(Addr.Base.Reg);
    547 
    548   // Indexed form.
    549   } else {
    550     // Get the RR opcode corresponding to the RI one.  FIXME: It would be
    551     // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
    552     // is hard to get at.
    553     switch (Opc) {
    554       default:        llvm_unreachable("Unexpected opcode!");
    555       case PPC::LBZ:    Opc = PPC::LBZX;    break;
    556       case PPC::LBZ8:   Opc = PPC::LBZX8;   break;
    557       case PPC::LHZ:    Opc = PPC::LHZX;    break;
    558       case PPC::LHZ8:   Opc = PPC::LHZX8;   break;
    559       case PPC::LHA:    Opc = PPC::LHAX;    break;
    560       case PPC::LHA8:   Opc = PPC::LHAX8;   break;
    561       case PPC::LWZ:    Opc = PPC::LWZX;    break;
    562       case PPC::LWZ8:   Opc = PPC::LWZX8;   break;
    563       case PPC::LWA:    Opc = PPC::LWAX;    break;
    564       case PPC::LWA_32: Opc = PPC::LWAX_32; break;
    565       case PPC::LD:     Opc = PPC::LDX;     break;
    566       case PPC::LFS:    Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
    567       case PPC::LFD:    Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
    568       case PPC::EVLDD:  Opc = PPC::EVLDDX;  break;
    569       case PPC::SPELWZ: Opc = PPC::SPELWZX;    break;
    570     }
    571 
    572     auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
    573                        ResultReg);
    574 
    575     // If we have an index register defined we use it in the store inst,
    576     // otherwise we use X0 as base as it makes the vector instructions to
    577     // use zero in the computation of the effective address regardless the
    578     // content of the register.
    579     if (IndexReg)
    580       MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
    581     else
    582       MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
    583   }
    584 
    585   return true;
    586 }
    587 
    588 // Attempt to fast-select a load instruction.
    589 bool PPCFastISel::SelectLoad(const Instruction *I) {
    590   // FIXME: No atomic loads are supported.
    591   if (cast<LoadInst>(I)->isAtomic())
    592     return false;
    593 
    594   // Verify we have a legal type before going any further.
    595   MVT VT;
    596   if (!isLoadTypeLegal(I->getType(), VT))
    597     return false;
    598 
    599   // See if we can handle this address.
    600   Address Addr;
    601   if (!PPCComputeAddress(I->getOperand(0), Addr))
    602     return false;
    603 
    604   // Look at the currently assigned register for this instruction
    605   // to determine the required register class.  This is necessary
    606   // to constrain RA from using R0/X0 when this is not legal.
    607   unsigned AssignedReg = FuncInfo.ValueMap[I];
    608   const TargetRegisterClass *RC =
    609     AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
    610 
    611   Register ResultReg = 0;
    612   if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
    613                    Subtarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
    614     return false;
    615   updateValueMap(I, ResultReg);
    616   return true;
    617 }
    618 
    619 // Emit a store instruction to store SrcReg at Addr.
    620 bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
    621   assert(SrcReg && "Nothing to store!");
    622   unsigned Opc;
    623   bool UseOffset = true;
    624 
    625   const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
    626   bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
    627 
    628   switch (VT.SimpleTy) {
    629     default: // e.g., vector types not handled
    630       return false;
    631     case MVT::i8:
    632       Opc = Is32BitInt ? PPC::STB : PPC::STB8;
    633       break;
    634     case MVT::i16:
    635       Opc = Is32BitInt ? PPC::STH : PPC::STH8;
    636       break;
    637     case MVT::i32:
    638       assert(Is32BitInt && "Not GPRC for i32??");
    639       Opc = PPC::STW;
    640       break;
    641     case MVT::i64:
    642       Opc = PPC::STD;
    643       UseOffset = ((Addr.Offset & 3) == 0);
    644       break;
    645     case MVT::f32:
    646       Opc = Subtarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
    647       break;
    648     case MVT::f64:
    649       Opc = Subtarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
    650       break;
    651   }
    652 
    653   // If necessary, materialize the offset into a register and use
    654   // the indexed form.  Also handle stack pointers with special needs.
    655   unsigned IndexReg = 0;
    656   PPCSimplifyAddress(Addr, UseOffset, IndexReg);
    657 
    658   // If this is a potential VSX store with an offset of 0, a VSX indexed store
    659   // can be used.
    660   bool IsVSSRC = isVSSRCRegClass(RC);
    661   bool IsVSFRC = isVSFRCRegClass(RC);
    662   bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
    663   bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
    664   if ((Is32VSXStore || Is64VSXStore) &&
    665       (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
    666       (Addr.Offset == 0)) {
    667     UseOffset = false;
    668   }
    669 
    670   // Note: If we still have a frame index here, we know the offset is
    671   // in range, as otherwise PPCSimplifyAddress would have converted it
    672   // into a RegBase.
    673   if (Addr.BaseType == Address::FrameIndexBase) {
    674     // VSX only provides an indexed store.
    675     if (Is32VSXStore || Is64VSXStore) return false;
    676 
    677     MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
    678         MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
    679                                           Addr.Offset),
    680         MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
    681         MFI.getObjectAlign(Addr.Base.FI));
    682 
    683     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
    684         .addReg(SrcReg)
    685         .addImm(Addr.Offset)
    686         .addFrameIndex(Addr.Base.FI)
    687         .addMemOperand(MMO);
    688 
    689   // Base reg with offset in range.
    690   } else if (UseOffset) {
    691     // VSX only provides an indexed store.
    692     if (Is32VSXStore || Is64VSXStore)
    693       return false;
    694 
    695     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
    696       .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
    697 
    698   // Indexed form.
    699   } else {
    700     // Get the RR opcode corresponding to the RI one.  FIXME: It would be
    701     // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
    702     // is hard to get at.
    703     switch (Opc) {
    704       default:        llvm_unreachable("Unexpected opcode!");
    705       case PPC::STB:  Opc = PPC::STBX;  break;
    706       case PPC::STH : Opc = PPC::STHX;  break;
    707       case PPC::STW : Opc = PPC::STWX;  break;
    708       case PPC::STB8: Opc = PPC::STBX8; break;
    709       case PPC::STH8: Opc = PPC::STHX8; break;
    710       case PPC::STW8: Opc = PPC::STWX8; break;
    711       case PPC::STD:  Opc = PPC::STDX;  break;
    712       case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
    713       case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
    714       case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;
    715       case PPC::SPESTW: Opc = PPC::SPESTWX; break;
    716     }
    717 
    718     auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
    719         .addReg(SrcReg);
    720 
    721     // If we have an index register defined we use it in the store inst,
    722     // otherwise we use X0 as base as it makes the vector instructions to
    723     // use zero in the computation of the effective address regardless the
    724     // content of the register.
    725     if (IndexReg)
    726       MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
    727     else
    728       MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
    729   }
    730 
    731   return true;
    732 }
    733 
    734 // Attempt to fast-select a store instruction.
    735 bool PPCFastISel::SelectStore(const Instruction *I) {
    736   Value *Op0 = I->getOperand(0);
    737   unsigned SrcReg = 0;
    738 
    739   // FIXME: No atomics loads are supported.
    740   if (cast<StoreInst>(I)->isAtomic())
    741     return false;
    742 
    743   // Verify we have a legal type before going any further.
    744   MVT VT;
    745   if (!isLoadTypeLegal(Op0->getType(), VT))
    746     return false;
    747 
    748   // Get the value to be stored into a register.
    749   SrcReg = getRegForValue(Op0);
    750   if (SrcReg == 0)
    751     return false;
    752 
    753   // See if we can handle this address.
    754   Address Addr;
    755   if (!PPCComputeAddress(I->getOperand(1), Addr))
    756     return false;
    757 
    758   if (!PPCEmitStore(VT, SrcReg, Addr))
    759     return false;
    760 
    761   return true;
    762 }
    763 
    764 // Attempt to fast-select a branch instruction.
    765 bool PPCFastISel::SelectBranch(const Instruction *I) {
    766   const BranchInst *BI = cast<BranchInst>(I);
    767   MachineBasicBlock *BrBB = FuncInfo.MBB;
    768   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
    769   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
    770 
    771   // For now, just try the simplest case where it's fed by a compare.
    772   if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
    773     if (isValueAvailable(CI)) {
    774       Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
    775       if (!OptPPCPred)
    776         return false;
    777 
    778       PPC::Predicate PPCPred = OptPPCPred.getValue();
    779 
    780       // Take advantage of fall-through opportunities.
    781       if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
    782         std::swap(TBB, FBB);
    783         PPCPred = PPC::InvertPredicate(PPCPred);
    784       }
    785 
    786       unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
    787 
    788       if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
    789                       CondReg, PPCPred))
    790         return false;
    791 
    792       BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
    793           .addImm(Subtarget->hasSPE() ? PPC::PRED_SPE : PPCPred)
    794           .addReg(CondReg)
    795           .addMBB(TBB);
    796       finishCondBranch(BI->getParent(), TBB, FBB);
    797       return true;
    798     }
    799   } else if (const ConstantInt *CI =
    800              dyn_cast<ConstantInt>(BI->getCondition())) {
    801     uint64_t Imm = CI->getZExtValue();
    802     MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
    803     fastEmitBranch(Target, DbgLoc);
    804     return true;
    805   }
    806 
    807   // FIXME: ARM looks for a case where the block containing the compare
    808   // has been split from the block containing the branch.  If this happens,
    809   // there is a vreg available containing the result of the compare.  I'm
    810   // not sure we can do much, as we've lost the predicate information with
    811   // the compare instruction -- we have a 4-bit CR but don't know which bit
    812   // to test here.
    813   return false;
    814 }
    815 
    816 // Attempt to emit a compare of the two source values.  Signed and unsigned
    817 // comparisons are supported.  Return false if we can't handle it.
    818 bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
    819                              bool IsZExt, unsigned DestReg,
    820                              const PPC::Predicate Pred) {
    821   Type *Ty = SrcValue1->getType();
    822   EVT SrcEVT = TLI.getValueType(DL, Ty, true);
    823   if (!SrcEVT.isSimple())
    824     return false;
    825   MVT SrcVT = SrcEVT.getSimpleVT();
    826 
    827   if (SrcVT == MVT::i1 && Subtarget->useCRBits())
    828     return false;
    829 
    830   // See if operand 2 is an immediate encodeable in the compare.
    831   // FIXME: Operands are not in canonical order at -O0, so an immediate
    832   // operand in position 1 is a lost opportunity for now.  We are
    833   // similar to ARM in this regard.
    834   long Imm = 0;
    835   bool UseImm = false;
    836   const bool HasSPE = Subtarget->hasSPE();
    837 
    838   // Only 16-bit integer constants can be represented in compares for
    839   // PowerPC.  Others will be materialized into a register.
    840   if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
    841     if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
    842         SrcVT == MVT::i8 || SrcVT == MVT::i1) {
    843       const APInt &CIVal = ConstInt->getValue();
    844       Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue();
    845       if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
    846         UseImm = true;
    847     }
    848   }
    849 
    850   unsigned SrcReg1 = getRegForValue(SrcValue1);
    851   if (SrcReg1 == 0)
    852     return false;
    853 
    854   unsigned SrcReg2 = 0;
    855   if (!UseImm) {
    856     SrcReg2 = getRegForValue(SrcValue2);
    857     if (SrcReg2 == 0)
    858       return false;
    859   }
    860 
    861   unsigned CmpOpc;
    862   bool NeedsExt = false;
    863 
    864   auto RC1 = MRI.getRegClass(SrcReg1);
    865   auto RC2 = SrcReg2 != 0 ? MRI.getRegClass(SrcReg2) : nullptr;
    866 
    867   switch (SrcVT.SimpleTy) {
    868     default: return false;
    869     case MVT::f32:
    870       if (HasSPE) {
    871         switch (Pred) {
    872           default: return false;
    873           case PPC::PRED_EQ:
    874             CmpOpc = PPC::EFSCMPEQ;
    875             break;
    876           case PPC::PRED_LT:
    877             CmpOpc = PPC::EFSCMPLT;
    878             break;
    879           case PPC::PRED_GT:
    880             CmpOpc = PPC::EFSCMPGT;
    881             break;
    882         }
    883       } else {
    884         CmpOpc = PPC::FCMPUS;
    885         if (isVSSRCRegClass(RC1))
    886           SrcReg1 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg1);
    887         if (RC2 && isVSSRCRegClass(RC2))
    888           SrcReg2 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg2);
    889       }
    890       break;
    891     case MVT::f64:
    892       if (HasSPE) {
    893         switch (Pred) {
    894           default: return false;
    895           case PPC::PRED_EQ:
    896             CmpOpc = PPC::EFDCMPEQ;
    897             break;
    898           case PPC::PRED_LT:
    899             CmpOpc = PPC::EFDCMPLT;
    900             break;
    901           case PPC::PRED_GT:
    902             CmpOpc = PPC::EFDCMPGT;
    903             break;
    904         }
    905       } else if (isVSFRCRegClass(RC1) || (RC2 && isVSFRCRegClass(RC2))) {
    906         CmpOpc = PPC::XSCMPUDP;
    907       } else {
    908         CmpOpc = PPC::FCMPUD;
    909       }
    910       break;
    911     case MVT::i1:
    912     case MVT::i8:
    913     case MVT::i16:
    914       NeedsExt = true;
    915       LLVM_FALLTHROUGH;
    916     case MVT::i32:
    917       if (!UseImm)
    918         CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
    919       else
    920         CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
    921       break;
    922     case MVT::i64:
    923       if (!UseImm)
    924         CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
    925       else
    926         CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
    927       break;
    928   }
    929 
    930   if (NeedsExt) {
    931     unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
    932     if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
    933       return false;
    934     SrcReg1 = ExtReg;
    935 
    936     if (!UseImm) {
    937       unsigned ExtReg = createResultReg(&PPC::GPRCRegClass);
    938       if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
    939         return false;
    940       SrcReg2 = ExtReg;
    941     }
    942   }
    943 
    944   if (!UseImm)
    945     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
    946       .addReg(SrcReg1).addReg(SrcReg2);
    947   else
    948     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg)
    949       .addReg(SrcReg1).addImm(Imm);
    950 
    951   return true;
    952 }
    953 
    954 // Attempt to fast-select a floating-point extend instruction.
    955 bool PPCFastISel::SelectFPExt(const Instruction *I) {
    956   Value *Src  = I->getOperand(0);
    957   EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
    958   EVT DestVT = TLI.getValueType(DL, I->getType(), true);
    959 
    960   if (SrcVT != MVT::f32 || DestVT != MVT::f64)
    961     return false;
    962 
    963   unsigned SrcReg = getRegForValue(Src);
    964   if (!SrcReg)
    965     return false;
    966 
    967   // No code is generated for a FP extend.
    968   updateValueMap(I, SrcReg);
    969   return true;
    970 }
    971 
    972 // Attempt to fast-select a floating-point truncate instruction.
    973 bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
    974   Value *Src  = I->getOperand(0);
    975   EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
    976   EVT DestVT = TLI.getValueType(DL, I->getType(), true);
    977 
    978   if (SrcVT != MVT::f64 || DestVT != MVT::f32)
    979     return false;
    980 
    981   unsigned SrcReg = getRegForValue(Src);
    982   if (!SrcReg)
    983     return false;
    984 
    985   // Round the result to single precision.
    986   unsigned DestReg;
    987   auto RC = MRI.getRegClass(SrcReg);
    988   if (Subtarget->hasSPE()) {
    989     DestReg = createResultReg(&PPC::GPRCRegClass);
    990     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
    991       TII.get(PPC::EFSCFD), DestReg)
    992       .addReg(SrcReg);
    993   } else if (isVSFRCRegClass(RC)) {
    994     DestReg = createResultReg(&PPC::VSSRCRegClass);
    995     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
    996       TII.get(PPC::XSRSP), DestReg)
    997       .addReg(SrcReg);
    998   } else {
    999     DestReg = createResultReg(&PPC::F4RCRegClass);
   1000     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1001       TII.get(PPC::FRSP), DestReg)
   1002       .addReg(SrcReg);
   1003   }
   1004 
   1005   updateValueMap(I, DestReg);
   1006   return true;
   1007 }
   1008 
   1009 // Move an i32 or i64 value in a GPR to an f64 value in an FPR.
   1010 // FIXME: When direct register moves are implemented (see PowerISA 2.07),
   1011 // those should be used instead of moving via a stack slot when the
   1012 // subtarget permits.
   1013 // FIXME: The code here is sloppy for the 4-byte case.  Can use a 4-byte
   1014 // stack slot and 4-byte store/load sequence.  Or just sext the 4-byte
   1015 // case to 8 bytes which produces tighter code but wastes stack space.
   1016 unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
   1017                                      bool IsSigned) {
   1018 
   1019   // If necessary, extend 32-bit int to 64-bit.
   1020   if (SrcVT == MVT::i32) {
   1021     unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
   1022     if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
   1023       return 0;
   1024     SrcReg = TmpReg;
   1025   }
   1026 
   1027   // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
   1028   Address Addr;
   1029   Addr.BaseType = Address::FrameIndexBase;
   1030   Addr.Base.FI = MFI.CreateStackObject(8, Align(8), false);
   1031 
   1032   // Store the value from the GPR.
   1033   if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
   1034     return 0;
   1035 
   1036   // Load the integer value into an FPR.  The kind of load used depends
   1037   // on a number of conditions.
   1038   unsigned LoadOpc = PPC::LFD;
   1039 
   1040   if (SrcVT == MVT::i32) {
   1041     if (!IsSigned) {
   1042       LoadOpc = PPC::LFIWZX;
   1043       Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
   1044     } else if (Subtarget->hasLFIWAX()) {
   1045       LoadOpc = PPC::LFIWAX;
   1046       Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
   1047     }
   1048   }
   1049 
   1050   const TargetRegisterClass *RC = &PPC::F8RCRegClass;
   1051   Register ResultReg = 0;
   1052   if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
   1053     return 0;
   1054 
   1055   return ResultReg;
   1056 }
   1057 
   1058 // Attempt to fast-select an integer-to-floating-point conversion.
   1059 // FIXME: Once fast-isel has better support for VSX, conversions using
   1060 //        direct moves should be implemented.
   1061 bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
   1062   MVT DstVT;
   1063   Type *DstTy = I->getType();
   1064   if (!isTypeLegal(DstTy, DstVT))
   1065     return false;
   1066 
   1067   if (DstVT != MVT::f32 && DstVT != MVT::f64)
   1068     return false;
   1069 
   1070   Value *Src = I->getOperand(0);
   1071   EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
   1072   if (!SrcEVT.isSimple())
   1073     return false;
   1074 
   1075   MVT SrcVT = SrcEVT.getSimpleVT();
   1076 
   1077   if (SrcVT != MVT::i8  && SrcVT != MVT::i16 &&
   1078       SrcVT != MVT::i32 && SrcVT != MVT::i64)
   1079     return false;
   1080 
   1081   unsigned SrcReg = getRegForValue(Src);
   1082   if (SrcReg == 0)
   1083     return false;
   1084 
   1085   // Shortcut for SPE.  Doesn't need to store/load, since it's all in the GPRs
   1086   if (Subtarget->hasSPE()) {
   1087     unsigned Opc;
   1088     if (DstVT == MVT::f32)
   1089       Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
   1090     else
   1091       Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
   1092 
   1093     unsigned DestReg = createResultReg(&PPC::SPERCRegClass);
   1094     // Generate the convert.
   1095     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
   1096       .addReg(SrcReg);
   1097     updateValueMap(I, DestReg);
   1098     return true;
   1099   }
   1100 
   1101   // We can only lower an unsigned convert if we have the newer
   1102   // floating-point conversion operations.
   1103   if (!IsSigned && !Subtarget->hasFPCVT())
   1104     return false;
   1105 
   1106   // FIXME: For now we require the newer floating-point conversion operations
   1107   // (which are present only on P7 and A2 server models) when converting
   1108   // to single-precision float.  Otherwise we have to generate a lot of
   1109   // fiddly code to avoid double rounding.  If necessary, the fiddly code
   1110   // can be found in PPCTargetLowering::LowerINT_TO_FP().
   1111   if (DstVT == MVT::f32 && !Subtarget->hasFPCVT())
   1112     return false;
   1113 
   1114   // Extend the input if necessary.
   1115   if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
   1116     unsigned TmpReg = createResultReg(&PPC::G8RCRegClass);
   1117     if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
   1118       return false;
   1119     SrcVT = MVT::i64;
   1120     SrcReg = TmpReg;
   1121   }
   1122 
   1123   // Move the integer value to an FPR.
   1124   unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
   1125   if (FPReg == 0)
   1126     return false;
   1127 
   1128   // Determine the opcode for the conversion.
   1129   const TargetRegisterClass *RC = &PPC::F8RCRegClass;
   1130   unsigned DestReg = createResultReg(RC);
   1131   unsigned Opc;
   1132 
   1133   if (DstVT == MVT::f32)
   1134     Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
   1135   else
   1136     Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
   1137 
   1138   // Generate the convert.
   1139   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
   1140     .addReg(FPReg);
   1141 
   1142   updateValueMap(I, DestReg);
   1143   return true;
   1144 }
   1145 
   1146 // Move the floating-point value in SrcReg into an integer destination
   1147 // register, and return the register (or zero if we can't handle it).
   1148 // FIXME: When direct register moves are implemented (see PowerISA 2.07),
   1149 // those should be used instead of moving via a stack slot when the
   1150 // subtarget permits.
   1151 unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
   1152                                       unsigned SrcReg, bool IsSigned) {
   1153   // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
   1154   // Note that if have STFIWX available, we could use a 4-byte stack
   1155   // slot for i32, but this being fast-isel we'll just go with the
   1156   // easiest code gen possible.
   1157   Address Addr;
   1158   Addr.BaseType = Address::FrameIndexBase;
   1159   Addr.Base.FI = MFI.CreateStackObject(8, Align(8), false);
   1160 
   1161   // Store the value from the FPR.
   1162   if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
   1163     return 0;
   1164 
   1165   // Reload it into a GPR.  If we want an i32 on big endian, modify the
   1166   // address to have a 4-byte offset so we load from the right place.
   1167   if (VT == MVT::i32)
   1168     Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
   1169 
   1170   // Look at the currently assigned register for this instruction
   1171   // to determine the required register class.
   1172   unsigned AssignedReg = FuncInfo.ValueMap[I];
   1173   const TargetRegisterClass *RC =
   1174     AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
   1175 
   1176   Register ResultReg = 0;
   1177   if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
   1178     return 0;
   1179 
   1180   return ResultReg;
   1181 }
   1182 
   1183 // Attempt to fast-select a floating-point-to-integer conversion.
   1184 // FIXME: Once fast-isel has better support for VSX, conversions using
   1185 //        direct moves should be implemented.
   1186 bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
   1187   MVT DstVT, SrcVT;
   1188   Type *DstTy = I->getType();
   1189   if (!isTypeLegal(DstTy, DstVT))
   1190     return false;
   1191 
   1192   if (DstVT != MVT::i32 && DstVT != MVT::i64)
   1193     return false;
   1194 
   1195   // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
   1196   if (DstVT == MVT::i64 && !IsSigned && !Subtarget->hasFPCVT() &&
   1197       !Subtarget->hasSPE())
   1198     return false;
   1199 
   1200   Value *Src = I->getOperand(0);
   1201   Type *SrcTy = Src->getType();
   1202   if (!isTypeLegal(SrcTy, SrcVT))
   1203     return false;
   1204 
   1205   if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
   1206     return false;
   1207 
   1208   unsigned SrcReg = getRegForValue(Src);
   1209   if (SrcReg == 0)
   1210     return false;
   1211 
   1212   // Convert f32 to f64 or convert VSSRC to VSFRC if necessary. This is just a
   1213   // meaningless copy to get the register class right.
   1214   const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
   1215   if (InRC == &PPC::F4RCRegClass)
   1216     SrcReg = copyRegToRegClass(&PPC::F8RCRegClass, SrcReg);
   1217   else if (InRC == &PPC::VSSRCRegClass)
   1218     SrcReg = copyRegToRegClass(&PPC::VSFRCRegClass, SrcReg);
   1219 
   1220   // Determine the opcode for the conversion, which takes place
   1221   // entirely within FPRs or VSRs.
   1222   unsigned DestReg;
   1223   unsigned Opc;
   1224   auto RC = MRI.getRegClass(SrcReg);
   1225 
   1226   if (Subtarget->hasSPE()) {
   1227     DestReg = createResultReg(&PPC::GPRCRegClass);
   1228     if (IsSigned)
   1229       Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
   1230     else
   1231       Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
   1232   } else if (isVSFRCRegClass(RC)) {
   1233     DestReg = createResultReg(&PPC::VSFRCRegClass);
   1234     if (DstVT == MVT::i32)
   1235       Opc = IsSigned ? PPC::XSCVDPSXWS : PPC::XSCVDPUXWS;
   1236     else
   1237       Opc = IsSigned ? PPC::XSCVDPSXDS : PPC::XSCVDPUXDS;
   1238   } else {
   1239     DestReg = createResultReg(&PPC::F8RCRegClass);
   1240     if (DstVT == MVT::i32)
   1241       if (IsSigned)
   1242         Opc = PPC::FCTIWZ;
   1243       else
   1244         Opc = Subtarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
   1245     else
   1246       Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
   1247   }
   1248 
   1249   // Generate the convert.
   1250   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
   1251     .addReg(SrcReg);
   1252 
   1253   // Now move the integer value from a float register to an integer register.
   1254   unsigned IntReg = Subtarget->hasSPE()
   1255                         ? DestReg
   1256                         : PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
   1257 
   1258   if (IntReg == 0)
   1259     return false;
   1260 
   1261   updateValueMap(I, IntReg);
   1262   return true;
   1263 }
   1264 
   1265 // Attempt to fast-select a binary integer operation that isn't already
   1266 // handled automatically.
   1267 bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
   1268   EVT DestVT = TLI.getValueType(DL, I->getType(), true);
   1269 
   1270   // We can get here in the case when we have a binary operation on a non-legal
   1271   // type and the target independent selector doesn't know how to handle it.
   1272   if (DestVT != MVT::i16 && DestVT != MVT::i8)
   1273     return false;
   1274 
   1275   // Look at the currently assigned register for this instruction
   1276   // to determine the required register class.  If there is no register,
   1277   // make a conservative choice (don't assign R0).
   1278   unsigned AssignedReg = FuncInfo.ValueMap[I];
   1279   const TargetRegisterClass *RC =
   1280     (AssignedReg ? MRI.getRegClass(AssignedReg) :
   1281      &PPC::GPRC_and_GPRC_NOR0RegClass);
   1282   bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
   1283 
   1284   unsigned Opc;
   1285   switch (ISDOpcode) {
   1286     default: return false;
   1287     case ISD::ADD:
   1288       Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
   1289       break;
   1290     case ISD::OR:
   1291       Opc = IsGPRC ? PPC::OR : PPC::OR8;
   1292       break;
   1293     case ISD::SUB:
   1294       Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
   1295       break;
   1296   }
   1297 
   1298   unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
   1299   unsigned SrcReg1 = getRegForValue(I->getOperand(0));
   1300   if (SrcReg1 == 0) return false;
   1301 
   1302   // Handle case of small immediate operand.
   1303   if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
   1304     const APInt &CIVal = ConstInt->getValue();
   1305     int Imm = (int)CIVal.getSExtValue();
   1306     bool UseImm = true;
   1307     if (isInt<16>(Imm)) {
   1308       switch (Opc) {
   1309         default:
   1310           llvm_unreachable("Missing case!");
   1311         case PPC::ADD4:
   1312           Opc = PPC::ADDI;
   1313           MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
   1314           break;
   1315         case PPC::ADD8:
   1316           Opc = PPC::ADDI8;
   1317           MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
   1318           break;
   1319         case PPC::OR:
   1320           Opc = PPC::ORI;
   1321           break;
   1322         case PPC::OR8:
   1323           Opc = PPC::ORI8;
   1324           break;
   1325         case PPC::SUBF:
   1326           if (Imm == -32768)
   1327             UseImm = false;
   1328           else {
   1329             Opc = PPC::ADDI;
   1330             MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
   1331             Imm = -Imm;
   1332           }
   1333           break;
   1334         case PPC::SUBF8:
   1335           if (Imm == -32768)
   1336             UseImm = false;
   1337           else {
   1338             Opc = PPC::ADDI8;
   1339             MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
   1340             Imm = -Imm;
   1341           }
   1342           break;
   1343       }
   1344 
   1345       if (UseImm) {
   1346         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
   1347                 ResultReg)
   1348             .addReg(SrcReg1)
   1349             .addImm(Imm);
   1350         updateValueMap(I, ResultReg);
   1351         return true;
   1352       }
   1353     }
   1354   }
   1355 
   1356   // Reg-reg case.
   1357   unsigned SrcReg2 = getRegForValue(I->getOperand(1));
   1358   if (SrcReg2 == 0) return false;
   1359 
   1360   // Reverse operands for subtract-from.
   1361   if (ISDOpcode == ISD::SUB)
   1362     std::swap(SrcReg1, SrcReg2);
   1363 
   1364   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
   1365     .addReg(SrcReg1).addReg(SrcReg2);
   1366   updateValueMap(I, ResultReg);
   1367   return true;
   1368 }
   1369 
   1370 // Handle arguments to a call that we're attempting to fast-select.
   1371 // Return false if the arguments are too complex for us at the moment.
   1372 bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
   1373                                   SmallVectorImpl<unsigned> &ArgRegs,
   1374                                   SmallVectorImpl<MVT> &ArgVTs,
   1375                                   SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
   1376                                   SmallVectorImpl<unsigned> &RegArgs,
   1377                                   CallingConv::ID CC,
   1378                                   unsigned &NumBytes,
   1379                                   bool IsVarArg) {
   1380   SmallVector<CCValAssign, 16> ArgLocs;
   1381   CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
   1382 
   1383   // Reserve space for the linkage area on the stack.
   1384   unsigned LinkageSize = Subtarget->getFrameLowering()->getLinkageSize();
   1385   CCInfo.AllocateStack(LinkageSize, Align(8));
   1386 
   1387   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
   1388 
   1389   // Bail out if we can't handle any of the arguments.
   1390   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
   1391     CCValAssign &VA = ArgLocs[I];
   1392     MVT ArgVT = ArgVTs[VA.getValNo()];
   1393 
   1394     // Skip vector arguments for now, as well as long double and
   1395     // uint128_t, and anything that isn't passed in a register.
   1396     if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
   1397         !VA.isRegLoc() || VA.needsCustom())
   1398       return false;
   1399 
   1400     // Skip bit-converted arguments for now.
   1401     if (VA.getLocInfo() == CCValAssign::BCvt)
   1402       return false;
   1403   }
   1404 
   1405   // Get a count of how many bytes are to be pushed onto the stack.
   1406   NumBytes = CCInfo.getNextStackOffset();
   1407 
   1408   // The prolog code of the callee may store up to 8 GPR argument registers to
   1409   // the stack, allowing va_start to index over them in memory if its varargs.
   1410   // Because we cannot tell if this is needed on the caller side, we have to
   1411   // conservatively assume that it is needed.  As such, make sure we have at
   1412   // least enough stack space for the caller to store the 8 GPRs.
   1413   // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
   1414   NumBytes = std::max(NumBytes, LinkageSize + 64);
   1415 
   1416   // Issue CALLSEQ_START.
   1417   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1418           TII.get(TII.getCallFrameSetupOpcode()))
   1419     .addImm(NumBytes).addImm(0);
   1420 
   1421   // Prepare to assign register arguments.  Every argument uses up a
   1422   // GPR protocol register even if it's passed in a floating-point
   1423   // register (unless we're using the fast calling convention).
   1424   unsigned NextGPR = PPC::X3;
   1425   unsigned NextFPR = PPC::F1;
   1426 
   1427   // Process arguments.
   1428   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
   1429     CCValAssign &VA = ArgLocs[I];
   1430     unsigned Arg = ArgRegs[VA.getValNo()];
   1431     MVT ArgVT = ArgVTs[VA.getValNo()];
   1432 
   1433     // Handle argument promotion and bitcasts.
   1434     switch (VA.getLocInfo()) {
   1435       default:
   1436         llvm_unreachable("Unknown loc info!");
   1437       case CCValAssign::Full:
   1438         break;
   1439       case CCValAssign::SExt: {
   1440         MVT DestVT = VA.getLocVT();
   1441         const TargetRegisterClass *RC =
   1442           (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
   1443         unsigned TmpReg = createResultReg(RC);
   1444         if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
   1445           llvm_unreachable("Failed to emit a sext!");
   1446         ArgVT = DestVT;
   1447         Arg = TmpReg;
   1448         break;
   1449       }
   1450       case CCValAssign::AExt:
   1451       case CCValAssign::ZExt: {
   1452         MVT DestVT = VA.getLocVT();
   1453         const TargetRegisterClass *RC =
   1454           (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
   1455         unsigned TmpReg = createResultReg(RC);
   1456         if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
   1457           llvm_unreachable("Failed to emit a zext!");
   1458         ArgVT = DestVT;
   1459         Arg = TmpReg;
   1460         break;
   1461       }
   1462       case CCValAssign::BCvt: {
   1463         // FIXME: Not yet handled.
   1464         llvm_unreachable("Should have bailed before getting here!");
   1465         break;
   1466       }
   1467     }
   1468 
   1469     // Copy this argument to the appropriate register.
   1470     unsigned ArgReg;
   1471     if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
   1472       ArgReg = NextFPR++;
   1473       if (CC != CallingConv::Fast)
   1474         ++NextGPR;
   1475     } else
   1476       ArgReg = NextGPR++;
   1477 
   1478     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1479             TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
   1480     RegArgs.push_back(ArgReg);
   1481   }
   1482 
   1483   return true;
   1484 }
   1485 
   1486 // For a call that we've determined we can fast-select, finish the
   1487 // call sequence and generate a copy to obtain the return value (if any).
   1488 bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
   1489   CallingConv::ID CC = CLI.CallConv;
   1490 
   1491   // Issue CallSEQ_END.
   1492   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1493           TII.get(TII.getCallFrameDestroyOpcode()))
   1494     .addImm(NumBytes).addImm(0);
   1495 
   1496   // Next, generate a copy to obtain the return value.
   1497   // FIXME: No multi-register return values yet, though I don't foresee
   1498   // any real difficulties there.
   1499   if (RetVT != MVT::isVoid) {
   1500     SmallVector<CCValAssign, 16> RVLocs;
   1501     CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
   1502     CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
   1503     CCValAssign &VA = RVLocs[0];
   1504     assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
   1505     assert(VA.isRegLoc() && "Can only return in registers!");
   1506 
   1507     MVT DestVT = VA.getValVT();
   1508     MVT CopyVT = DestVT;
   1509 
   1510     // Ints smaller than a register still arrive in a full 64-bit
   1511     // register, so make sure we recognize this.
   1512     if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
   1513       CopyVT = MVT::i64;
   1514 
   1515     unsigned SourcePhysReg = VA.getLocReg();
   1516     unsigned ResultReg = 0;
   1517 
   1518     if (RetVT == CopyVT) {
   1519       const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
   1520       ResultReg = copyRegToRegClass(CpyRC, SourcePhysReg);
   1521 
   1522     // If necessary, round the floating result to single precision.
   1523     } else if (CopyVT == MVT::f64) {
   1524       ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
   1525       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP),
   1526               ResultReg).addReg(SourcePhysReg);
   1527 
   1528     // If only the low half of a general register is needed, generate
   1529     // a GPRC copy instead of a G8RC copy.  (EXTRACT_SUBREG can't be
   1530     // used along the fast-isel path (not lowered), and downstream logic
   1531     // also doesn't like a direct subreg copy on a physical reg.)
   1532     } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
   1533       // Convert physical register from G8RC to GPRC.
   1534       SourcePhysReg -= PPC::X0 - PPC::R0;
   1535       ResultReg = copyRegToRegClass(&PPC::GPRCRegClass, SourcePhysReg);
   1536     }
   1537 
   1538     assert(ResultReg && "ResultReg unset!");
   1539     CLI.InRegs.push_back(SourcePhysReg);
   1540     CLI.ResultReg = ResultReg;
   1541     CLI.NumResultRegs = 1;
   1542   }
   1543 
   1544   return true;
   1545 }
   1546 
   1547 bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
   1548   CallingConv::ID CC  = CLI.CallConv;
   1549   bool IsTailCall     = CLI.IsTailCall;
   1550   bool IsVarArg       = CLI.IsVarArg;
   1551   const Value *Callee = CLI.Callee;
   1552   const MCSymbol *Symbol = CLI.Symbol;
   1553 
   1554   if (!Callee && !Symbol)
   1555     return false;
   1556 
   1557   // Allow SelectionDAG isel to handle tail calls.
   1558   if (IsTailCall)
   1559     return false;
   1560 
   1561   // Let SDISel handle vararg functions.
   1562   if (IsVarArg)
   1563     return false;
   1564 
   1565   // If this is a PC-Rel function, let SDISel handle the call.
   1566   if (Subtarget->isUsingPCRelativeCalls())
   1567     return false;
   1568 
   1569   // Handle simple calls for now, with legal return types and
   1570   // those that can be extended.
   1571   Type *RetTy = CLI.RetTy;
   1572   MVT RetVT;
   1573   if (RetTy->isVoidTy())
   1574     RetVT = MVT::isVoid;
   1575   else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
   1576            RetVT != MVT::i8)
   1577     return false;
   1578   else if (RetVT == MVT::i1 && Subtarget->useCRBits())
   1579     // We can't handle boolean returns when CR bits are in use.
   1580     return false;
   1581 
   1582   // FIXME: No multi-register return values yet.
   1583   if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
   1584       RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
   1585       RetVT != MVT::f64) {
   1586     SmallVector<CCValAssign, 16> RVLocs;
   1587     CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
   1588     CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
   1589     if (RVLocs.size() > 1)
   1590       return false;
   1591   }
   1592 
   1593   // Bail early if more than 8 arguments, as we only currently
   1594   // handle arguments passed in registers.
   1595   unsigned NumArgs = CLI.OutVals.size();
   1596   if (NumArgs > 8)
   1597     return false;
   1598 
   1599   // Set up the argument vectors.
   1600   SmallVector<Value*, 8> Args;
   1601   SmallVector<unsigned, 8> ArgRegs;
   1602   SmallVector<MVT, 8> ArgVTs;
   1603   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
   1604 
   1605   Args.reserve(NumArgs);
   1606   ArgRegs.reserve(NumArgs);
   1607   ArgVTs.reserve(NumArgs);
   1608   ArgFlags.reserve(NumArgs);
   1609 
   1610   for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
   1611     // Only handle easy calls for now.  It would be reasonably easy
   1612     // to handle <= 8-byte structures passed ByVal in registers, but we
   1613     // have to ensure they are right-justified in the register.
   1614     ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
   1615     if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
   1616       return false;
   1617 
   1618     Value *ArgValue = CLI.OutVals[i];
   1619     Type *ArgTy = ArgValue->getType();
   1620     MVT ArgVT;
   1621     if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
   1622       return false;
   1623 
   1624     // FIXME: FastISel cannot handle non-simple types yet, including 128-bit FP
   1625     // types, which is passed through vector register. Skip these types and
   1626     // fallback to default SelectionDAG based selection.
   1627     if (ArgVT.isVector() || ArgVT == MVT::f128)
   1628       return false;
   1629 
   1630     unsigned Arg = getRegForValue(ArgValue);
   1631     if (Arg == 0)
   1632       return false;
   1633 
   1634     Args.push_back(ArgValue);
   1635     ArgRegs.push_back(Arg);
   1636     ArgVTs.push_back(ArgVT);
   1637     ArgFlags.push_back(Flags);
   1638   }
   1639 
   1640   // Process the arguments.
   1641   SmallVector<unsigned, 8> RegArgs;
   1642   unsigned NumBytes;
   1643 
   1644   if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
   1645                        RegArgs, CC, NumBytes, IsVarArg))
   1646     return false;
   1647 
   1648   MachineInstrBuilder MIB;
   1649   // FIXME: No handling for function pointers yet.  This requires
   1650   // implementing the function descriptor (OPD) setup.
   1651   const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
   1652   if (!GV) {
   1653     // patchpoints are a special case; they always dispatch to a pointer value.
   1654     // However, we don't actually want to generate the indirect call sequence
   1655     // here (that will be generated, as necessary, during asm printing), and
   1656     // the call we generate here will be erased by FastISel::selectPatchpoint,
   1657     // so don't try very hard...
   1658     if (CLI.IsPatchPoint)
   1659       MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::NOP));
   1660     else
   1661       return false;
   1662   } else {
   1663     // Build direct call with NOP for TOC restore.
   1664     // FIXME: We can and should optimize away the NOP for local calls.
   1665     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1666                   TII.get(PPC::BL8_NOP));
   1667     // Add callee.
   1668     MIB.addGlobalAddress(GV);
   1669   }
   1670 
   1671   // Add implicit physical register uses to the call.
   1672   for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
   1673     MIB.addReg(RegArgs[II], RegState::Implicit);
   1674 
   1675   // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
   1676   // into the call.
   1677   PPCFuncInfo->setUsesTOCBasePtr();
   1678   MIB.addReg(PPC::X2, RegState::Implicit);
   1679 
   1680   // Add a register mask with the call-preserved registers.  Proper
   1681   // defs for return values will be added by setPhysRegsDeadExcept().
   1682   MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
   1683 
   1684   CLI.Call = MIB;
   1685 
   1686   // Finish off the call including any return values.
   1687   return finishCall(RetVT, CLI, NumBytes);
   1688 }
   1689 
   1690 // Attempt to fast-select a return instruction.
   1691 bool PPCFastISel::SelectRet(const Instruction *I) {
   1692 
   1693   if (!FuncInfo.CanLowerReturn)
   1694     return false;
   1695 
   1696   const ReturnInst *Ret = cast<ReturnInst>(I);
   1697   const Function &F = *I->getParent()->getParent();
   1698 
   1699   // Build a list of return value registers.
   1700   SmallVector<unsigned, 4> RetRegs;
   1701   CallingConv::ID CC = F.getCallingConv();
   1702 
   1703   if (Ret->getNumOperands() > 0) {
   1704     SmallVector<ISD::OutputArg, 4> Outs;
   1705     GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
   1706 
   1707     // Analyze operands of the call, assigning locations to each operand.
   1708     SmallVector<CCValAssign, 16> ValLocs;
   1709     CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
   1710     CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
   1711     const Value *RV = Ret->getOperand(0);
   1712 
   1713     // FIXME: Only one output register for now.
   1714     if (ValLocs.size() > 1)
   1715       return false;
   1716 
   1717     // Special case for returning a constant integer of any size - materialize
   1718     // the constant as an i64 and copy it to the return register.
   1719     if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
   1720       CCValAssign &VA = ValLocs[0];
   1721 
   1722       Register RetReg = VA.getLocReg();
   1723       // We still need to worry about properly extending the sign. For example,
   1724       // we could have only a single bit or a constant that needs zero
   1725       // extension rather than sign extension. Make sure we pass the return
   1726       // value extension property to integer materialization.
   1727       unsigned SrcReg =
   1728           PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
   1729 
   1730       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1731             TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
   1732 
   1733       RetRegs.push_back(RetReg);
   1734 
   1735     } else {
   1736       unsigned Reg = getRegForValue(RV);
   1737 
   1738       if (Reg == 0)
   1739         return false;
   1740 
   1741       // Copy the result values into the output registers.
   1742       for (unsigned i = 0; i < ValLocs.size(); ++i) {
   1743 
   1744         CCValAssign &VA = ValLocs[i];
   1745         assert(VA.isRegLoc() && "Can only return in registers!");
   1746         RetRegs.push_back(VA.getLocReg());
   1747         unsigned SrcReg = Reg + VA.getValNo();
   1748 
   1749         EVT RVEVT = TLI.getValueType(DL, RV->getType());
   1750         if (!RVEVT.isSimple())
   1751           return false;
   1752         MVT RVVT = RVEVT.getSimpleVT();
   1753         MVT DestVT = VA.getLocVT();
   1754 
   1755         if (RVVT != DestVT && RVVT != MVT::i8 &&
   1756             RVVT != MVT::i16 && RVVT != MVT::i32)
   1757           return false;
   1758 
   1759         if (RVVT != DestVT) {
   1760           switch (VA.getLocInfo()) {
   1761             default:
   1762               llvm_unreachable("Unknown loc info!");
   1763             case CCValAssign::Full:
   1764               llvm_unreachable("Full value assign but types don't match?");
   1765             case CCValAssign::AExt:
   1766             case CCValAssign::ZExt: {
   1767               const TargetRegisterClass *RC =
   1768                 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
   1769               unsigned TmpReg = createResultReg(RC);
   1770               if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
   1771                 return false;
   1772               SrcReg = TmpReg;
   1773               break;
   1774             }
   1775             case CCValAssign::SExt: {
   1776               const TargetRegisterClass *RC =
   1777                 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
   1778               unsigned TmpReg = createResultReg(RC);
   1779               if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
   1780                 return false;
   1781               SrcReg = TmpReg;
   1782               break;
   1783             }
   1784           }
   1785         }
   1786 
   1787         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1788                 TII.get(TargetOpcode::COPY), RetRegs[i])
   1789           .addReg(SrcReg);
   1790       }
   1791     }
   1792   }
   1793 
   1794   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1795                                     TII.get(PPC::BLR8));
   1796 
   1797   for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
   1798     MIB.addReg(RetRegs[i], RegState::Implicit);
   1799 
   1800   return true;
   1801 }
   1802 
   1803 // Attempt to emit an integer extend of SrcReg into DestReg.  Both
   1804 // signed and zero extensions are supported.  Return false if we
   1805 // can't handle it.
   1806 bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
   1807                                 unsigned DestReg, bool IsZExt) {
   1808   if (DestVT != MVT::i32 && DestVT != MVT::i64)
   1809     return false;
   1810   if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
   1811     return false;
   1812 
   1813   // Signed extensions use EXTSB, EXTSH, EXTSW.
   1814   if (!IsZExt) {
   1815     unsigned Opc;
   1816     if (SrcVT == MVT::i8)
   1817       Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
   1818     else if (SrcVT == MVT::i16)
   1819       Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
   1820     else {
   1821       assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
   1822       Opc = PPC::EXTSW_32_64;
   1823     }
   1824     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
   1825       .addReg(SrcReg);
   1826 
   1827   // Unsigned 32-bit extensions use RLWINM.
   1828   } else if (DestVT == MVT::i32) {
   1829     unsigned MB;
   1830     if (SrcVT == MVT::i8)
   1831       MB = 24;
   1832     else {
   1833       assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
   1834       MB = 16;
   1835     }
   1836     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM),
   1837             DestReg)
   1838       .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
   1839 
   1840   // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
   1841   } else {
   1842     unsigned MB;
   1843     if (SrcVT == MVT::i8)
   1844       MB = 56;
   1845     else if (SrcVT == MVT::i16)
   1846       MB = 48;
   1847     else
   1848       MB = 32;
   1849     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   1850             TII.get(PPC::RLDICL_32_64), DestReg)
   1851       .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
   1852   }
   1853 
   1854   return true;
   1855 }
   1856 
   1857 // Attempt to fast-select an indirect branch instruction.
   1858 bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
   1859   unsigned AddrReg = getRegForValue(I->getOperand(0));
   1860   if (AddrReg == 0)
   1861     return false;
   1862 
   1863   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
   1864     .addReg(AddrReg);
   1865   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
   1866 
   1867   const IndirectBrInst *IB = cast<IndirectBrInst>(I);
   1868   for (const BasicBlock *SuccBB : IB->successors())
   1869     FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
   1870 
   1871   return true;
   1872 }
   1873 
   1874 // Attempt to fast-select an integer truncate instruction.
   1875 bool PPCFastISel::SelectTrunc(const Instruction *I) {
   1876   Value *Src  = I->getOperand(0);
   1877   EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
   1878   EVT DestVT = TLI.getValueType(DL, I->getType(), true);
   1879 
   1880   if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
   1881     return false;
   1882 
   1883   if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
   1884     return false;
   1885 
   1886   unsigned SrcReg = getRegForValue(Src);
   1887   if (!SrcReg)
   1888     return false;
   1889 
   1890   // The only interesting case is when we need to switch register classes.
   1891   if (SrcVT == MVT::i64)
   1892     SrcReg = copyRegToRegClass(&PPC::GPRCRegClass, SrcReg, 0, PPC::sub_32);
   1893 
   1894   updateValueMap(I, SrcReg);
   1895   return true;
   1896 }
   1897 
   1898 // Attempt to fast-select an integer extend instruction.
   1899 bool PPCFastISel::SelectIntExt(const Instruction *I) {
   1900   Type *DestTy = I->getType();
   1901   Value *Src = I->getOperand(0);
   1902   Type *SrcTy = Src->getType();
   1903 
   1904   bool IsZExt = isa<ZExtInst>(I);
   1905   unsigned SrcReg = getRegForValue(Src);
   1906   if (!SrcReg) return false;
   1907 
   1908   EVT SrcEVT, DestEVT;
   1909   SrcEVT = TLI.getValueType(DL, SrcTy, true);
   1910   DestEVT = TLI.getValueType(DL, DestTy, true);
   1911   if (!SrcEVT.isSimple())
   1912     return false;
   1913   if (!DestEVT.isSimple())
   1914     return false;
   1915 
   1916   MVT SrcVT = SrcEVT.getSimpleVT();
   1917   MVT DestVT = DestEVT.getSimpleVT();
   1918 
   1919   // If we know the register class needed for the result of this
   1920   // instruction, use it.  Otherwise pick the register class of the
   1921   // correct size that does not contain X0/R0, since we don't know
   1922   // whether downstream uses permit that assignment.
   1923   unsigned AssignedReg = FuncInfo.ValueMap[I];
   1924   const TargetRegisterClass *RC =
   1925     (AssignedReg ? MRI.getRegClass(AssignedReg) :
   1926      (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
   1927       &PPC::GPRC_and_GPRC_NOR0RegClass));
   1928   unsigned ResultReg = createResultReg(RC);
   1929 
   1930   if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
   1931     return false;
   1932 
   1933   updateValueMap(I, ResultReg);
   1934   return true;
   1935 }
   1936 
   1937 // Attempt to fast-select an instruction that wasn't handled by
   1938 // the table-generated machinery.
   1939 bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
   1940 
   1941   switch (I->getOpcode()) {
   1942     case Instruction::Load:
   1943       return SelectLoad(I);
   1944     case Instruction::Store:
   1945       return SelectStore(I);
   1946     case Instruction::Br:
   1947       return SelectBranch(I);
   1948     case Instruction::IndirectBr:
   1949       return SelectIndirectBr(I);
   1950     case Instruction::FPExt:
   1951       return SelectFPExt(I);
   1952     case Instruction::FPTrunc:
   1953       return SelectFPTrunc(I);
   1954     case Instruction::SIToFP:
   1955       return SelectIToFP(I, /*IsSigned*/ true);
   1956     case Instruction::UIToFP:
   1957       return SelectIToFP(I, /*IsSigned*/ false);
   1958     case Instruction::FPToSI:
   1959       return SelectFPToI(I, /*IsSigned*/ true);
   1960     case Instruction::FPToUI:
   1961       return SelectFPToI(I, /*IsSigned*/ false);
   1962     case Instruction::Add:
   1963       return SelectBinaryIntOp(I, ISD::ADD);
   1964     case Instruction::Or:
   1965       return SelectBinaryIntOp(I, ISD::OR);
   1966     case Instruction::Sub:
   1967       return SelectBinaryIntOp(I, ISD::SUB);
   1968     case Instruction::Call:
   1969       // On AIX, call lowering uses the DAG-ISEL path currently so that the
   1970       // callee of the direct function call instruction will be mapped to the
   1971       // symbol for the function's entry point, which is distinct from the
   1972       // function descriptor symbol. The latter is the symbol whose XCOFF symbol
   1973       // name is the C-linkage name of the source level function.
   1974       if (TM.getTargetTriple().isOSAIX())
   1975         break;
   1976       return selectCall(I);
   1977     case Instruction::Ret:
   1978       return SelectRet(I);
   1979     case Instruction::Trunc:
   1980       return SelectTrunc(I);
   1981     case Instruction::ZExt:
   1982     case Instruction::SExt:
   1983       return SelectIntExt(I);
   1984     // Here add other flavors of Instruction::XXX that automated
   1985     // cases don't catch.  For example, switches are terminators
   1986     // that aren't yet handled.
   1987     default:
   1988       break;
   1989   }
   1990   return false;
   1991 }
   1992 
   1993 // Materialize a floating-point constant into a register, and return
   1994 // the register number (or zero if we failed to handle it).
   1995 unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
   1996   // If this is a PC-Rel function, let SDISel handle constant pool.
   1997   if (Subtarget->isUsingPCRelativeCalls())
   1998     return false;
   1999 
   2000   // No plans to handle long double here.
   2001   if (VT != MVT::f32 && VT != MVT::f64)
   2002     return 0;
   2003 
   2004   // All FP constants are loaded from the constant pool.
   2005   Align Alignment = DL.getPrefTypeAlign(CFP->getType());
   2006   unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Alignment);
   2007   const bool HasSPE = Subtarget->hasSPE();
   2008   const TargetRegisterClass *RC;
   2009   if (HasSPE)
   2010     RC = ((VT == MVT::f32) ? &PPC::GPRCRegClass : &PPC::SPERCRegClass);
   2011   else
   2012     RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
   2013 
   2014   unsigned DestReg = createResultReg(RC);
   2015   CodeModel::Model CModel = TM.getCodeModel();
   2016 
   2017   MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
   2018       MachinePointerInfo::getConstantPool(*FuncInfo.MF),
   2019       MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Alignment);
   2020 
   2021   unsigned Opc;
   2022 
   2023   if (HasSPE)
   2024     Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
   2025   else
   2026     Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
   2027 
   2028   unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
   2029 
   2030   PPCFuncInfo->setUsesTOCBasePtr();
   2031   // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
   2032   if (CModel == CodeModel::Small) {
   2033     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT),
   2034             TmpReg)
   2035       .addConstantPoolIndex(Idx).addReg(PPC::X2);
   2036     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
   2037       .addImm(0).addReg(TmpReg).addMemOperand(MMO);
   2038   } else {
   2039     // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA8(X2, Idx)).
   2040     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA8),
   2041             TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
   2042     // But for large code model, we must generate a LDtocL followed
   2043     // by the LF[SD].
   2044     if (CModel == CodeModel::Large) {
   2045       unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
   2046       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
   2047               TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
   2048       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
   2049           .addImm(0)
   2050           .addReg(TmpReg2);
   2051     } else
   2052       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg)
   2053         .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
   2054         .addReg(TmpReg)
   2055         .addMemOperand(MMO);
   2056   }
   2057 
   2058   return DestReg;
   2059 }
   2060 
   2061 // Materialize the address of a global value into a register, and return
   2062 // the register number (or zero if we failed to handle it).
   2063 unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
   2064   // If this is a PC-Rel function, let SDISel handle GV materialization.
   2065   if (Subtarget->isUsingPCRelativeCalls())
   2066     return false;
   2067 
   2068   assert(VT == MVT::i64 && "Non-address!");
   2069   const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
   2070   unsigned DestReg = createResultReg(RC);
   2071 
   2072   // Global values may be plain old object addresses, TLS object
   2073   // addresses, constant pool entries, or jump tables.  How we generate
   2074   // code for these may depend on small, medium, or large code model.
   2075   CodeModel::Model CModel = TM.getCodeModel();
   2076 
   2077   // FIXME: Jump tables are not yet required because fast-isel doesn't
   2078   // handle switches; if that changes, we need them as well.  For now,
   2079   // what follows assumes everything's a generic (or TLS) global address.
   2080 
   2081   // FIXME: We don't yet handle the complexity of TLS.
   2082   if (GV->isThreadLocal())
   2083     return 0;
   2084 
   2085   PPCFuncInfo->setUsesTOCBasePtr();
   2086   // For small code model, generate a simple TOC load.
   2087   if (CModel == CodeModel::Small)
   2088     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc),
   2089             DestReg)
   2090         .addGlobalAddress(GV)
   2091         .addReg(PPC::X2);
   2092   else {
   2093     // If the address is an externally defined symbol, a symbol with common
   2094     // or externally available linkage, a non-local function address, or a
   2095     // jump table address (not yet needed), or if we are generating code
   2096     // for large code model, we generate:
   2097     //       LDtocL(GV, ADDIStocHA8(%x2, GV))
   2098     // Otherwise we generate:
   2099     //       ADDItocL(ADDIStocHA8(%x2, GV), GV)
   2100     // Either way, start with the ADDIStocHA8:
   2101     unsigned HighPartReg = createResultReg(RC);
   2102     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA8),
   2103             HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
   2104 
   2105     if (Subtarget->isGVIndirectSymbol(GV)) {
   2106       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
   2107               DestReg).addGlobalAddress(GV).addReg(HighPartReg);
   2108     } else {
   2109       // Otherwise generate the ADDItocL.
   2110       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL),
   2111               DestReg).addReg(HighPartReg).addGlobalAddress(GV);
   2112     }
   2113   }
   2114 
   2115   return DestReg;
   2116 }
   2117 
   2118 // Materialize a 32-bit integer constant into a register, and return
   2119 // the register number (or zero if we failed to handle it).
   2120 unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
   2121                                              const TargetRegisterClass *RC) {
   2122   unsigned Lo = Imm & 0xFFFF;
   2123   unsigned Hi = (Imm >> 16) & 0xFFFF;
   2124 
   2125   unsigned ResultReg = createResultReg(RC);
   2126   bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
   2127 
   2128   if (isInt<16>(Imm))
   2129     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   2130             TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
   2131       .addImm(Imm);
   2132   else if (Lo) {
   2133     // Both Lo and Hi have nonzero bits.
   2134     unsigned TmpReg = createResultReg(RC);
   2135     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   2136             TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
   2137       .addImm(Hi);
   2138     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   2139             TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
   2140       .addReg(TmpReg).addImm(Lo);
   2141   } else
   2142     // Just Hi bits.
   2143     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   2144             TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
   2145         .addImm(Hi);
   2146 
   2147   return ResultReg;
   2148 }
   2149 
   2150 // Materialize a 64-bit integer constant into a register, and return
   2151 // the register number (or zero if we failed to handle it).
   2152 unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
   2153                                              const TargetRegisterClass *RC) {
   2154   unsigned Remainder = 0;
   2155   unsigned Shift = 0;
   2156 
   2157   // If the value doesn't fit in 32 bits, see if we can shift it
   2158   // so that it fits in 32 bits.
   2159   if (!isInt<32>(Imm)) {
   2160     Shift = countTrailingZeros<uint64_t>(Imm);
   2161     int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
   2162 
   2163     if (isInt<32>(ImmSh))
   2164       Imm = ImmSh;
   2165     else {
   2166       Remainder = Imm;
   2167       Shift = 32;
   2168       Imm >>= 32;
   2169     }
   2170   }
   2171 
   2172   // Handle the high-order 32 bits (if shifted) or the whole 32 bits
   2173   // (if not shifted).
   2174   unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
   2175   if (!Shift)
   2176     return TmpReg1;
   2177 
   2178   // If upper 32 bits were not zero, we've built them and need to shift
   2179   // them into place.
   2180   unsigned TmpReg2;
   2181   if (Imm) {
   2182     TmpReg2 = createResultReg(RC);
   2183     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR),
   2184             TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
   2185   } else
   2186     TmpReg2 = TmpReg1;
   2187 
   2188   unsigned TmpReg3, Hi, Lo;
   2189   if ((Hi = (Remainder >> 16) & 0xFFFF)) {
   2190     TmpReg3 = createResultReg(RC);
   2191     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8),
   2192             TmpReg3).addReg(TmpReg2).addImm(Hi);
   2193   } else
   2194     TmpReg3 = TmpReg2;
   2195 
   2196   if ((Lo = Remainder & 0xFFFF)) {
   2197     unsigned ResultReg = createResultReg(RC);
   2198     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8),
   2199             ResultReg).addReg(TmpReg3).addImm(Lo);
   2200     return ResultReg;
   2201   }
   2202 
   2203   return TmpReg3;
   2204 }
   2205 
   2206 // Materialize an integer constant into a register, and return
   2207 // the register number (or zero if we failed to handle it).
   2208 unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
   2209                                         bool UseSExt) {
   2210   // If we're using CR bit registers for i1 values, handle that as a special
   2211   // case first.
   2212   if (VT == MVT::i1 && Subtarget->useCRBits()) {
   2213     unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
   2214     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   2215             TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
   2216     return ImmReg;
   2217   }
   2218 
   2219   if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
   2220       VT != MVT::i1)
   2221     return 0;
   2222 
   2223   const TargetRegisterClass *RC =
   2224       ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
   2225   int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
   2226 
   2227   // If the constant is in range, use a load-immediate.
   2228   // Since LI will sign extend the constant we need to make sure that for
   2229   // our zeroext constants that the sign extended constant fits into 16-bits -
   2230   // a range of 0..0x7fff.
   2231   if (isInt<16>(Imm)) {
   2232     unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
   2233     unsigned ImmReg = createResultReg(RC);
   2234     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
   2235         .addImm(Imm);
   2236     return ImmReg;
   2237   }
   2238 
   2239   // Construct the constant piecewise.
   2240   if (VT == MVT::i64)
   2241     return PPCMaterialize64BitInt(Imm, RC);
   2242   else if (VT == MVT::i32)
   2243     return PPCMaterialize32BitInt(Imm, RC);
   2244 
   2245   return 0;
   2246 }
   2247 
   2248 // Materialize a constant into a register, and return the register
   2249 // number (or zero if we failed to handle it).
   2250 unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
   2251   EVT CEVT = TLI.getValueType(DL, C->getType(), true);
   2252 
   2253   // Only handle simple types.
   2254   if (!CEVT.isSimple()) return 0;
   2255   MVT VT = CEVT.getSimpleVT();
   2256 
   2257   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
   2258     return PPCMaterializeFP(CFP, VT);
   2259   else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
   2260     return PPCMaterializeGV(GV, VT);
   2261   else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
   2262     // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
   2263     // assumes that constant PHI operands will be zero extended, and failure to
   2264     // match that assumption will cause problems if we sign extend here but
   2265     // some user of a PHI is in a block for which we fall back to full SDAG
   2266     // instruction selection.
   2267     return PPCMaterializeInt(CI, VT, false);
   2268 
   2269   return 0;
   2270 }
   2271 
   2272 // Materialize the address created by an alloca into a register, and
   2273 // return the register number (or zero if we failed to handle it).
   2274 unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
   2275   // Don't handle dynamic allocas.
   2276   if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
   2277 
   2278   MVT VT;
   2279   if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
   2280 
   2281   DenseMap<const AllocaInst*, int>::iterator SI =
   2282     FuncInfo.StaticAllocaMap.find(AI);
   2283 
   2284   if (SI != FuncInfo.StaticAllocaMap.end()) {
   2285     unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
   2286     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8),
   2287             ResultReg).addFrameIndex(SI->second).addImm(0);
   2288     return ResultReg;
   2289   }
   2290 
   2291   return 0;
   2292 }
   2293 
   2294 // Fold loads into extends when possible.
   2295 // FIXME: We can have multiple redundant extend/trunc instructions
   2296 // following a load.  The folding only picks up one.  Extend this
   2297 // to check subsequent instructions for the same pattern and remove
   2298 // them.  Thus ResultReg should be the def reg for the last redundant
   2299 // instruction in a chain, and all intervening instructions can be
   2300 // removed from parent.  Change test/CodeGen/PowerPC/fast-isel-fold.ll
   2301 // to add ELF64-NOT: rldicl to the appropriate tests when this works.
   2302 bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
   2303                                       const LoadInst *LI) {
   2304   // Verify we have a legal type before going any further.
   2305   MVT VT;
   2306   if (!isLoadTypeLegal(LI->getType(), VT))
   2307     return false;
   2308 
   2309   // Combine load followed by zero- or sign-extend.
   2310   bool IsZExt = false;
   2311   switch(MI->getOpcode()) {
   2312     default:
   2313       return false;
   2314 
   2315     case PPC::RLDICL:
   2316     case PPC::RLDICL_32_64: {
   2317       IsZExt = true;
   2318       unsigned MB = MI->getOperand(3).getImm();
   2319       if ((VT == MVT::i8 && MB <= 56) ||
   2320           (VT == MVT::i16 && MB <= 48) ||
   2321           (VT == MVT::i32 && MB <= 32))
   2322         break;
   2323       return false;
   2324     }
   2325 
   2326     case PPC::RLWINM:
   2327     case PPC::RLWINM8: {
   2328       IsZExt = true;
   2329       unsigned MB = MI->getOperand(3).getImm();
   2330       if ((VT == MVT::i8 && MB <= 24) ||
   2331           (VT == MVT::i16 && MB <= 16))
   2332         break;
   2333       return false;
   2334     }
   2335 
   2336     case PPC::EXTSB:
   2337     case PPC::EXTSB8:
   2338     case PPC::EXTSB8_32_64:
   2339       /* There is no sign-extending load-byte instruction. */
   2340       return false;
   2341 
   2342     case PPC::EXTSH:
   2343     case PPC::EXTSH8:
   2344     case PPC::EXTSH8_32_64: {
   2345       if (VT != MVT::i16 && VT != MVT::i8)
   2346         return false;
   2347       break;
   2348     }
   2349 
   2350     case PPC::EXTSW:
   2351     case PPC::EXTSW_32:
   2352     case PPC::EXTSW_32_64: {
   2353       if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
   2354         return false;
   2355       break;
   2356     }
   2357   }
   2358 
   2359   // See if we can handle this address.
   2360   Address Addr;
   2361   if (!PPCComputeAddress(LI->getOperand(0), Addr))
   2362     return false;
   2363 
   2364   Register ResultReg = MI->getOperand(0).getReg();
   2365 
   2366   if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
   2367                    Subtarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
   2368     return false;
   2369 
   2370   MachineBasicBlock::iterator I(MI);
   2371   removeDeadCode(I, std::next(I));
   2372   return true;
   2373 }
   2374 
   2375 // Attempt to lower call arguments in a faster way than done by
   2376 // the selection DAG code.
   2377 bool PPCFastISel::fastLowerArguments() {
   2378   // Defer to normal argument lowering for now.  It's reasonably
   2379   // efficient.  Consider doing something like ARM to handle the
   2380   // case where all args fit in registers, no varargs, no float
   2381   // or vector args.
   2382   return false;
   2383 }
   2384 
   2385 // Handle materializing integer constants into a register.  This is not
   2386 // automatically generated for PowerPC, so must be explicitly created here.
   2387 unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
   2388 
   2389   if (Opc != ISD::Constant)
   2390     return 0;
   2391 
   2392   // If we're using CR bit registers for i1 values, handle that as a special
   2393   // case first.
   2394   if (VT == MVT::i1 && Subtarget->useCRBits()) {
   2395     unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
   2396     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
   2397             TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
   2398     return ImmReg;
   2399   }
   2400 
   2401   if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
   2402       VT != MVT::i1)
   2403     return 0;
   2404 
   2405   const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
   2406                                    &PPC::GPRCRegClass);
   2407   if (VT == MVT::i64)
   2408     return PPCMaterialize64BitInt(Imm, RC);
   2409   else
   2410     return PPCMaterialize32BitInt(Imm, RC);
   2411 }
   2412 
   2413 // Override for ADDI and ADDI8 to set the correct register class
   2414 // on RHS operand 0.  The automatic infrastructure naively assumes
   2415 // GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
   2416 // for these cases.  At the moment, none of the other automatically
   2417 // generated RI instructions require special treatment.  However, once
   2418 // SelectSelect is implemented, "isel" requires similar handling.
   2419 //
   2420 // Also be conservative about the output register class.  Avoid
   2421 // assigning R0 or X0 to the output register for GPRC and G8RC
   2422 // register classes, as any such result could be used in ADDI, etc.,
   2423 // where those regs have another meaning.
   2424 unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
   2425                                       const TargetRegisterClass *RC,
   2426                                       unsigned Op0,
   2427                                       uint64_t Imm) {
   2428   if (MachineInstOpcode == PPC::ADDI)
   2429     MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
   2430   else if (MachineInstOpcode == PPC::ADDI8)
   2431     MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
   2432 
   2433   const TargetRegisterClass *UseRC =
   2434     (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
   2435      (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
   2436 
   2437   return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC, Op0, Imm);
   2438 }
   2439 
   2440 // Override for instructions with one register operand to avoid use of
   2441 // R0/X0.  The automatic infrastructure isn't aware of the context so
   2442 // we must be conservative.
   2443 unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
   2444                                      const TargetRegisterClass* RC,
   2445                                      unsigned Op0) {
   2446   const TargetRegisterClass *UseRC =
   2447     (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
   2448      (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
   2449 
   2450   return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0);
   2451 }
   2452 
   2453 // Override for instructions with two register operands to avoid use
   2454 // of R0/X0.  The automatic infrastructure isn't aware of the context
   2455 // so we must be conservative.
   2456 unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
   2457                                       const TargetRegisterClass* RC,
   2458                                       unsigned Op0, unsigned Op1) {
   2459   const TargetRegisterClass *UseRC =
   2460     (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
   2461      (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
   2462 
   2463   return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op1);
   2464 }
   2465 
   2466 namespace llvm {
   2467   // Create the fast instruction selector for PowerPC64 ELF.
   2468   FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
   2469                                 const TargetLibraryInfo *LibInfo) {
   2470     // Only available on 64-bit ELF for now.
   2471     const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
   2472     if (Subtarget.is64BitELFABI())
   2473       return new PPCFastISel(FuncInfo, LibInfo);
   2474     return nullptr;
   2475   }
   2476 }
   2477