LCOV - code coverage report
Current view: top level - lib/Target/ARM - ARMISelLowering.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 5143 6405 80.3 %
Date: 2018-10-20 13:21:21 Functions: 253 299 84.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- ARMISelLowering.cpp - ARM DAG Lowering Implementation --------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file defines the interfaces that ARM uses to lower LLVM code into a
      11             : // selection DAG.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "ARMISelLowering.h"
      16             : #include "ARMBaseInstrInfo.h"
      17             : #include "ARMBaseRegisterInfo.h"
      18             : #include "ARMCallingConv.h"
      19             : #include "ARMConstantPoolValue.h"
      20             : #include "ARMMachineFunctionInfo.h"
      21             : #include "ARMPerfectShuffle.h"
      22             : #include "ARMRegisterInfo.h"
      23             : #include "ARMSelectionDAGInfo.h"
      24             : #include "ARMSubtarget.h"
      25             : #include "MCTargetDesc/ARMAddressingModes.h"
      26             : #include "MCTargetDesc/ARMBaseInfo.h"
      27             : #include "Utils/ARMBaseInfo.h"
      28             : #include "llvm/ADT/APFloat.h"
      29             : #include "llvm/ADT/APInt.h"
      30             : #include "llvm/ADT/ArrayRef.h"
      31             : #include "llvm/ADT/BitVector.h"
      32             : #include "llvm/ADT/DenseMap.h"
      33             : #include "llvm/ADT/STLExtras.h"
      34             : #include "llvm/ADT/SmallPtrSet.h"
      35             : #include "llvm/ADT/SmallVector.h"
      36             : #include "llvm/ADT/Statistic.h"
      37             : #include "llvm/ADT/StringExtras.h"
      38             : #include "llvm/ADT/StringRef.h"
      39             : #include "llvm/ADT/StringSwitch.h"
      40             : #include "llvm/ADT/Triple.h"
      41             : #include "llvm/ADT/Twine.h"
      42             : #include "llvm/Analysis/VectorUtils.h"
      43             : #include "llvm/CodeGen/CallingConvLower.h"
      44             : #include "llvm/CodeGen/ISDOpcodes.h"
      45             : #include "llvm/CodeGen/IntrinsicLowering.h"
      46             : #include "llvm/CodeGen/MachineBasicBlock.h"
      47             : #include "llvm/CodeGen/MachineConstantPool.h"
      48             : #include "llvm/CodeGen/MachineFrameInfo.h"
      49             : #include "llvm/CodeGen/MachineFunction.h"
      50             : #include "llvm/CodeGen/MachineInstr.h"
      51             : #include "llvm/CodeGen/MachineInstrBuilder.h"
      52             : #include "llvm/CodeGen/MachineJumpTableInfo.h"
      53             : #include "llvm/CodeGen/MachineMemOperand.h"
      54             : #include "llvm/CodeGen/MachineOperand.h"
      55             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      56             : #include "llvm/CodeGen/RuntimeLibcalls.h"
      57             : #include "llvm/CodeGen/SelectionDAG.h"
      58             : #include "llvm/CodeGen/SelectionDAGNodes.h"
      59             : #include "llvm/CodeGen/TargetInstrInfo.h"
      60             : #include "llvm/CodeGen/TargetLowering.h"
      61             : #include "llvm/CodeGen/TargetOpcodes.h"
      62             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      63             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      64             : #include "llvm/CodeGen/ValueTypes.h"
      65             : #include "llvm/IR/Attributes.h"
      66             : #include "llvm/IR/CallingConv.h"
      67             : #include "llvm/IR/Constant.h"
      68             : #include "llvm/IR/Constants.h"
      69             : #include "llvm/IR/DataLayout.h"
      70             : #include "llvm/IR/DebugLoc.h"
      71             : #include "llvm/IR/DerivedTypes.h"
      72             : #include "llvm/IR/Function.h"
      73             : #include "llvm/IR/GlobalAlias.h"
      74             : #include "llvm/IR/GlobalValue.h"
      75             : #include "llvm/IR/GlobalVariable.h"
      76             : #include "llvm/IR/IRBuilder.h"
      77             : #include "llvm/IR/InlineAsm.h"
      78             : #include "llvm/IR/Instruction.h"
      79             : #include "llvm/IR/Instructions.h"
      80             : #include "llvm/IR/IntrinsicInst.h"
      81             : #include "llvm/IR/Intrinsics.h"
      82             : #include "llvm/IR/Module.h"
      83             : #include "llvm/IR/Type.h"
      84             : #include "llvm/IR/User.h"
      85             : #include "llvm/IR/Value.h"
      86             : #include "llvm/MC/MCInstrDesc.h"
      87             : #include "llvm/MC/MCInstrItineraries.h"
      88             : #include "llvm/MC/MCRegisterInfo.h"
      89             : #include "llvm/MC/MCSchedule.h"
      90             : #include "llvm/Support/AtomicOrdering.h"
      91             : #include "llvm/Support/BranchProbability.h"
      92             : #include "llvm/Support/Casting.h"
      93             : #include "llvm/Support/CodeGen.h"
      94             : #include "llvm/Support/CommandLine.h"
      95             : #include "llvm/Support/Compiler.h"
      96             : #include "llvm/Support/Debug.h"
      97             : #include "llvm/Support/ErrorHandling.h"
      98             : #include "llvm/Support/KnownBits.h"
      99             : #include "llvm/Support/MachineValueType.h"
     100             : #include "llvm/Support/MathExtras.h"
     101             : #include "llvm/Support/raw_ostream.h"
     102             : #include "llvm/Target/TargetMachine.h"
     103             : #include "llvm/Target/TargetOptions.h"
     104             : #include <algorithm>
     105             : #include <cassert>
     106             : #include <cstdint>
     107             : #include <cstdlib>
     108             : #include <iterator>
     109             : #include <limits>
     110             : #include <string>
     111             : #include <tuple>
     112             : #include <utility>
     113             : #include <vector>
     114             : 
     115             : using namespace llvm;
     116             : 
     117             : #define DEBUG_TYPE "arm-isel"
     118             : 
     119             : STATISTIC(NumTailCalls, "Number of tail calls");
     120             : STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
     121             : STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
     122             : STATISTIC(NumConstpoolPromoted,
     123             :   "Number of constants with their storage promoted into constant pools");
     124             : 
     125             : static cl::opt<bool>
     126             : ARMInterworking("arm-interworking", cl::Hidden,
     127             :   cl::desc("Enable / disable ARM interworking (for debugging only)"),
     128             :   cl::init(true));
     129             : 
     130             : static cl::opt<bool> EnableConstpoolPromotion(
     131             :     "arm-promote-constant", cl::Hidden,
     132             :     cl::desc("Enable / disable promotion of unnamed_addr constants into "
     133             :              "constant pools"),
     134             :     cl::init(false)); // FIXME: set to true by default once PR32780 is fixed
     135             : static cl::opt<unsigned> ConstpoolPromotionMaxSize(
     136             :     "arm-promote-constant-max-size", cl::Hidden,
     137             :     cl::desc("Maximum size of constant to promote into a constant pool"),
     138             :     cl::init(64));
     139             : static cl::opt<unsigned> ConstpoolPromotionMaxTotal(
     140             :     "arm-promote-constant-max-total", cl::Hidden,
     141             :     cl::desc("Maximum size of ALL constants to promote into a constant pool"),
     142             :     cl::init(128));
     143             : 
     144             : // The APCS parameter registers.
     145             : static const MCPhysReg GPRArgRegs[] = {
     146             :   ARM::R0, ARM::R1, ARM::R2, ARM::R3
     147             : };
     148             : 
     149       28648 : void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
     150             :                                        MVT PromotedBitwiseVT) {
     151       28648 :   if (VT != PromotedLdStVT) {
     152             :     setOperationAction(ISD::LOAD, VT, Promote);
     153             :     AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
     154             : 
     155             :     setOperationAction(ISD::STORE, VT, Promote);
     156             :     AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
     157             :   }
     158             : 
     159       28648 :   MVT ElemTy = VT.getVectorElementType();
     160       28648 :   if (ElemTy != MVT::f64)
     161             :     setOperationAction(ISD::SETCC, VT, Custom);
     162             :   setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
     163             :   setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
     164       28648 :   if (ElemTy == MVT::i32) {
     165             :     setOperationAction(ISD::SINT_TO_FP, VT, Custom);
     166             :     setOperationAction(ISD::UINT_TO_FP, VT, Custom);
     167             :     setOperationAction(ISD::FP_TO_SINT, VT, Custom);
     168             :     setOperationAction(ISD::FP_TO_UINT, VT, Custom);
     169             :   } else {
     170             :     setOperationAction(ISD::SINT_TO_FP, VT, Expand);
     171             :     setOperationAction(ISD::UINT_TO_FP, VT, Expand);
     172             :     setOperationAction(ISD::FP_TO_SINT, VT, Expand);
     173             :     setOperationAction(ISD::FP_TO_UINT, VT, Expand);
     174             :   }
     175             :   setOperationAction(ISD::BUILD_VECTOR,      VT, Custom);
     176             :   setOperationAction(ISD::VECTOR_SHUFFLE,    VT, Custom);
     177             :   setOperationAction(ISD::CONCAT_VECTORS,    VT, Legal);
     178             :   setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
     179             :   setOperationAction(ISD::SELECT,            VT, Expand);
     180             :   setOperationAction(ISD::SELECT_CC,         VT, Expand);
     181             :   setOperationAction(ISD::VSELECT,           VT, Expand);
     182             :   setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
     183       28648 :   if (VT.isInteger()) {
     184             :     setOperationAction(ISD::SHL, VT, Custom);
     185             :     setOperationAction(ISD::SRA, VT, Custom);
     186             :     setOperationAction(ISD::SRL, VT, Custom);
     187             :   }
     188             : 
     189             :   // Promote all bit-wise operations.
     190       28648 :   if (VT.isInteger() && VT != PromotedBitwiseVT) {
     191             :     setOperationAction(ISD::AND, VT, Promote);
     192             :     AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
     193             :     setOperationAction(ISD::OR,  VT, Promote);
     194             :     AddPromotedToType (ISD::OR,  VT, PromotedBitwiseVT);
     195             :     setOperationAction(ISD::XOR, VT, Promote);
     196             :     AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
     197             :   }
     198             : 
     199             :   // Neon does not support vector divide/remainder operations.
     200             :   setOperationAction(ISD::SDIV, VT, Expand);
     201             :   setOperationAction(ISD::UDIV, VT, Expand);
     202             :   setOperationAction(ISD::FDIV, VT, Expand);
     203             :   setOperationAction(ISD::SREM, VT, Expand);
     204             :   setOperationAction(ISD::UREM, VT, Expand);
     205             :   setOperationAction(ISD::FREM, VT, Expand);
     206             : 
     207       20784 :   if (!VT.isFloatingPoint() &&
     208       46834 :       VT != MVT::v2i64 && VT != MVT::v1i64)
     209       93528 :     for (auto Opcode : {ISD::ABS, ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
     210             :       setOperationAction(Opcode, VT, Legal);
     211       28648 : }
     212             : 
     213       13025 : void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
     214             :   addRegisterClass(VT, &ARM::DPRRegClass);
     215       13025 :   addTypeForNEON(VT, MVT::f64, MVT::v2i32);
     216       13025 : }
     217             : 
     218       15623 : void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
     219             :   addRegisterClass(VT, &ARM::DPairRegClass);
     220       15623 :   addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
     221       15623 : }
     222             : 
     223        5050 : ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
     224        5050 :                                      const ARMSubtarget &STI)
     225        5050 :     : TargetLowering(TM), Subtarget(&STI) {
     226        5050 :   RegInfo = Subtarget->getRegisterInfo();
     227        5050 :   Itins = Subtarget->getInstrItineraryData();
     228             : 
     229             :   setBooleanContents(ZeroOrOneBooleanContent);
     230             :   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
     231             : 
     232        5050 :   if (!Subtarget->isTargetDarwin() && !Subtarget->isTargetIOS() &&
     233             :       !Subtarget->isTargetWatchOS()) {
     234        4273 :     bool IsHFTarget = TM.Options.FloatABIType == FloatABI::Hard;
     235     2012583 :     for (int LCID = 0; LCID < RTLIB::UNKNOWN_LIBCALL; ++LCID)
     236     2008310 :       setLibcallCallingConv(static_cast<RTLIB::Libcall>(LCID),
     237             :                             IsHFTarget ? CallingConv::ARM_AAPCS_VFP
     238             :                                        : CallingConv::ARM_AAPCS);
     239             :   }
     240             : 
     241       10100 :   if (Subtarget->isTargetMachO()) {
     242             :     // Uses VFP for Thumb libfuncs if available.
     243         469 :     if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
     244        1184 :         Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
     245             :       static const struct {
     246             :         const RTLIB::Libcall Op;
     247             :         const char * const Name;
     248             :         const ISD::CondCode Cond;
     249             :       } LibraryCalls[] = {
     250             :         // Single-precision floating-point arithmetic.
     251             :         { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
     252             :         { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
     253             :         { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
     254             :         { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
     255             : 
     256             :         // Double-precision floating-point arithmetic.
     257             :         { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
     258             :         { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
     259             :         { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
     260             :         { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
     261             : 
     262             :         // Single-precision comparisons.
     263             :         { RTLIB::OEQ_F32, "__eqsf2vfp",    ISD::SETNE },
     264             :         { RTLIB::UNE_F32, "__nesf2vfp",    ISD::SETNE },
     265             :         { RTLIB::OLT_F32, "__ltsf2vfp",    ISD::SETNE },
     266             :         { RTLIB::OLE_F32, "__lesf2vfp",    ISD::SETNE },
     267             :         { RTLIB::OGE_F32, "__gesf2vfp",    ISD::SETNE },
     268             :         { RTLIB::OGT_F32, "__gtsf2vfp",    ISD::SETNE },
     269             :         { RTLIB::UO_F32,  "__unordsf2vfp", ISD::SETNE },
     270             :         { RTLIB::O_F32,   "__unordsf2vfp", ISD::SETEQ },
     271             : 
     272             :         // Double-precision comparisons.
     273             :         { RTLIB::OEQ_F64, "__eqdf2vfp",    ISD::SETNE },
     274             :         { RTLIB::UNE_F64, "__nedf2vfp",    ISD::SETNE },
     275             :         { RTLIB::OLT_F64, "__ltdf2vfp",    ISD::SETNE },
     276             :         { RTLIB::OLE_F64, "__ledf2vfp",    ISD::SETNE },
     277             :         { RTLIB::OGE_F64, "__gedf2vfp",    ISD::SETNE },
     278             :         { RTLIB::OGT_F64, "__gtdf2vfp",    ISD::SETNE },
     279             :         { RTLIB::UO_F64,  "__unorddf2vfp", ISD::SETNE },
     280             :         { RTLIB::O_F64,   "__unorddf2vfp", ISD::SETEQ },
     281             : 
     282             :         // Floating-point to integer conversions.
     283             :         // i64 conversions are done via library routines even when generating VFP
     284             :         // instructions, so use the same ones.
     285             :         { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp",    ISD::SETCC_INVALID },
     286             :         { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
     287             :         { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp",    ISD::SETCC_INVALID },
     288             :         { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
     289             : 
     290             :         // Conversions between floating types.
     291             :         { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp",  ISD::SETCC_INVALID },
     292             :         { RTLIB::FPEXT_F32_F64,   "__extendsfdf2vfp", ISD::SETCC_INVALID },
     293             : 
     294             :         // Integer to floating-point conversions.
     295             :         // i64 conversions are done via library routines even when generating VFP
     296             :         // instructions, so use the same ones.
     297             :         // FIXME: There appears to be some naming inconsistency in ARM libgcc:
     298             :         // e.g., __floatunsidf vs. __floatunssidfvfp.
     299             :         { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp",    ISD::SETCC_INVALID },
     300             :         { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
     301             :         { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp",    ISD::SETCC_INVALID },
     302             :         { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
     303             :       };
     304             : 
     305       12390 :       for (const auto &LC : LibraryCalls) {
     306       12036 :         setLibcallName(LC.Op, LC.Name);
     307       12036 :         if (LC.Cond != ISD::SETCC_INVALID)
     308        5664 :           setCmpLibcallCC(LC.Op, LC.Cond);
     309             :       }
     310             :     }
     311             :   }
     312             : 
     313             :   // These libcalls are not available in 32-bit.
     314             :   setLibcallName(RTLIB::SHL_I128, nullptr);
     315             :   setLibcallName(RTLIB::SRL_I128, nullptr);
     316             :   setLibcallName(RTLIB::SRA_I128, nullptr);
     317             : 
     318             :   // RTLIB
     319        5050 :   if (Subtarget->isAAPCS_ABI() &&
     320        4301 :       (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
     321        1034 :        Subtarget->isTargetMuslAEABI() || Subtarget->isTargetAndroid())) {
     322             :     static const struct {
     323             :       const RTLIB::Libcall Op;
     324             :       const char * const Name;
     325             :       const CallingConv::ID CC;
     326             :       const ISD::CondCode Cond;
     327             :     } LibraryCalls[] = {
     328             :       // Double-precision floating-point arithmetic helper functions
     329             :       // RTABI chapter 4.1.2, Table 2
     330             :       { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     331             :       { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     332             :       { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     333             :       { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     334             : 
     335             :       // Double-precision floating-point comparison helper functions
     336             :       // RTABI chapter 4.1.2, Table 3
     337             :       { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
     338             :       { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
     339             :       { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
     340             :       { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
     341             :       { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
     342             :       { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
     343             :       { RTLIB::UO_F64,  "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
     344             :       { RTLIB::O_F64,   "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
     345             : 
     346             :       // Single-precision floating-point arithmetic helper functions
     347             :       // RTABI chapter 4.1.2, Table 4
     348             :       { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     349             :       { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     350             :       { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     351             :       { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     352             : 
     353             :       // Single-precision floating-point comparison helper functions
     354             :       // RTABI chapter 4.1.2, Table 5
     355             :       { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
     356             :       { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
     357             :       { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
     358             :       { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
     359             :       { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
     360             :       { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
     361             :       { RTLIB::UO_F32,  "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
     362             :       { RTLIB::O_F32,   "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
     363             : 
     364             :       // Floating-point to integer conversions.
     365             :       // RTABI chapter 4.1.2, Table 6
     366             :       { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     367             :       { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     368             :       { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     369             :       { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     370             :       { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     371             :       { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     372             :       { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     373             :       { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     374             : 
     375             :       // Conversions between floating types.
     376             :       // RTABI chapter 4.1.2, Table 7
     377             :       { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     378             :       { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     379             :       { RTLIB::FPEXT_F32_F64,   "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     380             : 
     381             :       // Integer to floating-point conversions.
     382             :       // RTABI chapter 4.1.2, Table 8
     383             :       { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     384             :       { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     385             :       { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     386             :       { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     387             :       { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     388             :       { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     389             :       { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     390             :       { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     391             : 
     392             :       // Long long helper functions
     393             :       // RTABI chapter 4.2, Table 9
     394             :       { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     395             :       { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     396             :       { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     397             :       { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     398             : 
     399             :       // Integer division functions
     400             :       // RTABI chapter 4.3.1
     401             :       { RTLIB::SDIV_I8,  "__aeabi_idiv",     CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     402             :       { RTLIB::SDIV_I16, "__aeabi_idiv",     CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     403             :       { RTLIB::SDIV_I32, "__aeabi_idiv",     CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     404             :       { RTLIB::SDIV_I64, "__aeabi_ldivmod",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     405             :       { RTLIB::UDIV_I8,  "__aeabi_uidiv",    CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     406             :       { RTLIB::UDIV_I16, "__aeabi_uidiv",    CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     407             :       { RTLIB::UDIV_I32, "__aeabi_uidiv",    CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     408             :       { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     409             :     };
     410             : 
     411      188048 :     for (const auto &LC : LibraryCalls) {
     412      184690 :       setLibcallName(LC.Op, LC.Name);
     413      184690 :       setLibcallCallingConv(LC.Op, LC.CC);
     414      184690 :       if (LC.Cond != ISD::SETCC_INVALID)
     415       53728 :         setCmpLibcallCC(LC.Op, LC.Cond);
     416             :     }
     417             : 
     418             :     // EABI dependent RTLIB
     419        3358 :     if (TM.Options.EABIVersion == EABI::EABI4 ||
     420             :         TM.Options.EABIVersion == EABI::EABI5) {
     421             :       static const struct {
     422             :         const RTLIB::Libcall Op;
     423             :         const char *const Name;
     424             :         const CallingConv::ID CC;
     425             :         const ISD::CondCode Cond;
     426             :       } MemOpsLibraryCalls[] = {
     427             :         // Memory operations
     428             :         // RTABI chapter 4.3.4
     429             :         { RTLIB::MEMCPY,  "__aeabi_memcpy",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     430             :         { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     431             :         { RTLIB::MEMSET,  "__aeabi_memset",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
     432             :       };
     433             : 
     434        8012 :       for (const auto &LC : MemOpsLibraryCalls) {
     435        6009 :         setLibcallName(LC.Op, LC.Name);
     436        6009 :         setLibcallCallingConv(LC.Op, LC.CC);
     437        6009 :         if (LC.Cond != ISD::SETCC_INVALID)
     438           0 :           setCmpLibcallCC(LC.Op, LC.Cond);
     439             :       }
     440             :     }
     441             :   }
     442             : 
     443       10100 :   if (Subtarget->isTargetWindows()) {
     444             :     static const struct {
     445             :       const RTLIB::Libcall Op;
     446             :       const char * const Name;
     447             :       const CallingConv::ID CC;
     448             :     } LibraryCalls[] = {
     449             :       { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
     450             :       { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
     451             :       { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
     452             :       { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
     453             :       { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
     454             :       { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
     455             :       { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
     456             :       { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
     457             :     };
     458             : 
     459         756 :     for (const auto &LC : LibraryCalls) {
     460         672 :       setLibcallName(LC.Op, LC.Name);
     461         672 :       setLibcallCallingConv(LC.Op, LC.CC);
     462             :     }
     463             :   }
     464             : 
     465             :   // Use divmod compiler-rt calls for iOS 5.0 and later.
     466        5050 :   if (Subtarget->isTargetMachO() &&
     467         382 :       !(Subtarget->isTargetIOS() &&
     468         382 :         Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
     469             :     setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
     470             :     setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
     471             :   }
     472             : 
     473             :   // The half <-> float conversion functions are always soft-float on
     474             :   // non-watchos platforms, but are needed for some targets which use a
     475             :   // hard-float calling convention by default.
     476       10100 :   if (!Subtarget->isTargetWatchABI()) {
     477        5025 :     if (Subtarget->isAAPCS_ABI()) {
     478             :       setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
     479             :       setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
     480             :       setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
     481             :     } else {
     482             :       setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
     483             :       setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
     484             :       setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
     485             :     }
     486             :   }
     487             : 
     488             :   // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
     489             :   // a __gnu_ prefix (which is the default).
     490        5050 :   if (Subtarget->isTargetAEABI()) {
     491             :     static const struct {
     492             :       const RTLIB::Libcall Op;
     493             :       const char * const Name;
     494             :       const CallingConv::ID CC;
     495             :     } LibraryCalls[] = {
     496             :       { RTLIB::FPROUND_F32_F16, "__aeabi_f2h", CallingConv::ARM_AAPCS },
     497             :       { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS },
     498             :       { RTLIB::FPEXT_F16_F32, "__aeabi_h2f", CallingConv::ARM_AAPCS },
     499             :     };
     500             : 
     501        7624 :     for (const auto &LC : LibraryCalls) {
     502        5718 :       setLibcallName(LC.Op, LC.Name);
     503        5718 :       setLibcallCallingConv(LC.Op, LC.CC);
     504             :     }
     505             :   }
     506             : 
     507        5050 :   if (Subtarget->isThumb1Only())
     508             :     addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
     509             :   else
     510             :     addRegisterClass(MVT::i32, &ARM::GPRRegClass);
     511             : 
     512        5050 :   if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
     513        3028 :       !Subtarget->isThumb1Only()) {
     514             :     addRegisterClass(MVT::f32, &ARM::SPRRegClass);
     515             :     addRegisterClass(MVT::f64, &ARM::DPRRegClass);
     516             :   }
     517             : 
     518        5050 :   if (Subtarget->hasFullFP16()) {
     519             :     addRegisterClass(MVT::f16, &ARM::HPRRegClass);
     520             :     setOperationAction(ISD::BITCAST, MVT::i16, Custom);
     521             :     setOperationAction(ISD::BITCAST, MVT::i32, Custom);
     522             :     setOperationAction(ISD::BITCAST, MVT::f16, Custom);
     523             : 
     524             :     setOperationAction(ISD::FMINNUM, MVT::f16, Legal);
     525             :     setOperationAction(ISD::FMAXNUM, MVT::f16, Legal);
     526             :   }
     527             : 
     528      484800 :   for (MVT VT : MVT::vector_valuetypes()) {
     529    46056000 :     for (MVT InnerVT : MVT::vector_valuetypes()) {
     530             :       setTruncStoreAction(VT, InnerVT, Expand);
     531             :       setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
     532             :       setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
     533             :       setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
     534             :     }
     535             : 
     536             :     setOperationAction(ISD::MULHS, VT, Expand);
     537             :     setOperationAction(ISD::SMUL_LOHI, VT, Expand);
     538             :     setOperationAction(ISD::MULHU, VT, Expand);
     539             :     setOperationAction(ISD::UMUL_LOHI, VT, Expand);
     540             : 
     541             :     setOperationAction(ISD::BSWAP, VT, Expand);
     542             :   }
     543             : 
     544             :   setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
     545             :   setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
     546             : 
     547             :   setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
     548             :   setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
     549             : 
     550        5050 :   if (Subtarget->hasNEON()) {
     551        2598 :     addDRTypeForNEON(MVT::v2f32);
     552        2598 :     addDRTypeForNEON(MVT::v8i8);
     553        2598 :     addDRTypeForNEON(MVT::v4i16);
     554        2598 :     addDRTypeForNEON(MVT::v2i32);
     555        2598 :     addDRTypeForNEON(MVT::v1i64);
     556             : 
     557        2598 :     addQRTypeForNEON(MVT::v4f32);
     558        2598 :     addQRTypeForNEON(MVT::v2f64);
     559        2598 :     addQRTypeForNEON(MVT::v16i8);
     560        2598 :     addQRTypeForNEON(MVT::v8i16);
     561        2598 :     addQRTypeForNEON(MVT::v4i32);
     562        2598 :     addQRTypeForNEON(MVT::v2i64);
     563             : 
     564        2598 :     if (Subtarget->hasFullFP16()) {
     565          35 :       addQRTypeForNEON(MVT::v8f16);
     566          35 :       addDRTypeForNEON(MVT::v4f16);
     567             :     }
     568             : 
     569             :     // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
     570             :     // neither Neon nor VFP support any arithmetic operations on it.
     571             :     // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
     572             :     // supported for v4f32.
     573             :     setOperationAction(ISD::FADD, MVT::v2f64, Expand);
     574             :     setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
     575             :     setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
     576             :     // FIXME: Code duplication: FDIV and FREM are expanded always, see
     577             :     // ARMTargetLowering::addTypeForNEON method for details.
     578             :     setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
     579             :     setOperationAction(ISD::FREM, MVT::v2f64, Expand);
     580             :     // FIXME: Create unittest.
     581             :     // In another words, find a way when "copysign" appears in DAG with vector
     582             :     // operands.
     583             :     setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
     584             :     // FIXME: Code duplication: SETCC has custom operation action, see
     585             :     // ARMTargetLowering::addTypeForNEON method for details.
     586             :     setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
     587             :     // FIXME: Create unittest for FNEG and for FABS.
     588             :     setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
     589             :     setOperationAction(ISD::FABS, MVT::v2f64, Expand);
     590             :     setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
     591             :     setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
     592             :     setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
     593             :     setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
     594             :     setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
     595             :     setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
     596             :     setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
     597             :     setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
     598             :     setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
     599             :     // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
     600             :     setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
     601             :     setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
     602             :     setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
     603             :     setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
     604             :     setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
     605             :     setOperationAction(ISD::FMA, MVT::v2f64, Expand);
     606             : 
     607             :     setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
     608             :     setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
     609             :     setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
     610             :     setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
     611             :     setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
     612             :     setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
     613             :     setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
     614             :     setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
     615             :     setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
     616             :     setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
     617             :     setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
     618             :     setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
     619             :     setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
     620             :     setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
     621             : 
     622             :     // Mark v2f32 intrinsics.
     623             :     setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
     624             :     setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
     625             :     setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
     626             :     setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
     627             :     setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
     628             :     setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
     629             :     setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
     630             :     setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
     631             :     setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
     632             :     setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
     633             :     setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
     634             :     setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
     635             :     setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
     636             :     setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
     637             : 
     638             :     // Neon does not support some operations on v1i64 and v2i64 types.
     639             :     setOperationAction(ISD::MUL, MVT::v1i64, Expand);
     640             :     // Custom handling for some quad-vector types to detect VMULL.
     641             :     setOperationAction(ISD::MUL, MVT::v8i16, Custom);
     642             :     setOperationAction(ISD::MUL, MVT::v4i32, Custom);
     643             :     setOperationAction(ISD::MUL, MVT::v2i64, Custom);
     644             :     // Custom handling for some vector types to avoid expensive expansions
     645             :     setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
     646             :     setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
     647             :     setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
     648             :     setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
     649             :     // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
     650             :     // a destination type that is wider than the source, and nor does
     651             :     // it have a FP_TO_[SU]INT instruction with a narrower destination than
     652             :     // source.
     653             :     setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
     654             :     setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Custom);
     655             :     setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
     656             :     setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
     657             :     setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
     658             :     setOperationAction(ISD::FP_TO_UINT, MVT::v8i16, Custom);
     659             :     setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
     660             :     setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
     661             : 
     662             :     setOperationAction(ISD::FP_ROUND,   MVT::v2f32, Expand);
     663             :     setOperationAction(ISD::FP_EXTEND,  MVT::v2f64, Expand);
     664             : 
     665             :     // NEON does not have single instruction CTPOP for vectors with element
     666             :     // types wider than 8-bits.  However, custom lowering can leverage the
     667             :     // v8i8/v16i8 vcnt instruction.
     668             :     setOperationAction(ISD::CTPOP,      MVT::v2i32, Custom);
     669             :     setOperationAction(ISD::CTPOP,      MVT::v4i32, Custom);
     670             :     setOperationAction(ISD::CTPOP,      MVT::v4i16, Custom);
     671             :     setOperationAction(ISD::CTPOP,      MVT::v8i16, Custom);
     672             :     setOperationAction(ISD::CTPOP,      MVT::v1i64, Custom);
     673             :     setOperationAction(ISD::CTPOP,      MVT::v2i64, Custom);
     674             : 
     675             :     setOperationAction(ISD::CTLZ,       MVT::v1i64, Expand);
     676             :     setOperationAction(ISD::CTLZ,       MVT::v2i64, Expand);
     677             : 
     678             :     // NEON does not have single instruction CTTZ for vectors.
     679             :     setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
     680             :     setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
     681             :     setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
     682             :     setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
     683             : 
     684             :     setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
     685             :     setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
     686             :     setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
     687             :     setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
     688             : 
     689             :     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
     690             :     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
     691             :     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
     692             :     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
     693             : 
     694             :     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
     695             :     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
     696             :     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
     697             :     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
     698             : 
     699             :     // NEON only has FMA instructions as of VFP4.
     700        2598 :     if (!Subtarget->hasVFP4()) {
     701             :       setOperationAction(ISD::FMA, MVT::v2f32, Expand);
     702             :       setOperationAction(ISD::FMA, MVT::v4f32, Expand);
     703             :     }
     704             : 
     705             :     setTargetDAGCombine(ISD::INTRINSIC_VOID);
     706             :     setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
     707             :     setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
     708             :     setTargetDAGCombine(ISD::SHL);
     709             :     setTargetDAGCombine(ISD::SRL);
     710             :     setTargetDAGCombine(ISD::SRA);
     711             :     setTargetDAGCombine(ISD::SIGN_EXTEND);
     712             :     setTargetDAGCombine(ISD::ZERO_EXTEND);
     713             :     setTargetDAGCombine(ISD::ANY_EXTEND);
     714             :     setTargetDAGCombine(ISD::BUILD_VECTOR);
     715             :     setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
     716             :     setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
     717             :     setTargetDAGCombine(ISD::STORE);
     718             :     setTargetDAGCombine(ISD::FP_TO_SINT);
     719             :     setTargetDAGCombine(ISD::FP_TO_UINT);
     720             :     setTargetDAGCombine(ISD::FDIV);
     721             :     setTargetDAGCombine(ISD::LOAD);
     722             : 
     723             :     // It is legal to extload from v4i8 to v4i16 or v4i32.
     724       15588 :     for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
     725       18186 :                    MVT::v2i32}) {
     726     1122336 :       for (MVT VT : MVT::integer_vector_valuetypes()) {
     727             :         setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
     728             :         setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
     729             :         setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
     730             :       }
     731             :     }
     732             :   }
     733             : 
     734        5050 :   if (Subtarget->isFPOnlySP()) {
     735             :     // When targeting a floating-point unit with only single-precision
     736             :     // operations, f64 is legal for the few double-precision instructions which
     737             :     // are present However, no double-precision operations other than moves,
     738             :     // loads and stores are provided by the hardware.
     739             :     setOperationAction(ISD::FADD,       MVT::f64, Expand);
     740             :     setOperationAction(ISD::FSUB,       MVT::f64, Expand);
     741             :     setOperationAction(ISD::FMUL,       MVT::f64, Expand);
     742             :     setOperationAction(ISD::FMA,        MVT::f64, Expand);
     743             :     setOperationAction(ISD::FDIV,       MVT::f64, Expand);
     744             :     setOperationAction(ISD::FREM,       MVT::f64, Expand);
     745             :     setOperationAction(ISD::FCOPYSIGN,  MVT::f64, Expand);
     746             :     setOperationAction(ISD::FGETSIGN,   MVT::f64, Expand);
     747             :     setOperationAction(ISD::FNEG,       MVT::f64, Expand);
     748             :     setOperationAction(ISD::FABS,       MVT::f64, Expand);
     749             :     setOperationAction(ISD::FSQRT,      MVT::f64, Expand);
     750             :     setOperationAction(ISD::FSIN,       MVT::f64, Expand);
     751             :     setOperationAction(ISD::FCOS,       MVT::f64, Expand);
     752             :     setOperationAction(ISD::FPOW,       MVT::f64, Expand);
     753             :     setOperationAction(ISD::FLOG,       MVT::f64, Expand);
     754             :     setOperationAction(ISD::FLOG2,      MVT::f64, Expand);
     755             :     setOperationAction(ISD::FLOG10,     MVT::f64, Expand);
     756             :     setOperationAction(ISD::FEXP,       MVT::f64, Expand);
     757             :     setOperationAction(ISD::FEXP2,      MVT::f64, Expand);
     758             :     setOperationAction(ISD::FCEIL,      MVT::f64, Expand);
     759             :     setOperationAction(ISD::FTRUNC,     MVT::f64, Expand);
     760             :     setOperationAction(ISD::FRINT,      MVT::f64, Expand);
     761             :     setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
     762             :     setOperationAction(ISD::FFLOOR,     MVT::f64, Expand);
     763             :     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
     764             :     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
     765             :     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
     766             :     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
     767             :     setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
     768             :     setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
     769             :     setOperationAction(ISD::FP_ROUND,   MVT::f32, Custom);
     770             :     setOperationAction(ISD::FP_EXTEND,  MVT::f64, Custom);
     771             :   }
     772             : 
     773        5050 :   computeRegisterProperties(Subtarget->getRegisterInfo());
     774             : 
     775             :   // ARM does not have floating-point extending loads.
     776       35350 :   for (MVT VT : MVT::fp_valuetypes()) {
     777             :     setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
     778             :     setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
     779             :   }
     780             : 
     781             :   // ... or truncating stores
     782             :   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
     783             :   setTruncStoreAction(MVT::f32, MVT::f16, Expand);
     784             :   setTruncStoreAction(MVT::f64, MVT::f16, Expand);
     785             : 
     786             :   // ARM does not have i1 sign extending load.
     787       35350 :   for (MVT VT : MVT::integer_valuetypes())
     788             :     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
     789             : 
     790             :   // ARM supports all 4 flavors of integer indexed load / store.
     791        5050 :   if (!Subtarget->isThumb1Only()) {
     792       18176 :     for (unsigned im = (unsigned)ISD::PRE_INC;
     793       22720 :          im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
     794             :       setIndexedLoadAction(im,  MVT::i1,  Legal);
     795             :       setIndexedLoadAction(im,  MVT::i8,  Legal);
     796             :       setIndexedLoadAction(im,  MVT::i16, Legal);
     797             :       setIndexedLoadAction(im,  MVT::i32, Legal);
     798             :       setIndexedStoreAction(im, MVT::i1,  Legal);
     799             :       setIndexedStoreAction(im, MVT::i8,  Legal);
     800             :       setIndexedStoreAction(im, MVT::i16, Legal);
     801             :       setIndexedStoreAction(im, MVT::i32, Legal);
     802             :     }
     803             :   } else {
     804             :     // Thumb-1 has limited post-inc load/store support - LDM r0!, {r1}.
     805             :     setIndexedLoadAction(ISD::POST_INC, MVT::i32,  Legal);
     806             :     setIndexedStoreAction(ISD::POST_INC, MVT::i32,  Legal);
     807             :   }
     808             : 
     809             :   setOperationAction(ISD::SADDO, MVT::i32, Custom);
     810             :   setOperationAction(ISD::UADDO, MVT::i32, Custom);
     811             :   setOperationAction(ISD::SSUBO, MVT::i32, Custom);
     812             :   setOperationAction(ISD::USUBO, MVT::i32, Custom);
     813             : 
     814             :   setOperationAction(ISD::ADDCARRY, MVT::i32, Custom);
     815             :   setOperationAction(ISD::SUBCARRY, MVT::i32, Custom);
     816             : 
     817             :   // i64 operation support.
     818             :   setOperationAction(ISD::MUL,     MVT::i64, Expand);
     819             :   setOperationAction(ISD::MULHU,   MVT::i32, Expand);
     820        5050 :   if (Subtarget->isThumb1Only()) {
     821             :     setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
     822             :     setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
     823             :   }
     824        6607 :   if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
     825        1557 :       || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
     826             :     setOperationAction(ISD::MULHS, MVT::i32, Expand);
     827             : 
     828             :   setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
     829             :   setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
     830             :   setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
     831             :   setOperationAction(ISD::SRL,       MVT::i64, Custom);
     832             :   setOperationAction(ISD::SRA,       MVT::i64, Custom);
     833             :   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
     834             : 
     835             :   // Expand to __aeabi_l{lsl,lsr,asr} calls for Thumb1.
     836        5050 :   if (Subtarget->isThumb1Only()) {
     837             :     setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
     838             :     setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
     839             :     setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
     840             :   }
     841             : 
     842        6607 :   if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
     843             :     setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
     844             : 
     845             :   // ARM does not have ROTL.
     846             :   setOperationAction(ISD::ROTL, MVT::i32, Expand);
     847      484800 :   for (MVT VT : MVT::vector_valuetypes()) {
     848             :     setOperationAction(ISD::ROTL, VT, Expand);
     849             :     setOperationAction(ISD::ROTR, VT, Expand);
     850             :   }
     851             :   setOperationAction(ISD::CTTZ,  MVT::i32, Custom);
     852             :   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
     853        5050 :   if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) {
     854             :     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
     855             :     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, LibCall);
     856             :   }
     857             : 
     858             :   // @llvm.readcyclecounter requires the Performance Monitors extension.
     859             :   // Default to the 0 expansion on unsupported platforms.
     860             :   // FIXME: Technically there are older ARM CPUs that have
     861             :   // implementation-specific ways of obtaining this information.
     862        5050 :   if (Subtarget->hasPerfMon())
     863             :     setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
     864             : 
     865             :   // Only ARMv6 has BSWAP.
     866        5050 :   if (!Subtarget->hasV6Ops())
     867             :     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
     868             : 
     869        5050 :   bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode()
     870        2987 :                                         : Subtarget->hasDivideInARMMode();
     871        5050 :   if (!hasDivide) {
     872             :     // These are expanded into libcalls if the cpu doesn't have HW divider.
     873             :     setOperationAction(ISD::SDIV,  MVT::i32, LibCall);
     874             :     setOperationAction(ISD::UDIV,  MVT::i32, LibCall);
     875             :   }
     876             : 
     877        5050 :   if (Subtarget->isTargetWindows() && !Subtarget->hasDivideInThumbMode()) {
     878             :     setOperationAction(ISD::SDIV, MVT::i32, Custom);
     879             :     setOperationAction(ISD::UDIV, MVT::i32, Custom);
     880             : 
     881             :     setOperationAction(ISD::SDIV, MVT::i64, Custom);
     882             :     setOperationAction(ISD::UDIV, MVT::i64, Custom);
     883             :   }
     884             : 
     885             :   setOperationAction(ISD::SREM,  MVT::i32, Expand);
     886             :   setOperationAction(ISD::UREM,  MVT::i32, Expand);
     887             : 
     888             :   // Register based DivRem for AEABI (RTABI 4.2)
     889        8194 :   if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
     890        1690 :       Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() ||
     891             :       Subtarget->isTargetWindows()) {
     892             :     setOperationAction(ISD::SREM, MVT::i64, Custom);
     893             :     setOperationAction(ISD::UREM, MVT::i64, Custom);
     894        3444 :     HasStandaloneRem = false;
     895             : 
     896        3444 :     if (Subtarget->isTargetWindows()) {
     897             :       const struct {
     898             :         const RTLIB::Libcall Op;
     899             :         const char * const Name;
     900             :         const CallingConv::ID CC;
     901          84 :       } LibraryCalls[] = {
     902             :         { RTLIB::SDIVREM_I8, "__rt_sdiv", CallingConv::ARM_AAPCS },
     903             :         { RTLIB::SDIVREM_I16, "__rt_sdiv", CallingConv::ARM_AAPCS },
     904             :         { RTLIB::SDIVREM_I32, "__rt_sdiv", CallingConv::ARM_AAPCS },
     905             :         { RTLIB::SDIVREM_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS },
     906             : 
     907             :         { RTLIB::UDIVREM_I8, "__rt_udiv", CallingConv::ARM_AAPCS },
     908             :         { RTLIB::UDIVREM_I16, "__rt_udiv", CallingConv::ARM_AAPCS },
     909             :         { RTLIB::UDIVREM_I32, "__rt_udiv", CallingConv::ARM_AAPCS },
     910             :         { RTLIB::UDIVREM_I64, "__rt_udiv64", CallingConv::ARM_AAPCS },
     911             :       };
     912             : 
     913         756 :       for (const auto &LC : LibraryCalls) {
     914         672 :         setLibcallName(LC.Op, LC.Name);
     915         672 :         setLibcallCallingConv(LC.Op, LC.CC);
     916             :       }
     917             :     } else {
     918             :       const struct {
     919             :         const RTLIB::Libcall Op;
     920             :         const char * const Name;
     921             :         const CallingConv::ID CC;
     922        3360 :       } LibraryCalls[] = {
     923             :         { RTLIB::SDIVREM_I8, "__aeabi_idivmod", CallingConv::ARM_AAPCS },
     924             :         { RTLIB::SDIVREM_I16, "__aeabi_idivmod", CallingConv::ARM_AAPCS },
     925             :         { RTLIB::SDIVREM_I32, "__aeabi_idivmod", CallingConv::ARM_AAPCS },
     926             :         { RTLIB::SDIVREM_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS },
     927             : 
     928             :         { RTLIB::UDIVREM_I8, "__aeabi_uidivmod", CallingConv::ARM_AAPCS },
     929             :         { RTLIB::UDIVREM_I16, "__aeabi_uidivmod", CallingConv::ARM_AAPCS },
     930             :         { RTLIB::UDIVREM_I32, "__aeabi_uidivmod", CallingConv::ARM_AAPCS },
     931             :         { RTLIB::UDIVREM_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS },
     932             :       };
     933             : 
     934       30240 :       for (const auto &LC : LibraryCalls) {
     935       26880 :         setLibcallName(LC.Op, LC.Name);
     936       26880 :         setLibcallCallingConv(LC.Op, LC.CC);
     937             :       }
     938             :     }
     939             : 
     940             :     setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
     941             :     setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
     942             :     setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
     943             :     setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
     944             :   } else {
     945             :     setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
     946             :     setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
     947             :   }
     948             : 
     949       10100 :   if (Subtarget->isTargetWindows() && Subtarget->getTargetTriple().isOSMSVCRT())
     950         252 :     for (auto &VT : {MVT::f32, MVT::f64})
     951         168 :       setOperationAction(ISD::FPOWI, VT, Custom);
     952             : 
     953             :   setOperationAction(ISD::GlobalAddress, MVT::i32,   Custom);
     954             :   setOperationAction(ISD::ConstantPool,  MVT::i32,   Custom);
     955             :   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
     956             :   setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
     957             : 
     958             :   setOperationAction(ISD::TRAP, MVT::Other, Legal);
     959             : 
     960             :   // Use the default implementation.
     961             :   setOperationAction(ISD::VASTART,            MVT::Other, Custom);
     962             :   setOperationAction(ISD::VAARG,              MVT::Other, Expand);
     963             :   setOperationAction(ISD::VACOPY,             MVT::Other, Expand);
     964             :   setOperationAction(ISD::VAEND,              MVT::Other, Expand);
     965             :   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
     966             :   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
     967             : 
     968       10100 :   if (Subtarget->isTargetWindows())
     969             :     setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
     970             :   else
     971             :     setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
     972             : 
     973             :   // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
     974             :   // the default expansion.
     975        5050 :   InsertFencesForAtomic = false;
     976        3428 :   if (Subtarget->hasAnyDataBarrier() &&
     977        3428 :       (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) {
     978             :     // ATOMIC_FENCE needs custom lowering; the others should have been expanded
     979             :     // to ldrex/strex loops already.
     980             :     setOperationAction(ISD::ATOMIC_FENCE,     MVT::Other, Custom);
     981        3217 :     if (!Subtarget->isThumb() || !Subtarget->isMClass())
     982             :       setOperationAction(ISD::ATOMIC_CMP_SWAP,  MVT::i64, Custom);
     983             : 
     984             :     // On v8, we have particularly efficient implementations of atomic fences
     985             :     // if they can be combined with nearby atomic loads and stores.
     986        3217 :     if (!Subtarget->hasV8Ops() || getTargetMachine().getOptLevel() == 0) {
     987             :       // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
     988        2894 :       InsertFencesForAtomic = true;
     989             :     }
     990             :   } else {
     991             :     // If there's anything we can use as a barrier, go through custom lowering
     992             :     // for ATOMIC_FENCE.
     993             :     // If target has DMB in thumb, Fences can be inserted.
     994        1833 :     if (Subtarget->hasDataBarrier())
     995         211 :       InsertFencesForAtomic = true;
     996             : 
     997             :     setOperationAction(ISD::ATOMIC_FENCE,   MVT::Other,
     998             :                        Subtarget->hasAnyDataBarrier() ? Custom : Expand);
     999             : 
    1000             :     // Set them all for expansion, which will force libcalls.
    1001             :     setOperationAction(ISD::ATOMIC_CMP_SWAP,  MVT::i32, Expand);
    1002             :     setOperationAction(ISD::ATOMIC_SWAP,      MVT::i32, Expand);
    1003             :     setOperationAction(ISD::ATOMIC_LOAD_ADD,  MVT::i32, Expand);
    1004             :     setOperationAction(ISD::ATOMIC_LOAD_SUB,  MVT::i32, Expand);
    1005             :     setOperationAction(ISD::ATOMIC_LOAD_AND,  MVT::i32, Expand);
    1006             :     setOperationAction(ISD::ATOMIC_LOAD_OR,   MVT::i32, Expand);
    1007             :     setOperationAction(ISD::ATOMIC_LOAD_XOR,  MVT::i32, Expand);
    1008             :     setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
    1009             :     setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
    1010             :     setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
    1011             :     setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
    1012             :     setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
    1013             :     // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
    1014             :     // Unordered/Monotonic case.
    1015        1833 :     if (!InsertFencesForAtomic) {
    1016             :       setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
    1017             :       setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
    1018             :     }
    1019             :   }
    1020             : 
    1021             :   setOperationAction(ISD::PREFETCH,         MVT::Other, Custom);
    1022             : 
    1023             :   // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
    1024        5050 :   if (!Subtarget->hasV6Ops()) {
    1025             :     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
    1026             :     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
    1027             :   }
    1028             :   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
    1029             : 
    1030        5050 :   if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
    1031        3028 :       !Subtarget->isThumb1Only()) {
    1032             :     // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
    1033             :     // iff target supports vfp2.
    1034             :     setOperationAction(ISD::BITCAST, MVT::i64, Custom);
    1035             :     setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
    1036             :   }
    1037             : 
    1038             :   // We want to custom lower some of our intrinsics.
    1039             :   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
    1040             :   setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
    1041             :   setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
    1042             :   setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
    1043        5050 :   if (Subtarget->useSjLjEH())
    1044             :     setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
    1045             : 
    1046             :   setOperationAction(ISD::SETCC,     MVT::i32, Expand);
    1047             :   setOperationAction(ISD::SETCC,     MVT::f32, Expand);
    1048             :   setOperationAction(ISD::SETCC,     MVT::f64, Expand);
    1049             :   setOperationAction(ISD::SELECT,    MVT::i32, Custom);
    1050             :   setOperationAction(ISD::SELECT,    MVT::f32, Custom);
    1051             :   setOperationAction(ISD::SELECT,    MVT::f64, Custom);
    1052             :   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
    1053             :   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
    1054             :   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
    1055        5050 :   if (Subtarget->hasFullFP16()) {
    1056             :     setOperationAction(ISD::SETCC,     MVT::f16, Expand);
    1057             :     setOperationAction(ISD::SELECT,    MVT::f16, Custom);
    1058             :     setOperationAction(ISD::SELECT_CC, MVT::f16, Custom);
    1059             :   }
    1060             : 
    1061             :   setOperationAction(ISD::SETCCCARRY, MVT::i32, Custom);
    1062             : 
    1063             :   setOperationAction(ISD::BRCOND,    MVT::Other, Custom);
    1064             :   setOperationAction(ISD::BR_CC,     MVT::i32,   Custom);
    1065        5050 :   if (Subtarget->hasFullFP16())
    1066             :       setOperationAction(ISD::BR_CC, MVT::f16,   Custom);
    1067             :   setOperationAction(ISD::BR_CC,     MVT::f32,   Custom);
    1068             :   setOperationAction(ISD::BR_CC,     MVT::f64,   Custom);
    1069             :   setOperationAction(ISD::BR_JT,     MVT::Other, Custom);
    1070             : 
    1071             :   // We don't support sin/cos/fmod/copysign/pow
    1072             :   setOperationAction(ISD::FSIN,      MVT::f64, Expand);
    1073             :   setOperationAction(ISD::FSIN,      MVT::f32, Expand);
    1074             :   setOperationAction(ISD::FCOS,      MVT::f32, Expand);
    1075             :   setOperationAction(ISD::FCOS,      MVT::f64, Expand);
    1076             :   setOperationAction(ISD::FSINCOS,   MVT::f64, Expand);
    1077             :   setOperationAction(ISD::FSINCOS,   MVT::f32, Expand);
    1078             :   setOperationAction(ISD::FREM,      MVT::f64, Expand);
    1079             :   setOperationAction(ISD::FREM,      MVT::f32, Expand);
    1080        5050 :   if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
    1081        3028 :       !Subtarget->isThumb1Only()) {
    1082             :     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
    1083             :     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
    1084             :   }
    1085             :   setOperationAction(ISD::FPOW,      MVT::f64, Expand);
    1086             :   setOperationAction(ISD::FPOW,      MVT::f32, Expand);
    1087             : 
    1088        5050 :   if (!Subtarget->hasVFP4()) {
    1089             :     setOperationAction(ISD::FMA, MVT::f64, Expand);
    1090             :     setOperationAction(ISD::FMA, MVT::f32, Expand);
    1091             :   }
    1092             : 
    1093             :   // Various VFP goodness
    1094        5050 :   if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
    1095             :     // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
    1096        4507 :     if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
    1097             :       setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
    1098             :       setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
    1099             :     }
    1100             : 
    1101             :     // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
    1102        4507 :     if (!Subtarget->hasFP16()) {
    1103             :       setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
    1104             :       setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
    1105             :     }
    1106             :   }
    1107             : 
    1108             :   // Use __sincos_stret if available.
    1109        5050 :   if (getLibcallName(RTLIB::SINCOS_STRET_F32) != nullptr &&
    1110             :       getLibcallName(RTLIB::SINCOS_STRET_F64) != nullptr) {
    1111             :     setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
    1112             :     setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
    1113             :   }
    1114             : 
    1115             :   // FP-ARMv8 implements a lot of rounding-like FP operations.
    1116        5050 :   if (Subtarget->hasFPARMv8()) {
    1117             :     setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
    1118             :     setOperationAction(ISD::FCEIL, MVT::f32, Legal);
    1119             :     setOperationAction(ISD::FROUND, MVT::f32, Legal);
    1120             :     setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
    1121             :     setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
    1122             :     setOperationAction(ISD::FRINT, MVT::f32, Legal);
    1123             :     setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
    1124             :     setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
    1125             :     setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
    1126             :     setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
    1127             :     setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
    1128             :     setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
    1129             : 
    1130         465 :     if (!Subtarget->isFPOnlySP()) {
    1131             :       setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
    1132             :       setOperationAction(ISD::FCEIL, MVT::f64, Legal);
    1133             :       setOperationAction(ISD::FROUND, MVT::f64, Legal);
    1134             :       setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
    1135             :       setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
    1136             :       setOperationAction(ISD::FRINT, MVT::f64, Legal);
    1137             :       setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
    1138             :       setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
    1139             :     }
    1140             :   }
    1141             : 
    1142        5050 :   if (Subtarget->hasNEON()) {
    1143             :     // vmin and vmax aren't available in a scalar form, so we use
    1144             :     // a NEON instruction with an undef lane instead.
    1145             :     setOperationAction(ISD::FMINNAN, MVT::f16, Legal);
    1146             :     setOperationAction(ISD::FMAXNAN, MVT::f16, Legal);
    1147             :     setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
    1148             :     setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
    1149             :     setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
    1150             :     setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
    1151             :     setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
    1152             :     setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
    1153             : 
    1154        2598 :     if (Subtarget->hasFullFP16()) {
    1155             :       setOperationAction(ISD::FMINNUM, MVT::v4f16, Legal);
    1156             :       setOperationAction(ISD::FMAXNUM, MVT::v4f16, Legal);
    1157             :       setOperationAction(ISD::FMINNUM, MVT::v8f16, Legal);
    1158             :       setOperationAction(ISD::FMAXNUM, MVT::v8f16, Legal);
    1159             : 
    1160             :       setOperationAction(ISD::FMINNAN, MVT::v4f16, Legal);
    1161             :       setOperationAction(ISD::FMAXNAN, MVT::v4f16, Legal);
    1162             :       setOperationAction(ISD::FMINNAN, MVT::v8f16, Legal);
    1163             :       setOperationAction(ISD::FMAXNAN, MVT::v8f16, Legal);
    1164             :     }
    1165             :   }
    1166             : 
    1167             :   // We have target-specific dag combine patterns for the following nodes:
    1168             :   // ARMISD::VMOVRRD  - No need to call setTargetDAGCombine
    1169             :   setTargetDAGCombine(ISD::ADD);
    1170             :   setTargetDAGCombine(ISD::SUB);
    1171             :   setTargetDAGCombine(ISD::MUL);
    1172             :   setTargetDAGCombine(ISD::AND);
    1173             :   setTargetDAGCombine(ISD::OR);
    1174             :   setTargetDAGCombine(ISD::XOR);
    1175             : 
    1176        5050 :   if (Subtarget->hasV6Ops())
    1177             :     setTargetDAGCombine(ISD::SRL);
    1178             : 
    1179             :   setStackPointerRegisterToSaveRestore(ARM::SP);
    1180             : 
    1181        5050 :   if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
    1182        4507 :       !Subtarget->hasVFP2())
    1183             :     setSchedulingPreference(Sched::RegPressure);
    1184             :   else
    1185             :     setSchedulingPreference(Sched::Hybrid);
    1186             : 
    1187             :   //// temporary - rewrite interface to use type
    1188        5050 :   MaxStoresPerMemset = 8;
    1189        5050 :   MaxStoresPerMemsetOptSize = 4;
    1190        5050 :   MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
    1191        5050 :   MaxStoresPerMemcpyOptSize = 2;
    1192        5050 :   MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
    1193        5050 :   MaxStoresPerMemmoveOptSize = 2;
    1194             : 
    1195             :   // On ARM arguments smaller than 4 bytes are extended, so all arguments
    1196             :   // are at least 4 bytes aligned.
    1197             :   setMinStackArgumentAlignment(4);
    1198             : 
    1199             :   // Prefer likely predicted branches to selects on out-of-order cores.
    1200        5050 :   PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder();
    1201             : 
    1202        5050 :   setPrefLoopAlignment(Subtarget->getPrefLoopAlignment());
    1203             : 
    1204        5050 :   setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
    1205        5050 : }
    1206             : 
    1207        4386 : bool ARMTargetLowering::useSoftFloat() const {
    1208        4386 :   return Subtarget->useSoftFloat();
    1209             : }
    1210             : 
    1211             : // FIXME: It might make sense to define the representative register class as the
    1212             : // nearest super-register that has a non-null superset. For example, DPR_VFP2 is
    1213             : // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
    1214             : // SPR's representative would be DPR_VFP2. This should work well if register
    1215             : // pressure tracking were modified such that a register use would increment the
    1216             : // pressure of the register class's representative and all of it's super
    1217             : // classes' representatives transitively. We have not implemented this because
    1218             : // of the difficulty prior to coalescing of modeling operand register classes
    1219             : // due to the common occurrence of cross class copies and subregister insertions
    1220             : // and extractions.
    1221             : std::pair<const TargetRegisterClass *, uint8_t>
    1222      575700 : ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
    1223             :                                            MVT VT) const {
    1224             :   const TargetRegisterClass *RRC = nullptr;
    1225             :   uint8_t Cost = 1;
    1226      575700 :   switch (VT.SimpleTy) {
    1227      499950 :   default:
    1228      499950 :     return TargetLowering::findRepresentativeClass(TRI, VT);
    1229             :   // Use DPR as representative register class for all floating point
    1230             :   // and vector types. Since there are 32 SPR registers and 32 DPR registers so
    1231             :   // the cost is 1 for both f32 and f64.
    1232       35350 :   case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
    1233             :   case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
    1234             :     RRC = &ARM::DPRRegClass;
    1235             :     // When NEON is used for SP, only half of the register file is available
    1236             :     // because operations that define both SP and DP results will be constrained
    1237             :     // to the VFP2 class (D0-D15). We currently model this constraint prior to
    1238             :     // coalescing by double-counting the SP regs. See the FIXME above.
    1239       35350 :     if (Subtarget->useNEONForSinglePrecisionFP())
    1240             :       Cost = 2;
    1241             :     break;
    1242             :   case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
    1243             :   case MVT::v4f32: case MVT::v2f64:
    1244             :     RRC = &ARM::DPRRegClass;
    1245             :     Cost = 2;
    1246             :     break;
    1247        5050 :   case MVT::v4i64:
    1248             :     RRC = &ARM::DPRRegClass;
    1249             :     Cost = 4;
    1250        5050 :     break;
    1251        5050 :   case MVT::v8i64:
    1252             :     RRC = &ARM::DPRRegClass;
    1253             :     Cost = 8;
    1254        5050 :     break;
    1255             :   }
    1256             :   return std::make_pair(RRC, Cost);
    1257             : }
    1258             : 
    1259           0 : const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
    1260           0 :   switch ((ARMISD::NodeType)Opcode) {
    1261             :   case ARMISD::FIRST_NUMBER:  break;
    1262             :   case ARMISD::Wrapper:       return "ARMISD::Wrapper";
    1263           0 :   case ARMISD::WrapperPIC:    return "ARMISD::WrapperPIC";
    1264           0 :   case ARMISD::WrapperJT:     return "ARMISD::WrapperJT";
    1265           0 :   case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
    1266           0 :   case ARMISD::CALL:          return "ARMISD::CALL";
    1267           0 :   case ARMISD::CALL_PRED:     return "ARMISD::CALL_PRED";
    1268           0 :   case ARMISD::CALL_NOLINK:   return "ARMISD::CALL_NOLINK";
    1269           0 :   case ARMISD::BRCOND:        return "ARMISD::BRCOND";
    1270           0 :   case ARMISD::BR_JT:         return "ARMISD::BR_JT";
    1271           0 :   case ARMISD::BR2_JT:        return "ARMISD::BR2_JT";
    1272           0 :   case ARMISD::RET_FLAG:      return "ARMISD::RET_FLAG";
    1273           0 :   case ARMISD::INTRET_FLAG:   return "ARMISD::INTRET_FLAG";
    1274           0 :   case ARMISD::PIC_ADD:       return "ARMISD::PIC_ADD";
    1275           0 :   case ARMISD::CMP:           return "ARMISD::CMP";
    1276           0 :   case ARMISD::CMN:           return "ARMISD::CMN";
    1277           0 :   case ARMISD::CMPZ:          return "ARMISD::CMPZ";
    1278           0 :   case ARMISD::CMPFP:         return "ARMISD::CMPFP";
    1279           0 :   case ARMISD::CMPFPw0:       return "ARMISD::CMPFPw0";
    1280           0 :   case ARMISD::BCC_i64:       return "ARMISD::BCC_i64";
    1281           0 :   case ARMISD::FMSTAT:        return "ARMISD::FMSTAT";
    1282             : 
    1283           0 :   case ARMISD::CMOV:          return "ARMISD::CMOV";
    1284             : 
    1285           0 :   case ARMISD::SSAT:          return "ARMISD::SSAT";
    1286           0 :   case ARMISD::USAT:          return "ARMISD::USAT";
    1287             : 
    1288           0 :   case ARMISD::SRL_FLAG:      return "ARMISD::SRL_FLAG";
    1289           0 :   case ARMISD::SRA_FLAG:      return "ARMISD::SRA_FLAG";
    1290           0 :   case ARMISD::RRX:           return "ARMISD::RRX";
    1291             : 
    1292           0 :   case ARMISD::ADDC:          return "ARMISD::ADDC";
    1293           0 :   case ARMISD::ADDE:          return "ARMISD::ADDE";
    1294           0 :   case ARMISD::SUBC:          return "ARMISD::SUBC";
    1295           0 :   case ARMISD::SUBE:          return "ARMISD::SUBE";
    1296             : 
    1297           0 :   case ARMISD::VMOVRRD:       return "ARMISD::VMOVRRD";
    1298           0 :   case ARMISD::VMOVDRR:       return "ARMISD::VMOVDRR";
    1299           0 :   case ARMISD::VMOVhr:        return "ARMISD::VMOVhr";
    1300           0 :   case ARMISD::VMOVrh:        return "ARMISD::VMOVrh";
    1301           0 :   case ARMISD::VMOVSR:        return "ARMISD::VMOVSR";
    1302             : 
    1303           0 :   case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
    1304           0 :   case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
    1305           0 :   case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
    1306             : 
    1307           0 :   case ARMISD::TC_RETURN:     return "ARMISD::TC_RETURN";
    1308             : 
    1309           0 :   case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
    1310             : 
    1311           0 :   case ARMISD::DYN_ALLOC:     return "ARMISD::DYN_ALLOC";
    1312             : 
    1313           0 :   case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
    1314             : 
    1315           0 :   case ARMISD::PRELOAD:       return "ARMISD::PRELOAD";
    1316             : 
    1317           0 :   case ARMISD::WIN__CHKSTK:   return "ARMISD::WIN__CHKSTK";
    1318           0 :   case ARMISD::WIN__DBZCHK:   return "ARMISD::WIN__DBZCHK";
    1319             : 
    1320           0 :   case ARMISD::VCEQ:          return "ARMISD::VCEQ";
    1321           0 :   case ARMISD::VCEQZ:         return "ARMISD::VCEQZ";
    1322           0 :   case ARMISD::VCGE:          return "ARMISD::VCGE";
    1323           0 :   case ARMISD::VCGEZ:         return "ARMISD::VCGEZ";
    1324           0 :   case ARMISD::VCLEZ:         return "ARMISD::VCLEZ";
    1325           0 :   case ARMISD::VCGEU:         return "ARMISD::VCGEU";
    1326           0 :   case ARMISD::VCGT:          return "ARMISD::VCGT";
    1327           0 :   case ARMISD::VCGTZ:         return "ARMISD::VCGTZ";
    1328           0 :   case ARMISD::VCLTZ:         return "ARMISD::VCLTZ";
    1329           0 :   case ARMISD::VCGTU:         return "ARMISD::VCGTU";
    1330           0 :   case ARMISD::VTST:          return "ARMISD::VTST";
    1331             : 
    1332           0 :   case ARMISD::VSHL:          return "ARMISD::VSHL";
    1333           0 :   case ARMISD::VSHRs:         return "ARMISD::VSHRs";
    1334           0 :   case ARMISD::VSHRu:         return "ARMISD::VSHRu";
    1335           0 :   case ARMISD::VRSHRs:        return "ARMISD::VRSHRs";
    1336           0 :   case ARMISD::VRSHRu:        return "ARMISD::VRSHRu";
    1337           0 :   case ARMISD::VRSHRN:        return "ARMISD::VRSHRN";
    1338           0 :   case ARMISD::VQSHLs:        return "ARMISD::VQSHLs";
    1339           0 :   case ARMISD::VQSHLu:        return "ARMISD::VQSHLu";
    1340           0 :   case ARMISD::VQSHLsu:       return "ARMISD::VQSHLsu";
    1341           0 :   case ARMISD::VQSHRNs:       return "ARMISD::VQSHRNs";
    1342           0 :   case ARMISD::VQSHRNu:       return "ARMISD::VQSHRNu";
    1343           0 :   case ARMISD::VQSHRNsu:      return "ARMISD::VQSHRNsu";
    1344           0 :   case ARMISD::VQRSHRNs:      return "ARMISD::VQRSHRNs";
    1345           0 :   case ARMISD::VQRSHRNu:      return "ARMISD::VQRSHRNu";
    1346           0 :   case ARMISD::VQRSHRNsu:     return "ARMISD::VQRSHRNsu";
    1347           0 :   case ARMISD::VSLI:          return "ARMISD::VSLI";
    1348           0 :   case ARMISD::VSRI:          return "ARMISD::VSRI";
    1349           0 :   case ARMISD::VGETLANEu:     return "ARMISD::VGETLANEu";
    1350           0 :   case ARMISD::VGETLANEs:     return "ARMISD::VGETLANEs";
    1351           0 :   case ARMISD::VMOVIMM:       return "ARMISD::VMOVIMM";
    1352           0 :   case ARMISD::VMVNIMM:       return "ARMISD::VMVNIMM";
    1353           0 :   case ARMISD::VMOVFPIMM:     return "ARMISD::VMOVFPIMM";
    1354           0 :   case ARMISD::VDUP:          return "ARMISD::VDUP";
    1355           0 :   case ARMISD::VDUPLANE:      return "ARMISD::VDUPLANE";
    1356           0 :   case ARMISD::VEXT:          return "ARMISD::VEXT";
    1357           0 :   case ARMISD::VREV64:        return "ARMISD::VREV64";
    1358           0 :   case ARMISD::VREV32:        return "ARMISD::VREV32";
    1359           0 :   case ARMISD::VREV16:        return "ARMISD::VREV16";
    1360           0 :   case ARMISD::VZIP:          return "ARMISD::VZIP";
    1361           0 :   case ARMISD::VUZP:          return "ARMISD::VUZP";
    1362           0 :   case ARMISD::VTRN:          return "ARMISD::VTRN";
    1363           0 :   case ARMISD::VTBL1:         return "ARMISD::VTBL1";
    1364           0 :   case ARMISD::VTBL2:         return "ARMISD::VTBL2";
    1365           0 :   case ARMISD::VMULLs:        return "ARMISD::VMULLs";
    1366           0 :   case ARMISD::VMULLu:        return "ARMISD::VMULLu";
    1367           0 :   case ARMISD::UMAAL:         return "ARMISD::UMAAL";
    1368           0 :   case ARMISD::UMLAL:         return "ARMISD::UMLAL";
    1369           0 :   case ARMISD::SMLAL:         return "ARMISD::SMLAL";
    1370           0 :   case ARMISD::SMLALBB:       return "ARMISD::SMLALBB";
    1371           0 :   case ARMISD::SMLALBT:       return "ARMISD::SMLALBT";
    1372           0 :   case ARMISD::SMLALTB:       return "ARMISD::SMLALTB";
    1373           0 :   case ARMISD::SMLALTT:       return "ARMISD::SMLALTT";
    1374           0 :   case ARMISD::SMULWB:        return "ARMISD::SMULWB";
    1375           0 :   case ARMISD::SMULWT:        return "ARMISD::SMULWT";
    1376           0 :   case ARMISD::SMLALD:        return "ARMISD::SMLALD";
    1377           0 :   case ARMISD::SMLALDX:       return "ARMISD::SMLALDX";
    1378           0 :   case ARMISD::SMLSLD:        return "ARMISD::SMLSLD";
    1379           0 :   case ARMISD::SMLSLDX:       return "ARMISD::SMLSLDX";
    1380           0 :   case ARMISD::SMMLAR:        return "ARMISD::SMMLAR";
    1381           0 :   case ARMISD::SMMLSR:        return "ARMISD::SMMLSR";
    1382           0 :   case ARMISD::BUILD_VECTOR:  return "ARMISD::BUILD_VECTOR";
    1383           0 :   case ARMISD::BFI:           return "ARMISD::BFI";
    1384           0 :   case ARMISD::VORRIMM:       return "ARMISD::VORRIMM";
    1385           0 :   case ARMISD::VBICIMM:       return "ARMISD::VBICIMM";
    1386           0 :   case ARMISD::VBSL:          return "ARMISD::VBSL";
    1387           0 :   case ARMISD::MEMCPY:        return "ARMISD::MEMCPY";
    1388           0 :   case ARMISD::VLD1DUP:       return "ARMISD::VLD1DUP";
    1389           0 :   case ARMISD::VLD2DUP:       return "ARMISD::VLD2DUP";
    1390           0 :   case ARMISD::VLD3DUP:       return "ARMISD::VLD3DUP";
    1391           0 :   case ARMISD::VLD4DUP:       return "ARMISD::VLD4DUP";
    1392           0 :   case ARMISD::VLD1_UPD:      return "ARMISD::VLD1_UPD";
    1393           0 :   case ARMISD::VLD2_UPD:      return "ARMISD::VLD2_UPD";
    1394           0 :   case ARMISD::VLD3_UPD:      return "ARMISD::VLD3_UPD";
    1395           0 :   case ARMISD::VLD4_UPD:      return "ARMISD::VLD4_UPD";
    1396           0 :   case ARMISD::VLD2LN_UPD:    return "ARMISD::VLD2LN_UPD";
    1397           0 :   case ARMISD::VLD3LN_UPD:    return "ARMISD::VLD3LN_UPD";
    1398           0 :   case ARMISD::VLD4LN_UPD:    return "ARMISD::VLD4LN_UPD";
    1399           0 :   case ARMISD::VLD1DUP_UPD:   return "ARMISD::VLD1DUP_UPD";
    1400           0 :   case ARMISD::VLD2DUP_UPD:   return "ARMISD::VLD2DUP_UPD";
    1401           0 :   case ARMISD::VLD3DUP_UPD:   return "ARMISD::VLD3DUP_UPD";
    1402           0 :   case ARMISD::VLD4DUP_UPD:   return "ARMISD::VLD4DUP_UPD";
    1403           0 :   case ARMISD::VST1_UPD:      return "ARMISD::VST1_UPD";
    1404           0 :   case ARMISD::VST2_UPD:      return "ARMISD::VST2_UPD";
    1405           0 :   case ARMISD::VST3_UPD:      return "ARMISD::VST3_UPD";
    1406           0 :   case ARMISD::VST4_UPD:      return "ARMISD::VST4_UPD";
    1407           0 :   case ARMISD::VST2LN_UPD:    return "ARMISD::VST2LN_UPD";
    1408           0 :   case ARMISD::VST3LN_UPD:    return "ARMISD::VST3LN_UPD";
    1409           0 :   case ARMISD::VST4LN_UPD:    return "ARMISD::VST4LN_UPD";
    1410             :   }
    1411           0 :   return nullptr;
    1412             : }
    1413             : 
    1414       10625 : EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
    1415             :                                           EVT VT) const {
    1416       10625 :   if (!VT.isVector())
    1417       10438 :     return getPointerTy(DL);
    1418         187 :   return VT.changeVectorElementTypeToInteger();
    1419             : }
    1420             : 
    1421             : /// getRegClassFor - Return the register class that should be used for the
    1422             : /// specified value type.
    1423      218707 : const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
    1424             :   // Map v4i64 to QQ registers but do not make the type legal. Similarly map
    1425             :   // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
    1426             :   // load / store 4 to 8 consecutive D registers.
    1427      218707 :   if (Subtarget->hasNEON()) {
    1428      139037 :     if (VT == MVT::v4i64)
    1429             :       return &ARM::QQPRRegClass;
    1430      139037 :     if (VT == MVT::v8i64)
    1431             :       return &ARM::QQQQPRRegClass;
    1432             :   }
    1433      218673 :   return TargetLowering::getRegClassFor(VT);
    1434             : }
    1435             : 
    1436             : // memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
    1437             : // source/dest is aligned and the copy size is large enough. We therefore want
    1438             : // to align such objects passed to memory intrinsics.
    1439       10913 : bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
    1440             :                                                unsigned &PrefAlign) const {
    1441       10913 :   if (!isa<MemIntrinsic>(CI))
    1442             :     return false;
    1443         664 :   MinSize = 8;
    1444             :   // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
    1445             :   // cycle faster than 4-byte aligned LDM.
    1446         664 :   PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
    1447         664 :   return true;
    1448             : }
    1449             : 
    1450             : // Create a fast isel object.
    1451             : FastISel *
    1452        1221 : ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
    1453             :                                   const TargetLibraryInfo *libInfo) const {
    1454        1221 :   return ARM::createFastISel(funcInfo, libInfo);
    1455             : }
    1456             : 
    1457      141597 : Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
    1458      141597 :   unsigned NumVals = N->getNumValues();
    1459      141597 :   if (!NumVals)
    1460             :     return Sched::RegPressure;
    1461             : 
    1462      328292 :   for (unsigned i = 0; i != NumVals; ++i) {
    1463      426746 :     EVT VT = N->getValueType(i);
    1464             :     if (VT == MVT::Glue || VT == MVT::Other)
    1465      109095 :       continue;
    1466      186901 :     if (VT.isFloatingPoint() || VT.isVector())
    1467       26678 :       return Sched::ILP;
    1468             :   }
    1469             : 
    1470      114919 :   if (!N->isMachineOpcode())
    1471             :     return Sched::RegPressure;
    1472             : 
    1473             :   // Load are scheduled for latency even if there instruction itinerary
    1474             :   // is not available.
    1475       73553 :   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
    1476      147106 :   const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
    1477             : 
    1478       73553 :   if (MCID.getNumDefs() == 0)
    1479             :     return Sched::RegPressure;
    1480       65863 :   if (!Itins->isEmpty() &&
    1481       32957 :       Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
    1482       12467 :     return Sched::ILP;
    1483             : 
    1484             :   return Sched::RegPressure;
    1485             : }
    1486             : 
    1487             : //===----------------------------------------------------------------------===//
    1488             : // Lowering Code
    1489             : //===----------------------------------------------------------------------===//
    1490             : 
    1491        1616 : static bool isSRL16(const SDValue &Op) {
    1492        3232 :   if (Op.getOpcode() != ISD::SRL)
    1493             :     return false;
    1494             :   if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
    1495         118 :     return Const->getZExtValue() == 16;
    1496             :   return false;
    1497             : }
    1498             : 
    1499         228 : static bool isSRA16(const SDValue &Op) {
    1500         456 :   if (Op.getOpcode() != ISD::SRA)
    1501             :     return false;
    1502             :   if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
    1503         282 :     return Const->getZExtValue() == 16;
    1504             :   return false;
    1505             : }
    1506             : 
    1507         111 : static bool isSHL16(const SDValue &Op) {
    1508         222 :   if (Op.getOpcode() != ISD::SHL)
    1509             :     return false;
    1510             :   if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
    1511          64 :     return Const->getZExtValue() == 16;
    1512             :   return false;
    1513             : }
    1514             : 
    1515             : // Check for a signed 16-bit value. We special case SRA because it makes it
    1516             : // more simple when also looking for SRAs that aren't sign extending a
    1517             : // smaller value. Without the check, we'd need to take extra care with
    1518             : // checking order for some operations.
    1519         167 : static bool isS16(const SDValue &Op, SelectionDAG &DAG) {
    1520         167 :   if (isSRA16(Op))
    1521         160 :     return isSHL16(Op.getOperand(0));
    1522          87 :   return DAG.ComputeNumSignBits(Op) == 17;
    1523             : }
    1524             : 
    1525             : /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
    1526        3327 : static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
    1527        3327 :   switch (CC) {
    1528           0 :   default: llvm_unreachable("Unknown condition code!");
    1529             :   case ISD::SETNE:  return ARMCC::NE;
    1530         932 :   case ISD::SETEQ:  return ARMCC::EQ;
    1531         172 :   case ISD::SETGT:  return ARMCC::GT;
    1532         110 :   case ISD::SETGE:  return ARMCC::GE;
    1533         274 :   case ISD::SETLT:  return ARMCC::LT;
    1534          42 :   case ISD::SETLE:  return ARMCC::LE;
    1535         154 :   case ISD::SETUGT: return ARMCC::HI;
    1536          22 :   case ISD::SETUGE: return ARMCC::HS;
    1537         122 :   case ISD::SETULT: return ARMCC::LO;
    1538          50 :   case ISD::SETULE: return ARMCC::LS;
    1539             :   }
    1540             : }
    1541             : 
    1542             : /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
    1543         570 : static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
    1544             :                         ARMCC::CondCodes &CondCode2, bool &InvalidOnQNaN) {
    1545         570 :   CondCode2 = ARMCC::AL;
    1546         570 :   InvalidOnQNaN = true;
    1547         570 :   switch (CC) {
    1548           0 :   default: llvm_unreachable("Unknown FP condition!");
    1549          47 :   case ISD::SETEQ:
    1550             :   case ISD::SETOEQ:
    1551          47 :     CondCode = ARMCC::EQ;
    1552          47 :     InvalidOnQNaN = false;
    1553          47 :     break;
    1554          61 :   case ISD::SETGT:
    1555          61 :   case ISD::SETOGT: CondCode = ARMCC::GT; break;
    1556          44 :   case ISD::SETGE:
    1557          44 :   case ISD::SETOGE: CondCode = ARMCC::GE; break;
    1558         113 :   case ISD::SETOLT: CondCode = ARMCC::MI; break;
    1559          40 :   case ISD::SETOLE: CondCode = ARMCC::LS; break;
    1560           7 :   case ISD::SETONE:
    1561           7 :     CondCode = ARMCC::MI;
    1562           7 :     CondCode2 = ARMCC::GT;
    1563           7 :     InvalidOnQNaN = false;
    1564           7 :     break;
    1565           7 :   case ISD::SETO:   CondCode = ARMCC::VC; break;
    1566          17 :   case ISD::SETUO:  CondCode = ARMCC::VS; break;
    1567          24 :   case ISD::SETUEQ:
    1568          24 :     CondCode = ARMCC::EQ;
    1569          24 :     CondCode2 = ARMCC::VS;
    1570          24 :     InvalidOnQNaN = false;
    1571          24 :     break;
    1572          30 :   case ISD::SETUGT: CondCode = ARMCC::HI; break;
    1573          38 :   case ISD::SETUGE: CondCode = ARMCC::PL; break;
    1574          31 :   case ISD::SETLT:
    1575          31 :   case ISD::SETULT: CondCode = ARMCC::LT; break;
    1576          51 :   case ISD::SETLE:
    1577          51 :   case ISD::SETULE: CondCode = ARMCC::LE; break;
    1578          60 :   case ISD::SETNE:
    1579             :   case ISD::SETUNE:
    1580          60 :     CondCode = ARMCC::NE;
    1581          60 :     InvalidOnQNaN = false;
    1582          60 :     break;
    1583             :   }
    1584         570 : }
    1585             : 
    1586             : //===----------------------------------------------------------------------===//
    1587             : //                      Calling Convention Implementation
    1588             : //===----------------------------------------------------------------------===//
    1589             : 
    1590             : #include "ARMGenCallingConv.inc"
    1591             : 
    1592             : /// getEffectiveCallingConv - Get the effective calling convention, taking into
    1593             : /// account presence of floating point hardware and calling convention
    1594             : /// limitations, such as support for variadic functions.
    1595             : CallingConv::ID
    1596      102120 : ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
    1597             :                                            bool isVarArg) const {
    1598      102120 :   switch (CC) {
    1599           0 :   default:
    1600           0 :     report_fatal_error("Unsupported calling convention");
    1601             :   case CallingConv::ARM_AAPCS:
    1602             :   case CallingConv::ARM_APCS:
    1603             :   case CallingConv::GHC:
    1604             :     return CC;
    1605           0 :   case CallingConv::PreserveMost:
    1606           0 :     return CallingConv::PreserveMost;
    1607        5288 :   case CallingConv::ARM_AAPCS_VFP:
    1608             :   case CallingConv::Swift:
    1609        5288 :     return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
    1610       82084 :   case CallingConv::C:
    1611       82084 :     if (!Subtarget->isAAPCS_ABI())
    1612             :       return CallingConv::ARM_APCS;
    1613       38311 :     else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
    1614       98471 :              getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
    1615             :              !isVarArg)
    1616       11491 :       return CallingConv::ARM_AAPCS_VFP;
    1617             :     else
    1618             :       return CallingConv::ARM_AAPCS;
    1619         880 :   case CallingConv::Fast:
    1620             :   case CallingConv::CXX_FAST_TLS:
    1621         880 :     if (!Subtarget->isAAPCS_ABI()) {
    1622         724 :       if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
    1623         399 :         return CallingConv::Fast;
    1624             :       return CallingConv::ARM_APCS;
    1625         406 :     } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
    1626         205 :       return CallingConv::ARM_AAPCS_VFP;
    1627             :     else
    1628             :       return CallingConv::ARM_AAPCS;
    1629             :   }
    1630             : }
    1631             : 
    1632       22361 : CCAssignFn *ARMTargetLowering::CCAssignFnForCall(CallingConv::ID CC,
    1633             :                                                  bool isVarArg) const {
    1634       22361 :   return CCAssignFnForNode(CC, false, isVarArg);
    1635             : }
    1636             : 
    1637       44982 : CCAssignFn *ARMTargetLowering::CCAssignFnForReturn(CallingConv::ID CC,
    1638             :                                                    bool isVarArg) const {
    1639       44982 :   return CCAssignFnForNode(CC, true, isVarArg);
    1640             : }
    1641             : 
    1642             : /// CCAssignFnForNode - Selects the correct CCAssignFn for the given
    1643             : /// CallingConvention.
    1644       67343 : CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
    1645             :                                                  bool Return,
    1646             :                                                  bool isVarArg) const {
    1647       67343 :   switch (getEffectiveCallingConv(CC, isVarArg)) {
    1648           0 :   default:
    1649           0 :     report_fatal_error("Unsupported calling convention");
    1650       13534 :   case CallingConv::ARM_APCS:
    1651       13534 :     return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
    1652       42020 :   case CallingConv::ARM_AAPCS:
    1653       42020 :     return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
    1654       11501 :   case CallingConv::ARM_AAPCS_VFP:
    1655       11501 :     return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
    1656         276 :   case CallingConv::Fast:
    1657         276 :     return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
    1658          12 :   case CallingConv::GHC:
    1659          12 :     return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
    1660           0 :   case CallingConv::PreserveMost:
    1661           0 :     return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
    1662             :   }
    1663             : }
    1664             : 
    1665             : /// LowerCallResult - Lower the result values of a call into the
    1666             : /// appropriate copies out of appropriate physical registers.
    1667        7026 : SDValue ARMTargetLowering::LowerCallResult(
    1668             :     SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
    1669             :     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
    1670             :     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
    1671             :     SDValue ThisVal) const {
    1672             :   // Assign locations to each value returned by this call.
    1673             :   SmallVector<CCValAssign, 16> RVLocs;
    1674             :   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
    1675        7026 :                  *DAG.getContext());
    1676        7026 :   CCInfo.AnalyzeCallResult(Ins, CCAssignFnForReturn(CallConv, isVarArg));
    1677             : 
    1678             :   // Copy all of the result registers out of their specified physreg.
    1679       12444 :   for (unsigned i = 0; i != RVLocs.size(); ++i) {
    1680        5418 :     CCValAssign VA = RVLocs[i];
    1681             : 
    1682             :     // Pass 'this' value directly from the argument to return value, to avoid
    1683             :     // reg unit interference
    1684        5418 :     if (i == 0 && isThisReturn) {
    1685             :       assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
    1686             :              "unexpected return calling convention register assignment");
    1687          54 :       InVals.push_back(ThisVal);
    1688          54 :       continue;
    1689             :     }
    1690             : 
    1691        5364 :     SDValue Val;
    1692        5364 :     if (VA.needsCustom()) {
    1693             :       // Handle f64 or half of a v2f64.
    1694             :       SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
    1695         229 :                                       InFlag);
    1696         229 :       Chain = Lo.getValue(1);
    1697         229 :       InFlag = Lo.getValue(2);
    1698         458 :       VA = RVLocs[++i]; // skip ahead to next loc
    1699             :       SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
    1700         229 :                                       InFlag);
    1701         229 :       Chain = Hi.getValue(1);
    1702         229 :       InFlag = Hi.getValue(2);
    1703         229 :       if (!Subtarget->isLittle())
    1704             :         std::swap (Lo, Hi);
    1705         229 :       Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
    1706             : 
    1707         229 :       if (VA.getLocVT() == MVT::v2f64) {
    1708          43 :         SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
    1709          43 :         Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
    1710          43 :                           DAG.getConstant(0, dl, MVT::i32));
    1711             : 
    1712          86 :         VA = RVLocs[++i]; // skip ahead to next loc
    1713          43 :         Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
    1714          43 :         Chain = Lo.getValue(1);
    1715          43 :         InFlag = Lo.getValue(2);
    1716          86 :         VA = RVLocs[++i]; // skip ahead to next loc
    1717          43 :         Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
    1718          43 :         Chain = Hi.getValue(1);
    1719          43 :         InFlag = Hi.getValue(2);
    1720          43 :         if (!Subtarget->isLittle())
    1721             :           std::swap (Lo, Hi);
    1722          43 :         Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
    1723          43 :         Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
    1724          43 :                           DAG.getConstant(1, dl, MVT::i32));
    1725             :       }
    1726             :     } else {
    1727        5135 :       Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
    1728        5135 :                                InFlag);
    1729        5135 :       Chain = Val.getValue(1);
    1730        5135 :       InFlag = Val.getValue(2);
    1731             :     }
    1732             : 
    1733        5364 :     switch (VA.getLocInfo()) {
    1734           0 :     default: llvm_unreachable("Unknown loc info!");
    1735             :     case CCValAssign::Full: break;
    1736             :     case CCValAssign::BCvt:
    1737         622 :       Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
    1738         622 :       break;
    1739             :     }
    1740             : 
    1741        5364 :     InVals.push_back(Val);
    1742             :   }
    1743             : 
    1744        7026 :   return Chain;
    1745             : }
    1746             : 
    1747             : /// LowerMemOpCallTo - Store the argument to the stack.
    1748         936 : SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr,
    1749             :                                             SDValue Arg, const SDLoc &dl,
    1750             :                                             SelectionDAG &DAG,
    1751             :                                             const CCValAssign &VA,
    1752             :                                             ISD::ArgFlagsTy Flags) const {
    1753         936 :   unsigned LocMemOffset = VA.getLocMemOffset();
    1754         936 :   SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
    1755         936 :   PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
    1756        1872 :                        StackPtr, PtrOff);
    1757             :   return DAG.getStore(
    1758             :       Chain, dl, Arg, PtrOff,
    1759         936 :       MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset));
    1760             : }
    1761             : 
    1762         551 : void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG,
    1763             :                                          SDValue Chain, SDValue &Arg,
    1764             :                                          RegsToPassVector &RegsToPass,
    1765             :                                          CCValAssign &VA, CCValAssign &NextVA,
    1766             :                                          SDValue &StackPtr,
    1767             :                                          SmallVectorImpl<SDValue> &MemOpChains,
    1768             :                                          ISD::ArgFlagsTy Flags) const {
    1769             :   SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
    1770         551 :                               DAG.getVTList(MVT::i32, MVT::i32), Arg);
    1771         551 :   unsigned id = Subtarget->isLittle() ? 0 : 1;
    1772        1102 :   RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
    1773             : 
    1774         551 :   if (NextVA.isRegLoc())
    1775        1086 :     RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
    1776             :   else {
    1777             :     assert(NextVA.isMemLoc());
    1778           8 :     if (!StackPtr.getNode())
    1779           0 :       StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
    1780           0 :                                     getPointerTy(DAG.getDataLayout()));
    1781             : 
    1782           8 :     MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
    1783             :                                            dl, DAG, NextVA,
    1784           8 :                                            Flags));
    1785             :   }
    1786         551 : }
    1787             : 
    1788             : /// LowerCall - Lowering a call into a callseq_start <-
    1789             : /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
    1790             : /// nodes.
    1791             : SDValue
    1792        7641 : ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
    1793             :                              SmallVectorImpl<SDValue> &InVals) const {
    1794        7641 :   SelectionDAG &DAG                     = CLI.DAG;
    1795        7641 :   SDLoc &dl                             = CLI.DL;
    1796        7641 :   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
    1797        7641 :   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
    1798        7641 :   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
    1799        7641 :   SDValue Chain                         = CLI.Chain;
    1800        7641 :   SDValue Callee                        = CLI.Callee;
    1801             :   bool &isTailCall                      = CLI.IsTailCall;
    1802        7641 :   CallingConv::ID CallConv              = CLI.CallConv;
    1803        7641 :   bool doesNotRet                       = CLI.DoesNotReturn;
    1804        7641 :   bool isVarArg                         = CLI.IsVarArg;
    1805             : 
    1806        7641 :   MachineFunction &MF = DAG.getMachineFunction();
    1807        7641 :   bool isStructRet    = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
    1808             :   bool isThisReturn   = false;
    1809             :   bool isSibCall      = false;
    1810        7641 :   auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
    1811             : 
    1812             :   // Disable tail calls if they're not supported.
    1813        7641 :   if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
    1814        2379 :     isTailCall = false;
    1815             : 
    1816        7641 :   if (isTailCall) {
    1817             :     // Check if it's really possible to do a tail call.
    1818         693 :     isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
    1819         693 :                     isVarArg, isStructRet, MF.getFunction().hasStructRetAttr(),
    1820             :                                                    Outs, OutVals, Ins, DAG);
    1821         693 :     if (!isTailCall && CLI.CS && CLI.CS.isMustTailCall())
    1822           0 :       report_fatal_error("failed to perform tail call elimination on a call "
    1823             :                          "site marked musttail");
    1824             :     // We don't support GuaranteedTailCallOpt for ARM, only automatically
    1825             :     // detected sibcalls.
    1826         693 :     if (isTailCall) {
    1827             :       ++NumTailCalls;
    1828             :       isSibCall = true;
    1829             :     }
    1830             :   }
    1831             : 
    1832             :   // Analyze operands of the call, assigning locations to each operand.
    1833             :   SmallVector<CCValAssign, 16> ArgLocs;
    1834             :   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
    1835       15282 :                  *DAG.getContext());
    1836        7641 :   CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CallConv, isVarArg));
    1837             : 
    1838             :   // Get a count of how many bytes are to be pushed on the stack.
    1839        7641 :   unsigned NumBytes = CCInfo.getNextStackOffset();
    1840             : 
    1841             :   // For tail calls, memory operands are available in our caller's stack.
    1842        7641 :   if (isSibCall)
    1843             :     NumBytes = 0;
    1844             : 
    1845             :   // Adjust the stack pointer for the new arguments...
    1846             :   // These operations are automatically eliminated by the prolog/epilog pass
    1847        7641 :   if (!isSibCall)
    1848        7026 :     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
    1849             : 
    1850             :   SDValue StackPtr =
    1851       15282 :       DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
    1852             : 
    1853             :   RegsToPassVector RegsToPass;
    1854             :   SmallVector<SDValue, 8> MemOpChains;
    1855             : 
    1856             :   // Walk the register/memloc assignments, inserting copies/loads.  In the case
    1857             :   // of tail call optimization, arguments are handled later.
    1858       21450 :   for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
    1859       21450 :        i != e;
    1860             :        ++i, ++realArgIdx) {
    1861       13809 :     CCValAssign &VA = ArgLocs[i];
    1862       27618 :     SDValue Arg = OutVals[realArgIdx];
    1863       13809 :     ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
    1864       13809 :     bool isByVal = Flags.isByVal();
    1865             : 
    1866             :     // Promote the value if needed.
    1867       13809 :     switch (VA.getLocInfo()) {
    1868           0 :     default: llvm_unreachable("Unknown loc info!");
    1869             :     case CCValAssign::Full: break;
    1870             :     case CCValAssign::SExt:
    1871           0 :       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
    1872           0 :       break;
    1873             :     case CCValAssign::ZExt:
    1874           0 :       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
    1875           0 :       break;
    1876             :     case CCValAssign::AExt:
    1877           0 :       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
    1878           0 :       break;
    1879             :     case CCValAssign::BCvt:
    1880        1184 :       Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
    1881        1184 :       break;
    1882             :     }
    1883             : 
    1884             :     // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
    1885       13809 :     if (VA.needsCustom()) {
    1886         504 :       if (VA.getLocVT() == MVT::v2f64) {
    1887             :         SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
    1888          48 :                                   DAG.getConstant(0, dl, MVT::i32));
    1889             :         SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
    1890          48 :                                   DAG.getConstant(1, dl, MVT::i32));
    1891             : 
    1892          48 :         PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
    1893          48 :                          VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
    1894             : 
    1895          96 :         VA = ArgLocs[++i]; // skip ahead to next loc
    1896          48 :         if (VA.isRegLoc()) {
    1897          47 :           PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
    1898          47 :                            VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
    1899             :         } else {
    1900             :           assert(VA.isMemLoc());
    1901             : 
    1902           1 :           MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
    1903           1 :                                                  dl, DAG, VA, Flags));
    1904             :         }
    1905             :       } else {
    1906         912 :         PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
    1907             :                          StackPtr, MemOpChains, Flags);
    1908             :       }
    1909       13305 :     } else if (VA.isRegLoc()) {
    1910       11998 :       if (realArgIdx == 0 && Flags.isReturned() && !Flags.isSwiftSelf() &&
    1911          61 :           Outs[0].VT == MVT::i32) {
    1912             :         assert(VA.getLocVT() == MVT::i32 &&
    1913             :                "unexpected calling convention register assignment");
    1914             :         assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
    1915             :                "unexpected use of 'returned'");
    1916             :         isThisReturn = true;
    1917             :       }
    1918       23996 :       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
    1919        1307 :     } else if (isByVal) {
    1920             :       assert(VA.isMemLoc());
    1921             :       unsigned offset = 0;
    1922             : 
    1923             :       // True if this byval aggregate will be split between registers
    1924             :       // and memory.
    1925             :       unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
    1926         369 :       unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
    1927             : 
    1928         369 :       if (CurByValIdx < ByValArgsCount) {
    1929             : 
    1930             :         unsigned RegBegin, RegEnd;
    1931             :         CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
    1932             : 
    1933             :         EVT PtrVT =
    1934         356 :             DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
    1935             :         unsigned int i, j;
    1936        1719 :         for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
    1937        1363 :           SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
    1938        1363 :           SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
    1939             :           SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
    1940             :                                      MachinePointerInfo(),
    1941        1363 :                                      DAG.InferPtrAlignment(AddArg));
    1942        1363 :           MemOpChains.push_back(Load.getValue(1));
    1943        1363 :           RegsToPass.push_back(std::make_pair(j, Load));
    1944             :         }
    1945             : 
    1946             :         // If parameter size outsides register area, "offset" value
    1947             :         // helps us to calculate stack slot for remained part properly.
    1948         356 :         offset = RegEnd - RegBegin;
    1949             : 
    1950             :         CCInfo.nextInRegsParam();
    1951             :       }
    1952             : 
    1953         369 :       if (Flags.getByValSize() > 4*offset) {
    1954         360 :         auto PtrVT = getPointerTy(DAG.getDataLayout());
    1955         360 :         unsigned LocMemOffset = VA.getLocMemOffset();
    1956         360 :         SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
    1957         360 :         SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
    1958         360 :         SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
    1959         360 :         SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
    1960         360 :         SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
    1961         360 :                                            MVT::i32);
    1962             :         SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
    1963         360 :                                             MVT::i32);
    1964             : 
    1965         360 :         SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
    1966         360 :         SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
    1967         360 :         MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
    1968         720 :                                           Ops));
    1969             :       }
    1970         938 :     } else if (!isSibCall) {
    1971             :       assert(VA.isMemLoc());
    1972             : 
    1973         927 :       MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
    1974         927 :                                              dl, DAG, VA, Flags));
    1975             :     }
    1976             :   }
    1977             : 
    1978        7641 :   if (!MemOpChains.empty())
    1979         713 :     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
    1980             : 
    1981             :   // Build a sequence of copy-to-reg nodes chained together with token chain
    1982             :   // and flag operands which copy the outgoing args into the appropriate regs.
    1983        7641 :   SDValue InFlag;
    1984             :   // Tail call byval lowering might overwrite argument registers so in case of
    1985             :   // tail call optimization the copies to registers are lowered later.
    1986        7641 :   if (!isTailCall)
    1987       20760 :     for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
    1988       13734 :       Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
    1989       27468 :                                RegsToPass[i].second, InFlag);
    1990       13734 :       InFlag = Chain.getValue(1);
    1991             :     }
    1992             : 
    1993             :   // For tail calls lower the arguments to the 'real' stack slot.
    1994        7641 :   if (isTailCall) {
    1995             :     // Force all the incoming stack arguments to be loaded from the stack
    1996             :     // before any new outgoing arguments are stored to the stack, because the
    1997             :     // outgoing stack slots may alias the incoming argument stack slots, and
    1998             :     // the alias isn't otherwise explicit. This is slightly more conservative
    1999             :     // than necessary, because it means that each store effectively depends
    2000             :     // on every argument instead of just those arguments it would clobber.
    2001             : 
    2002             :     // Do not flag preceding copytoreg stuff together with the following stuff.
    2003         615 :     InFlag = SDValue();
    2004        1336 :     for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
    2005         721 :       Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
    2006        1442 :                                RegsToPass[i].second, InFlag);
    2007         721 :       InFlag = Chain.getValue(1);
    2008             :     }
    2009         615 :     InFlag = SDValue();
    2010             :   }
    2011             : 
    2012             :   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
    2013             :   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
    2014             :   // node so that legalize doesn't hack it.
    2015             :   bool isDirect = false;
    2016             : 
    2017        7641 :   const TargetMachine &TM = getTargetMachine();
    2018        7641 :   const Module *Mod = MF.getFunction().getParent();
    2019             :   const GlobalValue *GV = nullptr;
    2020             :   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
    2021        4351 :     GV = G->getGlobal();
    2022             :   bool isStub =
    2023        7641 :       !TM.shouldAssumeDSOLocal(*Mod, GV) && Subtarget->isTargetMachO();
    2024             : 
    2025        7641 :   bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
    2026             :   bool isLocalARMFunc = false;
    2027        7641 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    2028        7641 :   auto PtrVt = getPointerTy(DAG.getDataLayout());
    2029             : 
    2030        7641 :   if (Subtarget->genLongCalls()) {
    2031             :     assert((!isPositionIndependent() || Subtarget->isTargetWindows()) &&
    2032             :            "long-calls codegen is not position independent!");
    2033             :     // Handle a global address or an external symbol. If it's not one of
    2034             :     // those, the target's already in a register, so we don't need to do
    2035             :     // anything extra.
    2036             :     if (isa<GlobalAddressSDNode>(Callee)) {
    2037             :       // Create a constant pool entry for the callee address
    2038             :       unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
    2039             :       ARMConstantPoolValue *CPV =
    2040           7 :         ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
    2041             : 
    2042             :       // Get the address of the callee into a register
    2043           7 :       SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
    2044           7 :       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
    2045           7 :       Callee = DAG.getLoad(
    2046             :           PtrVt, dl, DAG.getEntryNode(), CPAddr,
    2047           7 :           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    2048             :     } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
    2049           0 :       const char *Sym = S->getSymbol();
    2050             : 
    2051             :       // Create a constant pool entry for the callee address
    2052             :       unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
    2053             :       ARMConstantPoolValue *CPV =
    2054           0 :         ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
    2055             :                                       ARMPCLabelIndex, 0);
    2056             :       // Get the address of the callee into a register
    2057           0 :       SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
    2058           0 :       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
    2059           0 :       Callee = DAG.getLoad(
    2060             :           PtrVt, dl, DAG.getEntryNode(), CPAddr,
    2061           0 :           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    2062             :     }
    2063             :   } else if (isa<GlobalAddressSDNode>(Callee)) {
    2064             :     // If we're optimizing for minimum size and the function is called three or
    2065             :     // more times in this block, we can improve codesize by calling indirectly
    2066             :     // as BLXr has a 16-bit encoding.
    2067        4344 :     auto *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
    2068        4344 :     auto *BB = CLI.CS.getParent();
    2069             :     bool PreferIndirect =
    2070        4416 :         Subtarget->isThumb() && MF.getFunction().optForMinSize() &&
    2071         342 :         count_if(GV->users(), [&BB](const User *U) {
    2072         347 :           return isa<Instruction>(U) && cast<Instruction>(U)->getParent() == BB;
    2073             :         }) > 2;
    2074             : 
    2075             :     if (!PreferIndirect) {
    2076             :       isDirect = true;
    2077        4341 :       bool isDef = GV->isStrongDefinitionForLinker();
    2078             : 
    2079             :       // ARM call to a local ARM function is predicable.
    2080        4341 :       isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
    2081             :       // tBX takes a register source operand.
    2082        4487 :       if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
    2083             :         assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
    2084          50 :         Callee = DAG.getNode(
    2085             :             ARMISD::WrapperPIC, dl, PtrVt,
    2086         100 :             DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
    2087          50 :         Callee = DAG.getLoad(
    2088             :             PtrVt, dl, DAG.getEntryNode(), Callee,
    2089             :             MachinePointerInfo::getGOT(DAG.getMachineFunction()),
    2090             :             /* Alignment = */ 0, MachineMemOperand::MODereferenceable |
    2091          50 :                                      MachineMemOperand::MOInvariant);
    2092        4291 :       } else if (Subtarget->isTargetCOFF()) {
    2093             :         assert(Subtarget->isTargetWindows() &&
    2094             :                "Windows is the only supported COFF target");
    2095             :         unsigned TargetFlags = GV->hasDLLImportStorageClass()
    2096          52 :                                    ? ARMII::MO_DLLIMPORT
    2097             :                                    : ARMII::MO_NO_FLAG;
    2098          52 :         Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0,
    2099          52 :                                             TargetFlags);
    2100          52 :         if (GV->hasDLLImportStorageClass())
    2101           3 :           Callee =
    2102           3 :               DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
    2103             :                           DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
    2104           3 :                           MachinePointerInfo::getGOT(DAG.getMachineFunction()));
    2105             :       } else {
    2106        4239 :         Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, 0);
    2107             :       }
    2108             :     }
    2109             :   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
    2110             :     isDirect = true;
    2111             :     // tBX takes a register source operand.
    2112        3198 :     const char *Sym = S->getSymbol();
    2113        3265 :     if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
    2114             :       unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
    2115             :       ARMConstantPoolValue *CPV =
    2116           9 :         ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
    2117             :                                       ARMPCLabelIndex, 4);
    2118           9 :       SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
    2119           9 :       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
    2120           9 :       Callee = DAG.getLoad(
    2121             :           PtrVt, dl, DAG.getEntryNode(), CPAddr,
    2122           9 :           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    2123           9 :       SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
    2124           9 :       Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
    2125             :     } else {
    2126        3189 :       Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, 0);
    2127             :     }
    2128             :   }
    2129             : 
    2130             :   // FIXME: handle tail calls differently.
    2131             :   unsigned CallOpc;
    2132        7641 :   if (Subtarget->isThumb()) {
    2133        3360 :     if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
    2134             :       CallOpc = ARMISD::CALL_NOLINK;
    2135             :     else
    2136             :       CallOpc = ARMISD::CALL;
    2137             :   } else {
    2138        4281 :     if (!isDirect && !Subtarget->hasV5TOps())
    2139             :       CallOpc = ARMISD::CALL_NOLINK;
    2140        4284 :     else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() &&
    2141             :              // Emit regular call when code size is the priority
    2142          20 :              !MF.getFunction().optForMinSize())
    2143             :       // "mov lr, pc; b _foo" to avoid confusing the RSP
    2144             :       CallOpc = ARMISD::CALL_NOLINK;
    2145             :     else
    2146        4248 :       CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
    2147             :   }
    2148             : 
    2149             :   std::vector<SDValue> Ops;
    2150        7641 :   Ops.push_back(Chain);
    2151        7641 :   Ops.push_back(Callee);
    2152             : 
    2153             :   // Add argument registers to the end of the list so that they are known live
    2154             :   // into the call.
    2155       22096 :   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
    2156       14455 :     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
    2157       43365 :                                   RegsToPass[i].second.getValueType()));
    2158             : 
    2159             :   // Add a register mask operand representing the call-preserved registers.
    2160        7641 :   if (!isTailCall) {
    2161             :     const uint32_t *Mask;
    2162        7026 :     const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
    2163        7026 :     if (isThisReturn) {
    2164             :       // For 'this' returns, use the R0-preserving mask if applicable
    2165          54 :       Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
    2166          54 :       if (!Mask) {
    2167             :         // Set isThisReturn to false if the calling convention is not one that
    2168             :         // allows 'returned' to be modeled in this way, so LowerCallResult does
    2169             :         // not try to pass 'this' straight through
    2170             :         isThisReturn = false;
    2171           0 :         Mask = ARI->getCallPreservedMask(MF, CallConv);
    2172             :       }
    2173             :     } else
    2174        6972 :       Mask = ARI->getCallPreservedMask(MF, CallConv);
    2175             : 
    2176             :     assert(Mask && "Missing call preserved mask for calling convention");
    2177       14052 :     Ops.push_back(DAG.getRegisterMask(Mask));
    2178             :   }
    2179             : 
    2180        7641 :   if (InFlag.getNode())
    2181        6200 :     Ops.push_back(InFlag);
    2182             : 
    2183        7641 :   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
    2184        7641 :   if (isTailCall) {
    2185         615 :     MF.getFrameInfo().setHasTailCall();
    2186         615 :     return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
    2187             :   }
    2188             : 
    2189             :   // Returns a chain and a flag for retval copy to use.
    2190        7026 :   Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
    2191        7026 :   InFlag = Chain.getValue(1);
    2192             : 
    2193        7026 :   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
    2194        7026 :                              DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
    2195        7026 :   if (!Ins.empty())
    2196        4490 :     InFlag = Chain.getValue(1);
    2197             : 
    2198             :   // Handle result values, copying them out of physregs into vregs that we
    2199             :   // return.
    2200             :   return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
    2201             :                          InVals, isThisReturn,
    2202        7026 :                          isThisReturn ? OutVals[0] : SDValue());
    2203             : }
    2204             : 
    2205             : /// HandleByVal - Every parameter *after* a byval parameter is passed
    2206             : /// on the stack.  Remember the next parameter register to allocate,
    2207             : /// and then confiscate the rest of the parameter registers to insure
    2208             : /// this.
    2209         419 : void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
    2210             :                                     unsigned Align) const {
    2211             :   // Byval (as with any stack) slots are always at least 4 byte aligned.
    2212         419 :   Align = std::max(Align, 4U);
    2213             : 
    2214         419 :   unsigned Reg = State->AllocateReg(GPRArgRegs);
    2215         419 :   if (!Reg)
    2216             :     return;
    2217             : 
    2218         399 :   unsigned AlignInRegs = Align / 4;
    2219         399 :   unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
    2220         415 :   for (unsigned i = 0; i < Waste; ++i)
    2221          16 :     Reg = State->AllocateReg(GPRArgRegs);
    2222             : 
    2223         399 :   if (!Reg)
    2224             :     return;
    2225             : 
    2226         395 :   unsigned Excess = 4 * (ARM::R4 - Reg);
    2227             : 
    2228             :   // Special case when NSAA != SP and parameter size greater than size of
    2229             :   // all remained GPR regs. In that case we can't split parameter, we must
    2230             :   // send it to stack. We also must set NCRN to R4, so waste all
    2231             :   // remained registers.
    2232         395 :   const unsigned NSAAOffset = State->getNextStackOffset();
    2233         395 :   if (NSAAOffset != 0 && Size > Excess) {
    2234          15 :     while (State->AllocateReg(GPRArgRegs))
    2235             :       ;
    2236             :     return;
    2237             :   }
    2238             : 
    2239             :   // First register for byval parameter is the first register that wasn't
    2240             :   // allocated before this method call, so it would be "reg".
    2241             :   // If parameter is small enough to be saved in range [reg, r4), then
    2242             :   // the end (first after last) register would be reg + param-size-in-regs,
    2243             :   // else parameter would be splitted between registers and stack,
    2244             :   // end register would be r4 in this case.
    2245             :   unsigned ByValRegBegin = Reg;
    2246         751 :   unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
    2247             :   State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
    2248             :   // Note, first register is allocated in the beginning of function already,
    2249             :   // allocate remained amount of registers we need.
    2250        1439 :   for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
    2251        1049 :     State->AllocateReg(GPRArgRegs);
    2252             :   // A byval parameter that is split between registers and memory needs its
    2253             :   // size truncated here.
    2254             :   // In the case where the entire structure fits in registers, we set the
    2255             :   // size in memory to zero.
    2256         414 :   Size = std::max<int>(Size - Excess, 0);
    2257             : }
    2258             : 
    2259             : /// MatchingStackOffset - Return true if the given stack call argument is
    2260             : /// already available in the same position (relatively) of the caller's
    2261             : /// incoming argument stack.
    2262             : static
    2263          31 : bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
    2264             :                          MachineFrameInfo &MFI, const MachineRegisterInfo *MRI,
    2265             :                          const TargetInstrInfo *TII) {
    2266          31 :   unsigned Bytes = Arg.getValueSizeInBits() / 8;
    2267          31 :   int FI = std::numeric_limits<int>::max();
    2268          62 :   if (Arg.getOpcode() == ISD::CopyFromReg) {
    2269           6 :     unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
    2270           6 :     if (!TargetRegisterInfo::isVirtualRegister(VR))
    2271             :       return false;
    2272           6 :     MachineInstr *Def = MRI->getVRegDef(VR);
    2273           6 :     if (!Def)
    2274             :       return false;
    2275           1 :     if (!Flags.isByVal()) {
    2276           1 :       if (!TII->isLoadFromStackSlot(*Def, FI))
    2277             :         return false;
    2278             :     } else {
    2279             :       return false;
    2280             :     }
    2281             :   } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
    2282          11 :     if (Flags.isByVal())
    2283             :       // ByVal argument is passed in as a pointer but it's now being
    2284             :       // dereferenced. e.g.
    2285             :       // define @foo(%struct.X* %A) {
    2286             :       //   tail call @bar(%struct.X* byval %A)
    2287             :       // }
    2288             :       return false;
    2289          11 :     SDValue Ptr = Ld->getBasePtr();
    2290             :     FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
    2291             :     if (!FINode)
    2292             :       return false;
    2293          11 :     FI = FINode->getIndex();
    2294             :   } else
    2295             :     return false;
    2296             : 
    2297             :   assert(FI != std::numeric_limits<int>::max());
    2298          12 :   if (!MFI.isFixedObjectIndex(FI))
    2299             :     return false;
    2300          24 :   return Offset == MFI.getObjectOffset(FI) && Bytes == MFI.getObjectSize(FI);
    2301             : }
    2302             : 
    2303             : /// IsEligibleForTailCallOptimization - Check whether the call is eligible
    2304             : /// for tail call optimization. Targets which want to do tail call
    2305             : /// optimization should implement this function.
    2306             : bool
    2307         693 : ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
    2308             :                                                      CallingConv::ID CalleeCC,
    2309             :                                                      bool isVarArg,
    2310             :                                                      bool isCalleeStructRet,
    2311             :                                                      bool isCallerStructRet,
    2312             :                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
    2313             :                                     const SmallVectorImpl<SDValue> &OutVals,
    2314             :                                     const SmallVectorImpl<ISD::InputArg> &Ins,
    2315             :                                                      SelectionDAG& DAG) const {
    2316         693 :   MachineFunction &MF = DAG.getMachineFunction();
    2317         693 :   const Function &CallerF = MF.getFunction();
    2318             :   CallingConv::ID CallerCC = CallerF.getCallingConv();
    2319             : 
    2320             :   assert(Subtarget->supportsTailCall());
    2321             : 
    2322             :   // Tail calls to function pointers cannot be optimized for Thumb1 if the args
    2323             :   // to the call take up r0-r3. The reason is that there are no legal registers
    2324             :   // left to hold the pointer to the function to be called.
    2325         706 :   if (Subtarget->isThumb1Only() && Outs.size() >= 4 &&
    2326             :       !isa<GlobalAddressSDNode>(Callee.getNode()))
    2327             :       return false;
    2328             : 
    2329             :   // Look for obvious safe cases to perform tail call optimization that do not
    2330             :   // require ABI changes. This is what gcc calls sibcall.
    2331             : 
    2332             :   // Exception-handling functions need a special set of instructions to indicate
    2333             :   // a return to the hardware. Tail-calling another function would probably
    2334             :   // break this.
    2335         691 :   if (CallerF.hasFnAttribute("interrupt"))
    2336             :     return false;
    2337             : 
    2338             :   // Also avoid sibcall optimization if either caller or callee uses struct
    2339             :   // return semantics.
    2340         691 :   if (isCalleeStructRet || isCallerStructRet)
    2341             :     return false;
    2342             : 
    2343             :   // Externally-defined functions with weak linkage should not be
    2344             :   // tail-called on ARM when the OS does not support dynamic
    2345             :   // pre-emption of symbols, as the AAELF spec requires normal calls
    2346             :   // to undefined weak functions to be replaced with a NOP or jump to the
    2347             :   // next instruction. The behaviour of branch instructions in this
    2348             :   // situation (as used for tail calls) is implementation-defined, so we
    2349             :   // cannot rely on the linker replacing the tail call with a return.
    2350             :   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
    2351         398 :     const GlobalValue *GV = G->getGlobal();
    2352         398 :     const Triple &TT = getTargetMachine().getTargetTriple();
    2353         398 :     if (GV->hasExternalWeakLinkage() &&
    2354           1 :         (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
    2355             :       return false;
    2356             :   }
    2357             : 
    2358             :   // Check that the call results are passed in the same way.
    2359         685 :   LLVMContext &C = *DAG.getContext();
    2360         685 :   if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
    2361             :                                   CCAssignFnForReturn(CalleeCC, isVarArg),
    2362             :                                   CCAssignFnForReturn(CallerCC, isVarArg)))
    2363             :     return false;
    2364             :   // The callee has to preserve all registers the caller needs to preserve.
    2365         663 :   const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
    2366         663 :   const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
    2367         663 :   if (CalleeCC != CallerCC) {
    2368         259 :     const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
    2369         259 :     if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
    2370             :       return false;
    2371             :   }
    2372             : 
    2373             :   // If Caller's vararg or byval argument has been split between registers and
    2374             :   // stack, do not perform tail call, since part of the argument is in caller's
    2375             :   // local frame.
    2376         649 :   const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>();
    2377         649 :   if (AFI_Caller->getArgRegsSaveSize())
    2378             :     return false;
    2379             : 
    2380             :   // If the callee takes no arguments then go on to check the results of the
    2381             :   // call.
    2382         638 :   if (!Outs.empty()) {
    2383             :     // Check if stack adjustment is needed. For now, do not do this if any
    2384             :     // argument is passed on the stack.
    2385             :     SmallVector<CCValAssign, 16> ArgLocs;
    2386         957 :     CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C);
    2387         490 :     CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg));
    2388         490 :     if (CCInfo.getNextStackOffset()) {
    2389             :       // Check if the arguments are already laid out in the right way as
    2390             :       // the caller's fixed stack objects.
    2391          29 :       MachineFrameInfo &MFI = MF.getFrameInfo();
    2392          29 :       const MachineRegisterInfo *MRI = &MF.getRegInfo();
    2393          29 :       const TargetInstrInfo *TII = Subtarget->getInstrInfo();
    2394         151 :       for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
    2395         151 :            i != e;
    2396             :            ++i, ++realArgIdx) {
    2397         142 :         CCValAssign &VA = ArgLocs[i];
    2398             :         EVT RegVT = VA.getLocVT();
    2399         284 :         SDValue Arg = OutVals[realArgIdx];
    2400         142 :         ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
    2401         142 :         if (VA.getLocInfo() == CCValAssign::Indirect)
    2402          20 :           return false;
    2403         142 :         if (VA.needsCustom()) {
    2404             :           // f64 and vector types are split into multiple registers or
    2405             :           // register/stack-slot combinations.  The types will not match
    2406             :           // the registers; give up on memory f64 refs until we figure
    2407             :           // out what to do about this.
    2408          18 :           if (!VA.isRegLoc())
    2409             :             return false;
    2410          36 :           if (!ArgLocs[++i].isRegLoc())
    2411             :             return false;
    2412             :           if (RegVT == MVT::v2f64) {
    2413           0 :             if (!ArgLocs[++i].isRegLoc())
    2414             :               return false;
    2415           0 :             if (!ArgLocs[++i].isRegLoc())
    2416             :               return false;
    2417             :           }
    2418         124 :         } else if (!VA.isRegLoc()) {
    2419          31 :           if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
    2420             :                                    MFI, MRI, TII))
    2421             :             return false;
    2422             :         }
    2423             :       }
    2424             :     }
    2425             : 
    2426         470 :     const MachineRegisterInfo &MRI = MF.getRegInfo();
    2427         470 :     if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
    2428             :       return false;
    2429             :   }
    2430             : 
    2431             :   return true;
    2432             : }
    2433             : 
    2434             : bool
    2435       22675 : ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
    2436             :                                   MachineFunction &MF, bool isVarArg,
    2437             :                                   const SmallVectorImpl<ISD::OutputArg> &Outs,
    2438             :                                   LLVMContext &Context) const {
    2439             :   SmallVector<CCValAssign, 16> RVLocs;
    2440       45350 :   CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
    2441       22675 :   return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg));
    2442             : }
    2443             : 
    2444          12 : static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
    2445             :                                     const SDLoc &DL, SelectionDAG &DAG) {
    2446          12 :   const MachineFunction &MF = DAG.getMachineFunction();
    2447          12 :   const Function &F = MF.getFunction();
    2448             : 
    2449          12 :   StringRef IntKind = F.getFnAttribute("interrupt").getValueAsString();
    2450             : 
    2451             :   // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
    2452             :   // version of the "preferred return address". These offsets affect the return
    2453             :   // instruction if this is a return from PL1 without hypervisor extensions.
    2454             :   //    IRQ/FIQ: +4     "subs pc, lr, #4"
    2455             :   //    SWI:     0      "subs pc, lr, #0"
    2456             :   //    ABORT:   +4     "subs pc, lr, #4"
    2457             :   //    UNDEF:   +4/+2  "subs pc, lr, #0"
    2458             :   // UNDEF varies depending on where the exception came from ARM or Thumb
    2459             :   // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
    2460             : 
    2461             :   int64_t LROffset;
    2462             :   if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
    2463             :       IntKind == "ABORT")
    2464             :     LROffset = 4;
    2465             :   else if (IntKind == "SWI" || IntKind == "UNDEF")
    2466             :     LROffset = 0;
    2467             :   else
    2468           0 :     report_fatal_error("Unsupported interrupt attribute. If present, value "
    2469             :                        "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
    2470             : 
    2471          12 :   RetOps.insert(RetOps.begin() + 1,
    2472          24 :                 DAG.getConstant(LROffset, DL, MVT::i32, false));
    2473             : 
    2474          12 :   return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
    2475             : }
    2476             : 
    2477             : SDValue
    2478       13429 : ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
    2479             :                                bool isVarArg,
    2480             :                                const SmallVectorImpl<ISD::OutputArg> &Outs,
    2481             :                                const SmallVectorImpl<SDValue> &OutVals,
    2482             :                                const SDLoc &dl, SelectionDAG &DAG) const {
    2483             :   // CCValAssign - represent the assignment of the return value to a location.
    2484             :   SmallVector<CCValAssign, 16> RVLocs;
    2485             : 
    2486             :   // CCState - Info about the registers and stack slots.
    2487             :   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
    2488       26858 :                  *DAG.getContext());
    2489             : 
    2490             :   // Analyze outgoing return values.
    2491       13429 :   CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg));
    2492             : 
    2493       13429 :   SDValue Flag;
    2494             :   SmallVector<SDValue, 4> RetOps;
    2495       13429 :   RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
    2496       13429 :   bool isLittleEndian = Subtarget->isLittle();
    2497             : 
    2498       13429 :   MachineFunction &MF = DAG.getMachineFunction();
    2499       13429 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    2500       13429 :   AFI->setReturnRegsCount(RVLocs.size());
    2501             : 
    2502             :   // Copy the result values into the output registers.
    2503       10954 :   for (unsigned i = 0, realRVLocIdx = 0;
    2504       24383 :        i != RVLocs.size();
    2505             :        ++i, ++realRVLocIdx) {
    2506             :     CCValAssign &VA = RVLocs[i];
    2507             :     assert(VA.isRegLoc() && "Can only return in registers!");
    2508             : 
    2509       10954 :     SDValue Arg = OutVals[realRVLocIdx];
    2510             :     bool ReturnF16 = false;
    2511             : 
    2512       10954 :     if (Subtarget->hasFullFP16() && Subtarget->isTargetHardFloat()) {
    2513             :       // Half-precision return values can be returned like this:
    2514             :       //
    2515             :       // t11 f16 = fadd ...
    2516             :       // t12: i16 = bitcast t11
    2517             :       //   t13: i32 = zero_extend t12
    2518             :       // t14: f32 = bitcast t13  <~~~~~~~ Arg
    2519             :       //
    2520             :       // to avoid code generation for bitcasts, we simply set Arg to the node
    2521             :       // that produces the f16 value, t11 in this case.
    2522             :       //
    2523          76 :       if (Arg.getValueType() == MVT::f32 && Arg.getOpcode() == ISD::BITCAST) {
    2524          68 :         SDValue ZE = Arg.getOperand(0);
    2525          68 :         if (ZE.getOpcode() == ISD::ZERO_EXTEND && ZE.getValueType() == MVT::i32) {
    2526          68 :           SDValue BC = ZE.getOperand(0);
    2527          68 :           if (BC.getOpcode() == ISD::BITCAST && BC.getValueType() == MVT::i16) {
    2528          68 :             Arg = BC.getOperand(0);
    2529             :             ReturnF16 = true;
    2530             :           }
    2531             :         }
    2532             :       }
    2533             :     }
    2534             : 
    2535       10954 :     switch (VA.getLocInfo()) {
    2536           0 :     default: llvm_unreachable("Unknown loc info!");
    2537             :     case CCValAssign::Full: break;
    2538        2538 :     case CCValAssign::BCvt:
    2539        2538 :       if (!ReturnF16)
    2540        2538 :         Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
    2541             :       break;
    2542             :     }
    2543             : 
    2544       10954 :     if (VA.needsCustom()) {
    2545        1815 :       if (VA.getLocVT() == MVT::v2f64) {
    2546             :         // Extract the first half and return it in two registers.
    2547             :         SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
    2548         792 :                                    DAG.getConstant(0, dl, MVT::i32));
    2549             :         SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
    2550         792 :                                        DAG.getVTList(MVT::i32, MVT::i32), Half);
    2551             : 
    2552         792 :         Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
    2553             :                                  HalfGPRs.getValue(isLittleEndian ? 0 : 1),
    2554         837 :                                  Flag);
    2555         792 :         Flag = Chain.getValue(1);
    2556         792 :         RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
    2557         792 :         VA = RVLocs[++i]; // skip ahead to next loc
    2558         792 :         Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
    2559             :                                  HalfGPRs.getValue(isLittleEndian ? 1 : 0),
    2560         837 :                                  Flag);
    2561         792 :         Flag = Chain.getValue(1);
    2562         792 :         RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
    2563        1584 :         VA = RVLocs[++i]; // skip ahead to next loc
    2564             : 
    2565             :         // Extract the 2nd half and fall through to handle it as an f64 value.
    2566         792 :         Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
    2567         792 :                           DAG.getConstant(1, dl, MVT::i32));
    2568             :       }
    2569             :       // Legalize ret f64 -> ret 2 x i32.  We always have fmrrd if f64 is
    2570             :       // available.
    2571             :       SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
    2572        1815 :                                   DAG.getVTList(MVT::i32, MVT::i32), Arg);
    2573        1815 :       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
    2574             :                                fmrrd.getValue(isLittleEndian ? 0 : 1),
    2575        1901 :                                Flag);
    2576        1815 :       Flag = Chain.getValue(1);
    2577        1815 :       RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
    2578        1815 :       VA = RVLocs[++i]; // skip ahead to next loc
    2579        1815 :       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
    2580             :                                fmrrd.getValue(isLittleEndian ? 1 : 0),
    2581        1901 :                                Flag);
    2582             :     } else
    2583        9139 :       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
    2584             : 
    2585             :     // Guarantee that all emitted copies are
    2586             :     // stuck together, avoiding something bad.
    2587       10954 :     Flag = Chain.getValue(1);
    2588       10954 :     RetOps.push_back(DAG.getRegister(VA.getLocReg(),
    2589       32794 :                                      ReturnF16 ? MVT::f16 : VA.getLocVT()));
    2590             :   }
    2591       13429 :   const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
    2592             :   const MCPhysReg *I =
    2593       13429 :       TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
    2594       13429 :   if (I) {
    2595         600 :     for (; *I; ++I) {
    2596         585 :       if (ARM::GPRRegClass.contains(*I))
    2597         105 :         RetOps.push_back(DAG.getRegister(*I, MVT::i32));
    2598         480 :       else if (ARM::DPRRegClass.contains(*I))
    2599         480 :         RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
    2600             :       else
    2601           0 :         llvm_unreachable("Unexpected register class in CSRsViaCopy!");
    2602             :     }
    2603             :   }
    2604             : 
    2605             :   // Update chain and glue.
    2606       13429 :   RetOps[0] = Chain;
    2607       13429 :   if (Flag.getNode())
    2608        9925 :     RetOps.push_back(Flag);
    2609             : 
    2610             :   // CPUs which aren't M-class use a special sequence to return from
    2611             :   // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
    2612             :   // though we use "subs pc, lr, #N").
    2613             :   //
    2614             :   // M-class CPUs actually use a normal return sequence with a special
    2615             :   // (hardware-provided) value in LR, so the normal code path works.
    2616       13429 :   if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt") &&
    2617          18 :       !Subtarget->isMClass()) {
    2618          12 :     if (Subtarget->isThumb1Only())
    2619           0 :       report_fatal_error("interrupt attribute is not supported in Thumb1");
    2620          12 :     return LowerInterruptReturn(RetOps, dl, DAG);
    2621             :   }
    2622             : 
    2623       13417 :   return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
    2624             : }
    2625             : 
    2626         985 : bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
    2627         985 :   if (N->getNumValues() != 1)
    2628             :     return false;
    2629         985 :   if (!N->hasNUsesOfValue(1, 0))
    2630             :     return false;
    2631             : 
    2632         961 :   SDValue TCChain = Chain;
    2633         961 :   SDNode *Copy = *N->use_begin();
    2634        1922 :   if (Copy->getOpcode() == ISD::CopyToReg) {
    2635             :     // If the copy has a glue operand, we conservatively assume it isn't safe to
    2636             :     // perform a tail call.
    2637         786 :     if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
    2638           7 :       return false;
    2639         255 :     TCChain = Copy->getOperand(0);
    2640         699 :   } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
    2641             :     SDNode *VMov = Copy;
    2642             :     // f64 returned in a pair of GPRs.
    2643             :     SmallPtrSet<SDNode*, 2> Copies;
    2644          63 :     for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
    2645         189 :          UI != UE; ++UI) {
    2646         126 :       if (UI->getOpcode() != ISD::CopyToReg)
    2647             :         return false;
    2648         126 :       Copies.insert(*UI);
    2649             :     }
    2650         126 :     if (Copies.size() > 2)
    2651             :       return false;
    2652             : 
    2653          63 :     for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
    2654         181 :          UI != UE; ++UI) {
    2655         126 :       SDValue UseChain = UI->getOperand(0);
    2656         126 :       if (Copies.count(UseChain.getNode()))
    2657             :         // Second CopyToReg
    2658             :         Copy = *UI;
    2659             :       else {
    2660             :         // We are at the top of this chain.
    2661             :         // If the copy has a glue operand, we conservatively assume it
    2662             :         // isn't safe to perform a tail call.
    2663         189 :         if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
    2664           8 :           return false;
    2665             :         // First CopyToReg
    2666          55 :         TCChain = UseChain;
    2667             :       }
    2668             :     }
    2669         636 :   } else if (Copy->getOpcode() == ISD::BITCAST) {
    2670             :     // f32 returned in a single GPR.
    2671             :     if (!Copy->hasOneUse())
    2672             :       return false;
    2673             :     Copy = *Copy->use_begin();
    2674          46 :     if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
    2675           2 :       return false;
    2676             :     // If the copy has a glue operand, we conservatively assume it isn't safe to
    2677             :     // perform a tail call.
    2678         132 :     if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
    2679           2 :       return false;
    2680          42 :     TCChain = Copy->getOperand(0);
    2681             :   } else {
    2682             :     return false;
    2683             :   }
    2684             : 
    2685             :   bool HasRet = false;
    2686         352 :   for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
    2687         926 :        UI != UE; ++UI) {
    2688        1276 :     if (UI->getOpcode() != ARMISD::RET_FLAG &&
    2689             :         UI->getOpcode() != ARMISD::INTRET_FLAG)
    2690             :       return false;
    2691             :     HasRet = true;
    2692             :   }
    2693             : 
    2694         288 :   if (!HasRet)
    2695             :     return false;
    2696             : 
    2697         287 :   Chain = TCChain;
    2698         287 :   return true;
    2699             : }
    2700             : 
    2701         142 : bool ARMTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
    2702         142 :   if (!Subtarget->supportsTailCall())
    2703             :     return false;
    2704             : 
    2705             :   auto Attr =
    2706         121 :       CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
    2707         121 :   if (!CI->isTailCall() || Attr.getValueAsString() == "true")
    2708          33 :     return false;
    2709             : 
    2710             :   return true;
    2711             : }
    2712             : 
    2713             : // Trying to write a 64 bit value so need to split into two 32 bit values first,
    2714             : // and pass the lower and high parts through.
    2715           0 : static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
    2716           0 :   SDLoc DL(Op);
    2717           0 :   SDValue WriteValue = Op->getOperand(2);
    2718             : 
    2719             :   // This function is only supposed to be called for i64 type argument.
    2720             :   assert(WriteValue.getValueType() == MVT::i64
    2721             :           && "LowerWRITE_REGISTER called for non-i64 type argument.");
    2722             : 
    2723             :   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
    2724           0 :                            DAG.getConstant(0, DL, MVT::i32));
    2725             :   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
    2726           0 :                            DAG.getConstant(1, DL, MVT::i32));
    2727           0 :   SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
    2728           0 :   return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
    2729             : }
    2730             : 
    2731             : // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
    2732             : // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
    2733             : // one of the above mentioned nodes. It has to be wrapped because otherwise
    2734             : // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
    2735             : // be used to form addressing mode. These wrapped nodes will be selected
    2736             : // into MOVi.
    2737        1266 : SDValue ARMTargetLowering::LowerConstantPool(SDValue Op,
    2738             :                                              SelectionDAG &DAG) const {
    2739             :   EVT PtrVT = Op.getValueType();
    2740             :   // FIXME there is no actual debug info here
    2741             :   SDLoc dl(Op);
    2742             :   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
    2743        1266 :   SDValue Res;
    2744             : 
    2745             :   // When generating execute-only code Constant Pools must be promoted to the
    2746             :   // global data section. It's a bit ugly that we can't share them across basic
    2747             :   // blocks, but this way we guarantee that execute-only behaves correct with
    2748             :   // position-independent addressing modes.
    2749        1266 :   if (Subtarget->genExecuteOnly()) {
    2750           6 :     auto AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
    2751           6 :     auto T = const_cast<Type*>(CP->getType());
    2752           6 :     auto C = const_cast<Constant*>(CP->getConstVal());
    2753           6 :     auto M = const_cast<Module*>(DAG.getMachineFunction().
    2754           6 :                                  getFunction().getParent());
    2755             :     auto GV = new GlobalVariable(
    2756             :                     *M, T, /*isConst=*/true, GlobalVariable::InternalLinkage, C,
    2757           6 :                     Twine(DAG.getDataLayout().getPrivateGlobalPrefix()) + "CP" +
    2758           6 :                     Twine(DAG.getMachineFunction().getFunctionNumber()) + "_" +
    2759           6 :                     Twine(AFI->createPICLabelUId())
    2760           6 :                   );
    2761             :     SDValue GA = DAG.getTargetGlobalAddress(dyn_cast<GlobalValue>(GV),
    2762           6 :                                             dl, PtrVT);
    2763           6 :     return LowerGlobalAddress(GA, DAG);
    2764             :   }
    2765             : 
    2766        1260 :   if (CP->isMachineConstantPoolEntry())
    2767           0 :     Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
    2768           0 :                                     CP->getAlignment());
    2769             :   else
    2770        1260 :     Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
    2771        1260 :                                     CP->getAlignment());
    2772        1260 :   return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
    2773             : }
    2774             : 
    2775          38 : unsigned ARMTargetLowering::getJumpTableEncoding() const {
    2776          38 :   return MachineJumpTableInfo::EK_Inline;
    2777             : }
    2778             : 
    2779          37 : SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
    2780             :                                              SelectionDAG &DAG) const {
    2781          37 :   MachineFunction &MF = DAG.getMachineFunction();
    2782          37 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    2783             :   unsigned ARMPCLabelIndex = 0;
    2784             :   SDLoc DL(Op);
    2785          37 :   EVT PtrVT = getPointerTy(DAG.getDataLayout());
    2786          37 :   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
    2787          37 :   SDValue CPAddr;
    2788          37 :   bool IsPositionIndependent = isPositionIndependent() || Subtarget->isROPI();
    2789             :   if (!IsPositionIndependent) {
    2790          19 :     CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
    2791             :   } else {
    2792          18 :     unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
    2793             :     ARMPCLabelIndex = AFI->createPICLabelUId();
    2794             :     ARMConstantPoolValue *CPV =
    2795          18 :       ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
    2796             :                                       ARMCP::CPBlockAddress, PCAdj);
    2797          18 :     CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
    2798             :   }
    2799          37 :   CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
    2800             :   SDValue Result = DAG.getLoad(
    2801             :       PtrVT, DL, DAG.getEntryNode(), CPAddr,
    2802          37 :       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    2803          37 :   if (!IsPositionIndependent)
    2804          19 :     return Result;
    2805          18 :   SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
    2806          18 :   return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
    2807             : }
    2808             : 
    2809             : /// Convert a TLS address reference into the correct sequence of loads
    2810             : /// and calls to compute the variable's address for Darwin, and return an
    2811             : /// SDValue containing the final node.
    2812             : 
    2813             : /// Darwin only has one TLS scheme which must be capable of dealing with the
    2814             : /// fully general situation, in the worst case. This means:
    2815             : ///     + "extern __thread" declaration.
    2816             : ///     + Defined in a possibly unknown dynamic library.
    2817             : ///
    2818             : /// The general system is that each __thread variable has a [3 x i32] descriptor
    2819             : /// which contains information used by the runtime to calculate the address. The
    2820             : /// only part of this the compiler needs to know about is the first word, which
    2821             : /// contains a function pointer that must be called with the address of the
    2822             : /// entire descriptor in "r0".
    2823             : ///
    2824             : /// Since this descriptor may be in a different unit, in general access must
    2825             : /// proceed along the usual ARM rules. A common sequence to produce is:
    2826             : ///
    2827             : ///     movw rT1, :lower16:_var$non_lazy_ptr
    2828             : ///     movt rT1, :upper16:_var$non_lazy_ptr
    2829             : ///     ldr r0, [rT1]
    2830             : ///     ldr rT2, [r0]
    2831             : ///     blx rT2
    2832             : ///     [...address now in r0...]
    2833             : SDValue
    2834          79 : ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
    2835             :                                                SelectionDAG &DAG) const {
    2836             :   assert(Subtarget->isTargetDarwin() &&
    2837             :          "This function expects a Darwin target");
    2838             :   SDLoc DL(Op);
    2839             : 
    2840             :   // First step is to get the address of the actua global symbol. This is where
    2841             :   // the TLS descriptor lives.
    2842          79 :   SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
    2843             : 
    2844             :   // The first entry in the descriptor is a function pointer that we must call
    2845             :   // to obtain the address of the variable.
    2846          79 :   SDValue Chain = DAG.getEntryNode();
    2847             :   SDValue FuncTLVGet = DAG.getLoad(
    2848             :       MVT::i32, DL, Chain, DescAddr,
    2849             :       MachinePointerInfo::getGOT(DAG.getMachineFunction()),
    2850             :       /* Alignment = */ 4,
    2851             :       MachineMemOperand::MONonTemporal | MachineMemOperand::MODereferenceable |
    2852          79 :           MachineMemOperand::MOInvariant);
    2853          79 :   Chain = FuncTLVGet.getValue(1);
    2854             : 
    2855          79 :   MachineFunction &F = DAG.getMachineFunction();
    2856          79 :   MachineFrameInfo &MFI = F.getFrameInfo();
    2857             :   MFI.setAdjustsStack(true);
    2858             : 
    2859             :   // TLS calls preserve all registers except those that absolutely must be
    2860             :   // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
    2861             :   // silly).
    2862             :   auto TRI =
    2863          79 :       getTargetMachine().getSubtargetImpl(F.getFunction())->getRegisterInfo();
    2864             :   auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
    2865          79 :   const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
    2866             : 
    2867             :   // Finally, we can make the call. This is just a degenerate version of a
    2868             :   // normal AArch64 call node: r0 takes the address of the descriptor, and
    2869             :   // returns the address of the variable in this thread.
    2870          79 :   Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
    2871          79 :   Chain =
    2872          79 :       DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
    2873             :                   Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
    2874          79 :                   DAG.getRegisterMask(Mask), Chain.getValue(1));
    2875          79 :   return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
    2876             : }
    2877             : 
    2878             : SDValue
    2879           7 : ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
    2880             :                                                 SelectionDAG &DAG) const {
    2881             :   assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
    2882             : 
    2883           7 :   SDValue Chain = DAG.getEntryNode();
    2884           7 :   EVT PtrVT = getPointerTy(DAG.getDataLayout());
    2885             :   SDLoc DL(Op);
    2886             : 
    2887             :   // Load the current TEB (thread environment block)
    2888             :   SDValue Ops[] = {Chain,
    2889           7 :                    DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
    2890           7 :                    DAG.getConstant(15, DL, MVT::i32),
    2891           7 :                    DAG.getConstant(0, DL, MVT::i32),
    2892           7 :                    DAG.getConstant(13, DL, MVT::i32),
    2893           7 :                    DAG.getConstant(0, DL, MVT::i32),
    2894          14 :                    DAG.getConstant(2, DL, MVT::i32)};
    2895             :   SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
    2896           7 :                                    DAG.getVTList(MVT::i32, MVT::Other), Ops);
    2897             : 
    2898           7 :   SDValue TEB = CurrentTEB.getValue(0);
    2899           7 :   Chain = CurrentTEB.getValue(1);
    2900             : 
    2901             :   // Load the ThreadLocalStoragePointer from the TEB
    2902             :   // A pointer to the TLS array is located at offset 0x2c from the TEB.
    2903             :   SDValue TLSArray =
    2904           7 :       DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
    2905           7 :   TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo());
    2906             : 
    2907             :   // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
    2908             :   // offset into the TLSArray.
    2909             : 
    2910             :   // Load the TLS index from the C runtime
    2911             :   SDValue TLSIndex =
    2912           7 :       DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
    2913           7 :   TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
    2914           7 :   TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo());
    2915             : 
    2916             :   SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
    2917           7 :                               DAG.getConstant(2, DL, MVT::i32));
    2918             :   SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
    2919             :                             DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
    2920           7 :                             MachinePointerInfo());
    2921             : 
    2922             :   // Get the offset of the start of the .tls section (section base)
    2923             :   const auto *GA = cast<GlobalAddressSDNode>(Op);
    2924           7 :   auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL);
    2925             :   SDValue Offset = DAG.getLoad(
    2926             :       PtrVT, DL, Chain, DAG.getNode(ARMISD::Wrapper, DL, MVT::i32,
    2927             :                                     DAG.getTargetConstantPool(CPV, PtrVT, 4)),
    2928          21 :       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    2929             : 
    2930           7 :   return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset);
    2931             : }
    2932             : 
    2933             : // Lower ISD::GlobalTLSAddress using the "general dynamic" model
    2934             : SDValue
    2935          15 : ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
    2936             :                                                  SelectionDAG &DAG) const {
    2937             :   SDLoc dl(GA);
    2938          15 :   EVT PtrVT = getPointerTy(DAG.getDataLayout());
    2939          15 :   unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
    2940          15 :   MachineFunction &MF = DAG.getMachineFunction();
    2941          15 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    2942             :   unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
    2943             :   ARMConstantPoolValue *CPV =
    2944          15 :     ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
    2945             :                                     ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
    2946          15 :   SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
    2947          15 :   Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
    2948          15 :   Argument = DAG.getLoad(
    2949             :       PtrVT, dl, DAG.getEntryNode(), Argument,
    2950          15 :       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    2951             :   SDValue Chain = Argument.getValue(1);
    2952             : 
    2953          15 :   SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
    2954          15 :   Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
    2955             : 
    2956             :   // call __tls_get_addr.
    2957             :   ArgListTy Args;
    2958             :   ArgListEntry Entry;
    2959          15 :   Entry.Node = Argument;
    2960          15 :   Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
    2961          15 :   Args.push_back(Entry);
    2962             : 
    2963             :   // FIXME: is there useful debug info available here?
    2964          15 :   TargetLowering::CallLoweringInfo CLI(DAG);
    2965          15 :   CLI.setDebugLoc(dl).setChain(Chain).setLibCallee(
    2966          15 :       CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
    2967          30 :       DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args));
    2968             : 
    2969          15 :   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
    2970          15 :   return CallResult.first;
    2971             : }
    2972             : 
    2973             : // Lower ISD::GlobalTLSAddress using the "initial exec" or
    2974             : // "local exec" model.
    2975             : SDValue
    2976          39 : ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
    2977             :                                         SelectionDAG &DAG,
    2978             :                                         TLSModel::Model model) const {
    2979          39 :   const GlobalValue *GV = GA->getGlobal();
    2980             :   SDLoc dl(GA);
    2981          39 :   SDValue Offset;
    2982          39 :   SDValue Chain = DAG.getEntryNode();
    2983          39 :   EVT PtrVT = getPointerTy(DAG.getDataLayout());
    2984             :   // Get the Thread Pointer
    2985          39 :   SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
    2986             : 
    2987          39 :   if (model == TLSModel::InitialExec) {
    2988          18 :     MachineFunction &MF = DAG.getMachineFunction();
    2989          18 :     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    2990             :     unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
    2991             :     // Initial exec model.
    2992          18 :     unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
    2993             :     ARMConstantPoolValue *CPV =
    2994          18 :       ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
    2995             :                                       ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
    2996             :                                       true);
    2997          18 :     Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
    2998          18 :     Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
    2999          18 :     Offset = DAG.getLoad(
    3000             :         PtrVT, dl, Chain, Offset,
    3001          18 :         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    3002          18 :     Chain = Offset.getValue(1);
    3003             : 
    3004          18 :     SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
    3005          18 :     Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
    3006             : 
    3007          18 :     Offset = DAG.getLoad(
    3008             :         PtrVT, dl, Chain, Offset,
    3009          18 :         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    3010             :   } else {
    3011             :     // local exec model
    3012             :     assert(model == TLSModel::LocalExec);
    3013             :     ARMConstantPoolValue *CPV =
    3014          21 :       ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
    3015          21 :     Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
    3016          21 :     Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
    3017          21 :     Offset = DAG.getLoad(
    3018             :         PtrVT, dl, Chain, Offset,
    3019          21 :         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    3020             :   }
    3021             : 
    3022             :   // The address of the thread local variable is the add of the thread
    3023             :   // pointer with the offset of the variable.
    3024          39 :   return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
    3025             : }
    3026             : 
    3027             : SDValue
    3028         219 : ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
    3029             :   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
    3030         219 :   if (DAG.getTarget().useEmulatedTLS())
    3031          79 :     return LowerToTLSEmulatedModel(GA, DAG);
    3032             : 
    3033         140 :   if (Subtarget->isTargetDarwin())
    3034          79 :     return LowerGlobalTLSAddressDarwin(Op, DAG);
    3035             : 
    3036          61 :   if (Subtarget->isTargetWindows())
    3037           7 :     return LowerGlobalTLSAddressWindows(Op, DAG);
    3038             : 
    3039             :   // TODO: implement the "local dynamic" model
    3040             :   assert(Subtarget->isTargetELF() && "Only ELF implemented here");
    3041          54 :   TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
    3042             : 
    3043          54 :   switch (model) {
    3044          15 :     case TLSModel::GeneralDynamic:
    3045             :     case TLSModel::LocalDynamic:
    3046          15 :       return LowerToTLSGeneralDynamicModel(GA, DAG);
    3047          39 :     case TLSModel::InitialExec:
    3048             :     case TLSModel::LocalExec:
    3049          39 :       return LowerToTLSExecModels(GA, DAG, model);
    3050             :   }
    3051           0 :   llvm_unreachable("bogus TLS model");
    3052             : }
    3053             : 
    3054             : /// Return true if all users of V are within function F, looking through
    3055             : /// ConstantExprs.
    3056         148 : static bool allUsersAreInFunction(const Value *V, const Function *F) {
    3057             :   SmallVector<const User*,4> Worklist;
    3058         298 :   for (auto *U : V->users())
    3059         150 :     Worklist.push_back(U);
    3060         418 :   while (!Worklist.empty()) {
    3061             :     auto *U = Worklist.pop_back_val();
    3062         332 :     if (isa<ConstantExpr>(U)) {
    3063         366 :       for (auto *UU : U->users())
    3064         219 :         Worklist.push_back(UU);
    3065             :       continue;
    3066             :     }
    3067             : 
    3068             :     auto *I = dyn_cast<Instruction>(U);
    3069         173 :     if (!I || I->getParent()->getParent() != F)
    3070             :       return false;
    3071             :   }
    3072             :   return true;
    3073             : }
    3074             : 
    3075        1388 : static SDValue promoteToConstantPool(const ARMTargetLowering *TLI,
    3076             :                                      const GlobalValue *GV, SelectionDAG &DAG,
    3077             :                                      EVT PtrVT, const SDLoc &dl) {
    3078             :   // If we're creating a pool entry for a constant global with unnamed address,
    3079             :   // and the global is small enough, we can emit it inline into the constant pool
    3080             :   // to save ourselves an indirection.
    3081             :   //
    3082             :   // This is a win if the constant is only used in one function (so it doesn't
    3083             :   // need to be duplicated) or duplicating the constant wouldn't increase code
    3084             :   // size (implying the constant is no larger than 4 bytes).
    3085        1388 :   const Function &F = DAG.getMachineFunction().getFunction();
    3086             : 
    3087             :   // We rely on this decision to inline being idemopotent and unrelated to the
    3088             :   // use-site. We know that if we inline a variable at one use site, we'll
    3089             :   // inline it elsewhere too (and reuse the constant pool entry). Fast-isel
    3090             :   // doesn't know about this optimization, so bail out if it's enabled else
    3091             :   // we could decide to inline here (and thus never emit the GV) but require
    3092             :   // the GV from fast-isel generated code.
    3093        1388 :   if (!EnableConstpoolPromotion ||
    3094         218 :       DAG.getMachineFunction().getTarget().Options.EnableFastISel)
    3095        1170 :       return SDValue();
    3096             : 
    3097             :   auto *GVar = dyn_cast<GlobalVariable>(GV);
    3098         218 :   if (!GVar || !GVar->hasInitializer() ||
    3099         218 :       !GVar->isConstant() || !GVar->hasGlobalUnnamedAddr() ||
    3100             :       !GVar->hasLocalLinkage())
    3101           0 :     return SDValue();
    3102             : 
    3103             :   // If we inline a value that contains relocations, we move the relocations
    3104             :   // from .data to .text. This is not allowed in position-independent code.
    3105             :   auto *Init = GVar->getInitializer();
    3106         324 :   if ((TLI->isPositionIndependent() || TLI->getSubtarget()->isROPI()) &&
    3107         106 :       Init->needsRelocation())
    3108           6 :     return SDValue();
    3109             : 
    3110             :   // The constant islands pass can only really deal with alignment requests
    3111             :   // <= 4 bytes and cannot pad constants itself. Therefore we cannot promote
    3112             :   // any type wanting greater alignment requirements than 4 bytes. We also
    3113             :   // can only promote constants that are multiples of 4 bytes in size or
    3114             :   // are paddable to a multiple of 4. Currently we only try and pad constants
    3115             :   // that are strings for simplicity.
    3116             :   auto *CDAInit = dyn_cast<ConstantDataArray>(Init);
    3117         212 :   unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType());
    3118         212 :   unsigned Align = DAG.getDataLayout().getPreferredAlignment(GVar);
    3119         212 :   unsigned RequiredPadding = 4 - (Size % 4);
    3120             :   bool PaddingPossible =
    3121         212 :     RequiredPadding == 4 || (CDAInit && CDAInit->isString());
    3122         212 :   if (!PaddingPossible || Align > 4 || Size > ConstpoolPromotionMaxSize ||
    3123             :       Size == 0)
    3124          64 :     return SDValue();
    3125             : 
    3126         148 :   unsigned PaddedSize = Size + ((RequiredPadding == 4) ? 0 : RequiredPadding);
    3127         148 :   MachineFunction &MF = DAG.getMachineFunction();
    3128         148 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    3129             : 
    3130             :   // We can't bloat the constant pool too much, else the ConstantIslands pass
    3131             :   // may fail to converge. If we haven't promoted this global yet (it may have
    3132             :   // multiple uses), and promoting it would increase the constant pool size (Sz
    3133             :   // > 4), ensure we have space to do so up to MaxTotal.
    3134         148 :   if (!AFI->getGlobalsPromotedToConstantPool().count(GVar) && Size > 4)
    3135         144 :     if (AFI->getPromotedConstpoolIncrease() + PaddedSize - 4 >=
    3136             :         ConstpoolPromotionMaxTotal)
    3137           0 :       return SDValue();
    3138             : 
    3139             :   // This is only valid if all users are in a single function; we can't clone
    3140             :   // the constant in general. The LLVM IR unnamed_addr allows merging
    3141             :   // constants, but not cloning them.
    3142             :   //
    3143             :   // We could potentially allow cloning if we could prove all uses of the
    3144             :   // constant in the current function don't care about the address, like
    3145             :   // printf format strings. But that isn't implemented for now.
    3146         148 :   if (!allUsersAreInFunction(GVar, &F))
    3147          62 :     return SDValue();
    3148             : 
    3149             :   // We're going to inline this global. Pad it out if needed.
    3150          86 :   if (RequiredPadding != 4) {
    3151          48 :     StringRef S = CDAInit->getAsString();
    3152             : 
    3153          48 :     SmallVector<uint8_t,16> V(S.size());
    3154             :     std::copy(S.bytes_begin(), S.bytes_end(), V.begin());
    3155         132 :     while (RequiredPadding--)
    3156          84 :       V.push_back(0);
    3157          48 :     Init = ConstantDataArray::get(*DAG.getContext(), V);
    3158             :   }
    3159             : 
    3160          86 :   auto CPVal = ARMConstantPoolConstant::Create(GVar, Init);
    3161             :   SDValue CPAddr =
    3162          86 :     DAG.getTargetConstantPool(CPVal, PtrVT, /*Align=*/4);
    3163          86 :   if (!AFI->getGlobalsPromotedToConstantPool().count(GVar)) {
    3164             :     AFI->markGlobalAsPromotedToConstantPool(GVar);
    3165         172 :     AFI->setPromotedConstpoolIncrease(AFI->getPromotedConstpoolIncrease() +
    3166          86 :                                       PaddedSize - 4);
    3167             :   }
    3168             :   ++NumConstpoolPromoted;
    3169          86 :   return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
    3170             : }
    3171             : 
    3172        1605 : bool ARMTargetLowering::isReadOnly(const GlobalValue *GV) const {
    3173             :   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
    3174           4 :     GV = GA->getBaseObject();
    3175        1605 :   return (isa<GlobalVariable>(GV) && cast<GlobalVariable>(GV)->isConstant()) ||
    3176        1605 :          isa<Function>(GV);
    3177             : }
    3178             : 
    3179        2185 : SDValue ARMTargetLowering::LowerGlobalAddress(SDValue Op,
    3180             :                                               SelectionDAG &DAG) const {
    3181        2185 :   switch (Subtarget->getTargetTriple().getObjectFormat()) {
    3182           0 :   default: llvm_unreachable("unknown object format");
    3183          52 :   case Triple::COFF:
    3184          52 :     return LowerGlobalAddressWindows(Op, DAG);
    3185        1541 :   case Triple::ELF:
    3186        1541 :     return LowerGlobalAddressELF(Op, DAG);
    3187         592 :   case Triple::MachO:
    3188         592 :     return LowerGlobalAddressDarwin(Op, DAG);
    3189             :   }
    3190             : }
    3191             : 
    3192        1541 : SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
    3193             :                                                  SelectionDAG &DAG) const {
    3194        1541 :   EVT PtrVT = getPointerTy(DAG.getDataLayout());
    3195             :   SDLoc dl(Op);
    3196        1541 :   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
    3197        1541 :   const TargetMachine &TM = getTargetMachine();
    3198        1541 :   bool IsRO = isReadOnly(GV);
    3199             : 
    3200             :   // promoteToConstantPool only if not generating XO text section
    3201        1541 :   if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV) && !Subtarget->genExecuteOnly())
    3202        1388 :     if (SDValue V = promoteToConstantPool(this, GV, DAG, PtrVT, dl))
    3203          86 :       return V;
    3204             : 
    3205        1455 :   if (isPositionIndependent()) {
    3206         123 :     bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
    3207             :     SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
    3208         246 :                                            UseGOT_PREL ? ARMII::MO_GOT : 0);
    3209         123 :     SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G);
    3210         123 :     if (UseGOT_PREL)
    3211          61 :       Result =
    3212          61 :           DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
    3213          61 :                       MachinePointerInfo::getGOT(DAG.getMachineFunction()));
    3214         123 :     return Result;
    3215        1332 :   } else if (Subtarget->isROPI() && IsRO) {
    3216             :     // PC-relative.
    3217          60 :     SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
    3218          60 :     SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G);
    3219          60 :     return Result;
    3220        1272 :   } else if (Subtarget->isRWPI() && !IsRO) {
    3221             :     // SB-relative.
    3222          30 :     SDValue RelAddr;
    3223          30 :     if (Subtarget->useMovt(DAG.getMachineFunction())) {
    3224             :       ++NumMovwMovt;
    3225          12 :       SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_SBREL);
    3226          12 :       RelAddr = DAG.getNode(ARMISD::Wrapper, dl, PtrVT, G);
    3227             :     } else { // use literal pool for address constant
    3228             :       ARMConstantPoolValue *CPV =
    3229          18 :         ARMConstantPoolConstant::Create(GV, ARMCP::SBREL);
    3230          18 :       SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
    3231          18 :       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
    3232          18 :       RelAddr = DAG.getLoad(
    3233             :           PtrVT, dl, DAG.getEntryNode(), CPAddr,
    3234          18 :           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    3235             :     }
    3236          30 :     SDValue SB = DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::R9, PtrVT);
    3237          30 :     SDValue Result = DAG.getNode(ISD::ADD, dl, PtrVT, SB, RelAddr);
    3238          30 :     return Result;
    3239             :   }
    3240             : 
    3241             :   // If we have T2 ops, we can materialize the address directly via movt/movw
    3242             :   // pair. This is always cheaper.
    3243        1242 :   if (Subtarget->useMovt(DAG.getMachineFunction())) {
    3244             :     ++NumMovwMovt;
    3245             :     // FIXME: Once remat is capable of dealing with instructions with register
    3246             :     // operands, expand this into two nodes.
    3247             :     return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
    3248         764 :                        DAG.getTargetGlobalAddress(GV, dl, PtrVT));
    3249             :   } else {
    3250         478 :     SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
    3251         478 :     CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
    3252             :     return DAG.getLoad(
    3253             :         PtrVT, dl, DAG.getEntryNode(), CPAddr,
    3254         478 :         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    3255             :   }
    3256             : }
    3257             : 
    3258         671 : SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
    3259             :                                                     SelectionDAG &DAG) const {
    3260             :   assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
    3261             :          "ROPI/RWPI not currently supported for Darwin");
    3262         671 :   EVT PtrVT = getPointerTy(DAG.getDataLayout());
    3263             :   SDLoc dl(Op);
    3264         671 :   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
    3265             : 
    3266         671 :   if (Subtarget->useMovt(DAG.getMachineFunction()))
    3267             :     ++NumMovwMovt;
    3268             : 
    3269             :   // FIXME: Once remat is capable of dealing with instructions with register
    3270             :   // operands, expand this into multiple nodes
    3271             :   unsigned Wrapper =
    3272         671 :       isPositionIndependent() ? ARMISD::WrapperPIC : ARMISD::Wrapper;
    3273             : 
    3274         671 :   SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
    3275         671 :   SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
    3276             : 
    3277         671 :   if (Subtarget->isGVIndirectSymbol(GV))
    3278         376 :     Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
    3279         376 :                          MachinePointerInfo::getGOT(DAG.getMachineFunction()));
    3280         671 :   return Result;
    3281             : }
    3282             : 
    3283          52 : SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
    3284             :                                                      SelectionDAG &DAG) const {
    3285             :   assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
    3286             :   assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
    3287             :          "Windows on ARM expects to use movw/movt");
    3288             :   assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
    3289             :          "ROPI/RWPI not currently supported for Windows");
    3290             : 
    3291          52 :   const TargetMachine &TM = getTargetMachine();
    3292          52 :   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
    3293             :   ARMII::TOF TargetFlags = ARMII::MO_NO_FLAG;
    3294          52 :   if (GV->hasDLLImportStorageClass())
    3295             :     TargetFlags = ARMII::MO_DLLIMPORT;
    3296          49 :   else if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
    3297             :     TargetFlags = ARMII::MO_COFFSTUB;
    3298          52 :   EVT PtrVT = getPointerTy(DAG.getDataLayout());
    3299             :   SDValue Result;
    3300             :   SDLoc DL(Op);
    3301             : 
    3302             :   ++NumMovwMovt;
    3303             : 
    3304             :   // FIXME: Once remat is capable of dealing with instructions with register
    3305             :   // operands, expand this into two nodes.
    3306          52 :   Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
    3307             :                        DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
    3308          52 :                                                   TargetFlags));
    3309          52 :   if (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB))
    3310           6 :     Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
    3311           6 :                          MachinePointerInfo::getGOT(DAG.getMachineFunction()));
    3312          52 :   return Result;
    3313             : }
    3314             : 
    3315             : SDValue
    3316           8 : ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
    3317             :   SDLoc dl(Op);
    3318           8 :   SDValue Val = DAG.getConstant(0, dl, MVT::i32);
    3319             :   return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
    3320             :                      DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
    3321           8 :                      Op.getOperand(1), Val);
    3322             : }
    3323             : 
    3324             : SDValue
    3325           9 : ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
    3326             :   SDLoc dl(Op);
    3327             :   return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
    3328           9 :                      Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
    3329             : }
    3330             : 
    3331          32 : SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
    3332             :                                                       SelectionDAG &DAG) const {
    3333             :   SDLoc dl(Op);
    3334             :   return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
    3335          32 :                      Op.getOperand(0));
    3336             : }
    3337             : 
    3338             : SDValue
    3339        2477 : ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
    3340             :                                           const ARMSubtarget *Subtarget) const {
    3341        4954 :   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
    3342             :   SDLoc dl(Op);
    3343        2477 :   switch (IntNo) {
    3344        2362 :   default: return SDValue();    // Don't custom lower most intrinsics.
    3345           1 :   case Intrinsic::thread_pointer: {
    3346           1 :     EVT PtrVT = getPointerTy(DAG.getDataLayout());
    3347           1 :     return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
    3348             :   }
    3349          32 :   case Intrinsic::eh_sjlj_lsda: {
    3350          32 :     MachineFunction &MF = DAG.getMachineFunction();
    3351          32 :     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    3352             :     unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
    3353          32 :     EVT PtrVT = getPointerTy(DAG.getDataLayout());
    3354             :     SDValue CPAddr;
    3355          32 :     bool IsPositionIndependent = isPositionIndependent();
    3356          32 :     unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0;
    3357             :     ARMConstantPoolValue *CPV =
    3358          32 :       ARMConstantPoolConstant::Create(&MF.getFunction(), ARMPCLabelIndex,
    3359             :                                       ARMCP::CPLSDA, PCAdj);
    3360          32 :     CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
    3361          32 :     CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
    3362             :     SDValue Result = DAG.getLoad(
    3363             :         PtrVT, dl, DAG.getEntryNode(), CPAddr,
    3364          32 :         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
    3365             : 
    3366          32 :     if (IsPositionIndependent) {
    3367          25 :       SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
    3368          25 :       Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
    3369             :     }
    3370          32 :     return Result;
    3371             :   }
    3372             :   case Intrinsic::arm_neon_vabs:
    3373          16 :     return DAG.getNode(ISD::ABS, SDLoc(Op), Op.getValueType(),
    3374          32 :                         Op.getOperand(1));
    3375          15 :   case Intrinsic::arm_neon_vmulls:
    3376             :   case Intrinsic::arm_neon_vmullu: {
    3377             :     unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
    3378          15 :       ? ARMISD::VMULLs : ARMISD::VMULLu;
    3379          15 :     return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
    3380          30 :                        Op.getOperand(1), Op.getOperand(2));
    3381             :   }
    3382           8 :   case Intrinsic::arm_neon_vminnm:
    3383             :   case Intrinsic::arm_neon_vmaxnm: {
    3384             :     unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
    3385           8 :       ? ISD::FMINNUM : ISD::FMAXNUM;
    3386           8 :     return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
    3387          16 :                        Op.getOperand(1), Op.getOperand(2));
    3388             :   }
    3389          16 :   case Intrinsic::arm_neon_vminu:
    3390             :   case Intrinsic::arm_neon_vmaxu: {
    3391          16 :     if (Op.getValueType().isFloatingPoint())
    3392           0 :       return SDValue();
    3393             :     unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
    3394          16 :       ? ISD::UMIN : ISD::UMAX;
    3395          16 :     return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
    3396          32 :                          Op.getOperand(1), Op.getOperand(2));
    3397             :   }
    3398          22 :   case Intrinsic::arm_neon_vmins:
    3399             :   case Intrinsic::arm_neon_vmaxs: {
    3400             :     // v{min,max}s is overloaded between signed integers and floats.
    3401          22 :     if (!Op.getValueType().isFloatingPoint()) {
    3402             :       unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
    3403          12 :         ? ISD::SMIN : ISD::SMAX;
    3404          12 :       return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
    3405          24 :                          Op.getOperand(1), Op.getOperand(2));
    3406             :     }
    3407             :     unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
    3408          10 :       ? ISD::FMINNAN : ISD::FMAXNAN;
    3409          10 :     return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
    3410          20 :                        Op.getOperand(1), Op.getOperand(2));
    3411             :   }
    3412             :   case Intrinsic::arm_neon_vtbl1:
    3413           1 :     return DAG.getNode(ARMISD::VTBL1, SDLoc(Op), Op.getValueType(),
    3414           2 :                        Op.getOperand(1), Op.getOperand(2));
    3415             :   case Intrinsic::arm_neon_vtbl2:
    3416           4 :     return DAG.getNode(ARMISD::VTBL2, SDLoc(Op), Op.getValueType(),
    3417           8 :                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
    3418             :   }
    3419             : }
    3420             : 
    3421          21 : static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
    3422             :                                  const ARMSubtarget *Subtarget) {
    3423             :   SDLoc dl(Op);
    3424             :   ConstantSDNode *SSIDNode = cast<ConstantSDNode>(Op.getOperand(2));
    3425          21 :   auto SSID = static_cast<SyncScope::ID>(SSIDNode->getZExtValue());
    3426          21 :   if (SSID == SyncScope::SingleThread)
    3427           6 :     return Op;
    3428             : 
    3429          15 :   if (!Subtarget->hasDataBarrier()) {
    3430             :     // Some ARMv6 cpus can support data barriers with an mcr instruction.
    3431             :     // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
    3432             :     // here.
    3433             :     assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
    3434             :            "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
    3435             :     return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
    3436           0 :                        DAG.getConstant(0, dl, MVT::i32));
    3437             :   }
    3438             : 
    3439             :   ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
    3440          15 :   AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
    3441             :   ARM_MB::MemBOpt Domain = ARM_MB::ISH;
    3442          15 :   if (Subtarget->isMClass()) {
    3443             :     // Only a full system barrier exists in the M-class architectures.
    3444             :     Domain = ARM_MB::SY;
    3445          13 :   } else if (Subtarget->preferISHSTBarriers() &&
    3446             :              Ord == AtomicOrdering::Release) {
    3447             :     // Swift happens to implement ISHST barriers in a way that's compatible with
    3448             :     // Release semantics but weaker than ISH so we'd be fools not to use
    3449             :     // it. Beware: other processors probably don't!
    3450             :     Domain = ARM_MB::ISHST;
    3451             :   }
    3452             : 
    3453             :   return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
    3454             :                      DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
    3455          15 :                      DAG.getConstant(Domain, dl, MVT::i32));
    3456             : }
    3457             : 
    3458           0 : static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
    3459             :                              const ARMSubtarget *Subtarget) {
    3460             :   // ARM pre v5TE and Thumb1 does not have preload instructions.
    3461           0 :   if (!(Subtarget->isThumb2() ||
    3462           0 :         (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
    3463             :     // Just preserve the chain.
    3464           0 :     return Op.getOperand(0);
    3465             : 
    3466           0 :   SDLoc dl(Op);
    3467           0 :   unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
    3468           0 :   if (!isRead &&
    3469           0 :       (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
    3470             :     // ARMv7 with MP extension has PLDW.
    3471           0 :     return Op.getOperand(0);
    3472             : 
    3473           0 :   unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
    3474           0 :   if (Subtarget->isThumb()) {
    3475             :     // Invert the bits.
    3476           0 :     isRead = ~isRead & 1;
    3477           0 :     isData = ~isData & 1;
    3478             :   }
    3479             : 
    3480             :   return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
    3481             :                      Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
    3482           0 :                      DAG.getConstant(isData, dl, MVT::i32));
    3483             : }
    3484             : 
    3485           0 : static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
    3486           0 :   MachineFunction &MF = DAG.getMachineFunction();
    3487           0 :   ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
    3488             : 
    3489             :   // vastart just stores the address of the VarArgsFrameIndex slot into the
    3490             :   // memory location argument.
    3491           0 :   SDLoc dl(Op);
    3492           0 :   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
    3493           0 :   SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
    3494           0 :   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
    3495             :   return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
    3496           0 :                       MachinePointerInfo(SV));
    3497             : }
    3498             : 
    3499        1075 : SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA,
    3500             :                                                 CCValAssign &NextVA,
    3501             :                                                 SDValue &Root,
    3502             :                                                 SelectionDAG &DAG,
    3503             :                                                 const SDLoc &dl) const {
    3504        1075 :   MachineFunction &MF = DAG.getMachineFunction();
    3505        1075 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    3506             : 
    3507             :   const TargetRegisterClass *RC;
    3508        1075 :   if (AFI->isThumb1OnlyFunction())
    3509             :     RC = &ARM::tGPRRegClass;
    3510             :   else
    3511             :     RC = &ARM::GPRRegClass;
    3512             : 
    3513             :   // Transform the arguments stored in physical registers into virtual ones.
    3514        1075 :   unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
    3515        1075 :   SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
    3516             : 
    3517             :   SDValue ArgValue2;
    3518        1075 :   if (NextVA.isMemLoc()) {
    3519          29 :     MachineFrameInfo &MFI = MF.getFrameInfo();
    3520          29 :     int FI = MFI.CreateFixedObject(4, NextVA.getLocMemOffset(), true);
    3521             : 
    3522             :     // Create load node to retrieve arguments from the stack.
    3523          58 :     SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
    3524          29 :     ArgValue2 = DAG.getLoad(
    3525             :         MVT::i32, dl, Root, FIN,
    3526          29 :         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
    3527             :   } else {
    3528        1046 :     Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
    3529        1046 :     ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
    3530             :   }
    3531        1075 :   if (!Subtarget->isLittle())
    3532             :     std::swap (ArgValue, ArgValue2);
    3533        1075 :   return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
    3534             : }
    3535             : 
    3536             : // The remaining GPRs hold either the beginning of variable-argument
    3537             : // data, or the beginning of an aggregate passed by value (usually
    3538             : // byval).  Either way, we allocate stack slots adjacent to the data
    3539             : // provided by our caller, and store the unallocated registers there.
    3540             : // If this is a variadic function, the va_list pointer will begin with
    3541             : // these values; otherwise, this reassembles a (byval) structure that
    3542             : // was split between registers and memory.
    3543             : // Return: The frame index registers were stored into.
    3544          81 : int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
    3545             :                                       const SDLoc &dl, SDValue &Chain,
    3546             :                                       const Value *OrigArg,
    3547             :                                       unsigned InRegsParamRecordIdx,
    3548             :                                       int ArgOffset, unsigned ArgSize) const {
    3549             :   // Currently, two use-cases possible:
    3550             :   // Case #1. Non-var-args function, and we meet first byval parameter.
    3551             :   //          Setup first unallocated register as first byval register;
    3552             :   //          eat all remained registers
    3553             :   //          (these two actions are performed by HandleByVal method).
    3554             :   //          Then, here, we initialize stack frame with
    3555             :   //          "store-reg" instructions.
    3556             :   // Case #2. Var-args function, that doesn't contain byval parameters.
    3557             :   //          The same: eat all remained unallocated registers,
    3558             :   //          initialize stack frame.
    3559             : 
    3560          81 :   MachineFunction &MF = DAG.getMachineFunction();
    3561          81 :   MachineFrameInfo &MFI = MF.getFrameInfo();
    3562          81 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    3563             :   unsigned RBegin, REnd;
    3564          81 :   if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
    3565             :     CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
    3566             :   } else {
    3567             :     unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
    3568          47 :     RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
    3569             :     REnd = ARM::R4;
    3570             :   }
    3571             : 
    3572          81 :   if (REnd != RBegin)
    3573          67 :     ArgOffset = -4 * (ARM::R4 - RBegin);
    3574             : 
    3575          81 :   auto PtrVT = getPointerTy(DAG.getDataLayout());
    3576          81 :   int FrameIndex = MFI.CreateFixedObject(ArgSize, ArgOffset, false);
    3577          81 :   SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
    3578             : 
    3579             :   SmallVector<SDValue, 4> MemOps;
    3580             :   const TargetRegisterClass *RC =
    3581          81 :       AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
    3582             : 
    3583         261 :   for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
    3584         180 :     unsigned VReg = MF.addLiveIn(Reg, RC);
    3585         180 :     SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
    3586             :     SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
    3587         256 :                                  MachinePointerInfo(OrigArg, 4 * i));
    3588         180 :     MemOps.push_back(Store);
    3589         180 :     FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
    3590             :   }
    3591             : 
    3592          81 :   if (!MemOps.empty())
    3593          67 :     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
    3594          81 :   return FrameIndex;
    3595             : }
    3596             : 
    3597             : // Setup stack frame, the va_list pointer will start from.
    3598          34 : void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
    3599             :                                              const SDLoc &dl, SDValue &Chain,
    3600             :                                              unsigned ArgOffset,
    3601             :                                              unsigned TotalArgRegsSaveSize,
    3602             :                                              bool ForceMutable) const {
    3603          34 :   MachineFunction &MF = DAG.getMachineFunction();
    3604          34 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    3605             : 
    3606             :   // Try to store any remaining integer argument regs
    3607             :   // to their spots on the stack so that they may be loaded by dereferencing
    3608             :   // the result of va_next.
    3609             :   // If there is no regs to be stored, just point address after last
    3610             :   // argument passed via stack.
    3611          34 :   int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
    3612             :                                   CCInfo.getInRegsParamsCount(),
    3613          34 :                                   CCInfo.getNextStackOffset(), 4);
    3614             :   AFI->setVarArgsFrameIndex(FrameIndex);
    3615          34 : }
    3616             : 
    3617       13769 : SDValue ARMTargetLowering::LowerFormalArguments(
    3618             :     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
    3619             :     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
    3620             :     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
    3621       13769 :   MachineFunction &MF = DAG.getMachineFunction();
    3622       13769 :   MachineFrameInfo &MFI = MF.getFrameInfo();
    3623             : 
    3624       13769 :   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
    3625             : 
    3626             :   // Assign locations to all of the incoming arguments.
    3627             :   SmallVector<CCValAssign, 16> ArgLocs;
    3628             :   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
    3629       27538 :                  *DAG.getContext());
    3630       13769 :   CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, isVarArg));
    3631             : 
    3632             :   SmallVector<SDValue, 16> ArgValues;
    3633       13769 :   SDValue ArgValue;
    3634       13769 :   Function::const_arg_iterator CurOrigArg = MF.getFunction().arg_begin();
    3635             :   unsigned CurArgIdx = 0;
    3636             : 
    3637             :   // Initially ArgRegsSaveSize is zero.
    3638             :   // Then we increase this value each time we meet byval parameter.
    3639             :   // We also increase this value in case of varargs function.
    3640             :   AFI->setArgRegsSaveSize(0);
    3641             : 
    3642             :   // Calculate the amount of stack space that we need to allocate to store
    3643             :   // byval and variadic arguments that are passed in registers.
    3644             :   // We need to know this before we allocate the first byval or variadic
    3645             :   // argument, as they will be allocated a stack slot below the CFA (Canonical
    3646             :   // Frame Address, the stack pointer at entry to the function).
    3647       13769 :   unsigned ArgRegBegin = ARM::R4;
    3648       13823 :   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
    3649       22592 :     if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
    3650             :       break;
    3651             : 
    3652          54 :     CCValAssign &VA = ArgLocs[i];
    3653          54 :     unsigned Index = VA.getValNo();
    3654         108 :     ISD::ArgFlagsTy Flags = Ins[Index].Flags;
    3655          54 :     if (!Flags.isByVal())
    3656          20 :       continue;
    3657             : 
    3658             :     assert(VA.isMemLoc() && "unexpected byval pointer in reg");
    3659             :     unsigned RBegin, REnd;
    3660             :     CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
    3661          34 :     ArgRegBegin = std::min(ArgRegBegin, RBegin);
    3662             : 
    3663             :     CCInfo.nextInRegsParam();
    3664             :   }
    3665             :   CCInfo.rewindByValRegsInfo();
    3666             : 
    3667             :   int lastInsIndex = -1;
    3668       13769 :   if (isVarArg && MFI.hasVAStart()) {
    3669             :     unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
    3670          34 :     if (RegIdx != array_lengthof(GPRArgRegs))
    3671          66 :       ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
    3672             :   }
    3673             : 
    3674       13769 :   unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
    3675             :   AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
    3676       13769 :   auto PtrVT = getPointerTy(DAG.getDataLayout());
    3677             : 
    3678       36693 :   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
    3679       22924 :     CCValAssign &VA = ArgLocs[i];
    3680       45848 :     if (Ins[VA.getValNo()].isOrigArg()) {
    3681       22665 :       std::advance(CurOrigArg,
    3682             :                    Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
    3683             :       CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
    3684             :     }
    3685             :     // Arguments stored in registers.
    3686       22924 :     if (VA.isRegLoc()) {
    3687             :       EVT RegVT = VA.getLocVT();
    3688             : 
    3689       21302 :       if (VA.needsCustom()) {
    3690             :         // f64 and vector types are split up into multiple registers or
    3691             :         // combinations of registers and stack slots.
    3692         878 :         if (VA.getLocVT() == MVT::v2f64) {
    3693         216 :           SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
    3694         432 :                                                    Chain, DAG, dl);
    3695         216 :           VA = ArgLocs[++i]; // skip ahead to next loc
    3696         216 :           SDValue ArgValue2;
    3697         216 :           if (VA.isMemLoc()) {
    3698          19 :             int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), true);
    3699          19 :             SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
    3700          19 :             ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
    3701             :                                     MachinePointerInfo::getFixedStack(
    3702          19 :                                         DAG.getMachineFunction(), FI));
    3703             :           } else {
    3704         197 :             ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
    3705         197 :                                              Chain, DAG, dl);
    3706             :           }
    3707         216 :           ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
    3708         216 :           ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
    3709             :                                  ArgValue, ArgValue1,
    3710         216 :                                  DAG.getIntPtrConstant(0, dl));
    3711         216 :           ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
    3712             :                                  ArgValue, ArgValue2,
    3713         216 :                                  DAG.getIntPtrConstant(1, dl));
    3714             :         } else
    3715        1324 :           ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
    3716             :       } else {
    3717             :         const TargetRegisterClass *RC;
    3718             : 
    3719             : 
    3720             :         if (RegVT == MVT::f16)
    3721             :           RC = &ARM::HPRRegClass;
    3722             :         else if (RegVT == MVT::f32)
    3723             :           RC = &ARM::SPRRegClass;
    3724             :         else if (RegVT == MVT::f64 || RegVT == MVT::v4f16)
    3725             :           RC = &ARM::DPRRegClass;
    3726             :         else if (RegVT == MVT::v2f64 || RegVT == MVT::v8f16)
    3727             :           RC = &ARM::QPRRegClass;
    3728             :         else if (RegVT == MVT::i32)
    3729       17817 :           RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
    3730             :                                            : &ARM::GPRRegClass;
    3731             :         else
    3732           0 :           llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
    3733             : 
    3734             :         // Transform the arguments in physical registers into virtual ones.
    3735       20424 :         unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
    3736       20424 :         ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
    3737             :       }
    3738             : 
    3739             :       // If this is an 8 or 16-bit value, it is really passed promoted
    3740             :       // to 32 bits.  Insert an assert[sz]ext to capture this, then
    3741             :       // truncate to the right size.
    3742       21302 :       switch (VA.getLocInfo()) {
    3743           0 :       default: llvm_unreachable("Unknown loc info!");
    3744             :       case CCValAssign::Full: break;
    3745             :       case CCValAssign::BCvt:
    3746        2079 :         ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
    3747        2079 :         break;
    3748             :       case CCValAssign::SExt:
    3749           0 :         ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
    3750           0 :                                DAG.getValueType(VA.getValVT()));
    3751           0 :         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
    3752           0 :         break;
    3753             :       case CCValAssign::ZExt:
    3754           0 :         ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
    3755           0 :                                DAG.getValueType(VA.getValVT()));
    3756           0 :         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
    3757           0 :         break;
    3758             :       }
    3759             : 
    3760       21302 :       InVals.push_back(ArgValue);
    3761             :     } else { // VA.isRegLoc()
    3762             :       // sanity check
    3763             :       assert(VA.isMemLoc());
    3764             :       assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
    3765             : 
    3766        1622 :       int index = VA.getValNo();
    3767             : 
    3768             :       // Some Ins[] entries become multiple ArgLoc[] entries.
    3769             :       // Process them only once.
    3770        1622 :       if (index != lastInsIndex)
    3771             :         {
    3772        3244 :           ISD::ArgFlagsTy Flags = Ins[index].Flags;
    3773             :           // FIXME: For now, all byval parameter objects are marked mutable.
    3774             :           // This can be changed with more analysis.
    3775             :           // In case of tail call optimization mark all arguments mutable.
    3776             :           // Since they could be overwritten by lowering of arguments in case of
    3777             :           // a tail call.
    3778        1622 :           if (Flags.isByVal()) {
    3779             :             assert(Ins[index].isOrigArg() &&
    3780             :                    "Byval arguments cannot be implicit");
    3781          47 :             unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
    3782             : 
    3783          47 :             int FrameIndex = StoreByValRegs(
    3784             :                 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
    3785          47 :                 VA.getLocMemOffset(), Flags.getByValSize());
    3786          94 :             InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
    3787             :             CCInfo.nextInRegsParam();
    3788             :           } else {
    3789        1575 :             unsigned FIOffset = VA.getLocMemOffset();
    3790        1575 :             int FI = MFI.CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
    3791             :                                            FIOffset, true);
    3792             : 
    3793             :             // Create load nodes to retrieve arguments from the stack.
    3794        1575 :             SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
    3795        3150 :             InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
    3796             :                                          MachinePointerInfo::getFixedStack(
    3797        3150 :                                              DAG.getMachineFunction(), FI)));
    3798             :           }
    3799             :           lastInsIndex = index;
    3800             :         }
    3801             :     }
    3802             :   }
    3803             : 
    3804             :   // varargs
    3805       13769 :   if (isVarArg && MFI.hasVAStart())
    3806          34 :     VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
    3807             :                          CCInfo.getNextStackOffset(),
    3808             :                          TotalArgRegsSaveSize);
    3809             : 
    3810       13769 :   AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
    3811             : 
    3812       13769 :   return Chain;
    3813             : }
    3814             : 
    3815             : /// isFloatingPointZero - Return true if this is +0.0.
    3816           0 : static bool isFloatingPointZero(SDValue Op) {
    3817             :   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
    3818           0 :     return CFP->getValueAPF().isPosZero();
    3819             :   else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
    3820             :     // Maybe this has already been legalized into the constant pool?
    3821           0 :     if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
    3822           0 :       SDValue WrapperOp = Op.getOperand(1).getOperand(0);
    3823             :       if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
    3824           0 :         if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
    3825           0 :           return CFP->getValueAPF().isPosZero();
    3826             :     }
    3827           0 :   } else if (Op->getOpcode() == ISD::BITCAST &&
    3828           0 :              Op->getValueType(0) == MVT::f64) {
    3829             :     // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
    3830             :     // created by LowerConstantFP().
    3831           0 :     SDValue BitcastOp = Op->getOperand(0);
    3832           0 :     if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
    3833           0 :         isNullConstant(BitcastOp->getOperand(0)))
    3834           0 :       return true;
    3835             :   }
    3836             :   return false;
    3837             : }
    3838             : 
    3839             : /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
    3840             : /// the given operands.
    3841        3223 : SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
    3842             :                                      SDValue &ARMcc, SelectionDAG &DAG,
    3843             :                                      const SDLoc &dl) const {
    3844             :   if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
    3845        2641 :     unsigned C = RHSC->getZExtValue();
    3846        2641 :     if (!isLegalICmpImmediate((int32_t)C)) {
    3847             :       // Constant does not fit, try adjusting it by one.
    3848         104 :       switch (CC) {
    3849             :       default: break;
    3850          25 :       case ISD::SETLT:
    3851             :       case ISD::SETGE:
    3852          25 :         if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
    3853           4 :           CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
    3854           4 :           RHS = DAG.getConstant(C - 1, dl, MVT::i32);
    3855             :         }
    3856             :         break;
    3857           3 :       case ISD::SETULT:
    3858             :       case ISD::SETUGE:
    3859           3 :         if (C != 0 && isLegalICmpImmediate(C-1)) {
    3860           1 :           CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
    3861           1 :           RHS = DAG.getConstant(C - 1, dl, MVT::i32);
    3862             :         }
    3863             :         break;
    3864          49 :       case ISD::SETLE:
    3865             :       case ISD::SETGT:
    3866          49 :         if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
    3867          42 :           CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
    3868          42 :           RHS = DAG.getConstant(C + 1, dl, MVT::i32);
    3869             :         }
    3870             :         break;
    3871           5 :       case ISD::SETULE:
    3872             :       case ISD::SETUGT:
    3873           5 :         if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
    3874           0 :           CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
    3875           0 :           RHS = DAG.getConstant(C + 1, dl, MVT::i32);
    3876             :         }
    3877             :         break;
    3878             :       }
    3879             :     }
    3880          24 :   } else if ((ARM_AM::getShiftOpcForNode(LHS.getOpcode()) != ARM_AM::no_shift) &&
    3881             :              (ARM_AM::getShiftOpcForNode(RHS.getOpcode()) == ARM_AM::no_shift)) {
    3882             :     // In ARM and Thumb-2, the compare instructions can shift their second
    3883             :     // operand.
    3884          24 :     CC = ISD::getSetCCSwappedOperands(CC);
    3885             :     std::swap(LHS, RHS);
    3886             :   }
    3887             : 
    3888        3223 :   ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
    3889             :   ARMISD::NodeType CompareType;
    3890        3223 :   switch (CondCode) {
    3891             :   default:
    3892             :     CompareType = ARMISD::CMP;
    3893             :     break;
    3894        2374 :   case ARMCC::EQ:
    3895             :   case ARMCC::NE:
    3896             :     // Uses only Z Flag
    3897             :     CompareType = ARMISD::CMPZ;
    3898        2374 :     break;
    3899             :   }
    3900        3223 :   ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
    3901        3223 :   return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
    3902             : }
    3903             : 
    3904             : /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
    3905         599 : SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS,
    3906             :                                      SelectionDAG &DAG, const SDLoc &dl,
    3907             :                                      bool InvalidOnQNaN) const {
    3908             :   assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
    3909         599 :   SDValue Cmp;
    3910         599 :   SDValue C = DAG.getConstant(InvalidOnQNaN, dl, MVT::i32);
    3911         599 :   if (!isFloatingPointZero(RHS))
    3912         517 :     Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS, C);
    3913             :   else
    3914          82 :     Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS, C);
    3915         599 :   return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
    3916             : }
    3917             : 
    3918             : /// duplicateCmp - Glue values can have only one use, so this function
    3919             : /// duplicates a comparison node.
    3920             : SDValue
    3921           2 : ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
    3922             :   unsigned Opc = Cmp.getOpcode();
    3923             :   SDLoc DL(Cmp);
    3924           2 :   if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
    3925           1 :     return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
    3926             : 
    3927             :   assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
    3928           1 :   Cmp = Cmp.getOperand(0);
    3929             :   Opc = Cmp.getOpcode();
    3930           1 :   if (Opc == ARMISD::CMPFP)
    3931           0 :     Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),
    3932           0 :                       Cmp.getOperand(1), Cmp.getOperand(2));
    3933             :   else {
    3934             :     assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
    3935           1 :     Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),
    3936           1 :                       Cmp.getOperand(1));
    3937             :   }
    3938           1 :   return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
    3939             : }
    3940             : 
    3941             : // This function returns three things: the arithmetic computation itself
    3942             : // (Value), a comparison (OverflowCmp), and a condition code (ARMcc).  The
    3943             : // comparison and the condition code define the case in which the arithmetic
    3944             : // computation *does not* overflow.
    3945             : std::pair<SDValue, SDValue>
    3946          36 : ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
    3947             :                                  SDValue &ARMcc) const {
    3948             :   assert(Op.getValueType() == MVT::i32 &&  "Unsupported value type");
    3949             : 
    3950             :   SDValue Value, OverflowCmp;
    3951          36 :   SDValue LHS = Op.getOperand(0);
    3952          36 :   SDValue RHS = Op.getOperand(1);
    3953             :   SDLoc dl(Op);
    3954             : 
    3955             :   // FIXME: We are currently always generating CMPs because we don't support
    3956             :   // generating CMN through the backend. This is not as good as the natural
    3957             :   // CMP case because it causes a register dependency and cannot be folded
    3958             :   // later.
    3959             : 
    3960          36 :   switch (Op.getOpcode()) {
    3961           0 :   default:
    3962           0 :     llvm_unreachable("Unknown overflow instruction!");
    3963             :   case ISD::SADDO:
    3964          19 :     ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
    3965          19 :     Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
    3966          19 :     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
    3967          19 :     break;
    3968             :   case ISD::UADDO:
    3969           2 :     ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
    3970             :     // We use ADDC here to correspond to its use in LowerUnsignedALUO.
    3971             :     // We do not use it in the USUBO case as Value may not be used.
    3972           4 :     Value = DAG.getNode(ARMISD::ADDC, dl,
    3973           2 :                         DAG.getVTList(Op.getValueType(), MVT::i32), LHS, RHS)
    3974           2 :                 .getValue(0);
    3975           2 :     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
    3976           2 :     break;
    3977             :   case ISD::SSUBO:
    3978          11 :     ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
    3979          11 :     Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
    3980          11 :     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
    3981          11 :     break;
    3982             :   case ISD::USUBO:
    3983           2 :     ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
    3984           2 :     Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
    3985           2 :     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
    3986           2 :     break;
    3987             :   case ISD::UMULO:
    3988             :     // We generate a UMUL_LOHI and then check if the high word is 0.
    3989           1 :     ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32);
    3990           1 :     Value = DAG.getNode(ISD::UMUL_LOHI, dl,
    3991             :                         DAG.getVTList(Op.getValueType(), Op.getValueType()),
    3992           1 :                         LHS, RHS);
    3993           1 :     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1),
    3994           1 :                               DAG.getConstant(0, dl, MVT::i32));
    3995             :     Value = Value.getValue(0); // We only want the low 32 bits for the result.
    3996           1 :     break;
    3997             :   case ISD::SMULO:
    3998             :     // We generate a SMUL_LOHI and then check if all the bits of the high word
    3999             :     // are the same as the sign bit of the low word.
    4000           1 :     ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32);
    4001           1 :     Value = DAG.getNode(ISD::SMUL_LOHI, dl,
    4002             :                         DAG.getVTList(Op.getValueType(), Op.getValueType()),
    4003           1 :                         LHS, RHS);
    4004           1 :     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1),
    4005             :                               DAG.getNode(ISD::SRA, dl, Op.getValueType(),
    4006             :                                           Value.getValue(0),
    4007           1 :                                           DAG.getConstant(31, dl, MVT::i32)));
    4008             :     Value = Value.getValue(0); // We only want the low 32 bits for the result.
    4009           1 :     break;
    4010             :   } // switch (...)
    4011             : 
    4012          36 :   return std::make_pair(Value, OverflowCmp);
    4013             : }
    4014             : 
    4015             : SDValue
    4016          18 : ARMTargetLowering::LowerSignedALUO(SDValue Op, SelectionDAG &DAG) const {
    4017             :   // Let legalize expand this if it isn't a legal type yet.
    4018          18 :   if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
    4019           0 :     return SDValue();
    4020             : 
    4021             :   SDValue Value, OverflowCmp;
    4022          18 :   SDValue ARMcc;
    4023          18 :   std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
    4024          18 :   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4025             :   SDLoc dl(Op);
    4026             :   // We use 0 and 1 as false and true values.
    4027          18 :   SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
    4028          18 :   SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
    4029          18 :   EVT VT = Op.getValueType();
    4030             : 
    4031             :   SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
    4032          18 :                                  ARMcc, CCR, OverflowCmp);
    4033             : 
    4034          18 :   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
    4035          18 :   return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
    4036             : }
    4037             : 
    4038         652 : static SDValue ConvertBooleanCarryToCarryFlag(SDValue BoolCarry,
    4039             :                                               SelectionDAG &DAG) {
    4040             :   SDLoc DL(BoolCarry);
    4041         652 :   EVT CarryVT = BoolCarry.getValueType();
    4042             : 
    4043             :   // This converts the boolean value carry into the carry flag by doing
    4044             :   // ARMISD::SUBC Carry, 1
    4045             :   SDValue Carry = DAG.getNode(ARMISD::SUBC, DL,
    4046             :                               DAG.getVTList(CarryVT, MVT::i32),
    4047         652 :                               BoolCarry, DAG.getConstant(1, DL, CarryVT));
    4048         652 :   return Carry.getValue(1);
    4049             : }
    4050             : 
    4051        1218 : static SDValue ConvertCarryFlagToBooleanCarry(SDValue Flags, EVT VT,
    4052             :                                               SelectionDAG &DAG) {
    4053             :   SDLoc DL(Flags);
    4054             : 
    4055             :   // Now convert the carry flag into a boolean carry. We do this
    4056             :   // using ARMISD:ADDE 0, 0, Carry
    4057             :   return DAG.getNode(ARMISD::ADDE, DL, DAG.getVTList(VT, MVT::i32),
    4058             :                      DAG.getConstant(0, DL, MVT::i32),
    4059        1218 :                      DAG.getConstant(0, DL, MVT::i32), Flags);
    4060             : }
    4061             : 
    4062         653 : SDValue ARMTargetLowering::LowerUnsignedALUO(SDValue Op,
    4063             :                                              SelectionDAG &DAG) const {
    4064             :   // Let legalize expand this if it isn't a legal type yet.
    4065         653 :   if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
    4066           0 :     return SDValue();
    4067             : 
    4068         653 :   SDValue LHS = Op.getOperand(0);
    4069         653 :   SDValue RHS = Op.getOperand(1);
    4070             :   SDLoc dl(Op);
    4071             : 
    4072         653 :   EVT VT = Op.getValueType();
    4073         653 :   SDVTList VTs = DAG.getVTList(VT, MVT::i32);
    4074         653 :   SDValue Value;
    4075         653 :   SDValue Overflow;
    4076         653 :   switch (Op.getOpcode()) {
    4077           0 :   default:
    4078           0 :     llvm_unreachable("Unknown overflow instruction!");
    4079         452 :   case ISD::UADDO:
    4080         452 :     Value = DAG.getNode(ARMISD::ADDC, dl, VTs, LHS, RHS);
    4081             :     // Convert the carry flag into a boolean value.
    4082         452 :     Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG);
    4083         452 :     break;
    4084         201 :   case ISD::USUBO: {
    4085         201 :     Value = DAG.getNode(ARMISD::SUBC, dl, VTs, LHS, RHS);
    4086             :     // Convert the carry flag into a boolean value.
    4087         201 :     Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG);
    4088             :     // ARMISD::SUBC returns 0 when we have to borrow, so make it an overflow
    4089             :     // value. So compute 1 - C.
    4090         201 :     Overflow = DAG.getNode(ISD::SUB, dl, MVT::i32,
    4091         201 :                            DAG.getConstant(1, dl, MVT::i32), Overflow);
    4092         201 :     break;
    4093             :   }
    4094             :   }
    4095             : 
    4096         653 :   return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
    4097             : }
    4098             : 
    4099         140 : SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
    4100         140 :   SDValue Cond = Op.getOperand(0);
    4101         140 :   SDValue SelectTrue = Op.getOperand(1);
    4102         140 :   SDValue SelectFalse = Op.getOperand(2);
    4103             :   SDLoc dl(Op);
    4104             :   unsigned Opc = Cond.getOpcode();
    4105             : 
    4106         140 :   if (Cond.getResNo() == 1 &&
    4107           1 :       (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
    4108             :        Opc == ISD::USUBO)) {
    4109           0 :     if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
    4110           0 :       return SDValue();
    4111             : 
    4112             :     SDValue Value, OverflowCmp;
    4113           0 :     SDValue ARMcc;
    4114           0 :     std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
    4115           0 :     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4116           0 :     EVT VT = Op.getValueType();
    4117             : 
    4118             :     return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
    4119           0 :                    OverflowCmp, DAG);
    4120             :   }
    4121             : 
    4122             :   // Convert:
    4123             :   //
    4124             :   //   (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
    4125             :   //   (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
    4126             :   //
    4127         140 :   if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
    4128             :     const ConstantSDNode *CMOVTrue =
    4129             :       dyn_cast<ConstantSDNode>(Cond.getOperand(0));
    4130             :     const ConstantSDNode *CMOVFalse =
    4131             :       dyn_cast<ConstantSDNode>(Cond.getOperand(1));
    4132             : 
    4133           0 :     if (CMOVTrue && CMOVFalse) {
    4134           0 :       unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
    4135           0 :       unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
    4136             : 
    4137           0 :       SDValue True;
    4138           0 :       SDValue False;
    4139           0 :       if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
    4140           0 :         True = SelectTrue;
    4141           0 :         False = SelectFalse;
    4142           0 :       } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
    4143           0 :         True = SelectFalse;
    4144           0 :         False = SelectTrue;
    4145             :       }
    4146             : 
    4147           0 :       if (True.getNode() && False.getNode()) {
    4148           0 :         EVT VT = Op.getValueType();
    4149           0 :         SDValue ARMcc = Cond.getOperand(2);
    4150           0 :         SDValue CCR = Cond.getOperand(3);
    4151           0 :         SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
    4152             :         assert(True.getValueType() == VT);
    4153           0 :         return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
    4154             :       }
    4155             :     }
    4156             :   }
    4157             : 
    4158             :   // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
    4159             :   // undefined bits before doing a full-word comparison with zero.
    4160         140 :   Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
    4161         140 :                      DAG.getConstant(1, dl, Cond.getValueType()));
    4162             : 
    4163             :   return DAG.getSelectCC(dl, Cond,
    4164             :                          DAG.getConstant(0, dl, Cond.getValueType()),
    4165         140 :                          SelectTrue, SelectFalse, ISD::SETNE);
    4166             : }
    4167             : 
    4168         156 : static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
    4169             :                                  bool &swpCmpOps, bool &swpVselOps) {
    4170             :   // Start by selecting the GE condition code for opcodes that return true for
    4171             :   // 'equality'
    4172         156 :   if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
    4173         125 :       CC == ISD::SETULE)
    4174          60 :     CondCode = ARMCC::GE;
    4175             : 
    4176             :   // and GT for opcodes that return false for 'equality'.
    4177          96 :   else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
    4178          64 :            CC == ISD::SETULT)
    4179          72 :     CondCode = ARMCC::GT;
    4180             : 
    4181             :   // Since we are constrained to GE/GT, if the opcode contains 'less', we need
    4182             :   // to swap the compare operands.
    4183         156 :   if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
    4184         127 :       CC == ISD::SETULT)
    4185          69 :     swpCmpOps = true;
    4186             : 
    4187             :   // Both GT and GE are ordered comparisons, and return false for 'unordered'.
    4188             :   // If we have an unordered opcode, we need to swap the operands to the VSEL
    4189             :   // instruction (effectively negating the condition).
    4190             :   //
    4191             :   // This also has the effect of swapping which one of 'less' or 'greater'
    4192             :   // returns true, so we also swap the compare operands. It also switches
    4193             :   // whether we return true for 'equality', so we compensate by picking the
    4194             :   // opposite condition code to our original choice.
    4195         156 :   if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
    4196             :       CC == ISD::SETUGT) {
    4197          62 :     swpCmpOps = !swpCmpOps;
    4198          62 :     swpVselOps = !swpVselOps;
    4199          92 :     CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
    4200             :   }
    4201             : 
    4202             :   // 'ordered' is 'anything but unordered', so use the VS condition code and
    4203             :   // swap the VSEL operands.
    4204         156 :   if (CC == ISD::SETO) {
    4205           2 :     CondCode = ARMCC::VS;
    4206           2 :     swpVselOps = true;
    4207             :   }
    4208             : 
    4209             :   // 'unordered or not equal' is 'anything but equal', so use the EQ condition
    4210             :   // code and swap the VSEL operands.
    4211         156 :   if (CC == ISD::SETUNE) {
    4212           2 :     CondCode = ARMCC::EQ;
    4213           2 :     swpVselOps = true;
    4214             :   }
    4215         156 : }
    4216             : 
    4217        1854 : SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal,
    4218             :                                    SDValue TrueVal, SDValue ARMcc, SDValue CCR,
    4219             :                                    SDValue Cmp, SelectionDAG &DAG) const {
    4220        1854 :   if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
    4221           2 :     FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
    4222           2 :                            DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
    4223           2 :     TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
    4224           2 :                           DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
    4225             : 
    4226           2 :     SDValue TrueLow = TrueVal.getValue(0);
    4227           2 :     SDValue TrueHigh = TrueVal.getValue(1);
    4228           2 :     SDValue FalseLow = FalseVal.getValue(0);
    4229           2 :     SDValue FalseHigh = FalseVal.getValue(1);
    4230             : 
    4231             :     SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
    4232           2 :                               ARMcc, CCR, Cmp);
    4233             :     SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
    4234           2 :                                ARMcc, CCR, duplicateCmp(Cmp, DAG));
    4235             : 
    4236           2 :     return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
    4237             :   } else {
    4238             :     return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
    4239        1852 :                        Cmp);
    4240             :   }
    4241             : }
    4242             : 
    4243             : static bool isGTorGE(ISD::CondCode CC) {
    4244           0 :   return CC == ISD::SETGT || CC == ISD::SETGE;
    4245             : }
    4246             : 
    4247             : static bool isLTorLE(ISD::CondCode CC) {
    4248           0 :   return CC == ISD::SETLT || CC == ISD::SETLE;
    4249             : }
    4250             : 
    4251             : // See if a conditional (LHS CC RHS ? TrueVal : FalseVal) is lower-saturating.
    4252             : // All of these conditions (and their <= and >= counterparts) will do:
    4253             : //          x < k ? k : x
    4254             : //          x > k ? x : k
    4255             : //          k < x ? x : k
    4256             : //          k > x ? k : x
    4257           0 : static bool isLowerSaturate(const SDValue LHS, const SDValue RHS,
    4258             :                             const SDValue TrueVal, const SDValue FalseVal,
    4259             :                             const ISD::CondCode CC, const SDValue K) {
    4260             :   return (isGTorGE(CC) &&
    4261           0 :           ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))) ||
    4262             :          (isLTorLE(CC) &&
    4263           0 :           ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal)));
    4264             : }
    4265             : 
    4266             : // Similar to isLowerSaturate(), but checks for upper-saturating conditions.
    4267           0 : static bool isUpperSaturate(const SDValue LHS, const SDValue RHS,
    4268             :                             const SDValue TrueVal, const SDValue FalseVal,
    4269             :                             const ISD::CondCode CC, const SDValue K) {
    4270             :   return (isGTorGE(CC) &&
    4271           0 :           ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))) ||
    4272             :          (isLTorLE(CC) &&
    4273           0 :           ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal)));
    4274             : }
    4275             : 
    4276             : // Check if two chained conditionals could be converted into SSAT or USAT.
    4277             : //
    4278             : // SSAT can replace a set of two conditional selectors that bound a number to an
    4279             : // interval of type [k, ~k] when k + 1 is a power of 2. Here are some examples:
    4280             : //
    4281             : //     x < -k ? -k : (x > k ? k : x)
    4282             : //     x < -k ? -k : (x < k ? x : k)
    4283             : //     x > -k ? (x > k ? k : x) : -k
    4284             : //     x < k ? (x < -k ? -k : x) : k
    4285             : //     etc.
    4286             : //
    4287             : // USAT works similarily to SSAT but bounds on the interval [0, k] where k + 1 is
    4288             : // a power of 2.
    4289             : //
    4290             : // It returns true if the conversion can be done, false otherwise.
    4291             : // Additionally, the variable is returned in parameter V, the constant in K and
    4292             : // usat is set to true if the conditional represents an unsigned saturation
    4293        1287 : static bool isSaturatingConditional(const SDValue &Op, SDValue &V,
    4294             :                                     uint64_t &K, bool &usat) {
    4295        1287 :   SDValue LHS1 = Op.getOperand(0);
    4296        1287 :   SDValue RHS1 = Op.getOperand(1);
    4297        1287 :   SDValue TrueVal1 = Op.getOperand(2);
    4298        1287 :   SDValue FalseVal1 = Op.getOperand(3);
    4299        1287 :   ISD::CondCode CC1 = cast<CondCodeSDNode>(Op.getOperand(4))->get();
    4300             : 
    4301        1287 :   const SDValue Op2 = isa<ConstantSDNode>(TrueVal1) ? FalseVal1 : TrueVal1;
    4302        2574 :   if (Op2.getOpcode() != ISD::SELECT_CC)
    4303             :     return false;
    4304             : 
    4305          55 :   SDValue LHS2 = Op2.getOperand(0);
    4306          55 :   SDValue RHS2 = Op2.getOperand(1);
    4307          55 :   SDValue TrueVal2 = Op2.getOperand(2);
    4308          55 :   SDValue FalseVal2 = Op2.getOperand(3);
    4309          55 :   ISD::CondCode CC2 = cast<CondCodeSDNode>(Op2.getOperand(4))->get();
    4310             : 
    4311             :   // Find out which are the constants and which are the variables
    4312             :   // in each conditional
    4313             :   SDValue *K1 = isa<ConstantSDNode>(LHS1) ? &LHS1 : isa<ConstantSDNode>(RHS1)
    4314             :                                                         ? &RHS1
    4315             :                                                         : nullptr;
    4316             :   SDValue *K2 = isa<ConstantSDNode>(LHS2) ? &LHS2 : isa<ConstantSDNode>(RHS2)
    4317             :                                                         ? &RHS2
    4318             :                                                         : nullptr;
    4319          55 :   SDValue K2Tmp = isa<ConstantSDNode>(TrueVal2) ? TrueVal2 : FalseVal2;
    4320          55 :   SDValue V1Tmp = (K1 && *K1 == LHS1) ? RHS1 : LHS1;
    4321          55 :   SDValue V2Tmp = (K2 && *K2 == LHS2) ? RHS2 : LHS2;
    4322          55 :   SDValue V2 = (K2Tmp == TrueVal2) ? FalseVal2 : TrueVal2;
    4323             : 
    4324             :   // We must detect cases where the original operations worked with 16- or
    4325             :   // 8-bit values. In such case, V2Tmp != V2 because the comparison operations
    4326             :   // must work with sign-extended values but the select operations return
    4327             :   // the original non-extended value.
    4328             :   SDValue V2TmpReg = V2Tmp;
    4329          55 :   if (V2Tmp->getOpcode() == ISD::SIGN_EXTEND_INREG)
    4330           9 :     V2TmpReg = V2Tmp->getOperand(0);
    4331             : 
    4332             :   // Check that the registers and the constants have the correct values
    4333             :   // in both conditionals
    4334          55 :   if (!K1 || !K2 || *K1 == Op2 || *K2 != K2Tmp || V1Tmp != V2Tmp ||
    4335             :       V2TmpReg != V2)
    4336             :     return false;
    4337             : 
    4338             :   // Figure out which conditional is saturating the lower/upper bound.
    4339             :   const SDValue *LowerCheckOp =
    4340          34 :       isLowerSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
    4341          34 :           ? &Op
    4342          16 :           : isLowerSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2)
    4343          16 :                 ? &Op2
    4344             :                 : nullptr;
    4345             :   const SDValue *UpperCheckOp =
    4346          34 :       isUpperSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
    4347          34 :           ? &Op
    4348          18 :           : isUpperSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2)
    4349          18 :                 ? &Op2
    4350             :                 : nullptr;
    4351             : 
    4352          34 :   if (!UpperCheckOp || !LowerCheckOp || LowerCheckOp == UpperCheckOp)
    4353             :     return false;
    4354             : 
    4355             :   // Check that the constant in the lower-bound check is
    4356             :   // the opposite of the constant in the upper-bound check
    4357             :   // in 1's complement.
    4358          28 :   int64_t Val1 = cast<ConstantSDNode>(*K1)->getSExtValue();
    4359          56 :   int64_t Val2 = cast<ConstantSDNode>(*K2)->getSExtValue();
    4360          28 :   int64_t PosVal = std::max(Val1, Val2);
    4361          28 :   int64_t NegVal = std::min(Val1, Val2);
    4362             : 
    4363          28 :   if (((Val1 > Val2 && UpperCheckOp == &Op) ||
    4364          43 :        (Val1 < Val2 && UpperCheckOp == &Op2)) &&
    4365          28 :       isPowerOf2_64(PosVal + 1)) {
    4366             : 
    4367             :     // Handle the difference between USAT (unsigned) and SSAT (signed) saturation
    4368          28 :     if (Val1 == ~Val2)
    4369           9 :       usat = false;
    4370          19 :     else if (NegVal == 0)
    4371          16 :       usat = true;
    4372             :     else
    4373             :       return false;
    4374             : 
    4375          25 :     V = V2;
    4376          25 :     K = (uint64_t)PosVal; // At this point, PosVal is guaranteed to be positive
    4377             : 
    4378          25 :     return true;
    4379             :   }
    4380             : 
    4381             :   return false;
    4382             : }
    4383             : 
    4384             : // Check if a condition of the type x < k ? k : x can be converted into a
    4385             : // bit operation instead of conditional moves.
    4386             : // Currently this is allowed given:
    4387             : // - The conditions and values match up
    4388             : // - k is 0 or -1 (all ones)
    4389             : // This function will not check the last condition, thats up to the caller
    4390             : // It returns true if the transformation can be made, and in such case
    4391             : // returns x in V, and k in SatK.
    4392        1546 : static bool isLowerSaturatingConditional(const SDValue &Op, SDValue &V,
    4393             :                                          SDValue &SatK)
    4394             : {
    4395        1546 :   SDValue LHS = Op.getOperand(0);
    4396        1546 :   SDValue RHS = Op.getOperand(1);
    4397        1546 :   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
    4398        1546 :   SDValue TrueVal = Op.getOperand(2);
    4399        1546 :   SDValue FalseVal = Op.getOperand(3);
    4400             : 
    4401             :   SDValue *K = isa<ConstantSDNode>(LHS) ? &LHS : isa<ConstantSDNode>(RHS)
    4402             :                                                ? &RHS
    4403             :                                                : nullptr;
    4404             : 
    4405             :   // No constant operation in comparison, early out
    4406             :   if (!K)
    4407             :     return false;
    4408             : 
    4409         910 :   SDValue KTmp = isa<ConstantSDNode>(TrueVal) ? TrueVal : FalseVal;
    4410         910 :   V = (KTmp == TrueVal) ? FalseVal : TrueVal;
    4411         910 :   SDValue VTmp = (K && *K == LHS) ? RHS : LHS;
    4412             : 
    4413             :   // If the constant on left and right side, or variable on left and right,
    4414             :   // does not match, early out
    4415             :   if (*K != KTmp || V != VTmp)
    4416             :     return false;
    4417             : 
    4418          76 :   if (isLowerSaturate(LHS, RHS, TrueVal, FalseVal, CC, *K)) {
    4419          38 :     SatK = *K;
    4420          38 :     return true;
    4421             :   }
    4422             : 
    4423             :   return false;
    4424             : }
    4425             : 
    4426        1873 : SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
    4427        3746 :   EVT VT = Op.getValueType();
    4428             :   SDLoc dl(Op);
    4429             : 
    4430             :   // Try to convert two saturating conditional selects into a single SSAT
    4431        1873 :   SDValue SatValue;
    4432             :   uint64_t SatConstant;
    4433             :   bool SatUSat;
    4434        3160 :   if (((!Subtarget->isThumb() && Subtarget->hasV6Ops()) || Subtarget->isThumb2()) &&
    4435        1287 :       isSaturatingConditional(Op, SatValue, SatConstant, SatUSat)) {
    4436          25 :     if (SatUSat)
    4437             :       return DAG.getNode(ARMISD::USAT, dl, VT, SatValue,
    4438          32 :                          DAG.getConstant(countTrailingOnes(SatConstant), dl, VT));
    4439             :     else
    4440             :       return DAG.getNode(ARMISD::SSAT, dl, VT, SatValue,
    4441          18 :                          DAG.getConstant(countTrailingOnes(SatConstant), dl, VT));
    4442             :   }
    4443             : 
    4444             :   // Try to convert expressions of the form x < k ? k : x (and similar forms)
    4445             :   // into more efficient bit operations, which is possible when k is 0 or -1
    4446             :   // On ARM and Thumb-2 which have flexible operand 2 this will result in
    4447             :   // single instructions. On Thumb the shift and the bit operation will be two
    4448             :   // instructions.
    4449             :   // Only allow this transformation on full-width (32-bit) operations
    4450        1848 :   SDValue LowerSatConstant;
    4451        1546 :   if (VT == MVT::i32 &&
    4452        1546 :       isLowerSaturatingConditional(Op, SatValue, LowerSatConstant)) {
    4453             :     SDValue ShiftV = DAG.getNode(ISD::SRA, dl, VT, SatValue,
    4454          38 :                                  DAG.getConstant(31, dl, VT));
    4455          38 :     if (isNullConstant(LowerSatConstant)) {
    4456             :       SDValue NotShiftV = DAG.getNode(ISD::XOR, dl, VT, ShiftV,
    4457          17 :                                       DAG.getAllOnesConstant(dl, VT));
    4458          17 :       return DAG.getNode(ISD::AND, dl, VT, SatValue, NotShiftV);
    4459          21 :     } else if (isAllOnesConstant(LowerSatConstant))
    4460           6 :       return DAG.getNode(ISD::OR, dl, VT, SatValue, ShiftV);
    4461             :   }
    4462             : 
    4463        1825 :   SDValue LHS = Op.getOperand(0);
    4464        1825 :   SDValue RHS = Op.getOperand(1);
    4465        1825 :   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
    4466        1825 :   SDValue TrueVal = Op.getOperand(2);
    4467        1825 :   SDValue FalseVal = Op.getOperand(3);
    4468             : 
    4469        1825 :   if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
    4470          56 :     DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
    4471             :                                                     dl);
    4472             : 
    4473             :     // If softenSetCCOperands only returned one value, we should compare it to
    4474             :     // zero.
    4475          28 :     if (!RHS.getNode()) {
    4476           4 :       RHS = DAG.getConstant(0, dl, LHS.getValueType());
    4477           2 :       CC = ISD::SETNE;
    4478             :     }
    4479             :   }
    4480             : 
    4481        1825 :   if (LHS.getValueType() == MVT::i32) {
    4482             :     // Try to generate VSEL on ARMv8.
    4483             :     // The VSEL instruction can't use all the usual ARM condition
    4484             :     // codes: it only has two bits to select the condition code, so it's
    4485             :     // constrained to use only GE, GT, VS and EQ.
    4486             :     //
    4487             :     // To implement all the various ISD::SETXXX opcodes, we sometimes need to
    4488             :     // swap the operands of the previous compare instruction (effectively
    4489             :     // inverting the compare condition, swapping 'less' and 'greater') and
    4490             :     // sometimes need to swap the operands to the VSEL (which inverts the
    4491             :     // condition in the sense of firing whenever the previous condition didn't)
    4492        1306 :     if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
    4493             :                                     TrueVal.getValueType() == MVT::f64)) {
    4494          16 :       ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
    4495          16 :       if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
    4496          12 :           CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
    4497           6 :         CC = ISD::getSetCCInverse(CC, true);
    4498             :         std::swap(TrueVal, FalseVal);
    4499             :       }
    4500             :     }
    4501             : 
    4502        1306 :     SDValue ARMcc;
    4503        1306 :     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4504        1306 :     SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
    4505        1306 :     return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
    4506             :   }
    4507             : 
    4508             :   ARMCC::CondCodes CondCode, CondCode2;
    4509             :   bool InvalidOnQNaN;
    4510         519 :   FPCCToARMCC(CC, CondCode, CondCode2, InvalidOnQNaN);
    4511             : 
    4512             :   // Normalize the fp compare. If RHS is zero we keep it there so we match
    4513             :   // CMPFPw0 instead of CMPFP.
    4514         519 :   if (Subtarget->hasFPARMv8() && !isFloatingPointZero(RHS) &&
    4515             :      (TrueVal.getValueType() == MVT::f16 ||
    4516             :       TrueVal.getValueType() == MVT::f32 ||
    4517             :       TrueVal.getValueType() == MVT::f64)) {
    4518         156 :     bool swpCmpOps = false;
    4519         156 :     bool swpVselOps = false;
    4520         156 :     checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
    4521             : 
    4522         156 :     if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
    4523          20 :         CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
    4524         156 :       if (swpCmpOps)
    4525             :         std::swap(LHS, RHS);
    4526         156 :       if (swpVselOps)
    4527             :         std::swap(TrueVal, FalseVal);
    4528             :     }
    4529             :   }
    4530             : 
    4531         519 :   SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
    4532         519 :   SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN);
    4533         519 :   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4534         519 :   SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
    4535         519 :   if (CondCode2 != ARMCC::AL) {
    4536          29 :     SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
    4537             :     // FIXME: Needs another CMP because flag can have but one use.
    4538          29 :     SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN);
    4539          29 :     Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
    4540             :   }
    4541         519 :   return Result;
    4542             : }
    4543             : 
    4544             : /// canChangeToInt - Given the fp compare operand, return true if it is suitable
    4545             : /// to morph to an integer compare sequence.
    4546           0 : static bool canChangeToInt(SDValue Op, bool &SeenZero,
    4547             :                            const ARMSubtarget *Subtarget) {
    4548             :   SDNode *N = Op.getNode();
    4549             :   if (!N->hasOneUse())
    4550             :     // Otherwise it requires moving the value from fp to integer registers.
    4551           0 :     return false;
    4552           0 :   if (!N->getNumValues())
    4553           0 :     return false;
    4554           0 :   EVT VT = Op.getValueType();
    4555           0 :   if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
    4556             :     // f32 case is generally profitable. f64 case only makes sense when vcmpe +
    4557             :     // vmrs are very slow, e.g. cortex-a8.
    4558           0 :     return false;
    4559             : 
    4560           0 :   if (isFloatingPointZero(Op)) {
    4561           0 :     SeenZero = true;
    4562           0 :     return true;
    4563             :   }
    4564             :   return ISD::isNormalLoad(N);
    4565             : }
    4566             : 
    4567           0 : static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
    4568           0 :   if (isFloatingPointZero(Op))
    4569           0 :     return DAG.getConstant(0, SDLoc(Op), MVT::i32);
    4570             : 
    4571             :   if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
    4572           0 :     return DAG.getLoad(MVT::i32, SDLoc(Op), Ld->getChain(), Ld->getBasePtr(),
    4573           0 :                        Ld->getPointerInfo(), Ld->getAlignment(),
    4574           0 :                        Ld->getMemOperand()->getFlags());
    4575             : 
    4576           0 :   llvm_unreachable("Unknown VFP cmp argument!");
    4577             : }
    4578             : 
    4579           0 : static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
    4580             :                            SDValue &RetVal1, SDValue &RetVal2) {
    4581           0 :   SDLoc dl(Op);
    4582             : 
    4583           0 :   if (isFloatingPointZero(Op)) {
    4584           0 :     RetVal1 = DAG.getConstant(0, dl, MVT::i32);
    4585           0 :     RetVal2 = DAG.getConstant(0, dl, MVT::i32);
    4586           0 :     return;
    4587             :   }
    4588             : 
    4589             :   if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
    4590           0 :     SDValue Ptr = Ld->getBasePtr();
    4591           0 :     RetVal1 =
    4592           0 :         DAG.getLoad(MVT::i32, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(),
    4593           0 :                     Ld->getAlignment(), Ld->getMemOperand()->getFlags());
    4594             : 
    4595           0 :     EVT PtrType = Ptr.getValueType();
    4596           0 :     unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
    4597             :     SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
    4598           0 :                                  PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
    4599           0 :     RetVal2 = DAG.getLoad(MVT::i32, dl, Ld->getChain(), NewPtr,
    4600             :                           Ld->getPointerInfo().getWithOffset(4), NewAlign,
    4601           0 :                           Ld->getMemOperand()->getFlags());
    4602           0 :     return;
    4603             :   }
    4604             : 
    4605           0 :   llvm_unreachable("Unknown VFP cmp argument!");
    4606             : }
    4607             : 
    4608             : /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
    4609             : /// f32 and even f64 comparisons to integer ones.
    4610             : SDValue
    4611           3 : ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
    4612           3 :   SDValue Chain = Op.getOperand(0);
    4613           3 :   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
    4614           3 :   SDValue LHS = Op.getOperand(2);
    4615           3 :   SDValue RHS = Op.getOperand(3);
    4616           3 :   SDValue Dest = Op.getOperand(4);
    4617             :   SDLoc dl(Op);
    4618             : 
    4619           3 :   bool LHSSeenZero = false;
    4620           3 :   bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
    4621           3 :   bool RHSSeenZero = false;
    4622           3 :   bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
    4623           3 :   if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
    4624             :     // If unsafe fp math optimization is enabled and there are no other uses of
    4625             :     // the CMP operands, and the condition code is EQ or NE, we can optimize it
    4626             :     // to an integer comparison.
    4627           2 :     if (CC == ISD::SETOEQ)
    4628             :       CC = ISD::SETEQ;
    4629           2 :     else if (CC == ISD::SETUNE)
    4630             :       CC = ISD::SETNE;
    4631             : 
    4632           2 :     SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
    4633           2 :     SDValue ARMcc;
    4634             :     if (LHS.getValueType() == MVT::f32) {
    4635           1 :       LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
    4636           1 :                         bitcastf32Toi32(LHS, DAG), Mask);
    4637           1 :       RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
    4638           1 :                         bitcastf32Toi32(RHS, DAG), Mask);
    4639           1 :       SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
    4640           1 :       SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4641             :       return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
    4642           1 :                          Chain, Dest, ARMcc, CCR, Cmp);
    4643             :     }
    4644             : 
    4645           1 :     SDValue LHS1, LHS2;
    4646           1 :     SDValue RHS1, RHS2;
    4647           1 :     expandf64Toi32(LHS, DAG, LHS1, LHS2);
    4648           1 :     expandf64Toi32(RHS, DAG, RHS1, RHS2);
    4649           1 :     LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
    4650           1 :     RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
    4651           1 :     ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
    4652           1 :     ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
    4653           1 :     SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
    4654           1 :     SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
    4655           1 :     return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
    4656             :   }
    4657             : 
    4658           1 :   return SDValue();
    4659             : }
    4660             : 
    4661         281 : SDValue ARMTargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
    4662         281 :   SDValue Chain = Op.getOperand(0);
    4663         281 :   SDValue Cond = Op.getOperand(1);
    4664         281 :   SDValue Dest = Op.getOperand(2);
    4665             :   SDLoc dl(Op);
    4666             : 
    4667             :   // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch
    4668             :   // instruction.
    4669             :   unsigned Opc = Cond.getOpcode();
    4670         281 :   bool OptimizeMul = (Opc == ISD::SMULO || Opc == ISD::UMULO) &&
    4671           0 :                       !Subtarget->isThumb1Only();
    4672         281 :   if (Cond.getResNo() == 1 &&
    4673           6 :       (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
    4674           0 :        Opc == ISD::USUBO || OptimizeMul)) {
    4675             :     // Only lower legal XALUO ops.
    4676           6 :     if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
    4677           0 :       return SDValue();
    4678             : 
    4679             :     // The actual operation with overflow check.
    4680             :     SDValue Value, OverflowCmp;
    4681           6 :     SDValue ARMcc;
    4682           6 :     std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
    4683             : 
    4684             :     // Reverse the condition code.
    4685             :     ARMCC::CondCodes CondCode =
    4686          12 :         (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue();
    4687             :     CondCode = ARMCC::getOppositeCondition(CondCode);
    4688           6 :     ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32);
    4689           6 :     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4690             : 
    4691             :     return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR,
    4692           6 :                        OverflowCmp);
    4693             :   }
    4694             : 
    4695         275 :   return SDValue();
    4696             : }
    4697             : 
    4698        1955 : SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
    4699        1955 :   SDValue Chain = Op.getOperand(0);
    4700        1955 :   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
    4701        1955 :   SDValue LHS = Op.getOperand(2);
    4702        1955 :   SDValue RHS = Op.getOperand(3);
    4703        1955 :   SDValue Dest = Op.getOperand(4);
    4704             :   SDLoc dl(Op);
    4705             : 
    4706        1955 :   if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
    4707           0 :     DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
    4708             :                                                     dl);
    4709             : 
    4710             :     // If softenSetCCOperands only returned one value, we should compare it to
    4711             :     // zero.
    4712           0 :     if (!RHS.getNode()) {
    4713           0 :       RHS = DAG.getConstant(0, dl, LHS.getValueType());
    4714           0 :       CC = ISD::SETNE;
    4715             :     }
    4716             :   }
    4717             : 
    4718             :   // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch
    4719             :   // instruction.
    4720        1955 :   unsigned Opc = LHS.getOpcode();
    4721        1955 :   bool OptimizeMul = (Opc == ISD::SMULO || Opc == ISD::UMULO) &&
    4722           4 :                       !Subtarget->isThumb1Only();
    4723          14 :   if (LHS.getResNo() == 1 && (isOneConstant(RHS) || isNullConstant(RHS)) &&
    4724          14 :       (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
    4725        1959 :        Opc == ISD::USUBO || OptimizeMul) &&
    4726          12 :       (CC == ISD::SETEQ || CC == ISD::SETNE)) {
    4727             :     // Only lower legal XALUO ops.
    4728          12 :     if (!DAG.getTargetLoweringInfo().isTypeLegal(LHS->getValueType(0)))
    4729           0 :       return SDValue();
    4730             : 
    4731             :     // The actual operation with overflow check.
    4732             :     SDValue Value, OverflowCmp;
    4733          12 :     SDValue ARMcc;
    4734          12 :     std::tie(Value, OverflowCmp) = getARMXALUOOp(LHS.getValue(0), DAG, ARMcc);
    4735             : 
    4736          12 :     if ((CC == ISD::SETNE) != isOneConstant(RHS)) {
    4737             :       // Reverse the condition code.
    4738             :       ARMCC::CondCodes CondCode =
    4739           0 :           (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue();
    4740             :       CondCode = ARMCC::getOppositeCondition(CondCode);
    4741           0 :       ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32);
    4742             :     }
    4743          12 :     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4744             : 
    4745             :     return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR,
    4746          12 :                        OverflowCmp);
    4747             :   }
    4748             : 
    4749        1943 :   if (LHS.getValueType() == MVT::i32) {
    4750        1890 :     SDValue ARMcc;
    4751        1890 :     SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
    4752        1890 :     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4753             :     return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
    4754        1890 :                        Chain, Dest, ARMcc, CCR, Cmp);
    4755             :   }
    4756             : 
    4757         109 :   if (getTargetMachine().Options.UnsafeFPMath &&
    4758           3 :       (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
    4759          55 :        CC == ISD::SETNE || CC == ISD::SETUNE)) {
    4760           3 :     if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
    4761           2 :       return Result;
    4762             :   }
    4763             : 
    4764             :   ARMCC::CondCodes CondCode, CondCode2;
    4765             :   bool InvalidOnQNaN;
    4766          51 :   FPCCToARMCC(CC, CondCode, CondCode2, InvalidOnQNaN);
    4767             : 
    4768          51 :   SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
    4769          51 :   SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN);
    4770          51 :   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    4771          51 :   SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
    4772          51 :   SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
    4773          51 :   SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
    4774          51 :   if (CondCode2 != ARMCC::AL) {
    4775           2 :     ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
    4776           2 :     SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
    4777           2 :     Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
    4778             :   }
    4779          51 :   return Res;
    4780             : }
    4781             : 
    4782          38 : SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
    4783          38 :   SDValue Chain = Op.getOperand(0);
    4784          38 :   SDValue Table = Op.getOperand(1);
    4785          38 :   SDValue Index = Op.getOperand(2);
    4786             :   SDLoc dl(Op);
    4787             : 
    4788          38 :   EVT PTy = getPointerTy(DAG.getDataLayout());
    4789             :   JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
    4790          38 :   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
    4791          38 :   Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
    4792          38 :   Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
    4793          38 :   SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Index);
    4794          53 :   if (Subtarget->isThumb2() || (Subtarget->hasV8MBaselineOps() && Subtarget->isThumb())) {
    4795             :     // Thumb2 and ARMv8-M use a two-level jump. That is, it jumps into the jump table
    4796             :     // which does another jump to the destination. This also makes it easier
    4797             :     // to translate it to TBB / TBH later (Thumb2 only).
    4798             :     // FIXME: This might not work if the function is extremely large.
    4799             :     return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
    4800          19 :                        Addr, Op.getOperand(2), JTI);
    4801             :   }
    4802          19 :   if (isPositionIndependent() || Subtarget->isROPI()) {
    4803          11 :     Addr =
    4804          11 :         DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
    4805          11 :                     MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
    4806          11 :     Chain = Addr.getValue(1);
    4807          11 :     Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Addr);
    4808          11 :     return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
    4809             :   } else {
    4810           8 :     Addr =
    4811           8 :         DAG.getLoad(PTy, dl, Chain, Addr,
    4812           8 :                     MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
    4813           8 :     Chain = Addr.getValue(1);
    4814           8 :     return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
    4815             :   }
    4816             : }
    4817             : 
    4818          91 : static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
    4819          91 :   EVT VT = Op.getValueType();
    4820             :   SDLoc dl(Op);
    4821             : 
    4822         169 :   if (Op.getValueType().getVectorElementType() == MVT::i32) {
    4823         156 :     if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
    4824          72 :       return Op;
    4825           6 :     return DAG.UnrollVectorOp(Op.getNode());
    4826             :   }
    4827             : 
    4828             :   const bool HasFullFP16 =
    4829          13 :     static_cast<const ARMSubtarget&>(DAG.getSubtarget()).hasFullFP16();
    4830             : 
    4831          13 :   EVT NewTy;
    4832          13 :   const EVT OpTy = Op.getOperand(0).getValueType();
    4833             :   if (OpTy == MVT::v4f32)
    4834           1 :     NewTy = MVT::v4i32;
    4835           6 :   else if (OpTy == MVT::v4f16 && HasFullFP16)
    4836           6 :     NewTy = MVT::v4i16;
    4837           6 :   else if (OpTy == MVT::v8f16 && HasFullFP16)
    4838           6 :     NewTy = MVT::v8i16;
    4839             :   else
    4840           0 :     llvm_unreachable("Invalid type for custom lowering!");
    4841             : 
    4842             :   if (VT != MVT::v4i16 && VT != MVT::v8i16)
    4843           0 :     return DAG.UnrollVectorOp(Op.getNode());
    4844             : 
    4845          13 :   Op = DAG.getNode(Op.getOpcode(), dl, NewTy, Op.getOperand(0));
    4846          13 :   return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
    4847             : }
    4848             : 
    4849         117 : SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
    4850         117 :   EVT VT = Op.getValueType();
    4851         117 :   if (VT.isVector())
    4852          91 :     return LowerVectorFP_TO_INT(Op, DAG);
    4853          26 :   if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
    4854             :     RTLIB::Libcall LC;
    4855          10 :     if (Op.getOpcode() == ISD::FP_TO_SINT)
    4856           4 :       LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
    4857             :                               Op.getValueType());
    4858             :     else
    4859           6 :       LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
    4860             :                               Op.getValueType());
    4861          10 :     return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
    4862          10 :                        /*isSigned*/ false, SDLoc(Op)).first;
    4863             :   }
    4864             : 
    4865          16 :   return Op;
    4866             : }
    4867             : 
    4868         160 : static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
    4869         160 :   EVT VT = Op.getValueType();
    4870             :   SDLoc dl(Op);
    4871             : 
    4872         320 :   if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
    4873         141 :     if (VT.getVectorElementType() == MVT::f32)
    4874         129 :       return Op;
    4875          12 :     return DAG.UnrollVectorOp(Op.getNode());
    4876             :   }
    4877             : 
    4878             :   assert((Op.getOperand(0).getValueType() == MVT::v4i16 ||
    4879             :           Op.getOperand(0).getValueType() == MVT::v8i16) &&
    4880             :          "Invalid type for custom lowering!");
    4881             : 
    4882             :   const bool HasFullFP16 =
    4883          19 :     static_cast<const ARMSubtarget&>(DAG.getSubtarget()).hasFullFP16();
    4884             : 
    4885          19 :   EVT DestVecType;
    4886             :   if (VT == MVT::v4f32)
    4887           7 :     DestVecType = MVT::v4i32;
    4888           6 :   else if (VT == MVT::v4f16 && HasFullFP16)
    4889           6 :     DestVecType = MVT::v4i16;
    4890           6 :   else if (VT == MVT::v8f16 && HasFullFP16)
    4891           6 :     DestVecType = MVT::v8i16;
    4892             :   else
    4893           0 :     return DAG.UnrollVectorOp(Op.getNode());
    4894             : 
    4895             :   unsigned CastOpc;
    4896             :   unsigned Opc;
    4897          19 :   switch (Op.getOpcode()) {
    4898           0 :   default: llvm_unreachable("Invalid opcode!");
    4899             :   case ISD::SINT_TO_FP:
    4900             :     CastOpc = ISD::SIGN_EXTEND;
    4901             :     Opc = ISD::SINT_TO_FP;
    4902             :     break;
    4903          10 :   case ISD::UINT_TO_FP:
    4904             :     CastOpc = ISD::ZERO_EXTEND;
    4905             :     Opc = ISD::UINT_TO_FP;
    4906          10 :     break;
    4907             :   }
    4908             : 
    4909          19 :   Op = DAG.getNode(CastOpc, dl, DestVecType, Op.getOperand(0));
    4910          19 :   return DAG.getNode(Opc, dl, VT, Op);
    4911             : }
    4912             : 
    4913         186 : SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
    4914         186 :   EVT VT = Op.getValueType();
    4915         186 :   if (VT.isVector())
    4916         160 :     return LowerVectorINT_TO_FP(Op, DAG);
    4917          26 :   if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
    4918             :     RTLIB::Libcall LC;
    4919          10 :     if (Op.getOpcode() == ISD::SINT_TO_FP)
    4920          12 :       LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
    4921             :                               Op.getValueType());
    4922             :     else
    4923           8 :       LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
    4924             :                               Op.getValueType());
    4925          10 :     return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
    4926          10 :                        /*isSigned*/ false, SDLoc(Op)).first;
    4927             :   }
    4928             : 
    4929          16 :   return Op;
    4930             : }
    4931             : 
    4932          23 : SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
    4933             :   // Implement fcopysign with a fabs and a conditional fneg.
    4934          23 :   SDValue Tmp0 = Op.getOperand(0);
    4935          23 :   SDValue Tmp1 = Op.getOperand(1);
    4936             :   SDLoc dl(Op);
    4937             :   EVT VT = Op.getValueType();
    4938             :   EVT SrcVT = Tmp1.getValueType();
    4939          23 :   bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
    4940             :     Tmp0.getOpcode() == ARMISD::VMOVDRR;
    4941          21 :   bool UseNEON = !InGPR && Subtarget->hasNEON();
    4942             : 
    4943             :   if (UseNEON) {
    4944             :     // Use VBSL to copy the sign bit.
    4945             :     unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
    4946             :     SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
    4947          24 :                                DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
    4948             :     EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
    4949             :     if (VT == MVT::f64)
    4950           5 :       Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
    4951             :                          DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
    4952           5 :                          DAG.getConstant(32, dl, MVT::i32));
    4953             :     else /*if (VT == MVT::f32)*/
    4954           7 :       Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
    4955             :     if (SrcVT == MVT::f32) {
    4956           5 :       Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
    4957             :       if (VT == MVT::f64)
    4958           0 :         Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
    4959             :                            DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
    4960           0 :                            DAG.getConstant(32, dl, MVT::i32));
    4961             :     } else if (VT == MVT::f32)
    4962           2 :       Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
    4963             :                          DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
    4964           2 :                          DAG.getConstant(32, dl, MVT::i32));
    4965          12 :     Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
    4966          12 :     Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
    4967             : 
    4968             :     SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
    4969          12 :                                             dl, MVT::i32);
    4970          12 :     AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
    4971             :     SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
    4972          12 :                                   DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
    4973             : 
    4974             :     SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
    4975             :                               DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
    4976          12 :                               DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
    4977             :     if (VT == MVT::f32) {
    4978           7 :       Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
    4979           7 :       Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
    4980           7 :                         DAG.getConstant(0, dl, MVT::i32));
    4981             :     } else {
    4982           5 :       Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
    4983             :     }
    4984             : 
    4985          12 :     return Res;
    4986             :   }
    4987             : 
    4988             :   // Bitcast operand 1 to i32.
    4989             :   if (SrcVT == MVT::f64)
    4990           6 :     Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
    4991           6 :                        Tmp1).getValue(1);
    4992          11 :   Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
    4993             : 
    4994             :   // Or in the signbit with integer operations.
    4995          11 :   SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
    4996          11 :   SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
    4997          11 :   Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
    4998             :   if (VT == MVT::f32) {
    4999           5 :     Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
    5000           5 :                        DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
    5001             :     return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
    5002           5 :                        DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
    5003             :   }
    5004             : 
    5005             :   // f64: Or the high part with signbit and then combine two parts.
    5006           6 :   Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
    5007           6 :                      Tmp0);
    5008           6 :   SDValue Lo = Tmp0.getValue(0);
    5009           6 :   SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
    5010           6 :   Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
    5011           6 :   return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
    5012             : }
    5013             : 
    5014          12 : SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
    5015          12 :   MachineFunction &MF = DAG.getMachineFunction();
    5016          12 :   MachineFrameInfo &MFI = MF.getFrameInfo();
    5017             :   MFI.setReturnAddressIsTaken(true);
    5018             : 
    5019          12 :   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
    5020           0 :     return SDValue();
    5021             : 
    5022          12 :   EVT VT = Op.getValueType();
    5023             :   SDLoc dl(Op);
    5024          12 :   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
    5025          12 :   if (Depth) {
    5026           4 :     SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
    5027           4 :     SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
    5028             :     return DAG.getLoad(VT, dl, DAG.getEntryNode(),
    5029             :                        DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
    5030           4 :                        MachinePointerInfo());
    5031             :   }
    5032             : 
    5033             :   // Return LR, which contains the return address. Mark it an implicit live-in.
    5034          16 :   unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
    5035           8 :   return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
    5036             : }
    5037             : 
    5038          48 : SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
    5039          48 :   const ARMBaseRegisterInfo &ARI =
    5040             :     *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
    5041          48 :   MachineFunction &MF = DAG.getMachineFunction();
    5042          48 :   MachineFrameInfo &MFI = MF.getFrameInfo();
    5043             :   MFI.setFrameAddressIsTaken(true);
    5044             : 
    5045          48 :   EVT VT = Op.getValueType();
    5046             :   SDLoc dl(Op);  // FIXME probably not meaningful
    5047          48 :   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
    5048          48 :   unsigned FrameReg = ARI.getFrameRegister(MF);
    5049          48 :   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
    5050          60 :   while (Depth--)
    5051          12 :     FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
    5052          12 :                             MachinePointerInfo());
    5053          48 :   return FrameAddr;
    5054             : }
    5055             : 
    5056             : // FIXME? Maybe this could be a TableGen attribute on some registers and
    5057             : // this table could be generated automatically from RegInfo.
    5058          13 : unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
    5059             :                                               SelectionDAG &DAG) const {
    5060           9 :   unsigned Reg = StringSwitch<unsigned>(RegName)
    5061          13 :                        .Case("sp", ARM::SP)
    5062             :                        .Default(0);
    5063           4 :   if (Reg)
    5064           4 :     return Reg;
    5065           9 :   report_fatal_error(Twine("Invalid register name \""
    5066             :                               + StringRef(RegName)  + "\"."));
    5067             : }
    5068             : 
    5069             : // Result is 64 bit value so split into two 32 bit values and return as a
    5070             : // pair of values.
    5071           2 : static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
    5072             :                                 SelectionDAG &DAG) {
    5073             :   SDLoc DL(N);
    5074             : 
    5075             :   // This function is only supposed to be called for i64 type destination.
    5076             :   assert(N->getValueType(0) == MVT::i64
    5077             :           && "ExpandREAD_REGISTER called for non-i64 type result.");
    5078             : 
    5079             :   SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
    5080             :                              DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
    5081             :                              N->getOperand(0),
    5082           4 :                              N->getOperand(1));
    5083             : 
    5084           2 :   Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
    5085           4 :                     Read.getValue(1)));
    5086           2 :   Results.push_back(Read.getOperand(0));
    5087           2 : }
    5088             : 
    5089             : /// \p BC is a bitcast that is about to be turned into a VMOVDRR.
    5090             : /// When \p DstVT, the destination type of \p BC, is on the vector
    5091             : /// register bank and the source of bitcast, \p Op, operates on the same bank,
    5092             : /// it might be possible to combine them, such that everything stays on the
    5093             : /// vector register bank.
    5094             : /// \p return The node that would replace \p BT, if the combine
    5095             : /// is possible.
    5096          64 : static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
    5097             :                                                 SelectionDAG &DAG) {
    5098          64 :   SDValue Op = BC->getOperand(0);
    5099         128 :   EVT DstVT = BC->getValueType(0);
    5100             : 
    5101             :   // The only vector instruction that can produce a scalar (remember,
    5102             :   // since the bitcast was about to be turned into VMOVDRR, the source
    5103             :   // type is i64) from a vector is EXTRACT_VECTOR_ELT.
    5104             :   // Moreover, we can do this combine only if there is one use.
    5105             :   // Finally, if the destination type is not a vector, there is not
    5106             :   // much point on forcing everything on the vector bank.
    5107          70 :   if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
    5108             :       !Op.hasOneUse())
    5109          59 :     return SDValue();
    5110             : 
    5111             :   // If the index is not constant, we will introduce an additional
    5112             :   // multiply that will stick.
    5113             :   // Give up in that case.
    5114             :   ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
    5115             :   if (!Index)
    5116           1 :     return SDValue();
    5117             :   unsigned DstNumElt = DstVT.getVectorNumElements();
    5118             : 
    5119             :   // Compute the new index.
    5120           4 :   const APInt &APIntIndex = Index->getAPIntValue();
    5121           4 :   APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
    5122           4 :   NewIndex *= APIntIndex;
    5123             :   // Check if the new constant index fits into i32.
    5124           4 :   if (NewIndex.getBitWidth() > 32)
    5125           0 :     return SDValue();
    5126             : 
    5127             :   // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
    5128             :   // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
    5129             :   SDLoc dl(Op);
    5130           4 :   SDValue ExtractSrc = Op.getOperand(0);
    5131             :   EVT VecVT = EVT::getVectorVT(
    5132           4 :       *DAG.getContext(), DstVT.getScalarType(),
    5133           8 :       ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
    5134           4 :   SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
    5135             :   return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
    5136           4 :                      DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
    5137             : }
    5138             : 
    5139             : /// ExpandBITCAST - If the target supports VFP, this function is called to
    5140             : /// expand a bit convert where either the source or destination type is i64 to
    5141             : /// use a VMOVDRR or VMOVRRD node.  This should not be done when the non-i64
    5142             : /// operand type is illegal (e.g., v2f32 for a target that doesn't support
    5143             : /// vectors), since the legalizer won't know what to do with that.
    5144         921 : static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG,
    5145             :                              const ARMSubtarget *Subtarget) {
    5146         921 :   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
    5147             :   SDLoc dl(N);
    5148         921 :   SDValue Op = N->getOperand(0);
    5149             : 
    5150             :   // This function is only supposed to be called for i64 types, either as the
    5151             :   // source or destination of the bit convert.
    5152         921 :   EVT SrcVT = Op.getValueType();
    5153         921 :   EVT DstVT = N->getValueType(0);
    5154         921 :   const bool HasFullFP16 = Subtarget->hasFullFP16();
    5155             : 
    5156             :   if (SrcVT == MVT::f32 && DstVT == MVT::i32) {
    5157             :      // FullFP16: half values are passed in S-registers, and we don't
    5158             :      // need any of the bitcast and moves:
    5159             :      //
    5160             :      // t2: f32,ch = CopyFromReg t0, Register:f32 %0
    5161             :      //   t5: i32 = bitcast t2
    5162             :      // t18: f16 = ARMISD::VMOVhr t5
    5163         178 :      if (Op.getOpcode() != ISD::CopyFromReg ||
    5164             :          Op.getValueType() != MVT::f32)
    5165           4 :        return SDValue();
    5166             : 
    5167         174 :      auto Move = N->use_begin();
    5168         174 :      if (Move->getOpcode() != ARMISD::VMOVhr)
    5169           0 :        return SDValue();
    5170             : 
    5171         174 :      SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
    5172         348 :      SDValue Copy = DAG.getNode(ISD::CopyFromReg, SDLoc(Op), MVT::f16, Ops);
    5173         174 :      DAG.ReplaceAllUsesWith(*Move, &Copy);
    5174         174 :      return Copy;
    5175             :   }
    5176             : 
    5177             :   if (SrcVT == MVT::i16 && DstVT == MVT::f16) {
    5178         546 :     if (!HasFullFP16)
    5179           0 :       return SDValue();
    5180             :     // SoftFP: read half-precision arguments:
    5181             :     //
    5182             :     // t2: i32,ch = ...
    5183             :     //        t7: i16 = truncate t2 <~~~~ Op
    5184             :     //      t8: f16 = bitcast t7    <~~~~ N
    5185             :     //
    5186         546 :     if (Op.getOperand(0).getValueType() == MVT::i32)
    5187         546 :       return DAG.getNode(ARMISD::VMOVhr, SDLoc(Op),
    5188        1092 :                          MVT::f16, Op.getOperand(0));
    5189             : 
    5190           0 :     return SDValue();
    5191             :   }
    5192             : 
    5193             :   // Half-precision return values
    5194             :   if (SrcVT == MVT::f16 && DstVT == MVT::i16) {
    5195         100 :     if (!HasFullFP16)
    5196           0 :       return SDValue();
    5197             :     //
    5198             :     //          t11: f16 = fadd t8, t10
    5199             :     //        t12: i16 = bitcast t11       <~~~ SDNode N
    5200             :     //      t13: i32 = zero_extend t12
    5201             :     //    t16: ch,glue = CopyToReg t0, Register:i32 %r0, t13
    5202             :     //  t17: ch = ARMISD::RET_FLAG t16, Register:i32 %r0, t16:1
    5203             :     //
    5204             :     // transform this into:
    5205             :     //
    5206             :     //    t20: i32 = ARMISD::VMOVrh t11
    5207             :     //  t16: ch,glue = CopyToReg t0, Register:i32 %r0, t20
    5208             :     //
    5209         100 :     auto ZeroExtend = N->use_begin();
    5210         100 :     if (N->use_size() != 1 || ZeroExtend->getOpcode() != ISD::ZERO_EXTEND ||
    5211         100 :         ZeroExtend->getValueType(0) != MVT::i32)
    5212           0 :       return SDValue();
    5213             : 
    5214         100 :     auto Copy = ZeroExtend->use_begin();
    5215         100 :     if (Copy->getOpcode() == ISD::CopyToReg &&
    5216         200 :         Copy->use_begin()->getOpcode() == ARMISD::RET_FLAG) {
    5217         200 :       SDValue Cvt = DAG.getNode(ARMISD::VMOVrh, SDLoc(Op), MVT::i32, Op);
    5218         100 :       DAG.ReplaceAllUsesWith(*ZeroExtend, &Cvt);
    5219         100 :       return Cvt;
    5220             :     }
    5221           0 :     return SDValue();
    5222             :   }
    5223             : 
    5224             :   if (!(SrcVT == MVT::i64 || DstVT == MVT::i64))
    5225           0 :     return SDValue();
    5226             : 
    5227             :   // Turn i64->f64 into VMOVDRR.
    5228             :   if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
    5229             :     // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
    5230             :     // if we can combine the bitcast with its source.
    5231          64 :     if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
    5232           4 :       return Val;
    5233             : 
    5234             :     SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
    5235          60 :                              DAG.getConstant(0, dl, MVT::i32));
    5236             :     SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
    5237          60 :                              DAG.getConstant(1, dl, MVT::i32));
    5238             :     return DAG.getNode(ISD::BITCAST, dl, DstVT,
    5239          60 :                        DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
    5240             :   }
    5241             : 
    5242             :   // Turn f64->i64 into VMOVRRD.
    5243             :   if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
    5244          33 :     SDValue Cvt;
    5245          51 :     if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
    5246             :         SrcVT.getVectorNumElements() > 1)
    5247          16 :       Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
    5248             :                         DAG.getVTList(MVT::i32, MVT::i32),
    5249          16 :                         DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
    5250             :     else
    5251          17 :       Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
    5252          17 :                         DAG.getVTList(MVT::i32, MVT::i32), Op);
    5253             :     // Merge the pieces into a single i64 value.
    5254          33 :     return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
    5255             :   }
    5256             : 
    5257           0 :   return SDValue();
    5258             : }
    5259             : 
    5260             : /// getZeroVector - Returns a vector of specified type with all zero elements.
    5261             : /// Zero vectors are used to represent vector negation and in those cases
    5262             : /// will be implemented with the NEON VNEG instruction.  However, VNEG does
    5263             : /// not support i64 elements, so sometimes the zero vectors will need to be
    5264             : /// explicitly constructed.  Regardless, use a canonical VMOV to create the
    5265             : /// zero vector.
    5266          71 : static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) {
    5267             :   assert(VT.isVector() && "Expected a vector type");
    5268             :   // The canonical modified immediate encoding of a zero vector is....0!
    5269          71 :   SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
    5270          71 :   EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
    5271          71 :   SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
    5272          71 :   return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
    5273             : }
    5274             : 
    5275             : /// LowerShiftRightParts - Lower SRA_PARTS, which returns two
    5276             : /// i32 values and take a 2 x i32 value to shift plus a shift amount.
    5277           6 : SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
    5278             :                                                 SelectionDAG &DAG) const {
    5279             :   assert(Op.getNumOperands() == 3 && "Not a double-shift!");
    5280           6 :   EVT VT = Op.getValueType();
    5281           6 :   unsigned VTBits = VT.getSizeInBits();
    5282             :   SDLoc dl(Op);
    5283           6 :   SDValue ShOpLo = Op.getOperand(0);
    5284           6 :   SDValue ShOpHi = Op.getOperand(1);
    5285           6 :   SDValue ShAmt  = Op.getOperand(2);
    5286           6 :   SDValue ARMcc;
    5287           6 :   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    5288           6 :   unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
    5289             : 
    5290             :   assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
    5291             : 
    5292             :   SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
    5293           6 :                                  DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
    5294           6 :   SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
    5295             :   SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
    5296           6 :                                    DAG.getConstant(VTBits, dl, MVT::i32));
    5297           6 :   SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
    5298           6 :   SDValue LoSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
    5299           6 :   SDValue LoBigShift = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
    5300             :   SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
    5301           6 :                             ISD::SETGE, ARMcc, DAG, dl);
    5302             :   SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, LoBigShift,
    5303           6 :                            ARMcc, CCR, CmpLo);
    5304             : 
    5305           6 :   SDValue HiSmallShift = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
    5306             :   SDValue HiBigShift = Opc == ISD::SRA
    5307             :                            ? DAG.getNode(Opc, dl, VT, ShOpHi,
    5308           6 :                                          DAG.getConstant(VTBits - 1, dl, VT))
    5309           6 :                            : DAG.getConstant(0, dl, VT);
    5310             :   SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
    5311           6 :                             ISD::SETGE, ARMcc, DAG, dl);
    5312             :   SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift,
    5313           6 :                            ARMcc, CCR, CmpHi);
    5314             : 
    5315           6 :   SDValue Ops[2] = { Lo, Hi };
    5316           6 :   return DAG.getMergeValues(Ops, dl);
    5317             : }
    5318             : 
    5319             : /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
    5320             : /// i32 values and take a 2 x i32 value to shift plus a shift amount.
    5321           3 : SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
    5322             :                                                SelectionDAG &DAG) const {
    5323             :   assert(Op.getNumOperands() == 3 && "Not a double-shift!");
    5324           3 :   EVT VT = Op.getValueType();
    5325           3 :   unsigned VTBits = VT.getSizeInBits();
    5326             :   SDLoc dl(Op);
    5327           3 :   SDValue ShOpLo = Op.getOperand(0);
    5328           3 :   SDValue ShOpHi = Op.getOperand(1);
    5329           3 :   SDValue ShAmt  = Op.getOperand(2);
    5330           3 :   SDValue ARMcc;
    5331           3 :   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    5332             : 
    5333             :   assert(Op.getOpcode() == ISD::SHL_PARTS);
    5334             :   SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
    5335           3 :                                  DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
    5336           3 :   SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
    5337           3 :   SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
    5338           3 :   SDValue HiSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
    5339             : 
    5340             :   SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
    5341           3 :                                    DAG.getConstant(VTBits, dl, MVT::i32));
    5342           3 :   SDValue HiBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
    5343             :   SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
    5344           3 :                             ISD::SETGE, ARMcc, DAG, dl);
    5345             :   SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift,
    5346           3 :                            ARMcc, CCR, CmpHi);
    5347             : 
    5348             :   SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
    5349           3 :                           ISD::SETGE, ARMcc, DAG, dl);
    5350           3 :   SDValue LoSmallShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
    5351             :   SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift,
    5352           3 :                            DAG.getConstant(0, dl, VT), ARMcc, CCR, CmpLo);
    5353             : 
    5354           3 :   SDValue Ops[2] = { Lo, Hi };
    5355           3 :   return DAG.getMergeValues(Ops, dl);
    5356             : }
    5357             : 
    5358           3 : SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
    5359             :                                             SelectionDAG &DAG) const {
    5360             :   // The rounding mode is in bits 23:22 of the FPSCR.
    5361             :   // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
    5362             :   // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
    5363             :   // so that the shift + and get folded into a bitfield extract.
    5364             :   SDLoc dl(Op);
    5365           3 :   SDValue Ops[] = { DAG.getEntryNode(),
    5366           3 :                     DAG.getConstant(Intrinsic::arm_get_fpscr, dl, MVT::i32) };
    5367             : 
    5368           3 :   SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_W_CHAIN, dl, MVT::i32, Ops);
    5369             :   SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
    5370           3 :                                   DAG.getConstant(1U << 22, dl, MVT::i32));
    5371             :   SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
    5372           3 :                               DAG.getConstant(22, dl, MVT::i32));
    5373             :   return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
    5374           3 :                      DAG.getConstant(3, dl, MVT::i32));
    5375             : }
    5376             : 
    5377          48 : static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
    5378             :                          const ARMSubtarget *ST) {
    5379             :   SDLoc dl(N);
    5380          96 :   EVT VT = N->getValueType(0);
    5381          48 :   if (VT.isVector()) {
    5382             :     assert(ST->hasNEON());
    5383             : 
    5384             :     // Compute the least significant set bit: LSB = X & -X
    5385          22 :     SDValue X = N->getOperand(0);
    5386          22 :     SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
    5387          22 :     SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
    5388             : 
    5389          22 :     EVT ElemTy = VT.getVectorElementType();
    5390             : 
    5391             :     if (ElemTy == MVT::i8) {
    5392             :       // Compute with: cttz(x) = ctpop(lsb - 1)
    5393             :       SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
    5394           4 :                                 DAG.getTargetConstant(1, dl, ElemTy));
    5395           4 :       SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
    5396           4 :       return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
    5397             :     }
    5398             : 
    5399          14 :     if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
    5400          14 :         (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
    5401             :       // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
    5402           9 :       unsigned NumBits = ElemTy.getSizeInBits();
    5403             :       SDValue WidthMinus1 =
    5404             :           DAG.getNode(ARMISD::VMOVIMM, dl, VT,
    5405           9 :                       DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
    5406           9 :       SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
    5407           9 :       return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
    5408             :     }
    5409             : 
    5410             :     // Compute with: cttz(x) = ctpop(lsb - 1)
    5411             : 
    5412             :     // Compute LSB - 1.
    5413           9 :     SDValue Bits;
    5414             :     if (ElemTy == MVT::i64) {
    5415             :       // Load constant 0xffff'ffff'ffff'ffff to register.
    5416             :       SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
    5417           4 :                                DAG.getTargetConstant(0x1eff, dl, MVT::i32));
    5418           4 :       Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
    5419             :     } else {
    5420             :       SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
    5421           5 :                                 DAG.getTargetConstant(1, dl, ElemTy));
    5422           5 :       Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
    5423             :     }
    5424           9 :     return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
    5425             :   }
    5426             : 
    5427          26 :   if (!ST->hasV6T2Ops())
    5428           0 :     return SDValue();
    5429             : 
    5430          52 :   SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
    5431          26 :   return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
    5432             : }
    5433             : 
    5434           0 : static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
    5435             :                           const ARMSubtarget *ST) {
    5436           0 :   EVT VT = N->getValueType(0);
    5437             :   SDLoc DL(N);
    5438             : 
    5439             :   assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
    5440             :   assert((VT == MVT::v1i64 || VT == MVT::v2i64 || VT == MVT::v2i32 ||
    5441             :           VT == MVT::v4i32 || VT == MVT::v4i16 || VT == MVT::v8i16) &&
    5442             :          "Unexpected type for custom ctpop lowering");
    5443             : 
    5444             :   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
    5445           0 :   EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
    5446           0 :   SDValue Res = DAG.getBitcast(VT8Bit, N->getOperand(0));
    5447           0 :   Res = DAG.getNode(ISD::CTPOP, DL, VT8Bit, Res);
    5448             : 
    5449             :   // Widen v8i8/v16i8 CTPOP result to VT by repeatedly widening pairwise adds.
    5450             :   unsigned EltSize = 8;
    5451           0 :   unsigned NumElts = VT.is64BitVector() ? 8 : 16;
    5452           0 :   while (EltSize != VT.getScalarSizeInBits()) {
    5453             :     SmallVector<SDValue, 8> Ops;
    5454           0 :     Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddlu, DL,
    5455           0 :                                   TLI.getPointerTy(DAG.getDataLayout())));
    5456           0 :     Ops.push_back(Res);
    5457             : 
    5458           0 :     EltSize *= 2;
    5459           0 :     NumElts /= 2;
    5460           0 :     MVT WidenVT = MVT::getVectorVT(MVT::getIntegerVT(EltSize), NumElts);
    5461           0 :     Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, WidenVT, Ops);
    5462             :   }
    5463             : 
    5464           0 :   return Res;
    5465             : }
    5466             : 
    5467           0 : static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
    5468             :                           const ARMSubtarget *ST) {
    5469           0 :   EVT VT = N->getValueType(0);
    5470             :   SDLoc dl(N);
    5471             : 
    5472           0 :   if (!VT.isVector())
    5473           0 :     return SDValue();
    5474             : 
    5475             :   // Lower vector shifts on NEON to use VSHL.
    5476             :   assert(ST->hasNEON() && "unexpected vector shift");
    5477             : 
    5478             :   // Left shifts translate directly to the vshiftu intrinsic.
    5479           0 :   if (N->getOpcode() == ISD::SHL)
    5480             :     return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
    5481             :                        DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
    5482             :                                        MVT::i32),
    5483           0 :                        N->getOperand(0), N->getOperand(1));
    5484             : 
    5485             :   assert((N->getOpcode() == ISD::SRA ||
    5486             :           N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
    5487             : 
    5488             :   // NEON uses the same intrinsics for both left and right shifts.  For
    5489             :   // right shifts, the shift amounts are negative, so negate the vector of
    5490             :   // shift amounts.
    5491           0 :   EVT ShiftVT = N->getOperand(1).getValueType();
    5492             :   SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
    5493             :                                      getZeroVector(ShiftVT, DAG, dl),
    5494           0 :                                      N->getOperand(1));
    5495           0 :   Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
    5496             :                              Intrinsic::arm_neon_vshifts :
    5497             :                              Intrinsic::arm_neon_vshiftu);
    5498             :   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
    5499             :                      DAG.getConstant(vshiftInt, dl, MVT::i32),
    5500           0 :                      N->getOperand(0), NegatedCount);
    5501             : }
    5502             : 
    5503         855 : static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
    5504             :                                 const ARMSubtarget *ST) {
    5505         855 :   EVT VT = N->getValueType(0);
    5506             :   SDLoc dl(N);
    5507             : 
    5508             :   // We can get here for a node like i32 = ISD::SHL i32, i64
    5509             :   if (VT != MVT::i64)
    5510           0 :     return SDValue();
    5511             : 
    5512             :   assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
    5513             :          "Unknown shift to lower!");
    5514             : 
    5515             :   // We only lower SRA, SRL of 1 here, all others use generic lowering.
    5516        1710 :   if (!isOneConstant(N->getOperand(1)))
    5517         847 :     return SDValue();
    5518             : 
    5519             :   // If we are in thumb mode, we don't have RRX.
    5520           9 :   if (ST->isThumb1Only()) return SDValue();
    5521             : 
    5522             :   // Okay, we have a 64-bit SRA or SRL of 1.  Lower this to an RRX expr.
    5523           7 :   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
    5524           7 :                            DAG.getConstant(0, dl, MVT::i32));
    5525           7 :   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
    5526           7 :                            DAG.getConstant(1, dl, MVT::i32));
    5527             : 
    5528             :   // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
    5529             :   // captures the result into a carry flag.
    5530           7 :   unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
    5531           7 :   Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
    5532             : 
    5533             :   // The low part is an ARMISD::RRX operand, which shifts the carry in.
    5534           7 :   Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
    5535             : 
    5536             :   // Merge the pieces into a single i64 value.
    5537           7 :  return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
    5538             : }
    5539             : 
    5540           0 : static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
    5541           0 :   SDValue TmpOp0, TmpOp1;
    5542             :   bool Invert = false;
    5543             :   bool Swap = false;
    5544             :   unsigned Opc = 0;
    5545             : 
    5546           0 :   SDValue Op0 = Op.getOperand(0);
    5547           0 :   SDValue Op1 = Op.getOperand(1);
    5548           0 :   SDValue CC = Op.getOperand(2);
    5549           0 :   EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
    5550           0 :   EVT VT = Op.getValueType();
    5551           0 :   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
    5552           0 :   SDLoc dl(Op);
    5553             : 
    5554           0 :   if (Op0.getValueType().getVectorElementType() == MVT::i64 &&
    5555           0 :       (SetCCOpcode == ISD::SETEQ || SetCCOpcode == ISD::SETNE)) {
    5556             :     // Special-case integer 64-bit equality comparisons. They aren't legal,
    5557             :     // but they can be lowered with a few vector instructions.
    5558           0 :     unsigned CmpElements = CmpVT.getVectorNumElements() * 2;
    5559           0 :     EVT SplitVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, CmpElements);
    5560           0 :     SDValue CastOp0 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op0);
    5561           0 :     SDValue CastOp1 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op1);
    5562             :     SDValue Cmp = DAG.getNode(ISD::SETCC, dl, SplitVT, CastOp0, CastOp1,
    5563           0 :                               DAG.getCondCode(ISD::SETEQ));
    5564           0 :     SDValue Reversed = DAG.getNode(ARMISD::VREV64, dl, SplitVT, Cmp);
    5565           0 :     SDValue Merged = DAG.getNode(ISD::AND, dl, SplitVT, Cmp, Reversed);
    5566           0 :     Merged = DAG.getNode(ISD::BITCAST, dl, CmpVT, Merged);
    5567           0 :     if (SetCCOpcode == ISD::SETNE)
    5568           0 :       Merged = DAG.getNOT(dl, Merged, CmpVT);
    5569           0 :     Merged = DAG.getSExtOrTrunc(Merged, dl, VT);
    5570           0 :     return Merged;
    5571             :   }
    5572             : 
    5573           0 :   if (CmpVT.getVectorElementType() == MVT::i64)
    5574             :     // 64-bit comparisons are not legal in general.
    5575           0 :     return SDValue();
    5576             : 
    5577           0 :   if (Op1.getValueType().isFloatingPoint()) {
    5578           0 :     switch (SetCCOpcode) {
    5579           0 :     default: llvm_unreachable("Illegal FP comparison");
    5580           0 :     case ISD::SETUNE:
    5581             :     case ISD::SETNE:  Invert = true; LLVM_FALLTHROUGH;
    5582             :     case ISD::SETOEQ:
    5583             :     case ISD::SETEQ:  Opc = ARMISD::VCEQ; break;
    5584           0 :     case ISD::SETOLT:
    5585             :     case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH;
    5586             :     case ISD::SETOGT:
    5587             :     case ISD::SETGT:  Opc = ARMISD::VCGT; break;
    5588           0 :     case ISD::SETOLE:
    5589             :     case ISD::SETLE:  Swap = true; LLVM_FALLTHROUGH;
    5590             :     case ISD::SETOGE:
    5591             :     case ISD::SETGE: Opc = ARMISD::VCGE; break;
    5592           0 :     case ISD::SETUGE: Swap = true; LLVM_FALLTHROUGH;
    5593             :     case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
    5594           0 :     case ISD::SETUGT: Swap = true; LLVM_FALLTHROUGH;
    5595             :     case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
    5596           0 :     case ISD::SETUEQ: Invert = true; LLVM_FALLTHROUGH;
    5597           0 :     case ISD::SETONE:
    5598             :       // Expand this to (OLT | OGT).
    5599           0 :       TmpOp0 = Op0;
    5600           0 :       TmpOp1 = Op1;
    5601             :       Opc = ISD::OR;
    5602           0 :       Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
    5603           0 :       Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
    5604           0 :       break;
    5605           0 :     case ISD::SETUO:
    5606             :       Invert = true;
    5607             :       LLVM_FALLTHROUGH;
    5608           0 :     case ISD::SETO:
    5609             :       // Expand this to (OLT | OGE).
    5610           0 :       TmpOp0 = Op0;
    5611           0 :       TmpOp1 = Op1;
    5612             :       Opc = ISD::OR;
    5613           0 :       Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
    5614           0 :       Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
    5615           0 :       break;
    5616             :     }
    5617             :   } else {
    5618             :     // Integer comparisons.
    5619           0 :     switch (SetCCOpcode) {
    5620           0 :     default: llvm_unreachable("Illegal integer comparison");
    5621           0 :     case ISD::SETNE:  Invert = true; LLVM_FALLTHROUGH;
    5622             :     case ISD::SETEQ:  Opc = ARMISD::VCEQ; break;
    5623           0 :     case ISD::SETLT:  Swap = true; LLVM_FALLTHROUGH;
    5624             :     case ISD::SETGT:  Opc = ARMISD::VCGT; break;
    5625           0 :     case ISD::SETLE:  Swap = true; LLVM_FALLTHROUGH;
    5626             :     case ISD::SETGE:  Opc = ARMISD::VCGE; break;
    5627           0 :     case ISD::SETULT: Swap = true; LLVM_FALLTHROUGH;
    5628             :     case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
    5629           0 :     case ISD::SETULE: Swap = true; LLVM_FALLTHROUGH;
    5630             :     case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
    5631             :     }
    5632             : 
    5633             :     // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
    5634           0 :     if (Opc == ARMISD::VCEQ) {
    5635             :       SDValue AndOp;
    5636           0 :       if (ISD::isBuildVectorAllZeros(Op1.getNode()))
    5637             :         AndOp = Op0;
    5638           0 :       else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
    5639             :         AndOp = Op1;
    5640             : 
    5641             :       // Ignore bitconvert.
    5642           0 :       if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
    5643           0 :         AndOp = AndOp.getOperand(0);
    5644             : 
    5645           0 :       if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
    5646             :         Opc = ARMISD::VTST;
    5647           0 :         Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
    5648           0 :         Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
    5649           0 :         Invert = !Invert;
    5650             :       }
    5651             :     }
    5652             :   }
    5653             : 
    5654           0 :   if (Swap)
    5655             :     std::swap(Op0, Op1);
    5656             : 
    5657             :   // If one of the operands is a constant vector zero, attempt to fold the
    5658             :   // comparison to a specialized compare-against-zero form.
    5659           0 :   SDValue SingleOp;
    5660           0 :   if (ISD::isBuildVectorAllZeros(Op1.getNode()))
    5661           0 :     SingleOp = Op0;
    5662           0 :   else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
    5663           0 :     if (Opc == ARMISD::VCGE)
    5664             :       Opc = ARMISD::VCLEZ;
    5665           0 :     else if (Opc == ARMISD::VCGT)
    5666             :       Opc = ARMISD::VCLTZ;
    5667           0 :     SingleOp = Op1;
    5668             :   }
    5669             : 
    5670             :   SDValue Result;
    5671           0 :   if (SingleOp.getNode()) {
    5672           0 :     switch (Opc) {
    5673             :     case ARMISD::VCEQ:
    5674           0 :       Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
    5675             :     case ARMISD::VCGE:
    5676           0 :       Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
    5677             :     case ARMISD::VCLEZ:
    5678           0 :       Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
    5679             :     case ARMISD::VCGT:
    5680           0 :       Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
    5681             :     case ARMISD::VCLTZ:
    5682           0 :       Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
    5683             :     default:
    5684           0 :       Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
    5685             :     }
    5686             :   } else {
    5687           0 :      Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
    5688             :   }
    5689             : 
    5690           0 :   Result = DAG.getSExtOrTrunc(Result, dl, VT);
    5691             : 
    5692           0 :   if (Invert)
    5693           0 :     Result = DAG.getNOT(dl, Result, VT);
    5694             : 
    5695           0 :   return Result;
    5696             : }
    5697             : 
    5698           0 : static SDValue LowerSETCCCARRY(SDValue Op, SelectionDAG &DAG) {
    5699           0 :   SDValue LHS = Op.getOperand(0);
    5700           0 :   SDValue RHS = Op.getOperand(1);
    5701           0 :   SDValue Carry = Op.getOperand(2);
    5702           0 :   SDValue Cond = Op.getOperand(3);
    5703           0 :   SDLoc DL(Op);
    5704             : 
    5705             :   assert(LHS.getSimpleValueType().isInteger() && "SETCCCARRY is integer only.");
    5706             : 
    5707             :   // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we
    5708             :   // have to invert the carry first.
    5709           0 :   Carry = DAG.getNode(ISD::SUB, DL, MVT::i32,
    5710           0 :                       DAG.getConstant(1, DL, MVT::i32), Carry);
    5711             :   // This converts the boolean value carry into the carry flag.
    5712           0 :   Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG);
    5713             : 
    5714           0 :   SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
    5715           0 :   SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry);
    5716             : 
    5717           0 :   SDValue FVal = DAG.getConstant(0, DL, MVT::i32);
    5718           0 :   SDValue TVal = DAG.getConstant(1, DL, MVT::i32);
    5719             :   SDValue ARMcc = DAG.getConstant(
    5720           0 :       IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32);
    5721           0 :   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
    5722             :   SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR,
    5723           0 :                                    Cmp.getValue(1), SDValue());
    5724             :   return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc,
    5725           0 :                      CCR, Chain.getValue(1));
    5726             : }
    5727             : 
    5728             : /// isNEONModifiedImm - Check if the specified splat value corresponds to a
    5729             : /// valid vector constant for a NEON instruction with a "modified immediate"
    5730             : /// operand (e.g., VMOV).  If so, return the encoded value.
    5731        1035 : static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
    5732             :                                  unsigned SplatBitSize, SelectionDAG &DAG,
    5733             :                                  const SDLoc &dl, EVT &VT, bool is128Bits,
    5734             :                                  NEONModImmType type) {
    5735             :   unsigned OpCmode, Imm;
    5736             : 
    5737             :   // SplatBitSize is set to the smallest size that splats the vector, so a
    5738             :   // zero vector will always have SplatBitSize == 8.  However, NEON modified
    5739             :   // immediate instructions others than VMOV do not support the 8-bit encoding
    5740             :   // of a zero vector, and the default encoding of zero is supposed to be the
    5741             :   // 32-bit version.
    5742        1035 :   if (SplatBits == 0)
    5743             :     SplatBitSize = 32;
    5744             : 
    5745         953 :   switch (SplatBitSize) {
    5746          79 :   case 8:
    5747          79 :     if (type != VMOVModImm)
    5748           0 :       return SDValue();
    5749             :     // Any 1-byte value is OK.  Op=0, Cmode=1110.
    5750             :     assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
    5751             :     OpCmode = 0xe;
    5752          79 :     Imm = SplatBits;
    5753          79 :     VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
    5754          79 :     break;
    5755             : 
    5756          34 :   case 16:
    5757             :     // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
    5758          34 :     VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
    5759          34 :     if ((SplatBits & ~0xff) == 0) {
    5760             :       // Value = 0x00nn: Op=x, Cmode=100x.
    5761             :       OpCmode = 0x8;
    5762          16 :       Imm = SplatBits;
    5763          16 :       break;
    5764             :     }
    5765          18 :     if ((SplatBits & ~0xff00) == 0) {
    5766             :       // Value = 0xnn00: Op=x, Cmode=101x.
    5767             :       OpCmode = 0xa;
    5768           6 :       Imm = SplatBits >> 8;
    5769           6 :       break;
    5770             :     }
    5771          12 :     return SDValue();
    5772             : 
    5773         826 :   case 32:
    5774             :     // NEON's 32-bit VMOV supports splat values where:
    5775             :     // * only one byte is nonzero, or
    5776             :     // * the least significant byte is 0xff and the second byte is nonzero, or
    5777             :     // * the least significant 2 bytes are 0xff and the third is nonzero.
    5778         826 :     VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
    5779         826 :     if ((SplatBits & ~0xff) == 0) {
    5780             :       // Value = 0x000000nn: Op=x, Cmode=000x.
    5781             :       OpCmode = 0;
    5782         108 :       Imm = SplatBits;
    5783         108 :       break;
    5784             :     }
    5785         718 :     if ((SplatBits & ~0xff00) == 0) {
    5786             :       // Value = 0x0000nn00: Op=x, Cmode=001x.
    5787             :       OpCmode = 0x2;
    5788           6 :       Imm = SplatBits >> 8;
    5789           6 :       break;
    5790             :     }
    5791         712 :     if ((SplatBits & ~0xff0000) == 0) {
    5792             :       // Value = 0x00nn0000: Op=x, Cmode=010x.
    5793             :       OpCmode = 0x4;
    5794           5 :       Imm = SplatBits >> 16;
    5795           5 :       break;
    5796             :     }
    5797         707 :     if ((SplatBits & ~0xff000000) == 0) {
    5798             :       // Value = 0xnn000000: Op=x, Cmode=011x.
    5799             :       OpCmode = 0x6;
    5800          15 :       Imm = SplatBits >> 24;
    5801          15 :       break;
    5802             :     }
    5803             : 
    5804             :     // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
    5805         692 :     if (type == OtherModImm) return SDValue();
    5806             : 
    5807         672 :     if ((SplatBits & ~0xffff) == 0 &&
    5808           5 :         ((SplatBits | SplatUndef) & 0xff) == 0xff) {
    5809             :       // Value = 0x0000nnff: Op=x, Cmode=1100.
    5810             :       OpCmode = 0xc;
    5811           3 :       Imm = SplatBits >> 8;
    5812           3 :       break;
    5813             :     }
    5814             : 
    5815         669 :     if ((SplatBits & ~0xffffff) == 0 &&
    5816           6 :         ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
    5817             :       // Value = 0x00nnffff: Op=x, Cmode=1101.
    5818             :       OpCmode = 0xd;
    5819           4 :       Imm = SplatBits >> 16;
    5820           4 :       break;
    5821             :     }
    5822             : 
    5823             :     // Note: there are a few 32-bit splat values (specifically: 00ffff00,
    5824             :     // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
    5825             :     // VMOV.I32.  A (very) minor optimization would be to replicate the value
    5826             :     // and fall through here to test for a valid 64-bit splat.  But, then the
    5827             :     // caller would also need to check and handle the change in size.
    5828         665 :     return SDValue();
    5829             : 
    5830          96 :   case 64: {
    5831          96 :     if (type != VMOVModImm)
    5832          47 :       return SDValue();
    5833             :     // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
    5834             :     uint64_t BitMask = 0xff;
    5835             :     uint64_t Val = 0;
    5836             :     unsigned ImmMask = 1;
    5837             :     Imm = 0;
    5838         102 :     for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
    5839          99 :       if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
    5840             :         Val |= BitMask;
    5841          23 :         Imm |= ImmMask;
    5842          76 :       } else if ((SplatBits & BitMask) != 0) {
    5843          46 :         return SDValue();
    5844             :       }
    5845          53 :       BitMask <<= 8;
    5846          53 :       ImmMask <<= 1;
    5847             :     }
    5848             : 
    5849           3 :     if (DAG.getDataLayout().isBigEndian())
    5850             :       // swap higher and lower 32 bit word
    5851           0 :       Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
    5852             : 
    5853             :     // Op=1, Cmode=1110.
    5854             :     OpCmode = 0x1e;
    5855           3 :     VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
    5856           3 :     break;
    5857             :   }
    5858             : 
    5859           0 :   default:
    5860           0 :     llvm_unreachable("unexpected size for isNEONModifiedImm");
    5861             :   }
    5862             : 
    5863             :   unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
    5864         245 :   return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
    5865             : }
    5866             : 
    5867        1317 : SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
    5868             :                                            const ARMSubtarget *ST) const {
    5869        1317 :   EVT VT = Op.getValueType();
    5870             :   bool IsDouble = (VT == MVT::f64);
    5871             :   ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
    5872        1317 :   const APFloat &FPVal = CFP->getValueAPF();
    5873             : 
    5874             :   // Prevent floating-point constants from using literal loads
    5875             :   // when execute-only is enabled.
    5876        1317 :   if (ST->genExecuteOnly()) {
    5877             :     // If we can represent the constant as an immediate, don't lower it
    5878         115 :     if (isFPImmLegal(FPVal, VT))
    5879          66 :       return Op;
    5880             :     // Otherwise, construct as integer, and move to float register
    5881          49 :     APInt INTVal = FPVal.bitcastToAPInt();
    5882             :     SDLoc DL(CFP);
    5883          49 :     switch (VT.getSimpleVT().SimpleTy) {
    5884           0 :       default:
    5885           0 :         llvm_unreachable("Unknown floating point type!");
    5886             :         break;
    5887             :       case MVT::f64: {
    5888          58 :         SDValue Lo = DAG.getConstant(INTVal.trunc(32), DL, MVT::i32);
    5889          58 :         SDValue Hi = DAG.getConstant(INTVal.lshr(32).trunc(32), DL, MVT::i32);
    5890          29 :         if (!ST->isLittle())
    5891             :           std::swap(Lo, Hi);
    5892          29 :         return DAG.getNode(ARMISD::VMOVDRR, DL, MVT::f64, Lo, Hi);
    5893             :       }
    5894             :       case MVT::f32:
    5895             :           return DAG.getNode(ARMISD::VMOVSR, DL, VT,
    5896          20 :               DAG.getConstant(INTVal, DL, MVT::i32));
    5897             :     }
    5898             :   }
    5899             : 
    5900        1202 :   if (!ST->hasVFP3())
    5901          28 :     return SDValue();
    5902             : 
    5903             :   // Use the default (constant pool) lowering for double constants when we have
    5904             :   // an SP-only FPU
    5905        1174 :   if (IsDouble && Subtarget->isFPOnlySP())
    5906          22 :     return SDValue();
    5907             : 
    5908             :   // Try splatting with a VMOV.f32...
    5909        1152 :   int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
    5910             : 
    5911        1152 :   if (ImmVal != -1) {
    5912         443 :     if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
    5913             :       // We have code in place to select a valid ConstantFP already, no need to
    5914             :       // do any mangling.
    5915         410 :       return Op;
    5916             :     }
    5917             : 
    5918             :     // It's a float and we are trying to use NEON operations where
    5919             :     // possible. Lower it to a splat followed by an extract.
    5920             :     SDLoc DL(Op);
    5921          33 :     SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
    5922             :     SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
    5923          33 :                                       NewVal);
    5924             :     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
    5925          33 :                        DAG.getConstant(0, DL, MVT::i32));
    5926             :   }
    5927             : 
    5928             :   // The rest of our options are NEON only, make sure that's allowed before
    5929             :   // proceeding..
    5930         709 :   if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
    5931         304 :     return SDValue();
    5932             : 
    5933         405 :   EVT VMovVT;
    5934         405 :   uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
    5935             : 
    5936             :   // It wouldn't really be worth bothering for doubles except for one very
    5937             :   // important value, which does happen to match: 0.0. So make sure we don't do
    5938             :   // anything stupid.
    5939         405 :   if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
    5940          81 :     return SDValue();
    5941             : 
    5942             :   // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
    5943         324 :   SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
    5944         324 :                                      VMovVT, false, VMOVModImm);
    5945             :   if (NewVal != SDValue()) {
    5946             :     SDLoc DL(Op);
    5947             :     SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
    5948          19 :                                       NewVal);
    5949          19 :     if (IsDouble)
    5950          13 :       return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
    5951             : 
    5952             :     // It's a float: cast and extract a vector element.
    5953             :     SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
    5954           6 :                                        VecConstant);
    5955             :     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
    5956           6 :                        DAG.getConstant(0, DL, MVT::i32));
    5957             :   }
    5958             : 
    5959             :   // Finally, try a VMVN.i32
    5960         305 :   NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
    5961         305 :                              false, VMVNModImm);
    5962             :   if (NewVal != SDValue()) {
    5963             :     SDLoc DL(Op);
    5964           3 :     SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
    5965             : 
    5966           3 :     if (IsDouble)
    5967           2 :       return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
    5968             : 
    5969             :     // It's a float: cast and extract a vector element.
    5970             :     SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
    5971           1 :                                        VecConstant);
    5972             :     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
    5973           1 :                        DAG.getConstant(0, DL, MVT::i32));
    5974             :   }
    5975             : 
    5976         302 :   return SDValue();
    5977             : }
    5978             : 
    5979             : // check if an VEXT instruction can handle the shuffle mask when the
    5980             : // vector sources of the shuffle are the same.
    5981           0 : static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
    5982             :   unsigned NumElts = VT.getVectorNumElements();
    5983             : 
    5984             :   // Assume that the first shuffle index is not UNDEF.  Fail if it is.
    5985           0 :   if (M[0] < 0)
    5986           0 :     return false;
    5987             : 
    5988           0 :   Imm = M[0];
    5989             : 
    5990             :   // If this is a VEXT shuffle, the immediate value is the index of the first
    5991             :   // element.  The other shuffle indices must be the successive elements after
    5992             :   // the first one.
    5993             :   unsigned ExpectedElt = Imm;
    5994           0 :   for (unsigned i = 1; i < NumElts; ++i) {
    5995             :     // Increment the expected index.  If it wraps around, just follow it
    5996             :     // back to index zero and keep going.
    5997           0 :     ++ExpectedElt;
    5998           0 :     if (ExpectedElt == NumElts)
    5999             :       ExpectedElt = 0;
    6000             : 
    6001           0 :     if (M[i] < 0) continue; // ignore UNDEF indices
    6002           0 :     if (ExpectedElt != static_cast<unsigned>(M[i]))
    6003           0 :       return false;
    6004             :   }
    6005             : 
    6006             :   return true;
    6007             : }
    6008             : 
    6009           0 : static bool isVEXTMask(ArrayRef<int> M, EVT VT,
    6010             :                        bool &ReverseVEXT, unsigned &Imm) {
    6011             :   unsigned NumElts = VT.getVectorNumElements();
    6012           0 :   ReverseVEXT = false;
    6013             : 
    6014             :   // Assume that the first shuffle index is not UNDEF.  Fail if it is.
    6015           0 :   if (M[0] < 0)
    6016           0 :     return false;
    6017             : 
    6018           0 :   Imm = M[0];
    6019             : 
    6020             :   // If this is a VEXT shuffle, the immediate value is the index of the first
    6021             :   // element.  The other shuffle indices must be the successive elements after
    6022             :   // the first one.
    6023             :   unsigned ExpectedElt = Imm;
    6024           0 :   for (unsigned i = 1; i < NumElts; ++i) {
    6025             :     // Increment the expected index.  If it wraps around, it may still be
    6026             :     // a VEXT but the source vectors must be swapped.
    6027           0 :     ExpectedElt += 1;
    6028           0 :     if (ExpectedElt == NumElts * 2) {
    6029             :       ExpectedElt = 0;
    6030           0 :       ReverseVEXT = true;
    6031             :     }
    6032             : 
    6033           0 :     if (M[i] < 0) continue; // ignore UNDEF indices
    6034           0 :     if (ExpectedElt != static_cast<unsigned>(M[i]))
    6035           0 :       return false;
    6036             :   }
    6037             : 
    6038             :   // Adjust the index value if the source operands will be swapped.
    6039           0 :   if (ReverseVEXT)
    6040           0 :     Imm -= NumElts;
    6041             : 
    6042             :   return true;
    6043             : }
    6044             : 
    6045             : /// isVREVMask - Check if a vector shuffle corresponds to a VREV
    6046             : /// instruction with the specified blocksize.  (The order of the elements
    6047             : /// within each block of the vector is reversed.)
    6048           0 : static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
    6049             :   assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
    6050             :          "Only possible block sizes for VREV are: 16, 32, 64");
    6051             : 
    6052             :   unsigned EltSz = VT.getScalarSizeInBits();
    6053           0 :   if (EltSz == 64)
    6054           0 :     return false;
    6055             : 
    6056             :   unsigned NumElts = VT.getVectorNumElements();
    6057           0 :   unsigned BlockElts = M[0] + 1;
    6058             :   // If the first shuffle index is UNDEF, be optimistic.
    6059           0 :   if (M[0] < 0)
    6060           0 :     BlockElts = BlockSize / EltSz;
    6061             : 
    6062           0 :   if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
    6063           0 :     return false;
    6064             : 
    6065           0 :   for (unsigned i = 0; i < NumElts; ++i) {
    6066           0 :     if (M[i] < 0) continue; // ignore UNDEF indices
    6067           0 :     if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
    6068           0 :       return false;
    6069             :   }
    6070             : 
    6071             :   return true;
    6072             : }
    6073             : 
    6074           0 : static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
    6075             :   // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
    6076             :   // range, then 0 is placed into the resulting vector. So pretty much any mask
    6077             :   // of 8 elements can work here.
    6078           8 :   return VT == MVT::v8i8 && M.size() == 8;
    6079             : }
    6080             : 
    6081             : static unsigned SelectPairHalf(unsigned Elements, ArrayRef<int> Mask,
    6082             :                                unsigned Index) {
    6083         887 :   if (Mask.size() == Elements * 2)
    6084         130 :     return Index / Elements;
    6085         757 :   return Mask[Index] == 0 ? 0 : 1;
    6086             : }
    6087             : 
    6088             : // Checks whether the shuffle mask represents a vector transpose (VTRN) by
    6089             : // checking that pairs of elements in the shuffle mask represent the same index
    6090             : // in each vector, incrementing the expected index by 2 at each step.
    6091             : // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
    6092             : //  v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
    6093             : //  v2={e,f,g,h}
    6094             : // WhichResult gives the offset for each element in the mask based on which
    6095             : // of the two results it belongs to.
    6096             : //
    6097             : // The transpose can be represented either as:
    6098             : // result1 = shufflevector v1, v2, result1_shuffle_mask
    6099             : // result2 = shufflevector v1, v2, result2_shuffle_mask
    6100             : // where v1/v2 and the shuffle masks have the same number of elements
    6101             : // (here WhichResult (see below) indicates which result is being checked)
    6102             : //
    6103             : // or as:
    6104             : // results = shufflevector v1, v2, shuffle_mask
    6105             : // where both results are returned in one vector and the shuffle mask has twice
    6106             : // as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
    6107             : // want to check the low half and high half of the shuffle mask as if it were
    6108             : // the other case
    6109         255 : static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
    6110             :   unsigned EltSz = VT.getScalarSizeInBits();
    6111         255 :   if (EltSz == 64)
    6112             :     return false;
    6113             : 
    6114             :   unsigned NumElts = VT.getVectorNumElements();
    6115         255 :   if (M.size() != NumElts && M.size() != NumElts*2)
    6116             :     return false;
    6117             : 
    6118             :   // If the mask is twice as long as the input vector then we need to check the
    6119             :   // upper and lower parts of the mask with a matching value for WhichResult
    6120             :   // FIXME: A mask with only even values will be rejected in case the first
    6121             :   // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
    6122             :   // M[0] is used to determine WhichResult
    6123         320 :   for (unsigned i = 0; i < M.size(); i += NumElts) {
    6124         265 :     WhichResult = SelectPairHalf(NumElts, M, i);
    6125         494 :     for (unsigned j = 0; j < NumElts; j += 2) {
    6126         858 :       if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
    6127         708 :           (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
    6128             :         return false;
    6129             :     }
    6130             :   }
    6131             : 
    6132          55 :   if (M.size() == NumElts*2)
    6133           6 :     WhichResult = 0;
    6134             : 
    6135             :   return true;
    6136             : }
    6137             : 
    6138             : /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
    6139             : /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
    6140             : /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
    6141          92 : static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
    6142             :   unsigned EltSz = VT.getScalarSizeInBits();
    6143          92 :   if (EltSz == 64)
    6144             :     return false;
    6145             : 
    6146             :   unsigned NumElts = VT.getVectorNumElements();
    6147          92 :   if (M.size() != NumElts && M.size() != NumElts*2)
    6148             :     return false;
    6149             : 
    6150         102 :   for (unsigned i = 0; i < M.size(); i += NumElts) {
    6151          98 :     WhichResult = SelectPairHalf(NumElts, M, i);
    6152         153 :     for (unsigned j = 0; j < NumElts; j += 2) {
    6153         286 :       if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
    6154         194 :           (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
    6155             :         return false;
    6156             :     }
    6157             :   }
    6158             : 
    6159           4 :   if (M.size() == NumElts*2)
    6160           0 :     WhichResult = 0;
    6161             : 
    6162             :   return true;
    6163             : }
    6164             : 
    6165             : // Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
    6166             : // that the mask elements are either all even and in steps of size 2 or all odd
    6167             : // and in steps of size 2.
    6168             : // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
    6169             : //  v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
    6170             : //  v2={e,f,g,h}
    6171             : // Requires similar checks to that of isVTRNMask with
    6172             : // respect the how results are returned.
    6173         200 : static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
    6174             :   unsigned EltSz = VT.getScalarSizeInBits();
    6175         200 :   if (EltSz == 64)
    6176             :     return false;
    6177             : 
    6178             :   unsigned NumElts = VT.getVectorNumElements();
    6179         200 :   if (M.size() != NumElts && M.size() != NumElts*2)
    6180             :     return false;
    6181             : 
    6182         279 :   for (unsigned i = 0; i < M.size(); i += NumElts) {
    6183         206 :     WhichResult = SelectPairHalf(NumElts, M, i);
    6184         892 :     for (unsigned j = 0; j < NumElts; ++j) {
    6185        1626 :       if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
    6186             :         return false;
    6187             :     }
    6188             :   }
    6189             : 
    6190          73 :   if (M.size() == NumElts*2)
    6191           3 :     WhichResult = 0;
    6192             : 
    6193             :   // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
    6194          73 :   if (VT.is64BitVector() && EltSz == 32)
    6195           0 :     return false;
    6196             : 
    6197             :   return true;
    6198             : }
    6199             : 
    6200             : /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
    6201             : /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
    6202             : /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
    6203          88 : static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
    6204             :   unsigned EltSz = VT.getScalarSizeInBits();
    6205          88 :   if (EltSz == 64)
    6206             :     return false;
    6207             : 
    6208             :   unsigned NumElts = VT.getVectorNumElements();
    6209          88 :   if (M.size() != NumElts && M.size() != NumElts*2)
    6210             :     return false;
    6211             : 
    6212          88 :   unsigned Half = NumElts / 2;
    6213          97 :   for (unsigned i = 0; i < M.size(); i += NumElts) {
    6214          94 :     WhichResult = SelectPairHalf(NumElts, M, i);
    6215         133 :     for (unsigned j = 0; j < NumElts; j += Half) {
    6216             :       unsigned Idx = WhichResult;
    6217         282 :       for (unsigned k = 0; k < Half; ++k) {
    6218         243 :         int MIdx = M[i + j + k];
    6219         243 :         if (MIdx >= 0 && (unsigned) MIdx != Idx)
    6220             :           return false;
    6221         158 :         Idx += 2;
    6222             :       }
    6223             :     }
    6224             :   }
    6225             : 
    6226           3 :   if (M.size() == NumElts*2)
    6227           0 :     WhichResult = 0;
    6228             : 
    6229             :   // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
    6230           3 :   if (VT.is64BitVector() && EltSz == 32)
    6231           0 :     return false;
    6232             : 
    6233             :   return true;
    6234             : }
    6235             : 
    6236             : // Checks whether the shuffle mask represents a vector zip (VZIP) by checking
    6237             : // that pairs of elements of the shufflemask represent the same index in each
    6238             : // vector incrementing sequentially through the vectors.
    6239             : // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
    6240             : //  v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
    6241             : //  v2={e,f,g,h}
    6242             : // Requires similar checks to that of isVTRNMask with respect the how results
    6243             : // are returned.
    6244         127 : static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
    6245             :   unsigned EltSz = VT.getScalarSizeInBits();
    6246         127 :   if (EltSz == 64)
    6247             :     return false;
    6248             : 
    6249             :   unsigned NumElts = VT.getVectorNumElements();
    6250         127 :   if (M.size() != NumElts && M.size() != NumElts*2)
    6251             :     return false;
    6252             : 
    6253         168 :   for (unsigned i = 0; i < M.size(); i += NumElts) {
    6254         133 :     WhichResult = SelectPairHalf(NumElts, M, i);
    6255         133 :     unsigned Idx = WhichResult * NumElts / 2;
    6256         320 :     for (unsigned j = 0; j < NumElts; j += 2) {
    6257         558 :       if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
    6258         466 :           (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
    6259             :         return false;
    6260         187 :       Idx += 1;
    6261             :     }
    6262             :   }
    6263             : 
    6264          35 :   if (M.size() == NumElts*2)
    6265           4 :     WhichResult = 0;
    6266             : 
    6267             :   // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
    6268          35 :   if (VT.is64BitVector() && EltSz == 32)
    6269           0 :     return false;
    6270             : 
    6271             :   return true;
    6272             : }
    6273             : 
    6274             : /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
    6275             : /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
    6276             : /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
    6277          85 : static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
    6278             :   unsigned EltSz = VT.getScalarSizeInBits();
    6279          85 :   if (EltSz == 64)
    6280             :     return false;
    6281             : 
    6282             :   unsigned NumElts = VT.getVectorNumElements();
    6283          85 :   if (M.size() != NumElts && M.size() != NumElts*2)
    6284             :     return false;
    6285             : 
    6286          92 :   for (unsigned i = 0; i < M.size(); i += NumElts) {
    6287          91 :     WhichResult = SelectPairHalf(NumElts, M, i);
    6288          91 :     unsigned Idx = WhichResult * NumElts / 2;
    6289         129 :     for (unsigned j = 0; j < NumElts; j += 2) {
    6290         244 :       if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
    6291         156 :           (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
    6292             :         return false;
    6293          38 :       Idx += 1;
    6294             :     }
    6295             :   }
    6296             : 
    6297           1 :   if (M.size() == NumElts*2)
    6298           0 :     WhichResult = 0;
    6299             : 
    6300             :   // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
    6301           1 :   if (VT.is64BitVector() && EltSz == 32)
    6302           0 :     return false;
    6303             : 
    6304             :   return true;
    6305             : }
    6306             : 
    6307             : /// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
    6308             : /// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
    6309         255 : static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
    6310             :                                            unsigned &WhichResult,
    6311             :                                            bool &isV_UNDEF) {
    6312         255 :   isV_UNDEF = false;
    6313         255 :   if (isVTRNMask(ShuffleMask, VT, WhichResult))
    6314             :     return ARMISD::VTRN;
    6315         200 :   if (isVUZPMask(ShuffleMask, VT, WhichResult))
    6316             :     return ARMISD::VUZP;
    6317         127 :   if (isVZIPMask(ShuffleMask, VT, WhichResult))
    6318             :     return ARMISD::VZIP;
    6319             : 
    6320          92 :   isV_UNDEF = true;
    6321          92 :   if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
    6322             :     return ARMISD::VTRN;
    6323          88 :   if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
    6324             :     return ARMISD::VUZP;
    6325          85 :   if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
    6326           1 :     return ARMISD::VZIP;
    6327             : 
    6328             :   return 0;
    6329             : }
    6330             : 
    6331             : /// \return true if this is a reverse operation on an vector.
    6332          37 : static bool isReverseMask(ArrayRef<int> M, EVT VT) {
    6333             :   unsigned NumElts = VT.getVectorNumElements();
    6334             :   // Make sure the mask has the right size.
    6335          37 :   if (NumElts != M.size())
    6336             :       return false;
    6337             : 
    6338             :   // Look for <15, ..., 3, -1, 1, 0>.
    6339          83 :   for (unsigned i = 0; i != NumElts; ++i)
    6340         162 :     if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
    6341             :       return false;
    6342             : 
    6343             :   return true;
    6344             : }
    6345             : 
    6346             : // If N is an integer constant that can be moved into a register in one
    6347             : // instruction, return an SDValue of such a constant (will become a MOV
    6348             : // instruction).  Otherwise return null.
    6349           0 : static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
    6350             :                                      const ARMSubtarget *ST, const SDLoc &dl) {
    6351             :   uint64_t Val;
    6352             :   if (!isa<ConstantSDNode>(N))
    6353           0 :     return SDValue();
    6354           0 :   Val = cast<ConstantSDNode>(N)->getZExtValue();
    6355             : 
    6356           0 :   if (ST->isThumb1Only()) {
    6357           0 :     if (Val <= 255 || ~Val <= 255)
    6358           0 :       return DAG.getConstant(Val, dl, MVT::i32);
    6359             :   } else {
    6360           0 :     if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
    6361           0 :       return DAG.getConstant(Val, dl, MVT::i32);
    6362             :   }
    6363           0 :   return SDValue();
    6364             : }
    6365             : 
    6366             : // If this is a case we can't handle, return null and let the default
    6367             : // expansion code take care of it.
    6368        1164 : SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
    6369             :                                              const ARMSubtarget *ST) const {
    6370             :   BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
    6371             :   SDLoc dl(Op);
    6372        1164 :   EVT VT = Op.getValueType();
    6373             : 
    6374             :   APInt SplatBits, SplatUndef;
    6375             :   unsigned SplatBitSize;
    6376             :   bool HasAnyUndefs;
    6377        1164 :   if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
    6378         461 :     if (SplatUndef.isAllOnesValue())
    6379           9 :       return DAG.getUNDEF(VT);
    6380             : 
    6381         452 :     if (SplatBitSize <= 64) {
    6382             :       // Check if an immediate VMOV works.
    6383         285 :       EVT VmovVT;
    6384             :       SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
    6385             :                                       SplatUndef.getZExtValue(), SplatBitSize,
    6386         285 :                                       DAG, dl, VmovVT, VT.is128BitVector(),
    6387         570 :                                       VMOVModImm);
    6388         285 :       if (Val.getNode()) {
    6389         200 :         SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
    6390         200 :         return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
    6391             :       }
    6392             : 
    6393             :       // Try an immediate VMVN.
    6394          85 :       uint64_t NegatedImm = (~SplatBits).getZExtValue();
    6395          85 :       Val = isNEONModifiedImm(NegatedImm,
    6396             :                                       SplatUndef.getZExtValue(), SplatBitSize,
    6397          85 :                                       DAG, dl, VmovVT, VT.is128BitVector(),
    6398         170 :                                       VMVNModImm);
    6399          85 :       if (Val.getNode()) {
    6400          10 :         SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
    6401          10 :         return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
    6402             :       }
    6403             : 
    6404             :       // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
    6405          17 :       if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
    6406          17 :         int ImmVal = ARM_AM::getFP32Imm(SplatBits);
    6407          17 :         if (ImmVal != -1) {
    6408           5 :           SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
    6409           5 :           return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
    6410             :         }
    6411             :       }
    6412             :     }
    6413             :   }
    6414             : 
    6415             :   // Scan through the operands to see if only one value is used.
    6416             :   //
    6417             :   // As an optimisation, even if more than one value is used it may be more
    6418             :   // profitable to splat with one value then change some lanes.
    6419             :   //
    6420             :   // Heuristically we decide to do this if the vector has a "dominant" value,
    6421             :   // defined as splatted to more than half of the lanes.
    6422             :   unsigned NumElts = VT.getVectorNumElements();
    6423             :   bool isOnlyLowElement = true;
    6424             :   bool usesOnlyOneValue = true;
    6425             :   bool hasDominantValue = false;
    6426             :   bool isConstant = true;
    6427             : 
    6428             :   // Map of the number of times a particular SDValue appears in the
    6429             :   // element list.
    6430             :   DenseMap<SDValue, unsigned> ValueCounts;
    6431             :   SDValue Value;
    6432        3982 :   for (unsigned i = 0; i < NumElts; ++i) {
    6433        3042 :     SDValue V = Op.getOperand(i);
    6434        6084 :     if (V.isUndef())
    6435         246 :       continue;
    6436        2796 :     if (i > 0)
    6437             :       isOnlyLowElement = false;
    6438             :     if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
    6439             :       isConstant = false;
    6440             : 
    6441        2796 :     ValueCounts.insert(std::make_pair(V, 0));
    6442             :     unsigned &Count = ValueCounts[V];
    6443             : 
    6444             :     // Is this value dominant? (takes up more than half of the lanes)
    6445        2796 :     if (++Count > (NumElts / 2)) {
    6446             :       hasDominantValue = true;
    6447         267 :       Value = V;
    6448             :     }
    6449             :   }
    6450         940 :   if (ValueCounts.size() != 1)
    6451             :     usesOnlyOneValue = false;
    6452         940 :   if (!Value.getNode() && !ValueCounts.empty())
    6453         816 :     Value = ValueCounts.begin()->first;
    6454             : 
    6455         940 :   if (ValueCounts.empty())
    6456           0 :     return DAG.getUNDEF(VT);
    6457             : 
    6458             :   // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
    6459             :   // Keep going if we are hitting this case.
    6460         940 :   if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
    6461          20 :     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
    6462             : 
    6463             :   unsigned EltSize = VT.getScalarSizeInBits();
    6464             : 
    6465             :   // Use VDUP for non-constant splats.  For f32 constant splats, reduce to
    6466             :   // i32 and try again.
    6467         920 :   if (hasDominantValue && EltSize <= 32) {
    6468         123 :     if (!isConstant) {
    6469         102 :       SDValue N;
    6470             : 
    6471             :       // If we are VDUPing a value that comes directly from a vector, that will
    6472             :       // cause an unnecessary move to and from a GPR, where instead we could
    6473             :       // just use VDUPLANE. We can only do this if the lane being extracted
    6474             :       // is at a constant index, as the VDUP from lane instructions only have
    6475             :       // constant-index forms.
    6476             :       ConstantSDNode *constIndex;
    6477         102 :       if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
    6478           2 :           (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
    6479             :         // We need to create a new undef vector to use for the VDUPLANE if the
    6480             :         // size of the vector from which we get the value is different than the
    6481             :         // size of the vector that we need to create. We will insert the element
    6482             :         // such that the register coalescer will remove unnecessary copies.
    6483           2 :         if (VT != Value->getOperand(0).getValueType()) {
    6484           0 :           unsigned index = constIndex->getAPIntValue().getLimitedValue() %
    6485           0 :                              VT.getVectorNumElements();
    6486           0 :           N =  DAG.getNode(ARMISD::VDUPLANE, dl, VT,
    6487             :                  DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
    6488             :                         Value, DAG.getConstant(index, dl, MVT::i32)),
    6489           0 :                            DAG.getConstant(index, dl, MVT::i32));
    6490             :         } else
    6491           1 :           N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
    6492           1 :                         Value->getOperand(0), Value->getOperand(1));
    6493             :       } else
    6494         101 :         N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
    6495             : 
    6496         102 :       if (!usesOnlyOneValue) {
    6497             :         // The dominant value was splatted as 'N', but we now have to insert
    6498             :         // all differing elements.
    6499          15 :         for (unsigned I = 0; I < NumElts; ++I) {
    6500          12 :           if (Op.getOperand(I) == Value)
    6501           9 :             continue;
    6502             :           SmallVector<SDValue, 3> Ops;
    6503           3 :           Ops.push_back(N);
    6504           3 :           Ops.push_back(Op.getOperand(I));
    6505           3 :           Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
    6506           3 :           N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
    6507             :         }
    6508             :       }
    6509         102 :       return N;
    6510             :     }
    6511          21 :     if (VT.getVectorElementType().isFloatingPoint()) {
    6512             :       SmallVector<SDValue, 8> Ops;
    6513          38 :       for (unsigned i = 0; i < NumElts; ++i)
    6514          30 :         Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
    6515          60 :                                   Op.getOperand(i)));
    6516           8 :       EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
    6517           8 :       SDValue Val = DAG.getBuildVector(VecVT, dl, Ops);
    6518           8 :       Val = LowerBUILD_VECTOR(Val, DAG, ST);
    6519           8 :       if (Val.getNode())
    6520           0 :         return DAG.getNode(ISD::BITCAST, dl, VT, Val);
    6521             :     }
    6522          21 :     if (usesOnlyOneValue) {
    6523          19 :       SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
    6524          19 :       if (isConstant && Val.getNode())
    6525           0 :         return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
    6526             :     }
    6527             :   }
    6528             : 
    6529             :   // If all elements are constants and the case above didn't get hit, fall back
    6530             :   // to the default expansion, which will generate a load from the constant
    6531             :   // pool.
    6532         818 :   if (isConstant)
    6533         235 :     return SDValue();
    6534             : 
    6535             :   // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
    6536         583 :   if (NumElts >= 4) {
    6537         157 :     SDValue shuffle = ReconstructShuffle(Op, DAG);
    6538             :     if (shuffle != SDValue())
    6539          20 :       return shuffle;
    6540             :   }
    6541             : 
    6542         563 :   if (VT.is128BitVector() && VT != MVT::v2f64 && VT != MVT::v4f32) {
    6543             :     // If we haven't found an efficient lowering, try splitting a 128-bit vector
    6544             :     // into two 64-bit vectors; we might discover a better way to lower it.
    6545          74 :     SmallVector<SDValue, 64> Ops(Op->op_begin(), Op->op_begin() + NumElts);
    6546          74 :     EVT ExtVT = VT.getVectorElementType();
    6547          74 :     EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElts / 2);
    6548             :     SDValue Lower =
    6549         148 :         DAG.getBuildVector(HVT, dl, makeArrayRef(&Ops[0], NumElts / 2));
    6550          74 :     if (Lower.getOpcode() == ISD::BUILD_VECTOR)
    6551          74 :       Lower = LowerBUILD_VECTOR(Lower, DAG, ST);
    6552             :     SDValue Upper = DAG.getBuildVector(
    6553          74 :         HVT, dl, makeArrayRef(&Ops[NumElts / 2], NumElts / 2));
    6554          74 :     if (Upper.getOpcode() == ISD::BUILD_VECTOR)
    6555          74 :       Upper = LowerBUILD_VECTOR(Upper, DAG, ST);
    6556          74 :     if (Lower && Upper)
    6557          73 :       return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lower, Upper);
    6558             :   }
    6559             : 
    6560             :   // Vectors with 32- or 64-bit elements can be built by directly assigning
    6561             :   // the subregisters.  Lower it to an ARMISD::BUILD_VECTOR so the operands
    6562             :   // will be legalized.
    6563         490 :   if (EltSize >= 32) {
    6564             :     // Do the expansion with floating-point types, since that is what the VFP
    6565             :     // registers are defined to use, and since i64 is not legal.
    6566         483 :     EVT EltVT = EVT::getFloatingPointVT(EltSize);
    6567         483 :     EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
    6568             :     SmallVector<SDValue, 8> Ops;
    6569        1563 :     for (unsigned i = 0; i < NumElts; ++i)
    6570        1080 :       Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
    6571         483 :     SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
    6572         483 :     return DAG.getNode(ISD::BITCAST, dl, VT, Val);
    6573             :   }
    6574             : 
    6575             :   // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
    6576             :   // know the default expansion would otherwise fall back on something even
    6577             :   // worse. For a vector with one or two non-undef values, that's
    6578             :   // scalar_to_vector for the elements followed by a shuffle (provided the
    6579             :   // shuffle is valid for the target) and materialization element by element
    6580             :   // on the stack followed by a load for everything else.
    6581           7 :   if (!isConstant && !usesOnlyOneValue) {
    6582           4 :     SDValue Vec = DAG.getUNDEF(VT);
    6583          24 :     for (unsigned i = 0 ; i < NumElts; ++i) {
    6584          20 :       SDValue V = Op.getOperand(i);
    6585          20 :       if (V.isUndef())
    6586           8 :         continue;
    6587          12 :       SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
    6588          12 :       Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
    6589             :     }
    6590           4 :     return Vec;
    6591             :   }
    6592             : 
    6593           3 :   return SDValue();
    6594             : }
    6595             : 
    6596             : // Gather data to see if the operation can be modelled as a
    6597             : // shuffle in combination with VEXTs.
    6598         157 : SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
    6599             :                                               SelectionDAG &DAG) const {
    6600             :   assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
    6601             :   SDLoc dl(Op);
    6602         157 :   EVT VT = Op.getValueType();
    6603             :   unsigned NumElts = VT.getVectorNumElements();
    6604             : 
    6605             :   struct ShuffleSourceInfo {
    6606             :     SDValue Vec;
    6607             :     unsigned MinElt = std::numeric_limits<unsigned>::max();
    6608             :     unsigned MaxElt = 0;
    6609             : 
    6610             :     // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
    6611             :     // be compatible with the shuffle we intend to construct. As a result
    6612             :     // ShuffleVec will be some sliding window into the original Vec.
    6613             :     SDValue ShuffleVec;
    6614             : 
    6615             :     // Code should guarantee that element i in Vec starts at element "WindowBase
    6616             :     // + i * WindowScale in ShuffleVec".
    6617             :     int WindowBase = 0;
    6618             :     int WindowScale = 1;
    6619             : 
    6620          56 :     ShuffleSourceInfo(SDValue Vec) : Vec(Vec), ShuffleVec(Vec) {}
    6621             : 
    6622           0 :     bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
    6623             :   };
    6624             : 
    6625             :   // First gather all vectors used as an immediate source for this BUILD_VECTOR
    6626             :   // node.
    6627         157 :   SmallVector<ShuffleSourceInfo, 2> Sources;
    6628         407 :   for (unsigned i = 0; i < NumElts; ++i) {
    6629         376 :     SDValue V = Op.getOperand(i);
    6630         376 :     if (V.isUndef())
    6631          98 :       continue;
    6632         278 :     else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
    6633             :       // A shuffle can only come from building a vector from various
    6634             :       // elements of other vectors.
    6635         125 :       return SDValue();
    6636             :     } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
    6637             :       // Furthermore, shuffles require a constant mask, whereas extractelts
    6638             :       // accept variable indices.
    6639           1 :       return SDValue();
    6640             :     }
    6641             : 
    6642             :     // Add this element source to the list if it's not already there.
    6643         152 :     SDValue SourceVec = V.getOperand(0);
    6644         152 :     auto Source = llvm::find(Sources, SourceVec);
    6645         152 :     if (Source == Sources.end())
    6646          56 :       Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
    6647             : 
    6648             :     // Update the minimum and maximum lane number seen.
    6649         152 :     unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
    6650         152 :     Source->MinElt = std::min(Source->MinElt, EltNo);
    6651         251 :     Source->MaxElt = std::max(Source->MaxElt, EltNo);
    6652             :   }
    6653             : 
    6654             :   // Currently only do something sane when at most two source vectors
    6655             :   // are involved.
    6656          31 :   if (Sources.size() > 2)
    6657           0 :     return SDValue();
    6658             : 
    6659             :   // Find out the smallest element size among result and two sources, and use
    6660             :   // it as element size to build the shuffle_vector.
    6661          31 :   EVT SmallestEltTy = VT.getVectorElementType();
    6662          84 :   for (auto &Source : Sources) {
    6663         106 :     EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
    6664          53 :     if (SrcEltTy.bitsLT(SmallestEltTy))
    6665           4 :       SmallestEltTy = SrcEltTy;
    6666             :   }
    6667             :   unsigned ResMultiplier =
    6668          31 :       VT.getScalarSizeInBits() / SmallestEltTy.getSizeInBits();
    6669          31 :   NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
    6670          31 :   EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
    6671             : 
    6672             :   // If the source vector is too wide or too narrow, we may nevertheless be able
    6673             :   // to construct a compatible shuffle either by concatenating it with UNDEF or
    6674             :   // extracting a suitable range of elements.
    6675          79 :   for (auto &Src : Sources) {
    6676          51 :     EVT SrcVT = Src.ShuffleVec.getValueType();
    6677             : 
    6678          51 :     if (SrcVT.getSizeInBits() == VT.getSizeInBits())
    6679          38 :       continue;
    6680             : 
    6681             :     // This stage of the search produces a source with the same element type as
    6682             :     // the original, but with a total width matching the BUILD_VECTOR output.
    6683          21 :     EVT EltVT = SrcVT.getVectorElementType();
    6684          21 :     unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
    6685          21 :     EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
    6686             : 
    6687          21 :     if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
    6688           8 :       if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
    6689           0 :         return SDValue();
    6690             :       // We can pad out the smaller vector for free, so if it's part of a
    6691             :       // shuffle...
    6692           8 :       Src.ShuffleVec =
    6693           8 :           DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
    6694          16 :                       DAG.getUNDEF(Src.ShuffleVec.getValueType()));
    6695           8 :       continue;
    6696             :     }
    6697             : 
    6698          13 :     if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
    6699           0 :       return SDValue();
    6700             : 
    6701          13 :     if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
    6702             :       // Span too large for a VEXT to cope
    6703           3 :       return SDValue();
    6704             :     }
    6705             : 
    6706          10 :     if (Src.MinElt >= NumSrcElts) {
    6707             :       // The extraction can just take the second half
    6708           0 :       Src.ShuffleVec =
    6709           0 :           DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
    6710           0 :                       DAG.getConstant(NumSrcElts, dl, MVT::i32));
    6711           0 :       Src.WindowBase = -NumSrcElts;
    6712          10 :     } else if (Src.MaxElt < NumSrcElts) {
    6713             :       // The extraction can just take the first half
    6714           9 :       Src.ShuffleVec =
    6715           9 :           DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
    6716           9 :                       DAG.getConstant(0, dl, MVT::i32));
    6717             :     } else {
    6718             :       // An actual VEXT is needed
    6719             :       SDValue VEXTSrc1 =
    6720             :           DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
    6721           1 :                       DAG.getConstant(0, dl, MVT::i32));
    6722             :       SDValue VEXTSrc2 =
    6723             :           DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
    6724           1 :                       DAG.getConstant(NumSrcElts, dl, MVT::i32));
    6725             : 
    6726           1 :       Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
    6727             :                                    VEXTSrc2,
    6728           1 :                                    DAG.getConstant(Src.MinElt, dl, MVT::i32));
    6729           1 :       Src.WindowBase = -Src.MinElt;
    6730             :     }
    6731             :   }
    6732             : 
    6733             :   // Another possible incompatibility occurs from the vector element types. We
    6734             :   // can fix this by bitcasting the source vectors to the same type we intend
    6735             :   // for the shuffle.
    6736          76 :   for (auto &Src : Sources) {
    6737          96 :     EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
    6738          48 :     if (SrcEltTy == SmallestEltTy)
    6739          36 :       continue;
    6740             :     assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
    6741          12 :     Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
    6742          12 :     Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
    6743          12 :     Src.WindowBase *= Src.WindowScale;
    6744             :   }
    6745             : 
    6746             :   // Final sanity check before we try to actually produce a shuffle.
    6747             :   LLVM_DEBUG(for (auto Src
    6748             :                   : Sources)
    6749             :                  assert(Src.ShuffleVec.getValueType() == ShuffleVT););
    6750             : 
    6751             :   // The stars all align, our next step is to produce the mask for the shuffle.
    6752          56 :   SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
    6753          28 :   int BitsPerShuffleLane = ShuffleVT.getScalarSizeInBits();
    6754         396 :   for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
    6755         184 :     SDValue Entry = Op.getOperand(i);
    6756         184 :     if (Entry.isUndef())
    6757          52 :       continue;
    6758             : 
    6759         132 :     auto Src = llvm::find(Sources, Entry.getOperand(0));
    6760         132 :     int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
    6761             : 
    6762             :     // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
    6763             :     // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
    6764             :     // segment.
    6765         264 :     EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
    6766         396 :     int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
    6767         132 :                                VT.getScalarSizeInBits());
    6768         132 :     int LanesDefined = BitsDefined / BitsPerShuffleLane;
    6769             : 
    6770             :     // This source is expected to fill ResMultiplier lanes of the final shuffle,
    6771             :     // starting at the appropriate offset.
    6772         132 :     int *LaneMask = &Mask[i * ResMultiplier];
    6773             : 
    6774         132 :     int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
    6775         264 :     ExtractBase += NumElts * (Src - Sources.begin());
    6776         264 :     for (int j = 0; j < LanesDefined; ++j)
    6777         132 :       LaneMask[j] = ExtractBase + j;
    6778             :   }
    6779             : 
    6780             :   // Final check before we try to produce nonsense...
    6781          56 :   if (!isShuffleMaskLegal(Mask, ShuffleVT))
    6782           8 :     return SDValue();
    6783             : 
    6784             :   // We can't handle more than two sources. This should have already
    6785             :   // been checked before this point.
    6786             :   assert(Sources.size() <= 2 && "Too many sources!");
    6787             : 
    6788          20 :   SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
    6789          52 :   for (unsigned i = 0; i < Sources.size(); ++i)
    6790          32 :     ShuffleOps[i] = Sources[i].ShuffleVec;
    6791             : 
    6792             :   SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
    6793          20 :                                          ShuffleOps[1], Mask);
    6794          20 :   return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
    6795             : }
    6796             : 
    6797             : /// isShuffleMaskLegal - Targets can use this to indicate that they only
    6798             : /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
    6799             : /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
    6800             : /// are assumed to be legal.
    6801          93 : bool ARMTargetLowering::isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const {
    6802          93 :   if (VT.getVectorNumElements() == 4 &&
    6803          25 :       (VT.is128BitVector() || VT.is64BitVector())) {
    6804             :     unsigned PFIndexes[4];
    6805             :     for (unsigned i = 0; i != 4; ++i) {
    6806             :       if (M[i] < 0)
    6807             :         PFIndexes[i] = 8;
    6808             :       else
    6809             :         PFIndexes[i] = M[i];
    6810             :     }
    6811             : 
    6812             :     // Compute the index in the perfect shuffle table.
    6813             :     unsigned PFTableIndex =
    6814             :       PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
    6815             :     unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
    6816             :     unsigned Cost = (PFEntry >> 30);
    6817             : 
    6818             :     if (Cost <= 4)
    6819             :       return true;
    6820             :   }
    6821             : 
    6822             :   bool ReverseVEXT, isV_UNDEF;
    6823             :   unsigned Imm, WhichResult;
    6824             : 
    6825             :   unsigned EltSize = VT.getScalarSizeInBits();
    6826          44 :   return (EltSize >= 32 ||
    6827          86 :           ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
    6828          84 :           isVREVMask(M, VT, 64) ||
    6829          83 :           isVREVMask(M, VT, 32) ||
    6830          80 :           isVREVMask(M, VT, 16) ||
    6831          39 :           isVEXTMask(M, VT, ReverseVEXT, Imm) ||
    6832          64 :           isVTBLMask(M, VT) ||
    6833          96 :           isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
    6834          28 :           ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
    6835             : }
    6836             : 
    6837             : /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
    6838             : /// the specified operations to build the shuffle.
    6839         100 : static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
    6840             :                                       SDValue RHS, SelectionDAG &DAG,
    6841             :                                       const SDLoc &dl) {
    6842         100 :   unsigned OpNum = (PFEntry >> 26) & 0x0F;
    6843         100 :   unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
    6844         100 :   unsigned RHSID = (PFEntry >>  0) & ((1 << 13)-1);
    6845             : 
    6846             :   enum {
    6847             :     OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
    6848             :     OP_VREV,
    6849             :     OP_VDUP0,
    6850             :     OP_VDUP1,
    6851             :     OP_VDUP2,
    6852             :     OP_VDUP3,
    6853             :     OP_VEXT1,
    6854             :     OP_VEXT2,
    6855             :     OP_VEXT3,
    6856             :     OP_VUZPL, // VUZP, left result
    6857             :     OP_VUZPR, // VUZP, right result
    6858             :     OP_VZIPL, // VZIP, left result
    6859             :     OP_VZIPR, // VZIP, right result
    6860             :     OP_VTRNL, // VTRN, left result
    6861             :     OP_VTRNR  // VTRN, right result
    6862             :   };
    6863             : 
    6864         100 :   if (OpNum == OP_COPY) {
    6865          58 :     if (LHSID == (1*9+2)*9+3) return LHS;
    6866             :     assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
    6867          13 :     return RHS;
    6868             :   }
    6869             : 
    6870             :   SDValue OpLHS, OpRHS;
    6871          42 :   OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
    6872          42 :   OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
    6873          42 :   EVT VT = OpLHS.getValueType();
    6874             : 
    6875          42 :   switch (OpNum) {
    6876           0 :   default: llvm_unreachable("Unknown shuffle opcode!");
    6877             :   case OP_VREV:
    6878             :     // VREV divides the vector in half and swaps within the half.
    6879           3 :     if (VT.getVectorElementType() == MVT::i32 ||
    6880           4 :         VT.getVectorElementType() == MVT::f32)
    6881           3 :       return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
    6882             :     // vrev <4 x i16> -> VREV32
    6883           0 :     if (VT.getVectorElementType() == MVT::i16)
    6884           0 :       return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
    6885             :     // vrev <4 x i8> -> VREV16
    6886             :     assert(VT.getVectorElementType() == MVT::i8);
    6887           0 :     return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
    6888             :   case OP_VDUP0:
    6889             :   case OP_VDUP1:
    6890             :   case OP_VDUP2:
    6891             :   case OP_VDUP3:
    6892             :     return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
    6893           7 :                        OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
    6894             :   case OP_VEXT1:
    6895             :   case OP_VEXT2:
    6896             :   case OP_VEXT3:
    6897             :     return DAG.getNode(ARMISD::VEXT, dl, VT,
    6898             :                        OpLHS, OpRHS,
    6899          17 :                        DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
    6900           6 :   case OP_VUZPL:
    6901             :   case OP_VUZPR:
    6902           6 :     return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
    6903           6 :                        OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
    6904           8 :   case OP_VZIPL:
    6905             :   case OP_VZIPR:
    6906           8 :     return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
    6907           8 :                        OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
    6908           1 :   case OP_VTRNL:
    6909             :   case OP_VTRNR:
    6910           1 :     return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
    6911           1 :                        OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
    6912             :   }
    6913             : }
    6914             : 
    6915           0 : static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
    6916             :                                        ArrayRef<int> ShuffleMask,
    6917             :                                        SelectionDAG &DAG) {
    6918             :   // Check to see if we can use the VTBL instruction.
    6919           0 :   SDValue V1 = Op.getOperand(0);
    6920           0 :   SDValue V2 = Op.getOperand(1);
    6921           0 :   SDLoc DL(Op);
    6922             : 
    6923             :   SmallVector<SDValue, 8> VTBLMask;
    6924           0 :   for (ArrayRef<int>::iterator
    6925           0 :          I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
    6926           0 :     VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
    6927             : 
    6928           0 :   if (V2.getNode()->isUndef())
    6929             :     return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
    6930           0 :                        DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
    6931             : 
    6932             :   return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
    6933           0 :                      DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
    6934             : }
    6935             : 
    6936           0 : static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
    6937             :                                                       SelectionDAG &DAG) {
    6938           0 :   SDLoc DL(Op);
    6939           0 :   SDValue OpLHS = Op.getOperand(0);
    6940           0 :   EVT VT = OpLHS.getValueType();
    6941             : 
    6942             :   assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
    6943             :          "Expect an v8i16/v16i8 type");
    6944           0 :   OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
    6945             :   // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
    6946             :   // extract the first 8 bytes into the top double word and the last 8 bytes
    6947             :   // into the bottom double word. The v8i16 case is similar.
    6948           0 :   unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
    6949             :   return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
    6950           0 :                      DAG.getConstant(ExtractNum, DL, MVT::i32));
    6951             : }
    6952             : 
    6953           0 : static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
    6954           0 :   SDValue V1 = Op.getOperand(0);
    6955           0 :   SDValue V2 = Op.getOperand(1);
    6956           0 :   SDLoc dl(Op);
    6957           0 :   EVT VT = Op.getValueType();
    6958             :   ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
    6959             : 
    6960             :   // Convert shuffles that are directly supported on NEON to target-specific
    6961             :   // DAG nodes, instead of keeping them as shuffles and matching them again
    6962             :   // during code selection.  This is more efficient and avoids the possibility
    6963             :   // of inconsistencies between legalization and selection.
    6964             :   // FIXME: floating-point vectors should be canonicalized to integer vectors
    6965             :   // of the same time so that they get CSEd properly.
    6966           0 :   ArrayRef<int> ShuffleMask = SVN->getMask();
    6967             : 
    6968             :   unsigned EltSize = VT.getScalarSizeInBits();
    6969           0 :   if (EltSize <= 32) {
    6970           0 :     if (SVN->isSplat()) {
    6971           0 :       int Lane = SVN->getSplatIndex();
    6972             :       // If this is undef splat, generate it via "just" vdup, if possible.
    6973           0 :       if (Lane == -1) Lane = 0;
    6974             : 
    6975             :       // Test if V1 is a SCALAR_TO_VECTOR.
    6976           0 :       if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
    6977           0 :         return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
    6978             :       }
    6979             :       // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
    6980             :       // (and probably will turn into a SCALAR_TO_VECTOR once legalization
    6981             :       // reaches it).
    6982           0 :       if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
    6983             :           !isa<ConstantSDNode>(V1.getOperand(0))) {
    6984             :         bool IsScalarToVector = true;
    6985           0 :         for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
    6986           0 :           if (!V1.getOperand(i).isUndef()) {
    6987             :             IsScalarToVector = false;
    6988             :             break;
    6989             :           }
    6990           0 :         if (IsScalarToVector)
    6991           0 :           return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
    6992             :       }
    6993             :       return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
    6994           0 :                          DAG.getConstant(Lane, dl, MVT::i32));
    6995             :     }
    6996             : 
    6997             :     bool ReverseVEXT;
    6998             :     unsigned Imm;
    6999           0 :     if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
    7000           0 :       if (ReverseVEXT)
    7001             :         std::swap(V1, V2);
    7002             :       return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
    7003           0 :                          DAG.getConstant(Imm, dl, MVT::i32));
    7004             :     }
    7005             : 
    7006           0 :     if (isVREVMask(ShuffleMask, VT, 64))
    7007           0 :       return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
    7008           0 :     if (isVREVMask(ShuffleMask, VT, 32))
    7009           0 :       return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
    7010           0 :     if (isVREVMask(ShuffleMask, VT, 16))
    7011           0 :       return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
    7012             : 
    7013           0 :     if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
    7014             :       return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
    7015           0 :                          DAG.getConstant(Imm, dl, MVT::i32));
    7016             :     }
    7017             : 
    7018             :     // Check for Neon shuffles that modify both input vectors in place.
    7019             :     // If both results are used, i.e., if there are two shuffles with the same
    7020             :     // source operands and with masks corresponding to both results of one of
    7021             :     // these operations, DAG memoization will ensure that a single node is
    7022             :     // used for both shuffles.
    7023             :     unsigned WhichResult;
    7024             :     bool isV_UNDEF;
    7025           0 :     if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
    7026             :             ShuffleMask, VT, WhichResult, isV_UNDEF)) {
    7027           0 :       if (isV_UNDEF)
    7028           0 :         V2 = V1;
    7029           0 :       return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
    7030           0 :           .getValue(WhichResult);
    7031             :     }
    7032             : 
    7033             :     // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
    7034             :     // shuffles that produce a result larger than their operands with:
    7035             :     //   shuffle(concat(v1, undef), concat(v2, undef))
    7036             :     // ->
    7037             :     //   shuffle(concat(v1, v2), undef)
    7038             :     // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
    7039             :     //
    7040             :     // This is useful in the general case, but there are special cases where
    7041             :     // native shuffles produce larger results: the two-result ops.
    7042             :     //
    7043             :     // Look through the concat when lowering them:
    7044             :     //   shuffle(concat(v1, v2), undef)
    7045             :     // ->
    7046             :     //   concat(VZIP(v1, v2):0, :1)
    7047             :     //
    7048           0 :     if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
    7049           0 :       SDValue SubV1 = V1->getOperand(0);
    7050           0 :       SDValue SubV2 = V1->getOperand(1);
    7051           0 :       EVT SubVT = SubV1.getValueType();
    7052             : 
    7053             :       // We expect these to have been canonicalized to -1.
    7054             :       assert(llvm::all_of(ShuffleMask, [&](int i) {
    7055             :         return i < (int)VT.getVectorNumElements();
    7056             :       }) && "Unexpected shuffle index into UNDEF operand!");
    7057             : 
    7058           0 :       if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
    7059             :               ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
    7060           0 :         if (isV_UNDEF)
    7061           0 :           SubV2 = SubV1;
    7062             :         assert((WhichResult == 0) &&
    7063             :                "In-place shuffle of concat can only have one result!");
    7064             :         SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
    7065           0 :                                   SubV1, SubV2);
    7066             :         return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
    7067           0 :                            Res.getValue(1));
    7068             :       }
    7069             :     }
    7070             :   }
    7071             : 
    7072             :   // If the shuffle is not directly supported and it has 4 elements, use
    7073             :   // the PerfectShuffle-generated table to synthesize it from other shuffles.
    7074             :   unsigned NumElts = VT.getVectorNumElements();
    7075           0 :   if (NumElts == 4) {
    7076             :     unsigned PFIndexes[4];
    7077           0 :     for (unsigned i = 0; i != 4; ++i) {
    7078           0 :       if (ShuffleMask[i] < 0)
    7079           0 :         PFIndexes[i] = 8;
    7080             :       else
    7081           0 :         PFIndexes[i] = ShuffleMask[i];
    7082             :     }
    7083             : 
    7084             :     // Compute the index in the perfect shuffle table.
    7085           0 :     unsigned PFTableIndex =
    7086           0 :       PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
    7087           0 :     unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
    7088             :     unsigned Cost = (PFEntry >> 30);
    7089             : 
    7090             :     if (Cost <= 4)
    7091           0 :       return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
    7092             :   }
    7093             : 
    7094             :   // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
    7095           0 :   if (EltSize >= 32) {
    7096             :     // Do the expansion with floating-point types, since that is what the VFP
    7097             :     // registers are defined to use, and since i64 is not legal.
    7098           0 :     EVT EltVT = EVT::getFloatingPointVT(EltSize);
    7099           0 :     EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
    7100           0 :     V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
    7101           0 :     V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
    7102             :     SmallVector<SDValue, 8> Ops;
    7103           0 :     for (unsigned i = 0; i < NumElts; ++i) {
    7104           0 :       if (ShuffleMask[i] < 0)
    7105           0 :         Ops.push_back(DAG.getUNDEF(EltVT));
    7106             :       else
    7107           0 :         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
    7108           0 :                                   ShuffleMask[i] < (int)NumElts ? V1 : V2,
    7109           0 :                                   DAG.getConstant(ShuffleMask[i] & (NumElts-1),
    7110           0 :                                                   dl, MVT::i32)));
    7111             :     }
    7112           0 :     SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
    7113           0 :     return DAG.getNode(ISD::BITCAST, dl, VT, Val);
    7114             :   }
    7115             : 
    7116           0 :   if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
    7117           0 :     return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
    7118             : 
    7119           0 :   if (VT == MVT::v8i8)
    7120           0 :     if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
    7121           0 :       return NewOp;
    7122             : 
    7123           0 :   return SDValue();
    7124             : }
    7125             : 
    7126           0 : static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
    7127             :   // INSERT_VECTOR_ELT is legal only for immediate indexes.
    7128         938 :   SDValue Lane = Op.getOperand(2);
    7129             :   if (!isa<ConstantSDNode>(Lane))
    7130           0 :     return SDValue();
    7131             : 
    7132           0 :   return Op;
    7133             : }
    7134             : 
    7135        5529 : static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
    7136             :   // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
    7137        5529 :   SDValue Lane = Op.getOperand(1);
    7138             :   if (!isa<ConstantSDNode>(Lane))
    7139           8 :     return SDValue();
    7140             : 
    7141        5521 :   SDValue Vec = Op.getOperand(0);
    7142         847 :   if (Op.getValueType() == MVT::i32 && Vec.getScalarValueSizeInBits() < 32) {
    7143             :     SDLoc dl(Op);
    7144          77 :     return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
    7145             :   }
    7146             : 
    7147        5444 :   return Op;
    7148             : }
    7149             : 
    7150           0 : static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
    7151             :   // The only time a CONCAT_VECTORS operation can have legal types is when
    7152             :   // two 64-bit vectors are concatenated to a 128-bit vector.
    7153             :   assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
    7154             :          "unexpected CONCAT_VECTORS");
    7155           0 :   SDLoc dl(Op);
    7156           0 :   SDValue Val = DAG.getUNDEF(MVT::v2f64);
    7157           0 :   SDValue Op0 = Op.getOperand(0);
    7158           0 :   SDValue Op1 = Op.getOperand(1);
    7159           0 :   if (!Op0.isUndef())
    7160           0 :     Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
    7161             :                       DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
    7162           0 :                       DAG.getIntPtrConstant(0, dl));
    7163           0 :   if (!Op1.isUndef())
    7164           0 :     Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
    7165             :                       DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
    7166           0 :                       DAG.getIntPtrConstant(1, dl));
    7167           0 :   return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
    7168             : }
    7169             : 
    7170             : /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
    7171             : /// element has been zero/sign-extended, depending on the isSigned parameter,
    7172             : /// from an integer type half its size.
    7173         518 : static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
    7174             :                                    bool isSigned) {
    7175             :   // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
    7176        1036 :   EVT VT = N->getValueType(0);
    7177          18 :   if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
    7178           5 :     SDNode *BVN = N->getOperand(0).getNode();
    7179           5 :     if (BVN->getValueType(0) != MVT::v4i32 ||
    7180           5 :         BVN->getOpcode() != ISD::BUILD_VECTOR)
    7181             :       return false;
    7182           5 :     unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
    7183           5 :     unsigned HiElt = 1 - LoElt;
    7184           5 :     ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
    7185             :     ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
    7186           5 :     ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
    7187           5 :     ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
    7188           5 :     if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
    7189             :       return false;
    7190           5 :     if (isSigned) {
    7191          12 :       if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
    7192          12 :           Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
    7193             :         return true;
    7194             :     } else {
    7195           3 :       if (Hi0->isNullValue() && Hi1->isNullValue())
    7196             :         return true;
    7197             :     }
    7198           0 :     return false;
    7199             :   }
    7200             : 
    7201         513 :   if (N->getOpcode() != ISD::BUILD_VECTOR)
    7202             :     return false;
    7203             : 
    7204          87 :   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
    7205         156 :     SDNode *Elt = N->getOperand(i).getNode();
    7206             :     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
    7207             :       unsigned EltSize = VT.getScalarSizeInBits();
    7208          66 :       unsigned HalfSize = EltSize / 2;
    7209          66 :       if (isSigned) {
    7210          34 :         if (!isIntN(HalfSize, C->getSExtValue()))
    7211             :           return false;
    7212             :       } else {
    7213          32 :         if (!isUIntN(HalfSize, C->getZExtValue()))
    7214             :           return false;
    7215             :       }
    7216             :       continue;
    7217             :     }
    7218             :     return false;
    7219             :   }
    7220             : 
    7221             :   return true;
    7222             : }
    7223             : 
    7224             : /// isSignExtended - Check if a node is a vector value that is sign-extended
    7225             : /// or a constant BUILD_VECTOR with sign-extended elements.
    7226         354 : static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
    7227         354 :   if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
    7228             :     return true;
    7229         274 :   if (isExtendedBUILD_VECTOR(N, DAG, true))
    7230           9 :     return true;
    7231             :   return false;
    7232             : }
    7233             : 
    7234             : /// isZeroExtended - Check if a node is a vector value that is zero-extended
    7235             : /// or a constant BUILD_VECTOR with zero-extended elements.
    7236         292 : static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
    7237         292 :   if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
    7238             :     return true;
    7239         244 :   if (isExtendedBUILD_VECTOR(N, DAG, false))
    7240           5 :     return true;
    7241             :   return false;
    7242             : }
    7243             : 
    7244          59 : static EVT getExtensionTo64Bits(const EVT &OrigVT) {
    7245          59 :   if (OrigVT.getSizeInBits() >= 64)
    7246          46 :     return OrigVT;
    7247             : 
    7248             :   assert(OrigVT.isSimple() && "Expecting a simple value type");
    7249             : 
    7250             :   MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
    7251          13 :   switch (OrigSimpleTy) {
    7252           0 :   default: llvm_unreachable("Unexpected Vector Type");
    7253             :   case MVT::v2i8:
    7254             :   case MVT::v2i16:
    7255           9 :      return MVT::v2i32;
    7256             :   case MVT::v4i8:
    7257           4 :     return  MVT::v4i16;
    7258             :   }
    7259             : }
    7260             : 
    7261             : /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
    7262             : /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
    7263             : /// We insert the required extension here to get the vector to fill a D register.
    7264           0 : static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
    7265             :                                             const EVT &OrigTy,
    7266             :                                             const EVT &ExtTy,
    7267             :                                             unsigned ExtOpcode) {
    7268             :   // The vector originally had a size of OrigTy. It was then extended to ExtTy.
    7269             :   // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
    7270             :   // 64-bits we need to insert a new extension so that it will be 64-bits.
    7271             :   assert(ExtTy.is128BitVector() && "Unexpected extension size");
    7272           0 :   if (OrigTy.getSizeInBits() >= 64)
    7273           0 :     return N;
    7274             : 
    7275             :   // Must extend size to at least 64 bits to be used as an operand for VMULL.
    7276           0 :   EVT NewVT = getExtensionTo64Bits(OrigTy);
    7277             : 
    7278           0 :   return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
    7279             : }
    7280             : 
    7281             : /// SkipLoadExtensionForVMULL - return a load of the original vector size that
    7282             : /// does not do any sign/zero extension. If the original vector is less
    7283             : /// than 64 bits, an appropriate extension will be added after the load to
    7284             : /// reach a total size of 64 bits. We have to add the extension separately
    7285             : /// because ARM does not have a sign/zero extending load for vectors.
    7286          59 : static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
    7287          59 :   EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
    7288             : 
    7289             :   // The load already has the right type.
    7290           0 :   if (ExtendedTy == LD->getMemoryVT())
    7291          46 :     return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
    7292          46 :                        LD->getBasePtr(), LD->getPointerInfo(),
    7293         138 :                        LD->getAlignment(), LD->getMemOperand()->getFlags());
    7294             : 
    7295             :   // We need to create a zextload/sextload. We cannot just create a load
    7296             :   // followed by a zext/zext node because LowerMUL is also run during normal
    7297             :   // operation legalization where we can't create illegal types.
    7298          13 :   return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
    7299          13 :                         LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
    7300             :                         LD->getMemoryVT(), LD->getAlignment(),
    7301          39 :                         LD->getMemOperand()->getFlags());
    7302             : }
    7303             : 
    7304             : /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
    7305             : /// extending load, or BUILD_VECTOR with extended elements, return the
    7306             : /// unextended value. The unextended vector should be 64 bits so that it can
    7307             : /// be used as an operand to a VMULL instruction. If the original vector size
    7308             : /// before extension is less than 64 bits we add a an extension to resize
    7309             : /// the vector to 64 bits.
    7310         106 : static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
    7311         212 :   if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
    7312             :     return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
    7313          76 :                                         N->getOperand(0)->getValueType(0),
    7314             :                                         N->getValueType(0),
    7315          38 :                                         N->getOpcode());
    7316             : 
    7317             :   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
    7318             :     assert((ISD::isSEXTLoad(LD) || ISD::isZEXTLoad(LD)) &&
    7319             :            "Expected extending load");
    7320             : 
    7321          59 :     SDValue newLoad = SkipLoadExtensionForVMULL(LD, DAG);
    7322          59 :     DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), newLoad.getValue(1));
    7323             :     unsigned Opcode = ISD::isSEXTLoad(LD) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
    7324             :     SDValue extLoad =
    7325         118 :         DAG.getNode(Opcode, SDLoc(newLoad), LD->getValueType(0), newLoad);
    7326          59 :     DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 0), extLoad);
    7327             : 
    7328          59 :     return newLoad;
    7329             :   }
    7330             : 
    7331             :   // Otherwise, the value must be a BUILD_VECTOR.  For v2i64, it will
    7332             :   // have been legalized as a BITCAST from v4i32.
    7333           9 :   if (N->getOpcode() == ISD::BITCAST) {
    7334           4 :     SDNode *BVN = N->getOperand(0).getNode();
    7335             :     assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
    7336             :            BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
    7337           4 :     unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
    7338             :     return DAG.getBuildVector(
    7339           4 :         MVT::v2i32, SDLoc(N),
    7340          12 :         {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)});
    7341             :   }
    7342             :   // Construct a new BUILD_VECTOR with elements truncated to half the size.
    7343             :   assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
    7344          10 :   EVT VT = N->getValueType(0);
    7345           5 :   unsigned EltSize = VT.getScalarSizeInBits() / 2;
    7346             :   unsigned NumElts = VT.getVectorNumElements();
    7347           5 :   MVT TruncVT = MVT::getIntegerVT(EltSize);
    7348             :   SmallVector<SDValue, 8> Ops;
    7349             :   SDLoc dl(N);
    7350          33 :   for (unsigned i = 0; i != NumElts; ++i) {
    7351          28 :     ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
    7352          28 :     const APInt &CInt = C->getAPIntValue();
    7353             :     // Element types smaller than 32 bits are not legal, so use i32 elements.
    7354             :     // The values are implicitly truncated so sext vs. zext doesn't matter.
    7355          28 :     Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
    7356             :   }
    7357           5 :   return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops);
    7358             : }
    7359             : 
    7360          16 : static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
    7361          16 :   unsigned Opcode = N->getOpcode();
    7362          16 :   if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
    7363           0 :     SDNode *N0 = N->getOperand(0).getNode();
    7364           0 :     SDNode *N1 = N->getOperand(1).getNode();
    7365           0 :     return N0->hasOneUse() && N1->hasOneUse() &&
    7366           0 :       isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
    7367             :   }
    7368             :   return false;
    7369             : }
    7370             : 
    7371           6 : static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
    7372           6 :   unsigned Opcode = N->getOpcode();
    7373           6 :   if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
    7374           0 :     SDNode *N0 = N->getOperand(0).getNode();
    7375           0 :     SDNode *N1 = N->getOperand(1).getNode();
    7376           0 :     return N0->hasOneUse() && N1->hasOneUse() &&
    7377           0 :       isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
    7378             :   }
    7379             :   return false;
    7380             : }
    7381             : 
    7382         177 : static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
    7383             :   // Multiplications are only custom-lowered for 128-bit vectors so that
    7384             :   // VMULL can be detected.  Otherwise v2i64 multiplications are not legal.
    7385         177 :   EVT VT = Op.getValueType();
    7386             :   assert(VT.is128BitVector() && VT.isInteger() &&
    7387             :          "unexpected type for custom-lowering ISD::MUL");
    7388         177 :   SDNode *N0 = Op.getOperand(0).getNode();
    7389         177 :   SDNode *N1 = Op.getOperand(1).getNode();
    7390             :   unsigned NewOpc = 0;
    7391             :   bool isMLA = false;
    7392         177 :   bool isN0SExt = isSignExtended(N0, DAG);
    7393         177 :   bool isN1SExt = isSignExtended(N1, DAG);
    7394         177 :   if (isN0SExt && isN1SExt)
    7395             :     NewOpc = ARMISD::VMULLs;
    7396             :   else {
    7397         146 :     bool isN0ZExt = isZeroExtended(N0, DAG);
    7398         146 :     bool isN1ZExt = isZeroExtended(N1, DAG);
    7399         146 :     if (isN0ZExt && isN1ZExt)
    7400             :       NewOpc = ARMISD::VMULLu;
    7401         124 :     else if (isN1SExt || isN1ZExt) {
    7402             :       // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
    7403             :       // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
    7404          18 :       if (isN1SExt && isAddSubSExt(N0, DAG)) {
    7405             :         NewOpc = ARMISD::VMULLs;
    7406             :         isMLA = true;
    7407          18 :       } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
    7408             :         NewOpc = ARMISD::VMULLu;
    7409             :         isMLA = true;
    7410          18 :       } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
    7411             :         std::swap(N0, N1);
    7412             :         NewOpc = ARMISD::VMULLu;
    7413             :         isMLA = true;
    7414             :       }
    7415             :     }
    7416             : 
    7417         146 :     if (!NewOpc) {
    7418             :       if (VT == MVT::v2i64)
    7419             :         // Fall through to expand this.  It is not legal.
    7420           0 :         return SDValue();
    7421             :       else
    7422             :         // Other vector multiplications are legal.
    7423         124 :         return Op;
    7424             :     }
    7425             :   }
    7426             : 
    7427             :   // Legalize to a VMULL instruction.
    7428             :   SDLoc DL(Op);
    7429          53 :   SDValue Op0;
    7430          53 :   SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
    7431          53 :   if (!isMLA) {
    7432          53 :     Op0 = SkipExtensionForVMULL(N0, DAG);
    7433             :     assert(Op0.getValueType().is64BitVector() &&
    7434             :            Op1.getValueType().is64BitVector() &&
    7435             :            "unexpected types for extended operands to VMULL");
    7436          53 :     return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
    7437             :   }
    7438             : 
    7439             :   // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
    7440             :   // isel lowering to take advantage of no-stall back to back vmul + vmla.
    7441             :   //   vmull q0, d4, d6
    7442             :   //   vmlal q0, d5, d6
    7443             :   // is faster than
    7444             :   //   vaddl q0, d4, d5
    7445             :   //   vmovl q1, d6
    7446             :   //   vmul  q0, q0, q1
    7447           0 :   SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
    7448           0 :   SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
    7449           0 :   EVT Op1VT = Op1.getValueType();
    7450             :   return DAG.getNode(N0->getOpcode(), DL, VT,
    7451             :                      DAG.getNode(NewOpc, DL, VT,
    7452             :                                DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
    7453             :                      DAG.getNode(NewOpc, DL, VT,
    7454           0 :                                DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
    7455             : }
    7456             : 
    7457           4 : static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl,
    7458             :                               SelectionDAG &DAG) {
    7459             :   // TODO: Should this propagate fast-math-flags?
    7460             : 
    7461             :   // Convert to float
    7462             :   // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
    7463             :   // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
    7464           4 :   X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
    7465           4 :   Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
    7466           4 :   X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
    7467           4 :   Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
    7468             :   // Get reciprocal estimate.
    7469             :   // float4 recip = vrecpeq_f32(yf);
    7470           4 :   Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
    7471             :                    DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
    7472           4 :                    Y);
    7473             :   // Because char has a smaller range than uchar, we can actually get away
    7474             :   // without any newton steps.  This requires that we use a weird bias
    7475             :   // of 0xb000, however (again, this has been exhaustively tested).
    7476             :   // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
    7477           4 :   X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
    7478           4 :   X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
    7479           4 :   Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
    7480           4 :   X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
    7481           4 :   X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
    7482             :   // Convert back to short.
    7483           4 :   X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
    7484           4 :   X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
    7485           4 :   return X;
    7486             : }
    7487             : 
    7488           8 : static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl,
    7489             :                                SelectionDAG &DAG) {
    7490             :   // TODO: Should this propagate fast-math-flags?
    7491             : 
    7492             :   SDValue N2;
    7493             :   // Convert to float.
    7494             :   // float4 yf = vcvt_f32_s32(vmovl_s16(y));
    7495             :   // float4 xf = vcvt_f32_s32(vmovl_s16(x));
    7496           8 :   N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
    7497           8 :   N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
    7498           8 :   N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
    7499           8 :   N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
    7500             : 
    7501             :   // Use reciprocal estimate and one refinement step.
    7502             :   // float4 recip = vrecpeq_f32(yf);
    7503             :   // recip *= vrecpsq_f32(yf, recip);
    7504           8 :   N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
    7505             :                    DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
    7506           8 :                    N1);
    7507           8 :   N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
    7508             :                    DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
    7509           8 :                    N1, N2);
    7510           8 :   N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
    7511             :   // Because short has a smaller range than ushort, we can actually get away
    7512             :   // with only a single newton step.  This requires that we use a weird bias
    7513             :   // of 89, however (again, this has been exhaustively tested).
    7514             :   // float4 result = as_float4(as_int4(xf*recip) + 0x89);
    7515           8 :   N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
    7516           8 :   N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
    7517           8 :   N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
    7518           8 :   N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
    7519           8 :   N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
    7520             :   // Convert back to integer and return.
    7521             :   // return vmovn_s32(vcvt_s32_f32(result));
    7522           8 :   N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
    7523           8 :   N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
    7524           8 :   return N0;
    7525             : }
    7526             : 
    7527           0 : static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
    7528             :   EVT VT = Op.getValueType();
    7529             :   assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
    7530             :          "unexpected type for custom-lowering ISD::SDIV");
    7531             : 
    7532           0 :   SDLoc dl(Op);
    7533           0 :   SDValue N0 = Op.getOperand(0);
    7534           0 :   SDValue N1 = Op.getOperand(1);
    7535           0 :   SDValue N2, N3;
    7536             : 
    7537           0 :   if (VT == MVT::v8i8) {
    7538           0 :     N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
    7539           0 :     N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
    7540             : 
    7541           0 :     N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
    7542           0 :                      DAG.getIntPtrConstant(4, dl));
    7543           0 :     N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
    7544           0 :                      DAG.getIntPtrConstant(4, dl));
    7545           0 :     N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
    7546           0 :                      DAG.getIntPtrConstant(0, dl));
    7547           0 :     N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
    7548           0 :                      DAG.getIntPtrConstant(0, dl));
    7549             : 
    7550           0 :     N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
    7551           0 :     N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
    7552             : 
    7553           0 :     N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
    7554           0 :     N0 = LowerCONCAT_VECTORS(N0, DAG);
    7555             : 
    7556           0 :     N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
    7557           0 :     return N0;
    7558             :   }
    7559           0 :   return LowerSDIV_v4i16(N0, N1, dl, DAG);
    7560             : }
    7561             : 
    7562           0 : static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
    7563             :   // TODO: Should this propagate fast-math-flags?
    7564             :   EVT VT = Op.getValueType();
    7565             :   assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
    7566             :          "unexpected type for custom-lowering ISD::UDIV");
    7567             : 
    7568           0 :   SDLoc dl(Op);
    7569           0 :   SDValue N0 = Op.getOperand(0);
    7570           0 :   SDValue N1 = Op.getOperand(1);
    7571           0 :   SDValue N2, N3;
    7572             : 
    7573           0 :   if (VT == MVT::v8i8) {
    7574           0 :     N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
    7575           0 :     N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
    7576             : 
    7577           0 :     N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
    7578           0 :                      DAG.getIntPtrConstant(4, dl));
    7579           0 :     N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
    7580           0 :                      DAG.getIntPtrConstant(4, dl));
    7581           0 :     N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
    7582           0 :                      DAG.getIntPtrConstant(0, dl));
    7583           0 :     N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
    7584           0 :                      DAG.getIntPtrConstant(0, dl));
    7585             : 
    7586           0 :     N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
    7587           0 :     N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
    7588             : 
    7589           0 :     N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
    7590           0 :     N0 = LowerCONCAT_VECTORS(N0, DAG);
    7591             : 
    7592           0 :     N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
    7593             :                      DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
    7594             :                                      MVT::i32),
    7595           0 :                      N0);
    7596           0 :     return N0;
    7597             :   }
    7598             : 
    7599             :   // v4i16 sdiv ... Convert to float.
    7600             :   // float4 yf = vcvt_f32_s32(vmovl_u16(y));
    7601             :   // float4 xf = vcvt_f32_s32(vmovl_u16(x));
    7602           0 :   N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
    7603           0 :   N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
    7604           0 :   N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
    7605           0 :   SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
    7606             : 
    7607             :   // Use reciprocal estimate and two refinement steps.
    7608             :   // float4 recip = vrecpeq_f32(yf);
    7609             :   // recip *= vrecpsq_f32(yf, recip);
    7610             :   // recip *= vrecpsq_f32(yf, recip);
    7611           0 :   N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
    7612             :                    DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
    7613           0 :                    BN1);
    7614           0 :   N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
    7615             :                    DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
    7616           0 :                    BN1, N2);
    7617           0 :   N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
    7618           0 :   N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
    7619             :                    DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
    7620           0 :                    BN1, N2);
    7621           0 :   N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
    7622             :   // Simply multiplying by the reciprocal estimate can leave us a few ulps
    7623             :   // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
    7624             :   // and that it will never cause us to return an answer too large).
    7625             :   // float4 result = as_float4(as_int4(xf*recip) + 2);
    7626           0 :   N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
    7627           0 :   N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
    7628           0 :   N1 = DAG.getConstant(2, dl, MVT::v4i32);
    7629           0 :   N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
    7630           0 :   N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
    7631             :   // Convert back to integer and return.
    7632             :   // return vmovn_u32(vcvt_s32_f32(result));
    7633           0 :   N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
    7634           0 :   N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
    7635           0 :   return N0;
    7636             : }
    7637             : 
    7638           0 : static SDValue LowerADDSUBCARRY(SDValue Op, SelectionDAG &DAG) {
    7639             :   SDNode *N = Op.getNode();
    7640           0 :   EVT VT = N->getValueType(0);
    7641           0 :   SDVTList VTs = DAG.getVTList(VT, MVT::i32);
    7642             : 
    7643           0 :   SDValue Carry = Op.getOperand(2);
    7644             : 
    7645           0 :   SDLoc DL(Op);
    7646             : 
    7647           0 :   SDValue Result;
    7648           0 :   if (Op.getOpcode() == ISD::ADDCARRY) {
    7649             :     // This converts the boolean value carry into the carry flag.
    7650           0 :     Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG);
    7651             : 
    7652             :     // Do the addition proper using the carry flag we wanted.
    7653           0 :     Result = DAG.getNode(ARMISD::ADDE, DL, VTs, Op.getOperand(0),
    7654           0 :                          Op.getOperand(1), Carry);
    7655             : 
    7656             :     // Now convert the carry flag into a boolean value.
    7657           0 :     Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG);
    7658             :   } else {
    7659             :     // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we
    7660             :     // have to invert the carry first.
    7661           0 :     Carry = DAG.getNode(ISD::SUB, DL, MVT::i32,
    7662           0 :                         DAG.getConstant(1, DL, MVT::i32), Carry);
    7663             :     // This converts the boolean value carry into the carry flag.
    7664           0 :     Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG);
    7665             : 
    7666             :     // Do the subtraction proper using the carry flag we wanted.
    7667           0 :     Result = DAG.getNode(ARMISD::SUBE, DL, VTs, Op.getOperand(0),
    7668           0 :                          Op.getOperand(1), Carry);
    7669             : 
    7670             :     // Now convert the carry flag into a boolean value.
    7671           0 :     Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG);
    7672             :     // But the carry returned by ARMISD::SUBE is not a borrow as expected
    7673             :     // by ISD::SUBCARRY, so compute 1 - C.
    7674           0 :     Carry = DAG.getNode(ISD::SUB, DL, MVT::i32,
    7675           0 :                         DAG.getConstant(1, DL, MVT::i32), Carry);
    7676             :   }
    7677             : 
    7678             :   // Return both values.
    7679           0 :   return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, Carry);
    7680             : }
    7681             : 
    7682           5 : SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
    7683             :   assert(Subtarget->isTargetDarwin());
    7684             : 
    7685             :   // For iOS, we want to call an alternative entry point: __sincos_stret,
    7686             :   // return values are passed via sret.
    7687             :   SDLoc dl(Op);
    7688           5 :   SDValue Arg = Op.getOperand(0);
    7689           5 :   EVT ArgVT = Arg.getValueType();
    7690           5 :   Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
    7691           5 :   auto PtrVT = getPointerTy(DAG.getDataLayout());
    7692             : 
    7693           5 :   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
    7694             :   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
    7695             : 
    7696             :   // Pair of floats / doubles used to pass the result.
    7697           5 :   Type *RetTy = StructType::get(ArgTy, ArgTy);
    7698           5 :   auto &DL = DAG.getDataLayout();
    7699             : 
    7700             :   ArgListTy Args;
    7701           5 :   bool ShouldUseSRet = Subtarget->isAPCS_ABI();
    7702             :   SDValue SRet;
    7703           5 :   if (ShouldUseSRet) {
    7704             :     // Create stack object for sret.
    7705           2 :     const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
    7706           2 :     const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
    7707           2 :     int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false);
    7708           2 :     SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
    7709             : 
    7710             :     ArgListEntry Entry;
    7711           2 :     Entry.Node = SRet;
    7712           2 :     Entry.Ty = RetTy->getPointerTo();
    7713           2 :     Entry.IsSExt = false;
    7714           2 :     Entry.IsZExt = false;
    7715           2 :     Entry.IsSRet = true;
    7716           2 :     Args.push_back(Entry);
    7717           2 :     RetTy = Type::getVoidTy(*DAG.getContext());
    7718             :   }
    7719             : 
    7720             :   ArgListEntry Entry;
    7721           5 :   Entry.Node = Arg;
    7722           5 :   Entry.Ty = ArgTy;
    7723             :   Entry.IsSExt = false;
    7724             :   Entry.IsZExt = false;
    7725           5 :   Args.push_back(Entry);
    7726             : 
    7727             :   RTLIB::Libcall LC =
    7728             :       (ArgVT == MVT::f64) ? RTLIB::SINCOS_STRET_F64 : RTLIB::SINCOS_STRET_F32;
    7729             :   const char *LibcallName = getLibcallName(LC);
    7730             :   CallingConv::ID CC = getLibcallCallingConv(LC);
    7731           5 :   SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
    7732             : 
    7733          10 :   TargetLowering::CallLoweringInfo CLI(DAG);
    7734             :   CLI.setDebugLoc(dl)
    7735           5 :       .setChain(DAG.getEntryNode())
    7736           5 :       .setCallee(CC, RetTy, Callee, std::move(Args))
    7737             :       .setDiscardResult(ShouldUseSRet);
    7738           5 :   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
    7739             : 
    7740           5 :   if (!ShouldUseSRet)
    7741           3 :     return CallResult.first;
    7742             : 
    7743             :   SDValue LoadSin =
    7744           2 :       DAG.getLoad(ArgVT, dl, CallResult.second, SRet, MachinePointerInfo());
    7745             : 
    7746             :   // Address of cos field.
    7747             :   SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
    7748           2 :                             DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
    7749             :   SDValue LoadCos =
    7750           2 :       DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, MachinePointerInfo());
    7751             : 
    7752           2 :   SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
    7753             :   return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
    7754           2 :                      LoadSin.getValue(0), LoadCos.getValue(0));
    7755             : }
    7756             : 
    7757          27 : SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
    7758             :                                                   bool Signed,
    7759             :                                                   SDValue &Chain) const {
    7760          27 :   EVT VT = Op.getValueType();
    7761             :   assert((VT == MVT::i32 || VT == MVT::i64) &&
    7762             :          "unexpected type for custom lowering DIV");
    7763             :   SDLoc dl(Op);
    7764             : 
    7765          27 :   const auto &DL = DAG.getDataLayout();
    7766             :   const auto &TLI = DAG.getTargetLoweringInfo();
    7767             : 
    7768             :   const char *Name = nullptr;
    7769          27 :   if (Signed)
    7770             :     Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
    7771             :   else
    7772             :     Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
    7773             : 
    7774          27 :   SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
    7775             : 
    7776             :   ARMTargetLowering::ArgListTy Args;
    7777             : 
    7778          81 :   for (auto AI : {1, 0}) {
    7779             :     ArgListEntry Arg;
    7780          54 :     Arg.Node = Op.getOperand(AI);
    7781         108 :     Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
    7782          54 :     Args.push_back(Arg);
    7783             :   }
    7784             : 
    7785          27 :   CallLoweringInfo CLI(DAG);
    7786             :   CLI.setDebugLoc(dl)
    7787          27 :     .setChain(Chain)
    7788          27 :     .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
    7789          27 :                ES, std::move(Args));
    7790             : 
    7791          27 :   return LowerCallTo(CLI).first;
    7792             : }
    7793             : 
    7794          23 : SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
    7795             :                                             bool Signed) const {
    7796             :   assert(Op.getValueType() == MVT::i32 &&
    7797             :          "unexpected type for custom lowering DIV");
    7798             :   SDLoc dl(Op);
    7799             : 
    7800             :   SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
    7801          23 :                                DAG.getEntryNode(), Op.getOperand(1));
    7802             : 
    7803          23 :   return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
    7804             : }
    7805             : 
    7806          45 : static SDValue WinDBZCheckDenominator(SelectionDAG &DAG, SDNode *N, SDValue InChain) {
    7807             :   SDLoc DL(N);
    7808          45 :   SDValue Op = N->getOperand(1);
    7809          45 :   if (N->getValueType(0) == MVT::i32)
    7810          39 :     return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, Op);
    7811             :   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op,
    7812           6 :                            DAG.getConstant(0, DL, MVT::i32));
    7813             :   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op,
    7814           6 :                            DAG.getConstant(1, DL, MVT::i32));
    7815             :   return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain,
    7816           6 :                      DAG.getNode(ISD::OR, DL, MVT::i32, Lo, Hi));
    7817             : }
    7818             : 
    7819           4 : void ARMTargetLowering::ExpandDIV_Windows(
    7820             :     SDValue Op, SelectionDAG &DAG, bool Signed,
    7821             :     SmallVectorImpl<SDValue> &Results) const {
    7822           4 :   const auto &DL = DAG.getDataLayout();
    7823             :   const auto &TLI = DAG.getTargetLoweringInfo();
    7824             : 
    7825             :   assert(Op.getValueType() == MVT::i64 &&
    7826             :          "unexpected type for custom lowering DIV");
    7827             :   SDLoc dl(Op);
    7828             : 
    7829           4 :   SDValue DBZCHK = WinDBZCheckDenominator(DAG, Op.getNode(), DAG.getEntryNode());
    7830             : 
    7831           4 :   SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
    7832             : 
    7833           4 :   SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
    7834             :   SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
    7835           4 :                               DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
    7836           4 :   Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
    7837             : 
    7838           4 :   Results.push_back(Lower);
    7839           4 :   Results.push_back(Upper);
    7840           4 : }
    7841             : 
    7842           0 : static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
    7843          24 :   if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
    7844             :     // Acquire/Release load/store is not legal for targets without a dmb or
    7845             :     // equivalent available.
    7846           0 :     return SDValue();
    7847             : 
    7848             :   // Monotonic load/store is legal for all targets.
    7849           0 :   return Op;
    7850             : }
    7851             : 
    7852           0 : static void ReplaceREADCYCLECOUNTER(SDNode *N,
    7853             :                                     SmallVectorImpl<SDValue> &Results,
    7854             :                                     SelectionDAG &DAG,
    7855             :                                     const ARMSubtarget *Subtarget) {
    7856             :   SDLoc DL(N);
    7857             :   // Under Power Management extensions, the cycle-count is:
    7858             :   //    mrc p15, #0, <Rt>, c9, c13, #0
    7859           0 :   SDValue Ops[] = { N->getOperand(0), // Chain
    7860           0 :                     DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
    7861           0 :                     DAG.getConstant(15, DL, MVT::i32),
    7862           0 :                     DAG.getConstant(0, DL, MVT::i32),
    7863           0 :                     DAG.getConstant(9, DL, MVT::i32),
    7864           0 :                     DAG.getConstant(13, DL, MVT::i32),
    7865           0 :                     DAG.getConstant(0, DL, MVT::i32)
    7866           0 :   };
    7867             : 
    7868             :   SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
    7869           0 :                                  DAG.getVTList(MVT::i32, MVT::Other), Ops);
    7870           0 :   Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
    7871           0 :                                 DAG.getConstant(0, DL, MVT::i32)));
    7872           0 :   Results.push_back(Cycles32.getValue(1));
    7873           0 : }
    7874             : 
    7875          14 : static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) {
    7876          14 :   SDLoc dl(V.getNode());
    7877          14 :   SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32);
    7878             :   SDValue VHi = DAG.getAnyExtOrTrunc(
    7879             :       DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)),
    7880          14 :       dl, MVT::i32);
    7881          14 :   bool isBigEndian = DAG.getDataLayout().isBigEndian();
    7882          14 :   if (isBigEndian)
    7883             :     std::swap (VLo, VHi);
    7884             :   SDValue RegClass =
    7885          14 :       DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
    7886          14 :   SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32);
    7887          14 :   SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32);
    7888          14 :   const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 };
    7889             :   return SDValue(
    7890          14 :       DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0);
    7891             : }
    7892             : 
    7893           7 : static void ReplaceCMP_SWAP_64Results(SDNode *N,
    7894             :                                        SmallVectorImpl<SDValue> & Results,
    7895             :                                        SelectionDAG &DAG) {
    7896             :   assert(N->getValueType(0) == MVT::i64 &&
    7897             :          "AtomicCmpSwap on types less than 64 should be legal");
    7898           7 :   SDValue Ops[] = {N->getOperand(1),
    7899           7 :                    createGPRPairNode(DAG, N->getOperand(2)),
    7900          14 :                    createGPRPairNode(DAG, N->getOperand(3)),
    7901          14 :                    N->getOperand(0)};
    7902          14 :   SDNode *CmpSwap = DAG.getMachineNode(
    7903           7 :       ARM::CMP_SWAP_64, SDLoc(N),
    7904             :       DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops);
    7905             : 
    7906           7 :   MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand();
    7907          14 :   DAG.setNodeMemRefs(cast<MachineSDNode>(CmpSwap), {MemOp});
    7908             : 
    7909           7 :   bool isBigEndian = DAG.getDataLayout().isBigEndian();
    7910             : 
    7911          14 :   Results.push_back(
    7912           7 :       DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_1 : ARM::gsub_0,
    7913          20 :                                  SDLoc(N), MVT::i32, SDValue(CmpSwap, 0)));
    7914           7 :   Results.push_back(
    7915           7 :       DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_0 : ARM::gsub_1,
    7916          20 :                                  SDLoc(N), MVT::i32, SDValue(CmpSwap, 0)));
    7917           7 :   Results.push_back(SDValue(CmpSwap, 2));
    7918           7 : }
    7919             : 
    7920           0 : static SDValue LowerFPOWI(SDValue Op, const ARMSubtarget &Subtarget,
    7921             :                           SelectionDAG &DAG) {
    7922           0 :   const auto &TLI = DAG.getTargetLoweringInfo();
    7923             : 
    7924             :   assert(Subtarget.getTargetTriple().isOSMSVCRT() &&
    7925             :          "Custom lowering is MSVCRT specific!");
    7926             : 
    7927           0 :   SDLoc dl(Op);
    7928           0 :   SDValue Val = Op.getOperand(0);
    7929             :   MVT Ty = Val->getSimpleValueType(0);
    7930           0 :   SDValue Exponent = DAG.getNode(ISD::SINT_TO_FP, dl, Ty, Op.getOperand(1));
    7931             :   SDValue Callee = DAG.getExternalSymbol(Ty == MVT::f32 ? "powf" : "pow",
    7932           0 :                                          TLI.getPointerTy(DAG.getDataLayout()));
    7933             : 
    7934             :   TargetLowering::ArgListTy Args;
    7935             :   TargetLowering::ArgListEntry Entry;
    7936             : 
    7937           0 :   Entry.Node = Val;
    7938           0 :   Entry.Ty = Val.getValueType().getTypeForEVT(*DAG.getContext());
    7939           0 :   Entry.IsZExt = true;
    7940           0 :   Args.push_back(Entry);
    7941             : 
    7942           0 :   Entry.Node = Exponent;
    7943           0 :   Entry.Ty = Exponent.getValueType().getTypeForEVT(*DAG.getContext());
    7944           0 :   Entry.IsZExt = true;
    7945           0 :   Args.push_back(Entry);
    7946             : 
    7947           0 :   Type *LCRTy = Val.getValueType().getTypeForEVT(*DAG.getContext());
    7948             : 
    7949             :   // In the in-chain to the call is the entry node  If we are emitting a
    7950             :   // tailcall, the chain will be mutated if the node has a non-entry input
    7951             :   // chain.
    7952             :   SDValue InChain = DAG.getEntryNode();
    7953           0 :   SDValue TCChain = InChain;
    7954             : 
    7955           0 :   const Function &F = DAG.getMachineFunction().getFunction();
    7956           0 :   bool IsTC = TLI.isInTailCallPosition(DAG, Op.getNode(), TCChain) &&
    7957             :               F.getReturnType() == LCRTy;
    7958             :   if (IsTC)
    7959           0 :     InChain = TCChain;
    7960             : 
    7961           0 :   TargetLowering::CallLoweringInfo CLI(DAG);
    7962             :   CLI.setDebugLoc(dl)
    7963           0 :       .setChain(InChain)
    7964           0 :       .setCallee(CallingConv::ARM_AAPCS_VFP, LCRTy, Callee, std::move(Args))
    7965             :       .setTailCall(IsTC);
    7966           0 :   std::pair<SDValue, SDValue> CI = TLI.LowerCallTo(CLI);
    7967             : 
    7968             :   // Return the chain (the DAG root) if it is a tail call
    7969           0 :   return !CI.second.getNode() ? DAG.getRoot() : CI.first;
    7970             : }
    7971             : 
    7972       23027 : SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
    7973             :   LLVM_DEBUG(dbgs() << "Lowering node: "; Op.dump());
    7974       23027 :   switch (Op.getOpcode()) {
    7975           0 :   default: llvm_unreachable("Don't know how to custom lower this!");
    7976           2 :   case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
    7977        1266 :   case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
    7978          37 :   case ISD::BlockAddress:  return LowerBlockAddress(Op, DAG);
    7979        2179 :   case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
    7980         219 :   case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
    7981         140 :   case ISD::SELECT:        return LowerSELECT(Op, DAG);
    7982        1873 :   case ISD::SELECT_CC:     return LowerSELECT_CC(Op, DAG);
    7983         281 :   case ISD::BRCOND:        return LowerBRCOND(Op, DAG);
    7984        1955 :   case ISD::BR_CC:         return LowerBR_CC(Op, DAG);
    7985          38 :   case ISD::BR_JT:         return LowerBR_JT(Op, DAG);
    7986          34 :   case ISD::VASTART:       return LowerVASTART(Op, DAG);
    7987          21 :   case ISD::ATOMIC_FENCE:  return LowerATOMIC_FENCE(Op, DAG, Subtarget);
    7988          32 :   case ISD::PREFETCH:      return LowerPREFETCH(Op, DAG, Subtarget);
    7989         186 :   case ISD::SINT_TO_FP:
    7990         186 :   case ISD::UINT_TO_FP:    return LowerINT_TO_FP(Op, DAG);
    7991         117 :   case ISD::FP_TO_SINT:
    7992         117 :   case ISD::FP_TO_UINT:    return LowerFP_TO_INT(Op, DAG);
    7993          23 :   case ISD::FCOPYSIGN:     return LowerFCOPYSIGN(Op, DAG);
    7994          12 :   case ISD::RETURNADDR:    return LowerRETURNADDR(Op, DAG);
    7995          44 :   case ISD::FRAMEADDR:     return LowerFRAMEADDR(Op, DAG);
    7996           8 :   case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
    7997           9 :   case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
    7998          32 :   case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
    7999        2477 :   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
    8000        2477 :                                                                Subtarget);
    8001         788 :   case ISD::BITCAST:       return ExpandBITCAST(Op.getNode(), DAG, Subtarget);
    8002          81 :   case ISD::SHL:
    8003             :   case ISD::SRL:
    8004          81 :   case ISD::SRA:           return LowerShift(Op.getNode(), DAG, Subtarget);
    8005           0 :   case ISD::SREM:          return LowerREM(Op.getNode(), DAG);
    8006           0 :   case ISD::UREM:          return LowerREM(Op.getNode(), DAG);
    8007           3 :   case ISD::SHL_PARTS:     return LowerShiftLeftParts(Op, DAG);
    8008           6 :   case ISD::SRL_PARTS:
    8009           6 :   case ISD::SRA_PARTS:     return LowerShiftRightParts(Op, DAG);
    8010          48 :   case ISD::CTTZ:
    8011          48 :   case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
    8012          19 :   case ISD::CTPOP:         return LowerCTPOP(Op.getNode(), DAG, Subtarget);
    8013         146 :   case ISD::SETCC:         return LowerVSETCC(Op, DAG);
    8014          87 :   case ISD::SETCCCARRY:    return LowerSETCCCARRY(Op, DAG);
    8015        1317 :   case ISD::ConstantFP:    return LowerConstantFP(Op, DAG, Subtarget);
    8016        1008 :   case ISD::BUILD_VECTOR:  return LowerBUILD_VECTOR(Op, DAG, Subtarget);
    8017         375 :   case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
    8018        1876 :   case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
    8019        5529 :   case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
    8020           0 :   case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
    8021           3 :   case ISD::FLT_ROUNDS_:   return LowerFLT_ROUNDS_(Op, DAG);
    8022         177 :   case ISD::MUL:           return LowerMUL(Op, DAG);
    8023          25 :   case ISD::SDIV:
    8024          71 :     if (Subtarget->isTargetWindows() && !Op.getValueType().isVector())
    8025          19 :       return LowerDIV_Windows(Op, DAG, /* Signed */ true);
    8026           6 :     return LowerSDIV(Op, DAG);
    8027           9 :   case ISD::UDIV:
    8028          24 :     if (Subtarget->isTargetWindows() && !Op.getValueType().isVector())
    8029           4 :       return LowerDIV_Windows(Op, DAG, /* Signed */ false);
    8030           5 :     return LowerUDIV(Op, DAG);
    8031         565 :   case ISD::ADDCARRY:
    8032         565 :   case ISD::SUBCARRY:      return LowerADDSUBCARRY(Op, DAG);
    8033          18 :   case ISD::SADDO:
    8034             :   case ISD::SSUBO:
    8035          18 :     return LowerSignedALUO(Op, DAG);
    8036         653 :   case ISD::UADDO:
    8037             :   case ISD::USUBO:
    8038         653 :     return LowerUnsignedALUO(Op, DAG);
    8039          24 :   case ISD::ATOMIC_LOAD:
    8040          24 :   case ISD::ATOMIC_STORE:  return LowerAtomicLoadStore(Op, DAG);
    8041           5 :   case ISD::FSINCOS:       return LowerFSINCOS(Op, DAG);
    8042         191 :   case ISD::SDIVREM:
    8043         191 :   case ISD::UDIVREM:       return LowerDivRem(Op, DAG);
    8044           8 :   case ISD::DYNAMIC_STACKALLOC:
    8045          16 :     if (Subtarget->isTargetWindows())
    8046           8 :       return LowerDYNAMIC_STACKALLOC(Op, DAG);
    8047           0 :     llvm_unreachable("Don't know how to custom lower this!");
    8048           6 :   case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
    8049           9 :   case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
    8050           4 :   case ISD::FPOWI: return LowerFPOWI(Op, *Subtarget, DAG);
    8051           0 :   case ARMISD::WIN__DBZCHK: return SDValue();
    8052             :   }
    8053             : }
    8054             : 
    8055          24 : static void ReplaceLongIntrinsic(SDNode *N, SmallVectorImpl<SDValue> &Results,
    8056             :                                  SelectionDAG &DAG) {
    8057          48 :   unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
    8058             :   unsigned Opc = 0;
    8059          24 :   if (IntNo == Intrinsic::arm_smlald)
    8060             :     Opc = ARMISD::SMLALD;
    8061          18 :   else if (IntNo == Intrinsic::arm_smlaldx)
    8062             :     Opc = ARMISD::SMLALDX;
    8063          12 :   else if (IntNo == Intrinsic::arm_smlsld)
    8064             :     Opc = ARMISD::SMLSLD;
    8065           6 :   else if (IntNo == Intrinsic::arm_smlsldx)
    8066             :     Opc = ARMISD::SMLSLDX;
    8067             :   else
    8068           0 :     return;
    8069             : 
    8070             :   SDLoc dl(N);
    8071             :   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
    8072          24 :                            N->getOperand(3),
    8073          24 :                            DAG.getConstant(0, dl, MVT::i32));
    8074             :   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
    8075          24 :                            N->getOperand(3),
    8076          24 :                            DAG.getConstant(1, dl, MVT::i32));
    8077             : 
    8078             :   SDValue LongMul = DAG.getNode(Opc, dl,
    8079             :                                 DAG.getVTList(MVT::i32, MVT::i32),
    8080          24 :                                 N->getOperand(1), N->getOperand(2),
    8081          24 :                                 Lo, Hi);
    8082          24 :   Results.push_back(LongMul.getValue(0));
    8083          24 :   Results.push_back(LongMul.getValue(1));
    8084             : }
    8085             : 
    8086             : /// ReplaceNodeResults - Replace the results of node with an illegal result
    8087             : /// type with new values built out of custom code.
    8088        1045 : void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
    8089             :                                            SmallVectorImpl<SDValue> &Results,
    8090             :                                            SelectionDAG &DAG) const {
    8091        1045 :   SDValue Res;
    8092        2090 :   switch (N->getOpcode()) {
    8093           0 :   default:
    8094           0 :     llvm_unreachable("Don't know how to custom expand this!");
    8095           2 :   case ISD::READ_REGISTER:
    8096           2 :     ExpandREAD_REGISTER(N, Results, DAG);
    8097           2 :     break;
    8098         133 :   case ISD::BITCAST:
    8099         133 :     Res = ExpandBITCAST(N, DAG, Subtarget);
    8100         133 :     break;
    8101         855 :   case ISD::SRL:
    8102             :   case ISD::SRA:
    8103         855 :     Res = Expand64BitShift(N, DAG, Subtarget);
    8104         855 :     break;
    8105           4 :   case ISD::SREM:
    8106             :   case ISD::UREM:
    8107           4 :     Res = LowerREM(N, DAG);
    8108           4 :     break;
    8109             :   case ISD::SDIVREM:
    8110             :   case ISD::UDIVREM:
    8111          14 :     Res = LowerDivRem(SDValue(N, 0), DAG);
    8112             :     assert(Res.getNumOperands() == 2 && "DivRem needs two values");
    8113          14 :     Results.push_back(Res.getValue(0));
    8114          14 :     Results.push_back(Res.getValue(1));
    8115          51 :     return;
    8116           2 :   case ISD::READCYCLECOUNTER:
    8117           2 :     ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
    8118           2 :     return;
    8119           4 :   case ISD::UDIV:
    8120             :   case ISD::SDIV:
    8121             :     assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
    8122           8 :     return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
    8123           4 :                              Results);
    8124           7 :   case ISD::ATOMIC_CMP_SWAP:
    8125           7 :     ReplaceCMP_SWAP_64Results(N, Results, DAG);
    8126           7 :     return;
    8127          24 :   case ISD::INTRINSIC_WO_CHAIN:
    8128          24 :     return ReplaceLongIntrinsic(N, Results, DAG);
    8129             :   }
    8130         994 :   if (Res.getNode())
    8131         144 :     Results.push_back(Res);
    8132             : }
    8133             : 
    8134             : //===----------------------------------------------------------------------===//
    8135             : //                           ARM Scheduler Hooks
    8136             : //===----------------------------------------------------------------------===//
    8137             : 
    8138             : /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
    8139             : /// registers the function context.
    8140          32 : void ARMTargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI,
    8141             :                                                MachineBasicBlock *MBB,
    8142             :                                                MachineBasicBlock *DispatchBB,
    8143             :                                                int FI) const {
    8144             :   assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
    8145             :          "ROPI/RWPI not currently supported with SjLj");
    8146          32 :   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
    8147             :   DebugLoc dl = MI.getDebugLoc();
    8148          32 :   MachineFunction *MF = MBB->getParent();
    8149          32 :   MachineRegisterInfo *MRI = &MF->getRegInfo();
    8150          32 :   MachineConstantPool *MCP = MF->getConstantPool();
    8151          32 :   ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
    8152          32 :   const Function &F = MF->getFunction();
    8153             : 
    8154          32 :   bool isThumb = Subtarget->isThumb();
    8155             :   bool isThumb2 = Subtarget->isThumb2();
    8156             : 
    8157             :   unsigned PCLabelId = AFI->createPICLabelUId();
    8158          32 :   unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
    8159             :   ARMConstantPoolValue *CPV =
    8160          32 :     ARMConstantPoolMBB::Create(F.getContext(), DispatchBB, PCLabelId, PCAdj);
    8161          32 :   unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
    8162             : 
    8163          32 :   const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
    8164             :                                            : &ARM::GPRRegClass;
    8165             : 
    8166             :   // Grab constant pool and fixed stack memory operands.
    8167             :   MachineMemOperand *CPMMO =
    8168          32 :       MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
    8169             :                                MachineMemOperand::MOLoad, 4, 4);
    8170             : 
    8171             :   MachineMemOperand *FIMMOSt =
    8172          32 :       MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
    8173             :                                MachineMemOperand::MOStore, 4, 4);
    8174             : 
    8175             :   // Load the address of the dispatch MBB into the jump buffer.
    8176          32 :   if (isThumb2) {
    8177             :     // Incoming value: jbuf
    8178             :     //   ldr.n  r5, LCPI1_1
    8179             :     //   orr    r5, r5, #1
    8180             :     //   add    r5, pc
    8181             :     //   str    r5, [$jbuf, #+4] ; &jbuf[1]
    8182          11 :     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
    8183          22 :     BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
    8184             :         .addConstantPoolIndex(CPI)
    8185             :         .addMemOperand(CPMMO)
    8186          11 :         .add(predOps(ARMCC::AL));
    8187             :     // Set the low bit because of thumb mode.
    8188          11 :     unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
    8189          22 :     BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
    8190          11 :         .addReg(NewVReg1, RegState::Kill)
    8191             :         .addImm(0x01)
    8192          11 :         .add(predOps(ARMCC::AL))
    8193          11 :         .add(condCodeOp());
    8194          11 :     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
    8195          22 :     BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
    8196          11 :       .addReg(NewVReg2, RegState::Kill)
    8197          11 :       .addImm(PCLabelId);
    8198          22 :     BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
    8199          11 :         .addReg(NewVReg3, RegState::Kill)
    8200             :         .addFrameIndex(FI)
    8201             :         .addImm(36) // &jbuf[1] :: pc
    8202             :         .addMemOperand(FIMMOSt)
    8203          11 :         .add(predOps(ARMCC::AL));
    8204          21 :   } else if (isThumb) {
    8205             :     // Incoming value: jbuf
    8206             :     //   ldr.n  r1, LCPI1_4
    8207             :     //   add    r1, pc
    8208             :     //   mov    r2, #1
    8209             :     //   orrs   r1, r2
    8210             :     //   add    r2, $jbuf, #+4 ; &jbuf[1]
    8211             :     //   str    r1, [r2]
    8212           4 :     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
    8213           8 :     BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
    8214             :         .addConstantPoolIndex(CPI)
    8215             :         .addMemOperand(CPMMO)
    8216           4 :         .add(predOps(ARMCC::AL));
    8217           4 :     unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
    8218          12 :     BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
    8219           4 :       .addReg(NewVReg1, RegState::Kill)
    8220           4 :       .addImm(PCLabelId);
    8221             :     // Set the low bit because of thumb mode.
    8222           4 :     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
    8223           8 :     BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
    8224           4 :         .addReg(ARM::CPSR, RegState::Define)
    8225             :         .addImm(1)
    8226           4 :         .add(predOps(ARMCC::AL));
    8227           4 :     unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
    8228           8 :     BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
    8229           4 :         .addReg(ARM::CPSR, RegState::Define)
    8230           4 :         .addReg(NewVReg2, RegState::Kill)
    8231           4 :         .addReg(NewVReg3, RegState::Kill)
    8232           4 :         .add(predOps(ARMCC::AL));
    8233           4 :     unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
    8234           8 :     BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
    8235             :             .addFrameIndex(FI)
    8236             :             .addImm(36); // &jbuf[1] :: pc
    8237           8 :     BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
    8238           4 :         .addReg(NewVReg4, RegState::Kill)
    8239           4 :         .addReg(NewVReg5, RegState::Kill)
    8240             :         .addImm(0)
    8241             :         .addMemOperand(FIMMOSt)
    8242           4 :         .add(predOps(ARMCC::AL));
    8243             :   } else {
    8244             :     // Incoming value: jbuf
    8245             :     //   ldr  r1, LCPI1_1
    8246             :     //   add  r1, pc, r1
    8247             :     //   str  r1, [$jbuf, #+4] ; &jbuf[1]
    8248          17 :     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
    8249          34 :     BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
    8250             :         .addConstantPoolIndex(CPI)
    8251             :         .addImm(0)
    8252             :         .addMemOperand(CPMMO)
    8253          17 :         .add(predOps(ARMCC::AL));
    8254          17 :     unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
    8255          34 :     BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
    8256          17 :         .addReg(NewVReg1, RegState::Kill)
    8257          17 :         .addImm(PCLabelId)
    8258          17 :         .add(predOps(ARMCC::AL));
    8259          34 :     BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
    8260          17 :         .addReg(NewVReg2, RegState::Kill)
    8261             :         .addFrameIndex(FI)
    8262             :         .addImm(36) // &jbuf[1] :: pc
    8263             :         .addMemOperand(FIMMOSt)
    8264          17 :         .add(predOps(ARMCC::AL));
    8265             :   }
    8266          32 : }
    8267             : 
    8268          32 : void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI,
    8269             :                                               MachineBasicBlock *MBB) const {
    8270          32 :   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
    8271             :   DebugLoc dl = MI.getDebugLoc();
    8272          32 :   MachineFunction *MF = MBB->getParent();
    8273          32 :   MachineRegisterInfo *MRI = &MF->getRegInfo();
    8274          32 :   MachineFrameInfo &MFI = MF->getFrameInfo();
    8275          32 :   int FI = MFI.getFunctionContextIndex();
    8276             : 
    8277          32 :   const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
    8278             :                                                         : &ARM::GPRnopcRegClass;
    8279             : 
    8280             :   // Get a mapping of the call site numbers to all of the landing pads they're
    8281             :   // associated with.
    8282             :   DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2>> CallSiteNumToLPad;
    8283          32 :   unsigned MaxCSNum = 0;
    8284         390 :   for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
    8285             :        ++BB) {
    8286         358 :     if (!BB->isEHPad()) continue;
    8287             : 
    8288             :     // FIXME: We should assert that the EH_LABEL is the first MI in the landing
    8289             :     // pad.
    8290             :     for (MachineBasicBlock::iterator
    8291         124 :            II = BB->begin(), IE = BB->end(); II != IE; ++II) {
    8292         124 :       if (!II->isEHLabel()) continue;
    8293             : 
    8294         124 :       MCSymbol *Sym = II->getOperand(0).getMCSymbol();
    8295         124 :       if (!MF->hasCallSiteLandingPad(Sym)) continue;
    8296             : 
    8297         124 :       SmallVectorImpl<unsigned> &CallSiteIdxs = MF->getCallSiteLandingPad(Sym);
    8298         171 :       for (SmallVectorImpl<unsigned>::iterator
    8299             :              CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
    8300         295 :            CSI != CSE; ++CSI) {
    8301         171 :         CallSiteNumToLPad[*CSI].push_back(&*BB);
    8302         171 :         MaxCSNum = std::max(MaxCSNum, *CSI);
    8303             :       }
    8304             :       break;
    8305             :     }
    8306             :   }
    8307             : 
    8308             :   // Get an ordered list of the machine basic blocks for the jump table.
    8309             :   std::vector<MachineBasicBlock*> LPadList;
    8310             :   SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
    8311          32 :   LPadList.reserve(CallSiteNumToLPad.size());
    8312         203 :   for (unsigned I = 1; I <= MaxCSNum; ++I) {
    8313             :     SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
    8314         171 :     for (SmallVectorImpl<MachineBasicBlock*>::iterator
    8315         342 :            II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
    8316         171 :       LPadList.push_back(*II);
    8317         171 :       InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
    8318             :     }
    8319             :   }
    8320             : 
    8321             :   assert(!LPadList.empty() &&
    8322             :          "No landing pad destinations for the dispatch jump table!");
    8323             : 
    8324             :   // Create the jump table and associated information.
    8325             :   MachineJumpTableInfo *JTI =
    8326          32 :     MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
    8327          32 :   unsigned MJTI = JTI->createJumpTableIndex(LPadList);
    8328             : 
    8329             :   // Create the MBBs for the dispatch code.
    8330             : 
    8331             :   // Shove the dispatch's address into the return slot in the function context.
    8332          32 :   MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
    8333             :   DispatchBB->setIsEHPad();
    8334             : 
    8335          32 :   MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
    8336             :   unsigned trap_opcode;
    8337          32 :   if (Subtarget->isThumb())
    8338             :     trap_opcode = ARM::tTRAP;
    8339             :   else
    8340          17 :     trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
    8341             : 
    8342          32 :   BuildMI(TrapBB, dl, TII->get(trap_opcode));
    8343          32 :   DispatchBB->addSuccessor(TrapBB);
    8344             : 
    8345          32 :   MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
    8346          32 :   DispatchBB->addSuccessor(DispContBB);
    8347             : 
    8348             :   // Insert and MBBs.
    8349             :   MF->insert(MF->end(), DispatchBB);
    8350             :   MF->insert(MF->end(), DispContBB);
    8351             :   MF->insert(MF->end(), TrapBB);
    8352             : 
    8353             :   // Insert code into the entry block that creates and registers the function
    8354             :   // context.
    8355          32 :   SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
    8356             : 
    8357          32 :   MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
    8358             :       MachinePointerInfo::getFixedStack(*MF, FI),
    8359             :       MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
    8360             : 
    8361             :   MachineInstrBuilder MIB;
    8362          32 :   MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
    8363             : 
    8364             :   const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
    8365          32 :   const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
    8366             : 
    8367             :   // Add a register mask with no preserved registers.  This results in all
    8368             :   // registers being marked as clobbered. This can't work if the dispatch block
    8369             :   // is in a Thumb1 function and is linked with ARM code which uses the FP
    8370             :   // registers, as there is no way to preserve the FP registers in Thumb1 mode.
    8371          32 :   MIB.addRegMask(RI.getSjLjDispatchPreservedMask(*MF));
    8372             : 
    8373          32 :   bool IsPositionIndependent = isPositionIndependent();
    8374          32 :   unsigned NumLPads = LPadList.size();
    8375          32 :   if (Subtarget->isThumb2()) {
    8376          11 :     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
    8377          11 :     BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
    8378             :         .addFrameIndex(FI)
    8379             :         .addImm(4)
    8380             :         .addMemOperand(FIMMOLd)
    8381          11 :         .add(predOps(ARMCC::AL));
    8382             : 
    8383          11 :     if (NumLPads < 256) {
    8384          11 :       BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
    8385          11 :           .addReg(NewVReg1)
    8386          11 :           .addImm(LPadList.size())
    8387          11 :           .add(predOps(ARMCC::AL));
    8388             :     } else {
    8389           0 :       unsigned VReg1 = MRI->createVirtualRegister(TRC);
    8390           0 :       BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
    8391           0 :           .addImm(NumLPads & 0xFFFF)
    8392           0 :           .add(predOps(ARMCC::AL));
    8393             : 
    8394             :       unsigned VReg2 = VReg1;
    8395           0 :       if ((NumLPads & 0xFFFF0000) != 0) {
    8396           0 :         VReg2 = MRI->createVirtualRegister(TRC);
    8397           0 :         BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
    8398           0 :             .addReg(VReg1)
    8399           0 :             .addImm(NumLPads >> 16)
    8400           0 :             .add(predOps(ARMCC::AL));
    8401             :       }
    8402             : 
    8403           0 :       BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
    8404           0 :           .addReg(NewVReg1)
    8405           0 :           .addReg(VReg2)
    8406           0 :           .add(predOps(ARMCC::AL));
    8407             :     }
    8408             : 
    8409          22 :     BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
    8410             :       .addMBB(TrapBB)
    8411             :       .addImm(ARMCC::HI)
    8412          11 :       .addReg(ARM::CPSR);
    8413             : 
    8414          11 :     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
    8415          11 :     BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT), NewVReg3)
    8416             :         .addJumpTableIndex(MJTI)
    8417          11 :         .add(predOps(ARMCC::AL));
    8418             : 
    8419          11 :     unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
    8420          11 :     BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
    8421          11 :         .addReg(NewVReg3, RegState::Kill)
    8422          11 :         .addReg(NewVReg1)
    8423             :         .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))
    8424          11 :         .add(predOps(ARMCC::AL))
    8425          11 :         .add(condCodeOp());
    8426             : 
    8427          22 :     BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
    8428          11 :       .addReg(NewVReg4, RegState::Kill)
    8429          11 :       .addReg(NewVReg1)
    8430             :       .addJumpTableIndex(MJTI);
    8431          21 :   } else if (Subtarget->isThumb()) {
    8432           4 :     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
    8433           4 :     BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
    8434             :         .addFrameIndex(FI)
    8435             :         .addImm(1)
    8436             :         .addMemOperand(FIMMOLd)
    8437           4 :         .add(predOps(ARMCC::AL));
    8438             : 
    8439           4 :     if (NumLPads < 256) {
    8440           4 :       BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
    8441           4 :           .addReg(NewVReg1)
    8442             :           .addImm(NumLPads)
    8443           4 :           .add(predOps(ARMCC::AL));
    8444             :     } else {
    8445           0 :       MachineConstantPool *ConstantPool = MF->getConstantPool();
    8446           0 :       Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext());
    8447           0 :       const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
    8448             : 
    8449             :       // MachineConstantPool wants an explicit alignment.
    8450           0 :       unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
    8451           0 :       if (Align == 0)
    8452           0 :         Align = MF->getDataLayout().getTypeAllocSize(C->getType());
    8453           0 :       unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
    8454             : 
    8455           0 :       unsigned VReg1 = MRI->createVirtualRegister(TRC);
    8456           0 :       BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
    8457           0 :           .addReg(VReg1, RegState::Define)
    8458             :           .addConstantPoolIndex(Idx)
    8459           0 :           .add(predOps(ARMCC::AL));
    8460           0 :       BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
    8461           0 :           .addReg(NewVReg1)
    8462           0 :           .addReg(VReg1)
    8463           0 :           .add(predOps(ARMCC::AL));
    8464             :     }
    8465             : 
    8466           8 :     BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
    8467             :       .addMBB(TrapBB)
    8468             :       .addImm(ARMCC::HI)
    8469           4 :       .addReg(ARM::CPSR);
    8470             : 
    8471           4 :     unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
    8472           4 :     BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
    8473           4 :         .addReg(ARM::CPSR, RegState::Define)
    8474           4 :         .addReg(NewVReg1)
    8475             :         .addImm(2)
    8476           4 :         .add(predOps(ARMCC::AL));
    8477             : 
    8478           4 :     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
    8479           4 :     BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
    8480             :         .addJumpTableIndex(MJTI)
    8481           4 :         .add(predOps(ARMCC::AL));
    8482             : 
    8483           4 :     unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
    8484           4 :     BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
    8485           4 :         .addReg(ARM::CPSR, RegState::Define)
    8486           4 :         .addReg(NewVReg2, RegState::Kill)
    8487           4 :         .addReg(NewVReg3)
    8488           4 :         .add(predOps(ARMCC::AL));
    8489             : 
    8490           4 :     MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
    8491             :         MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
    8492             : 
    8493           4 :     unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
    8494           4 :     BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
    8495           4 :         .addReg(NewVReg4, RegState::Kill)
    8496             :         .addImm(0)
    8497             :         .addMemOperand(JTMMOLd)
    8498           4 :         .add(predOps(ARMCC::AL));
    8499             : 
    8500             :     unsigned NewVReg6 = NewVReg5;
    8501           4 :     if (IsPositionIndependent) {
    8502           2 :       NewVReg6 = MRI->createVirtualRegister(TRC);
    8503           2 :       BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
    8504           2 :           .addReg(ARM::CPSR, RegState::Define)
    8505           2 :           .addReg(NewVReg5, RegState::Kill)
    8506           2 :           .addReg(NewVReg3)
    8507           2 :           .add(predOps(ARMCC::AL));
    8508             :     }
    8509             : 
    8510           8 :     BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
    8511           4 :       .addReg(NewVReg6, RegState::Kill)
    8512             :       .addJumpTableIndex(MJTI);
    8513             :   } else {
    8514          17 :     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
    8515          17 :     BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
    8516             :         .addFrameIndex(FI)
    8517             :         .addImm(4)
    8518             :         .addMemOperand(FIMMOLd)
    8519          17 :         .add(predOps(ARMCC::AL));
    8520             : 
    8521          17 :     if (NumLPads < 256) {
    8522          17 :       BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
    8523          17 :           .addReg(NewVReg1)
    8524             :           .addImm(NumLPads)
    8525          17 :           .add(predOps(ARMCC::AL));
    8526           0 :     } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
    8527           0 :       unsigned VReg1 = MRI->createVirtualRegister(TRC);
    8528           0 :       BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
    8529           0 :           .addImm(NumLPads & 0xFFFF)
    8530           0 :           .add(predOps(ARMCC::AL));
    8531             : 
    8532             :       unsigned VReg2 = VReg1;
    8533           0 :       if ((NumLPads & 0xFFFF0000) != 0) {
    8534           0 :         VReg2 = MRI->createVirtualRegister(TRC);
    8535           0 :         BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
    8536           0 :             .addReg(VReg1)
    8537           0 :             .addImm(NumLPads >> 16)
    8538           0 :             .add(predOps(ARMCC::AL));
    8539             :       }
    8540             : 
    8541           0 :       BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
    8542           0 :           .addReg(NewVReg1)
    8543           0 :           .addReg(VReg2)
    8544           0 :           .add(predOps(ARMCC::AL));
    8545             :     } else {
    8546           0 :       MachineConstantPool *ConstantPool = MF->getConstantPool();
    8547           0 :       Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext());
    8548           0 :       const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
    8549             : 
    8550             :       // MachineConstantPool wants an explicit alignment.
    8551           0 :       unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
    8552           0 :       if (Align == 0)
    8553           0 :         Align = MF->getDataLayout().getTypeAllocSize(C->getType());
    8554           0 :       unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
    8555             : 
    8556           0 :       unsigned VReg1 = MRI->createVirtualRegister(TRC);
    8557           0 :       BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
    8558           0 :           .addReg(VReg1, RegState::Define)
    8559             :           .addConstantPoolIndex(Idx)
    8560             :           .addImm(0)
    8561           0 :           .add(predOps(ARMCC::AL));
    8562           0 :       BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
    8563           0 :           .addReg(NewVReg1)
    8564           0 :           .addReg(VReg1, RegState::Kill)
    8565           0 :           .add(predOps(ARMCC::AL));
    8566             :     }
    8567             : 
    8568          34 :     BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
    8569             :       .addMBB(TrapBB)
    8570             :       .addImm(ARMCC::HI)
    8571          17 :       .addReg(ARM::CPSR);
    8572             : 
    8573          17 :     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
    8574          17 :     BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
    8575          17 :         .addReg(NewVReg1)
    8576             :         .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))
    8577          17 :         .add(predOps(ARMCC::AL))
    8578          17 :         .add(condCodeOp());
    8579          17 :     unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
    8580          17 :     BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
    8581             :         .addJumpTableIndex(MJTI)
    8582          17 :         .add(predOps(ARMCC::AL));
    8583             : 
    8584          17 :     MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
    8585             :         MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
    8586          17 :     unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
    8587          17 :     BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
    8588          17 :         .addReg(NewVReg3, RegState::Kill)
    8589          17 :         .addReg(NewVReg4)
    8590             :         .addImm(0)
    8591             :         .addMemOperand(JTMMOLd)
    8592          17 :         .add(predOps(ARMCC::AL));
    8593             : 
    8594          17 :     if (IsPositionIndependent) {
    8595          26 :       BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
    8596          13 :         .addReg(NewVReg5, RegState::Kill)
    8597          13 :         .addReg(NewVReg4)
    8598             :         .addJumpTableIndex(MJTI);
    8599             :     } else {
    8600           8 :       BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
    8601           4 :         .addReg(NewVReg5, RegState::Kill)
    8602             :         .addJumpTableIndex(MJTI);
    8603             :     }
    8604             :   }
    8605             : 
    8606             :   // Add the jump table entries as successors to the MBB.
    8607             :   SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
    8608             :   for (std::vector<MachineBasicBlock*>::iterator
    8609         203 :          I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
    8610         171 :     MachineBasicBlock *CurMBB = *I;
    8611         171 :     if (SeenMBBs.insert(CurMBB).second)
    8612         124 :       DispContBB->addSuccessor(CurMBB);
    8613             :   }
    8614             : 
    8615             :   // N.B. the order the invoke BBs are processed in doesn't matter here.
    8616          32 :   const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
    8617             :   SmallVector<MachineBasicBlock*, 64> MBBLPads;
    8618         203 :   for (MachineBasicBlock *BB : InvokeBBs) {
    8619             : 
    8620             :     // Remove the landing pad successor from the invoke block and replace it
    8621             :     // with the new dispatch block.
    8622             :     SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
    8623         171 :                                                   BB->succ_end());
    8624         513 :     while (!Successors.empty()) {
    8625         342 :       MachineBasicBlock *SMBB = Successors.pop_back_val();
    8626         342 :       if (SMBB->isEHPad()) {
    8627         171 :         BB->removeSuccessor(SMBB);
    8628         171 :         MBBLPads.push_back(SMBB);
    8629             :       }
    8630             :     }
    8631             : 
    8632         171 :     BB->addSuccessor(DispatchBB, BranchProbability::getZero());
    8633             :     BB->normalizeSuccProbs();
    8634             : 
    8635             :     // Find the invoke call and mark all of the callee-saved registers as
    8636             :     // 'implicit defined' so that they're spilled. This prevents code from
    8637             :     // moving instructions to before the EH block, where they will never be
    8638             :     // executed.
    8639             :     for (MachineBasicBlock::reverse_iterator
    8640         779 :            II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
    8641         779 :       if (!II->isCall()) continue;
    8642             : 
    8643             :       DenseMap<unsigned, bool> DefRegs;
    8644        1510 :       for (MachineInstr::mop_iterator
    8645         171 :              OI = II->operands_begin(), OE = II->operands_end();
    8646        1681 :            OI != OE; ++OI) {
    8647        1510 :         if (!OI->isReg()) continue;
    8648        1079 :         DefRegs[OI->getReg()] = true;
    8649             :       }
    8650             : 
    8651             :       MachineInstrBuilder MIB(*MF, &*II);
    8652             : 
    8653        2910 :       for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
    8654        2739 :         unsigned Reg = SavedRegs[i];
    8655        4132 :         if (Subtarget->isThumb2() &&
    8656        2438 :             !ARM::tGPRRegClass.contains(Reg) &&
    8657        1045 :             !ARM::hGPRRegClass.contains(Reg))
    8658        1384 :           continue;
    8659          64 :         if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
    8660             :           continue;
    8661        1995 :         if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
    8662             :           continue;
    8663        1355 :         if (!DefRegs[Reg])
    8664        1188 :           MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
    8665             :       }
    8666             : 
    8667             :       break;
    8668             :     }
    8669             :   }
    8670             : 
    8671             :   // Mark all former landing pads as non-landing pads. The dispatch is the only
    8672             :   // landing pad now.
    8673         171 :   for (SmallVectorImpl<MachineBasicBlock*>::iterator
    8674         203 :          I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
    8675         171 :     (*I)->setIsEHPad(false);
    8676             : 
    8677             :   // The instruction is gone now.
    8678          32 :   MI.eraseFromParent();
    8679          32 : }
    8680             : 
    8681             : static
    8682             : MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
    8683             :   for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
    8684           1 :        E = MBB->succ_end(); I != E; ++I)
    8685           1 :     if (*I != Succ)
    8686             :       return *I;
    8687           0 :   llvm_unreachable("Expecting a BB with two successors!");
    8688             : }
    8689             : 
    8690             : /// Return the load opcode for a given load size. If load size >= 8,
    8691             : /// neon opcode will be returned.
    8692        2532 : static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
    8693        2532 :   if (LdSize >= 8)
    8694          83 :     return LdSize == 16 ? ARM::VLD1q32wb_fixed
    8695          83 :                         : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
    8696        2449 :   if (IsThumb1)
    8697         921 :     return LdSize == 4 ? ARM::tLDRi
    8698             :                        : LdSize == 2 ? ARM::tLDRHi
    8699         921 :                                      : LdSize == 1 ? ARM::tLDRBi : 0;
    8700        1528 :   if (IsThumb2)
    8701         512 :     return LdSize == 4 ? ARM::t2LDR_POST
    8702             :                        : LdSize == 2 ? ARM::t2LDRH_POST
    8703         512 :                                      : LdSize == 1 ? ARM::t2LDRB_POST : 0;
    8704        1016 :   return LdSize == 4 ? ARM::LDR_POST_IMM
    8705             :                      : LdSize == 2 ? ARM::LDRH_POST
    8706             :                                    : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
    8707             : }
    8708             : 
    8709             : /// Return the store opcode for a given store size. If store size >= 8,
    8710             : /// neon opcode will be returned.
    8711        2532 : static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
    8712        2532 :   if (StSize >= 8)
    8713          83 :     return StSize == 16 ? ARM::VST1q32wb_fixed
    8714          83 :                         : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
    8715        2449 :   if (IsThumb1)
    8716         921 :     return StSize == 4 ? ARM::tSTRi
    8717             :                        : StSize == 2 ? ARM::tSTRHi
    8718         921 :                                      : StSize == 1 ? ARM::tSTRBi : 0;
    8719        1528 :   if (IsThumb2)
    8720         512 :     return StSize == 4 ? ARM::t2STR_POST
    8721             :                        : StSize == 2 ? ARM::t2STRH_POST
    8722         512 :                                      : StSize == 1 ? ARM::t2STRB_POST : 0;
    8723        1016 :   return StSize == 4 ? ARM::STR_POST_IMM
    8724             :                      : StSize == 2 ? ARM::STRH_POST
    8725             :                                    : StSize == 1 ? ARM::STRB_POST_IMM : 0;
    8726             : }
    8727             : 
    8728             : /// Emit a post-increment load operation with given size. The instructions
    8729             : /// will be added to BB at Pos.
    8730        2532 : static void emitPostLd(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
    8731             :                        const TargetInstrInfo *TII, const DebugLoc &dl,
    8732             :                        unsigned LdSize, unsigned Data, unsigned AddrIn,
    8733             :                        unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
    8734        2532 :   unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
    8735             :   assert(LdOpc != 0 && "Should have a load opcode");
    8736        2532 :   if (LdSize >= 8) {
    8737         166 :     BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
    8738          83 :         .addReg(AddrOut, RegState::Define)
    8739          83 :         .addReg(AddrIn)
    8740             :         .addImm(0)
    8741          83 :         .add(predOps(ARMCC::AL));
    8742        2449 :   } else if (IsThumb1) {
    8743             :     // load + update AddrIn
    8744        1842 :     BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
    8745         921 :         .addReg(AddrIn)
    8746             :         .addImm(0)
    8747         921 :         .add(predOps(ARMCC::AL));
    8748        1842 :     BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut)
    8749         921 :         .add(t1CondCodeOp())
    8750         921 :         .addReg(AddrIn)
    8751         921 :         .addImm(LdSize)
    8752         921 :         .add(predOps(ARMCC::AL));
    8753        1528 :   } else if (IsThumb2) {
    8754        1024 :     BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
    8755         512 :         .addReg(AddrOut, RegState::Define)
    8756         512 :         .addReg(AddrIn)
    8757         512 :         .addImm(LdSize)
    8758         512 :         .add(predOps(ARMCC::AL));
    8759             :   } else { // arm
    8760        2032 :     BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
    8761        1016 :         .addReg(AddrOut, RegState::Define)
    8762        1016 :         .addReg(AddrIn)
    8763        1016 :         .addReg(0)
    8764        1016 :         .addImm(LdSize)
    8765        1016 :         .add(predOps(ARMCC::AL));
    8766             :   }
    8767        2532 : }
    8768             : 
    8769             : /// Emit a post-increment store operation with given size. The instructions
    8770             : /// will be added to BB at Pos.
    8771        2532 : static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
    8772             :                        const TargetInstrInfo *TII, const DebugLoc &dl,
    8773             :                        unsigned StSize, unsigned Data, unsigned AddrIn,
    8774             :                        unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
    8775        2532 :   unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
    8776             :   assert(StOpc != 0 && "Should have a store opcode");
    8777        2532 :   if (StSize >= 8) {
    8778         166 :     BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
    8779          83 :         .addReg(AddrIn)
    8780             :         .addImm(0)
    8781          83 :         .addReg(Data)
    8782          83 :         .add(predOps(ARMCC::AL));
    8783        2449 :   } else if (IsThumb1) {
    8784             :     // store + update AddrIn
    8785        1842 :     BuildMI(*BB, Pos, dl, TII->get(StOpc))
    8786         921 :         .addReg(Data)
    8787         921 :         .addReg(AddrIn)
    8788             :         .addImm(0)
    8789         921 :         .add(predOps(ARMCC::AL));
    8790        1842 :     BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut)
    8791         921 :         .add(t1CondCodeOp())
    8792         921 :         .addReg(AddrIn)
    8793         921 :         .addImm(StSize)
    8794         921 :         .add(predOps(ARMCC::AL));
    8795        1528 :   } else if (IsThumb2) {
    8796        1024 :     BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
    8797         512 :         .addReg(Data)
    8798         512 :         .addReg(AddrIn)
    8799         512 :         .addImm(StSize)
    8800         512 :         .add(predOps(ARMCC::AL));
    8801             :   } else { // arm
    8802        2032 :     BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
    8803        1016 :         .addReg(Data)
    8804        1016 :         .addReg(AddrIn)
    8805        1016 :         .addReg(0)
    8806        1016 :         .addImm(StSize)
    8807        1016 :         .add(predOps(ARMCC::AL));
    8808             :   }
    8809        2532 : }
    8810             : 
    8811             : MachineBasicBlock *
    8812         360 : ARMTargetLowering::EmitStructByval(MachineInstr &MI,
    8813             :                                    MachineBasicBlock *BB) const {
    8814             :   // This pseudo instruction has 3 operands: dst, src, size
    8815             :   // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
    8816             :   // Otherwise, we will generate unrolled scalar copies.
    8817         360 :   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
    8818         360 :   const BasicBlock *LLVM_BB = BB->getBasicBlock();
    8819         360 :   MachineFunction::iterator It = ++BB->getIterator();
    8820             : 
    8821         360 :   unsigned dest = MI.getOperand(0).getReg();
    8822         360 :   unsigned src = MI.getOperand(1).getReg();
    8823         360 :   unsigned SizeVal = MI.getOperand(2).getImm();
    8824         360 :   unsigned Align = MI.getOperand(3).getImm();
    8825             :   DebugLoc dl = MI.getDebugLoc();
    8826             : 
    8827         360 :   MachineFunction *MF = BB->getParent();
    8828         360 :   MachineRegisterInfo &MRI = MF->getRegInfo();
    8829             :   unsigned UnitSize = 0;
    8830             :   const TargetRegisterClass *TRC = nullptr;
    8831             :   const TargetRegisterClass *VecTRC = nullptr;
    8832             : 
    8833         360 :   bool IsThumb1 = Subtarget->isThumb1Only();
    8834             :   bool IsThumb2 = Subtarget->isThumb2();
    8835             :   bool IsThumb = Subtarget->isThumb();
    8836             : 
    8837         360 :   if (Align & 1) {
    8838             :     UnitSize = 1;
    8839         287 :   } else if (Align & 2) {
    8840             :     UnitSize = 2;
    8841             :   } else {
    8842             :     // Check whether we can use NEON instructions.
    8843         225 :     if (!MF->getFunction().hasFnAttribute(Attribute::NoImplicitFloat) &&
    8844         225 :         Subtarget->hasNEON()) {
    8845         102 :       if ((Align % 16 == 0) && SizeVal >= 16)
    8846             :         UnitSize = 16;
    8847          71 :       else if ((Align % 8 == 0) && SizeVal >= 8)
    8848             :         UnitSize = 8;
    8849             :     }
    8850             :     // Can't use NEON instructions.
    8851             :     if (UnitSize == 0)
    8852             :       UnitSize = 4;
    8853             :   }
    8854             : 
    8855             :   // Select the correct opcode and register class for unit size load/store
    8856             :   bool IsNeon = UnitSize >= 8;
    8857         360 :   TRC = IsThumb ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
    8858         360 :   if (IsNeon)
    8859          57 :     VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
    8860          26 :                             : UnitSize == 8 ? &ARM::DPRRegClass
    8861             :                                             : nullptr;
    8862             : 
    8863         360 :   unsigned BytesLeft = SizeVal % UnitSize;
    8864         360 :   unsigned LoopSize = SizeVal - BytesLeft;
    8865             : 
    8866         360 :   if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
    8867             :     // Use LDR and STR to copy.
    8868             :     // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
    8869             :     // [destOut] = STR_POST(scratch, destIn, UnitSize)
    8870             :     unsigned srcIn = src;
    8871             :     unsigned destIn = dest;
    8872        2186 :     for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
    8873        2011 :       unsigned srcOut = MRI.createVirtualRegister(TRC);
    8874        2011 :       unsigned destOut = MRI.createVirtualRegister(TRC);
    8875        3966 :       unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
    8876        4022 :       emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
    8877             :                  IsThumb1, IsThumb2);
    8878        2011 :       emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
    8879             :                  IsThumb1, IsThumb2);
    8880             :       srcIn = srcOut;
    8881             :       destIn = destOut;
    8882             :     }
    8883             : 
    8884             :     // Handle the leftover bytes with LDRB and STRB.
    8885             :     // [scratch, srcOut] = LDRB_POST(srcIn, 1)
    8886             :     // [destOut] = STRB_POST(scratch, destIn, 1)
    8887         409 :     for (unsigned i = 0; i < BytesLeft; i++) {
    8888         234 :       unsigned srcOut = MRI.createVirtualRegister(TRC);
    8889         234 :       unsigned destOut = MRI.createVirtualRegister(TRC);
    8890         234 :       unsigned scratch = MRI.createVirtualRegister(TRC);
    8891         468 :       emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
    8892             :                  IsThumb1, IsThumb2);
    8893         234 :       emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
    8894             :                  IsThumb1, IsThumb2);
    8895             :       srcIn = srcOut;
    8896             :       destIn = destOut;
    8897             :     }
    8898         175 :     MI.eraseFromParent(); // The instruction is gone now.
    8899         175 :     return BB;
    8900             :   }
    8901             : 
    8902             :   // Expand the pseudo op to a loop.
    8903             :   // thisMBB:
    8904             :   //   ...
    8905             :   //   movw varEnd, # --> with thumb2
    8906             :   //   movt varEnd, #
    8907             :   //   ldrcp varEnd, idx --> without thumb2
    8908             :   //   fallthrough --> loopMBB
    8909             :   // loopMBB:
    8910             :   //   PHI varPhi, varEnd, varLoop
    8911             :   //   PHI srcPhi, src, srcLoop
    8912             :   //   PHI destPhi, dst, destLoop
    8913             :   //   [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
    8914             :   //   [destLoop] = STR_POST(scratch, destPhi, UnitSize)
    8915             :   //   subs varLoop, varPhi, #UnitSize
    8916             :   //   bne loopMBB
    8917             :   //   fallthrough --> exitMBB
    8918             :   // exitMBB:
    8919             :   //   epilogue to handle left-over bytes
    8920             :   //   [scratch, srcOut] = LDRB_POST(srcLoop, 1)
    8921             :   //   [destOut] = STRB_POST(scratch, destLoop, 1)
    8922         185 :   MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
    8923         185 :   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
    8924             :   MF->insert(It, loopMBB);
    8925             :   MF->insert(It, exitMBB);
    8926             : 
    8927             :   // Transfer the remainder of BB and its successor edges to exitMBB.
    8928         185 :   exitMBB->splice(exitMBB->begin(), BB,
    8929             :                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
    8930         185 :   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
    8931             : 
    8932             :   // Load an immediate to varEnd.
    8933         185 :   unsigned varEnd = MRI.createVirtualRegister(TRC);
    8934         185 :   if (Subtarget->useMovt(*MF)) {
    8935             :     unsigned Vtmp = varEnd;
    8936         138 :     if ((LoopSize & 0xFFFF0000) != 0)
    8937           0 :       Vtmp = MRI.createVirtualRegister(TRC);
    8938         209 :     BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVi16 : ARM::MOVi16), Vtmp)
    8939         138 :         .addImm(LoopSize & 0xFFFF)
    8940         138 :         .add(predOps(ARMCC::AL));
    8941             : 
    8942         138 :     if ((LoopSize & 0xFFFF0000) != 0)
    8943           0 :       BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVTi16 : ARM::MOVTi16), varEnd)
    8944           0 :           .addReg(Vtmp)
    8945           0 :           .addImm(LoopSize >> 16)
    8946           0 :           .add(predOps(ARMCC::AL));
    8947             :   } else {
    8948          47 :     MachineConstantPool *ConstantPool = MF->getConstantPool();
    8949          47 :     Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext());
    8950          47 :     const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
    8951             : 
    8952             :     // MachineConstantPool wants an explicit alignment.
    8953          47 :     unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
    8954          47 :     if (Align == 0)
    8955           0 :       Align = MF->getDataLayout().getTypeAllocSize(C->getType());
    8956          47 :     unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
    8957             : 
    8958          47 :     if (IsThumb)
    8959          88 :       BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci))
    8960          44 :           .addReg(varEnd, RegState::Define)
    8961             :           .addConstantPoolIndex(Idx)
    8962          44 :           .add(predOps(ARMCC::AL));
    8963             :     else
    8964           6 :       BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp))
    8965           3 :           .addReg(varEnd, RegState::Define)
    8966             :           .addConstantPoolIndex(Idx)
    8967             :           .addImm(0)
    8968           3 :           .add(predOps(ARMCC::AL));
    8969             :   }
    8970         185 :   BB->addSuccessor(loopMBB);
    8971             : 
    8972             :   // Generate the loop body:
    8973             :   //   varPhi = PHI(varLoop, varEnd)
    8974             :   //   srcPhi = PHI(srcLoop, src)
    8975             :   //   destPhi = PHI(destLoop, dst)
    8976             :   MachineBasicBlock *entryBB = BB;
    8977             :   BB = loopMBB;
    8978         185 :   unsigned varLoop = MRI.createVirtualRegister(TRC);
    8979         185 :   unsigned varPhi = MRI.createVirtualRegister(TRC);
    8980         185 :   unsigned srcLoop = MRI.createVirtualRegister(TRC);
    8981         185 :   unsigned srcPhi = MRI.createVirtualRegister(TRC);
    8982         185 :   unsigned destLoop = MRI.createVirtualRegister(TRC);
    8983         185 :   unsigned destPhi = MRI.createVirtualRegister(TRC);
    8984             : 
    8985         370 :   BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
    8986         185 :     .addReg(varLoop).addMBB(loopMBB)
    8987         185 :     .addReg(varEnd).addMBB(entryBB);
    8988         185 :   BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
    8989         185 :     .addReg(srcLoop).addMBB(loopMBB)
    8990         185 :     .addReg(src).addMBB(entryBB);
    8991         370 :   BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
    8992         185 :     .addReg(destLoop).addMBB(loopMBB)
    8993         185 :     .addReg(dest).addMBB(entryBB);
    8994             : 
    8995             :   //   [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
    8996             :   //   [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
    8997         343 :   unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
    8998         185 :   emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
    8999             :              IsThumb1, IsThumb2);
    9000         185 :   emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
    9001             :              IsThumb1, IsThumb2);
    9002             : 
    9003             :   // Decrement loop variable by UnitSize.
    9004         185 :   if (IsThumb1) {
    9005         150 :     BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop)
    9006          75 :         .add(t1CondCodeOp())
    9007          75 :         .addReg(varPhi)
    9008          75 :         .addImm(UnitSize)
    9009          75 :         .add(predOps(ARMCC::AL));
    9010             :   } else {
    9011             :     MachineInstrBuilder MIB =
    9012             :         BuildMI(*BB, BB->end(), dl,
    9013         258 :                 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
    9014         110 :     MIB.addReg(varPhi)
    9015         110 :         .addImm(UnitSize)
    9016         110 :         .add(predOps(ARMCC::AL))
    9017         110 :         .add(condCodeOp());
    9018         220 :     MIB->getOperand(5).setReg(ARM::CPSR);
    9019         220 :     MIB->getOperand(5).setIsDef(true);
    9020             :   }
    9021         185 :   BuildMI(*BB, BB->end(), dl,
    9022         333 :           TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
    9023         185 :       .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
    9024             : 
    9025             :   // loopMBB can loop back to loopMBB or fall through to exitMBB.
    9026         185 :   BB->addSuccessor(loopMBB);
    9027         185 :   BB->addSuccessor(exitMBB);
    9028             : 
    9029             :   // Add epilogue to handle BytesLeft.
    9030             :   BB = exitMBB;
    9031         185 :   auto StartOfExit = exitMBB->begin();
    9032             : 
    9033             :   //   [scratch, srcOut] = LDRB_POST(srcLoop, 1)
    9034             :   //   [destOut] = STRB_POST(scratch, destLoop, 1)
    9035             :   unsigned srcIn = srcLoop;
    9036             :   unsigned destIn = destLoop;
    9037         287 :   for (unsigned i = 0; i < BytesLeft; i++) {
    9038         102 :     unsigned srcOut = MRI.createVirtualRegister(TRC);
    9039         102 :     unsigned destOut = MRI.createVirtualRegister(TRC);
    9040         102 :     unsigned scratch = MRI.createVirtualRegister(TRC);
    9041         102 :     emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
    9042             :                IsThumb1, IsThumb2);
    9043         102 :     emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
    9044             :                IsThumb1, IsThumb2);
    9045             :     srcIn = srcOut;
    9046             :     destIn = destOut;
    9047             :   }
    9048             : 
    9049         185 :   MI.eraseFromParent(); // The instruction is gone now.
    9050         185 :   return BB;
    9051             : }
    9052             : 
    9053             : MachineBasicBlock *
    9054           7 : ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
    9055             :                                        MachineBasicBlock *MBB) const {
    9056           7 :   const TargetMachine &TM = getTargetMachine();
    9057           7 :   const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
    9058             :   DebugLoc DL = MI.getDebugLoc();
    9059             : 
    9060             :   assert(Subtarget->isTargetWindows() &&
    9061             :          "__chkstk is only supported on Windows");
    9062             :   assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
    9063             : 
    9064             :   // __chkstk takes the number of words to allocate on the stack in R4, and
    9065             :   // returns the stack adjustment in number of bytes in R4.  This will not
    9066             :   // clober any other registers (other than the obvious lr).
    9067             :   //
    9068             :   // Although, technically, IP should be considered a register which may be
    9069             :   // clobbered, the call itself will not touch it.  Windows on ARM is a pure
    9070             :   // thumb-2 environment, so there is no interworking required.  As a result, we
    9071             :   // do not expect a veneer to be emitted by the linker, clobbering IP.
    9072             :   //
    9073             :   // Each module receives its own copy of __chkstk, so no import thunk is
    9074             :   // required, again, ensuring that IP is not clobbered.
    9075             :   //
    9076             :   // Finally, although some linkers may theoretically provide a trampoline for
    9077             :   // out of range calls (which is quite common due to a 32M range limitation of
    9078             :   // branches for Thumb), we can generate the long-call version via
    9079             :   // -mcmodel=large, alleviating the need for the trampoline which may clobber
    9080             :   // IP.
    9081             : 
    9082           7 :   switch (TM.getCodeModel()) {
    9083             :   case CodeModel::Tiny:
    9084             :     llvm_unreachable("Tiny code model not available on ARM.");
    9085           6 :   case CodeModel::Small:
    9086             :   case CodeModel::Medium:
    9087             :   case CodeModel::Kernel:
    9088          12 :     BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
    9089           6 :         .add(predOps(ARMCC::AL))
    9090             :         .addExternalSymbol("__chkstk")
    9091           6 :         .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
    9092           6 :         .addReg(ARM::R4, RegState::Implicit | RegState::Define)
    9093             :         .addReg(ARM::R12,
    9094           6 :                 RegState::Implicit | RegState::Define | RegState::Dead)
    9095             :         .addReg(ARM::CPSR,
    9096           6 :                 RegState::Implicit | RegState::Define | RegState::Dead);
    9097           6 :     break;
    9098           1 :   case CodeModel::Large: {
    9099           1 :     MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
    9100           1 :     unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
    9101             : 
    9102           2 :     BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
    9103             :       .addExternalSymbol("__chkstk");
    9104           2 :     BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
    9105           1 :         .add(predOps(ARMCC::AL))
    9106           1 :         .addReg(Reg, RegState::Kill)
    9107           1 :         .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
    9108           1 :         .addReg(ARM::R4, RegState::Implicit | RegState::Define)
    9109             :         .addReg(ARM::R12,
    9110           1 :                 RegState::Implicit | RegState::Define | RegState::Dead)
    9111             :         .addReg(ARM::CPSR,
    9112           1 :                 RegState::Implicit | RegState::Define | RegState::Dead);
    9113           1 :     break;
    9114             :   }
    9115             :   }
    9116             : 
    9117          14 :   BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr), ARM::SP)
    9118           7 :       .addReg(ARM::SP, RegState::Kill)
    9119           7 :       .addReg(ARM::R4, RegState::Kill)
    9120             :       .setMIFlags(MachineInstr::FrameSetup)
    9121           7 :       .add(predOps(ARMCC::AL))
    9122           7 :       .add(condCodeOp());
    9123             : 
    9124           7 :   MI.eraseFromParent();
    9125           7 :   return MBB;
    9126             : }
    9127             : 
    9128             : MachineBasicBlock *
    9129          58 : ARMTargetLowering::EmitLowered__dbzchk(MachineInstr &MI,
    9130             :                                        MachineBasicBlock *MBB) const {
    9131             :   DebugLoc DL = MI.getDebugLoc();
    9132          58 :   MachineFunction *MF = MBB->getParent();
    9133          58 :   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
    9134             : 
    9135          58 :   MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
    9136          58 :   MF->insert(++MBB->getIterator(), ContBB);
    9137          58 :   ContBB->splice(ContBB->begin(), MBB,
    9138             :                  std::next(MachineBasicBlock::iterator(MI)), MBB->end());
    9139          58 :   ContBB->transferSuccessorsAndUpdatePHIs(MBB);
    9140          58 :   MBB->addSuccessor(ContBB);
    9141             : 
    9142          58 :   MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
    9143          58 :   BuildMI(TrapBB, DL, TII->get(ARM::t__brkdiv0));
    9144             :   MF->push_back(TrapBB);
    9145          58 :   MBB->addSuccessor(TrapBB);
    9146             : 
    9147         116 :   BuildMI(*MBB, MI, DL, TII->get(ARM::tCMPi8))
    9148          58 :       .addReg(MI.getOperand(0).getReg())
    9149             :       .addImm(0)
    9150          58 :       .add(predOps(ARMCC::AL));
    9151         116 :   BuildMI(*MBB, MI, DL, TII->get(ARM::t2Bcc))
    9152             :       .addMBB(TrapBB)
    9153             :       .addImm(ARMCC::EQ)
    9154          58 :       .addReg(ARM::CPSR);
    9155             : 
    9156          58 :   MI.eraseFromParent();
    9157          58 :   return ContBB;
    9158             : }
    9159             : 
    9160             : MachineBasicBlock *
    9161         589 : ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
    9162             :                                                MachineBasicBlock *BB) const {
    9163         589 :   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
    9164             :   DebugLoc dl = MI.getDebugLoc();
    9165         589 :   bool isThumb2 = Subtarget->isThumb2();
    9166        1178 :   switch (MI.getOpcode()) {
    9167           0 :   default: {
    9168           0 :     MI.print(errs());
    9169           0 :     llvm_unreachable("Unexpected instr type to insert");
    9170             :   }
    9171             : 
    9172             :   // Thumb1 post-indexed loads are really just single-register LDMs.
    9173           3 :   case ARM::tLDR_postidx: {
    9174           3 :     MachineOperand Def(MI.getOperand(1));
    9175           6 :     BuildMI(*BB, MI, dl, TII->get(ARM::tLDMIA_UPD))
    9176             :         .add(Def)  // Rn_wb
    9177           3 :         .add(MI.getOperand(2))  // Rn
    9178           3 :         .add(MI.getOperand(3))  // PredImm
    9179           3 :         .add(MI.getOperand(4))  // PredReg
    9180           3 :         .add(MI.getOperand(0)); // Rt
    9181           3 :     MI.eraseFromParent();
    9182             :     return BB;
    9183             :   }
    9184             : 
    9185             :   // The Thumb2 pre-indexed stores have the same MI operands, they just
    9186             :   // define them differently in the .td files from the isel patterns, so
    9187             :   // they need pseudos.
    9188           4 :   case ARM::t2STR_preidx:
    9189           4 :     MI.setDesc(TII->get(ARM::t2STR_PRE));
    9190           4 :     return BB;
    9191           0 :   case ARM::t2STRB_preidx:
    9192           0 :     MI.setDesc(TII->get(ARM::t2STRB_PRE));
    9193           0 :     return BB;
    9194           1 :   case ARM::t2STRH_preidx:
    9195           1 :     MI.setDesc(TII->get(ARM::t2STRH_PRE));
    9196           1 :     return BB;
    9197             : 
    9198          10 :   case ARM::STRi_preidx:
    9199             :   case ARM::STRBi_preidx: {
    9200          10 :     unsigned NewOpc = MI.getOpcode() == ARM::STRi_preidx ? ARM::STR_PRE_IMM
    9201             :                                                          : ARM::STRB_PRE_IMM;
    9202             :     // Decode the offset.
    9203          10 :     unsigned Offset = MI.getOperand(4).getImm();
    9204             :     bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
    9205             :     Offset = ARM_AM::getAM2Offset(Offset);
    9206             :     if (isSub)
    9207           0 :       Offset = -Offset;
    9208             : 
    9209          10 :     MachineMemOperand *MMO = *MI.memoperands_begin();
    9210          20 :     BuildMI(*BB, MI, dl, TII->get(NewOpc))
    9211          10 :         .add(MI.getOperand(0)) // Rn_wb
    9212          10 :         .add(MI.getOperand(1)) // Rt
    9213          10 :         .add(MI.getOperand(2)) // Rn
    9214          10 :         .addImm(Offset)        // offset (skip GPR==zero_reg)
    9215          10 :         .add(MI.getOperand(5)) // pred
    9216          10 :         .add(MI.getOperand(6))
    9217             :         .addMemOperand(MMO);
    9218          10 :     MI.eraseFromParent();
    9219          10 :     return BB;
    9220             :   }
    9221           3 :   case ARM::STRr_preidx:
    9222             :   case ARM::STRBr_preidx:
    9223             :   case ARM::STRH_preidx: {
    9224             :     unsigned NewOpc;
    9225             :     switch (MI.getOpcode()) {
    9226           0 :     default: llvm_unreachable("unexpected opcode!");
    9227             :     case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
    9228           0 :     case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
    9229           1 :     case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
    9230             :     }
    9231           6 :     MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
    9232          24 :     for (unsigned i = 0; i < MI.getNumOperands(); ++i)
    9233          21 :       MIB.add(MI.getOperand(i));
    9234           3 :     MI.eraseFromParent();
    9235             :     return BB;
    9236             :   }
    9237             : 
    9238          94 :   case ARM::tMOVCCr_pseudo: {
    9239             :     // To "insert" a SELECT_CC instruction, we actually have to insert the
    9240             :     // diamond control-flow pattern.  The incoming instruction knows the
    9241             :     // destination vreg to set, the condition code register to branch on, the
    9242             :     // true/false values to select between, and a branch opcode to use.
    9243          94 :     const BasicBlock *LLVM_BB = BB->getBasicBlock();
    9244          94 :     MachineFunction::iterator It = ++BB->getIterator();
    9245             : 
    9246             :     //  thisMBB:
    9247             :     //  ...
    9248             :     //   TrueVal = ...
    9249             :     //   cmpTY ccX, r1, r2
    9250             :     //   bCC copy1MBB
    9251             :     //   fallthrough --> copy0MBB
    9252             :     MachineBasicBlock *thisMBB  = BB;
    9253          94 :     MachineFunction *F = BB->getParent();
    9254          94 :     MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
    9255          94 :     MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
    9256             :     F->insert(It, copy0MBB);
    9257             :     F->insert(It, sinkMBB);
    9258             : 
    9259             :     // Transfer the remainder of BB and its successor edges to sinkMBB.
    9260          94 :     sinkMBB->splice(sinkMBB->begin(), BB,
    9261             :                     std::next(MachineBasicBlock::iterator(MI)), BB->end());
    9262          94 :     sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
    9263             : 
    9264          94 :     BB->addSuccessor(copy0MBB);
    9265          94 :     BB->addSuccessor(sinkMBB);
    9266             : 
    9267          94 :     BuildMI(BB, dl, TII->get(ARM::tBcc))
    9268             :         .addMBB(sinkMBB)
    9269          94 :         .addImm(MI.getOperand(3).getImm())
    9270          94 :         .addReg(MI.getOperand(4).getReg());
    9271             : 
    9272             :     //  copy0MBB:
    9273             :     //   %FalseValue = ...
    9274             :     //   # fallthrough to sinkMBB
    9275             :     BB = copy0MBB;
    9276             : 
    9277             :     // Update machine-CFG edges
    9278          94 :     BB->addSuccessor(sinkMBB);
    9279             : 
    9280             :     //  sinkMBB:
    9281             :     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
    9282             :     //  ...
    9283             :     BB = sinkMBB;
    9284         188 :     BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), MI.getOperand(0).getReg())
    9285          94 :         .addReg(MI.getOperand(1).getReg())
    9286             :         .addMBB(copy0MBB)
    9287          94 :         .addReg(MI.getOperand(2).getReg())
    9288             :         .addMBB(thisMBB);
    9289             : 
    9290          94 :     MI.eraseFromParent(); // The pseudo instruction is gone now.
    9291             :     return BB;
    9292             :   }
    9293             : 
    9294             :   case ARM::BCCi64:
    9295             :   case ARM::BCCZi64: {
    9296             :     // If there is an unconditional branch to the other successor, remove it.
    9297           1 :     BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
    9298             : 
    9299             :     // Compare both parts that make up the double comparison separately for
    9300             :     // equality.
    9301           1 :     bool RHSisZero = MI.getOpcode() == ARM::BCCZi64;
    9302             : 
    9303           1 :     unsigned LHS1 = MI.getOperand(1).getReg();
    9304           1 :     unsigned LHS2 = MI.getOperand(2).getReg();
    9305           1 :     if (RHSisZero) {
    9306           2 :       BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
    9307           1 :           .addReg(LHS1)
    9308             :           .addImm(0)
    9309           1 :           .add(predOps(ARMCC::AL));
    9310           2 :       BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
    9311           1 :         .addReg(LHS2).addImm(0)
    9312           1 :         .addImm(ARMCC::EQ).addReg(ARM::CPSR);
    9313             :     } else {
    9314           0 :       unsigned RHS1 = MI.getOperand(3).getReg();
    9315           0 :       unsigned RHS2 = MI.getOperand(4).getReg();
    9316           0 :       BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
    9317           0 :           .addReg(LHS1)
    9318           0 :           .addReg(RHS1)
    9319           0 :           .add(predOps(ARMCC::AL));
    9320           0 :       BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
    9321           0 :         .addReg(LHS2).addReg(RHS2)
    9322           0 :         .addImm(ARMCC::EQ).addReg(ARM::CPSR);
    9323             :     }
    9324             : 
    9325           1 :     MachineBasicBlock *destMBB = MI.getOperand(RHSisZero ? 3 : 5).getMBB();
    9326             :     MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
    9327           1 :     if (MI.getOperand(0).getImm() == ARMCC::NE)
    9328             :       std::swap(destMBB, exitMBB);
    9329             : 
    9330           2 :     BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
    9331           1 :       .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
    9332           1 :     if (isThumb2)
    9333           0 :       BuildMI(BB, dl, TII->get(ARM::t2B))
    9334             :           .addMBB(exitMBB)
    9335           0 :           .add(predOps(ARMCC::AL));
    9336             :     else
    9337           1 :       BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
    9338             : 
    9339           1 :     MI.eraseFromParent(); // The pseudo instruction is gone now.
    9340             :     return BB;
    9341             :   }
    9342             : 
    9343             :   case ARM::Int_eh_sjlj_setjmp:
    9344             :   case ARM::Int_eh_sjlj_setjmp_nofp:
    9345             :   case ARM::tInt_eh_sjlj_setjmp:
    9346             :   case ARM::t2Int_eh_sjlj_setjmp:
    9347             :   case ARM::t2Int_eh_sjlj_setjmp_nofp:
    9348             :     return BB;
    9349             : 
    9350          32 :   case ARM::Int_eh_sjlj_setup_dispatch:
    9351          32 :     EmitSjLjDispatchBlock(MI, BB);
    9352          32 :     return BB;
    9353             : 
    9354           8 :   case ARM::ABS:
    9355             :   case ARM::t2ABS: {
    9356             :     // To insert an ABS instruction, we have to insert the
    9357             :     // diamond control-flow pattern.  The incoming instruction knows the
    9358             :     // source vreg to test against 0, the destination vreg to set,
    9359             :     // the condition code register to branch on, the
    9360             :     // true/false values to select between, and a branch opcode to use.
    9361             :     // It transforms
    9362             :     //     V1 = ABS V0
    9363             :     // into
    9364             :     //     V2 = MOVS V0
    9365             :     //     BCC                      (branch to SinkBB if V0 >= 0)
    9366             :     //     RSBBB: V3 = RSBri V2, 0  (compute ABS if V2 < 0)
    9367             :     //     SinkBB: V1 = PHI(V2, V3)
    9368           8 :     const BasicBlock *LLVM_BB = BB->getBasicBlock();
    9369           8 :     MachineFunction::iterator BBI = ++BB->getIterator();
    9370           8 :     MachineFunction *Fn = BB->getParent();
    9371           8 :     MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
    9372           8 :     MachineBasicBlock *SinkBB  = Fn->CreateMachineBasicBlock(LLVM_BB);
    9373             :     Fn->insert(BBI, RSBBB);
    9374             :     Fn->insert(BBI, SinkBB);
    9375             : 
    9376           8 :     unsigned int ABSSrcReg = MI.getOperand(1).getReg();
    9377           8 :     unsigned int ABSDstReg = MI.getOperand(0).getReg();
    9378             :     bool ABSSrcKIll = MI.getOperand(1).isKill();
    9379           8 :     bool isThumb2 = Subtarget->isThumb2();
    9380           8 :     MachineRegisterInfo &MRI = Fn->getRegInfo();
    9381             :     // In Thumb mode S must not be specified if source register is the SP or
    9382             :     // PC and if destination register is the SP, so restrict register class
    9383             :     unsigned NewRsbDstReg =
    9384          11 :       MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
    9385             : 
    9386             :     // Transfer the remainder of BB and its successor edges to sinkMBB.
    9387           8 :     SinkBB->splice(SinkBB->begin(), BB,
    9388             :                    std::next(MachineBasicBlock::iterator(MI)), BB->end());
    9389           8 :     SinkBB->transferSuccessorsAndUpdatePHIs(BB);
    9390             : 
    9391           8 :     BB->addSuccessor(RSBBB);
    9392           8 :     BB->addSuccessor(SinkBB);
    9393             : 
    9394             :     // fall through to SinkMBB
    9395           8 :     RSBBB->addSuccessor(SinkBB);
    9396             : 
    9397             :     // insert a cmp at the end of BB
    9398          11 :     BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
    9399           8 :         .addReg(ABSSrcReg)
    9400             :         .addImm(0)
    9401           8 :         .add(predOps(ARMCC::AL));
    9402             : 
    9403             :     // insert a bcc with opposite CC to ARMCC::MI at the end of BB
    9404           8 :     BuildMI(BB, dl,
    9405          11 :       TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
    9406           8 :       .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
    9407             : 
    9408             :     // insert rsbri in RSBBB
    9409             :     // Note: BCC and rsbri will be converted into predicated rsbmi
    9410             :     // by if-conversion pass
    9411           8 :     BuildMI(*RSBBB, RSBBB->begin(), dl,
    9412          14 :             TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
    9413          15 :         .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
    9414             :         .addImm(0)
    9415           8 :         .add(predOps(ARMCC::AL))
    9416           8 :         .add(condCodeOp());
    9417             : 
    9418             :     // insert PHI in SinkBB,
    9419             :     // reuse ABSDstReg to not change uses of ABS instruction
    9420           8 :     BuildMI(*SinkBB, SinkBB->begin(), dl,
    9421          16 :       TII->get(ARM::PHI), ABSDstReg)
    9422           8 :       .addReg(NewRsbDstReg).addMBB(RSBBB)
    9423           8 :       .addReg(ABSSrcReg).addMBB(BB);
    9424             : 
    9425             :     // remove ABS instruction
    9426           8 :     MI.eraseFromParent();
    9427             : 
    9428             :     // return last added BB
    9429             :     return SinkBB;
    9430             :   }
    9431             :   case ARM::COPY_STRUCT_BYVAL_I32:
    9432             :     ++NumLoopByVals;
    9433         360 :     return EmitStructByval(MI, BB);
    9434           7 :   case ARM::WIN__CHKSTK:
    9435           7 :     return EmitLowered__chkstk(MI, BB);
    9436          58 :   case ARM::WIN__DBZCHK:
    9437          58 :     return EmitLowered__dbzchk(MI, BB);
    9438             :   }
    9439             : }
    9440             : 
    9441             : /// Attaches vregs to MEMCPY that it will use as scratch registers
    9442             : /// when it is expanded into LDM/STM. This is done as a post-isel lowering
    9443             : /// instead of as a custom inserter because we need the use list from the SDNode.
    9444          56 : static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
    9445             :                                     MachineInstr &MI, const SDNode *Node) {
    9446          56 :   bool isThumb1 = Subtarget->isThumb1Only();
    9447             : 
    9448             :   DebugLoc DL = MI.getDebugLoc();
    9449          56 :   MachineFunction *MF = MI.getParent()->getParent();
    9450          56 :   MachineRegisterInfo &MRI = MF->getRegInfo();
    9451             :   MachineInstrBuilder MIB(*MF, MI);
    9452             : 
    9453             :   // If the new dst/src is unused mark it as dead.
    9454          56 :   if (!Node->hasAnyUseOfValue(0)) {
    9455          26 :     MI.getOperand(0).setIsDead(true);
    9456             :   }
    9457          56 :   if (!Node->hasAnyUseOfValue(1)) {
    9458          26 :     MI.getOperand(1).setIsDead(true);
    9459             :   }
    9460             : 
    9461             :   // The MEMCPY both defines and kills the scratch registers.
    9462         285 :   for (unsigned I = 0; I != MI.getOperand(4).getImm(); ++I) {
    9463         362 :     unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
    9464             :                                                          : &ARM::GPRRegClass);
    9465         229 :     MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
    9466             :   }
    9467          56 : }
    9468             : 
    9469        1105 : void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
    9470             :                                                       SDNode *Node) const {
    9471        2210 :   if (MI.getOpcode() == ARM::MEMCPY) {
    9472          56 :     attachMEMCPYScratchRegs(Subtarget, MI, Node);
    9473          56 :     return;
    9474             :   }
    9475             : 
    9476             :   const MCInstrDesc *MCID = &MI.getDesc();
    9477             :   // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
    9478             :   // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
    9479             :   // operand is still set to noreg. If needed, set the optional operand's
    9480             :   // register to CPSR, and remove the redundant implicit def.
    9481             :   //
    9482             :   // e.g. ADCS (..., implicit-def CPSR) -> ADC (... opt:def CPSR).
    9483             : 
    9484             :   // Rename pseudo opcodes.
    9485        1049 :   unsigned NewOpc = convertAddSubFlagsOpcode(MI.getOpcode());
    9486             :   unsigned ccOutIdx;
    9487        1049 :   if (NewOpc) {
    9488         653 :     const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
    9489         653 :     MCID = &TII->get(NewOpc);
    9490             : 
    9491             :     assert(MCID->getNumOperands() ==
    9492             :            MI.getDesc().getNumOperands() + 5 - MI.getDesc().getSize()
    9493             :         && "converted opcode should be the same except for cc_out"
    9494             :            " (and, on Thumb1, pred)");
    9495             : 
    9496             :     MI.setDesc(*MCID);
    9497             : 
    9498             :     // Add the optional cc_out operand
    9499         653 :     MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
    9500             : 
    9501             :     // On Thumb1, move all input operands to the end, then add the predicate
    9502         653 :     if (Subtarget->isThumb1Only()) {
    9503        1180 :       for (unsigned c = MCID->getNumOperands() - 4; c--;) {
    9504        1180 :         MI.addOperand(MI.getOperand(1));
    9505         590 :         MI.RemoveOperand(1);
    9506             :       }
    9507             : 
    9508             :       // Restore the ties
    9509        1921 :       for (unsigned i = MI.getNumOperands(); i--;) {
    9510        1626 :         const MachineOperand& op = MI.getOperand(i);
    9511        1626 :         if (op.isReg() && op.isUse()) {
    9512         707 :           int DefIdx = MCID->getOperandConstraint(i, MCOI::TIED_TO);
    9513             :           if (DefIdx != -1)
    9514         163 :             MI.tieOperands(DefIdx, i);
    9515             :         }
    9516             :       }
    9517             : 
    9518         295 :       MI.addOperand(MachineOperand::CreateImm(ARMCC::AL));
    9519         295 :       MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/false));
    9520             :       ccOutIdx = 1;
    9521             :     } else
    9522         716 :       ccOutIdx = MCID->getNumOperands() - 1;
    9523             :   } else
    9524         792 :     ccOutIdx = MCID->getNumOperands() - 1;
    9525             : 
    9526             :   // Any ARM instruction that sets the 's' bit should specify an optional
    9527             :   // "cc_out" operand in the last operand position.
    9528        1049 :   if (!MI.hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
    9529             :     assert(!NewOpc && "Optional cc_out operand required");
    9530             :     return;
    9531             :   }
    9532             :   // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
    9533             :   // since we already have an optional CPSR def.
    9534             :   bool definesCPSR = false;
    9535             :   bool deadCPSR = false;
    9536        2098 :   for (unsigned i = MCID->getNumOperands(), e = MI.getNumOperands(); i != e;
    9537             :        ++i) {
    9538        1049 :     const MachineOperand &MO = MI.getOperand(i);
    9539        1049 :     if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
    9540             :       definesCPSR = true;
    9541        1049 :       if (MO.isDead())
    9542             :         deadCPSR = true;
    9543        1049 :       MI.RemoveOperand(i);
    9544        1049 :       break;
    9545             :     }
    9546             :   }
    9547        1049 :   if (!definesCPSR) {
    9548             :     assert(!NewOpc && "Optional cc_out operand required");
    9549             :     return;
    9550             :   }
    9551             :   assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
    9552        1049 :   if (deadCPSR) {
    9553             :     assert(!MI.getOperand(ccOutIdx).getReg() &&
    9554             :            "expect uninitialized optional cc_out operand");
    9555             :     // Thumb1 instructions must have the S bit even if the CPSR is dead.
    9556         418 :     if (!Subtarget->isThumb1Only())
    9557             :       return;
    9558             :   }
    9559             : 
    9560             :   // If this instruction was defined with an optional CPSR def and its dag node
    9561             :   // had a live implicit CPSR def, then activate the optional CPSR def.
    9562         769 :   MachineOperand &MO = MI.getOperand(ccOutIdx);
    9563         769 :   MO.setReg(ARM::CPSR);
    9564         769 :   MO.setIsDef(true);
    9565             : }
    9566             : 
    9567             : //===----------------------------------------------------------------------===//
    9568             : //                           ARM Optimization Hooks
    9569             : //===----------------------------------------------------------------------===//
    9570             : 
    9571             : // Helper function that checks if N is a null or all ones constant.
    9572             : static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
    9573         245 :   return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
    9574             : }
    9575             : 
    9576             : // Return true if N is conditionally 0 or all ones.
    9577             : // Detects these expressions where cc is an i1 value:
    9578             : //
    9579             : //   (select cc 0, y)   [AllOnes=0]
    9580             : //   (select cc y, 0)   [AllOnes=0]
    9581             : //   (zext cc)          [AllOnes=0]
    9582             : //   (sext cc)          [AllOnes=0/1]
    9583             : //   (select cc -1, y)  [AllOnes=1]
    9584             : //   (select cc y, -1)  [AllOnes=1]
    9585             : //
    9586             : // Invert is set when N is the null/all ones constant when CC is false.
    9587             : // OtherOp is set to the alternative value of N.
    9588       34014 : static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
    9589             :                                        SDValue &CC, bool &Invert,
    9590             :                                        SDValue &OtherOp,
    9591             :                                        SelectionDAG &DAG) {
    9592       68028 :   switch (N->getOpcode()) {
    9593             :   default: return false;
    9594         130 :   case ISD::SELECT: {
    9595         130 :     CC = N->getOperand(0);
    9596         130 :     SDValue N1 = N->getOperand(1);
    9597         130 :     SDValue N2 = N->getOperand(2);
    9598         130 :     if (isZeroOrAllOnes(N1, AllOnes)) {
    9599          15 :       Invert = false;
    9600          15 :       OtherOp = N2;
    9601          15 :       return true;
    9602             :     }
    9603         115 :     if (isZeroOrAllOnes(N2, AllOnes)) {
    9604          24 :       Invert = true;
    9605          24 :       OtherOp = N1;
    9606          24 :       return true;
    9607             :     }
    9608             :     return false;
    9609             :   }
    9610         287 :   case ISD::ZERO_EXTEND:
    9611             :     // (zext cc) can never be the all ones value.
    9612         287 :     if (AllOnes)
    9613             :       return false;
    9614             :     LLVM_FALLTHROUGH;
    9615             :   case ISD::SIGN_EXTEND: {
    9616             :     SDLoc dl(N);
    9617         468 :     EVT VT = N->getValueType(0);
    9618         468 :     CC = N->getOperand(0);
    9619         468 :     if (CC.getValueType() != MVT::i1 || CC.getOpcode() != ISD::SETCC)
    9620             :       return false;
    9621          13 :     Invert = !AllOnes;
    9622          13 :     if (AllOnes)
    9623             :       // When looking for an AllOnes constant, N is an sext, and the 'other'
    9624             :       // value is 0.
    9625           0 :       OtherOp = DAG.getConstant(0, dl, VT);
    9626          13 :     else if (N->getOpcode() == ISD::ZERO_EXTEND)
    9627             :       // When looking for a 0 constant, N can be zext or sext.
    9628          13 :       OtherOp = DAG.getConstant(1, dl, VT);
    9629             :     else
    9630           0 :       OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
    9631           0 :                                 VT);
    9632             :     return true;
    9633             :   }
    9634             :   }
    9635             : }
    9636             : 
    9637             : // Combine a constant select operand into its use:
    9638             : //
    9639             : //   (add (select cc, 0, c), x)  -> (select cc, x, (add, x, c))
    9640             : //   (sub x, (select cc, 0, c))  -> (select cc, x, (sub, x, c))
    9641             : //   (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))  [AllOnes=1]
    9642             : //   (or  (select cc, 0, c), x)  -> (select cc, x, (or, x, c))
    9643             : //   (xor (select cc, 0, c), x)  -> (select cc, x, (xor, x, c))
    9644             : //
    9645             : // The transform is rejected if the select doesn't have a constant operand that
    9646             : // is null, or all ones when AllOnes is set.
    9647             : //
    9648             : // Also recognize sext/zext from i1:
    9649             : //
    9650             : //   (add (zext cc), x) -> (select cc (add x, 1), x)
    9651             : //   (add (sext cc), x) -> (select cc (add x, -1), x)
    9652             : //
    9653             : // These transformations eventually create predicated instructions.
    9654             : //
    9655             : // @param N       The node to transform.
    9656             : // @param Slct    The N operand that is a select.
    9657             : // @param OtherOp The other N operand (x above).
    9658             : // @param DCI     Context.
    9659             : // @param AllOnes Require the select constant to be all ones instead of null.
    9660             : // @returns The new node, or SDValue() on failure.
    9661             : static
    9662       34014 : SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
    9663             :                             TargetLowering::DAGCombinerInfo &DCI,
    9664             :                             bool AllOnes = false) {
    9665       34014 :   SelectionDAG &DAG = DCI.DAG;
    9666       34014 :   EVT VT = N->getValueType(0);
    9667       34014 :   SDValue NonConstantVal;
    9668       34014 :   SDValue CCOp;
    9669             :   bool SwapSelectOps;
    9670       34014 :   if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
    9671             :                                   NonConstantVal, DAG))
    9672       33962 :     return SDValue();
    9673             : 
    9674             :   // Slct is now know to be the desired identity constant when CC is true.
    9675          52 :   SDValue TrueVal = OtherOp;
    9676          52 :   SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
    9677         104 :                                  OtherOp, NonConstantVal);
    9678             :   // Unless SwapSelectOps says CC should be false.
    9679          52 :   if (SwapSelectOps)
    9680             :     std::swap(TrueVal, FalseVal);
    9681             : 
    9682          52 :   return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
    9683         104 :                      CCOp, TrueVal, FalseVal);
    9684             : }
    9685             : 
    9686             : // Attempt combineSelectAndUse on each operand of a commutative operator N.
    9687             : static
    9688        7518 : SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
    9689             :                                        TargetLowering::DAGCombinerInfo &DCI) {
    9690        7518 :   SDValue N0 = N->getOperand(0);
    9691        7518 :   SDValue N1 = N->getOperand(1);
    9692             :   if (N0.getNode()->hasOneUse())
    9693        5684 :     if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
    9694          12 :       return Result;
    9695             :   if (N1.getNode()->hasOneUse())
    9696        5654 :     if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
    9697           2 :       return Result;
    9698        7504 :   return SDValue();
    9699             : }
    9700             : 
    9701             : static bool IsVUZPShuffleNode(SDNode *N) {
    9702             :   // VUZP shuffle node.
    9703           0 :   if (N->getOpcode() == ARMISD::VUZP)
    9704             :     return true;
    9705             : 
    9706             :   // "VUZP" on i32 is an alias for VTRN.
    9707           0 :   if (N->getOpcode() == ARMISD::VTRN && N->getValueType(0) == MVT::v2i32)
    9708             :     return true;
    9709             : 
    9710             :   return false;
    9711             : }
    9712             : 
    9713           0 : static SDValue AddCombineToVPADD(SDNode *N, SDValue N0, SDValue N1,
    9714             :                                  TargetLowering::DAGCombinerInfo &DCI,
    9715             :                                  const ARMSubtarget *Subtarget) {
    9716             :   // Look for ADD(VUZP.0, VUZP.1).
    9717           0 :   if (!IsVUZPShuffleNode(N0.getNode()) || N0.getNode() != N1.getNode() ||
    9718             :       N0 == N1)
    9719           0 :    return SDValue();
    9720             : 
    9721             :   // Make sure the ADD is a 64-bit add; there is no 128-bit VPADD.
    9722           0 :   if (!N->getValueType(0).is64BitVector())
    9723           0 :     return SDValue();
    9724             : 
    9725             :   // Generate vpadd.
    9726           0 :   SelectionDAG &DAG = DCI.DAG;
    9727             :   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
    9728             :   SDLoc dl(N);
    9729             :   SDNode *Unzip = N0.getNode();
    9730           0 :   EVT VT = N->getValueType(0);
    9731             : 
    9732             :   SmallVector<SDValue, 8> Ops;
    9733           0 :   Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpadd, dl,
    9734           0 :                                 TLI.getPointerTy(DAG.getDataLayout())));
    9735           0 :   Ops.push_back(Unzip->getOperand(0));
    9736           0 :   Ops.push_back(Unzip->getOperand(1));
    9737             : 
    9738           0 :   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops);
    9739             : }
    9740             : 
    9741           0 : static SDValue AddCombineVUZPToVPADDL(SDNode *N, SDValue N0, SDValue N1,
    9742             :                                       TargetLowering::DAGCombinerInfo &DCI,
    9743             :                                       const ARMSubtarget *Subtarget) {
    9744             :   // Check for two extended operands.
    9745           0 :   if (!(N0.getOpcode() == ISD::SIGN_EXTEND &&
    9746           0 :         N1.getOpcode() == ISD::SIGN_EXTEND) &&
    9747           0 :       !(N0.getOpcode() == ISD::ZERO_EXTEND &&
    9748             :         N1.getOpcode() == ISD::ZERO_EXTEND))
    9749           0 :     return SDValue();
    9750             : 
    9751           0 :   SDValue N00 = N0.getOperand(0);
    9752           0 :   SDValue N10 = N1.getOperand(0);
    9753             : 
    9754             :   // Look for ADD(SEXT(VUZP.0), SEXT(VUZP.1))
    9755           0 :   if (!IsVUZPShuffleNode(N00.getNode()) || N00.getNode() != N10.getNode() ||
    9756             :       N00 == N10)
    9757           0 :     return SDValue();
    9758             : 
    9759             :   // We only recognize Q register paddl here; this can't be reached until
    9760             :   // after type legalization.
    9761           0 :   if (!N00.getValueType().is64BitVector() ||
    9762           0 :       !N0.getValueType().is128BitVector())
    9763           0 :     return SDValue();
    9764             : 
    9765             :   // Generate vpaddl.
    9766           0 :   SelectionDAG &DAG = DCI.DAG;
    9767             :   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
    9768             :   SDLoc dl(N);
    9769           0 :   EVT VT = N->getValueType(0);
    9770             : 
    9771             :   SmallVector<SDValue, 8> Ops;
    9772             :   // Form vpaddl.sN or vpaddl.uN depending on the kind of extension.
    9773             :   unsigned Opcode;
    9774           0 :   if (N0.getOpcode() == ISD::SIGN_EXTEND)
    9775             :     Opcode = Intrinsic::arm_neon_vpaddls;
    9776             :   else
    9777             :     Opcode = Intrinsic::arm_neon_vpaddlu;
    9778           0 :   Ops.push_back(DAG.getConstant(Opcode, dl,
    9779           0 :                                 TLI.getPointerTy(DAG.getDataLayout())));
    9780           0 :   EVT ElemTy = N00.getValueType().getVectorElementType();
    9781             :   unsigned NumElts = VT.getVectorNumElements();
    9782           0 :   EVT ConcatVT = EVT::getVectorVT(*DAG.getContext(), ElemTy, NumElts * 2);
    9783           0 :   SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), ConcatVT,
    9784           0 :                                N00.getOperand(0), N00.getOperand(1));
    9785           0 :   Ops.push_back(Concat);
    9786             : 
    9787           0 :   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops);
    9788             : }
    9789             : 
    9790             : // FIXME: This function shouldn't be necessary; if we lower BUILD_VECTOR in
    9791             : // an appropriate manner, we end up with ADD(VUZP(ZEXT(N))), which is
    9792             : // much easier to match.
    9793             : static SDValue
    9794           0 : AddCombineBUILD_VECTORToVPADDL(SDNode *N, SDValue N0, SDValue N1,
    9795             :                                TargetLowering::DAGCombinerInfo &DCI,
    9796             :                                const ARMSubtarget *Subtarget) {
    9797             :   // Only perform optimization if after legalize, and if NEON is available. We
    9798             :   // also expected both operands to be BUILD_VECTORs.
    9799           0 :   if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
    9800           0 :       || N0.getOpcode() != ISD::BUILD_VECTOR
    9801           0 :       || N1.getOpcode() != ISD::BUILD_VECTOR)
    9802           0 :     return SDValue();
    9803             : 
    9804             :   // Check output type since VPADDL operand elements can only be 8, 16, or 32.
    9805           0 :   EVT VT = N->getValueType(0);
    9806           0 :   if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
    9807           0 :     return SDValue();
    9808             : 
    9809             :   // Check that the vector operands are of the right form.
    9810             :   // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
    9811             :   // operands, where N is the size of the formed vector.
    9812             :   // Each EXTRACT_VECTOR should have the same input vector and odd or even
    9813             :   // index such that we have a pair wise add pattern.
    9814             : 
    9815             :   // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
    9816           0 :   if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
    9817           0 :     return SDValue();
    9818           0 :   SDValue Vec = N0->getOperand(0)->getOperand(0);
    9819           0 :   SDNode *V = Vec.getNode();
    9820             :   unsigned nextIndex = 0;
    9821             : 
    9822             :   // For each operands to the ADD which are BUILD_VECTORs,
    9823             :   // check to see if each of their operands are an EXTRACT_VECTOR with
    9824             :   // the same vector and appropriate index.
    9825           0 :   for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {