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 "ARMCallingConv.h" 17 #include "ARMConstantPoolValue.h" 18 #include "ARMMachineFunctionInfo.h" 19 #include "ARMPerfectShuffle.h" 20 #include "ARMSubtarget.h" 21 #include "ARMTargetMachine.h" 22 #include "ARMTargetObjectFile.h" 23 #include "MCTargetDesc/ARMAddressingModes.h" 24 #include "llvm/ADT/Statistic.h" 25 #include "llvm/ADT/StringExtras.h" 26 #include "llvm/CodeGen/CallingConvLower.h" 27 #include "llvm/CodeGen/IntrinsicLowering.h" 28 #include "llvm/CodeGen/MachineBasicBlock.h" 29 #include "llvm/CodeGen/MachineFrameInfo.h" 30 #include "llvm/CodeGen/MachineFunction.h" 31 #include "llvm/CodeGen/MachineInstrBuilder.h" 32 #include "llvm/CodeGen/MachineJumpTableInfo.h" 33 #include "llvm/CodeGen/MachineModuleInfo.h" 34 #include "llvm/CodeGen/MachineRegisterInfo.h" 35 #include "llvm/CodeGen/SelectionDAG.h" 36 #include "llvm/IR/CallingConv.h" 37 #include "llvm/IR/Constants.h" 38 #include "llvm/IR/Function.h" 39 #include "llvm/IR/GlobalValue.h" 40 #include "llvm/IR/IRBuilder.h" 41 #include "llvm/IR/Instruction.h" 42 #include "llvm/IR/Instructions.h" 43 #include "llvm/IR/Intrinsics.h" 44 #include "llvm/IR/Type.h" 45 #include "llvm/MC/MCSectionMachO.h" 46 #include "llvm/Support/CommandLine.h" 47 #include "llvm/Support/Debug.h" 48 #include "llvm/Support/ErrorHandling.h" 49 #include "llvm/Support/MathExtras.h" 50 #include "llvm/Target/TargetOptions.h" 51 #include <utility> 52 using namespace llvm; 53 54 #define DEBUG_TYPE "arm-isel" 55 56 STATISTIC(NumTailCalls, "Number of tail calls"); 57 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt"); 58 STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments"); 59 60 cl::opt<bool> 61 EnableARMLongCalls("arm-long-calls", cl::Hidden, 62 cl::desc("Generate calls via indirect call instructions"), 63 cl::init(false)); 64 65 static cl::opt<bool> 66 ARMInterworking("arm-interworking", cl::Hidden, 67 cl::desc("Enable / disable ARM interworking (for debugging only)"), 68 cl::init(true)); 69 70 namespace { 71 class ARMCCState : public CCState { 72 public: 73 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, 74 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C, 75 ParmContext PC) 76 : CCState(CC, isVarArg, MF, locs, C) { 77 assert(((PC == Call) || (PC == Prologue)) && 78 "ARMCCState users must specify whether their context is call" 79 "or prologue generation."); 80 CallOrPrologue = PC; 81 } 82 }; 83 } 84 85 // The APCS parameter registers. 86 static const MCPhysReg GPRArgRegs[] = { 87 ARM::R0, ARM::R1, ARM::R2, ARM::R3 88 }; 89 90 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT, 91 MVT PromotedBitwiseVT) { 92 if (VT != PromotedLdStVT) { 93 setOperationAction(ISD::LOAD, VT, Promote); 94 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT); 95 96 setOperationAction(ISD::STORE, VT, Promote); 97 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT); 98 } 99 100 MVT ElemTy = VT.getVectorElementType(); 101 if (ElemTy != MVT::i64 && ElemTy != MVT::f64) 102 setOperationAction(ISD::SETCC, VT, Custom); 103 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 104 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 105 if (ElemTy == MVT::i32) { 106 setOperationAction(ISD::SINT_TO_FP, VT, Custom); 107 setOperationAction(ISD::UINT_TO_FP, VT, Custom); 108 setOperationAction(ISD::FP_TO_SINT, VT, Custom); 109 setOperationAction(ISD::FP_TO_UINT, VT, Custom); 110 } else { 111 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 112 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 113 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 114 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 115 } 116 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 117 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 118 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal); 119 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); 120 setOperationAction(ISD::SELECT, VT, Expand); 121 setOperationAction(ISD::SELECT_CC, VT, Expand); 122 setOperationAction(ISD::VSELECT, VT, Expand); 123 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 124 if (VT.isInteger()) { 125 setOperationAction(ISD::SHL, VT, Custom); 126 setOperationAction(ISD::SRA, VT, Custom); 127 setOperationAction(ISD::SRL, VT, Custom); 128 } 129 130 // Promote all bit-wise operations. 131 if (VT.isInteger() && VT != PromotedBitwiseVT) { 132 setOperationAction(ISD::AND, VT, Promote); 133 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT); 134 setOperationAction(ISD::OR, VT, Promote); 135 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT); 136 setOperationAction(ISD::XOR, VT, Promote); 137 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT); 138 } 139 140 // Neon does not support vector divide/remainder operations. 141 setOperationAction(ISD::SDIV, VT, Expand); 142 setOperationAction(ISD::UDIV, VT, Expand); 143 setOperationAction(ISD::FDIV, VT, Expand); 144 setOperationAction(ISD::SREM, VT, Expand); 145 setOperationAction(ISD::UREM, VT, Expand); 146 setOperationAction(ISD::FREM, VT, Expand); 147 } 148 149 void ARMTargetLowering::addDRTypeForNEON(MVT VT) { 150 addRegisterClass(VT, &ARM::DPRRegClass); 151 addTypeForNEON(VT, MVT::f64, MVT::v2i32); 152 } 153 154 void ARMTargetLowering::addQRTypeForNEON(MVT VT) { 155 addRegisterClass(VT, &ARM::DPairRegClass); 156 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32); 157 } 158 159 static TargetLoweringObjectFile *createTLOF(const Triple &TT) { 160 if (TT.isOSBinFormatMachO()) 161 return new TargetLoweringObjectFileMachO(); 162 if (TT.isOSWindows()) 163 return new TargetLoweringObjectFileCOFF(); 164 return new ARMElfTargetObjectFile(); 165 } 166 167 ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM) 168 : TargetLowering(TM, createTLOF(Triple(TM.getTargetTriple()))) { 169 Subtarget = &TM.getSubtarget<ARMSubtarget>(); 170 RegInfo = TM.getSubtargetImpl()->getRegisterInfo(); 171 Itins = TM.getSubtargetImpl()->getInstrItineraryData(); 172 173 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 174 175 if (Subtarget->isTargetMachO()) { 176 // Uses VFP for Thumb libfuncs if available. 177 if (Subtarget->isThumb() && Subtarget->hasVFP2() && 178 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) { 179 // Single-precision floating-point arithmetic. 180 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp"); 181 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp"); 182 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp"); 183 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp"); 184 185 // Double-precision floating-point arithmetic. 186 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp"); 187 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp"); 188 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp"); 189 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp"); 190 191 // Single-precision comparisons. 192 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp"); 193 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp"); 194 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp"); 195 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp"); 196 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp"); 197 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp"); 198 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp"); 199 setLibcallName(RTLIB::O_F32, "__unordsf2vfp"); 200 201 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 202 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE); 203 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 204 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 205 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 206 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 207 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 208 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 209 210 // Double-precision comparisons. 211 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp"); 212 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp"); 213 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp"); 214 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp"); 215 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp"); 216 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp"); 217 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp"); 218 setLibcallName(RTLIB::O_F64, "__unorddf2vfp"); 219 220 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); 221 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE); 222 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); 223 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); 224 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); 225 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); 226 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); 227 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); 228 229 // Floating-point to integer conversions. 230 // i64 conversions are done via library routines even when generating VFP 231 // instructions, so use the same ones. 232 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp"); 233 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp"); 234 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp"); 235 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp"); 236 237 // Conversions between floating types. 238 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp"); 239 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp"); 240 241 // Integer to floating-point conversions. 242 // i64 conversions are done via library routines even when generating VFP 243 // instructions, so use the same ones. 244 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 245 // e.g., __floatunsidf vs. __floatunssidfvfp. 246 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp"); 247 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp"); 248 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp"); 249 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp"); 250 } 251 } 252 253 // These libcalls are not available in 32-bit. 254 setLibcallName(RTLIB::SHL_I128, nullptr); 255 setLibcallName(RTLIB::SRL_I128, nullptr); 256 setLibcallName(RTLIB::SRA_I128, nullptr); 257 258 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() && 259 !Subtarget->isTargetWindows()) { 260 static const struct { 261 const RTLIB::Libcall Op; 262 const char * const Name; 263 const CallingConv::ID CC; 264 const ISD::CondCode Cond; 265 } LibraryCalls[] = { 266 // Double-precision floating-point arithmetic helper functions 267 // RTABI chapter 4.1.2, Table 2 268 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 269 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 270 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 271 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 272 273 // Double-precision floating-point comparison helper functions 274 // RTABI chapter 4.1.2, Table 3 275 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 276 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 277 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 278 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 279 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 280 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 281 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 282 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 283 284 // Single-precision floating-point arithmetic helper functions 285 // RTABI chapter 4.1.2, Table 4 286 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 287 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 288 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 289 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 290 291 // Single-precision floating-point comparison helper functions 292 // RTABI chapter 4.1.2, Table 5 293 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 294 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 295 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 296 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 297 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 298 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 299 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 300 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 301 302 // Floating-point to integer conversions. 303 // RTABI chapter 4.1.2, Table 6 304 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 305 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 306 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 307 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 308 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 309 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 310 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 311 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 312 313 // Conversions between floating types. 314 // RTABI chapter 4.1.2, Table 7 315 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 316 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 317 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 318 319 // Integer to floating-point conversions. 320 // RTABI chapter 4.1.2, Table 8 321 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 322 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 323 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 324 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 325 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 326 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 327 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 328 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 329 330 // Long long helper functions 331 // RTABI chapter 4.2, Table 9 332 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 333 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 334 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 335 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 336 337 // Integer division functions 338 // RTABI chapter 4.3.1 339 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 340 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 341 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 342 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 343 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 344 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 345 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 346 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 347 348 // Memory operations 349 // RTABI chapter 4.3.4 350 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 351 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 352 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 353 }; 354 355 for (const auto &LC : LibraryCalls) { 356 setLibcallName(LC.Op, LC.Name); 357 setLibcallCallingConv(LC.Op, LC.CC); 358 if (LC.Cond != ISD::SETCC_INVALID) 359 setCmpLibcallCC(LC.Op, LC.Cond); 360 } 361 } 362 363 if (Subtarget->isTargetWindows()) { 364 static const struct { 365 const RTLIB::Libcall Op; 366 const char * const Name; 367 const CallingConv::ID CC; 368 } LibraryCalls[] = { 369 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP }, 370 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP }, 371 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP }, 372 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP }, 373 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP }, 374 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP }, 375 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP }, 376 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP }, 377 }; 378 379 for (const auto &LC : LibraryCalls) { 380 setLibcallName(LC.Op, LC.Name); 381 setLibcallCallingConv(LC.Op, LC.CC); 382 } 383 } 384 385 // Use divmod compiler-rt calls for iOS 5.0 and later. 386 if (Subtarget->getTargetTriple().isiOS() && 387 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) { 388 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 389 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 390 } 391 392 // The half <-> float conversion functions are always soft-float, but are 393 // needed for some targets which use a hard-float calling convention by 394 // default. 395 if (Subtarget->isAAPCS_ABI()) { 396 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS); 397 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS); 398 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS); 399 } else { 400 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS); 401 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS); 402 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS); 403 } 404 405 if (Subtarget->isThumb1Only()) 406 addRegisterClass(MVT::i32, &ARM::tGPRRegClass); 407 else 408 addRegisterClass(MVT::i32, &ARM::GPRRegClass); 409 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 410 !Subtarget->isThumb1Only()) { 411 addRegisterClass(MVT::f32, &ARM::SPRRegClass); 412 addRegisterClass(MVT::f64, &ARM::DPRRegClass); 413 } 414 415 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 416 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) { 417 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; 418 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT) 419 setTruncStoreAction((MVT::SimpleValueType)VT, 420 (MVT::SimpleValueType)InnerVT, Expand); 421 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand); 422 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand); 423 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand); 424 425 setOperationAction(ISD::MULHS, (MVT::SimpleValueType)VT, Expand); 426 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand); 427 setOperationAction(ISD::MULHU, (MVT::SimpleValueType)VT, Expand); 428 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand); 429 430 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand); 431 } 432 433 setOperationAction(ISD::ConstantFP, MVT::f32, Custom); 434 setOperationAction(ISD::ConstantFP, MVT::f64, Custom); 435 436 if (Subtarget->hasNEON()) { 437 addDRTypeForNEON(MVT::v2f32); 438 addDRTypeForNEON(MVT::v8i8); 439 addDRTypeForNEON(MVT::v4i16); 440 addDRTypeForNEON(MVT::v2i32); 441 addDRTypeForNEON(MVT::v1i64); 442 443 addQRTypeForNEON(MVT::v4f32); 444 addQRTypeForNEON(MVT::v2f64); 445 addQRTypeForNEON(MVT::v16i8); 446 addQRTypeForNEON(MVT::v8i16); 447 addQRTypeForNEON(MVT::v4i32); 448 addQRTypeForNEON(MVT::v2i64); 449 450 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 451 // neither Neon nor VFP support any arithmetic operations on it. 452 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively 453 // supported for v4f32. 454 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 455 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 456 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 457 // FIXME: Code duplication: FDIV and FREM are expanded always, see 458 // ARMTargetLowering::addTypeForNEON method for details. 459 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 460 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 461 // FIXME: Create unittest. 462 // In another words, find a way when "copysign" appears in DAG with vector 463 // operands. 464 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 465 // FIXME: Code duplication: SETCC has custom operation action, see 466 // ARMTargetLowering::addTypeForNEON method for details. 467 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 468 // FIXME: Create unittest for FNEG and for FABS. 469 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 470 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 471 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 472 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 473 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 474 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand); 475 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 476 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 477 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 478 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 479 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 480 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 481 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR. 482 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 483 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 484 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 485 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 486 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 487 setOperationAction(ISD::FMA, MVT::v2f64, Expand); 488 489 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 490 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 491 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 492 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand); 493 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 494 setOperationAction(ISD::FLOG, MVT::v4f32, Expand); 495 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand); 496 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand); 497 setOperationAction(ISD::FEXP, MVT::v4f32, Expand); 498 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand); 499 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand); 500 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand); 501 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 502 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 503 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand); 504 505 // Mark v2f32 intrinsics. 506 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand); 507 setOperationAction(ISD::FSIN, MVT::v2f32, Expand); 508 setOperationAction(ISD::FCOS, MVT::v2f32, Expand); 509 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand); 510 setOperationAction(ISD::FPOW, MVT::v2f32, Expand); 511 setOperationAction(ISD::FLOG, MVT::v2f32, Expand); 512 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand); 513 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand); 514 setOperationAction(ISD::FEXP, MVT::v2f32, Expand); 515 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand); 516 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand); 517 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand); 518 setOperationAction(ISD::FRINT, MVT::v2f32, Expand); 519 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand); 520 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand); 521 522 // Neon does not support some operations on v1i64 and v2i64 types. 523 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 524 // Custom handling for some quad-vector types to detect VMULL. 525 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 526 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 527 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 528 // Custom handling for some vector types to avoid expensive expansions 529 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 530 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 531 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 532 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 533 setOperationAction(ISD::SETCC, MVT::v1i64, Expand); 534 setOperationAction(ISD::SETCC, MVT::v2i64, Expand); 535 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 536 // a destination type that is wider than the source, and nor does 537 // it have a FP_TO_[SU]INT instruction with a narrower destination than 538 // source. 539 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 540 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 541 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 542 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 543 544 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 545 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand); 546 547 // NEON does not have single instruction CTPOP for vectors with element 548 // types wider than 8-bits. However, custom lowering can leverage the 549 // v8i8/v16i8 vcnt instruction. 550 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom); 551 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom); 552 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom); 553 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom); 554 555 // NEON only has FMA instructions as of VFP4. 556 if (!Subtarget->hasVFP4()) { 557 setOperationAction(ISD::FMA, MVT::v2f32, Expand); 558 setOperationAction(ISD::FMA, MVT::v4f32, Expand); 559 } 560 561 setTargetDAGCombine(ISD::INTRINSIC_VOID); 562 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 563 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 564 setTargetDAGCombine(ISD::SHL); 565 setTargetDAGCombine(ISD::SRL); 566 setTargetDAGCombine(ISD::SRA); 567 setTargetDAGCombine(ISD::SIGN_EXTEND); 568 setTargetDAGCombine(ISD::ZERO_EXTEND); 569 setTargetDAGCombine(ISD::ANY_EXTEND); 570 setTargetDAGCombine(ISD::SELECT_CC); 571 setTargetDAGCombine(ISD::BUILD_VECTOR); 572 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 573 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 574 setTargetDAGCombine(ISD::STORE); 575 setTargetDAGCombine(ISD::FP_TO_SINT); 576 setTargetDAGCombine(ISD::FP_TO_UINT); 577 setTargetDAGCombine(ISD::FDIV); 578 579 // It is legal to extload from v4i8 to v4i16 or v4i32. 580 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8, 581 MVT::v4i16, MVT::v2i16, 582 MVT::v2i32}; 583 for (unsigned i = 0; i < 6; ++i) { 584 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal); 585 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal); 586 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal); 587 } 588 } 589 590 // ARM and Thumb2 support UMLAL/SMLAL. 591 if (!Subtarget->isThumb1Only()) 592 setTargetDAGCombine(ISD::ADDC); 593 594 if (Subtarget->isFPOnlySP()) { 595 // When targetting a floating-point unit with only single-precision 596 // operations, f64 is legal for the few double-precision instructions which 597 // are present However, no double-precision operations other than moves, 598 // loads and stores are provided by the hardware. 599 setOperationAction(ISD::FADD, MVT::f64, Expand); 600 setOperationAction(ISD::FSUB, MVT::f64, Expand); 601 setOperationAction(ISD::FMUL, MVT::f64, Expand); 602 setOperationAction(ISD::FMA, MVT::f64, Expand); 603 setOperationAction(ISD::FDIV, MVT::f64, Expand); 604 setOperationAction(ISD::FREM, MVT::f64, Expand); 605 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 606 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand); 607 setOperationAction(ISD::FNEG, MVT::f64, Expand); 608 setOperationAction(ISD::FABS, MVT::f64, Expand); 609 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 610 setOperationAction(ISD::FSIN, MVT::f64, Expand); 611 setOperationAction(ISD::FCOS, MVT::f64, Expand); 612 setOperationAction(ISD::FPOWI, MVT::f64, Expand); 613 setOperationAction(ISD::FPOW, MVT::f64, Expand); 614 setOperationAction(ISD::FLOG, MVT::f64, Expand); 615 setOperationAction(ISD::FLOG2, MVT::f64, Expand); 616 setOperationAction(ISD::FLOG10, MVT::f64, Expand); 617 setOperationAction(ISD::FEXP, MVT::f64, Expand); 618 setOperationAction(ISD::FEXP2, MVT::f64, Expand); 619 setOperationAction(ISD::FCEIL, MVT::f64, Expand); 620 setOperationAction(ISD::FTRUNC, MVT::f64, Expand); 621 setOperationAction(ISD::FRINT, MVT::f64, Expand); 622 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand); 623 setOperationAction(ISD::FFLOOR, MVT::f64, Expand); 624 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); 625 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom); 626 } 627 628 computeRegisterProperties(); 629 630 // ARM does not have floating-point extending loads. 631 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); 632 setLoadExtAction(ISD::EXTLOAD, MVT::f16, Expand); 633 634 // ... or truncating stores 635 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 636 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 637 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 638 639 // ARM does not have i1 sign extending load. 640 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 641 642 // ARM supports all 4 flavors of integer indexed load / store. 643 if (!Subtarget->isThumb1Only()) { 644 for (unsigned im = (unsigned)ISD::PRE_INC; 645 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 646 setIndexedLoadAction(im, MVT::i1, Legal); 647 setIndexedLoadAction(im, MVT::i8, Legal); 648 setIndexedLoadAction(im, MVT::i16, Legal); 649 setIndexedLoadAction(im, MVT::i32, Legal); 650 setIndexedStoreAction(im, MVT::i1, Legal); 651 setIndexedStoreAction(im, MVT::i8, Legal); 652 setIndexedStoreAction(im, MVT::i16, Legal); 653 setIndexedStoreAction(im, MVT::i32, Legal); 654 } 655 } 656 657 setOperationAction(ISD::SADDO, MVT::i32, Custom); 658 setOperationAction(ISD::UADDO, MVT::i32, Custom); 659 setOperationAction(ISD::SSUBO, MVT::i32, Custom); 660 setOperationAction(ISD::USUBO, MVT::i32, Custom); 661 662 // i64 operation support. 663 setOperationAction(ISD::MUL, MVT::i64, Expand); 664 setOperationAction(ISD::MULHU, MVT::i32, Expand); 665 if (Subtarget->isThumb1Only()) { 666 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 667 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 668 } 669 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 670 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP())) 671 setOperationAction(ISD::MULHS, MVT::i32, Expand); 672 673 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 674 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 675 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 676 setOperationAction(ISD::SRL, MVT::i64, Custom); 677 setOperationAction(ISD::SRA, MVT::i64, Custom); 678 679 if (!Subtarget->isThumb1Only()) { 680 // FIXME: We should do this for Thumb1 as well. 681 setOperationAction(ISD::ADDC, MVT::i32, Custom); 682 setOperationAction(ISD::ADDE, MVT::i32, Custom); 683 setOperationAction(ISD::SUBC, MVT::i32, Custom); 684 setOperationAction(ISD::SUBE, MVT::i32, Custom); 685 } 686 687 // ARM does not have ROTL. 688 setOperationAction(ISD::ROTL, MVT::i32, Expand); 689 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 690 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 691 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) 692 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 693 694 // These just redirect to CTTZ and CTLZ on ARM. 695 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand); 696 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand); 697 698 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom); 699 700 // Only ARMv6 has BSWAP. 701 if (!Subtarget->hasV6Ops()) 702 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 703 704 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) && 705 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) { 706 // These are expanded into libcalls if the cpu doesn't have HW divider. 707 setOperationAction(ISD::SDIV, MVT::i32, Expand); 708 setOperationAction(ISD::UDIV, MVT::i32, Expand); 709 } 710 711 // FIXME: Also set divmod for SREM on EABI 712 setOperationAction(ISD::SREM, MVT::i32, Expand); 713 setOperationAction(ISD::UREM, MVT::i32, Expand); 714 // Register based DivRem for AEABI (RTABI 4.2) 715 if (Subtarget->isTargetAEABI()) { 716 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod"); 717 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod"); 718 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod"); 719 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod"); 720 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod"); 721 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod"); 722 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod"); 723 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod"); 724 725 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS); 726 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS); 727 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS); 728 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS); 729 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS); 730 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS); 731 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS); 732 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS); 733 734 setOperationAction(ISD::SDIVREM, MVT::i32, Custom); 735 setOperationAction(ISD::UDIVREM, MVT::i32, Custom); 736 } else { 737 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 738 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 739 } 740 741 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 742 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 743 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom); 744 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 745 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 746 747 setOperationAction(ISD::TRAP, MVT::Other, Legal); 748 749 // Use the default implementation. 750 setOperationAction(ISD::VASTART, MVT::Other, Custom); 751 setOperationAction(ISD::VAARG, MVT::Other, Expand); 752 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 753 setOperationAction(ISD::VAEND, MVT::Other, Expand); 754 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 755 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 756 757 if (!Subtarget->isTargetMachO()) { 758 // Non-MachO platforms may return values in these registers via the 759 // personality function. 760 setExceptionPointerRegister(ARM::R0); 761 setExceptionSelectorRegister(ARM::R1); 762 } 763 764 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment()) 765 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 766 else 767 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 768 769 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 770 // the default expansion. If we are targeting a single threaded system, 771 // then set them all for expand so we can lower them later into their 772 // non-atomic form. 773 if (TM.Options.ThreadModel == ThreadModel::Single) 774 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand); 775 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) { 776 // ATOMIC_FENCE needs custom lowering; the others should have been expanded 777 // to ldrex/strex loops already. 778 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 779 780 // On v8, we have particularly efficient implementations of atomic fences 781 // if they can be combined with nearby atomic loads and stores. 782 if (!Subtarget->hasV8Ops()) { 783 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc. 784 setInsertFencesForAtomic(true); 785 } 786 } else { 787 // If there's anything we can use as a barrier, go through custom lowering 788 // for ATOMIC_FENCE. 789 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, 790 Subtarget->hasAnyDataBarrier() ? Custom : Expand); 791 792 // Set them all for expansion, which will force libcalls. 793 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 794 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 795 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 796 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 797 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 798 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 799 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 800 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 801 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 802 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 803 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 804 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 805 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 806 // Unordered/Monotonic case. 807 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 808 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 809 } 810 811 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 812 813 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 814 if (!Subtarget->hasV6Ops()) { 815 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 816 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 817 } 818 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 819 820 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 821 !Subtarget->isThumb1Only()) { 822 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 823 // iff target supports vfp2. 824 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 825 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 826 } 827 828 // We want to custom lower some of our intrinsics. 829 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 830 if (Subtarget->isTargetDarwin()) { 831 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 832 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 833 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 834 } 835 836 setOperationAction(ISD::SETCC, MVT::i32, Expand); 837 setOperationAction(ISD::SETCC, MVT::f32, Expand); 838 setOperationAction(ISD::SETCC, MVT::f64, Expand); 839 setOperationAction(ISD::SELECT, MVT::i32, Custom); 840 setOperationAction(ISD::SELECT, MVT::f32, Custom); 841 setOperationAction(ISD::SELECT, MVT::f64, Custom); 842 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 843 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 844 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 845 846 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 847 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 848 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 849 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 850 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 851 852 // We don't support sin/cos/fmod/copysign/pow 853 setOperationAction(ISD::FSIN, MVT::f64, Expand); 854 setOperationAction(ISD::FSIN, MVT::f32, Expand); 855 setOperationAction(ISD::FCOS, MVT::f32, Expand); 856 setOperationAction(ISD::FCOS, MVT::f64, Expand); 857 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 858 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 859 setOperationAction(ISD::FREM, MVT::f64, Expand); 860 setOperationAction(ISD::FREM, MVT::f32, Expand); 861 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() && 862 !Subtarget->isThumb1Only()) { 863 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 864 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 865 } 866 setOperationAction(ISD::FPOW, MVT::f64, Expand); 867 setOperationAction(ISD::FPOW, MVT::f32, Expand); 868 869 if (!Subtarget->hasVFP4()) { 870 setOperationAction(ISD::FMA, MVT::f64, Expand); 871 setOperationAction(ISD::FMA, MVT::f32, Expand); 872 } 873 874 // Various VFP goodness 875 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) { 876 // int <-> fp are custom expanded into bit_convert + ARMISD ops. 877 if (Subtarget->hasVFP2()) { 878 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 879 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 880 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 881 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 882 } 883 884 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded. 885 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) { 886 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 887 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand); 888 } 889 890 // fp16 is a special v7 extension that adds f16 <-> f32 conversions. 891 if (!Subtarget->hasFP16()) { 892 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand); 893 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand); 894 } 895 } 896 897 // Combine sin / cos into one node or libcall if possible. 898 if (Subtarget->hasSinCos()) { 899 setLibcallName(RTLIB::SINCOS_F32, "sincosf"); 900 setLibcallName(RTLIB::SINCOS_F64, "sincos"); 901 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) { 902 // For iOS, we don't want to the normal expansion of a libcall to 903 // sincos. We want to issue a libcall to __sincos_stret. 904 setOperationAction(ISD::FSINCOS, MVT::f64, Custom); 905 setOperationAction(ISD::FSINCOS, MVT::f32, Custom); 906 } 907 } 908 909 // FP-ARMv8 implements a lot of rounding-like FP operations. 910 if (Subtarget->hasFPARMv8()) { 911 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 912 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 913 setOperationAction(ISD::FROUND, MVT::f32, Legal); 914 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 915 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal); 916 setOperationAction(ISD::FRINT, MVT::f32, Legal); 917 if (!Subtarget->isFPOnlySP()) { 918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 919 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 920 setOperationAction(ISD::FROUND, MVT::f64, Legal); 921 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal); 923 setOperationAction(ISD::FRINT, MVT::f64, Legal); 924 } 925 } 926 // We have target-specific dag combine patterns for the following nodes: 927 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 928 setTargetDAGCombine(ISD::ADD); 929 setTargetDAGCombine(ISD::SUB); 930 setTargetDAGCombine(ISD::MUL); 931 setTargetDAGCombine(ISD::AND); 932 setTargetDAGCombine(ISD::OR); 933 setTargetDAGCombine(ISD::XOR); 934 935 if (Subtarget->hasV6Ops()) 936 setTargetDAGCombine(ISD::SRL); 937 938 setStackPointerRegisterToSaveRestore(ARM::SP); 939 940 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() || 941 !Subtarget->hasVFP2()) 942 setSchedulingPreference(Sched::RegPressure); 943 else 944 setSchedulingPreference(Sched::Hybrid); 945 946 //// temporary - rewrite interface to use type 947 MaxStoresPerMemset = 8; 948 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4; 949 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores 950 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2; 951 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores 952 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2; 953 954 // On ARM arguments smaller than 4 bytes are extended, so all arguments 955 // are at least 4 bytes aligned. 956 setMinStackArgumentAlignment(4); 957 958 // Prefer likely predicted branches to selects on out-of-order cores. 959 PredictableSelectIsExpensive = Subtarget->isLikeA9(); 960 961 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2); 962 } 963 964 // FIXME: It might make sense to define the representative register class as the 965 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 966 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 967 // SPR's representative would be DPR_VFP2. This should work well if register 968 // pressure tracking were modified such that a register use would increment the 969 // pressure of the register class's representative and all of it's super 970 // classes' representatives transitively. We have not implemented this because 971 // of the difficulty prior to coalescing of modeling operand register classes 972 // due to the common occurrence of cross class copies and subregister insertions 973 // and extractions. 974 std::pair<const TargetRegisterClass*, uint8_t> 975 ARMTargetLowering::findRepresentativeClass(MVT VT) const{ 976 const TargetRegisterClass *RRC = nullptr; 977 uint8_t Cost = 1; 978 switch (VT.SimpleTy) { 979 default: 980 return TargetLowering::findRepresentativeClass(VT); 981 // Use DPR as representative register class for all floating point 982 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 983 // the cost is 1 for both f32 and f64. 984 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 985 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 986 RRC = &ARM::DPRRegClass; 987 // When NEON is used for SP, only half of the register file is available 988 // because operations that define both SP and DP results will be constrained 989 // to the VFP2 class (D0-D15). We currently model this constraint prior to 990 // coalescing by double-counting the SP regs. See the FIXME above. 991 if (Subtarget->useNEONForSinglePrecisionFP()) 992 Cost = 2; 993 break; 994 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 995 case MVT::v4f32: case MVT::v2f64: 996 RRC = &ARM::DPRRegClass; 997 Cost = 2; 998 break; 999 case MVT::v4i64: 1000 RRC = &ARM::DPRRegClass; 1001 Cost = 4; 1002 break; 1003 case MVT::v8i64: 1004 RRC = &ARM::DPRRegClass; 1005 Cost = 8; 1006 break; 1007 } 1008 return std::make_pair(RRC, Cost); 1009 } 1010 1011 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 1012 switch (Opcode) { 1013 default: return nullptr; 1014 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 1015 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 1016 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 1017 case ARMISD::CALL: return "ARMISD::CALL"; 1018 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 1019 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 1020 case ARMISD::tCALL: return "ARMISD::tCALL"; 1021 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 1022 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 1023 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 1024 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 1025 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG"; 1026 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 1027 case ARMISD::CMP: return "ARMISD::CMP"; 1028 case ARMISD::CMN: return "ARMISD::CMN"; 1029 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 1030 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 1031 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 1032 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 1033 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 1034 1035 case ARMISD::CMOV: return "ARMISD::CMOV"; 1036 1037 case ARMISD::RBIT: return "ARMISD::RBIT"; 1038 1039 case ARMISD::FTOSI: return "ARMISD::FTOSI"; 1040 case ARMISD::FTOUI: return "ARMISD::FTOUI"; 1041 case ARMISD::SITOF: return "ARMISD::SITOF"; 1042 case ARMISD::UITOF: return "ARMISD::UITOF"; 1043 1044 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 1045 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 1046 case ARMISD::RRX: return "ARMISD::RRX"; 1047 1048 case ARMISD::ADDC: return "ARMISD::ADDC"; 1049 case ARMISD::ADDE: return "ARMISD::ADDE"; 1050 case ARMISD::SUBC: return "ARMISD::SUBC"; 1051 case ARMISD::SUBE: return "ARMISD::SUBE"; 1052 1053 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 1054 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 1055 1056 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 1057 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP"; 1058 1059 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 1060 1061 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 1062 1063 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 1064 1065 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 1066 1067 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 1068 1069 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK"; 1070 1071 case ARMISD::VCEQ: return "ARMISD::VCEQ"; 1072 case ARMISD::VCEQZ: return "ARMISD::VCEQZ"; 1073 case ARMISD::VCGE: return "ARMISD::VCGE"; 1074 case ARMISD::VCGEZ: return "ARMISD::VCGEZ"; 1075 case ARMISD::VCLEZ: return "ARMISD::VCLEZ"; 1076 case ARMISD::VCGEU: return "ARMISD::VCGEU"; 1077 case ARMISD::VCGT: return "ARMISD::VCGT"; 1078 case ARMISD::VCGTZ: return "ARMISD::VCGTZ"; 1079 case ARMISD::VCLTZ: return "ARMISD::VCLTZ"; 1080 case ARMISD::VCGTU: return "ARMISD::VCGTU"; 1081 case ARMISD::VTST: return "ARMISD::VTST"; 1082 1083 case ARMISD::VSHL: return "ARMISD::VSHL"; 1084 case ARMISD::VSHRs: return "ARMISD::VSHRs"; 1085 case ARMISD::VSHRu: return "ARMISD::VSHRu"; 1086 case ARMISD::VRSHRs: return "ARMISD::VRSHRs"; 1087 case ARMISD::VRSHRu: return "ARMISD::VRSHRu"; 1088 case ARMISD::VRSHRN: return "ARMISD::VRSHRN"; 1089 case ARMISD::VQSHLs: return "ARMISD::VQSHLs"; 1090 case ARMISD::VQSHLu: return "ARMISD::VQSHLu"; 1091 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu"; 1092 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs"; 1093 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu"; 1094 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu"; 1095 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs"; 1096 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu"; 1097 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu"; 1098 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 1099 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 1100 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 1101 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 1102 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 1103 case ARMISD::VDUP: return "ARMISD::VDUP"; 1104 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 1105 case ARMISD::VEXT: return "ARMISD::VEXT"; 1106 case ARMISD::VREV64: return "ARMISD::VREV64"; 1107 case ARMISD::VREV32: return "ARMISD::VREV32"; 1108 case ARMISD::VREV16: return "ARMISD::VREV16"; 1109 case ARMISD::VZIP: return "ARMISD::VZIP"; 1110 case ARMISD::VUZP: return "ARMISD::VUZP"; 1111 case ARMISD::VTRN: return "ARMISD::VTRN"; 1112 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 1113 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 1114 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 1115 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 1116 case ARMISD::UMLAL: return "ARMISD::UMLAL"; 1117 case ARMISD::SMLAL: return "ARMISD::SMLAL"; 1118 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 1119 case ARMISD::FMAX: return "ARMISD::FMAX"; 1120 case ARMISD::FMIN: return "ARMISD::FMIN"; 1121 case ARMISD::VMAXNM: return "ARMISD::VMAX"; 1122 case ARMISD::VMINNM: return "ARMISD::VMIN"; 1123 case ARMISD::BFI: return "ARMISD::BFI"; 1124 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 1125 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 1126 case ARMISD::VBSL: return "ARMISD::VBSL"; 1127 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 1128 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 1129 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 1130 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 1131 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 1132 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 1133 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 1134 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 1135 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 1136 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 1137 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 1138 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 1139 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 1140 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 1141 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 1142 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 1143 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 1144 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 1145 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 1146 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 1147 } 1148 } 1149 1150 EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { 1151 if (!VT.isVector()) return getPointerTy(); 1152 return VT.changeVectorElementTypeToInteger(); 1153 } 1154 1155 /// getRegClassFor - Return the register class that should be used for the 1156 /// specified value type. 1157 const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const { 1158 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 1159 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 1160 // load / store 4 to 8 consecutive D registers. 1161 if (Subtarget->hasNEON()) { 1162 if (VT == MVT::v4i64) 1163 return &ARM::QQPRRegClass; 1164 if (VT == MVT::v8i64) 1165 return &ARM::QQQQPRRegClass; 1166 } 1167 return TargetLowering::getRegClassFor(VT); 1168 } 1169 1170 // Create a fast isel object. 1171 FastISel * 1172 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 1173 const TargetLibraryInfo *libInfo) const { 1174 return ARM::createFastISel(funcInfo, libInfo); 1175 } 1176 1177 /// getMaximalGlobalOffset - Returns the maximal possible offset which can 1178 /// be used for loads / stores from the global. 1179 unsigned ARMTargetLowering::getMaximalGlobalOffset() const { 1180 return (Subtarget->isThumb1Only() ? 127 : 4095); 1181 } 1182 1183 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 1184 unsigned NumVals = N->getNumValues(); 1185 if (!NumVals) 1186 return Sched::RegPressure; 1187 1188 for (unsigned i = 0; i != NumVals; ++i) { 1189 EVT VT = N->getValueType(i); 1190 if (VT == MVT::Glue || VT == MVT::Other) 1191 continue; 1192 if (VT.isFloatingPoint() || VT.isVector()) 1193 return Sched::ILP; 1194 } 1195 1196 if (!N->isMachineOpcode()) 1197 return Sched::RegPressure; 1198 1199 // Load are scheduled for latency even if there instruction itinerary 1200 // is not available. 1201 const TargetInstrInfo *TII = 1202 getTargetMachine().getSubtargetImpl()->getInstrInfo(); 1203 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1204 1205 if (MCID.getNumDefs() == 0) 1206 return Sched::RegPressure; 1207 if (!Itins->isEmpty() && 1208 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1209 return Sched::ILP; 1210 1211 return Sched::RegPressure; 1212 } 1213 1214 //===----------------------------------------------------------------------===// 1215 // Lowering Code 1216 //===----------------------------------------------------------------------===// 1217 1218 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1219 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1220 switch (CC) { 1221 default: llvm_unreachable("Unknown condition code!"); 1222 case ISD::SETNE: return ARMCC::NE; 1223 case ISD::SETEQ: return ARMCC::EQ; 1224 case ISD::SETGT: return ARMCC::GT; 1225 case ISD::SETGE: return ARMCC::GE; 1226 case ISD::SETLT: return ARMCC::LT; 1227 case ISD::SETLE: return ARMCC::LE; 1228 case ISD::SETUGT: return ARMCC::HI; 1229 case ISD::SETUGE: return ARMCC::HS; 1230 case ISD::SETULT: return ARMCC::LO; 1231 case ISD::SETULE: return ARMCC::LS; 1232 } 1233 } 1234 1235 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1236 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1237 ARMCC::CondCodes &CondCode2) { 1238 CondCode2 = ARMCC::AL; 1239 switch (CC) { 1240 default: llvm_unreachable("Unknown FP condition!"); 1241 case ISD::SETEQ: 1242 case ISD::SETOEQ: CondCode = ARMCC::EQ; break; 1243 case ISD::SETGT: 1244 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1245 case ISD::SETGE: 1246 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1247 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1248 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1249 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break; 1250 case ISD::SETO: CondCode = ARMCC::VC; break; 1251 case ISD::SETUO: CondCode = ARMCC::VS; break; 1252 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break; 1253 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1254 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1255 case ISD::SETLT: 1256 case ISD::SETULT: CondCode = ARMCC::LT; break; 1257 case ISD::SETLE: 1258 case ISD::SETULE: CondCode = ARMCC::LE; break; 1259 case ISD::SETNE: 1260 case ISD::SETUNE: CondCode = ARMCC::NE; break; 1261 } 1262 } 1263 1264 //===----------------------------------------------------------------------===// 1265 // Calling Convention Implementation 1266 //===----------------------------------------------------------------------===// 1267 1268 #include "ARMGenCallingConv.inc" 1269 1270 /// getEffectiveCallingConv - Get the effective calling convention, taking into 1271 /// account presence of floating point hardware and calling convention 1272 /// limitations, such as support for variadic functions. 1273 CallingConv::ID 1274 ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC, 1275 bool isVarArg) const { 1276 switch (CC) { 1277 default: 1278 llvm_unreachable("Unsupported calling convention"); 1279 case CallingConv::ARM_AAPCS: 1280 case CallingConv::ARM_APCS: 1281 case CallingConv::GHC: 1282 return CC; 1283 case CallingConv::ARM_AAPCS_VFP: 1284 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP; 1285 case CallingConv::C: 1286 if (!Subtarget->isAAPCS_ABI()) 1287 return CallingConv::ARM_APCS; 1288 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && 1289 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1290 !isVarArg) 1291 return CallingConv::ARM_AAPCS_VFP; 1292 else 1293 return CallingConv::ARM_AAPCS; 1294 case CallingConv::Fast: 1295 if (!Subtarget->isAAPCS_ABI()) { 1296 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg) 1297 return CallingConv::Fast; 1298 return CallingConv::ARM_APCS; 1299 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg) 1300 return CallingConv::ARM_AAPCS_VFP; 1301 else 1302 return CallingConv::ARM_AAPCS; 1303 } 1304 } 1305 1306 /// CCAssignFnForNode - Selects the correct CCAssignFn for the given 1307 /// CallingConvention. 1308 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1309 bool Return, 1310 bool isVarArg) const { 1311 switch (getEffectiveCallingConv(CC, isVarArg)) { 1312 default: 1313 llvm_unreachable("Unsupported calling convention"); 1314 case CallingConv::ARM_APCS: 1315 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1316 case CallingConv::ARM_AAPCS: 1317 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1318 case CallingConv::ARM_AAPCS_VFP: 1319 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1320 case CallingConv::Fast: 1321 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1322 case CallingConv::GHC: 1323 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC); 1324 } 1325 } 1326 1327 /// LowerCallResult - Lower the result values of a call into the 1328 /// appropriate copies out of appropriate physical registers. 1329 SDValue 1330 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 1331 CallingConv::ID CallConv, bool isVarArg, 1332 const SmallVectorImpl<ISD::InputArg> &Ins, 1333 SDLoc dl, SelectionDAG &DAG, 1334 SmallVectorImpl<SDValue> &InVals, 1335 bool isThisReturn, SDValue ThisVal) const { 1336 1337 // Assign locations to each value returned by this call. 1338 SmallVector<CCValAssign, 16> RVLocs; 1339 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1340 *DAG.getContext(), Call); 1341 CCInfo.AnalyzeCallResult(Ins, 1342 CCAssignFnForNode(CallConv, /* Return*/ true, 1343 isVarArg)); 1344 1345 // Copy all of the result registers out of their specified physreg. 1346 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1347 CCValAssign VA = RVLocs[i]; 1348 1349 // Pass 'this' value directly from the argument to return value, to avoid 1350 // reg unit interference 1351 if (i == 0 && isThisReturn) { 1352 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 && 1353 "unexpected return calling convention register assignment"); 1354 InVals.push_back(ThisVal); 1355 continue; 1356 } 1357 1358 SDValue Val; 1359 if (VA.needsCustom()) { 1360 // Handle f64 or half of a v2f64. 1361 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1362 InFlag); 1363 Chain = Lo.getValue(1); 1364 InFlag = Lo.getValue(2); 1365 VA = RVLocs[++i]; // skip ahead to next loc 1366 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1367 InFlag); 1368 Chain = Hi.getValue(1); 1369 InFlag = Hi.getValue(2); 1370 if (!Subtarget->isLittle()) 1371 std::swap (Lo, Hi); 1372 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1373 1374 if (VA.getLocVT() == MVT::v2f64) { 1375 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 1376 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1377 DAG.getConstant(0, MVT::i32)); 1378 1379 VA = RVLocs[++i]; // skip ahead to next loc 1380 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1381 Chain = Lo.getValue(1); 1382 InFlag = Lo.getValue(2); 1383 VA = RVLocs[++i]; // skip ahead to next loc 1384 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1385 Chain = Hi.getValue(1); 1386 InFlag = Hi.getValue(2); 1387 if (!Subtarget->isLittle()) 1388 std::swap (Lo, Hi); 1389 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1390 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1391 DAG.getConstant(1, MVT::i32)); 1392 } 1393 } else { 1394 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1395 InFlag); 1396 Chain = Val.getValue(1); 1397 InFlag = Val.getValue(2); 1398 } 1399 1400 switch (VA.getLocInfo()) { 1401 default: llvm_unreachable("Unknown loc info!"); 1402 case CCValAssign::Full: break; 1403 case CCValAssign::BCvt: 1404 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1405 break; 1406 } 1407 1408 InVals.push_back(Val); 1409 } 1410 1411 return Chain; 1412 } 1413 1414 /// LowerMemOpCallTo - Store the argument to the stack. 1415 SDValue 1416 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, 1417 SDValue StackPtr, SDValue Arg, 1418 SDLoc dl, SelectionDAG &DAG, 1419 const CCValAssign &VA, 1420 ISD::ArgFlagsTy Flags) const { 1421 unsigned LocMemOffset = VA.getLocMemOffset(); 1422 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); 1423 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); 1424 return DAG.getStore(Chain, dl, Arg, PtrOff, 1425 MachinePointerInfo::getStack(LocMemOffset), 1426 false, false, 0); 1427 } 1428 1429 void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG, 1430 SDValue Chain, SDValue &Arg, 1431 RegsToPassVector &RegsToPass, 1432 CCValAssign &VA, CCValAssign &NextVA, 1433 SDValue &StackPtr, 1434 SmallVectorImpl<SDValue> &MemOpChains, 1435 ISD::ArgFlagsTy Flags) const { 1436 1437 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1438 DAG.getVTList(MVT::i32, MVT::i32), Arg); 1439 unsigned id = Subtarget->isLittle() ? 0 : 1; 1440 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id))); 1441 1442 if (NextVA.isRegLoc()) 1443 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id))); 1444 else { 1445 assert(NextVA.isMemLoc()); 1446 if (!StackPtr.getNode()) 1447 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1448 1449 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id), 1450 dl, DAG, NextVA, 1451 Flags)); 1452 } 1453 } 1454 1455 /// LowerCall - Lowering a call into a callseq_start <- 1456 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 1457 /// nodes. 1458 SDValue 1459 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 1460 SmallVectorImpl<SDValue> &InVals) const { 1461 SelectionDAG &DAG = CLI.DAG; 1462 SDLoc &dl = CLI.DL; 1463 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1464 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1465 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1466 SDValue Chain = CLI.Chain; 1467 SDValue Callee = CLI.Callee; 1468 bool &isTailCall = CLI.IsTailCall; 1469 CallingConv::ID CallConv = CLI.CallConv; 1470 bool doesNotRet = CLI.DoesNotReturn; 1471 bool isVarArg = CLI.IsVarArg; 1472 1473 MachineFunction &MF = DAG.getMachineFunction(); 1474 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 1475 bool isThisReturn = false; 1476 bool isSibCall = false; 1477 1478 // Disable tail calls if they're not supported. 1479 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls) 1480 isTailCall = false; 1481 1482 if (isTailCall) { 1483 // Check if it's really possible to do a tail call. 1484 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, 1485 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(), 1486 Outs, OutVals, Ins, DAG); 1487 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall()) 1488 report_fatal_error("failed to perform tail call elimination on a call " 1489 "site marked musttail"); 1490 // We don't support GuaranteedTailCallOpt for ARM, only automatically 1491 // detected sibcalls. 1492 if (isTailCall) { 1493 ++NumTailCalls; 1494 isSibCall = true; 1495 } 1496 } 1497 1498 // Analyze operands of the call, assigning locations to each operand. 1499 SmallVector<CCValAssign, 16> ArgLocs; 1500 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1501 *DAG.getContext(), Call); 1502 CCInfo.AnalyzeCallOperands(Outs, 1503 CCAssignFnForNode(CallConv, /* Return*/ false, 1504 isVarArg)); 1505 1506 // Get a count of how many bytes are to be pushed on the stack. 1507 unsigned NumBytes = CCInfo.getNextStackOffset(); 1508 1509 // For tail calls, memory operands are available in our caller's stack. 1510 if (isSibCall) 1511 NumBytes = 0; 1512 1513 // Adjust the stack pointer for the new arguments... 1514 // These operations are automatically eliminated by the prolog/epilog pass 1515 if (!isSibCall) 1516 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true), 1517 dl); 1518 1519 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1520 1521 RegsToPassVector RegsToPass; 1522 SmallVector<SDValue, 8> MemOpChains; 1523 1524 // Walk the register/memloc assignments, inserting copies/loads. In the case 1525 // of tail call optimization, arguments are handled later. 1526 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1527 i != e; 1528 ++i, ++realArgIdx) { 1529 CCValAssign &VA = ArgLocs[i]; 1530 SDValue Arg = OutVals[realArgIdx]; 1531 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1532 bool isByVal = Flags.isByVal(); 1533 1534 // Promote the value if needed. 1535 switch (VA.getLocInfo()) { 1536 default: llvm_unreachable("Unknown loc info!"); 1537 case CCValAssign::Full: break; 1538 case CCValAssign::SExt: 1539 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 1540 break; 1541 case CCValAssign::ZExt: 1542 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 1543 break; 1544 case CCValAssign::AExt: 1545 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1546 break; 1547 case CCValAssign::BCvt: 1548 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1549 break; 1550 } 1551 1552 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 1553 if (VA.needsCustom()) { 1554 if (VA.getLocVT() == MVT::v2f64) { 1555 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1556 DAG.getConstant(0, MVT::i32)); 1557 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1558 DAG.getConstant(1, MVT::i32)); 1559 1560 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 1561 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1562 1563 VA = ArgLocs[++i]; // skip ahead to next loc 1564 if (VA.isRegLoc()) { 1565 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 1566 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1567 } else { 1568 assert(VA.isMemLoc()); 1569 1570 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 1571 dl, DAG, VA, Flags)); 1572 } 1573 } else { 1574 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 1575 StackPtr, MemOpChains, Flags); 1576 } 1577 } else if (VA.isRegLoc()) { 1578 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) { 1579 assert(VA.getLocVT() == MVT::i32 && 1580 "unexpected calling convention register assignment"); 1581 assert(!Ins.empty() && Ins[0].VT == MVT::i32 && 1582 "unexpected use of 'returned'"); 1583 isThisReturn = true; 1584 } 1585 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1586 } else if (isByVal) { 1587 assert(VA.isMemLoc()); 1588 unsigned offset = 0; 1589 1590 // True if this byval aggregate will be split between registers 1591 // and memory. 1592 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount(); 1593 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed(); 1594 1595 if (CurByValIdx < ByValArgsCount) { 1596 1597 unsigned RegBegin, RegEnd; 1598 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd); 1599 1600 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 1601 unsigned int i, j; 1602 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) { 1603 SDValue Const = DAG.getConstant(4*i, MVT::i32); 1604 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 1605 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 1606 MachinePointerInfo(), 1607 false, false, false, 1608 DAG.InferPtrAlignment(AddArg)); 1609 MemOpChains.push_back(Load.getValue(1)); 1610 RegsToPass.push_back(std::make_pair(j, Load)); 1611 } 1612 1613 // If parameter size outsides register area, "offset" value 1614 // helps us to calculate stack slot for remained part properly. 1615 offset = RegEnd - RegBegin; 1616 1617 CCInfo.nextInRegsParam(); 1618 } 1619 1620 if (Flags.getByValSize() > 4*offset) { 1621 unsigned LocMemOffset = VA.getLocMemOffset(); 1622 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset); 1623 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, 1624 StkPtrOff); 1625 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset); 1626 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset); 1627 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, 1628 MVT::i32); 1629 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32); 1630 1631 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 1632 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode}; 1633 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, 1634 Ops)); 1635 } 1636 } else if (!isSibCall) { 1637 assert(VA.isMemLoc()); 1638 1639 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 1640 dl, DAG, VA, Flags)); 1641 } 1642 } 1643 1644 if (!MemOpChains.empty()) 1645 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 1646 1647 // Build a sequence of copy-to-reg nodes chained together with token chain 1648 // and flag operands which copy the outgoing args into the appropriate regs. 1649 SDValue InFlag; 1650 // Tail call byval lowering might overwrite argument registers so in case of 1651 // tail call optimization the copies to registers are lowered later. 1652 if (!isTailCall) 1653 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1654 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1655 RegsToPass[i].second, InFlag); 1656 InFlag = Chain.getValue(1); 1657 } 1658 1659 // For tail calls lower the arguments to the 'real' stack slot. 1660 if (isTailCall) { 1661 // Force all the incoming stack arguments to be loaded from the stack 1662 // before any new outgoing arguments are stored to the stack, because the 1663 // outgoing stack slots may alias the incoming argument stack slots, and 1664 // the alias isn't otherwise explicit. This is slightly more conservative 1665 // than necessary, because it means that each store effectively depends 1666 // on every argument instead of just those arguments it would clobber. 1667 1668 // Do not flag preceding copytoreg stuff together with the following stuff. 1669 InFlag = SDValue(); 1670 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1671 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1672 RegsToPass[i].second, InFlag); 1673 InFlag = Chain.getValue(1); 1674 } 1675 InFlag = SDValue(); 1676 } 1677 1678 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 1679 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 1680 // node so that legalize doesn't hack it. 1681 bool isDirect = false; 1682 bool isARMFunc = false; 1683 bool isLocalARMFunc = false; 1684 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1685 1686 if (EnableARMLongCalls) { 1687 assert((Subtarget->isTargetWindows() || 1688 getTargetMachine().getRelocationModel() == Reloc::Static) && 1689 "long-calls with non-static relocation model!"); 1690 // Handle a global address or an external symbol. If it's not one of 1691 // those, the target's already in a register, so we don't need to do 1692 // anything extra. 1693 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1694 const GlobalValue *GV = G->getGlobal(); 1695 // Create a constant pool entry for the callee address 1696 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1697 ARMConstantPoolValue *CPV = 1698 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 1699 1700 // Get the address of the callee into a register 1701 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1702 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1703 Callee = DAG.getLoad(getPointerTy(), dl, 1704 DAG.getEntryNode(), CPAddr, 1705 MachinePointerInfo::getConstantPool(), 1706 false, false, false, 0); 1707 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 1708 const char *Sym = S->getSymbol(); 1709 1710 // Create a constant pool entry for the callee address 1711 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1712 ARMConstantPoolValue *CPV = 1713 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1714 ARMPCLabelIndex, 0); 1715 // Get the address of the callee into a register 1716 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1717 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1718 Callee = DAG.getLoad(getPointerTy(), dl, 1719 DAG.getEntryNode(), CPAddr, 1720 MachinePointerInfo::getConstantPool(), 1721 false, false, false, 0); 1722 } 1723 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1724 const GlobalValue *GV = G->getGlobal(); 1725 isDirect = true; 1726 bool isExt = GV->isDeclaration() || GV->isWeakForLinker(); 1727 bool isStub = (isExt && Subtarget->isTargetMachO()) && 1728 getTargetMachine().getRelocationModel() != Reloc::Static; 1729 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 1730 // ARM call to a local ARM function is predicable. 1731 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking); 1732 // tBX takes a register source operand. 1733 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1734 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?"); 1735 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(), 1736 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 1737 0, ARMII::MO_NONLAZY)); 1738 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee, 1739 MachinePointerInfo::getGOT(), false, false, true, 0); 1740 } else if (Subtarget->isTargetCOFF()) { 1741 assert(Subtarget->isTargetWindows() && 1742 "Windows is the only supported COFF target"); 1743 unsigned TargetFlags = GV->hasDLLImportStorageClass() 1744 ? ARMII::MO_DLLIMPORT 1745 : ARMII::MO_NO_FLAG; 1746 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0, 1747 TargetFlags); 1748 if (GV->hasDLLImportStorageClass()) 1749 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), 1750 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(), 1751 Callee), MachinePointerInfo::getGOT(), 1752 false, false, false, 0); 1753 } else { 1754 // On ELF targets for PIC code, direct calls should go through the PLT 1755 unsigned OpFlags = 0; 1756 if (Subtarget->isTargetELF() && 1757 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1758 OpFlags = ARMII::MO_PLT; 1759 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags); 1760 } 1761 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1762 isDirect = true; 1763 bool isStub = Subtarget->isTargetMachO() && 1764 getTargetMachine().getRelocationModel() != Reloc::Static; 1765 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 1766 // tBX takes a register source operand. 1767 const char *Sym = S->getSymbol(); 1768 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1769 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1770 ARMConstantPoolValue *CPV = 1771 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1772 ARMPCLabelIndex, 4); 1773 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1774 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1775 Callee = DAG.getLoad(getPointerTy(), dl, 1776 DAG.getEntryNode(), CPAddr, 1777 MachinePointerInfo::getConstantPool(), 1778 false, false, false, 0); 1779 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 1780 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 1781 getPointerTy(), Callee, PICLabel); 1782 } else { 1783 unsigned OpFlags = 0; 1784 // On ELF targets for PIC code, direct calls should go through the PLT 1785 if (Subtarget->isTargetELF() && 1786 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1787 OpFlags = ARMII::MO_PLT; 1788 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags); 1789 } 1790 } 1791 1792 // FIXME: handle tail calls differently. 1793 unsigned CallOpc; 1794 bool HasMinSizeAttr = MF.getFunction()->getAttributes().hasAttribute( 1795 AttributeSet::FunctionIndex, Attribute::MinSize); 1796 if (Subtarget->isThumb()) { 1797 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 1798 CallOpc = ARMISD::CALL_NOLINK; 1799 else 1800 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL; 1801 } else { 1802 if (!isDirect && !Subtarget->hasV5TOps()) 1803 CallOpc = ARMISD::CALL_NOLINK; 1804 else if (doesNotRet && isDirect && Subtarget->hasRAS() && 1805 // Emit regular call when code size is the priority 1806 !HasMinSizeAttr) 1807 // "mov lr, pc; b _foo" to avoid confusing the RSP 1808 CallOpc = ARMISD::CALL_NOLINK; 1809 else 1810 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL; 1811 } 1812 1813 std::vector<SDValue> Ops; 1814 Ops.push_back(Chain); 1815 Ops.push_back(Callee); 1816 1817 // Add argument registers to the end of the list so that they are known live 1818 // into the call. 1819 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1820 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1821 RegsToPass[i].second.getValueType())); 1822 1823 // Add a register mask operand representing the call-preserved registers. 1824 if (!isTailCall) { 1825 const uint32_t *Mask; 1826 const TargetRegisterInfo *TRI = 1827 getTargetMachine().getSubtargetImpl()->getRegisterInfo(); 1828 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI); 1829 if (isThisReturn) { 1830 // For 'this' returns, use the R0-preserving mask if applicable 1831 Mask = ARI->getThisReturnPreservedMask(CallConv); 1832 if (!Mask) { 1833 // Set isThisReturn to false if the calling convention is not one that 1834 // allows 'returned' to be modeled in this way, so LowerCallResult does 1835 // not try to pass 'this' straight through 1836 isThisReturn = false; 1837 Mask = ARI->getCallPreservedMask(CallConv); 1838 } 1839 } else 1840 Mask = ARI->getCallPreservedMask(CallConv); 1841 1842 assert(Mask && "Missing call preserved mask for calling convention"); 1843 Ops.push_back(DAG.getRegisterMask(Mask)); 1844 } 1845 1846 if (InFlag.getNode()) 1847 Ops.push_back(InFlag); 1848 1849 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1850 if (isTailCall) 1851 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops); 1852 1853 // Returns a chain and a flag for retval copy to use. 1854 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); 1855 InFlag = Chain.getValue(1); 1856 1857 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), 1858 DAG.getIntPtrConstant(0, true), InFlag, dl); 1859 if (!Ins.empty()) 1860 InFlag = Chain.getValue(1); 1861 1862 // Handle result values, copying them out of physregs into vregs that we 1863 // return. 1864 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG, 1865 InVals, isThisReturn, 1866 isThisReturn ? OutVals[0] : SDValue()); 1867 } 1868 1869 /// HandleByVal - Every parameter *after* a byval parameter is passed 1870 /// on the stack. Remember the next parameter register to allocate, 1871 /// and then confiscate the rest of the parameter registers to insure 1872 /// this. 1873 void 1874 ARMTargetLowering::HandleByVal( 1875 CCState *State, unsigned &size, unsigned Align) const { 1876 unsigned reg = State->AllocateReg(GPRArgRegs, 4); 1877 assert((State->getCallOrPrologue() == Prologue || 1878 State->getCallOrPrologue() == Call) && 1879 "unhandled ParmContext"); 1880 1881 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) { 1882 if (Subtarget->isAAPCS_ABI() && Align > 4) { 1883 unsigned AlignInRegs = Align / 4; 1884 unsigned Waste = (ARM::R4 - reg) % AlignInRegs; 1885 for (unsigned i = 0; i < Waste; ++i) 1886 reg = State->AllocateReg(GPRArgRegs, 4); 1887 } 1888 if (reg != 0) { 1889 unsigned excess = 4 * (ARM::R4 - reg); 1890 1891 // Special case when NSAA != SP and parameter size greater than size of 1892 // all remained GPR regs. In that case we can't split parameter, we must 1893 // send it to stack. We also must set NCRN to R4, so waste all 1894 // remained registers. 1895 const unsigned NSAAOffset = State->getNextStackOffset(); 1896 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) { 1897 while (State->AllocateReg(GPRArgRegs, 4)) 1898 ; 1899 return; 1900 } 1901 1902 // First register for byval parameter is the first register that wasn't 1903 // allocated before this method call, so it would be "reg". 1904 // If parameter is small enough to be saved in range [reg, r4), then 1905 // the end (first after last) register would be reg + param-size-in-regs, 1906 // else parameter would be splitted between registers and stack, 1907 // end register would be r4 in this case. 1908 unsigned ByValRegBegin = reg; 1909 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4; 1910 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd); 1911 // Note, first register is allocated in the beginning of function already, 1912 // allocate remained amount of registers we need. 1913 for (unsigned i = reg+1; i != ByValRegEnd; ++i) 1914 State->AllocateReg(GPRArgRegs, 4); 1915 // A byval parameter that is split between registers and memory needs its 1916 // size truncated here. 1917 // In the case where the entire structure fits in registers, we set the 1918 // size in memory to zero. 1919 if (size < excess) 1920 size = 0; 1921 else 1922 size -= excess; 1923 } 1924 } 1925 } 1926 1927 /// MatchingStackOffset - Return true if the given stack call argument is 1928 /// already available in the same position (relatively) of the caller's 1929 /// incoming argument stack. 1930 static 1931 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 1932 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI, 1933 const TargetInstrInfo *TII) { 1934 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8; 1935 int FI = INT_MAX; 1936 if (Arg.getOpcode() == ISD::CopyFromReg) { 1937 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 1938 if (!TargetRegisterInfo::isVirtualRegister(VR)) 1939 return false; 1940 MachineInstr *Def = MRI->getVRegDef(VR); 1941 if (!Def) 1942 return false; 1943 if (!Flags.isByVal()) { 1944 if (!TII->isLoadFromStackSlot(Def, FI)) 1945 return false; 1946 } else { 1947 return false; 1948 } 1949 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 1950 if (Flags.isByVal()) 1951 // ByVal argument is passed in as a pointer but it's now being 1952 // dereferenced. e.g. 1953 // define @foo(%struct.X* %A) { 1954 // tail call @bar(%struct.X* byval %A) 1955 // } 1956 return false; 1957 SDValue Ptr = Ld->getBasePtr(); 1958 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 1959 if (!FINode) 1960 return false; 1961 FI = FINode->getIndex(); 1962 } else 1963 return false; 1964 1965 assert(FI != INT_MAX); 1966 if (!MFI->isFixedObjectIndex(FI)) 1967 return false; 1968 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI); 1969 } 1970 1971 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 1972 /// for tail call optimization. Targets which want to do tail call 1973 /// optimization should implement this function. 1974 bool 1975 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 1976 CallingConv::ID CalleeCC, 1977 bool isVarArg, 1978 bool isCalleeStructRet, 1979 bool isCallerStructRet, 1980 const SmallVectorImpl<ISD::OutputArg> &Outs, 1981 const SmallVectorImpl<SDValue> &OutVals, 1982 const SmallVectorImpl<ISD::InputArg> &Ins, 1983 SelectionDAG& DAG) const { 1984 const Function *CallerF = DAG.getMachineFunction().getFunction(); 1985 CallingConv::ID CallerCC = CallerF->getCallingConv(); 1986 bool CCMatch = CallerCC == CalleeCC; 1987 1988 // Look for obvious safe cases to perform tail call optimization that do not 1989 // require ABI changes. This is what gcc calls sibcall. 1990 1991 // Do not sibcall optimize vararg calls unless the call site is not passing 1992 // any arguments. 1993 if (isVarArg && !Outs.empty()) 1994 return false; 1995 1996 // Exception-handling functions need a special set of instructions to indicate 1997 // a return to the hardware. Tail-calling another function would probably 1998 // break this. 1999 if (CallerF->hasFnAttribute("interrupt")) 2000 return false; 2001 2002 // Also avoid sibcall optimization if either caller or callee uses struct 2003 // return semantics. 2004 if (isCalleeStructRet || isCallerStructRet) 2005 return false; 2006 2007 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo:: 2008 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as 2009 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation 2010 // support in the assembler and linker to be used. This would need to be 2011 // fixed to fully support tail calls in Thumb1. 2012 // 2013 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take 2014 // LR. This means if we need to reload LR, it takes an extra instructions, 2015 // which outweighs the value of the tail call; but here we don't know yet 2016 // whether LR is going to be used. Probably the right approach is to 2017 // generate the tail call here and turn it back into CALL/RET in 2018 // emitEpilogue if LR is used. 2019 2020 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls, 2021 // but we need to make sure there are enough registers; the only valid 2022 // registers are the 4 used for parameters. We don't currently do this 2023 // case. 2024 if (Subtarget->isThumb1Only()) 2025 return false; 2026 2027 // Externally-defined functions with weak linkage should not be 2028 // tail-called on ARM when the OS does not support dynamic 2029 // pre-emption of symbols, as the AAELF spec requires normal calls 2030 // to undefined weak functions to be replaced with a NOP or jump to the 2031 // next instruction. The behaviour of branch instructions in this 2032 // situation (as used for tail calls) is implementation-defined, so we 2033 // cannot rely on the linker replacing the tail call with a return. 2034 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 2035 const GlobalValue *GV = G->getGlobal(); 2036 if (GV->hasExternalWeakLinkage()) 2037 return false; 2038 } 2039 2040 // If the calling conventions do not match, then we'd better make sure the 2041 // results are returned in the same way as what the caller expects. 2042 if (!CCMatch) { 2043 SmallVector<CCValAssign, 16> RVLocs1; 2044 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1, 2045 *DAG.getContext(), Call); 2046 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg)); 2047 2048 SmallVector<CCValAssign, 16> RVLocs2; 2049 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2, 2050 *DAG.getContext(), Call); 2051 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg)); 2052 2053 if (RVLocs1.size() != RVLocs2.size()) 2054 return false; 2055 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) { 2056 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc()) 2057 return false; 2058 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo()) 2059 return false; 2060 if (RVLocs1[i].isRegLoc()) { 2061 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg()) 2062 return false; 2063 } else { 2064 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset()) 2065 return false; 2066 } 2067 } 2068 } 2069 2070 // If Caller's vararg or byval argument has been split between registers and 2071 // stack, do not perform tail call, since part of the argument is in caller's 2072 // local frame. 2073 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction(). 2074 getInfo<ARMFunctionInfo>(); 2075 if (AFI_Caller->getArgRegsSaveSize()) 2076 return false; 2077 2078 // If the callee takes no arguments then go on to check the results of the 2079 // call. 2080 if (!Outs.empty()) { 2081 // Check if stack adjustment is needed. For now, do not do this if any 2082 // argument is passed on the stack. 2083 SmallVector<CCValAssign, 16> ArgLocs; 2084 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs, 2085 *DAG.getContext(), Call); 2086 CCInfo.AnalyzeCallOperands(Outs, 2087 CCAssignFnForNode(CalleeCC, false, isVarArg)); 2088 if (CCInfo.getNextStackOffset()) { 2089 MachineFunction &MF = DAG.getMachineFunction(); 2090 2091 // Check if the arguments are already laid out in the right way as 2092 // the caller's fixed stack objects. 2093 MachineFrameInfo *MFI = MF.getFrameInfo(); 2094 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 2095 const TargetInstrInfo *TII = 2096 getTargetMachine().getSubtargetImpl()->getInstrInfo(); 2097 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 2098 i != e; 2099 ++i, ++realArgIdx) { 2100 CCValAssign &VA = ArgLocs[i]; 2101 EVT RegVT = VA.getLocVT(); 2102 SDValue Arg = OutVals[realArgIdx]; 2103 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2104 if (VA.getLocInfo() == CCValAssign::Indirect) 2105 return false; 2106 if (VA.needsCustom()) { 2107 // f64 and vector types are split into multiple registers or 2108 // register/stack-slot combinations. The types will not match 2109 // the registers; give up on memory f64 refs until we figure 2110 // out what to do about this. 2111 if (!VA.isRegLoc()) 2112 return false; 2113 if (!ArgLocs[++i].isRegLoc()) 2114 return false; 2115 if (RegVT == MVT::v2f64) { 2116 if (!ArgLocs[++i].isRegLoc()) 2117 return false; 2118 if (!ArgLocs[++i].isRegLoc()) 2119 return false; 2120 } 2121 } else if (!VA.isRegLoc()) { 2122 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 2123 MFI, MRI, TII)) 2124 return false; 2125 } 2126 } 2127 } 2128 } 2129 2130 return true; 2131 } 2132 2133 bool 2134 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 2135 MachineFunction &MF, bool isVarArg, 2136 const SmallVectorImpl<ISD::OutputArg> &Outs, 2137 LLVMContext &Context) const { 2138 SmallVector<CCValAssign, 16> RVLocs; 2139 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 2140 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true, 2141 isVarArg)); 2142 } 2143 2144 static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps, 2145 SDLoc DL, SelectionDAG &DAG) { 2146 const MachineFunction &MF = DAG.getMachineFunction(); 2147 const Function *F = MF.getFunction(); 2148 2149 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString(); 2150 2151 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset 2152 // version of the "preferred return address". These offsets affect the return 2153 // instruction if this is a return from PL1 without hypervisor extensions. 2154 // IRQ/FIQ: +4 "subs pc, lr, #4" 2155 // SWI: 0 "subs pc, lr, #0" 2156 // ABORT: +4 "subs pc, lr, #4" 2157 // UNDEF: +4/+2 "subs pc, lr, #0" 2158 // UNDEF varies depending on where the exception came from ARM or Thumb 2159 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0. 2160 2161 int64_t LROffset; 2162 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" || 2163 IntKind == "ABORT") 2164 LROffset = 4; 2165 else if (IntKind == "SWI" || IntKind == "UNDEF") 2166 LROffset = 0; 2167 else 2168 report_fatal_error("Unsupported interrupt attribute. If present, value " 2169 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF"); 2170 2171 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false)); 2172 2173 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps); 2174 } 2175 2176 SDValue 2177 ARMTargetLowering::LowerReturn(SDValue Chain, 2178 CallingConv::ID CallConv, bool isVarArg, 2179 const SmallVectorImpl<ISD::OutputArg> &Outs, 2180 const SmallVectorImpl<SDValue> &OutVals, 2181 SDLoc dl, SelectionDAG &DAG) const { 2182 2183 // CCValAssign - represent the assignment of the return value to a location. 2184 SmallVector<CCValAssign, 16> RVLocs; 2185 2186 // CCState - Info about the registers and stack slots. 2187 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2188 *DAG.getContext(), Call); 2189 2190 // Analyze outgoing return values. 2191 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true, 2192 isVarArg)); 2193 2194 SDValue Flag; 2195 SmallVector<SDValue, 4> RetOps; 2196 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2197 bool isLittleEndian = Subtarget->isLittle(); 2198 2199 MachineFunction &MF = DAG.getMachineFunction(); 2200 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2201 AFI->setReturnRegsCount(RVLocs.size()); 2202 2203 // Copy the result values into the output registers. 2204 for (unsigned i = 0, realRVLocIdx = 0; 2205 i != RVLocs.size(); 2206 ++i, ++realRVLocIdx) { 2207 CCValAssign &VA = RVLocs[i]; 2208 assert(VA.isRegLoc() && "Can only return in registers!"); 2209 2210 SDValue Arg = OutVals[realRVLocIdx]; 2211 2212 switch (VA.getLocInfo()) { 2213 default: llvm_unreachable("Unknown loc info!"); 2214 case CCValAssign::Full: break; 2215 case CCValAssign::BCvt: 2216 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 2217 break; 2218 } 2219 2220 if (VA.needsCustom()) { 2221 if (VA.getLocVT() == MVT::v2f64) { 2222 // Extract the first half and return it in two registers. 2223 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2224 DAG.getConstant(0, MVT::i32)); 2225 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 2226 DAG.getVTList(MVT::i32, MVT::i32), Half); 2227 2228 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2229 HalfGPRs.getValue(isLittleEndian ? 0 : 1), 2230 Flag); 2231 Flag = Chain.getValue(1); 2232 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2233 VA = RVLocs[++i]; // skip ahead to next loc 2234 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2235 HalfGPRs.getValue(isLittleEndian ? 1 : 0), 2236 Flag); 2237 Flag = Chain.getValue(1); 2238 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2239 VA = RVLocs[++i]; // skip ahead to next loc 2240 2241 // Extract the 2nd half and fall through to handle it as an f64 value. 2242 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2243 DAG.getConstant(1, MVT::i32)); 2244 } 2245 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 2246 // available. 2247 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 2248 DAG.getVTList(MVT::i32, MVT::i32), Arg); 2249 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2250 fmrrd.getValue(isLittleEndian ? 0 : 1), 2251 Flag); 2252 Flag = Chain.getValue(1); 2253 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2254 VA = RVLocs[++i]; // skip ahead to next loc 2255 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2256 fmrrd.getValue(isLittleEndian ? 1 : 0), 2257 Flag); 2258 } else 2259 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 2260 2261 // Guarantee that all emitted copies are 2262 // stuck together, avoiding something bad. 2263 Flag = Chain.getValue(1); 2264 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2265 } 2266 2267 // Update chain and glue. 2268 RetOps[0] = Chain; 2269 if (Flag.getNode()) 2270 RetOps.push_back(Flag); 2271 2272 // CPUs which aren't M-class use a special sequence to return from 2273 // exceptions (roughly, any instruction setting pc and cpsr simultaneously, 2274 // though we use "subs pc, lr, #N"). 2275 // 2276 // M-class CPUs actually use a normal return sequence with a special 2277 // (hardware-provided) value in LR, so the normal code path works. 2278 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") && 2279 !Subtarget->isMClass()) { 2280 if (Subtarget->isThumb1Only()) 2281 report_fatal_error("interrupt attribute is not supported in Thumb1"); 2282 return LowerInterruptReturn(RetOps, dl, DAG); 2283 } 2284 2285 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps); 2286 } 2287 2288 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { 2289 if (N->getNumValues() != 1) 2290 return false; 2291 if (!N->hasNUsesOfValue(1, 0)) 2292 return false; 2293 2294 SDValue TCChain = Chain; 2295 SDNode *Copy = *N->use_begin(); 2296 if (Copy->getOpcode() == ISD::CopyToReg) { 2297 // If the copy has a glue operand, we conservatively assume it isn't safe to 2298 // perform a tail call. 2299 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2300 return false; 2301 TCChain = Copy->getOperand(0); 2302 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) { 2303 SDNode *VMov = Copy; 2304 // f64 returned in a pair of GPRs. 2305 SmallPtrSet<SDNode*, 2> Copies; 2306 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2307 UI != UE; ++UI) { 2308 if (UI->getOpcode() != ISD::CopyToReg) 2309 return false; 2310 Copies.insert(*UI); 2311 } 2312 if (Copies.size() > 2) 2313 return false; 2314 2315 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2316 UI != UE; ++UI) { 2317 SDValue UseChain = UI->getOperand(0); 2318 if (Copies.count(UseChain.getNode())) 2319 // Second CopyToReg 2320 Copy = *UI; 2321 else { 2322 // We are at the top of this chain. 2323 // If the copy has a glue operand, we conservatively assume it 2324 // isn't safe to perform a tail call. 2325 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue) 2326 return false; 2327 // First CopyToReg 2328 TCChain = UseChain; 2329 } 2330 } 2331 } else if (Copy->getOpcode() == ISD::BITCAST) { 2332 // f32 returned in a single GPR. 2333 if (!Copy->hasOneUse()) 2334 return false; 2335 Copy = *Copy->use_begin(); 2336 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0)) 2337 return false; 2338 // If the copy has a glue operand, we conservatively assume it isn't safe to 2339 // perform a tail call. 2340 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2341 return false; 2342 TCChain = Copy->getOperand(0); 2343 } else { 2344 return false; 2345 } 2346 2347 bool HasRet = false; 2348 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 2349 UI != UE; ++UI) { 2350 if (UI->getOpcode() != ARMISD::RET_FLAG && 2351 UI->getOpcode() != ARMISD::INTRET_FLAG) 2352 return false; 2353 HasRet = true; 2354 } 2355 2356 if (!HasRet) 2357 return false; 2358 2359 Chain = TCChain; 2360 return true; 2361 } 2362 2363 bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 2364 if (!Subtarget->supportsTailCall()) 2365 return false; 2366 2367 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls) 2368 return false; 2369 2370 return !Subtarget->isThumb1Only(); 2371 } 2372 2373 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 2374 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 2375 // one of the above mentioned nodes. It has to be wrapped because otherwise 2376 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 2377 // be used to form addressing mode. These wrapped nodes will be selected 2378 // into MOVi. 2379 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) { 2380 EVT PtrVT = Op.getValueType(); 2381 // FIXME there is no actual debug info here 2382 SDLoc dl(Op); 2383 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2384 SDValue Res; 2385 if (CP->isMachineConstantPoolEntry()) 2386 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 2387 CP->getAlignment()); 2388 else 2389 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 2390 CP->getAlignment()); 2391 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 2392 } 2393 2394 unsigned ARMTargetLowering::getJumpTableEncoding() const { 2395 return MachineJumpTableInfo::EK_Inline; 2396 } 2397 2398 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 2399 SelectionDAG &DAG) const { 2400 MachineFunction &MF = DAG.getMachineFunction(); 2401 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2402 unsigned ARMPCLabelIndex = 0; 2403 SDLoc DL(Op); 2404 EVT PtrVT = getPointerTy(); 2405 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 2406 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2407 SDValue CPAddr; 2408 if (RelocM == Reloc::Static) { 2409 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 2410 } else { 2411 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2412 ARMPCLabelIndex = AFI->createPICLabelUId(); 2413 ARMConstantPoolValue *CPV = 2414 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 2415 ARMCP::CPBlockAddress, PCAdj); 2416 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2417 } 2418 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 2419 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr, 2420 MachinePointerInfo::getConstantPool(), 2421 false, false, false, 0); 2422 if (RelocM == Reloc::Static) 2423 return Result; 2424 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2425 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 2426 } 2427 2428 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 2429 SDValue 2430 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 2431 SelectionDAG &DAG) const { 2432 SDLoc dl(GA); 2433 EVT PtrVT = getPointerTy(); 2434 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2435 MachineFunction &MF = DAG.getMachineFunction(); 2436 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2437 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2438 ARMConstantPoolValue *CPV = 2439 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2440 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 2441 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2442 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 2443 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, 2444 MachinePointerInfo::getConstantPool(), 2445 false, false, false, 0); 2446 SDValue Chain = Argument.getValue(1); 2447 2448 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2449 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 2450 2451 // call __tls_get_addr. 2452 ArgListTy Args; 2453 ArgListEntry Entry; 2454 Entry.Node = Argument; 2455 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 2456 Args.push_back(Entry); 2457 2458 // FIXME: is there useful debug info available here? 2459 TargetLowering::CallLoweringInfo CLI(DAG); 2460 CLI.setDebugLoc(dl).setChain(Chain) 2461 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()), 2462 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args), 2463 0); 2464 2465 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2466 return CallResult.first; 2467 } 2468 2469 // Lower ISD::GlobalTLSAddress using the "initial exec" or 2470 // "local exec" model. 2471 SDValue 2472 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 2473 SelectionDAG &DAG, 2474 TLSModel::Model model) const { 2475 const GlobalValue *GV = GA->getGlobal(); 2476 SDLoc dl(GA); 2477 SDValue Offset; 2478 SDValue Chain = DAG.getEntryNode(); 2479 EVT PtrVT = getPointerTy(); 2480 // Get the Thread Pointer 2481 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2482 2483 if (model == TLSModel::InitialExec) { 2484 MachineFunction &MF = DAG.getMachineFunction(); 2485 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2486 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2487 // Initial exec model. 2488 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2489 ARMConstantPoolValue *CPV = 2490 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2491 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 2492 true); 2493 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2494 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2495 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2496 MachinePointerInfo::getConstantPool(), 2497 false, false, false, 0); 2498 Chain = Offset.getValue(1); 2499 2500 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2501 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 2502 2503 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2504 MachinePointerInfo::getConstantPool(), 2505 false, false, false, 0); 2506 } else { 2507 // local exec model 2508 assert(model == TLSModel::LocalExec); 2509 ARMConstantPoolValue *CPV = 2510 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 2511 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2512 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2513 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2514 MachinePointerInfo::getConstantPool(), 2515 false, false, false, 0); 2516 } 2517 2518 // The address of the thread local variable is the add of the thread 2519 // pointer with the offset of the variable. 2520 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 2521 } 2522 2523 SDValue 2524 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 2525 // TODO: implement the "local dynamic" model 2526 assert(Subtarget->isTargetELF() && 2527 "TLS not implemented for non-ELF targets"); 2528 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2529 2530 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal()); 2531 2532 switch (model) { 2533 case TLSModel::GeneralDynamic: 2534 case TLSModel::LocalDynamic: 2535 return LowerToTLSGeneralDynamicModel(GA, DAG); 2536 case TLSModel::InitialExec: 2537 case TLSModel::LocalExec: 2538 return LowerToTLSExecModels(GA, DAG, model); 2539 } 2540 llvm_unreachable("bogus TLS model"); 2541 } 2542 2543 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 2544 SelectionDAG &DAG) const { 2545 EVT PtrVT = getPointerTy(); 2546 SDLoc dl(Op); 2547 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2548 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 2549 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility(); 2550 ARMConstantPoolValue *CPV = 2551 ARMConstantPoolConstant::Create(GV, 2552 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT); 2553 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2554 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2555 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 2556 CPAddr, 2557 MachinePointerInfo::getConstantPool(), 2558 false, false, false, 0); 2559 SDValue Chain = Result.getValue(1); 2560 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT); 2561 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT); 2562 if (!UseGOTOFF) 2563 Result = DAG.getLoad(PtrVT, dl, Chain, Result, 2564 MachinePointerInfo::getGOT(), 2565 false, false, false, 0); 2566 return Result; 2567 } 2568 2569 // If we have T2 ops, we can materialize the address directly via movt/movw 2570 // pair. This is always cheaper. 2571 if (Subtarget->useMovt(DAG.getMachineFunction())) { 2572 ++NumMovwMovt; 2573 // FIXME: Once remat is capable of dealing with instructions with register 2574 // operands, expand this into two nodes. 2575 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2576 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2577 } else { 2578 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2579 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2580 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2581 MachinePointerInfo::getConstantPool(), 2582 false, false, false, 0); 2583 } 2584 } 2585 2586 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 2587 SelectionDAG &DAG) const { 2588 EVT PtrVT = getPointerTy(); 2589 SDLoc dl(Op); 2590 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2591 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2592 2593 if (Subtarget->useMovt(DAG.getMachineFunction())) 2594 ++NumMovwMovt; 2595 2596 // FIXME: Once remat is capable of dealing with instructions with register 2597 // operands, expand this into multiple nodes 2598 unsigned Wrapper = 2599 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper; 2600 2601 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY); 2602 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G); 2603 2604 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2605 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 2606 MachinePointerInfo::getGOT(), false, false, false, 0); 2607 return Result; 2608 } 2609 2610 SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op, 2611 SelectionDAG &DAG) const { 2612 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported"); 2613 assert(Subtarget->useMovt(DAG.getMachineFunction()) && 2614 "Windows on ARM expects to use movw/movt"); 2615 2616 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2617 const ARMII::TOF TargetFlags = 2618 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG); 2619 EVT PtrVT = getPointerTy(); 2620 SDValue Result; 2621 SDLoc DL(Op); 2622 2623 ++NumMovwMovt; 2624 2625 // FIXME: Once remat is capable of dealing with instructions with register 2626 // operands, expand this into two nodes. 2627 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, 2628 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0, 2629 TargetFlags)); 2630 if (GV->hasDLLImportStorageClass()) 2631 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, 2632 MachinePointerInfo::getGOT(), false, false, false, 0); 2633 return Result; 2634 } 2635 2636 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, 2637 SelectionDAG &DAG) const { 2638 assert(Subtarget->isTargetELF() && 2639 "GLOBAL OFFSET TABLE not implemented for non-ELF targets"); 2640 MachineFunction &MF = DAG.getMachineFunction(); 2641 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2642 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2643 EVT PtrVT = getPointerTy(); 2644 SDLoc dl(Op); 2645 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2646 ARMConstantPoolValue *CPV = 2647 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_", 2648 ARMPCLabelIndex, PCAdj); 2649 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2650 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2651 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2652 MachinePointerInfo::getConstantPool(), 2653 false, false, false, 0); 2654 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2655 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2656 } 2657 2658 SDValue 2659 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 2660 SDLoc dl(Op); 2661 SDValue Val = DAG.getConstant(0, MVT::i32); 2662 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 2663 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 2664 Op.getOperand(1), Val); 2665 } 2666 2667 SDValue 2668 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 2669 SDLoc dl(Op); 2670 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 2671 Op.getOperand(1), DAG.getConstant(0, MVT::i32)); 2672 } 2673 2674 SDValue 2675 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 2676 const ARMSubtarget *Subtarget) const { 2677 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2678 SDLoc dl(Op); 2679 switch (IntNo) { 2680 default: return SDValue(); // Don't custom lower most intrinsics. 2681 case Intrinsic::arm_rbit: { 2682 assert(Op.getOperand(1).getValueType() == MVT::i32 && 2683 "RBIT intrinsic must have i32 type!"); 2684 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1)); 2685 } 2686 case Intrinsic::arm_thread_pointer: { 2687 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2688 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2689 } 2690 case Intrinsic::eh_sjlj_lsda: { 2691 MachineFunction &MF = DAG.getMachineFunction(); 2692 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2693 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2694 EVT PtrVT = getPointerTy(); 2695 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2696 SDValue CPAddr; 2697 unsigned PCAdj = (RelocM != Reloc::PIC_) 2698 ? 0 : (Subtarget->isThumb() ? 4 : 8); 2699 ARMConstantPoolValue *CPV = 2700 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex, 2701 ARMCP::CPLSDA, PCAdj); 2702 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2703 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2704 SDValue Result = 2705 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2706 MachinePointerInfo::getConstantPool(), 2707 false, false, false, 0); 2708 2709 if (RelocM == Reloc::PIC_) { 2710 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32); 2711 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2712 } 2713 return Result; 2714 } 2715 case Intrinsic::arm_neon_vmulls: 2716 case Intrinsic::arm_neon_vmullu: { 2717 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 2718 ? ARMISD::VMULLs : ARMISD::VMULLu; 2719 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 2720 Op.getOperand(1), Op.getOperand(2)); 2721 } 2722 } 2723 } 2724 2725 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 2726 const ARMSubtarget *Subtarget) { 2727 // FIXME: handle "fence singlethread" more efficiently. 2728 SDLoc dl(Op); 2729 if (!Subtarget->hasDataBarrier()) { 2730 // Some ARMv6 cpus can support data barriers with an mcr instruction. 2731 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 2732 // here. 2733 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 2734 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!"); 2735 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 2736 DAG.getConstant(0, MVT::i32)); 2737 } 2738 2739 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1)); 2740 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue()); 2741 ARM_MB::MemBOpt Domain = ARM_MB::ISH; 2742 if (Subtarget->isMClass()) { 2743 // Only a full system barrier exists in the M-class architectures. 2744 Domain = ARM_MB::SY; 2745 } else if (Subtarget->isSwift() && Ord == Release) { 2746 // Swift happens to implement ISHST barriers in a way that's compatible with 2747 // Release semantics but weaker than ISH so we'd be fools not to use 2748 // it. Beware: other processors probably don't! 2749 Domain = ARM_MB::ISHST; 2750 } 2751 2752 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0), 2753 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32), 2754 DAG.getConstant(Domain, MVT::i32)); 2755 } 2756 2757 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 2758 const ARMSubtarget *Subtarget) { 2759 // ARM pre v5TE and Thumb1 does not have preload instructions. 2760 if (!(Subtarget->isThumb2() || 2761 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 2762 // Just preserve the chain. 2763 return Op.getOperand(0); 2764 2765 SDLoc dl(Op); 2766 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 2767 if (!isRead && 2768 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 2769 // ARMv7 with MP extension has PLDW. 2770 return Op.getOperand(0); 2771 2772 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 2773 if (Subtarget->isThumb()) { 2774 // Invert the bits. 2775 isRead = ~isRead & 1; 2776 isData = ~isData & 1; 2777 } 2778 2779 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 2780 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32), 2781 DAG.getConstant(isData, MVT::i32)); 2782 } 2783 2784 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 2785 MachineFunction &MF = DAG.getMachineFunction(); 2786 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 2787 2788 // vastart just stores the address of the VarArgsFrameIndex slot into the 2789 // memory location argument. 2790 SDLoc dl(Op); 2791 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2792 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 2793 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2794 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 2795 MachinePointerInfo(SV), false, false, 0); 2796 } 2797 2798 SDValue 2799 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA, 2800 SDValue &Root, SelectionDAG &DAG, 2801 SDLoc dl) const { 2802 MachineFunction &MF = DAG.getMachineFunction(); 2803 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2804 2805 const TargetRegisterClass *RC; 2806 if (AFI->isThumb1OnlyFunction()) 2807 RC = &ARM::tGPRRegClass; 2808 else 2809 RC = &ARM::GPRRegClass; 2810 2811 // Transform the arguments stored in physical registers into virtual ones. 2812 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2813 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2814 2815 SDValue ArgValue2; 2816 if (NextVA.isMemLoc()) { 2817 MachineFrameInfo *MFI = MF.getFrameInfo(); 2818 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true); 2819 2820 // Create load node to retrieve arguments from the stack. 2821 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2822 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, 2823 MachinePointerInfo::getFixedStack(FI), 2824 false, false, false, 0); 2825 } else { 2826 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 2827 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2828 } 2829 if (!Subtarget->isLittle()) 2830 std::swap (ArgValue, ArgValue2); 2831 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 2832 } 2833 2834 void 2835 ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF, 2836 unsigned InRegsParamRecordIdx, 2837 unsigned ArgSize, 2838 unsigned &ArgRegsSize, 2839 unsigned &ArgRegsSaveSize) 2840 const { 2841 unsigned NumGPRs; 2842 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 2843 unsigned RBegin, REnd; 2844 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 2845 NumGPRs = REnd - RBegin; 2846 } else { 2847 unsigned int firstUnalloced; 2848 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs, 2849 sizeof(GPRArgRegs) / 2850 sizeof(GPRArgRegs[0])); 2851 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0; 2852 } 2853 2854 unsigned Align = MF.getTarget() 2855 .getSubtargetImpl() 2856 ->getFrameLowering() 2857 ->getStackAlignment(); 2858 ArgRegsSize = NumGPRs * 4; 2859 2860 // If parameter is split between stack and GPRs... 2861 if (NumGPRs && Align > 4 && 2862 (ArgRegsSize < ArgSize || 2863 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) { 2864 // Add padding for part of param recovered from GPRs. For example, 2865 // if Align == 8, its last byte must be at address K*8 - 1. 2866 // We need to do it, since remained (stack) part of parameter has 2867 // stack alignment, and we need to "attach" "GPRs head" without gaps 2868 // to it: 2869 // Stack: 2870 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes... 2871 // [ [padding] [GPRs head] ] [ Tail passed via stack .... 2872 // 2873 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2874 unsigned Padding = 2875 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align); 2876 ArgRegsSaveSize = ArgRegsSize + Padding; 2877 } else 2878 // We don't need to extend regs save size for byval parameters if they 2879 // are passed via GPRs only. 2880 ArgRegsSaveSize = ArgRegsSize; 2881 } 2882 2883 // The remaining GPRs hold either the beginning of variable-argument 2884 // data, or the beginning of an aggregate passed by value (usually 2885 // byval). Either way, we allocate stack slots adjacent to the data 2886 // provided by our caller, and store the unallocated registers there. 2887 // If this is a variadic function, the va_list pointer will begin with 2888 // these values; otherwise, this reassembles a (byval) structure that 2889 // was split between registers and memory. 2890 // Return: The frame index registers were stored into. 2891 int 2892 ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG, 2893 SDLoc dl, SDValue &Chain, 2894 const Value *OrigArg, 2895 unsigned InRegsParamRecordIdx, 2896 unsigned OffsetFromOrigArg, 2897 unsigned ArgOffset, 2898 unsigned ArgSize, 2899 bool ForceMutable, 2900 unsigned ByValStoreOffset, 2901 unsigned TotalArgRegsSaveSize) const { 2902 2903 // Currently, two use-cases possible: 2904 // Case #1. Non-var-args function, and we meet first byval parameter. 2905 // Setup first unallocated register as first byval register; 2906 // eat all remained registers 2907 // (these two actions are performed by HandleByVal method). 2908 // Then, here, we initialize stack frame with 2909 // "store-reg" instructions. 2910 // Case #2. Var-args function, that doesn't contain byval parameters. 2911 // The same: eat all remained unallocated registers, 2912 // initialize stack frame. 2913 2914 MachineFunction &MF = DAG.getMachineFunction(); 2915 MachineFrameInfo *MFI = MF.getFrameInfo(); 2916 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2917 unsigned firstRegToSaveIndex, lastRegToSaveIndex; 2918 unsigned RBegin, REnd; 2919 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 2920 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 2921 firstRegToSaveIndex = RBegin - ARM::R0; 2922 lastRegToSaveIndex = REnd - ARM::R0; 2923 } else { 2924 firstRegToSaveIndex = CCInfo.getFirstUnallocated 2925 (GPRArgRegs, array_lengthof(GPRArgRegs)); 2926 lastRegToSaveIndex = 4; 2927 } 2928 2929 unsigned ArgRegsSize, ArgRegsSaveSize; 2930 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize, 2931 ArgRegsSize, ArgRegsSaveSize); 2932 2933 // Store any by-val regs to their spots on the stack so that they may be 2934 // loaded by deferencing the result of formal parameter pointer or va_next. 2935 // Note: once stack area for byval/varargs registers 2936 // was initialized, it can't be initialized again. 2937 if (ArgRegsSaveSize) { 2938 unsigned Padding = ArgRegsSaveSize - ArgRegsSize; 2939 2940 if (Padding) { 2941 assert(AFI->getStoredByValParamsPadding() == 0 && 2942 "The only parameter may be padded."); 2943 AFI->setStoredByValParamsPadding(Padding); 2944 } 2945 2946 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize, 2947 Padding + 2948 ByValStoreOffset - 2949 (int64_t)TotalArgRegsSaveSize, 2950 false); 2951 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy()); 2952 if (Padding) { 2953 MFI->CreateFixedObject(Padding, 2954 ArgOffset + ByValStoreOffset - 2955 (int64_t)ArgRegsSaveSize, 2956 false); 2957 } 2958 2959 SmallVector<SDValue, 4> MemOps; 2960 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex; 2961 ++firstRegToSaveIndex, ++i) { 2962 const TargetRegisterClass *RC; 2963 if (AFI->isThumb1OnlyFunction()) 2964 RC = &ARM::tGPRRegClass; 2965 else 2966 RC = &ARM::GPRRegClass; 2967 2968 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC); 2969 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 2970 SDValue Store = 2971 DAG.getStore(Val.getValue(1), dl, Val, FIN, 2972 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i), 2973 false, false, 0); 2974 MemOps.push_back(Store); 2975 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, 2976 DAG.getConstant(4, getPointerTy())); 2977 } 2978 2979 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize()); 2980 2981 if (!MemOps.empty()) 2982 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 2983 return FrameIndex; 2984 } else { 2985 if (ArgSize == 0) { 2986 // We cannot allocate a zero-byte object for the first variadic argument, 2987 // so just make up a size. 2988 ArgSize = 4; 2989 } 2990 // This will point to the next argument passed via stack. 2991 return MFI->CreateFixedObject( 2992 ArgSize, ArgOffset, !ForceMutable); 2993 } 2994 } 2995 2996 // Setup stack frame, the va_list pointer will start from. 2997 void 2998 ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 2999 SDLoc dl, SDValue &Chain, 3000 unsigned ArgOffset, 3001 unsigned TotalArgRegsSaveSize, 3002 bool ForceMutable) const { 3003 MachineFunction &MF = DAG.getMachineFunction(); 3004 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3005 3006 // Try to store any remaining integer argument regs 3007 // to their spots on the stack so that they may be loaded by deferencing 3008 // the result of va_next. 3009 // If there is no regs to be stored, just point address after last 3010 // argument passed via stack. 3011 int FrameIndex = 3012 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr, 3013 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable, 3014 0, TotalArgRegsSaveSize); 3015 3016 AFI->setVarArgsFrameIndex(FrameIndex); 3017 } 3018 3019 SDValue 3020 ARMTargetLowering::LowerFormalArguments(SDValue Chain, 3021 CallingConv::ID CallConv, bool isVarArg, 3022 const SmallVectorImpl<ISD::InputArg> 3023 &Ins, 3024 SDLoc dl, SelectionDAG &DAG, 3025 SmallVectorImpl<SDValue> &InVals) 3026 const { 3027 MachineFunction &MF = DAG.getMachineFunction(); 3028 MachineFrameInfo *MFI = MF.getFrameInfo(); 3029 3030 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3031 3032 // Assign locations to all of the incoming arguments. 3033 SmallVector<CCValAssign, 16> ArgLocs; 3034 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 3035 *DAG.getContext(), Prologue); 3036 CCInfo.AnalyzeFormalArguments(Ins, 3037 CCAssignFnForNode(CallConv, /* Return*/ false, 3038 isVarArg)); 3039 3040 SmallVector<SDValue, 16> ArgValues; 3041 int lastInsIndex = -1; 3042 SDValue ArgValue; 3043 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin(); 3044 unsigned CurArgIdx = 0; 3045 3046 // Initially ArgRegsSaveSize is zero. 3047 // Then we increase this value each time we meet byval parameter. 3048 // We also increase this value in case of varargs function. 3049 AFI->setArgRegsSaveSize(0); 3050 3051 unsigned ByValStoreOffset = 0; 3052 unsigned TotalArgRegsSaveSize = 0; 3053 unsigned ArgRegsSaveSizeMaxAlign = 4; 3054 3055 // Calculate the amount of stack space that we need to allocate to store 3056 // byval and variadic arguments that are passed in registers. 3057 // We need to know this before we allocate the first byval or variadic 3058 // argument, as they will be allocated a stack slot below the CFA (Canonical 3059 // Frame Address, the stack pointer at entry to the function). 3060 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3061 CCValAssign &VA = ArgLocs[i]; 3062 if (VA.isMemLoc()) { 3063 int index = VA.getValNo(); 3064 if (index != lastInsIndex) { 3065 ISD::ArgFlagsTy Flags = Ins[index].Flags; 3066 if (Flags.isByVal()) { 3067 unsigned ExtraArgRegsSize; 3068 unsigned ExtraArgRegsSaveSize; 3069 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(), 3070 Flags.getByValSize(), 3071 ExtraArgRegsSize, ExtraArgRegsSaveSize); 3072 3073 TotalArgRegsSaveSize += ExtraArgRegsSaveSize; 3074 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign) 3075 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign(); 3076 CCInfo.nextInRegsParam(); 3077 } 3078 lastInsIndex = index; 3079 } 3080 } 3081 } 3082 CCInfo.rewindByValRegsInfo(); 3083 lastInsIndex = -1; 3084 if (isVarArg && MFI->hasVAStart()) { 3085 unsigned ExtraArgRegsSize; 3086 unsigned ExtraArgRegsSaveSize; 3087 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0, 3088 ExtraArgRegsSize, ExtraArgRegsSaveSize); 3089 TotalArgRegsSaveSize += ExtraArgRegsSaveSize; 3090 } 3091 // If the arg regs save area contains N-byte aligned values, the 3092 // bottom of it must be at least N-byte aligned. 3093 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign); 3094 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U); 3095 3096 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3097 CCValAssign &VA = ArgLocs[i]; 3098 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx); 3099 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex; 3100 // Arguments stored in registers. 3101 if (VA.isRegLoc()) { 3102 EVT RegVT = VA.getLocVT(); 3103 3104 if (VA.needsCustom()) { 3105 // f64 and vector types are split up into multiple registers or 3106 // combinations of registers and stack slots. 3107 if (VA.getLocVT() == MVT::v2f64) { 3108 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 3109 Chain, DAG, dl); 3110 VA = ArgLocs[++i]; // skip ahead to next loc 3111 SDValue ArgValue2; 3112 if (VA.isMemLoc()) { 3113 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true); 3114 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 3115 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN, 3116 MachinePointerInfo::getFixedStack(FI), 3117 false, false, false, 0); 3118 } else { 3119 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 3120 Chain, DAG, dl); 3121 } 3122 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 3123 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3124 ArgValue, ArgValue1, DAG.getIntPtrConstant(0)); 3125 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3126 ArgValue, ArgValue2, DAG.getIntPtrConstant(1)); 3127 } else 3128 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 3129 3130 } else { 3131 const TargetRegisterClass *RC; 3132 3133 if (RegVT == MVT::f32) 3134 RC = &ARM::SPRRegClass; 3135 else if (RegVT == MVT::f64) 3136 RC = &ARM::DPRRegClass; 3137 else if (RegVT == MVT::v2f64) 3138 RC = &ARM::QPRRegClass; 3139 else if (RegVT == MVT::i32) 3140 RC = AFI->isThumb1OnlyFunction() ? 3141 (const TargetRegisterClass*)&ARM::tGPRRegClass : 3142 (const TargetRegisterClass*)&ARM::GPRRegClass; 3143 else 3144 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 3145 3146 // Transform the arguments in physical registers into virtual ones. 3147 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3148 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 3149 } 3150 3151 // If this is an 8 or 16-bit value, it is really passed promoted 3152 // to 32 bits. Insert an assert[sz]ext to capture this, then 3153 // truncate to the right size. 3154 switch (VA.getLocInfo()) { 3155 default: llvm_unreachable("Unknown loc info!"); 3156 case CCValAssign::Full: break; 3157 case CCValAssign::BCvt: 3158 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 3159 break; 3160 case CCValAssign::SExt: 3161 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 3162 DAG.getValueType(VA.getValVT())); 3163 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3164 break; 3165 case CCValAssign::ZExt: 3166 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 3167 DAG.getValueType(VA.getValVT())); 3168 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3169 break; 3170 } 3171 3172 InVals.push_back(ArgValue); 3173 3174 } else { // VA.isRegLoc() 3175 3176 // sanity check 3177 assert(VA.isMemLoc()); 3178 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 3179 3180 int index = ArgLocs[i].getValNo(); 3181 3182 // Some Ins[] entries become multiple ArgLoc[] entries. 3183 // Process them only once. 3184 if (index != lastInsIndex) 3185 { 3186 ISD::ArgFlagsTy Flags = Ins[index].Flags; 3187 // FIXME: For now, all byval parameter objects are marked mutable. 3188 // This can be changed with more analysis. 3189 // In case of tail call optimization mark all arguments mutable. 3190 // Since they could be overwritten by lowering of arguments in case of 3191 // a tail call. 3192 if (Flags.isByVal()) { 3193 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed(); 3194 3195 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign()); 3196 int FrameIndex = StoreByValRegs( 3197 CCInfo, DAG, dl, Chain, CurOrigArg, 3198 CurByValIndex, 3199 Ins[VA.getValNo()].PartOffset, 3200 VA.getLocMemOffset(), 3201 Flags.getByValSize(), 3202 true /*force mutable frames*/, 3203 ByValStoreOffset, 3204 TotalArgRegsSaveSize); 3205 ByValStoreOffset += Flags.getByValSize(); 3206 ByValStoreOffset = std::min(ByValStoreOffset, 16U); 3207 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy())); 3208 CCInfo.nextInRegsParam(); 3209 } else { 3210 unsigned FIOffset = VA.getLocMemOffset(); 3211 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 3212 FIOffset, true); 3213 3214 // Create load nodes to retrieve arguments from the stack. 3215 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 3216 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 3217 MachinePointerInfo::getFixedStack(FI), 3218 false, false, false, 0)); 3219 } 3220 lastInsIndex = index; 3221 } 3222 } 3223 } 3224 3225 // varargs 3226 if (isVarArg && MFI->hasVAStart()) 3227 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 3228 CCInfo.getNextStackOffset(), 3229 TotalArgRegsSaveSize); 3230 3231 AFI->setArgumentStackSize(CCInfo.getNextStackOffset()); 3232 3233 return Chain; 3234 } 3235 3236 /// isFloatingPointZero - Return true if this is +0.0. 3237 static bool isFloatingPointZero(SDValue Op) { 3238 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 3239 return CFP->getValueAPF().isPosZero(); 3240 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 3241 // Maybe this has already been legalized into the constant pool? 3242 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 3243 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 3244 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 3245 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 3246 return CFP->getValueAPF().isPosZero(); 3247 } 3248 } 3249 return false; 3250 } 3251 3252 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 3253 /// the given operands. 3254 SDValue 3255 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 3256 SDValue &ARMcc, SelectionDAG &DAG, 3257 SDLoc dl) const { 3258 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 3259 unsigned C = RHSC->getZExtValue(); 3260 if (!isLegalICmpImmediate(C)) { 3261 // Constant does not fit, try adjusting it by one? 3262 switch (CC) { 3263 default: break; 3264 case ISD::SETLT: 3265 case ISD::SETGE: 3266 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 3267 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 3268 RHS = DAG.getConstant(C-1, MVT::i32); 3269 } 3270 break; 3271 case ISD::SETULT: 3272 case ISD::SETUGE: 3273 if (C != 0 && isLegalICmpImmediate(C-1)) { 3274 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 3275 RHS = DAG.getConstant(C-1, MVT::i32); 3276 } 3277 break; 3278 case ISD::SETLE: 3279 case ISD::SETGT: 3280 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 3281 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 3282 RHS = DAG.getConstant(C+1, MVT::i32); 3283 } 3284 break; 3285 case ISD::SETULE: 3286 case ISD::SETUGT: 3287 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 3288 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 3289 RHS = DAG.getConstant(C+1, MVT::i32); 3290 } 3291 break; 3292 } 3293 } 3294 } 3295 3296 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3297 ARMISD::NodeType CompareType; 3298 switch (CondCode) { 3299 default: 3300 CompareType = ARMISD::CMP; 3301 break; 3302 case ARMCC::EQ: 3303 case ARMCC::NE: 3304 // Uses only Z Flag 3305 CompareType = ARMISD::CMPZ; 3306 break; 3307 } 3308 ARMcc = DAG.getConstant(CondCode, MVT::i32); 3309 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 3310 } 3311 3312 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 3313 SDValue 3314 ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG, 3315 SDLoc dl) const { 3316 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64); 3317 SDValue Cmp; 3318 if (!isFloatingPointZero(RHS)) 3319 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS); 3320 else 3321 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS); 3322 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 3323 } 3324 3325 /// duplicateCmp - Glue values can have only one use, so this function 3326 /// duplicates a comparison node. 3327 SDValue 3328 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 3329 unsigned Opc = Cmp.getOpcode(); 3330 SDLoc DL(Cmp); 3331 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 3332 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3333 3334 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 3335 Cmp = Cmp.getOperand(0); 3336 Opc = Cmp.getOpcode(); 3337 if (Opc == ARMISD::CMPFP) 3338 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3339 else { 3340 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 3341 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0)); 3342 } 3343 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 3344 } 3345 3346 std::pair<SDValue, SDValue> 3347 ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG, 3348 SDValue &ARMcc) const { 3349 assert(Op.getValueType() == MVT::i32 && "Unsupported value type"); 3350 3351 SDValue Value, OverflowCmp; 3352 SDValue LHS = Op.getOperand(0); 3353 SDValue RHS = Op.getOperand(1); 3354 3355 3356 // FIXME: We are currently always generating CMPs because we don't support 3357 // generating CMN through the backend. This is not as good as the natural 3358 // CMP case because it causes a register dependency and cannot be folded 3359 // later. 3360 3361 switch (Op.getOpcode()) { 3362 default: 3363 llvm_unreachable("Unknown overflow instruction!"); 3364 case ISD::SADDO: 3365 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32); 3366 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS); 3367 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS); 3368 break; 3369 case ISD::UADDO: 3370 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32); 3371 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS); 3372 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS); 3373 break; 3374 case ISD::SSUBO: 3375 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32); 3376 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS); 3377 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS); 3378 break; 3379 case ISD::USUBO: 3380 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32); 3381 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS); 3382 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS); 3383 break; 3384 } // switch (...) 3385 3386 return std::make_pair(Value, OverflowCmp); 3387 } 3388 3389 3390 SDValue 3391 ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const { 3392 // Let legalize expand this if it isn't a legal type yet. 3393 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 3394 return SDValue(); 3395 3396 SDValue Value, OverflowCmp; 3397 SDValue ARMcc; 3398 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc); 3399 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3400 // We use 0 and 1 as false and true values. 3401 SDValue TVal = DAG.getConstant(1, MVT::i32); 3402 SDValue FVal = DAG.getConstant(0, MVT::i32); 3403 EVT VT = Op.getValueType(); 3404 3405 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal, 3406 ARMcc, CCR, OverflowCmp); 3407 3408 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); 3409 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow); 3410 } 3411 3412 3413 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 3414 SDValue Cond = Op.getOperand(0); 3415 SDValue SelectTrue = Op.getOperand(1); 3416 SDValue SelectFalse = Op.getOperand(2); 3417 SDLoc dl(Op); 3418 unsigned Opc = Cond.getOpcode(); 3419 3420 if (Cond.getResNo() == 1 && 3421 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 3422 Opc == ISD::USUBO)) { 3423 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 3424 return SDValue(); 3425 3426 SDValue Value, OverflowCmp; 3427 SDValue ARMcc; 3428 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 3429 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3430 EVT VT = Op.getValueType(); 3431 3432 return getCMOV(SDLoc(Op), VT, SelectTrue, SelectFalse, ARMcc, CCR, 3433 OverflowCmp, DAG); 3434 } 3435 3436 // Convert: 3437 // 3438 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 3439 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 3440 // 3441 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 3442 const ConstantSDNode *CMOVTrue = 3443 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 3444 const ConstantSDNode *CMOVFalse = 3445 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 3446 3447 if (CMOVTrue && CMOVFalse) { 3448 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 3449 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 3450 3451 SDValue True; 3452 SDValue False; 3453 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 3454 True = SelectTrue; 3455 False = SelectFalse; 3456 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 3457 True = SelectFalse; 3458 False = SelectTrue; 3459 } 3460 3461 if (True.getNode() && False.getNode()) { 3462 EVT VT = Op.getValueType(); 3463 SDValue ARMcc = Cond.getOperand(2); 3464 SDValue CCR = Cond.getOperand(3); 3465 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 3466 assert(True.getValueType() == VT); 3467 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG); 3468 } 3469 } 3470 } 3471 3472 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the 3473 // undefined bits before doing a full-word comparison with zero. 3474 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond, 3475 DAG.getConstant(1, Cond.getValueType())); 3476 3477 return DAG.getSelectCC(dl, Cond, 3478 DAG.getConstant(0, Cond.getValueType()), 3479 SelectTrue, SelectFalse, ISD::SETNE); 3480 } 3481 3482 static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) { 3483 if (CC == ISD::SETNE) 3484 return ISD::SETEQ; 3485 return ISD::getSetCCInverse(CC, true); 3486 } 3487 3488 static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 3489 bool &swpCmpOps, bool &swpVselOps) { 3490 // Start by selecting the GE condition code for opcodes that return true for 3491 // 'equality' 3492 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE || 3493 CC == ISD::SETULE) 3494 CondCode = ARMCC::GE; 3495 3496 // and GT for opcodes that return false for 'equality'. 3497 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT || 3498 CC == ISD::SETULT) 3499 CondCode = ARMCC::GT; 3500 3501 // Since we are constrained to GE/GT, if the opcode contains 'less', we need 3502 // to swap the compare operands. 3503 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT || 3504 CC == ISD::SETULT) 3505 swpCmpOps = true; 3506 3507 // Both GT and GE are ordered comparisons, and return false for 'unordered'. 3508 // If we have an unordered opcode, we need to swap the operands to the VSEL 3509 // instruction (effectively negating the condition). 3510 // 3511 // This also has the effect of swapping which one of 'less' or 'greater' 3512 // returns true, so we also swap the compare operands. It also switches 3513 // whether we return true for 'equality', so we compensate by picking the 3514 // opposite condition code to our original choice. 3515 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE || 3516 CC == ISD::SETUGT) { 3517 swpCmpOps = !swpCmpOps; 3518 swpVselOps = !swpVselOps; 3519 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT; 3520 } 3521 3522 // 'ordered' is 'anything but unordered', so use the VS condition code and 3523 // swap the VSEL operands. 3524 if (CC == ISD::SETO) { 3525 CondCode = ARMCC::VS; 3526 swpVselOps = true; 3527 } 3528 3529 // 'unordered or not equal' is 'anything but equal', so use the EQ condition 3530 // code and swap the VSEL operands. 3531 if (CC == ISD::SETUNE) { 3532 CondCode = ARMCC::EQ; 3533 swpVselOps = true; 3534 } 3535 } 3536 3537 SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal, 3538 SDValue TrueVal, SDValue ARMcc, SDValue CCR, 3539 SDValue Cmp, SelectionDAG &DAG) const { 3540 if (Subtarget->isFPOnlySP() && VT == MVT::f64) { 3541 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl, 3542 DAG.getVTList(MVT::i32, MVT::i32), FalseVal); 3543 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl, 3544 DAG.getVTList(MVT::i32, MVT::i32), TrueVal); 3545 3546 SDValue TrueLow = TrueVal.getValue(0); 3547 SDValue TrueHigh = TrueVal.getValue(1); 3548 SDValue FalseLow = FalseVal.getValue(0); 3549 SDValue FalseHigh = FalseVal.getValue(1); 3550 3551 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow, 3552 ARMcc, CCR, Cmp); 3553 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh, 3554 ARMcc, CCR, duplicateCmp(Cmp, DAG)); 3555 3556 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High); 3557 } else { 3558 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, 3559 Cmp); 3560 } 3561 } 3562 3563 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 3564 EVT VT = Op.getValueType(); 3565 SDValue LHS = Op.getOperand(0); 3566 SDValue RHS = Op.getOperand(1); 3567 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 3568 SDValue TrueVal = Op.getOperand(2); 3569 SDValue FalseVal = Op.getOperand(3); 3570 SDLoc dl(Op); 3571 3572 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) { 3573 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 3574 dl); 3575 3576 // If softenSetCCOperands only returned one value, we should compare it to 3577 // zero. 3578 if (!RHS.getNode()) { 3579 RHS = DAG.getConstant(0, LHS.getValueType()); 3580 CC = ISD::SETNE; 3581 } 3582 } 3583 3584 if (LHS.getValueType() == MVT::i32) { 3585 // Try to generate VSEL on ARMv8. 3586 // The VSEL instruction can't use all the usual ARM condition 3587 // codes: it only has two bits to select the condition code, so it's 3588 // constrained to use only GE, GT, VS and EQ. 3589 // 3590 // To implement all the various ISD::SETXXX opcodes, we sometimes need to 3591 // swap the operands of the previous compare instruction (effectively 3592 // inverting the compare condition, swapping 'less' and 'greater') and 3593 // sometimes need to swap the operands to the VSEL (which inverts the 3594 // condition in the sense of firing whenever the previous condition didn't) 3595 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 || 3596 TrueVal.getValueType() == MVT::f64)) { 3597 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3598 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE || 3599 CondCode == ARMCC::VC || CondCode == ARMCC::NE) { 3600 CC = getInverseCCForVSEL(CC); 3601 std::swap(TrueVal, FalseVal); 3602 } 3603 } 3604 3605 SDValue ARMcc; 3606 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3607 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3608 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 3609 } 3610 3611 ARMCC::CondCodes CondCode, CondCode2; 3612 FPCCToARMCC(CC, CondCode, CondCode2); 3613 3614 // Try to generate VSEL on ARMv8. 3615 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 || 3616 TrueVal.getValueType() == MVT::f64)) { 3617 // We can select VMAXNM/VMINNM from a compare followed by a select with the 3618 // same operands, as follows: 3619 // c = fcmp [ogt, olt, ugt, ult] a, b 3620 // select c, a, b 3621 // We only do this in unsafe-fp-math, because signed zeros and NaNs are 3622 // handled differently than the original code sequence. 3623 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal && 3624 RHS == FalseVal) { 3625 if (CC == ISD::SETOGT || CC == ISD::SETUGT) 3626 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal); 3627 if (CC == ISD::SETOLT || CC == ISD::SETULT) 3628 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal); 3629 } 3630 3631 bool swpCmpOps = false; 3632 bool swpVselOps = false; 3633 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps); 3634 3635 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE || 3636 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) { 3637 if (swpCmpOps) 3638 std::swap(LHS, RHS); 3639 if (swpVselOps) 3640 std::swap(TrueVal, FalseVal); 3641 } 3642 } 3643 3644 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32); 3645 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3646 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3647 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 3648 if (CondCode2 != ARMCC::AL) { 3649 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32); 3650 // FIXME: Needs another CMP because flag can have but one use. 3651 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl); 3652 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG); 3653 } 3654 return Result; 3655 } 3656 3657 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 3658 /// to morph to an integer compare sequence. 3659 static bool canChangeToInt(SDValue Op, bool &SeenZero, 3660 const ARMSubtarget *Subtarget) { 3661 SDNode *N = Op.getNode(); 3662 if (!N->hasOneUse()) 3663 // Otherwise it requires moving the value from fp to integer registers. 3664 return false; 3665 if (!N->getNumValues()) 3666 return false; 3667 EVT VT = Op.getValueType(); 3668 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 3669 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 3670 // vmrs are very slow, e.g. cortex-a8. 3671 return false; 3672 3673 if (isFloatingPointZero(Op)) { 3674 SeenZero = true; 3675 return true; 3676 } 3677 return ISD::isNormalLoad(N); 3678 } 3679 3680 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 3681 if (isFloatingPointZero(Op)) 3682 return DAG.getConstant(0, MVT::i32); 3683 3684 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 3685 return DAG.getLoad(MVT::i32, SDLoc(Op), 3686 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(), 3687 Ld->isVolatile(), Ld->isNonTemporal(), 3688 Ld->isInvariant(), Ld->getAlignment()); 3689 3690 llvm_unreachable("Unknown VFP cmp argument!"); 3691 } 3692 3693 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 3694 SDValue &RetVal1, SDValue &RetVal2) { 3695 if (isFloatingPointZero(Op)) { 3696 RetVal1 = DAG.getConstant(0, MVT::i32); 3697 RetVal2 = DAG.getConstant(0, MVT::i32); 3698 return; 3699 } 3700 3701 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 3702 SDValue Ptr = Ld->getBasePtr(); 3703 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op), 3704 Ld->getChain(), Ptr, 3705 Ld->getPointerInfo(), 3706 Ld->isVolatile(), Ld->isNonTemporal(), 3707 Ld->isInvariant(), Ld->getAlignment()); 3708 3709 EVT PtrType = Ptr.getValueType(); 3710 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 3711 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op), 3712 PtrType, Ptr, DAG.getConstant(4, PtrType)); 3713 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op), 3714 Ld->getChain(), NewPtr, 3715 Ld->getPointerInfo().getWithOffset(4), 3716 Ld->isVolatile(), Ld->isNonTemporal(), 3717 Ld->isInvariant(), NewAlign); 3718 return; 3719 } 3720 3721 llvm_unreachable("Unknown VFP cmp argument!"); 3722 } 3723 3724 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 3725 /// f32 and even f64 comparisons to integer ones. 3726 SDValue 3727 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 3728 SDValue Chain = Op.getOperand(0); 3729 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3730 SDValue LHS = Op.getOperand(2); 3731 SDValue RHS = Op.getOperand(3); 3732 SDValue Dest = Op.getOperand(4); 3733 SDLoc dl(Op); 3734 3735 bool LHSSeenZero = false; 3736 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget); 3737 bool RHSSeenZero = false; 3738 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget); 3739 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) { 3740 // If unsafe fp math optimization is enabled and there are no other uses of 3741 // the CMP operands, and the condition code is EQ or NE, we can optimize it 3742 // to an integer comparison. 3743 if (CC == ISD::SETOEQ) 3744 CC = ISD::SETEQ; 3745 else if (CC == ISD::SETUNE) 3746 CC = ISD::SETNE; 3747 3748 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32); 3749 SDValue ARMcc; 3750 if (LHS.getValueType() == MVT::f32) { 3751 LHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3752 bitcastf32Toi32(LHS, DAG), Mask); 3753 RHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3754 bitcastf32Toi32(RHS, DAG), Mask); 3755 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3756 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3757 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3758 Chain, Dest, ARMcc, CCR, Cmp); 3759 } 3760 3761 SDValue LHS1, LHS2; 3762 SDValue RHS1, RHS2; 3763 expandf64Toi32(LHS, DAG, LHS1, LHS2); 3764 expandf64Toi32(RHS, DAG, RHS1, RHS2); 3765 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask); 3766 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask); 3767 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3768 ARMcc = DAG.getConstant(CondCode, MVT::i32); 3769 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3770 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 3771 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops); 3772 } 3773 3774 return SDValue(); 3775 } 3776 3777 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 3778 SDValue Chain = Op.getOperand(0); 3779 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3780 SDValue LHS = Op.getOperand(2); 3781 SDValue RHS = Op.getOperand(3); 3782 SDValue Dest = Op.getOperand(4); 3783 SDLoc dl(Op); 3784 3785 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) { 3786 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 3787 dl); 3788 3789 // If softenSetCCOperands only returned one value, we should compare it to 3790 // zero. 3791 if (!RHS.getNode()) { 3792 RHS = DAG.getConstant(0, LHS.getValueType()); 3793 CC = ISD::SETNE; 3794 } 3795 } 3796 3797 if (LHS.getValueType() == MVT::i32) { 3798 SDValue ARMcc; 3799 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3800 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3801 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3802 Chain, Dest, ARMcc, CCR, Cmp); 3803 } 3804 3805 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 3806 3807 if (getTargetMachine().Options.UnsafeFPMath && 3808 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 3809 CC == ISD::SETNE || CC == ISD::SETUNE)) { 3810 SDValue Result = OptimizeVFPBrcond(Op, DAG); 3811 if (Result.getNode()) 3812 return Result; 3813 } 3814 3815 ARMCC::CondCodes CondCode, CondCode2; 3816 FPCCToARMCC(CC, CondCode, CondCode2); 3817 3818 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32); 3819 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3820 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3821 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3822 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 3823 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 3824 if (CondCode2 != ARMCC::AL) { 3825 ARMcc = DAG.getConstant(CondCode2, MVT::i32); 3826 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 3827 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 3828 } 3829 return Res; 3830 } 3831 3832 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 3833 SDValue Chain = Op.getOperand(0); 3834 SDValue Table = Op.getOperand(1); 3835 SDValue Index = Op.getOperand(2); 3836 SDLoc dl(Op); 3837 3838 EVT PTy = getPointerTy(); 3839 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 3840 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>(); 3841 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy); 3842 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 3843 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId); 3844 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy)); 3845 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); 3846 if (Subtarget->isThumb2()) { 3847 // Thumb2 uses a two-level jump. That is, it jumps into the jump table 3848 // which does another jump to the destination. This also makes it easier 3849 // to translate it to TBB / TBH later. 3850 // FIXME: This might not work if the function is extremely large. 3851 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 3852 Addr, Op.getOperand(2), JTI, UId); 3853 } 3854 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 3855 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 3856 MachinePointerInfo::getJumpTable(), 3857 false, false, false, 0); 3858 Chain = Addr.getValue(1); 3859 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table); 3860 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId); 3861 } else { 3862 Addr = DAG.getLoad(PTy, dl, Chain, Addr, 3863 MachinePointerInfo::getJumpTable(), 3864 false, false, false, 0); 3865 Chain = Addr.getValue(1); 3866 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId); 3867 } 3868 } 3869 3870 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 3871 EVT VT = Op.getValueType(); 3872 SDLoc dl(Op); 3873 3874 if (Op.getValueType().getVectorElementType() == MVT::i32) { 3875 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 3876 return Op; 3877 return DAG.UnrollVectorOp(Op.getNode()); 3878 } 3879 3880 assert(Op.getOperand(0).getValueType() == MVT::v4f32 && 3881 "Invalid type for custom lowering!"); 3882 if (VT != MVT::v4i16) 3883 return DAG.UnrollVectorOp(Op.getNode()); 3884 3885 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0)); 3886 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op); 3887 } 3888 3889 SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const { 3890 EVT VT = Op.getValueType(); 3891 if (VT.isVector()) 3892 return LowerVectorFP_TO_INT(Op, DAG); 3893 3894 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) { 3895 RTLIB::Libcall LC; 3896 if (Op.getOpcode() == ISD::FP_TO_SINT) 3897 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), 3898 Op.getValueType()); 3899 else 3900 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), 3901 Op.getValueType()); 3902 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1, 3903 /*isSigned*/ false, SDLoc(Op)).first; 3904 } 3905 3906 SDLoc dl(Op); 3907 unsigned Opc; 3908 3909 switch (Op.getOpcode()) { 3910 default: llvm_unreachable("Invalid opcode!"); 3911 case ISD::FP_TO_SINT: 3912 Opc = ARMISD::FTOSI; 3913 break; 3914 case ISD::FP_TO_UINT: 3915 Opc = ARMISD::FTOUI; 3916 break; 3917 } 3918 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0)); 3919 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); 3920 } 3921 3922 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 3923 EVT VT = Op.getValueType(); 3924 SDLoc dl(Op); 3925 3926 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 3927 if (VT.getVectorElementType() == MVT::f32) 3928 return Op; 3929 return DAG.UnrollVectorOp(Op.getNode()); 3930 } 3931 3932 assert(Op.getOperand(0).getValueType() == MVT::v4i16 && 3933 "Invalid type for custom lowering!"); 3934 if (VT != MVT::v4f32) 3935 return DAG.UnrollVectorOp(Op.getNode()); 3936 3937 unsigned CastOpc; 3938 unsigned Opc; 3939 switch (Op.getOpcode()) { 3940 default: llvm_unreachable("Invalid opcode!"); 3941 case ISD::SINT_TO_FP: 3942 CastOpc = ISD::SIGN_EXTEND; 3943 Opc = ISD::SINT_TO_FP; 3944 break; 3945 case ISD::UINT_TO_FP: 3946 CastOpc = ISD::ZERO_EXTEND; 3947 Opc = ISD::UINT_TO_FP; 3948 break; 3949 } 3950 3951 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0)); 3952 return DAG.getNode(Opc, dl, VT, Op); 3953 } 3954 3955 SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const { 3956 EVT VT = Op.getValueType(); 3957 if (VT.isVector()) 3958 return LowerVectorINT_TO_FP(Op, DAG); 3959 3960 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) { 3961 RTLIB::Libcall LC; 3962 if (Op.getOpcode() == ISD::SINT_TO_FP) 3963 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), 3964 Op.getValueType()); 3965 else 3966 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), 3967 Op.getValueType()); 3968 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1, 3969 /*isSigned*/ false, SDLoc(Op)).first; 3970 } 3971 3972 SDLoc dl(Op); 3973 unsigned Opc; 3974 3975 switch (Op.getOpcode()) { 3976 default: llvm_unreachable("Invalid opcode!"); 3977 case ISD::SINT_TO_FP: 3978 Opc = ARMISD::SITOF; 3979 break; 3980 case ISD::UINT_TO_FP: 3981 Opc = ARMISD::UITOF; 3982 break; 3983 } 3984 3985 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0)); 3986 return DAG.getNode(Opc, dl, VT, Op); 3987 } 3988 3989 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 3990 // Implement fcopysign with a fabs and a conditional fneg. 3991 SDValue Tmp0 = Op.getOperand(0); 3992 SDValue Tmp1 = Op.getOperand(1); 3993 SDLoc dl(Op); 3994 EVT VT = Op.getValueType(); 3995 EVT SrcVT = Tmp1.getValueType(); 3996 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 3997 Tmp0.getOpcode() == ARMISD::VMOVDRR; 3998 bool UseNEON = !InGPR && Subtarget->hasNEON(); 3999 4000 if (UseNEON) { 4001 // Use VBSL to copy the sign bit. 4002 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80); 4003 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 4004 DAG.getTargetConstant(EncodedVal, MVT::i32)); 4005 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 4006 if (VT == MVT::f64) 4007 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4008 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 4009 DAG.getConstant(32, MVT::i32)); 4010 else /*if (VT == MVT::f32)*/ 4011 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 4012 if (SrcVT == MVT::f32) { 4013 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 4014 if (VT == MVT::f64) 4015 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4016 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 4017 DAG.getConstant(32, MVT::i32)); 4018 } else if (VT == MVT::f32) 4019 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64, 4020 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 4021 DAG.getConstant(32, MVT::i32)); 4022 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 4023 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 4024 4025 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff), 4026 MVT::i32); 4027 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 4028 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 4029 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 4030 4031 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 4032 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 4033 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 4034 if (VT == MVT::f32) { 4035 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 4036 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 4037 DAG.getConstant(0, MVT::i32)); 4038 } else { 4039 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 4040 } 4041 4042 return Res; 4043 } 4044 4045 // Bitcast operand 1 to i32. 4046 if (SrcVT == MVT::f64) 4047 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 4048 Tmp1).getValue(1); 4049 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 4050 4051 // Or in the signbit with integer operations. 4052 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32); 4053 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32); 4054 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 4055 if (VT == MVT::f32) { 4056 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 4057 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 4058 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 4059 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 4060 } 4061 4062 // f64: Or the high part with signbit and then combine two parts. 4063 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 4064 Tmp0); 4065 SDValue Lo = Tmp0.getValue(0); 4066 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 4067 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 4068 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 4069 } 4070 4071 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 4072 MachineFunction &MF = DAG.getMachineFunction(); 4073 MachineFrameInfo *MFI = MF.getFrameInfo(); 4074 MFI->setReturnAddressIsTaken(true); 4075 4076 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 4077 return SDValue(); 4078 4079 EVT VT = Op.getValueType(); 4080 SDLoc dl(Op); 4081 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4082 if (Depth) { 4083 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 4084 SDValue Offset = DAG.getConstant(4, MVT::i32); 4085 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 4086 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 4087 MachinePointerInfo(), false, false, false, 0); 4088 } 4089 4090 // Return LR, which contains the return address. Mark it an implicit live-in. 4091 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 4092 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 4093 } 4094 4095 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 4096 const ARMBaseRegisterInfo &ARI = 4097 *static_cast<const ARMBaseRegisterInfo*>(RegInfo); 4098 MachineFunction &MF = DAG.getMachineFunction(); 4099 MachineFrameInfo *MFI = MF.getFrameInfo(); 4100 MFI->setFrameAddressIsTaken(true); 4101 4102 EVT VT = Op.getValueType(); 4103 SDLoc dl(Op); // FIXME probably not meaningful 4104 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4105 unsigned FrameReg = ARI.getFrameRegister(MF); 4106 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 4107 while (Depth--) 4108 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 4109 MachinePointerInfo(), 4110 false, false, false, 0); 4111 return FrameAddr; 4112 } 4113 4114 // FIXME? Maybe this could be a TableGen attribute on some registers and 4115 // this table could be generated automatically from RegInfo. 4116 unsigned ARMTargetLowering::getRegisterByName(const char* RegName, 4117 EVT VT) const { 4118 unsigned Reg = StringSwitch<unsigned>(RegName) 4119 .Case("sp", ARM::SP) 4120 .Default(0); 4121 if (Reg) 4122 return Reg; 4123 report_fatal_error("Invalid register name global variable"); 4124 } 4125 4126 /// ExpandBITCAST - If the target supports VFP, this function is called to 4127 /// expand a bit convert where either the source or destination type is i64 to 4128 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 4129 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 4130 /// vectors), since the legalizer won't know what to do with that. 4131 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) { 4132 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4133 SDLoc dl(N); 4134 SDValue Op = N->getOperand(0); 4135 4136 // This function is only supposed to be called for i64 types, either as the 4137 // source or destination of the bit convert. 4138 EVT SrcVT = Op.getValueType(); 4139 EVT DstVT = N->getValueType(0); 4140 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) && 4141 "ExpandBITCAST called for non-i64 type"); 4142 4143 // Turn i64->f64 into VMOVDRR. 4144 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 4145 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 4146 DAG.getConstant(0, MVT::i32)); 4147 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 4148 DAG.getConstant(1, MVT::i32)); 4149 return DAG.getNode(ISD::BITCAST, dl, DstVT, 4150 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 4151 } 4152 4153 // Turn f64->i64 into VMOVRRD. 4154 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 4155 SDValue Cvt; 4156 if (TLI.isBigEndian() && SrcVT.isVector() && 4157 SrcVT.getVectorNumElements() > 1) 4158 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 4159 DAG.getVTList(MVT::i32, MVT::i32), 4160 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op)); 4161 else 4162 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 4163 DAG.getVTList(MVT::i32, MVT::i32), Op); 4164 // Merge the pieces into a single i64 value. 4165 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 4166 } 4167 4168 return SDValue(); 4169 } 4170 4171 /// getZeroVector - Returns a vector of specified type with all zero elements. 4172 /// Zero vectors are used to represent vector negation and in those cases 4173 /// will be implemented with the NEON VNEG instruction. However, VNEG does 4174 /// not support i64 elements, so sometimes the zero vectors will need to be 4175 /// explicitly constructed. Regardless, use a canonical VMOV to create the 4176 /// zero vector. 4177 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) { 4178 assert(VT.isVector() && "Expected a vector type"); 4179 // The canonical modified immediate encoding of a zero vector is....0! 4180 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32); 4181 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 4182 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 4183 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 4184 } 4185 4186 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 4187 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 4188 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 4189 SelectionDAG &DAG) const { 4190 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4191 EVT VT = Op.getValueType(); 4192 unsigned VTBits = VT.getSizeInBits(); 4193 SDLoc dl(Op); 4194 SDValue ShOpLo = Op.getOperand(0); 4195 SDValue ShOpHi = Op.getOperand(1); 4196 SDValue ShAmt = Op.getOperand(2); 4197 SDValue ARMcc; 4198 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 4199 4200 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 4201 4202 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 4203 DAG.getConstant(VTBits, MVT::i32), ShAmt); 4204 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 4205 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 4206 DAG.getConstant(VTBits, MVT::i32)); 4207 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 4208 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 4209 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 4210 4211 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4212 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE, 4213 ARMcc, DAG, dl); 4214 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 4215 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, 4216 CCR, Cmp); 4217 4218 SDValue Ops[2] = { Lo, Hi }; 4219 return DAG.getMergeValues(Ops, dl); 4220 } 4221 4222 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 4223 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 4224 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 4225 SelectionDAG &DAG) const { 4226 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4227 EVT VT = Op.getValueType(); 4228 unsigned VTBits = VT.getSizeInBits(); 4229 SDLoc dl(Op); 4230 SDValue ShOpLo = Op.getOperand(0); 4231 SDValue ShOpHi = Op.getOperand(1); 4232 SDValue ShAmt = Op.getOperand(2); 4233 SDValue ARMcc; 4234 4235 assert(Op.getOpcode() == ISD::SHL_PARTS); 4236 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 4237 DAG.getConstant(VTBits, MVT::i32), ShAmt); 4238 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 4239 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 4240 DAG.getConstant(VTBits, MVT::i32)); 4241 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 4242 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 4243 4244 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 4245 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4246 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE, 4247 ARMcc, DAG, dl); 4248 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 4249 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc, 4250 CCR, Cmp); 4251 4252 SDValue Ops[2] = { Lo, Hi }; 4253 return DAG.getMergeValues(Ops, dl); 4254 } 4255 4256 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 4257 SelectionDAG &DAG) const { 4258 // The rounding mode is in bits 23:22 of the FPSCR. 4259 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 4260 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 4261 // so that the shift + and get folded into a bitfield extract. 4262 SDLoc dl(Op); 4263 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, 4264 DAG.getConstant(Intrinsic::arm_get_fpscr, 4265 MVT::i32)); 4266 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 4267 DAG.getConstant(1U << 22, MVT::i32)); 4268 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 4269 DAG.getConstant(22, MVT::i32)); 4270 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 4271 DAG.getConstant(3, MVT::i32)); 4272 } 4273 4274 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 4275 const ARMSubtarget *ST) { 4276 EVT VT = N->getValueType(0); 4277 SDLoc dl(N); 4278 4279 if (!ST->hasV6T2Ops()) 4280 return SDValue(); 4281 4282 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0)); 4283 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 4284 } 4285 4286 /// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count 4287 /// for each 16-bit element from operand, repeated. The basic idea is to 4288 /// leverage vcnt to get the 8-bit counts, gather and add the results. 4289 /// 4290 /// Trace for v4i16: 4291 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 4292 /// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element) 4293 /// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi) 4294 /// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6] 4295 /// [b0 b1 b2 b3 b4 b5 b6 b7] 4296 /// +[b1 b0 b3 b2 b5 b4 b7 b6] 4297 /// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0, 4298 /// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits) 4299 static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) { 4300 EVT VT = N->getValueType(0); 4301 SDLoc DL(N); 4302 4303 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 4304 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0)); 4305 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0); 4306 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1); 4307 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2); 4308 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3); 4309 } 4310 4311 /// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the 4312 /// bit-count for each 16-bit element from the operand. We need slightly 4313 /// different sequencing for v4i16 and v8i16 to stay within NEON's available 4314 /// 64/128-bit registers. 4315 /// 4316 /// Trace for v4i16: 4317 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 4318 /// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi) 4319 /// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ] 4320 /// v4i16:Extracted = [k0 k1 k2 k3 ] 4321 static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) { 4322 EVT VT = N->getValueType(0); 4323 SDLoc DL(N); 4324 4325 SDValue BitCounts = getCTPOP16BitCounts(N, DAG); 4326 if (VT.is64BitVector()) { 4327 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts); 4328 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended, 4329 DAG.getIntPtrConstant(0)); 4330 } else { 4331 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8, 4332 BitCounts, DAG.getIntPtrConstant(0)); 4333 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted); 4334 } 4335 } 4336 4337 /// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the 4338 /// bit-count for each 32-bit element from the operand. The idea here is 4339 /// to split the vector into 16-bit elements, leverage the 16-bit count 4340 /// routine, and then combine the results. 4341 /// 4342 /// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged): 4343 /// input = [v0 v1 ] (vi: 32-bit elements) 4344 /// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1]) 4345 /// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi) 4346 /// vrev: N0 = [k1 k0 k3 k2 ] 4347 /// [k0 k1 k2 k3 ] 4348 /// N1 =+[k1 k0 k3 k2 ] 4349 /// [k0 k2 k1 k3 ] 4350 /// N2 =+[k1 k3 k0 k2 ] 4351 /// [k0 k2 k1 k3 ] 4352 /// Extended =+[k1 k3 k0 k2 ] 4353 /// [k0 k2 ] 4354 /// Extracted=+[k1 k3 ] 4355 /// 4356 static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) { 4357 EVT VT = N->getValueType(0); 4358 SDLoc DL(N); 4359 4360 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16; 4361 4362 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0)); 4363 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG); 4364 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16); 4365 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0); 4366 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1); 4367 4368 if (VT.is64BitVector()) { 4369 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2); 4370 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended, 4371 DAG.getIntPtrConstant(0)); 4372 } else { 4373 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2, 4374 DAG.getIntPtrConstant(0)); 4375 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted); 4376 } 4377 } 4378 4379 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG, 4380 const ARMSubtarget *ST) { 4381 EVT VT = N->getValueType(0); 4382 4383 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON."); 4384 assert((VT == MVT::v2i32 || VT == MVT::v4i32 || 4385 VT == MVT::v4i16 || VT == MVT::v8i16) && 4386 "Unexpected type for custom ctpop lowering"); 4387 4388 if (VT.getVectorElementType() == MVT::i32) 4389 return lowerCTPOP32BitElements(N, DAG); 4390 else 4391 return lowerCTPOP16BitElements(N, DAG); 4392 } 4393 4394 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 4395 const ARMSubtarget *ST) { 4396 EVT VT = N->getValueType(0); 4397 SDLoc dl(N); 4398 4399 if (!VT.isVector()) 4400 return SDValue(); 4401 4402 // Lower vector shifts on NEON to use VSHL. 4403 assert(ST->hasNEON() && "unexpected vector shift"); 4404 4405 // Left shifts translate directly to the vshiftu intrinsic. 4406 if (N->getOpcode() == ISD::SHL) 4407 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4408 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32), 4409 N->getOperand(0), N->getOperand(1)); 4410 4411 assert((N->getOpcode() == ISD::SRA || 4412 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode"); 4413 4414 // NEON uses the same intrinsics for both left and right shifts. For 4415 // right shifts, the shift amounts are negative, so negate the vector of 4416 // shift amounts. 4417 EVT ShiftVT = N->getOperand(1).getValueType(); 4418 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT, 4419 getZeroVector(ShiftVT, DAG, dl), 4420 N->getOperand(1)); 4421 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ? 4422 Intrinsic::arm_neon_vshifts : 4423 Intrinsic::arm_neon_vshiftu); 4424 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4425 DAG.getConstant(vshiftInt, MVT::i32), 4426 N->getOperand(0), NegatedCount); 4427 } 4428 4429 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 4430 const ARMSubtarget *ST) { 4431 EVT VT = N->getValueType(0); 4432 SDLoc dl(N); 4433 4434 // We can get here for a node like i32 = ISD::SHL i32, i64 4435 if (VT != MVT::i64) 4436 return SDValue(); 4437 4438 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && 4439 "Unknown shift to lower!"); 4440 4441 // We only lower SRA, SRL of 1 here, all others use generic lowering. 4442 if (!isa<ConstantSDNode>(N->getOperand(1)) || 4443 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1) 4444 return SDValue(); 4445 4446 // If we are in thumb mode, we don't have RRX. 4447 if (ST->isThumb1Only()) return SDValue(); 4448 4449 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 4450 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 4451 DAG.getConstant(0, MVT::i32)); 4452 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 4453 DAG.getConstant(1, MVT::i32)); 4454 4455 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 4456 // captures the result into a carry flag. 4457 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 4458 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi); 4459 4460 // The low part is an ARMISD::RRX operand, which shifts the carry in. 4461 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 4462 4463 // Merge the pieces into a single i64 value. 4464 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 4465 } 4466 4467 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 4468 SDValue TmpOp0, TmpOp1; 4469 bool Invert = false; 4470 bool Swap = false; 4471 unsigned Opc = 0; 4472 4473 SDValue Op0 = Op.getOperand(0); 4474 SDValue Op1 = Op.getOperand(1); 4475 SDValue CC = Op.getOperand(2); 4476 EVT VT = Op.getValueType(); 4477 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 4478 SDLoc dl(Op); 4479 4480 if (Op1.getValueType().isFloatingPoint()) { 4481 switch (SetCCOpcode) { 4482 default: llvm_unreachable("Illegal FP comparison"); 4483 case ISD::SETUNE: 4484 case ISD::SETNE: Invert = true; // Fallthrough 4485 case ISD::SETOEQ: 4486 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 4487 case ISD::SETOLT: 4488 case ISD::SETLT: Swap = true; // Fallthrough 4489 case ISD::SETOGT: 4490 case ISD::SETGT: Opc = ARMISD::VCGT; break; 4491 case ISD::SETOLE: 4492 case ISD::SETLE: Swap = true; // Fallthrough 4493 case ISD::SETOGE: 4494 case ISD::SETGE: Opc = ARMISD::VCGE; break; 4495 case ISD::SETUGE: Swap = true; // Fallthrough 4496 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break; 4497 case ISD::SETUGT: Swap = true; // Fallthrough 4498 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break; 4499 case ISD::SETUEQ: Invert = true; // Fallthrough 4500 case ISD::SETONE: 4501 // Expand this to (OLT | OGT). 4502 TmpOp0 = Op0; 4503 TmpOp1 = Op1; 4504 Opc = ISD::OR; 4505 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0); 4506 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1); 4507 break; 4508 case ISD::SETUO: Invert = true; // Fallthrough 4509 case ISD::SETO: 4510 // Expand this to (OLT | OGE). 4511 TmpOp0 = Op0; 4512 TmpOp1 = Op1; 4513 Opc = ISD::OR; 4514 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0); 4515 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1); 4516 break; 4517 } 4518 } else { 4519 // Integer comparisons. 4520 switch (SetCCOpcode) { 4521 default: llvm_unreachable("Illegal integer comparison"); 4522 case ISD::SETNE: Invert = true; 4523 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 4524 case ISD::SETLT: Swap = true; 4525 case ISD::SETGT: Opc = ARMISD::VCGT; break; 4526 case ISD::SETLE: Swap = true; 4527 case ISD::SETGE: Opc = ARMISD::VCGE; break; 4528 case ISD::SETULT: Swap = true; 4529 case ISD::SETUGT: Opc = ARMISD::VCGTU; break; 4530 case ISD::SETULE: Swap = true; 4531 case ISD::SETUGE: Opc = ARMISD::VCGEU; break; 4532 } 4533 4534 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 4535 if (Opc == ARMISD::VCEQ) { 4536 4537 SDValue AndOp; 4538 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 4539 AndOp = Op0; 4540 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 4541 AndOp = Op1; 4542 4543 // Ignore bitconvert. 4544 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 4545 AndOp = AndOp.getOperand(0); 4546 4547 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 4548 Opc = ARMISD::VTST; 4549 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0)); 4550 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1)); 4551 Invert = !Invert; 4552 } 4553 } 4554 } 4555 4556 if (Swap) 4557 std::swap(Op0, Op1); 4558 4559 // If one of the operands is a constant vector zero, attempt to fold the 4560 // comparison to a specialized compare-against-zero form. 4561 SDValue SingleOp; 4562 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 4563 SingleOp = Op0; 4564 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 4565 if (Opc == ARMISD::VCGE) 4566 Opc = ARMISD::VCLEZ; 4567 else if (Opc == ARMISD::VCGT) 4568 Opc = ARMISD::VCLTZ; 4569 SingleOp = Op1; 4570 } 4571 4572 SDValue Result; 4573 if (SingleOp.getNode()) { 4574 switch (Opc) { 4575 case ARMISD::VCEQ: 4576 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break; 4577 case ARMISD::VCGE: 4578 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break; 4579 case ARMISD::VCLEZ: 4580 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break; 4581 case ARMISD::VCGT: 4582 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break; 4583 case ARMISD::VCLTZ: 4584 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break; 4585 default: 4586 Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 4587 } 4588 } else { 4589 Result = DAG.getNode(Opc, dl, VT, Op0, Op1); 4590 } 4591 4592 if (Invert) 4593 Result = DAG.getNOT(dl, Result, VT); 4594 4595 return Result; 4596 } 4597 4598 /// isNEONModifiedImm - Check if the specified splat value corresponds to a 4599 /// valid vector constant for a NEON instruction with a "modified immediate" 4600 /// operand (e.g., VMOV). If so, return the encoded value. 4601 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 4602 unsigned SplatBitSize, SelectionDAG &DAG, 4603 EVT &VT, bool is128Bits, NEONModImmType type) { 4604 unsigned OpCmode, Imm; 4605 4606 // SplatBitSize is set to the smallest size that splats the vector, so a 4607 // zero vector will always have SplatBitSize == 8. However, NEON modified 4608 // immediate instructions others than VMOV do not support the 8-bit encoding 4609 // of a zero vector, and the default encoding of zero is supposed to be the 4610 // 32-bit version. 4611 if (SplatBits == 0) 4612 SplatBitSize = 32; 4613 4614 switch (SplatBitSize) { 4615 case 8: 4616 if (type != VMOVModImm) 4617 return SDValue(); 4618 // Any 1-byte value is OK. Op=0, Cmode=1110. 4619 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 4620 OpCmode = 0xe; 4621 Imm = SplatBits; 4622 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 4623 break; 4624 4625 case 16: 4626 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 4627 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 4628 if ((SplatBits & ~0xff) == 0) { 4629 // Value = 0x00nn: Op=x, Cmode=100x. 4630 OpCmode = 0x8; 4631 Imm = SplatBits; 4632 break; 4633 } 4634 if ((SplatBits & ~0xff00) == 0) { 4635 // Value = 0xnn00: Op=x, Cmode=101x. 4636 OpCmode = 0xa; 4637 Imm = SplatBits >> 8; 4638 break; 4639 } 4640 return SDValue(); 4641 4642 case 32: 4643 // NEON's 32-bit VMOV supports splat values where: 4644 // * only one byte is nonzero, or 4645 // * the least significant byte is 0xff and the second byte is nonzero, or 4646 // * the least significant 2 bytes are 0xff and the third is nonzero. 4647 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 4648 if ((SplatBits & ~0xff) == 0) { 4649 // Value = 0x000000nn: Op=x, Cmode=000x. 4650 OpCmode = 0; 4651 Imm = SplatBits; 4652 break; 4653 } 4654 if ((SplatBits & ~0xff00) == 0) { 4655 // Value = 0x0000nn00: Op=x, Cmode=001x. 4656 OpCmode = 0x2; 4657 Imm = SplatBits >> 8; 4658 break; 4659 } 4660 if ((SplatBits & ~0xff0000) == 0) { 4661 // Value = 0x00nn0000: Op=x, Cmode=010x. 4662 OpCmode = 0x4; 4663 Imm = SplatBits >> 16; 4664 break; 4665 } 4666 if ((SplatBits & ~0xff000000) == 0) { 4667 // Value = 0xnn000000: Op=x, Cmode=011x. 4668 OpCmode = 0x6; 4669 Imm = SplatBits >> 24; 4670 break; 4671 } 4672 4673 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 4674 if (type == OtherModImm) return SDValue(); 4675 4676 if ((SplatBits & ~0xffff) == 0 && 4677 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 4678 // Value = 0x0000nnff: Op=x, Cmode=1100. 4679 OpCmode = 0xc; 4680 Imm = SplatBits >> 8; 4681 break; 4682 } 4683 4684 if ((SplatBits & ~0xffffff) == 0 && 4685 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 4686 // Value = 0x00nnffff: Op=x, Cmode=1101. 4687 OpCmode = 0xd; 4688 Imm = SplatBits >> 16; 4689 break; 4690 } 4691 4692 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 4693 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 4694 // VMOV.I32. A (very) minor optimization would be to replicate the value 4695 // and fall through here to test for a valid 64-bit splat. But, then the 4696 // caller would also need to check and handle the change in size. 4697 return SDValue(); 4698 4699 case 64: { 4700 if (type != VMOVModImm) 4701 return SDValue(); 4702 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 4703 uint64_t BitMask = 0xff; 4704 uint64_t Val = 0; 4705 unsigned ImmMask = 1; 4706 Imm = 0; 4707 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 4708 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 4709 Val |= BitMask; 4710 Imm |= ImmMask; 4711 } else if ((SplatBits & BitMask) != 0) { 4712 return SDValue(); 4713 } 4714 BitMask <<= 8; 4715 ImmMask <<= 1; 4716 } 4717 4718 if (DAG.getTargetLoweringInfo().isBigEndian()) 4719 // swap higher and lower 32 bit word 4720 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4); 4721 4722 // Op=1, Cmode=1110. 4723 OpCmode = 0x1e; 4724 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 4725 break; 4726 } 4727 4728 default: 4729 llvm_unreachable("unexpected size for isNEONModifiedImm"); 4730 } 4731 4732 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm); 4733 return DAG.getTargetConstant(EncodedVal, MVT::i32); 4734 } 4735 4736 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG, 4737 const ARMSubtarget *ST) const { 4738 if (!ST->hasVFP3()) 4739 return SDValue(); 4740 4741 bool IsDouble = Op.getValueType() == MVT::f64; 4742 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op); 4743 4744 // Use the default (constant pool) lowering for double constants when we have 4745 // an SP-only FPU 4746 if (IsDouble && Subtarget->isFPOnlySP()) 4747 return SDValue(); 4748 4749 // Try splatting with a VMOV.f32... 4750 APFloat FPVal = CFP->getValueAPF(); 4751 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal); 4752 4753 if (ImmVal != -1) { 4754 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) { 4755 // We have code in place to select a valid ConstantFP already, no need to 4756 // do any mangling. 4757 return Op; 4758 } 4759 4760 // It's a float and we are trying to use NEON operations where 4761 // possible. Lower it to a splat followed by an extract. 4762 SDLoc DL(Op); 4763 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32); 4764 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, 4765 NewVal); 4766 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant, 4767 DAG.getConstant(0, MVT::i32)); 4768 } 4769 4770 // The rest of our options are NEON only, make sure that's allowed before 4771 // proceeding.. 4772 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP())) 4773 return SDValue(); 4774 4775 EVT VMovVT; 4776 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue(); 4777 4778 // It wouldn't really be worth bothering for doubles except for one very 4779 // important value, which does happen to match: 0.0. So make sure we don't do 4780 // anything stupid. 4781 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32)) 4782 return SDValue(); 4783 4784 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too). 4785 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT, 4786 false, VMOVModImm); 4787 if (NewVal != SDValue()) { 4788 SDLoc DL(Op); 4789 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT, 4790 NewVal); 4791 if (IsDouble) 4792 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 4793 4794 // It's a float: cast and extract a vector element. 4795 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 4796 VecConstant); 4797 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 4798 DAG.getConstant(0, MVT::i32)); 4799 } 4800 4801 // Finally, try a VMVN.i32 4802 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT, 4803 false, VMVNModImm); 4804 if (NewVal != SDValue()) { 4805 SDLoc DL(Op); 4806 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal); 4807 4808 if (IsDouble) 4809 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 4810 4811 // It's a float: cast and extract a vector element. 4812 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 4813 VecConstant); 4814 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 4815 DAG.getConstant(0, MVT::i32)); 4816 } 4817 4818 return SDValue(); 4819 } 4820 4821 // check if an VEXT instruction can handle the shuffle mask when the 4822 // vector sources of the shuffle are the same. 4823 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) { 4824 unsigned NumElts = VT.getVectorNumElements(); 4825 4826 // Assume that the first shuffle index is not UNDEF. Fail if it is. 4827 if (M[0] < 0) 4828 return false; 4829 4830 Imm = M[0]; 4831 4832 // If this is a VEXT shuffle, the immediate value is the index of the first 4833 // element. The other shuffle indices must be the successive elements after 4834 // the first one. 4835 unsigned ExpectedElt = Imm; 4836 for (unsigned i = 1; i < NumElts; ++i) { 4837 // Increment the expected index. If it wraps around, just follow it 4838 // back to index zero and keep going. 4839 ++ExpectedElt; 4840 if (ExpectedElt == NumElts) 4841 ExpectedElt = 0; 4842 4843 if (M[i] < 0) continue; // ignore UNDEF indices 4844 if (ExpectedElt != static_cast<unsigned>(M[i])) 4845 return false; 4846 } 4847 4848 return true; 4849 } 4850 4851 4852 static bool isVEXTMask(ArrayRef<int> M, EVT VT, 4853 bool &ReverseVEXT, unsigned &Imm) { 4854 unsigned NumElts = VT.getVectorNumElements(); 4855 ReverseVEXT = false; 4856 4857 // Assume that the first shuffle index is not UNDEF. Fail if it is. 4858 if (M[0] < 0) 4859 return false; 4860 4861 Imm = M[0]; 4862 4863 // If this is a VEXT shuffle, the immediate value is the index of the first 4864 // element. The other shuffle indices must be the successive elements after 4865 // the first one. 4866 unsigned ExpectedElt = Imm; 4867 for (unsigned i = 1; i < NumElts; ++i) { 4868 // Increment the expected index. If it wraps around, it may still be 4869 // a VEXT but the source vectors must be swapped. 4870 ExpectedElt += 1; 4871 if (ExpectedElt == NumElts * 2) { 4872 ExpectedElt = 0; 4873 ReverseVEXT = true; 4874 } 4875 4876 if (M[i] < 0) continue; // ignore UNDEF indices 4877 if (ExpectedElt != static_cast<unsigned>(M[i])) 4878 return false; 4879 } 4880 4881 // Adjust the index value if the source operands will be swapped. 4882 if (ReverseVEXT) 4883 Imm -= NumElts; 4884 4885 return true; 4886 } 4887 4888 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 4889 /// instruction with the specified blocksize. (The order of the elements 4890 /// within each block of the vector is reversed.) 4891 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 4892 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 4893 "Only possible block sizes for VREV are: 16, 32, 64"); 4894 4895 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4896 if (EltSz == 64) 4897 return false; 4898 4899 unsigned NumElts = VT.getVectorNumElements(); 4900 unsigned BlockElts = M[0] + 1; 4901 // If the first shuffle index is UNDEF, be optimistic. 4902 if (M[0] < 0) 4903 BlockElts = BlockSize / EltSz; 4904 4905 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 4906 return false; 4907 4908 for (unsigned i = 0; i < NumElts; ++i) { 4909 if (M[i] < 0) continue; // ignore UNDEF indices 4910 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 4911 return false; 4912 } 4913 4914 return true; 4915 } 4916 4917 static bool isVTBLMask(ArrayRef<int> M, EVT VT) { 4918 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 4919 // range, then 0 is placed into the resulting vector. So pretty much any mask 4920 // of 8 elements can work here. 4921 return VT == MVT::v8i8 && M.size() == 8; 4922 } 4923 4924 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4925 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4926 if (EltSz == 64) 4927 return false; 4928 4929 unsigned NumElts = VT.getVectorNumElements(); 4930 WhichResult = (M[0] == 0 ? 0 : 1); 4931 for (unsigned i = 0; i < NumElts; i += 2) { 4932 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 4933 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult)) 4934 return false; 4935 } 4936 return true; 4937 } 4938 4939 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 4940 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4941 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 4942 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 4943 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4944 if (EltSz == 64) 4945 return false; 4946 4947 unsigned NumElts = VT.getVectorNumElements(); 4948 WhichResult = (M[0] == 0 ? 0 : 1); 4949 for (unsigned i = 0; i < NumElts; i += 2) { 4950 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 4951 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult)) 4952 return false; 4953 } 4954 return true; 4955 } 4956 4957 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4958 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4959 if (EltSz == 64) 4960 return false; 4961 4962 unsigned NumElts = VT.getVectorNumElements(); 4963 WhichResult = (M[0] == 0 ? 0 : 1); 4964 for (unsigned i = 0; i != NumElts; ++i) { 4965 if (M[i] < 0) continue; // ignore UNDEF indices 4966 if ((unsigned) M[i] != 2 * i + WhichResult) 4967 return false; 4968 } 4969 4970 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4971 if (VT.is64BitVector() && EltSz == 32) 4972 return false; 4973 4974 return true; 4975 } 4976 4977 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 4978 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4979 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 4980 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 4981 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4982 if (EltSz == 64) 4983 return false; 4984 4985 unsigned Half = VT.getVectorNumElements() / 2; 4986 WhichResult = (M[0] == 0 ? 0 : 1); 4987 for (unsigned j = 0; j != 2; ++j) { 4988 unsigned Idx = WhichResult; 4989 for (unsigned i = 0; i != Half; ++i) { 4990 int MIdx = M[i + j * Half]; 4991 if (MIdx >= 0 && (unsigned) MIdx != Idx) 4992 return false; 4993 Idx += 2; 4994 } 4995 } 4996 4997 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4998 if (VT.is64BitVector() && EltSz == 32) 4999 return false; 5000 5001 return true; 5002 } 5003 5004 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5005 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5006 if (EltSz == 64) 5007 return false; 5008 5009 unsigned NumElts = VT.getVectorNumElements(); 5010 WhichResult = (M[0] == 0 ? 0 : 1); 5011 unsigned Idx = WhichResult * NumElts / 2; 5012 for (unsigned i = 0; i != NumElts; i += 2) { 5013 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 5014 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts)) 5015 return false; 5016 Idx += 1; 5017 } 5018 5019 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5020 if (VT.is64BitVector() && EltSz == 32) 5021 return false; 5022 5023 return true; 5024 } 5025 5026 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 5027 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 5028 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 5029 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 5030 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5031 if (EltSz == 64) 5032 return false; 5033 5034 unsigned NumElts = VT.getVectorNumElements(); 5035 WhichResult = (M[0] == 0 ? 0 : 1); 5036 unsigned Idx = WhichResult * NumElts / 2; 5037 for (unsigned i = 0; i != NumElts; i += 2) { 5038 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 5039 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx)) 5040 return false; 5041 Idx += 1; 5042 } 5043 5044 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5045 if (VT.is64BitVector() && EltSz == 32) 5046 return false; 5047 5048 return true; 5049 } 5050 5051 /// \return true if this is a reverse operation on an vector. 5052 static bool isReverseMask(ArrayRef<int> M, EVT VT) { 5053 unsigned NumElts = VT.getVectorNumElements(); 5054 // Make sure the mask has the right size. 5055 if (NumElts != M.size()) 5056 return false; 5057 5058 // Look for <15, ..., 3, -1, 1, 0>. 5059 for (unsigned i = 0; i != NumElts; ++i) 5060 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i)) 5061 return false; 5062 5063 return true; 5064 } 5065 5066 // If N is an integer constant that can be moved into a register in one 5067 // instruction, return an SDValue of such a constant (will become a MOV 5068 // instruction). Otherwise return null. 5069 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 5070 const ARMSubtarget *ST, SDLoc dl) { 5071 uint64_t Val; 5072 if (!isa<ConstantSDNode>(N)) 5073 return SDValue(); 5074 Val = cast<ConstantSDNode>(N)->getZExtValue(); 5075 5076 if (ST->isThumb1Only()) { 5077 if (Val <= 255 || ~Val <= 255) 5078 return DAG.getConstant(Val, MVT::i32); 5079 } else { 5080 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 5081 return DAG.getConstant(Val, MVT::i32); 5082 } 5083 return SDValue(); 5084 } 5085 5086 // If this is a case we can't handle, return null and let the default 5087 // expansion code take care of it. 5088 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 5089 const ARMSubtarget *ST) const { 5090 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 5091 SDLoc dl(Op); 5092 EVT VT = Op.getValueType(); 5093 5094 APInt SplatBits, SplatUndef; 5095 unsigned SplatBitSize; 5096 bool HasAnyUndefs; 5097 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 5098 if (SplatBitSize <= 64) { 5099 // Check if an immediate VMOV works. 5100 EVT VmovVT; 5101 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 5102 SplatUndef.getZExtValue(), SplatBitSize, 5103 DAG, VmovVT, VT.is128BitVector(), 5104 VMOVModImm); 5105 if (Val.getNode()) { 5106 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 5107 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5108 } 5109 5110 // Try an immediate VMVN. 5111 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 5112 Val = isNEONModifiedImm(NegatedImm, 5113 SplatUndef.getZExtValue(), SplatBitSize, 5114 DAG, VmovVT, VT.is128BitVector(), 5115 VMVNModImm); 5116 if (Val.getNode()) { 5117 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 5118 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5119 } 5120 5121 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 5122 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) { 5123 int ImmVal = ARM_AM::getFP32Imm(SplatBits); 5124 if (ImmVal != -1) { 5125 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32); 5126 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 5127 } 5128 } 5129 } 5130 } 5131 5132 // Scan through the operands to see if only one value is used. 5133 // 5134 // As an optimisation, even if more than one value is used it may be more 5135 // profitable to splat with one value then change some lanes. 5136 // 5137 // Heuristically we decide to do this if the vector has a "dominant" value, 5138 // defined as splatted to more than half of the lanes. 5139 unsigned NumElts = VT.getVectorNumElements(); 5140 bool isOnlyLowElement = true; 5141 bool usesOnlyOneValue = true; 5142 bool hasDominantValue = false; 5143 bool isConstant = true; 5144 5145 // Map of the number of times a particular SDValue appears in the 5146 // element list. 5147 DenseMap<SDValue, unsigned> ValueCounts; 5148 SDValue Value; 5149 for (unsigned i = 0; i < NumElts; ++i) { 5150 SDValue V = Op.getOperand(i); 5151 if (V.getOpcode() == ISD::UNDEF) 5152 continue; 5153 if (i > 0) 5154 isOnlyLowElement = false; 5155 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 5156 isConstant = false; 5157 5158 ValueCounts.insert(std::make_pair(V, 0)); 5159 unsigned &Count = ValueCounts[V]; 5160 5161 // Is this value dominant? (takes up more than half of the lanes) 5162 if (++Count > (NumElts / 2)) { 5163 hasDominantValue = true; 5164 Value = V; 5165 } 5166 } 5167 if (ValueCounts.size() != 1) 5168 usesOnlyOneValue = false; 5169 if (!Value.getNode() && ValueCounts.size() > 0) 5170 Value = ValueCounts.begin()->first; 5171 5172 if (ValueCounts.size() == 0) 5173 return DAG.getUNDEF(VT); 5174 5175 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR. 5176 // Keep going if we are hitting this case. 5177 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode())) 5178 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 5179 5180 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5181 5182 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 5183 // i32 and try again. 5184 if (hasDominantValue && EltSize <= 32) { 5185 if (!isConstant) { 5186 SDValue N; 5187 5188 // If we are VDUPing a value that comes directly from a vector, that will 5189 // cause an unnecessary move to and from a GPR, where instead we could 5190 // just use VDUPLANE. We can only do this if the lane being extracted 5191 // is at a constant index, as the VDUP from lane instructions only have 5192 // constant-index forms. 5193 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5194 isa<ConstantSDNode>(Value->getOperand(1))) { 5195 // We need to create a new undef vector to use for the VDUPLANE if the 5196 // size of the vector from which we get the value is different than the 5197 // size of the vector that we need to create. We will insert the element 5198 // such that the register coalescer will remove unnecessary copies. 5199 if (VT != Value->getOperand(0).getValueType()) { 5200 ConstantSDNode *constIndex; 5201 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)); 5202 assert(constIndex && "The index is not a constant!"); 5203 unsigned index = constIndex->getAPIntValue().getLimitedValue() % 5204 VT.getVectorNumElements(); 5205 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5206 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT), 5207 Value, DAG.getConstant(index, MVT::i32)), 5208 DAG.getConstant(index, MVT::i32)); 5209 } else 5210 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5211 Value->getOperand(0), Value->getOperand(1)); 5212 } else 5213 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value); 5214 5215 if (!usesOnlyOneValue) { 5216 // The dominant value was splatted as 'N', but we now have to insert 5217 // all differing elements. 5218 for (unsigned I = 0; I < NumElts; ++I) { 5219 if (Op.getOperand(I) == Value) 5220 continue; 5221 SmallVector<SDValue, 3> Ops; 5222 Ops.push_back(N); 5223 Ops.push_back(Op.getOperand(I)); 5224 Ops.push_back(DAG.getConstant(I, MVT::i32)); 5225 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops); 5226 } 5227 } 5228 return N; 5229 } 5230 if (VT.getVectorElementType().isFloatingPoint()) { 5231 SmallVector<SDValue, 8> Ops; 5232 for (unsigned i = 0; i < NumElts; ++i) 5233 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32, 5234 Op.getOperand(i))); 5235 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 5236 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops); 5237 Val = LowerBUILD_VECTOR(Val, DAG, ST); 5238 if (Val.getNode()) 5239 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5240 } 5241 if (usesOnlyOneValue) { 5242 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 5243 if (isConstant && Val.getNode()) 5244 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 5245 } 5246 } 5247 5248 // If all elements are constants and the case above didn't get hit, fall back 5249 // to the default expansion, which will generate a load from the constant 5250 // pool. 5251 if (isConstant) 5252 return SDValue(); 5253 5254 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 5255 if (NumElts >= 4) { 5256 SDValue shuffle = ReconstructShuffle(Op, DAG); 5257 if (shuffle != SDValue()) 5258 return shuffle; 5259 } 5260 5261 // Vectors with 32- or 64-bit elements can be built by directly assigning 5262 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 5263 // will be legalized. 5264 if (EltSize >= 32) { 5265 // Do the expansion with floating-point types, since that is what the VFP 5266 // registers are defined to use, and since i64 is not legal. 5267 EVT EltVT = EVT::getFloatingPointVT(EltSize); 5268 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 5269 SmallVector<SDValue, 8> Ops; 5270 for (unsigned i = 0; i < NumElts; ++i) 5271 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 5272 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 5273 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5274 } 5275 5276 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 5277 // know the default expansion would otherwise fall back on something even 5278 // worse. For a vector with one or two non-undef values, that's 5279 // scalar_to_vector for the elements followed by a shuffle (provided the 5280 // shuffle is valid for the target) and materialization element by element 5281 // on the stack followed by a load for everything else. 5282 if (!isConstant && !usesOnlyOneValue) { 5283 SDValue Vec = DAG.getUNDEF(VT); 5284 for (unsigned i = 0 ; i < NumElts; ++i) { 5285 SDValue V = Op.getOperand(i); 5286 if (V.getOpcode() == ISD::UNDEF) 5287 continue; 5288 SDValue LaneIdx = DAG.getConstant(i, MVT::i32); 5289 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx); 5290 } 5291 return Vec; 5292 } 5293 5294 return SDValue(); 5295 } 5296 5297 // Gather data to see if the operation can be modelled as a 5298 // shuffle in combination with VEXTs. 5299 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 5300 SelectionDAG &DAG) const { 5301 SDLoc dl(Op); 5302 EVT VT = Op.getValueType(); 5303 unsigned NumElts = VT.getVectorNumElements(); 5304 5305 SmallVector<SDValue, 2> SourceVecs; 5306 SmallVector<unsigned, 2> MinElts; 5307 SmallVector<unsigned, 2> MaxElts; 5308 5309 for (unsigned i = 0; i < NumElts; ++i) { 5310 SDValue V = Op.getOperand(i); 5311 if (V.getOpcode() == ISD::UNDEF) 5312 continue; 5313 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 5314 // A shuffle can only come from building a vector from various 5315 // elements of other vectors. 5316 return SDValue(); 5317 } else if (V.getOperand(0).getValueType().getVectorElementType() != 5318 VT.getVectorElementType()) { 5319 // This code doesn't know how to handle shuffles where the vector 5320 // element types do not match (this happens because type legalization 5321 // promotes the return type of EXTRACT_VECTOR_ELT). 5322 // FIXME: It might be appropriate to extend this code to handle 5323 // mismatched types. 5324 return SDValue(); 5325 } 5326 5327 // Record this extraction against the appropriate vector if possible... 5328 SDValue SourceVec = V.getOperand(0); 5329 // If the element number isn't a constant, we can't effectively 5330 // analyze what's going on. 5331 if (!isa<ConstantSDNode>(V.getOperand(1))) 5332 return SDValue(); 5333 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 5334 bool FoundSource = false; 5335 for (unsigned j = 0; j < SourceVecs.size(); ++j) { 5336 if (SourceVecs[j] == SourceVec) { 5337 if (MinElts[j] > EltNo) 5338 MinElts[j] = EltNo; 5339 if (MaxElts[j] < EltNo) 5340 MaxElts[j] = EltNo; 5341 FoundSource = true; 5342 break; 5343 } 5344 } 5345 5346 // Or record a new source if not... 5347 if (!FoundSource) { 5348 SourceVecs.push_back(SourceVec); 5349 MinElts.push_back(EltNo); 5350 MaxElts.push_back(EltNo); 5351 } 5352 } 5353 5354 // Currently only do something sane when at most two source vectors 5355 // involved. 5356 if (SourceVecs.size() > 2) 5357 return SDValue(); 5358 5359 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) }; 5360 int VEXTOffsets[2] = {0, 0}; 5361 5362 // This loop extracts the usage patterns of the source vectors 5363 // and prepares appropriate SDValues for a shuffle if possible. 5364 for (unsigned i = 0; i < SourceVecs.size(); ++i) { 5365 if (SourceVecs[i].getValueType() == VT) { 5366 // No VEXT necessary 5367 ShuffleSrcs[i] = SourceVecs[i]; 5368 VEXTOffsets[i] = 0; 5369 continue; 5370 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) { 5371 // It probably isn't worth padding out a smaller vector just to 5372 // break it down again in a shuffle. 5373 return SDValue(); 5374 } 5375 5376 // Since only 64-bit and 128-bit vectors are legal on ARM and 5377 // we've eliminated the other cases... 5378 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts && 5379 "unexpected vector sizes in ReconstructShuffle"); 5380 5381 if (MaxElts[i] - MinElts[i] >= NumElts) { 5382 // Span too large for a VEXT to cope 5383 return SDValue(); 5384 } 5385 5386 if (MinElts[i] >= NumElts) { 5387 // The extraction can just take the second half 5388 VEXTOffsets[i] = NumElts; 5389 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5390 SourceVecs[i], 5391 DAG.getIntPtrConstant(NumElts)); 5392 } else if (MaxElts[i] < NumElts) { 5393 // The extraction can just take the first half 5394 VEXTOffsets[i] = 0; 5395 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5396 SourceVecs[i], 5397 DAG.getIntPtrConstant(0)); 5398 } else { 5399 // An actual VEXT is needed 5400 VEXTOffsets[i] = MinElts[i]; 5401 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5402 SourceVecs[i], 5403 DAG.getIntPtrConstant(0)); 5404 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5405 SourceVecs[i], 5406 DAG.getIntPtrConstant(NumElts)); 5407 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2, 5408 DAG.getConstant(VEXTOffsets[i], MVT::i32)); 5409 } 5410 } 5411 5412 SmallVector<int, 8> Mask; 5413 5414 for (unsigned i = 0; i < NumElts; ++i) { 5415 SDValue Entry = Op.getOperand(i); 5416 if (Entry.getOpcode() == ISD::UNDEF) { 5417 Mask.push_back(-1); 5418 continue; 5419 } 5420 5421 SDValue ExtractVec = Entry.getOperand(0); 5422 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i) 5423 .getOperand(1))->getSExtValue(); 5424 if (ExtractVec == SourceVecs[0]) { 5425 Mask.push_back(ExtractElt - VEXTOffsets[0]); 5426 } else { 5427 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]); 5428 } 5429 } 5430 5431 // Final check before we try to produce nonsense... 5432 if (isShuffleMaskLegal(Mask, VT)) 5433 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1], 5434 &Mask[0]); 5435 5436 return SDValue(); 5437 } 5438 5439 /// isShuffleMaskLegal - Targets can use this to indicate that they only 5440 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 5441 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 5442 /// are assumed to be legal. 5443 bool 5444 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, 5445 EVT VT) const { 5446 if (VT.getVectorNumElements() == 4 && 5447 (VT.is128BitVector() || VT.is64BitVector())) { 5448 unsigned PFIndexes[4]; 5449 for (unsigned i = 0; i != 4; ++i) { 5450 if (M[i] < 0) 5451 PFIndexes[i] = 8; 5452 else 5453 PFIndexes[i] = M[i]; 5454 } 5455 5456 // Compute the index in the perfect shuffle table. 5457 unsigned PFTableIndex = 5458 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 5459 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 5460 unsigned Cost = (PFEntry >> 30); 5461 5462 if (Cost <= 4) 5463 return true; 5464 } 5465 5466 bool ReverseVEXT; 5467 unsigned Imm, WhichResult; 5468 5469 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5470 return (EltSize >= 32 || 5471 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 5472 isVREVMask(M, VT, 64) || 5473 isVREVMask(M, VT, 32) || 5474 isVREVMask(M, VT, 16) || 5475 isVEXTMask(M, VT, ReverseVEXT, Imm) || 5476 isVTBLMask(M, VT) || 5477 isVTRNMask(M, VT, WhichResult) || 5478 isVUZPMask(M, VT, WhichResult) || 5479 isVZIPMask(M, VT, WhichResult) || 5480 isVTRN_v_undef_Mask(M, VT, WhichResult) || 5481 isVUZP_v_undef_Mask(M, VT, WhichResult) || 5482 isVZIP_v_undef_Mask(M, VT, WhichResult) || 5483 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT))); 5484 } 5485 5486 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 5487 /// the specified operations to build the shuffle. 5488 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 5489 SDValue RHS, SelectionDAG &DAG, 5490 SDLoc dl) { 5491 unsigned OpNum = (PFEntry >> 26) & 0x0F; 5492 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 5493 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 5494 5495 enum { 5496 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 5497 OP_VREV, 5498 OP_VDUP0, 5499 OP_VDUP1, 5500 OP_VDUP2, 5501 OP_VDUP3, 5502 OP_VEXT1, 5503 OP_VEXT2, 5504 OP_VEXT3, 5505 OP_VUZPL, // VUZP, left result 5506 OP_VUZPR, // VUZP, right result 5507 OP_VZIPL, // VZIP, left result 5508 OP_VZIPR, // VZIP, right result 5509 OP_VTRNL, // VTRN, left result 5510 OP_VTRNR // VTRN, right result 5511 }; 5512 5513 if (OpNum == OP_COPY) { 5514 if (LHSID == (1*9+2)*9+3) return LHS; 5515 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 5516 return RHS; 5517 } 5518 5519 SDValue OpLHS, OpRHS; 5520 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 5521 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 5522 EVT VT = OpLHS.getValueType(); 5523 5524 switch (OpNum) { 5525 default: llvm_unreachable("Unknown shuffle opcode!"); 5526 case OP_VREV: 5527 // VREV divides the vector in half and swaps within the half. 5528 if (VT.getVectorElementType() == MVT::i32 || 5529 VT.getVectorElementType() == MVT::f32) 5530 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 5531 // vrev <4 x i16> -> VREV32 5532 if (VT.getVectorElementType() == MVT::i16) 5533 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 5534 // vrev <4 x i8> -> VREV16 5535 assert(VT.getVectorElementType() == MVT::i8); 5536 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 5537 case OP_VDUP0: 5538 case OP_VDUP1: 5539 case OP_VDUP2: 5540 case OP_VDUP3: 5541 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5542 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32)); 5543 case OP_VEXT1: 5544 case OP_VEXT2: 5545 case OP_VEXT3: 5546 return DAG.getNode(ARMISD::VEXT, dl, VT, 5547 OpLHS, OpRHS, 5548 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32)); 5549 case OP_VUZPL: 5550 case OP_VUZPR: 5551 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5552 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 5553 case OP_VZIPL: 5554 case OP_VZIPR: 5555 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5556 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 5557 case OP_VTRNL: 5558 case OP_VTRNR: 5559 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5560 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 5561 } 5562 } 5563 5564 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 5565 ArrayRef<int> ShuffleMask, 5566 SelectionDAG &DAG) { 5567 // Check to see if we can use the VTBL instruction. 5568 SDValue V1 = Op.getOperand(0); 5569 SDValue V2 = Op.getOperand(1); 5570 SDLoc DL(Op); 5571 5572 SmallVector<SDValue, 8> VTBLMask; 5573 for (ArrayRef<int>::iterator 5574 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 5575 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32)); 5576 5577 if (V2.getNode()->getOpcode() == ISD::UNDEF) 5578 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 5579 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask)); 5580 5581 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 5582 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask)); 5583 } 5584 5585 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op, 5586 SelectionDAG &DAG) { 5587 SDLoc DL(Op); 5588 SDValue OpLHS = Op.getOperand(0); 5589 EVT VT = OpLHS.getValueType(); 5590 5591 assert((VT == MVT::v8i16 || VT == MVT::v16i8) && 5592 "Expect an v8i16/v16i8 type"); 5593 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS); 5594 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now, 5595 // extract the first 8 bytes into the top double word and the last 8 bytes 5596 // into the bottom double word. The v8i16 case is similar. 5597 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4; 5598 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS, 5599 DAG.getConstant(ExtractNum, MVT::i32)); 5600 } 5601 5602 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 5603 SDValue V1 = Op.getOperand(0); 5604 SDValue V2 = Op.getOperand(1); 5605 SDLoc dl(Op); 5606 EVT VT = Op.getValueType(); 5607 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 5608 5609 // Convert shuffles that are directly supported on NEON to target-specific 5610 // DAG nodes, instead of keeping them as shuffles and matching them again 5611 // during code selection. This is more efficient and avoids the possibility 5612 // of inconsistencies between legalization and selection. 5613 // FIXME: floating-point vectors should be canonicalized to integer vectors 5614 // of the same time so that they get CSEd properly. 5615 ArrayRef<int> ShuffleMask = SVN->getMask(); 5616 5617 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5618 if (EltSize <= 32) { 5619 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) { 5620 int Lane = SVN->getSplatIndex(); 5621 // If this is undef splat, generate it via "just" vdup, if possible. 5622 if (Lane == -1) Lane = 0; 5623 5624 // Test if V1 is a SCALAR_TO_VECTOR. 5625 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 5626 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 5627 } 5628 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 5629 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 5630 // reaches it). 5631 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 5632 !isa<ConstantSDNode>(V1.getOperand(0))) { 5633 bool IsScalarToVector = true; 5634 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 5635 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) { 5636 IsScalarToVector = false; 5637 break; 5638 } 5639 if (IsScalarToVector) 5640 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 5641 } 5642 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 5643 DAG.getConstant(Lane, MVT::i32)); 5644 } 5645 5646 bool ReverseVEXT; 5647 unsigned Imm; 5648 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 5649 if (ReverseVEXT) 5650 std::swap(V1, V2); 5651 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 5652 DAG.getConstant(Imm, MVT::i32)); 5653 } 5654 5655 if (isVREVMask(ShuffleMask, VT, 64)) 5656 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 5657 if (isVREVMask(ShuffleMask, VT, 32)) 5658 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 5659 if (isVREVMask(ShuffleMask, VT, 16)) 5660 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 5661 5662 if (V2->getOpcode() == ISD::UNDEF && 5663 isSingletonVEXTMask(ShuffleMask, VT, Imm)) { 5664 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1, 5665 DAG.getConstant(Imm, MVT::i32)); 5666 } 5667 5668 // Check for Neon shuffles that modify both input vectors in place. 5669 // If both results are used, i.e., if there are two shuffles with the same 5670 // source operands and with masks corresponding to both results of one of 5671 // these operations, DAG memoization will ensure that a single node is 5672 // used for both shuffles. 5673 unsigned WhichResult; 5674 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 5675 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5676 V1, V2).getValue(WhichResult); 5677 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 5678 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5679 V1, V2).getValue(WhichResult); 5680 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 5681 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5682 V1, V2).getValue(WhichResult); 5683 5684 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5685 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5686 V1, V1).getValue(WhichResult); 5687 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5688 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5689 V1, V1).getValue(WhichResult); 5690 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5691 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5692 V1, V1).getValue(WhichResult); 5693 } 5694 5695 // If the shuffle is not directly supported and it has 4 elements, use 5696 // the PerfectShuffle-generated table to synthesize it from other shuffles. 5697 unsigned NumElts = VT.getVectorNumElements(); 5698 if (NumElts == 4) { 5699 unsigned PFIndexes[4]; 5700 for (unsigned i = 0; i != 4; ++i) { 5701 if (ShuffleMask[i] < 0) 5702 PFIndexes[i] = 8; 5703 else 5704 PFIndexes[i] = ShuffleMask[i]; 5705 } 5706 5707 // Compute the index in the perfect shuffle table. 5708 unsigned PFTableIndex = 5709 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 5710 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 5711 unsigned Cost = (PFEntry >> 30); 5712 5713 if (Cost <= 4) 5714 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 5715 } 5716 5717 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 5718 if (EltSize >= 32) { 5719 // Do the expansion with floating-point types, since that is what the VFP 5720 // registers are defined to use, and since i64 is not legal. 5721 EVT EltVT = EVT::getFloatingPointVT(EltSize); 5722 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 5723 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 5724 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 5725 SmallVector<SDValue, 8> Ops; 5726 for (unsigned i = 0; i < NumElts; ++i) { 5727 if (ShuffleMask[i] < 0) 5728 Ops.push_back(DAG.getUNDEF(EltVT)); 5729 else 5730 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 5731 ShuffleMask[i] < (int)NumElts ? V1 : V2, 5732 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 5733 MVT::i32))); 5734 } 5735 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 5736 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5737 } 5738 5739 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT)) 5740 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG); 5741 5742 if (VT == MVT::v8i8) { 5743 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG); 5744 if (NewOp.getNode()) 5745 return NewOp; 5746 } 5747 5748 return SDValue(); 5749 } 5750 5751 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 5752 // INSERT_VECTOR_ELT is legal only for immediate indexes. 5753 SDValue Lane = Op.getOperand(2); 5754 if (!isa<ConstantSDNode>(Lane)) 5755 return SDValue(); 5756 5757 return Op; 5758 } 5759 5760 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 5761 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 5762 SDValue Lane = Op.getOperand(1); 5763 if (!isa<ConstantSDNode>(Lane)) 5764 return SDValue(); 5765 5766 SDValue Vec = Op.getOperand(0); 5767 if (Op.getValueType() == MVT::i32 && 5768 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) { 5769 SDLoc dl(Op); 5770 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 5771 } 5772 5773 return Op; 5774 } 5775 5776 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 5777 // The only time a CONCAT_VECTORS operation can have legal types is when 5778 // two 64-bit vectors are concatenated to a 128-bit vector. 5779 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 5780 "unexpected CONCAT_VECTORS"); 5781 SDLoc dl(Op); 5782 SDValue Val = DAG.getUNDEF(MVT::v2f64); 5783 SDValue Op0 = Op.getOperand(0); 5784 SDValue Op1 = Op.getOperand(1); 5785 if (Op0.getOpcode() != ISD::UNDEF) 5786 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 5787 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 5788 DAG.getIntPtrConstant(0)); 5789 if (Op1.getOpcode() != ISD::UNDEF) 5790 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 5791 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 5792 DAG.getIntPtrConstant(1)); 5793 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 5794 } 5795 5796 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 5797 /// element has been zero/sign-extended, depending on the isSigned parameter, 5798 /// from an integer type half its size. 5799 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 5800 bool isSigned) { 5801 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 5802 EVT VT = N->getValueType(0); 5803 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 5804 SDNode *BVN = N->getOperand(0).getNode(); 5805 if (BVN->getValueType(0) != MVT::v4i32 || 5806 BVN->getOpcode() != ISD::BUILD_VECTOR) 5807 return false; 5808 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 5809 unsigned HiElt = 1 - LoElt; 5810 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 5811 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 5812 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 5813 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 5814 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 5815 return false; 5816 if (isSigned) { 5817 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 5818 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 5819 return true; 5820 } else { 5821 if (Hi0->isNullValue() && Hi1->isNullValue()) 5822 return true; 5823 } 5824 return false; 5825 } 5826 5827 if (N->getOpcode() != ISD::BUILD_VECTOR) 5828 return false; 5829 5830 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 5831 SDNode *Elt = N->getOperand(i).getNode(); 5832 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 5833 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5834 unsigned HalfSize = EltSize / 2; 5835 if (isSigned) { 5836 if (!isIntN(HalfSize, C->getSExtValue())) 5837 return false; 5838 } else { 5839 if (!isUIntN(HalfSize, C->getZExtValue())) 5840 return false; 5841 } 5842 continue; 5843 } 5844 return false; 5845 } 5846 5847 return true; 5848 } 5849 5850 /// isSignExtended - Check if a node is a vector value that is sign-extended 5851 /// or a constant BUILD_VECTOR with sign-extended elements. 5852 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 5853 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 5854 return true; 5855 if (isExtendedBUILD_VECTOR(N, DAG, true)) 5856 return true; 5857 return false; 5858 } 5859 5860 /// isZeroExtended - Check if a node is a vector value that is zero-extended 5861 /// or a constant BUILD_VECTOR with zero-extended elements. 5862 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 5863 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 5864 return true; 5865 if (isExtendedBUILD_VECTOR(N, DAG, false)) 5866 return true; 5867 return false; 5868 } 5869 5870 static EVT getExtensionTo64Bits(const EVT &OrigVT) { 5871 if (OrigVT.getSizeInBits() >= 64) 5872 return OrigVT; 5873 5874 assert(OrigVT.isSimple() && "Expecting a simple value type"); 5875 5876 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy; 5877 switch (OrigSimpleTy) { 5878 default: llvm_unreachable("Unexpected Vector Type"); 5879 case MVT::v2i8: 5880 case MVT::v2i16: 5881 return MVT::v2i32; 5882 case MVT::v4i8: 5883 return MVT::v4i16; 5884 } 5885 } 5886 5887 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total 5888 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL. 5889 /// We insert the required extension here to get the vector to fill a D register. 5890 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG, 5891 const EVT &OrigTy, 5892 const EVT &ExtTy, 5893 unsigned ExtOpcode) { 5894 // The vector originally had a size of OrigTy. It was then extended to ExtTy. 5895 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than 5896 // 64-bits we need to insert a new extension so that it will be 64-bits. 5897 assert(ExtTy.is128BitVector() && "Unexpected extension size"); 5898 if (OrigTy.getSizeInBits() >= 64) 5899 return N; 5900 5901 // Must extend size to at least 64 bits to be used as an operand for VMULL. 5902 EVT NewVT = getExtensionTo64Bits(OrigTy); 5903 5904 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N); 5905 } 5906 5907 /// SkipLoadExtensionForVMULL - return a load of the original vector size that 5908 /// does not do any sign/zero extension. If the original vector is less 5909 /// than 64 bits, an appropriate extension will be added after the load to 5910 /// reach a total size of 64 bits. We have to add the extension separately 5911 /// because ARM does not have a sign/zero extending load for vectors. 5912 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) { 5913 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT()); 5914 5915 // The load already has the right type. 5916 if (ExtendedTy == LD->getMemoryVT()) 5917 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(), 5918 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(), 5919 LD->isNonTemporal(), LD->isInvariant(), 5920 LD->getAlignment()); 5921 5922 // We need to create a zextload/sextload. We cannot just create a load 5923 // followed by a zext/zext node because LowerMUL is also run during normal 5924 // operation legalization where we can't create illegal types. 5925 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy, 5926 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(), 5927 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(), 5928 LD->isNonTemporal(), LD->getAlignment()); 5929 } 5930 5931 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND, 5932 /// extending load, or BUILD_VECTOR with extended elements, return the 5933 /// unextended value. The unextended vector should be 64 bits so that it can 5934 /// be used as an operand to a VMULL instruction. If the original vector size 5935 /// before extension is less than 64 bits we add a an extension to resize 5936 /// the vector to 64 bits. 5937 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) { 5938 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 5939 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG, 5940 N->getOperand(0)->getValueType(0), 5941 N->getValueType(0), 5942 N->getOpcode()); 5943 5944 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) 5945 return SkipLoadExtensionForVMULL(LD, DAG); 5946 5947 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 5948 // have been legalized as a BITCAST from v4i32. 5949 if (N->getOpcode() == ISD::BITCAST) { 5950 SDNode *BVN = N->getOperand(0).getNode(); 5951 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 5952 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 5953 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 5954 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32, 5955 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2)); 5956 } 5957 // Construct a new BUILD_VECTOR with elements truncated to half the size. 5958 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 5959 EVT VT = N->getValueType(0); 5960 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2; 5961 unsigned NumElts = VT.getVectorNumElements(); 5962 MVT TruncVT = MVT::getIntegerVT(EltSize); 5963 SmallVector<SDValue, 8> Ops; 5964 for (unsigned i = 0; i != NumElts; ++i) { 5965 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 5966 const APInt &CInt = C->getAPIntValue(); 5967 // Element types smaller than 32 bits are not legal, so use i32 elements. 5968 // The values are implicitly truncated so sext vs. zext doesn't matter. 5969 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32)); 5970 } 5971 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), 5972 MVT::getVectorVT(TruncVT, NumElts), Ops); 5973 } 5974 5975 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 5976 unsigned Opcode = N->getOpcode(); 5977 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 5978 SDNode *N0 = N->getOperand(0).getNode(); 5979 SDNode *N1 = N->getOperand(1).getNode(); 5980 return N0->hasOneUse() && N1->hasOneUse() && 5981 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 5982 } 5983 return false; 5984 } 5985 5986 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 5987 unsigned Opcode = N->getOpcode(); 5988 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 5989 SDNode *N0 = N->getOperand(0).getNode(); 5990 SDNode *N1 = N->getOperand(1).getNode(); 5991 return N0->hasOneUse() && N1->hasOneUse() && 5992 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 5993 } 5994 return false; 5995 } 5996 5997 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 5998 // Multiplications are only custom-lowered for 128-bit vectors so that 5999 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 6000 EVT VT = Op.getValueType(); 6001 assert(VT.is128BitVector() && VT.isInteger() && 6002 "unexpected type for custom-lowering ISD::MUL"); 6003 SDNode *N0 = Op.getOperand(0).getNode(); 6004 SDNode *N1 = Op.getOperand(1).getNode(); 6005 unsigned NewOpc = 0; 6006 bool isMLA = false; 6007 bool isN0SExt = isSignExtended(N0, DAG); 6008 bool isN1SExt = isSignExtended(N1, DAG); 6009 if (isN0SExt && isN1SExt) 6010 NewOpc = ARMISD::VMULLs; 6011 else { 6012 bool isN0ZExt = isZeroExtended(N0, DAG); 6013 bool isN1ZExt = isZeroExtended(N1, DAG); 6014 if (isN0ZExt && isN1ZExt) 6015 NewOpc = ARMISD::VMULLu; 6016 else if (isN1SExt || isN1ZExt) { 6017 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 6018 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 6019 if (isN1SExt && isAddSubSExt(N0, DAG)) { 6020 NewOpc = ARMISD::VMULLs; 6021 isMLA = true; 6022 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 6023 NewOpc = ARMISD::VMULLu; 6024 isMLA = true; 6025 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 6026 std::swap(N0, N1); 6027 NewOpc = ARMISD::VMULLu; 6028 isMLA = true; 6029 } 6030 } 6031 6032 if (!NewOpc) { 6033 if (VT == MVT::v2i64) 6034 // Fall through to expand this. It is not legal. 6035 return SDValue(); 6036 else 6037 // Other vector multiplications are legal. 6038 return Op; 6039 } 6040 } 6041 6042 // Legalize to a VMULL instruction. 6043 SDLoc DL(Op); 6044 SDValue Op0; 6045 SDValue Op1 = SkipExtensionForVMULL(N1, DAG); 6046 if (!isMLA) { 6047 Op0 = SkipExtensionForVMULL(N0, DAG); 6048 assert(Op0.getValueType().is64BitVector() && 6049 Op1.getValueType().is64BitVector() && 6050 "unexpected types for extended operands to VMULL"); 6051 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 6052 } 6053 6054 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 6055 // isel lowering to take advantage of no-stall back to back vmul + vmla. 6056 // vmull q0, d4, d6 6057 // vmlal q0, d5, d6 6058 // is faster than 6059 // vaddl q0, d4, d5 6060 // vmovl q1, d6 6061 // vmul q0, q0, q1 6062 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG); 6063 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG); 6064 EVT Op1VT = Op1.getValueType(); 6065 return DAG.getNode(N0->getOpcode(), DL, VT, 6066 DAG.getNode(NewOpc, DL, VT, 6067 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 6068 DAG.getNode(NewOpc, DL, VT, 6069 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 6070 } 6071 6072 static SDValue 6073 LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) { 6074 // Convert to float 6075 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 6076 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 6077 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 6078 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 6079 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 6080 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 6081 // Get reciprocal estimate. 6082 // float4 recip = vrecpeq_f32(yf); 6083 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6084 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y); 6085 // Because char has a smaller range than uchar, we can actually get away 6086 // without any newton steps. This requires that we use a weird bias 6087 // of 0xb000, however (again, this has been exhaustively tested). 6088 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 6089 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 6090 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 6091 Y = DAG.getConstant(0xb000, MVT::i32); 6092 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y); 6093 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 6094 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 6095 // Convert back to short. 6096 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 6097 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 6098 return X; 6099 } 6100 6101 static SDValue 6102 LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) { 6103 SDValue N2; 6104 // Convert to float. 6105 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 6106 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 6107 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 6108 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 6109 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 6110 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 6111 6112 // Use reciprocal estimate and one refinement step. 6113 // float4 recip = vrecpeq_f32(yf); 6114 // recip *= vrecpsq_f32(yf, recip); 6115 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6116 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1); 6117 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6118 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 6119 N1, N2); 6120 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6121 // Because short has a smaller range than ushort, we can actually get away 6122 // with only a single newton step. This requires that we use a weird bias 6123 // of 89, however (again, this has been exhaustively tested). 6124 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 6125 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 6126 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 6127 N1 = DAG.getConstant(0x89, MVT::i32); 6128 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 6129 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 6130 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 6131 // Convert back to integer and return. 6132 // return vmovn_s32(vcvt_s32_f32(result)); 6133 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 6134 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 6135 return N0; 6136 } 6137 6138 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) { 6139 EVT VT = Op.getValueType(); 6140 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 6141 "unexpected type for custom-lowering ISD::SDIV"); 6142 6143 SDLoc dl(Op); 6144 SDValue N0 = Op.getOperand(0); 6145 SDValue N1 = Op.getOperand(1); 6146 SDValue N2, N3; 6147 6148 if (VT == MVT::v8i8) { 6149 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 6150 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 6151 6152 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6153 DAG.getIntPtrConstant(4)); 6154 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6155 DAG.getIntPtrConstant(4)); 6156 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6157 DAG.getIntPtrConstant(0)); 6158 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6159 DAG.getIntPtrConstant(0)); 6160 6161 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 6162 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 6163 6164 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 6165 N0 = LowerCONCAT_VECTORS(N0, DAG); 6166 6167 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 6168 return N0; 6169 } 6170 return LowerSDIV_v4i16(N0, N1, dl, DAG); 6171 } 6172 6173 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) { 6174 EVT VT = Op.getValueType(); 6175 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 6176 "unexpected type for custom-lowering ISD::UDIV"); 6177 6178 SDLoc dl(Op); 6179 SDValue N0 = Op.getOperand(0); 6180 SDValue N1 = Op.getOperand(1); 6181 SDValue N2, N3; 6182 6183 if (VT == MVT::v8i8) { 6184 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 6185 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 6186 6187 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6188 DAG.getIntPtrConstant(4)); 6189 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6190 DAG.getIntPtrConstant(4)); 6191 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6192 DAG.getIntPtrConstant(0)); 6193 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6194 DAG.getIntPtrConstant(0)); 6195 6196 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 6197 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 6198 6199 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 6200 N0 = LowerCONCAT_VECTORS(N0, DAG); 6201 6202 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 6203 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32), 6204 N0); 6205 return N0; 6206 } 6207 6208 // v4i16 sdiv ... Convert to float. 6209 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 6210 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 6211 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 6212 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 6213 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 6214 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 6215 6216 // Use reciprocal estimate and two refinement steps. 6217 // float4 recip = vrecpeq_f32(yf); 6218 // recip *= vrecpsq_f32(yf, recip); 6219 // recip *= vrecpsq_f32(yf, recip); 6220 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6221 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1); 6222 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6223 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 6224 BN1, N2); 6225 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6226 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6227 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32), 6228 BN1, N2); 6229 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6230 // Simply multiplying by the reciprocal estimate can leave us a few ulps 6231 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 6232 // and that it will never cause us to return an answer too large). 6233 // float4 result = as_float4(as_int4(xf*recip) + 2); 6234 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 6235 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 6236 N1 = DAG.getConstant(2, MVT::i32); 6237 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 6238 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 6239 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 6240 // Convert back to integer and return. 6241 // return vmovn_u32(vcvt_s32_f32(result)); 6242 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 6243 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 6244 return N0; 6245 } 6246 6247 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { 6248 EVT VT = Op.getNode()->getValueType(0); 6249 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 6250 6251 unsigned Opc; 6252 bool ExtraOp = false; 6253 switch (Op.getOpcode()) { 6254 default: llvm_unreachable("Invalid code"); 6255 case ISD::ADDC: Opc = ARMISD::ADDC; break; 6256 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break; 6257 case ISD::SUBC: Opc = ARMISD::SUBC; break; 6258 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break; 6259 } 6260 6261 if (!ExtraOp) 6262 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 6263 Op.getOperand(1)); 6264 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 6265 Op.getOperand(1), Op.getOperand(2)); 6266 } 6267 6268 SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const { 6269 assert(Subtarget->isTargetDarwin()); 6270 6271 // For iOS, we want to call an alternative entry point: __sincos_stret, 6272 // return values are passed via sret. 6273 SDLoc dl(Op); 6274 SDValue Arg = Op.getOperand(0); 6275 EVT ArgVT = Arg.getValueType(); 6276 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 6277 6278 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 6279 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 6280 6281 // Pair of floats / doubles used to pass the result. 6282 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL); 6283 6284 // Create stack object for sret. 6285 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy); 6286 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy); 6287 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false); 6288 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); 6289 6290 ArgListTy Args; 6291 ArgListEntry Entry; 6292 6293 Entry.Node = SRet; 6294 Entry.Ty = RetTy->getPointerTo(); 6295 Entry.isSExt = false; 6296 Entry.isZExt = false; 6297 Entry.isSRet = true; 6298 Args.push_back(Entry); 6299 6300 Entry.Node = Arg; 6301 Entry.Ty = ArgTy; 6302 Entry.isSExt = false; 6303 Entry.isZExt = false; 6304 Args.push_back(Entry); 6305 6306 const char *LibcallName = (ArgVT == MVT::f64) 6307 ? "__sincos_stret" : "__sincosf_stret"; 6308 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy()); 6309 6310 TargetLowering::CallLoweringInfo CLI(DAG); 6311 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode()) 6312 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee, 6313 std::move(Args), 0) 6314 .setDiscardResult(); 6315 6316 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 6317 6318 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet, 6319 MachinePointerInfo(), false, false, false, 0); 6320 6321 // Address of cos field. 6322 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet, 6323 DAG.getIntPtrConstant(ArgVT.getStoreSize())); 6324 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, 6325 MachinePointerInfo(), false, false, false, 0); 6326 6327 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT); 6328 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, 6329 LoadSin.getValue(0), LoadCos.getValue(0)); 6330 } 6331 6332 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 6333 // Monotonic load/store is legal for all targets 6334 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic) 6335 return Op; 6336 6337 // Acquire/Release load/store is not legal for targets without a 6338 // dmb or equivalent available. 6339 return SDValue(); 6340 } 6341 6342 static void ReplaceREADCYCLECOUNTER(SDNode *N, 6343 SmallVectorImpl<SDValue> &Results, 6344 SelectionDAG &DAG, 6345 const ARMSubtarget *Subtarget) { 6346 SDLoc DL(N); 6347 SDValue Cycles32, OutChain; 6348 6349 if (Subtarget->hasPerfMon()) { 6350 // Under Power Management extensions, the cycle-count is: 6351 // mrc p15, #0, <Rt>, c9, c13, #0 6352 SDValue Ops[] = { N->getOperand(0), // Chain 6353 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32), 6354 DAG.getConstant(15, MVT::i32), 6355 DAG.getConstant(0, MVT::i32), 6356 DAG.getConstant(9, MVT::i32), 6357 DAG.getConstant(13, MVT::i32), 6358 DAG.getConstant(0, MVT::i32) 6359 }; 6360 6361 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 6362 DAG.getVTList(MVT::i32, MVT::Other), Ops); 6363 OutChain = Cycles32.getValue(1); 6364 } else { 6365 // Intrinsic is defined to return 0 on unsupported platforms. Technically 6366 // there are older ARM CPUs that have implementation-specific ways of 6367 // obtaining this information (FIXME!). 6368 Cycles32 = DAG.getConstant(0, MVT::i32); 6369 OutChain = DAG.getEntryNode(); 6370 } 6371 6372 6373 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, 6374 Cycles32, DAG.getConstant(0, MVT::i32)); 6375 Results.push_back(Cycles64); 6376 Results.push_back(OutChain); 6377 } 6378 6379 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 6380 switch (Op.getOpcode()) { 6381 default: llvm_unreachable("Don't know how to custom lower this!"); 6382 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 6383 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 6384 case ISD::GlobalAddress: 6385 switch (Subtarget->getTargetTriple().getObjectFormat()) { 6386 default: llvm_unreachable("unknown object format"); 6387 case Triple::COFF: 6388 return LowerGlobalAddressWindows(Op, DAG); 6389 case Triple::ELF: 6390 return LowerGlobalAddressELF(Op, DAG); 6391 case Triple::MachO: 6392 return LowerGlobalAddressDarwin(Op, DAG); 6393 } 6394 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 6395 case ISD::SELECT: return LowerSELECT(Op, DAG); 6396 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 6397 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 6398 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 6399 case ISD::VASTART: return LowerVASTART(Op, DAG); 6400 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 6401 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 6402 case ISD::SINT_TO_FP: 6403 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 6404 case ISD::FP_TO_SINT: 6405 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 6406 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 6407 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 6408 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 6409 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG); 6410 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 6411 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 6412 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 6413 Subtarget); 6414 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG); 6415 case ISD::SHL: 6416 case ISD::SRL: 6417 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 6418 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 6419 case ISD::SRL_PARTS: 6420 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 6421 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 6422 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget); 6423 case ISD::SETCC: return LowerVSETCC(Op, DAG); 6424 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget); 6425 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 6426 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 6427 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 6428 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 6429 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 6430 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 6431 case ISD::MUL: return LowerMUL(Op, DAG); 6432 case ISD::SDIV: return LowerSDIV(Op, DAG); 6433 case ISD::UDIV: return LowerUDIV(Op, DAG); 6434 case ISD::ADDC: 6435 case ISD::ADDE: 6436 case ISD::SUBC: 6437 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); 6438 case ISD::SADDO: 6439 case ISD::UADDO: 6440 case ISD::SSUBO: 6441 case ISD::USUBO: 6442 return LowerXALUO(Op, DAG); 6443 case ISD::ATOMIC_LOAD: 6444 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 6445 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG); 6446 case ISD::SDIVREM: 6447 case ISD::UDIVREM: return LowerDivRem(Op, DAG); 6448 case ISD::DYNAMIC_STACKALLOC: 6449 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment()) 6450 return LowerDYNAMIC_STACKALLOC(Op, DAG); 6451 llvm_unreachable("Don't know how to custom lower this!"); 6452 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG); 6453 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG); 6454 } 6455 } 6456 6457 /// ReplaceNodeResults - Replace the results of node with an illegal result 6458 /// type with new values built out of custom code. 6459 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 6460 SmallVectorImpl<SDValue>&Results, 6461 SelectionDAG &DAG) const { 6462 SDValue Res; 6463 switch (N->getOpcode()) { 6464 default: 6465 llvm_unreachable("Don't know how to custom expand this!"); 6466 case ISD::BITCAST: 6467 Res = ExpandBITCAST(N, DAG); 6468 break; 6469 case ISD::SRL: 6470 case ISD::SRA: 6471 Res = Expand64BitShift(N, DAG, Subtarget); 6472 break; 6473 case ISD::READCYCLECOUNTER: 6474 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget); 6475 return; 6476 } 6477 if (Res.getNode()) 6478 Results.push_back(Res); 6479 } 6480 6481 //===----------------------------------------------------------------------===// 6482 // ARM Scheduler Hooks 6483 //===----------------------------------------------------------------------===// 6484 6485 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 6486 /// registers the function context. 6487 void ARMTargetLowering:: 6488 SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB, 6489 MachineBasicBlock *DispatchBB, int FI) const { 6490 const TargetInstrInfo *TII = 6491 getTargetMachine().getSubtargetImpl()->getInstrInfo(); 6492 DebugLoc dl = MI->getDebugLoc(); 6493 MachineFunction *MF = MBB->getParent(); 6494 MachineRegisterInfo *MRI = &MF->getRegInfo(); 6495 MachineConstantPool *MCP = MF->getConstantPool(); 6496 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 6497 const Function *F = MF->getFunction(); 6498 6499 bool isThumb = Subtarget->isThumb(); 6500 bool isThumb2 = Subtarget->isThumb2(); 6501 6502 unsigned PCLabelId = AFI->createPICLabelUId(); 6503 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 6504 ARMConstantPoolValue *CPV = 6505 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj); 6506 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 6507 6508 const TargetRegisterClass *TRC = isThumb ? 6509 (const TargetRegisterClass*)&ARM::tGPRRegClass : 6510 (const TargetRegisterClass*)&ARM::GPRRegClass; 6511 6512 // Grab constant pool and fixed stack memory operands. 6513 MachineMemOperand *CPMMO = 6514 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(), 6515 MachineMemOperand::MOLoad, 4, 4); 6516 6517 MachineMemOperand *FIMMOSt = 6518 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 6519 MachineMemOperand::MOStore, 4, 4); 6520 6521 // Load the address of the dispatch MBB into the jump buffer. 6522 if (isThumb2) { 6523 // Incoming value: jbuf 6524 // ldr.n r5, LCPI1_1 6525 // orr r5, r5, #1 6526 // add r5, pc 6527 // str r5, [$jbuf, #+4] ; &jbuf[1] 6528 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6529 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 6530 .addConstantPoolIndex(CPI) 6531 .addMemOperand(CPMMO)); 6532 // Set the low bit because of thumb mode. 6533 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6534 AddDefaultCC( 6535 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 6536 .addReg(NewVReg1, RegState::Kill) 6537 .addImm(0x01))); 6538 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6539 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 6540 .addReg(NewVReg2, RegState::Kill) 6541 .addImm(PCLabelId); 6542 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 6543 .addReg(NewVReg3, RegState::Kill) 6544 .addFrameIndex(FI) 6545 .addImm(36) // &jbuf[1] :: pc 6546 .addMemOperand(FIMMOSt)); 6547 } else if (isThumb) { 6548 // Incoming value: jbuf 6549 // ldr.n r1, LCPI1_4 6550 // add r1, pc 6551 // mov r2, #1 6552 // orrs r1, r2 6553 // add r2, $jbuf, #+4 ; &jbuf[1] 6554 // str r1, [r2] 6555 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6556 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 6557 .addConstantPoolIndex(CPI) 6558 .addMemOperand(CPMMO)); 6559 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6560 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 6561 .addReg(NewVReg1, RegState::Kill) 6562 .addImm(PCLabelId); 6563 // Set the low bit because of thumb mode. 6564 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6565 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 6566 .addReg(ARM::CPSR, RegState::Define) 6567 .addImm(1)); 6568 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6569 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 6570 .addReg(ARM::CPSR, RegState::Define) 6571 .addReg(NewVReg2, RegState::Kill) 6572 .addReg(NewVReg3, RegState::Kill)); 6573 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6574 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5) 6575 .addFrameIndex(FI) 6576 .addImm(36)); // &jbuf[1] :: pc 6577 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 6578 .addReg(NewVReg4, RegState::Kill) 6579 .addReg(NewVReg5, RegState::Kill) 6580 .addImm(0) 6581 .addMemOperand(FIMMOSt)); 6582 } else { 6583 // Incoming value: jbuf 6584 // ldr r1, LCPI1_1 6585 // add r1, pc, r1 6586 // str r1, [$jbuf, #+4] ; &jbuf[1] 6587 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6588 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 6589 .addConstantPoolIndex(CPI) 6590 .addImm(0) 6591 .addMemOperand(CPMMO)); 6592 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6593 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 6594 .addReg(NewVReg1, RegState::Kill) 6595 .addImm(PCLabelId)); 6596 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 6597 .addReg(NewVReg2, RegState::Kill) 6598 .addFrameIndex(FI) 6599 .addImm(36) // &jbuf[1] :: pc 6600 .addMemOperand(FIMMOSt)); 6601 } 6602 } 6603 6604 MachineBasicBlock *ARMTargetLowering:: 6605 EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const { 6606 const TargetInstrInfo *TII = 6607 getTargetMachine().getSubtargetImpl()->getInstrInfo(); 6608 DebugLoc dl = MI->getDebugLoc(); 6609 MachineFunction *MF = MBB->getParent(); 6610 MachineRegisterInfo *MRI = &MF->getRegInfo(); 6611 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 6612 MachineFrameInfo *MFI = MF->getFrameInfo(); 6613 int FI = MFI->getFunctionContextIndex(); 6614 6615 const TargetRegisterClass *TRC = Subtarget->isThumb() ? 6616 (const TargetRegisterClass*)&ARM::tGPRRegClass : 6617 (const TargetRegisterClass*)&ARM::GPRnopcRegClass; 6618 6619 // Get a mapping of the call site numbers to all of the landing pads they're 6620 // associated with. 6621 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad; 6622 unsigned MaxCSNum = 0; 6623 MachineModuleInfo &MMI = MF->getMMI(); 6624 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; 6625 ++BB) { 6626 if (!BB->isLandingPad()) continue; 6627 6628 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 6629 // pad. 6630 for (MachineBasicBlock::iterator 6631 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 6632 if (!II->isEHLabel()) continue; 6633 6634 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 6635 if (!MMI.hasCallSiteLandingPad(Sym)) continue; 6636 6637 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym); 6638 for (SmallVectorImpl<unsigned>::iterator 6639 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 6640 CSI != CSE; ++CSI) { 6641 CallSiteNumToLPad[*CSI].push_back(BB); 6642 MaxCSNum = std::max(MaxCSNum, *CSI); 6643 } 6644 break; 6645 } 6646 } 6647 6648 // Get an ordered list of the machine basic blocks for the jump table. 6649 std::vector<MachineBasicBlock*> LPadList; 6650 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs; 6651 LPadList.reserve(CallSiteNumToLPad.size()); 6652 for (unsigned I = 1; I <= MaxCSNum; ++I) { 6653 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 6654 for (SmallVectorImpl<MachineBasicBlock*>::iterator 6655 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 6656 LPadList.push_back(*II); 6657 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 6658 } 6659 } 6660 6661 assert(!LPadList.empty() && 6662 "No landing pad destinations for the dispatch jump table!"); 6663 6664 // Create the jump table and associated information. 6665 MachineJumpTableInfo *JTI = 6666 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 6667 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 6668 unsigned UId = AFI->createJumpTableUId(); 6669 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 6670 6671 // Create the MBBs for the dispatch code. 6672 6673 // Shove the dispatch's address into the return slot in the function context. 6674 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 6675 DispatchBB->setIsLandingPad(); 6676 6677 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 6678 unsigned trap_opcode; 6679 if (Subtarget->isThumb()) 6680 trap_opcode = ARM::tTRAP; 6681 else 6682 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP; 6683 6684 BuildMI(TrapBB, dl, TII->get(trap_opcode)); 6685 DispatchBB->addSuccessor(TrapBB); 6686 6687 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 6688 DispatchBB->addSuccessor(DispContBB); 6689 6690 // Insert and MBBs. 6691 MF->insert(MF->end(), DispatchBB); 6692 MF->insert(MF->end(), DispContBB); 6693 MF->insert(MF->end(), TrapBB); 6694 6695 // Insert code into the entry block that creates and registers the function 6696 // context. 6697 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 6698 6699 MachineMemOperand *FIMMOLd = 6700 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 6701 MachineMemOperand::MOLoad | 6702 MachineMemOperand::MOVolatile, 4, 4); 6703 6704 MachineInstrBuilder MIB; 6705 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup)); 6706 6707 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 6708 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 6709 6710 // Add a register mask with no preserved registers. This results in all 6711 // registers being marked as clobbered. 6712 MIB.addRegMask(RI.getNoPreservedMask()); 6713 6714 unsigned NumLPads = LPadList.size(); 6715 if (Subtarget->isThumb2()) { 6716 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6717 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 6718 .addFrameIndex(FI) 6719 .addImm(4) 6720 .addMemOperand(FIMMOLd)); 6721 6722 if (NumLPads < 256) { 6723 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 6724 .addReg(NewVReg1) 6725 .addImm(LPadList.size())); 6726 } else { 6727 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6728 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 6729 .addImm(NumLPads & 0xFFFF)); 6730 6731 unsigned VReg2 = VReg1; 6732 if ((NumLPads & 0xFFFF0000) != 0) { 6733 VReg2 = MRI->createVirtualRegister(TRC); 6734 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 6735 .addReg(VReg1) 6736 .addImm(NumLPads >> 16)); 6737 } 6738 6739 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 6740 .addReg(NewVReg1) 6741 .addReg(VReg2)); 6742 } 6743 6744 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 6745 .addMBB(TrapBB) 6746 .addImm(ARMCC::HI) 6747 .addReg(ARM::CPSR); 6748 6749 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6750 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3) 6751 .addJumpTableIndex(MJTI) 6752 .addImm(UId)); 6753 6754 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6755 AddDefaultCC( 6756 AddDefaultPred( 6757 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 6758 .addReg(NewVReg3, RegState::Kill) 6759 .addReg(NewVReg1) 6760 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 6761 6762 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 6763 .addReg(NewVReg4, RegState::Kill) 6764 .addReg(NewVReg1) 6765 .addJumpTableIndex(MJTI) 6766 .addImm(UId); 6767 } else if (Subtarget->isThumb()) { 6768 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6769 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 6770 .addFrameIndex(FI) 6771 .addImm(1) 6772 .addMemOperand(FIMMOLd)); 6773 6774 if (NumLPads < 256) { 6775 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 6776 .addReg(NewVReg1) 6777 .addImm(NumLPads)); 6778 } else { 6779 MachineConstantPool *ConstantPool = MF->getConstantPool(); 6780 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 6781 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 6782 6783 // MachineConstantPool wants an explicit alignment. 6784 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 6785 if (Align == 0) 6786 Align = getDataLayout()->getTypeAllocSize(C->getType()); 6787 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 6788 6789 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6790 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 6791 .addReg(VReg1, RegState::Define) 6792 .addConstantPoolIndex(Idx)); 6793 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 6794 .addReg(NewVReg1) 6795 .addReg(VReg1)); 6796 } 6797 6798 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 6799 .addMBB(TrapBB) 6800 .addImm(ARMCC::HI) 6801 .addReg(ARM::CPSR); 6802 6803 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6804 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 6805 .addReg(ARM::CPSR, RegState::Define) 6806 .addReg(NewVReg1) 6807 .addImm(2)); 6808 6809 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6810 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 6811 .addJumpTableIndex(MJTI) 6812 .addImm(UId)); 6813 6814 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6815 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 6816 .addReg(ARM::CPSR, RegState::Define) 6817 .addReg(NewVReg2, RegState::Kill) 6818 .addReg(NewVReg3)); 6819 6820 MachineMemOperand *JTMMOLd = 6821 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 6822 MachineMemOperand::MOLoad, 4, 4); 6823 6824 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6825 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 6826 .addReg(NewVReg4, RegState::Kill) 6827 .addImm(0) 6828 .addMemOperand(JTMMOLd)); 6829 6830 unsigned NewVReg6 = NewVReg5; 6831 if (RelocM == Reloc::PIC_) { 6832 NewVReg6 = MRI->createVirtualRegister(TRC); 6833 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 6834 .addReg(ARM::CPSR, RegState::Define) 6835 .addReg(NewVReg5, RegState::Kill) 6836 .addReg(NewVReg3)); 6837 } 6838 6839 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 6840 .addReg(NewVReg6, RegState::Kill) 6841 .addJumpTableIndex(MJTI) 6842 .addImm(UId); 6843 } else { 6844 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6845 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 6846 .addFrameIndex(FI) 6847 .addImm(4) 6848 .addMemOperand(FIMMOLd)); 6849 6850 if (NumLPads < 256) { 6851 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 6852 .addReg(NewVReg1) 6853 .addImm(NumLPads)); 6854 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 6855 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6856 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 6857 .addImm(NumLPads & 0xFFFF)); 6858 6859 unsigned VReg2 = VReg1; 6860 if ((NumLPads & 0xFFFF0000) != 0) { 6861 VReg2 = MRI->createVirtualRegister(TRC); 6862 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 6863 .addReg(VReg1) 6864 .addImm(NumLPads >> 16)); 6865 } 6866 6867 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 6868 .addReg(NewVReg1) 6869 .addReg(VReg2)); 6870 } else { 6871 MachineConstantPool *ConstantPool = MF->getConstantPool(); 6872 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 6873 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 6874 6875 // MachineConstantPool wants an explicit alignment. 6876 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 6877 if (Align == 0) 6878 Align = getDataLayout()->getTypeAllocSize(C->getType()); 6879 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 6880 6881 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6882 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 6883 .addReg(VReg1, RegState::Define) 6884 .addConstantPoolIndex(Idx) 6885 .addImm(0)); 6886 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 6887 .addReg(NewVReg1) 6888 .addReg(VReg1, RegState::Kill)); 6889 } 6890 6891 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 6892 .addMBB(TrapBB) 6893 .addImm(ARMCC::HI) 6894 .addReg(ARM::CPSR); 6895 6896 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6897 AddDefaultCC( 6898 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 6899 .addReg(NewVReg1) 6900 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 6901 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6902 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 6903 .addJumpTableIndex(MJTI) 6904 .addImm(UId)); 6905 6906 MachineMemOperand *JTMMOLd = 6907 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 6908 MachineMemOperand::MOLoad, 4, 4); 6909 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6910 AddDefaultPred( 6911 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 6912 .addReg(NewVReg3, RegState::Kill) 6913 .addReg(NewVReg4) 6914 .addImm(0) 6915 .addMemOperand(JTMMOLd)); 6916 6917 if (RelocM == Reloc::PIC_) { 6918 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 6919 .addReg(NewVReg5, RegState::Kill) 6920 .addReg(NewVReg4) 6921 .addJumpTableIndex(MJTI) 6922 .addImm(UId); 6923 } else { 6924 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr)) 6925 .addReg(NewVReg5, RegState::Kill) 6926 .addJumpTableIndex(MJTI) 6927 .addImm(UId); 6928 } 6929 } 6930 6931 // Add the jump table entries as successors to the MBB. 6932 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs; 6933 for (std::vector<MachineBasicBlock*>::iterator 6934 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 6935 MachineBasicBlock *CurMBB = *I; 6936 if (SeenMBBs.insert(CurMBB)) 6937 DispContBB->addSuccessor(CurMBB); 6938 } 6939 6940 // N.B. the order the invoke BBs are processed in doesn't matter here. 6941 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF); 6942 SmallVector<MachineBasicBlock*, 64> MBBLPads; 6943 for (MachineBasicBlock *BB : InvokeBBs) { 6944 6945 // Remove the landing pad successor from the invoke block and replace it 6946 // with the new dispatch block. 6947 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 6948 BB->succ_end()); 6949 while (!Successors.empty()) { 6950 MachineBasicBlock *SMBB = Successors.pop_back_val(); 6951 if (SMBB->isLandingPad()) { 6952 BB->removeSuccessor(SMBB); 6953 MBBLPads.push_back(SMBB); 6954 } 6955 } 6956 6957 BB->addSuccessor(DispatchBB); 6958 6959 // Find the invoke call and mark all of the callee-saved registers as 6960 // 'implicit defined' so that they're spilled. This prevents code from 6961 // moving instructions to before the EH block, where they will never be 6962 // executed. 6963 for (MachineBasicBlock::reverse_iterator 6964 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 6965 if (!II->isCall()) continue; 6966 6967 DenseMap<unsigned, bool> DefRegs; 6968 for (MachineInstr::mop_iterator 6969 OI = II->operands_begin(), OE = II->operands_end(); 6970 OI != OE; ++OI) { 6971 if (!OI->isReg()) continue; 6972 DefRegs[OI->getReg()] = true; 6973 } 6974 6975 MachineInstrBuilder MIB(*MF, &*II); 6976 6977 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 6978 unsigned Reg = SavedRegs[i]; 6979 if (Subtarget->isThumb2() && 6980 !ARM::tGPRRegClass.contains(Reg) && 6981 !ARM::hGPRRegClass.contains(Reg)) 6982 continue; 6983 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg)) 6984 continue; 6985 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg)) 6986 continue; 6987 if (!DefRegs[Reg]) 6988 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 6989 } 6990 6991 break; 6992 } 6993 } 6994 6995 // Mark all former landing pads as non-landing pads. The dispatch is the only 6996 // landing pad now. 6997 for (SmallVectorImpl<MachineBasicBlock*>::iterator 6998 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 6999 (*I)->setIsLandingPad(false); 7000 7001 // The instruction is gone now. 7002 MI->eraseFromParent(); 7003 7004 return MBB; 7005 } 7006 7007 static 7008 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 7009 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 7010 E = MBB->succ_end(); I != E; ++I) 7011 if (*I != Succ) 7012 return *I; 7013 llvm_unreachable("Expecting a BB with two successors!"); 7014 } 7015 7016 /// Return the load opcode for a given load size. If load size >= 8, 7017 /// neon opcode will be returned. 7018 static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) { 7019 if (LdSize >= 8) 7020 return LdSize == 16 ? ARM::VLD1q32wb_fixed 7021 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0; 7022 if (IsThumb1) 7023 return LdSize == 4 ? ARM::tLDRi 7024 : LdSize == 2 ? ARM::tLDRHi 7025 : LdSize == 1 ? ARM::tLDRBi : 0; 7026 if (IsThumb2) 7027 return LdSize == 4 ? ARM::t2LDR_POST 7028 : LdSize == 2 ? ARM::t2LDRH_POST 7029 : LdSize == 1 ? ARM::t2LDRB_POST : 0; 7030 return LdSize == 4 ? ARM::LDR_POST_IMM 7031 : LdSize == 2 ? ARM::LDRH_POST 7032 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0; 7033 } 7034 7035 /// Return the store opcode for a given store size. If store size >= 8, 7036 /// neon opcode will be returned. 7037 static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) { 7038 if (StSize >= 8) 7039 return StSize == 16 ? ARM::VST1q32wb_fixed 7040 : StSize == 8 ? ARM::VST1d32wb_fixed : 0; 7041 if (IsThumb1) 7042 return StSize == 4 ? ARM::tSTRi 7043 : StSize == 2 ? ARM::tSTRHi 7044 : StSize == 1 ? ARM::tSTRBi : 0; 7045 if (IsThumb2) 7046 return StSize == 4 ? ARM::t2STR_POST 7047 : StSize == 2 ? ARM::t2STRH_POST 7048 : StSize == 1 ? ARM::t2STRB_POST : 0; 7049 return StSize == 4 ? ARM::STR_POST_IMM 7050 : StSize == 2 ? ARM::STRH_POST 7051 : StSize == 1 ? ARM::STRB_POST_IMM : 0; 7052 } 7053 7054 /// Emit a post-increment load operation with given size. The instructions 7055 /// will be added to BB at Pos. 7056 static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos, 7057 const TargetInstrInfo *TII, DebugLoc dl, 7058 unsigned LdSize, unsigned Data, unsigned AddrIn, 7059 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 7060 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2); 7061 assert(LdOpc != 0 && "Should have a load opcode"); 7062 if (LdSize >= 8) { 7063 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7064 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7065 .addImm(0)); 7066 } else if (IsThumb1) { 7067 // load + update AddrIn 7068 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7069 .addReg(AddrIn).addImm(0)); 7070 MachineInstrBuilder MIB = 7071 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut); 7072 MIB = AddDefaultT1CC(MIB); 7073 MIB.addReg(AddrIn).addImm(LdSize); 7074 AddDefaultPred(MIB); 7075 } else if (IsThumb2) { 7076 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7077 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7078 .addImm(LdSize)); 7079 } else { // arm 7080 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7081 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7082 .addReg(0).addImm(LdSize)); 7083 } 7084 } 7085 7086 /// Emit a post-increment store operation with given size. The instructions 7087 /// will be added to BB at Pos. 7088 static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos, 7089 const TargetInstrInfo *TII, DebugLoc dl, 7090 unsigned StSize, unsigned Data, unsigned AddrIn, 7091 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 7092 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2); 7093 assert(StOpc != 0 && "Should have a store opcode"); 7094 if (StSize >= 8) { 7095 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7096 .addReg(AddrIn).addImm(0).addReg(Data)); 7097 } else if (IsThumb1) { 7098 // store + update AddrIn 7099 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data) 7100 .addReg(AddrIn).addImm(0)); 7101 MachineInstrBuilder MIB = 7102 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut); 7103 MIB = AddDefaultT1CC(MIB); 7104 MIB.addReg(AddrIn).addImm(StSize); 7105 AddDefaultPred(MIB); 7106 } else if (IsThumb2) { 7107 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7108 .addReg(Data).addReg(AddrIn).addImm(StSize)); 7109 } else { // arm 7110 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7111 .addReg(Data).addReg(AddrIn).addReg(0) 7112 .addImm(StSize)); 7113 } 7114 } 7115 7116 MachineBasicBlock * 7117 ARMTargetLowering::EmitStructByval(MachineInstr *MI, 7118 MachineBasicBlock *BB) const { 7119 // This pseudo instruction has 3 operands: dst, src, size 7120 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold(). 7121 // Otherwise, we will generate unrolled scalar copies. 7122 const TargetInstrInfo *TII = 7123 getTargetMachine().getSubtargetImpl()->getInstrInfo(); 7124 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7125 MachineFunction::iterator It = BB; 7126 ++It; 7127 7128 unsigned dest = MI->getOperand(0).getReg(); 7129 unsigned src = MI->getOperand(1).getReg(); 7130 unsigned SizeVal = MI->getOperand(2).getImm(); 7131 unsigned Align = MI->getOperand(3).getImm(); 7132 DebugLoc dl = MI->getDebugLoc(); 7133 7134 MachineFunction *MF = BB->getParent(); 7135 MachineRegisterInfo &MRI = MF->getRegInfo(); 7136 unsigned UnitSize = 0; 7137 const TargetRegisterClass *TRC = nullptr; 7138 const TargetRegisterClass *VecTRC = nullptr; 7139 7140 bool IsThumb1 = Subtarget->isThumb1Only(); 7141 bool IsThumb2 = Subtarget->isThumb2(); 7142 7143 if (Align & 1) { 7144 UnitSize = 1; 7145 } else if (Align & 2) { 7146 UnitSize = 2; 7147 } else { 7148 // Check whether we can use NEON instructions. 7149 if (!MF->getFunction()->getAttributes(). 7150 hasAttribute(AttributeSet::FunctionIndex, 7151 Attribute::NoImplicitFloat) && 7152 Subtarget->hasNEON()) { 7153 if ((Align % 16 == 0) && SizeVal >= 16) 7154 UnitSize = 16; 7155 else if ((Align % 8 == 0) && SizeVal >= 8) 7156 UnitSize = 8; 7157 } 7158 // Can't use NEON instructions. 7159 if (UnitSize == 0) 7160 UnitSize = 4; 7161 } 7162 7163 // Select the correct opcode and register class for unit size load/store 7164 bool IsNeon = UnitSize >= 8; 7165 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass 7166 : (const TargetRegisterClass *)&ARM::GPRRegClass; 7167 if (IsNeon) 7168 VecTRC = UnitSize == 16 7169 ? (const TargetRegisterClass *)&ARM::DPairRegClass 7170 : UnitSize == 8 7171 ? (const TargetRegisterClass *)&ARM::DPRRegClass 7172 : nullptr; 7173 7174 unsigned BytesLeft = SizeVal % UnitSize; 7175 unsigned LoopSize = SizeVal - BytesLeft; 7176 7177 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) { 7178 // Use LDR and STR to copy. 7179 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize) 7180 // [destOut] = STR_POST(scratch, destIn, UnitSize) 7181 unsigned srcIn = src; 7182 unsigned destIn = dest; 7183 for (unsigned i = 0; i < LoopSize; i+=UnitSize) { 7184 unsigned srcOut = MRI.createVirtualRegister(TRC); 7185 unsigned destOut = MRI.createVirtualRegister(TRC); 7186 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 7187 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut, 7188 IsThumb1, IsThumb2); 7189 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut, 7190 IsThumb1, IsThumb2); 7191 srcIn = srcOut; 7192 destIn = destOut; 7193 } 7194 7195 // Handle the leftover bytes with LDRB and STRB. 7196 // [scratch, srcOut] = LDRB_POST(srcIn, 1) 7197 // [destOut] = STRB_POST(scratch, destIn, 1) 7198 for (unsigned i = 0; i < BytesLeft; i++) { 7199 unsigned srcOut = MRI.createVirtualRegister(TRC); 7200 unsigned destOut = MRI.createVirtualRegister(TRC); 7201 unsigned scratch = MRI.createVirtualRegister(TRC); 7202 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut, 7203 IsThumb1, IsThumb2); 7204 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut, 7205 IsThumb1, IsThumb2); 7206 srcIn = srcOut; 7207 destIn = destOut; 7208 } 7209 MI->eraseFromParent(); // The instruction is gone now. 7210 return BB; 7211 } 7212 7213 // Expand the pseudo op to a loop. 7214 // thisMBB: 7215 // ... 7216 // movw varEnd, # --> with thumb2 7217 // movt varEnd, # 7218 // ldrcp varEnd, idx --> without thumb2 7219 // fallthrough --> loopMBB 7220 // loopMBB: 7221 // PHI varPhi, varEnd, varLoop 7222 // PHI srcPhi, src, srcLoop 7223 // PHI destPhi, dst, destLoop 7224 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 7225 // [destLoop] = STR_POST(scratch, destPhi, UnitSize) 7226 // subs varLoop, varPhi, #UnitSize 7227 // bne loopMBB 7228 // fallthrough --> exitMBB 7229 // exitMBB: 7230 // epilogue to handle left-over bytes 7231 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 7232 // [destOut] = STRB_POST(scratch, destLoop, 1) 7233 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 7234 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 7235 MF->insert(It, loopMBB); 7236 MF->insert(It, exitMBB); 7237 7238 // Transfer the remainder of BB and its successor edges to exitMBB. 7239 exitMBB->splice(exitMBB->begin(), BB, 7240 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7241 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 7242 7243 // Load an immediate to varEnd. 7244 unsigned varEnd = MRI.createVirtualRegister(TRC); 7245 if (IsThumb2) { 7246 unsigned Vtmp = varEnd; 7247 if ((LoopSize & 0xFFFF0000) != 0) 7248 Vtmp = MRI.createVirtualRegister(TRC); 7249 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp) 7250 .addImm(LoopSize & 0xFFFF)); 7251 7252 if ((LoopSize & 0xFFFF0000) != 0) 7253 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd) 7254 .addReg(Vtmp).addImm(LoopSize >> 16)); 7255 } else { 7256 MachineConstantPool *ConstantPool = MF->getConstantPool(); 7257 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 7258 const Constant *C = ConstantInt::get(Int32Ty, LoopSize); 7259 7260 // MachineConstantPool wants an explicit alignment. 7261 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 7262 if (Align == 0) 7263 Align = getDataLayout()->getTypeAllocSize(C->getType()); 7264 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 7265 7266 if (IsThumb1) 7267 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg( 7268 varEnd, RegState::Define).addConstantPoolIndex(Idx)); 7269 else 7270 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg( 7271 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0)); 7272 } 7273 BB->addSuccessor(loopMBB); 7274 7275 // Generate the loop body: 7276 // varPhi = PHI(varLoop, varEnd) 7277 // srcPhi = PHI(srcLoop, src) 7278 // destPhi = PHI(destLoop, dst) 7279 MachineBasicBlock *entryBB = BB; 7280 BB = loopMBB; 7281 unsigned varLoop = MRI.createVirtualRegister(TRC); 7282 unsigned varPhi = MRI.createVirtualRegister(TRC); 7283 unsigned srcLoop = MRI.createVirtualRegister(TRC); 7284 unsigned srcPhi = MRI.createVirtualRegister(TRC); 7285 unsigned destLoop = MRI.createVirtualRegister(TRC); 7286 unsigned destPhi = MRI.createVirtualRegister(TRC); 7287 7288 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi) 7289 .addReg(varLoop).addMBB(loopMBB) 7290 .addReg(varEnd).addMBB(entryBB); 7291 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi) 7292 .addReg(srcLoop).addMBB(loopMBB) 7293 .addReg(src).addMBB(entryBB); 7294 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi) 7295 .addReg(destLoop).addMBB(loopMBB) 7296 .addReg(dest).addMBB(entryBB); 7297 7298 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 7299 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz) 7300 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 7301 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop, 7302 IsThumb1, IsThumb2); 7303 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop, 7304 IsThumb1, IsThumb2); 7305 7306 // Decrement loop variable by UnitSize. 7307 if (IsThumb1) { 7308 MachineInstrBuilder MIB = 7309 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop); 7310 MIB = AddDefaultT1CC(MIB); 7311 MIB.addReg(varPhi).addImm(UnitSize); 7312 AddDefaultPred(MIB); 7313 } else { 7314 MachineInstrBuilder MIB = 7315 BuildMI(*BB, BB->end(), dl, 7316 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop); 7317 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize))); 7318 MIB->getOperand(5).setReg(ARM::CPSR); 7319 MIB->getOperand(5).setIsDef(true); 7320 } 7321 BuildMI(*BB, BB->end(), dl, 7322 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc)) 7323 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 7324 7325 // loopMBB can loop back to loopMBB or fall through to exitMBB. 7326 BB->addSuccessor(loopMBB); 7327 BB->addSuccessor(exitMBB); 7328 7329 // Add epilogue to handle BytesLeft. 7330 BB = exitMBB; 7331 MachineInstr *StartOfExit = exitMBB->begin(); 7332 7333 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 7334 // [destOut] = STRB_POST(scratch, destLoop, 1) 7335 unsigned srcIn = srcLoop; 7336 unsigned destIn = destLoop; 7337 for (unsigned i = 0; i < BytesLeft; i++) { 7338 unsigned srcOut = MRI.createVirtualRegister(TRC); 7339 unsigned destOut = MRI.createVirtualRegister(TRC); 7340 unsigned scratch = MRI.createVirtualRegister(TRC); 7341 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut, 7342 IsThumb1, IsThumb2); 7343 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut, 7344 IsThumb1, IsThumb2); 7345 srcIn = srcOut; 7346 destIn = destOut; 7347 } 7348 7349 MI->eraseFromParent(); // The instruction is gone now. 7350 return BB; 7351 } 7352 7353 MachineBasicBlock * 7354 ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI, 7355 MachineBasicBlock *MBB) const { 7356 const TargetMachine &TM = getTargetMachine(); 7357 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo(); 7358 DebugLoc DL = MI->getDebugLoc(); 7359 7360 assert(Subtarget->isTargetWindows() && 7361 "__chkstk is only supported on Windows"); 7362 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode"); 7363 7364 // __chkstk takes the number of words to allocate on the stack in R4, and 7365 // returns the stack adjustment in number of bytes in R4. This will not 7366 // clober any other registers (other than the obvious lr). 7367 // 7368 // Although, technically, IP should be considered a register which may be 7369 // clobbered, the call itself will not touch it. Windows on ARM is a pure 7370 // thumb-2 environment, so there is no interworking required. As a result, we 7371 // do not expect a veneer to be emitted by the linker, clobbering IP. 7372 // 7373 // Each module receives its own copy of __chkstk, so no import thunk is 7374 // required, again, ensuring that IP is not clobbered. 7375 // 7376 // Finally, although some linkers may theoretically provide a trampoline for 7377 // out of range calls (which is quite common due to a 32M range limitation of 7378 // branches for Thumb), we can generate the long-call version via 7379 // -mcmodel=large, alleviating the need for the trampoline which may clobber 7380 // IP. 7381 7382 switch (TM.getCodeModel()) { 7383 case CodeModel::Small: 7384 case CodeModel::Medium: 7385 case CodeModel::Default: 7386 case CodeModel::Kernel: 7387 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL)) 7388 .addImm((unsigned)ARMCC::AL).addReg(0) 7389 .addExternalSymbol("__chkstk") 7390 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 7391 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 7392 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead); 7393 break; 7394 case CodeModel::Large: 7395 case CodeModel::JITDefault: { 7396 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 7397 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass); 7398 7399 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg) 7400 .addExternalSymbol("__chkstk"); 7401 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr)) 7402 .addImm((unsigned)ARMCC::AL).addReg(0) 7403 .addReg(Reg, RegState::Kill) 7404 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 7405 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 7406 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead); 7407 break; 7408 } 7409 } 7410 7411 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr), 7412 ARM::SP) 7413 .addReg(ARM::SP).addReg(ARM::R4))); 7414 7415 MI->eraseFromParent(); 7416 return MBB; 7417 } 7418 7419 MachineBasicBlock * 7420 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 7421 MachineBasicBlock *BB) const { 7422 const TargetInstrInfo *TII = 7423 getTargetMachine().getSubtargetImpl()->getInstrInfo(); 7424 DebugLoc dl = MI->getDebugLoc(); 7425 bool isThumb2 = Subtarget->isThumb2(); 7426 switch (MI->getOpcode()) { 7427 default: { 7428 MI->dump(); 7429 llvm_unreachable("Unexpected instr type to insert"); 7430 } 7431 // The Thumb2 pre-indexed stores have the same MI operands, they just 7432 // define them differently in the .td files from the isel patterns, so 7433 // they need pseudos. 7434 case ARM::t2STR_preidx: 7435 MI->setDesc(TII->get(ARM::t2STR_PRE)); 7436 return BB; 7437 case ARM::t2STRB_preidx: 7438 MI->setDesc(TII->get(ARM::t2STRB_PRE)); 7439 return BB; 7440 case ARM::t2STRH_preidx: 7441 MI->setDesc(TII->get(ARM::t2STRH_PRE)); 7442 return BB; 7443 7444 case ARM::STRi_preidx: 7445 case ARM::STRBi_preidx: { 7446 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ? 7447 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM; 7448 // Decode the offset. 7449 unsigned Offset = MI->getOperand(4).getImm(); 7450 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 7451 Offset = ARM_AM::getAM2Offset(Offset); 7452 if (isSub) 7453 Offset = -Offset; 7454 7455 MachineMemOperand *MMO = *MI->memoperands_begin(); 7456 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 7457 .addOperand(MI->getOperand(0)) // Rn_wb 7458 .addOperand(MI->getOperand(1)) // Rt 7459 .addOperand(MI->getOperand(2)) // Rn 7460 .addImm(Offset) // offset (skip GPR==zero_reg) 7461 .addOperand(MI->getOperand(5)) // pred 7462 .addOperand(MI->getOperand(6)) 7463 .addMemOperand(MMO); 7464 MI->eraseFromParent(); 7465 return BB; 7466 } 7467 case ARM::STRr_preidx: 7468 case ARM::STRBr_preidx: 7469 case ARM::STRH_preidx: { 7470 unsigned NewOpc; 7471 switch (MI->getOpcode()) { 7472 default: llvm_unreachable("unexpected opcode!"); 7473 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 7474 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 7475 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 7476 } 7477 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 7478 for (unsigned i = 0; i < MI->getNumOperands(); ++i) 7479 MIB.addOperand(MI->getOperand(i)); 7480 MI->eraseFromParent(); 7481 return BB; 7482 } 7483 7484 case ARM::tMOVCCr_pseudo: { 7485 // To "insert" a SELECT_CC instruction, we actually have to insert the 7486 // diamond control-flow pattern. The incoming instruction knows the 7487 // destination vreg to set, the condition code register to branch on, the 7488 // true/false values to select between, and a branch opcode to use. 7489 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7490 MachineFunction::iterator It = BB; 7491 ++It; 7492 7493 // thisMBB: 7494 // ... 7495 // TrueVal = ... 7496 // cmpTY ccX, r1, r2 7497 // bCC copy1MBB 7498 // fallthrough --> copy0MBB 7499 MachineBasicBlock *thisMBB = BB; 7500 MachineFunction *F = BB->getParent(); 7501 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 7502 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 7503 F->insert(It, copy0MBB); 7504 F->insert(It, sinkMBB); 7505 7506 // Transfer the remainder of BB and its successor edges to sinkMBB. 7507 sinkMBB->splice(sinkMBB->begin(), BB, 7508 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7509 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 7510 7511 BB->addSuccessor(copy0MBB); 7512 BB->addSuccessor(sinkMBB); 7513 7514 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB) 7515 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg()); 7516 7517 // copy0MBB: 7518 // %FalseValue = ... 7519 // # fallthrough to sinkMBB 7520 BB = copy0MBB; 7521 7522 // Update machine-CFG edges 7523 BB->addSuccessor(sinkMBB); 7524 7525 // sinkMBB: 7526 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 7527 // ... 7528 BB = sinkMBB; 7529 BuildMI(*BB, BB->begin(), dl, 7530 TII->get(ARM::PHI), MI->getOperand(0).getReg()) 7531 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) 7532 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 7533 7534 MI->eraseFromParent(); // The pseudo instruction is gone now. 7535 return BB; 7536 } 7537 7538 case ARM::BCCi64: 7539 case ARM::BCCZi64: { 7540 // If there is an unconditional branch to the other successor, remove it. 7541 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7542 7543 // Compare both parts that make up the double comparison separately for 7544 // equality. 7545 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64; 7546 7547 unsigned LHS1 = MI->getOperand(1).getReg(); 7548 unsigned LHS2 = MI->getOperand(2).getReg(); 7549 if (RHSisZero) { 7550 AddDefaultPred(BuildMI(BB, dl, 7551 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7552 .addReg(LHS1).addImm(0)); 7553 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7554 .addReg(LHS2).addImm(0) 7555 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 7556 } else { 7557 unsigned RHS1 = MI->getOperand(3).getReg(); 7558 unsigned RHS2 = MI->getOperand(4).getReg(); 7559 AddDefaultPred(BuildMI(BB, dl, 7560 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 7561 .addReg(LHS1).addReg(RHS1)); 7562 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 7563 .addReg(LHS2).addReg(RHS2) 7564 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 7565 } 7566 7567 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB(); 7568 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 7569 if (MI->getOperand(0).getImm() == ARMCC::NE) 7570 std::swap(destMBB, exitMBB); 7571 7572 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 7573 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 7574 if (isThumb2) 7575 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB)); 7576 else 7577 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 7578 7579 MI->eraseFromParent(); // The pseudo instruction is gone now. 7580 return BB; 7581 } 7582 7583 case ARM::Int_eh_sjlj_setjmp: 7584 case ARM::Int_eh_sjlj_setjmp_nofp: 7585 case ARM::tInt_eh_sjlj_setjmp: 7586 case ARM::t2Int_eh_sjlj_setjmp: 7587 case ARM::t2Int_eh_sjlj_setjmp_nofp: 7588 EmitSjLjDispatchBlock(MI, BB); 7589 return BB; 7590 7591 case ARM::ABS: 7592 case ARM::t2ABS: { 7593 // To insert an ABS instruction, we have to insert the 7594 // diamond control-flow pattern. The incoming instruction knows the 7595 // source vreg to test against 0, the destination vreg to set, 7596 // the condition code register to branch on, the 7597 // true/false values to select between, and a branch opcode to use. 7598 // It transforms 7599 // V1 = ABS V0 7600 // into 7601 // V2 = MOVS V0 7602 // BCC (branch to SinkBB if V0 >= 0) 7603 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 7604 // SinkBB: V1 = PHI(V2, V3) 7605 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7606 MachineFunction::iterator BBI = BB; 7607 ++BBI; 7608 MachineFunction *Fn = BB->getParent(); 7609 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 7610 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 7611 Fn->insert(BBI, RSBBB); 7612 Fn->insert(BBI, SinkBB); 7613 7614 unsigned int ABSSrcReg = MI->getOperand(1).getReg(); 7615 unsigned int ABSDstReg = MI->getOperand(0).getReg(); 7616 bool isThumb2 = Subtarget->isThumb2(); 7617 MachineRegisterInfo &MRI = Fn->getRegInfo(); 7618 // In Thumb mode S must not be specified if source register is the SP or 7619 // PC and if destination register is the SP, so restrict register class 7620 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ? 7621 (const TargetRegisterClass*)&ARM::rGPRRegClass : 7622 (const TargetRegisterClass*)&ARM::GPRRegClass); 7623 7624 // Transfer the remainder of BB and its successor edges to sinkMBB. 7625 SinkBB->splice(SinkBB->begin(), BB, 7626 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7627 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 7628 7629 BB->addSuccessor(RSBBB); 7630 BB->addSuccessor(SinkBB); 7631 7632 // fall through to SinkMBB 7633 RSBBB->addSuccessor(SinkBB); 7634 7635 // insert a cmp at the end of BB 7636 AddDefaultPred(BuildMI(BB, dl, 7637 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7638 .addReg(ABSSrcReg).addImm(0)); 7639 7640 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 7641 BuildMI(BB, dl, 7642 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 7643 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 7644 7645 // insert rsbri in RSBBB 7646 // Note: BCC and rsbri will be converted into predicated rsbmi 7647 // by if-conversion pass 7648 BuildMI(*RSBBB, RSBBB->begin(), dl, 7649 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 7650 .addReg(ABSSrcReg, RegState::Kill) 7651 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 7652 7653 // insert PHI in SinkBB, 7654 // reuse ABSDstReg to not change uses of ABS instruction 7655 BuildMI(*SinkBB, SinkBB->begin(), dl, 7656 TII->get(ARM::PHI), ABSDstReg) 7657 .addReg(NewRsbDstReg).addMBB(RSBBB) 7658 .addReg(ABSSrcReg).addMBB(BB); 7659 7660 // remove ABS instruction 7661 MI->eraseFromParent(); 7662 7663 // return last added BB 7664 return SinkBB; 7665 } 7666 case ARM::COPY_STRUCT_BYVAL_I32: 7667 ++NumLoopByVals; 7668 return EmitStructByval(MI, BB); 7669 case ARM::WIN__CHKSTK: 7670 return EmitLowered__chkstk(MI, BB); 7671 } 7672 } 7673 7674 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, 7675 SDNode *Node) const { 7676 const MCInstrDesc *MCID = &MI->getDesc(); 7677 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 7678 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 7679 // operand is still set to noreg. If needed, set the optional operand's 7680 // register to CPSR, and remove the redundant implicit def. 7681 // 7682 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>). 7683 7684 // Rename pseudo opcodes. 7685 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode()); 7686 if (NewOpc) { 7687 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>( 7688 getTargetMachine().getSubtargetImpl()->getInstrInfo()); 7689 MCID = &TII->get(NewOpc); 7690 7691 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 && 7692 "converted opcode should be the same except for cc_out"); 7693 7694 MI->setDesc(*MCID); 7695 7696 // Add the optional cc_out operand 7697 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 7698 } 7699 unsigned ccOutIdx = MCID->getNumOperands() - 1; 7700 7701 // Any ARM instruction that sets the 's' bit should specify an optional 7702 // "cc_out" operand in the last operand position. 7703 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 7704 assert(!NewOpc && "Optional cc_out operand required"); 7705 return; 7706 } 7707 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 7708 // since we already have an optional CPSR def. 7709 bool definesCPSR = false; 7710 bool deadCPSR = false; 7711 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands(); 7712 i != e; ++i) { 7713 const MachineOperand &MO = MI->getOperand(i); 7714 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 7715 definesCPSR = true; 7716 if (MO.isDead()) 7717 deadCPSR = true; 7718 MI->RemoveOperand(i); 7719 break; 7720 } 7721 } 7722 if (!definesCPSR) { 7723 assert(!NewOpc && "Optional cc_out operand required"); 7724 return; 7725 } 7726 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 7727 if (deadCPSR) { 7728 assert(!MI->getOperand(ccOutIdx).getReg() && 7729 "expect uninitialized optional cc_out operand"); 7730 return; 7731 } 7732 7733 // If this instruction was defined with an optional CPSR def and its dag node 7734 // had a live implicit CPSR def, then activate the optional CPSR def. 7735 MachineOperand &MO = MI->getOperand(ccOutIdx); 7736 MO.setReg(ARM::CPSR); 7737 MO.setIsDef(true); 7738 } 7739 7740 //===----------------------------------------------------------------------===// 7741 // ARM Optimization Hooks 7742 //===----------------------------------------------------------------------===// 7743 7744 // Helper function that checks if N is a null or all ones constant. 7745 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) { 7746 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N); 7747 if (!C) 7748 return false; 7749 return AllOnes ? C->isAllOnesValue() : C->isNullValue(); 7750 } 7751 7752 // Return true if N is conditionally 0 or all ones. 7753 // Detects these expressions where cc is an i1 value: 7754 // 7755 // (select cc 0, y) [AllOnes=0] 7756 // (select cc y, 0) [AllOnes=0] 7757 // (zext cc) [AllOnes=0] 7758 // (sext cc) [AllOnes=0/1] 7759 // (select cc -1, y) [AllOnes=1] 7760 // (select cc y, -1) [AllOnes=1] 7761 // 7762 // Invert is set when N is the null/all ones constant when CC is false. 7763 // OtherOp is set to the alternative value of N. 7764 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, 7765 SDValue &CC, bool &Invert, 7766 SDValue &OtherOp, 7767 SelectionDAG &DAG) { 7768 switch (N->getOpcode()) { 7769 default: return false; 7770 case ISD::SELECT: { 7771 CC = N->getOperand(0); 7772 SDValue N1 = N->getOperand(1); 7773 SDValue N2 = N->getOperand(2); 7774 if (isZeroOrAllOnes(N1, AllOnes)) { 7775 Invert = false; 7776 OtherOp = N2; 7777 return true; 7778 } 7779 if (isZeroOrAllOnes(N2, AllOnes)) { 7780 Invert = true; 7781 OtherOp = N1; 7782 return true; 7783 } 7784 return false; 7785 } 7786 case ISD::ZERO_EXTEND: 7787 // (zext cc) can never be the all ones value. 7788 if (AllOnes) 7789 return false; 7790 // Fall through. 7791 case ISD::SIGN_EXTEND: { 7792 EVT VT = N->getValueType(0); 7793 CC = N->getOperand(0); 7794 if (CC.getValueType() != MVT::i1) 7795 return false; 7796 Invert = !AllOnes; 7797 if (AllOnes) 7798 // When looking for an AllOnes constant, N is an sext, and the 'other' 7799 // value is 0. 7800 OtherOp = DAG.getConstant(0, VT); 7801 else if (N->getOpcode() == ISD::ZERO_EXTEND) 7802 // When looking for a 0 constant, N can be zext or sext. 7803 OtherOp = DAG.getConstant(1, VT); 7804 else 7805 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT); 7806 return true; 7807 } 7808 } 7809 } 7810 7811 // Combine a constant select operand into its use: 7812 // 7813 // (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 7814 // (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 7815 // (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1] 7816 // (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 7817 // (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 7818 // 7819 // The transform is rejected if the select doesn't have a constant operand that 7820 // is null, or all ones when AllOnes is set. 7821 // 7822 // Also recognize sext/zext from i1: 7823 // 7824 // (add (zext cc), x) -> (select cc (add x, 1), x) 7825 // (add (sext cc), x) -> (select cc (add x, -1), x) 7826 // 7827 // These transformations eventually create predicated instructions. 7828 // 7829 // @param N The node to transform. 7830 // @param Slct The N operand that is a select. 7831 // @param OtherOp The other N operand (x above). 7832 // @param DCI Context. 7833 // @param AllOnes Require the select constant to be all ones instead of null. 7834 // @returns The new node, or SDValue() on failure. 7835 static 7836 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 7837 TargetLowering::DAGCombinerInfo &DCI, 7838 bool AllOnes = false) { 7839 SelectionDAG &DAG = DCI.DAG; 7840 EVT VT = N->getValueType(0); 7841 SDValue NonConstantVal; 7842 SDValue CCOp; 7843 bool SwapSelectOps; 7844 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps, 7845 NonConstantVal, DAG)) 7846 return SDValue(); 7847 7848 // Slct is now know to be the desired identity constant when CC is true. 7849 SDValue TrueVal = OtherOp; 7850 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 7851 OtherOp, NonConstantVal); 7852 // Unless SwapSelectOps says CC should be false. 7853 if (SwapSelectOps) 7854 std::swap(TrueVal, FalseVal); 7855 7856 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, 7857 CCOp, TrueVal, FalseVal); 7858 } 7859 7860 // Attempt combineSelectAndUse on each operand of a commutative operator N. 7861 static 7862 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes, 7863 TargetLowering::DAGCombinerInfo &DCI) { 7864 SDValue N0 = N->getOperand(0); 7865 SDValue N1 = N->getOperand(1); 7866 if (N0.getNode()->hasOneUse()) { 7867 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes); 7868 if (Result.getNode()) 7869 return Result; 7870 } 7871 if (N1.getNode()->hasOneUse()) { 7872 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes); 7873 if (Result.getNode()) 7874 return Result; 7875 } 7876 return SDValue(); 7877 } 7878 7879 // AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction 7880 // (only after legalization). 7881 static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1, 7882 TargetLowering::DAGCombinerInfo &DCI, 7883 const ARMSubtarget *Subtarget) { 7884 7885 // Only perform optimization if after legalize, and if NEON is available. We 7886 // also expected both operands to be BUILD_VECTORs. 7887 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 7888 || N0.getOpcode() != ISD::BUILD_VECTOR 7889 || N1.getOpcode() != ISD::BUILD_VECTOR) 7890 return SDValue(); 7891 7892 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 7893 EVT VT = N->getValueType(0); 7894 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 7895 return SDValue(); 7896 7897 // Check that the vector operands are of the right form. 7898 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 7899 // operands, where N is the size of the formed vector. 7900 // Each EXTRACT_VECTOR should have the same input vector and odd or even 7901 // index such that we have a pair wise add pattern. 7902 7903 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 7904 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 7905 return SDValue(); 7906 SDValue Vec = N0->getOperand(0)->getOperand(0); 7907 SDNode *V = Vec.getNode(); 7908 unsigned nextIndex = 0; 7909 7910 // For each operands to the ADD which are BUILD_VECTORs, 7911 // check to see if each of their operands are an EXTRACT_VECTOR with 7912 // the same vector and appropriate index. 7913 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 7914 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 7915 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 7916 7917 SDValue ExtVec0 = N0->getOperand(i); 7918 SDValue ExtVec1 = N1->getOperand(i); 7919 7920 // First operand is the vector, verify its the same. 7921 if (V != ExtVec0->getOperand(0).getNode() || 7922 V != ExtVec1->getOperand(0).getNode()) 7923 return SDValue(); 7924 7925 // Second is the constant, verify its correct. 7926 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 7927 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 7928 7929 // For the constant, we want to see all the even or all the odd. 7930 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 7931 || C1->getZExtValue() != nextIndex+1) 7932 return SDValue(); 7933 7934 // Increment index. 7935 nextIndex+=2; 7936 } else 7937 return SDValue(); 7938 } 7939 7940 // Create VPADDL node. 7941 SelectionDAG &DAG = DCI.DAG; 7942 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7943 7944 // Build operand list. 7945 SmallVector<SDValue, 8> Ops; 7946 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, 7947 TLI.getPointerTy())); 7948 7949 // Input is the vector. 7950 Ops.push_back(Vec); 7951 7952 // Get widened type and narrowed type. 7953 MVT widenType; 7954 unsigned numElem = VT.getVectorNumElements(); 7955 7956 EVT inputLaneType = Vec.getValueType().getVectorElementType(); 7957 switch (inputLaneType.getSimpleVT().SimpleTy) { 7958 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 7959 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 7960 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 7961 default: 7962 llvm_unreachable("Invalid vector element type for padd optimization."); 7963 } 7964 7965 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops); 7966 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE; 7967 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp); 7968 } 7969 7970 static SDValue findMUL_LOHI(SDValue V) { 7971 if (V->getOpcode() == ISD::UMUL_LOHI || 7972 V->getOpcode() == ISD::SMUL_LOHI) 7973 return V; 7974 return SDValue(); 7975 } 7976 7977 static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode, 7978 TargetLowering::DAGCombinerInfo &DCI, 7979 const ARMSubtarget *Subtarget) { 7980 7981 if (Subtarget->isThumb1Only()) return SDValue(); 7982 7983 // Only perform the checks after legalize when the pattern is available. 7984 if (DCI.isBeforeLegalize()) return SDValue(); 7985 7986 // Look for multiply add opportunities. 7987 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where 7988 // each add nodes consumes a value from ISD::UMUL_LOHI and there is 7989 // a glue link from the first add to the second add. 7990 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by 7991 // a S/UMLAL instruction. 7992 // loAdd UMUL_LOHI 7993 // \ / :lo \ :hi 7994 // \ / \ [no multiline comment] 7995 // ADDC | hiAdd 7996 // \ :glue / / 7997 // \ / / 7998 // ADDE 7999 // 8000 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC"); 8001 SDValue AddcOp0 = AddcNode->getOperand(0); 8002 SDValue AddcOp1 = AddcNode->getOperand(1); 8003 8004 // Check if the two operands are from the same mul_lohi node. 8005 if (AddcOp0.getNode() == AddcOp1.getNode()) 8006 return SDValue(); 8007 8008 assert(AddcNode->getNumValues() == 2 && 8009 AddcNode->getValueType(0) == MVT::i32 && 8010 "Expect ADDC with two result values. First: i32"); 8011 8012 // Check that we have a glued ADDC node. 8013 if (AddcNode->getValueType(1) != MVT::Glue) 8014 return SDValue(); 8015 8016 // Check that the ADDC adds the low result of the S/UMUL_LOHI. 8017 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI && 8018 AddcOp0->getOpcode() != ISD::SMUL_LOHI && 8019 AddcOp1->getOpcode() != ISD::UMUL_LOHI && 8020 AddcOp1->getOpcode() != ISD::SMUL_LOHI) 8021 return SDValue(); 8022 8023 // Look for the glued ADDE. 8024 SDNode* AddeNode = AddcNode->getGluedUser(); 8025 if (!AddeNode) 8026 return SDValue(); 8027 8028 // Make sure it is really an ADDE. 8029 if (AddeNode->getOpcode() != ISD::ADDE) 8030 return SDValue(); 8031 8032 assert(AddeNode->getNumOperands() == 3 && 8033 AddeNode->getOperand(2).getValueType() == MVT::Glue && 8034 "ADDE node has the wrong inputs"); 8035 8036 // Check for the triangle shape. 8037 SDValue AddeOp0 = AddeNode->getOperand(0); 8038 SDValue AddeOp1 = AddeNode->getOperand(1); 8039 8040 // Make sure that the ADDE operands are not coming from the same node. 8041 if (AddeOp0.getNode() == AddeOp1.getNode()) 8042 return SDValue(); 8043 8044 // Find the MUL_LOHI node walking up ADDE's operands. 8045 bool IsLeftOperandMUL = false; 8046 SDValue MULOp = findMUL_LOHI(AddeOp0); 8047 if (MULOp == SDValue()) 8048 MULOp = findMUL_LOHI(AddeOp1); 8049 else 8050 IsLeftOperandMUL = true; 8051 if (MULOp == SDValue()) 8052 return SDValue(); 8053 8054 // Figure out the right opcode. 8055 unsigned Opc = MULOp->getOpcode(); 8056 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL; 8057 8058 // Figure out the high and low input values to the MLAL node. 8059 SDValue* HiMul = &MULOp; 8060 SDValue* HiAdd = nullptr; 8061 SDValue* LoMul = nullptr; 8062 SDValue* LowAdd = nullptr; 8063 8064 if (IsLeftOperandMUL) 8065 HiAdd = &AddeOp1; 8066 else 8067 HiAdd = &AddeOp0; 8068 8069 8070 if (AddcOp0->getOpcode() == Opc) { 8071 LoMul = &AddcOp0; 8072 LowAdd = &AddcOp1; 8073 } 8074 if (AddcOp1->getOpcode() == Opc) { 8075 LoMul = &AddcOp1; 8076 LowAdd = &AddcOp0; 8077 } 8078 8079 if (!LoMul) 8080 return SDValue(); 8081 8082 if (LoMul->getNode() != HiMul->getNode()) 8083 return SDValue(); 8084 8085 // Create the merged node. 8086 SelectionDAG &DAG = DCI.DAG; 8087 8088 // Build operand list. 8089 SmallVector<SDValue, 8> Ops; 8090 Ops.push_back(LoMul->getOperand(0)); 8091 Ops.push_back(LoMul->getOperand(1)); 8092 Ops.push_back(*LowAdd); 8093 Ops.push_back(*HiAdd); 8094 8095 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode), 8096 DAG.getVTList(MVT::i32, MVT::i32), Ops); 8097 8098 // Replace the ADDs' nodes uses by the MLA node's values. 8099 SDValue HiMLALResult(MLALNode.getNode(), 1); 8100 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult); 8101 8102 SDValue LoMLALResult(MLALNode.getNode(), 0); 8103 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult); 8104 8105 // Return original node to notify the driver to stop replacing. 8106 SDValue resNode(AddcNode, 0); 8107 return resNode; 8108 } 8109 8110 /// PerformADDCCombine - Target-specific dag combine transform from 8111 /// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL. 8112 static SDValue PerformADDCCombine(SDNode *N, 8113 TargetLowering::DAGCombinerInfo &DCI, 8114 const ARMSubtarget *Subtarget) { 8115 8116 return AddCombineTo64bitMLAL(N, DCI, Subtarget); 8117 8118 } 8119 8120 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 8121 /// operands N0 and N1. This is a helper for PerformADDCombine that is 8122 /// called with the default operands, and if that fails, with commuted 8123 /// operands. 8124 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 8125 TargetLowering::DAGCombinerInfo &DCI, 8126 const ARMSubtarget *Subtarget){ 8127 8128 // Attempt to create vpaddl for this add. 8129 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget); 8130 if (Result.getNode()) 8131 return Result; 8132 8133 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 8134 if (N0.getNode()->hasOneUse()) { 8135 SDValue Result = combineSelectAndUse(N, N0, N1, DCI); 8136 if (Result.getNode()) return Result; 8137 } 8138 return SDValue(); 8139 } 8140 8141 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 8142 /// 8143 static SDValue PerformADDCombine(SDNode *N, 8144 TargetLowering::DAGCombinerInfo &DCI, 8145 const ARMSubtarget *Subtarget) { 8146 SDValue N0 = N->getOperand(0); 8147 SDValue N1 = N->getOperand(1); 8148 8149 // First try with the default operand order. 8150 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget); 8151 if (Result.getNode()) 8152 return Result; 8153 8154 // If that didn't work, try again with the operands commuted. 8155 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 8156 } 8157 8158 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 8159 /// 8160 static SDValue PerformSUBCombine(SDNode *N, 8161 TargetLowering::DAGCombinerInfo &DCI) { 8162 SDValue N0 = N->getOperand(0); 8163 SDValue N1 = N->getOperand(1); 8164 8165 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 8166 if (N1.getNode()->hasOneUse()) { 8167 SDValue Result = combineSelectAndUse(N, N1, N0, DCI); 8168 if (Result.getNode()) return Result; 8169 } 8170 8171 return SDValue(); 8172 } 8173 8174 /// PerformVMULCombine 8175 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 8176 /// special multiplier accumulator forwarding. 8177 /// vmul d3, d0, d2 8178 /// vmla d3, d1, d2 8179 /// is faster than 8180 /// vadd d3, d0, d1 8181 /// vmul d3, d3, d2 8182 // However, for (A + B) * (A + B), 8183 // vadd d2, d0, d1 8184 // vmul d3, d0, d2 8185 // vmla d3, d1, d2 8186 // is slower than 8187 // vadd d2, d0, d1 8188 // vmul d3, d2, d2 8189 static SDValue PerformVMULCombine(SDNode *N, 8190 TargetLowering::DAGCombinerInfo &DCI, 8191 const ARMSubtarget *Subtarget) { 8192 if (!Subtarget->hasVMLxForwarding()) 8193 return SDValue(); 8194 8195 SelectionDAG &DAG = DCI.DAG; 8196 SDValue N0 = N->getOperand(0); 8197 SDValue N1 = N->getOperand(1); 8198 unsigned Opcode = N0.getOpcode(); 8199 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 8200 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 8201 Opcode = N1.getOpcode(); 8202 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 8203 Opcode != ISD::FADD && Opcode != ISD::FSUB) 8204 return SDValue(); 8205 std::swap(N0, N1); 8206 } 8207 8208 if (N0 == N1) 8209 return SDValue(); 8210 8211 EVT VT = N->getValueType(0); 8212 SDLoc DL(N); 8213 SDValue N00 = N0->getOperand(0); 8214 SDValue N01 = N0->getOperand(1); 8215 return DAG.getNode(Opcode, DL, VT, 8216 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 8217 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 8218 } 8219 8220 static SDValue PerformMULCombine(SDNode *N, 8221 TargetLowering::DAGCombinerInfo &DCI, 8222 const ARMSubtarget *Subtarget) { 8223 SelectionDAG &DAG = DCI.DAG; 8224 8225 if (Subtarget->isThumb1Only()) 8226 return SDValue(); 8227 8228 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 8229 return SDValue(); 8230 8231 EVT VT = N->getValueType(0); 8232 if (VT.is64BitVector() || VT.is128BitVector()) 8233 return PerformVMULCombine(N, DCI, Subtarget); 8234 if (VT != MVT::i32) 8235 return SDValue(); 8236 8237 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 8238 if (!C) 8239 return SDValue(); 8240 8241 int64_t MulAmt = C->getSExtValue(); 8242 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt); 8243 8244 ShiftAmt = ShiftAmt & (32 - 1); 8245 SDValue V = N->getOperand(0); 8246 SDLoc DL(N); 8247 8248 SDValue Res; 8249 MulAmt >>= ShiftAmt; 8250 8251 if (MulAmt >= 0) { 8252 if (isPowerOf2_32(MulAmt - 1)) { 8253 // (mul x, 2^N + 1) => (add (shl x, N), x) 8254 Res = DAG.getNode(ISD::ADD, DL, VT, 8255 V, 8256 DAG.getNode(ISD::SHL, DL, VT, 8257 V, 8258 DAG.getConstant(Log2_32(MulAmt - 1), 8259 MVT::i32))); 8260 } else if (isPowerOf2_32(MulAmt + 1)) { 8261 // (mul x, 2^N - 1) => (sub (shl x, N), x) 8262 Res = DAG.getNode(ISD::SUB, DL, VT, 8263 DAG.getNode(ISD::SHL, DL, VT, 8264 V, 8265 DAG.getConstant(Log2_32(MulAmt + 1), 8266 MVT::i32)), 8267 V); 8268 } else 8269 return SDValue(); 8270 } else { 8271 uint64_t MulAmtAbs = -MulAmt; 8272 if (isPowerOf2_32(MulAmtAbs + 1)) { 8273 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 8274 Res = DAG.getNode(ISD::SUB, DL, VT, 8275 V, 8276 DAG.getNode(ISD::SHL, DL, VT, 8277 V, 8278 DAG.getConstant(Log2_32(MulAmtAbs + 1), 8279 MVT::i32))); 8280 } else if (isPowerOf2_32(MulAmtAbs - 1)) { 8281 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 8282 Res = DAG.getNode(ISD::ADD, DL, VT, 8283 V, 8284 DAG.getNode(ISD::SHL, DL, VT, 8285 V, 8286 DAG.getConstant(Log2_32(MulAmtAbs-1), 8287 MVT::i32))); 8288 Res = DAG.getNode(ISD::SUB, DL, VT, 8289 DAG.getConstant(0, MVT::i32),Res); 8290 8291 } else 8292 return SDValue(); 8293 } 8294 8295 if (ShiftAmt != 0) 8296 Res = DAG.getNode(ISD::SHL, DL, VT, 8297 Res, DAG.getConstant(ShiftAmt, MVT::i32)); 8298 8299 // Do not add new nodes to DAG combiner worklist. 8300 DCI.CombineTo(N, Res, false); 8301 return SDValue(); 8302 } 8303 8304 static SDValue PerformANDCombine(SDNode *N, 8305 TargetLowering::DAGCombinerInfo &DCI, 8306 const ARMSubtarget *Subtarget) { 8307 8308 // Attempt to use immediate-form VBIC 8309 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 8310 SDLoc dl(N); 8311 EVT VT = N->getValueType(0); 8312 SelectionDAG &DAG = DCI.DAG; 8313 8314 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8315 return SDValue(); 8316 8317 APInt SplatBits, SplatUndef; 8318 unsigned SplatBitSize; 8319 bool HasAnyUndefs; 8320 if (BVN && 8321 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 8322 if (SplatBitSize <= 64) { 8323 EVT VbicVT; 8324 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(), 8325 SplatUndef.getZExtValue(), SplatBitSize, 8326 DAG, VbicVT, VT.is128BitVector(), 8327 OtherModImm); 8328 if (Val.getNode()) { 8329 SDValue Input = 8330 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 8331 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 8332 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 8333 } 8334 } 8335 } 8336 8337 if (!Subtarget->isThumb1Only()) { 8338 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) 8339 SDValue Result = combineSelectAndUseCommutative(N, true, DCI); 8340 if (Result.getNode()) 8341 return Result; 8342 } 8343 8344 return SDValue(); 8345 } 8346 8347 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 8348 static SDValue PerformORCombine(SDNode *N, 8349 TargetLowering::DAGCombinerInfo &DCI, 8350 const ARMSubtarget *Subtarget) { 8351 // Attempt to use immediate-form VORR 8352 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 8353 SDLoc dl(N); 8354 EVT VT = N->getValueType(0); 8355 SelectionDAG &DAG = DCI.DAG; 8356 8357 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8358 return SDValue(); 8359 8360 APInt SplatBits, SplatUndef; 8361 unsigned SplatBitSize; 8362 bool HasAnyUndefs; 8363 if (BVN && Subtarget->hasNEON() && 8364 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 8365 if (SplatBitSize <= 64) { 8366 EVT VorrVT; 8367 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 8368 SplatUndef.getZExtValue(), SplatBitSize, 8369 DAG, VorrVT, VT.is128BitVector(), 8370 OtherModImm); 8371 if (Val.getNode()) { 8372 SDValue Input = 8373 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 8374 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 8375 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 8376 } 8377 } 8378 } 8379 8380 if (!Subtarget->isThumb1Only()) { 8381 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 8382 SDValue Result = combineSelectAndUseCommutative(N, false, DCI); 8383 if (Result.getNode()) 8384 return Result; 8385 } 8386 8387 // The code below optimizes (or (and X, Y), Z). 8388 // The AND operand needs to have a single user to make these optimizations 8389 // profitable. 8390 SDValue N0 = N->getOperand(0); 8391 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) 8392 return SDValue(); 8393 SDValue N1 = N->getOperand(1); 8394 8395 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 8396 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 8397 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 8398 APInt SplatUndef; 8399 unsigned SplatBitSize; 8400 bool HasAnyUndefs; 8401 8402 APInt SplatBits0, SplatBits1; 8403 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 8404 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 8405 // Ensure that the second operand of both ands are constants 8406 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 8407 HasAnyUndefs) && !HasAnyUndefs) { 8408 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 8409 HasAnyUndefs) && !HasAnyUndefs) { 8410 // Ensure that the bit width of the constants are the same and that 8411 // the splat arguments are logical inverses as per the pattern we 8412 // are trying to simplify. 8413 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() && 8414 SplatBits0 == ~SplatBits1) { 8415 // Canonicalize the vector type to make instruction selection 8416 // simpler. 8417 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 8418 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 8419 N0->getOperand(1), 8420 N0->getOperand(0), 8421 N1->getOperand(0)); 8422 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 8423 } 8424 } 8425 } 8426 } 8427 8428 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 8429 // reasonable. 8430 8431 // BFI is only available on V6T2+ 8432 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 8433 return SDValue(); 8434 8435 SDLoc DL(N); 8436 // 1) or (and A, mask), val => ARMbfi A, val, mask 8437 // iff (val & mask) == val 8438 // 8439 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 8440 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 8441 // && mask == ~mask2 8442 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 8443 // && ~mask == mask2 8444 // (i.e., copy a bitfield value into another bitfield of the same width) 8445 8446 if (VT != MVT::i32) 8447 return SDValue(); 8448 8449 SDValue N00 = N0.getOperand(0); 8450 8451 // The value and the mask need to be constants so we can verify this is 8452 // actually a bitfield set. If the mask is 0xffff, we can do better 8453 // via a movt instruction, so don't use BFI in that case. 8454 SDValue MaskOp = N0.getOperand(1); 8455 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 8456 if (!MaskC) 8457 return SDValue(); 8458 unsigned Mask = MaskC->getZExtValue(); 8459 if (Mask == 0xffff) 8460 return SDValue(); 8461 SDValue Res; 8462 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 8463 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 8464 if (N1C) { 8465 unsigned Val = N1C->getZExtValue(); 8466 if ((Val & ~Mask) != Val) 8467 return SDValue(); 8468 8469 if (ARM::isBitFieldInvertedMask(Mask)) { 8470 Val >>= countTrailingZeros(~Mask); 8471 8472 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 8473 DAG.getConstant(Val, MVT::i32), 8474 DAG.getConstant(Mask, MVT::i32)); 8475 8476 // Do not add new nodes to DAG combiner worklist. 8477 DCI.CombineTo(N, Res, false); 8478 return SDValue(); 8479 } 8480 } else if (N1.getOpcode() == ISD::AND) { 8481 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 8482 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 8483 if (!N11C) 8484 return SDValue(); 8485 unsigned Mask2 = N11C->getZExtValue(); 8486 8487 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 8488 // as is to match. 8489 if (ARM::isBitFieldInvertedMask(Mask) && 8490 (Mask == ~Mask2)) { 8491 // The pack halfword instruction works better for masks that fit it, 8492 // so use that when it's available. 8493 if (Subtarget->hasT2ExtractPack() && 8494 (Mask == 0xffff || Mask == 0xffff0000)) 8495 return SDValue(); 8496 // 2a 8497 unsigned amt = countTrailingZeros(Mask2); 8498 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 8499 DAG.getConstant(amt, MVT::i32)); 8500 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 8501 DAG.getConstant(Mask, MVT::i32)); 8502 // Do not add new nodes to DAG combiner worklist. 8503 DCI.CombineTo(N, Res, false); 8504 return SDValue(); 8505 } else if (ARM::isBitFieldInvertedMask(~Mask) && 8506 (~Mask == Mask2)) { 8507 // The pack halfword instruction works better for masks that fit it, 8508 // so use that when it's available. 8509 if (Subtarget->hasT2ExtractPack() && 8510 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 8511 return SDValue(); 8512 // 2b 8513 unsigned lsb = countTrailingZeros(Mask); 8514 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 8515 DAG.getConstant(lsb, MVT::i32)); 8516 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 8517 DAG.getConstant(Mask2, MVT::i32)); 8518 // Do not add new nodes to DAG combiner worklist. 8519 DCI.CombineTo(N, Res, false); 8520 return SDValue(); 8521 } 8522 } 8523 8524 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 8525 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 8526 ARM::isBitFieldInvertedMask(~Mask)) { 8527 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 8528 // where lsb(mask) == #shamt and masked bits of B are known zero. 8529 SDValue ShAmt = N00.getOperand(1); 8530 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 8531 unsigned LSB = countTrailingZeros(Mask); 8532 if (ShAmtC != LSB) 8533 return SDValue(); 8534 8535 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 8536 DAG.getConstant(~Mask, MVT::i32)); 8537 8538 // Do not add new nodes to DAG combiner worklist. 8539 DCI.CombineTo(N, Res, false); 8540 } 8541 8542 return SDValue(); 8543 } 8544 8545 static SDValue PerformXORCombine(SDNode *N, 8546 TargetLowering::DAGCombinerInfo &DCI, 8547 const ARMSubtarget *Subtarget) { 8548 EVT VT = N->getValueType(0); 8549 SelectionDAG &DAG = DCI.DAG; 8550 8551 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8552 return SDValue(); 8553 8554 if (!Subtarget->isThumb1Only()) { 8555 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 8556 SDValue Result = combineSelectAndUseCommutative(N, false, DCI); 8557 if (Result.getNode()) 8558 return Result; 8559 } 8560 8561 return SDValue(); 8562 } 8563 8564 /// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 8565 /// the bits being cleared by the AND are not demanded by the BFI. 8566 static SDValue PerformBFICombine(SDNode *N, 8567 TargetLowering::DAGCombinerInfo &DCI) { 8568 SDValue N1 = N->getOperand(1); 8569 if (N1.getOpcode() == ISD::AND) { 8570 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 8571 if (!N11C) 8572 return SDValue(); 8573 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 8574 unsigned LSB = countTrailingZeros(~InvMask); 8575 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB; 8576 unsigned Mask = (1 << Width)-1; 8577 unsigned Mask2 = N11C->getZExtValue(); 8578 if ((Mask & (~Mask2)) == 0) 8579 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0), 8580 N->getOperand(0), N1.getOperand(0), 8581 N->getOperand(2)); 8582 } 8583 return SDValue(); 8584 } 8585 8586 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 8587 /// ARMISD::VMOVRRD. 8588 static SDValue PerformVMOVRRDCombine(SDNode *N, 8589 TargetLowering::DAGCombinerInfo &DCI, 8590 const ARMSubtarget *Subtarget) { 8591 // vmovrrd(vmovdrr x, y) -> x,y 8592 SDValue InDouble = N->getOperand(0); 8593 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP()) 8594 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 8595 8596 // vmovrrd(load f64) -> (load i32), (load i32) 8597 SDNode *InNode = InDouble.getNode(); 8598 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 8599 InNode->getValueType(0) == MVT::f64 && 8600 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 8601 !cast<LoadSDNode>(InNode)->isVolatile()) { 8602 // TODO: Should this be done for non-FrameIndex operands? 8603 LoadSDNode *LD = cast<LoadSDNode>(InNode); 8604 8605 SelectionDAG &DAG = DCI.DAG; 8606 SDLoc DL(LD); 8607 SDValue BasePtr = LD->getBasePtr(); 8608 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, 8609 LD->getPointerInfo(), LD->isVolatile(), 8610 LD->isNonTemporal(), LD->isInvariant(), 8611 LD->getAlignment()); 8612 8613 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 8614 DAG.getConstant(4, MVT::i32)); 8615 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, 8616 LD->getPointerInfo(), LD->isVolatile(), 8617 LD->isNonTemporal(), LD->isInvariant(), 8618 std::min(4U, LD->getAlignment() / 2)); 8619 8620 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 8621 if (DCI.DAG.getTargetLoweringInfo().isBigEndian()) 8622 std::swap (NewLD1, NewLD2); 8623 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 8624 return Result; 8625 } 8626 8627 return SDValue(); 8628 } 8629 8630 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 8631 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 8632 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 8633 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 8634 SDValue Op0 = N->getOperand(0); 8635 SDValue Op1 = N->getOperand(1); 8636 if (Op0.getOpcode() == ISD::BITCAST) 8637 Op0 = Op0.getOperand(0); 8638 if (Op1.getOpcode() == ISD::BITCAST) 8639 Op1 = Op1.getOperand(0); 8640 if (Op0.getOpcode() == ARMISD::VMOVRRD && 8641 Op0.getNode() == Op1.getNode() && 8642 Op0.getResNo() == 0 && Op1.getResNo() == 1) 8643 return DAG.getNode(ISD::BITCAST, SDLoc(N), 8644 N->getValueType(0), Op0.getOperand(0)); 8645 return SDValue(); 8646 } 8647 8648 /// PerformSTORECombine - Target-specific dag combine xforms for 8649 /// ISD::STORE. 8650 static SDValue PerformSTORECombine(SDNode *N, 8651 TargetLowering::DAGCombinerInfo &DCI) { 8652 StoreSDNode *St = cast<StoreSDNode>(N); 8653 if (St->isVolatile()) 8654 return SDValue(); 8655 8656 // Optimize trunc store (of multiple scalars) to shuffle and store. First, 8657 // pack all of the elements in one place. Next, store to memory in fewer 8658 // chunks. 8659 SDValue StVal = St->getValue(); 8660 EVT VT = StVal.getValueType(); 8661 if (St->isTruncatingStore() && VT.isVector()) { 8662 SelectionDAG &DAG = DCI.DAG; 8663 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8664 EVT StVT = St->getMemoryVT(); 8665 unsigned NumElems = VT.getVectorNumElements(); 8666 assert(StVT != VT && "Cannot truncate to the same type"); 8667 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits(); 8668 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits(); 8669 8670 // From, To sizes and ElemCount must be pow of two 8671 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue(); 8672 8673 // We are going to use the original vector elt for storing. 8674 // Accumulated smaller vector elements must be a multiple of the store size. 8675 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue(); 8676 8677 unsigned SizeRatio = FromEltSz / ToEltSz; 8678 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits()); 8679 8680 // Create a type on which we perform the shuffle. 8681 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(), 8682 NumElems*SizeRatio); 8683 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); 8684 8685 SDLoc DL(St); 8686 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal); 8687 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); 8688 for (unsigned i = 0; i < NumElems; ++i) 8689 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio; 8690 8691 // Can't shuffle using an illegal type. 8692 if (!TLI.isTypeLegal(WideVecVT)) return SDValue(); 8693 8694 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec, 8695 DAG.getUNDEF(WideVec.getValueType()), 8696 ShuffleVec.data()); 8697 // At this point all of the data is stored at the bottom of the 8698 // register. We now need to save it to mem. 8699 8700 // Find the largest store unit 8701 MVT StoreType = MVT::i8; 8702 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE; 8703 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) { 8704 MVT Tp = (MVT::SimpleValueType)tp; 8705 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz) 8706 StoreType = Tp; 8707 } 8708 // Didn't find a legal store type. 8709 if (!TLI.isTypeLegal(StoreType)) 8710 return SDValue(); 8711 8712 // Bitcast the original vector into a vector of store-size units 8713 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(), 8714 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits()); 8715 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); 8716 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff); 8717 SmallVector<SDValue, 8> Chains; 8718 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8, 8719 TLI.getPointerTy()); 8720 SDValue BasePtr = St->getBasePtr(); 8721 8722 // Perform one or more big stores into memory. 8723 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits(); 8724 for (unsigned I = 0; I < E; I++) { 8725 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, 8726 StoreType, ShuffWide, 8727 DAG.getIntPtrConstant(I)); 8728 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr, 8729 St->getPointerInfo(), St->isVolatile(), 8730 St->isNonTemporal(), St->getAlignment()); 8731 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, 8732 Increment); 8733 Chains.push_back(Ch); 8734 } 8735 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 8736 } 8737 8738 if (!ISD::isNormalStore(St)) 8739 return SDValue(); 8740 8741 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and 8742 // ARM stores of arguments in the same cache line. 8743 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 8744 StVal.getNode()->hasOneUse()) { 8745 SelectionDAG &DAG = DCI.DAG; 8746 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian(); 8747 SDLoc DL(St); 8748 SDValue BasePtr = St->getBasePtr(); 8749 SDValue NewST1 = DAG.getStore(St->getChain(), DL, 8750 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ), 8751 BasePtr, St->getPointerInfo(), St->isVolatile(), 8752 St->isNonTemporal(), St->getAlignment()); 8753 8754 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 8755 DAG.getConstant(4, MVT::i32)); 8756 return DAG.getStore(NewST1.getValue(0), DL, 8757 StVal.getNode()->getOperand(isBigEndian ? 0 : 1), 8758 OffsetPtr, St->getPointerInfo(), St->isVolatile(), 8759 St->isNonTemporal(), 8760 std::min(4U, St->getAlignment() / 2)); 8761 } 8762 8763 if (StVal.getValueType() != MVT::i64 || 8764 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 8765 return SDValue(); 8766 8767 // Bitcast an i64 store extracted from a vector to f64. 8768 // Otherwise, the i64 value will be legalized to a pair of i32 values. 8769 SelectionDAG &DAG = DCI.DAG; 8770 SDLoc dl(StVal); 8771 SDValue IntVec = StVal.getOperand(0); 8772 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 8773 IntVec.getValueType().getVectorNumElements()); 8774 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 8775 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 8776 Vec, StVal.getOperand(1)); 8777 dl = SDLoc(N); 8778 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 8779 // Make the DAGCombiner fold the bitcasts. 8780 DCI.AddToWorklist(Vec.getNode()); 8781 DCI.AddToWorklist(ExtElt.getNode()); 8782 DCI.AddToWorklist(V.getNode()); 8783 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 8784 St->getPointerInfo(), St->isVolatile(), 8785 St->isNonTemporal(), St->getAlignment(), 8786 St->getAAInfo()); 8787 } 8788 8789 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 8790 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 8791 /// i64 vector to have f64 elements, since the value can then be loaded 8792 /// directly into a VFP register. 8793 static bool hasNormalLoadOperand(SDNode *N) { 8794 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 8795 for (unsigned i = 0; i < NumElts; ++i) { 8796 SDNode *Elt = N->getOperand(i).getNode(); 8797 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 8798 return true; 8799 } 8800 return false; 8801 } 8802 8803 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 8804 /// ISD::BUILD_VECTOR. 8805 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 8806 TargetLowering::DAGCombinerInfo &DCI, 8807 const ARMSubtarget *Subtarget) { 8808 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 8809 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 8810 // into a pair of GPRs, which is fine when the value is used as a scalar, 8811 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 8812 SelectionDAG &DAG = DCI.DAG; 8813 if (N->getNumOperands() == 2) { 8814 SDValue RV = PerformVMOVDRRCombine(N, DAG); 8815 if (RV.getNode()) 8816 return RV; 8817 } 8818 8819 // Load i64 elements as f64 values so that type legalization does not split 8820 // them up into i32 values. 8821 EVT VT = N->getValueType(0); 8822 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 8823 return SDValue(); 8824 SDLoc dl(N); 8825 SmallVector<SDValue, 8> Ops; 8826 unsigned NumElts = VT.getVectorNumElements(); 8827 for (unsigned i = 0; i < NumElts; ++i) { 8828 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 8829 Ops.push_back(V); 8830 // Make the DAGCombiner fold the bitcast. 8831 DCI.AddToWorklist(V.getNode()); 8832 } 8833 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 8834 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops); 8835 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 8836 } 8837 8838 /// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR. 8839 static SDValue 8840 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 8841 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR. 8842 // At that time, we may have inserted bitcasts from integer to float. 8843 // If these bitcasts have survived DAGCombine, change the lowering of this 8844 // BUILD_VECTOR in something more vector friendly, i.e., that does not 8845 // force to use floating point types. 8846 8847 // Make sure we can change the type of the vector. 8848 // This is possible iff: 8849 // 1. The vector is only used in a bitcast to a integer type. I.e., 8850 // 1.1. Vector is used only once. 8851 // 1.2. Use is a bit convert to an integer type. 8852 // 2. The size of its operands are 32-bits (64-bits are not legal). 8853 EVT VT = N->getValueType(0); 8854 EVT EltVT = VT.getVectorElementType(); 8855 8856 // Check 1.1. and 2. 8857 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse()) 8858 return SDValue(); 8859 8860 // By construction, the input type must be float. 8861 assert(EltVT == MVT::f32 && "Unexpected type!"); 8862 8863 // Check 1.2. 8864 SDNode *Use = *N->use_begin(); 8865 if (Use->getOpcode() != ISD::BITCAST || 8866 Use->getValueType(0).isFloatingPoint()) 8867 return SDValue(); 8868 8869 // Check profitability. 8870 // Model is, if more than half of the relevant operands are bitcast from 8871 // i32, turn the build_vector into a sequence of insert_vector_elt. 8872 // Relevant operands are everything that is not statically 8873 // (i.e., at compile time) bitcasted. 8874 unsigned NumOfBitCastedElts = 0; 8875 unsigned NumElts = VT.getVectorNumElements(); 8876 unsigned NumOfRelevantElts = NumElts; 8877 for (unsigned Idx = 0; Idx < NumElts; ++Idx) { 8878 SDValue Elt = N->getOperand(Idx); 8879 if (Elt->getOpcode() == ISD::BITCAST) { 8880 // Assume only bit cast to i32 will go away. 8881 if (Elt->getOperand(0).getValueType() == MVT::i32) 8882 ++NumOfBitCastedElts; 8883 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt)) 8884 // Constants are statically casted, thus do not count them as 8885 // relevant operands. 8886 --NumOfRelevantElts; 8887 } 8888 8889 // Check if more than half of the elements require a non-free bitcast. 8890 if (NumOfBitCastedElts <= NumOfRelevantElts / 2) 8891 return SDValue(); 8892 8893 SelectionDAG &DAG = DCI.DAG; 8894 // Create the new vector type. 8895 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 8896 // Check if the type is legal. 8897 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8898 if (!TLI.isTypeLegal(VecVT)) 8899 return SDValue(); 8900 8901 // Combine: 8902 // ARMISD::BUILD_VECTOR E1, E2, ..., EN. 8903 // => BITCAST INSERT_VECTOR_ELT 8904 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1), 8905 // (BITCAST EN), N. 8906 SDValue Vec = DAG.getUNDEF(VecVT); 8907 SDLoc dl(N); 8908 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) { 8909 SDValue V = N->getOperand(Idx); 8910 if (V.getOpcode() == ISD::UNDEF) 8911 continue; 8912 if (V.getOpcode() == ISD::BITCAST && 8913 V->getOperand(0).getValueType() == MVT::i32) 8914 // Fold obvious case. 8915 V = V.getOperand(0); 8916 else { 8917 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V); 8918 // Make the DAGCombiner fold the bitcasts. 8919 DCI.AddToWorklist(V.getNode()); 8920 } 8921 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32); 8922 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx); 8923 } 8924 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec); 8925 // Make the DAGCombiner fold the bitcasts. 8926 DCI.AddToWorklist(Vec.getNode()); 8927 return Vec; 8928 } 8929 8930 /// PerformInsertEltCombine - Target-specific dag combine xforms for 8931 /// ISD::INSERT_VECTOR_ELT. 8932 static SDValue PerformInsertEltCombine(SDNode *N, 8933 TargetLowering::DAGCombinerInfo &DCI) { 8934 // Bitcast an i64 load inserted into a vector to f64. 8935 // Otherwise, the i64 value will be legalized to a pair of i32 values. 8936 EVT VT = N->getValueType(0); 8937 SDNode *Elt = N->getOperand(1).getNode(); 8938 if (VT.getVectorElementType() != MVT::i64 || 8939 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 8940 return SDValue(); 8941 8942 SelectionDAG &DAG = DCI.DAG; 8943 SDLoc dl(N); 8944 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 8945 VT.getVectorNumElements()); 8946 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 8947 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 8948 // Make the DAGCombiner fold the bitcasts. 8949 DCI.AddToWorklist(Vec.getNode()); 8950 DCI.AddToWorklist(V.getNode()); 8951 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 8952 Vec, V, N->getOperand(2)); 8953 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 8954 } 8955 8956 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 8957 /// ISD::VECTOR_SHUFFLE. 8958 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 8959 // The LLVM shufflevector instruction does not require the shuffle mask 8960 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 8961 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 8962 // operands do not match the mask length, they are extended by concatenating 8963 // them with undef vectors. That is probably the right thing for other 8964 // targets, but for NEON it is better to concatenate two double-register 8965 // size vector operands into a single quad-register size vector. Do that 8966 // transformation here: 8967 // shuffle(concat(v1, undef), concat(v2, undef)) -> 8968 // shuffle(concat(v1, v2), undef) 8969 SDValue Op0 = N->getOperand(0); 8970 SDValue Op1 = N->getOperand(1); 8971 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 8972 Op1.getOpcode() != ISD::CONCAT_VECTORS || 8973 Op0.getNumOperands() != 2 || 8974 Op1.getNumOperands() != 2) 8975 return SDValue(); 8976 SDValue Concat0Op1 = Op0.getOperand(1); 8977 SDValue Concat1Op1 = Op1.getOperand(1); 8978 if (Concat0Op1.getOpcode() != ISD::UNDEF || 8979 Concat1Op1.getOpcode() != ISD::UNDEF) 8980 return SDValue(); 8981 // Skip the transformation if any of the types are illegal. 8982 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8983 EVT VT = N->getValueType(0); 8984 if (!TLI.isTypeLegal(VT) || 8985 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 8986 !TLI.isTypeLegal(Concat1Op1.getValueType())) 8987 return SDValue(); 8988 8989 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, 8990 Op0.getOperand(0), Op1.getOperand(0)); 8991 // Translate the shuffle mask. 8992 SmallVector<int, 16> NewMask; 8993 unsigned NumElts = VT.getVectorNumElements(); 8994 unsigned HalfElts = NumElts/2; 8995 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 8996 for (unsigned n = 0; n < NumElts; ++n) { 8997 int MaskElt = SVN->getMaskElt(n); 8998 int NewElt = -1; 8999 if (MaskElt < (int)HalfElts) 9000 NewElt = MaskElt; 9001 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 9002 NewElt = HalfElts + MaskElt - NumElts; 9003 NewMask.push_back(NewElt); 9004 } 9005 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat, 9006 DAG.getUNDEF(VT), NewMask.data()); 9007 } 9008 9009 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and 9010 /// NEON load/store intrinsics to merge base address updates. 9011 static SDValue CombineBaseUpdate(SDNode *N, 9012 TargetLowering::DAGCombinerInfo &DCI) { 9013 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 9014 return SDValue(); 9015 9016 SelectionDAG &DAG = DCI.DAG; 9017 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 9018 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 9019 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1); 9020 SDValue Addr = N->getOperand(AddrOpIdx); 9021 9022 // Search for a use of the address operand that is an increment. 9023 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 9024 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 9025 SDNode *User = *UI; 9026 if (User->getOpcode() != ISD::ADD || 9027 UI.getUse().getResNo() != Addr.getResNo()) 9028 continue; 9029 9030 // Check that the add is independent of the load/store. Otherwise, folding 9031 // it would create a cycle. 9032 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 9033 continue; 9034 9035 // Find the new opcode for the updating load/store. 9036 bool isLoad = true; 9037 bool isLaneOp = false; 9038 unsigned NewOpc = 0; 9039 unsigned NumVecs = 0; 9040 if (isIntrinsic) { 9041 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 9042 switch (IntNo) { 9043 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 9044 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 9045 NumVecs = 1; break; 9046 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 9047 NumVecs = 2; break; 9048 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 9049 NumVecs = 3; break; 9050 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 9051 NumVecs = 4; break; 9052 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 9053 NumVecs = 2; isLaneOp = true; break; 9054 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 9055 NumVecs = 3; isLaneOp = true; break; 9056 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 9057 NumVecs = 4; isLaneOp = true; break; 9058 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 9059 NumVecs = 1; isLoad = false; break; 9060 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 9061 NumVecs = 2; isLoad = false; break; 9062 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 9063 NumVecs = 3; isLoad = false; break; 9064 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 9065 NumVecs = 4; isLoad = false; break; 9066 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 9067 NumVecs = 2; isLoad = false; isLaneOp = true; break; 9068 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 9069 NumVecs = 3; isLoad = false; isLaneOp = true; break; 9070 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 9071 NumVecs = 4; isLoad = false; isLaneOp = true; break; 9072 } 9073 } else { 9074 isLaneOp = true; 9075 switch (N->getOpcode()) { 9076 default: llvm_unreachable("unexpected opcode for Neon base update"); 9077 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 9078 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 9079 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 9080 } 9081 } 9082 9083 // Find the size of memory referenced by the load/store. 9084 EVT VecTy; 9085 if (isLoad) 9086 VecTy = N->getValueType(0); 9087 else 9088 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 9089 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 9090 if (isLaneOp) 9091 NumBytes /= VecTy.getVectorNumElements(); 9092 9093 // If the increment is a constant, it must match the memory ref size. 9094 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 9095 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 9096 uint64_t IncVal = CInc->getZExtValue(); 9097 if (IncVal != NumBytes) 9098 continue; 9099 } else if (NumBytes >= 3 * 16) { 9100 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 9101 // separate instructions that make it harder to use a non-constant update. 9102 continue; 9103 } 9104 9105 // Create the new updating load/store node. 9106 EVT Tys[6]; 9107 unsigned NumResultVecs = (isLoad ? NumVecs : 0); 9108 unsigned n; 9109 for (n = 0; n < NumResultVecs; ++n) 9110 Tys[n] = VecTy; 9111 Tys[n++] = MVT::i32; 9112 Tys[n] = MVT::Other; 9113 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2)); 9114 SmallVector<SDValue, 8> Ops; 9115 Ops.push_back(N->getOperand(0)); // incoming chain 9116 Ops.push_back(N->getOperand(AddrOpIdx)); 9117 Ops.push_back(Inc); 9118 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) { 9119 Ops.push_back(N->getOperand(i)); 9120 } 9121 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N); 9122 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys, 9123 Ops, MemInt->getMemoryVT(), 9124 MemInt->getMemOperand()); 9125 9126 // Update the uses. 9127 std::vector<SDValue> NewResults; 9128 for (unsigned i = 0; i < NumResultVecs; ++i) { 9129 NewResults.push_back(SDValue(UpdN.getNode(), i)); 9130 } 9131 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 9132 DCI.CombineTo(N, NewResults); 9133 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 9134 9135 break; 9136 } 9137 return SDValue(); 9138 } 9139 9140 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 9141 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 9142 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 9143 /// return true. 9144 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 9145 SelectionDAG &DAG = DCI.DAG; 9146 EVT VT = N->getValueType(0); 9147 // vldN-dup instructions only support 64-bit vectors for N > 1. 9148 if (!VT.is64BitVector()) 9149 return false; 9150 9151 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 9152 SDNode *VLD = N->getOperand(0).getNode(); 9153 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 9154 return false; 9155 unsigned NumVecs = 0; 9156 unsigned NewOpc = 0; 9157 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 9158 if (IntNo == Intrinsic::arm_neon_vld2lane) { 9159 NumVecs = 2; 9160 NewOpc = ARMISD::VLD2DUP; 9161 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 9162 NumVecs = 3; 9163 NewOpc = ARMISD::VLD3DUP; 9164 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 9165 NumVecs = 4; 9166 NewOpc = ARMISD::VLD4DUP; 9167 } else { 9168 return false; 9169 } 9170 9171 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 9172 // numbers match the load. 9173 unsigned VLDLaneNo = 9174 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 9175 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 9176 UI != UE; ++UI) { 9177 // Ignore uses of the chain result. 9178 if (UI.getUse().getResNo() == NumVecs) 9179 continue; 9180 SDNode *User = *UI; 9181 if (User->getOpcode() != ARMISD::VDUPLANE || 9182 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 9183 return false; 9184 } 9185 9186 // Create the vldN-dup node. 9187 EVT Tys[5]; 9188 unsigned n; 9189 for (n = 0; n < NumVecs; ++n) 9190 Tys[n] = VT; 9191 Tys[n] = MVT::Other; 9192 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1)); 9193 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 9194 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 9195 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, 9196 Ops, VLDMemInt->getMemoryVT(), 9197 VLDMemInt->getMemOperand()); 9198 9199 // Update the uses. 9200 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 9201 UI != UE; ++UI) { 9202 unsigned ResNo = UI.getUse().getResNo(); 9203 // Ignore uses of the chain result. 9204 if (ResNo == NumVecs) 9205 continue; 9206 SDNode *User = *UI; 9207 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 9208 } 9209 9210 // Now the vldN-lane intrinsic is dead except for its chain result. 9211 // Update uses of the chain. 9212 std::vector<SDValue> VLDDupResults; 9213 for (unsigned n = 0; n < NumVecs; ++n) 9214 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 9215 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 9216 DCI.CombineTo(VLD, VLDDupResults); 9217 9218 return true; 9219 } 9220 9221 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 9222 /// ARMISD::VDUPLANE. 9223 static SDValue PerformVDUPLANECombine(SDNode *N, 9224 TargetLowering::DAGCombinerInfo &DCI) { 9225 SDValue Op = N->getOperand(0); 9226 9227 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 9228 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 9229 if (CombineVLDDUP(N, DCI)) 9230 return SDValue(N, 0); 9231 9232 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 9233 // redundant. Ignore bit_converts for now; element sizes are checked below. 9234 while (Op.getOpcode() == ISD::BITCAST) 9235 Op = Op.getOperand(0); 9236 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 9237 return SDValue(); 9238 9239 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 9240 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits(); 9241 // The canonical VMOV for a zero vector uses a 32-bit element size. 9242 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 9243 unsigned EltBits; 9244 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0) 9245 EltSize = 8; 9246 EVT VT = N->getValueType(0); 9247 if (EltSize > VT.getVectorElementType().getSizeInBits()) 9248 return SDValue(); 9249 9250 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 9251 } 9252 9253 // isConstVecPow2 - Return true if each vector element is a power of 2, all 9254 // elements are the same constant, C, and Log2(C) ranges from 1 to 32. 9255 static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C) 9256 { 9257 integerPart cN; 9258 integerPart c0 = 0; 9259 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements(); 9260 I != E; I++) { 9261 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I)); 9262 if (!C) 9263 return false; 9264 9265 bool isExact; 9266 APFloat APF = C->getValueAPF(); 9267 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact) 9268 != APFloat::opOK || !isExact) 9269 return false; 9270 9271 c0 = (I == 0) ? cN : c0; 9272 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32) 9273 return false; 9274 } 9275 C = c0; 9276 return true; 9277 } 9278 9279 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 9280 /// can replace combinations of VMUL and VCVT (floating-point to integer) 9281 /// when the VMUL has a constant operand that is a power of 2. 9282 /// 9283 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 9284 /// vmul.f32 d16, d17, d16 9285 /// vcvt.s32.f32 d16, d16 9286 /// becomes: 9287 /// vcvt.s32.f32 d16, d16, #3 9288 static SDValue PerformVCVTCombine(SDNode *N, 9289 TargetLowering::DAGCombinerInfo &DCI, 9290 const ARMSubtarget *Subtarget) { 9291 SelectionDAG &DAG = DCI.DAG; 9292 SDValue Op = N->getOperand(0); 9293 9294 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() || 9295 Op.getOpcode() != ISD::FMUL) 9296 return SDValue(); 9297 9298 uint64_t C; 9299 SDValue N0 = Op->getOperand(0); 9300 SDValue ConstVec = Op->getOperand(1); 9301 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 9302 9303 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 9304 !isConstVecPow2(ConstVec, isSigned, C)) 9305 return SDValue(); 9306 9307 MVT FloatTy = Op.getSimpleValueType().getVectorElementType(); 9308 MVT IntTy = N->getSimpleValueType(0).getVectorElementType(); 9309 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) { 9310 // These instructions only exist converting from f32 to i32. We can handle 9311 // smaller integers by generating an extra truncate, but larger ones would 9312 // be lossy. 9313 return SDValue(); 9314 } 9315 9316 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 9317 Intrinsic::arm_neon_vcvtfp2fxu; 9318 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 9319 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), 9320 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 9321 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0, 9322 DAG.getConstant(Log2_64(C), MVT::i32)); 9323 9324 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits()) 9325 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv); 9326 9327 return FixConv; 9328 } 9329 9330 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 9331 /// can replace combinations of VCVT (integer to floating-point) and VDIV 9332 /// when the VDIV has a constant operand that is a power of 2. 9333 /// 9334 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 9335 /// vcvt.f32.s32 d16, d16 9336 /// vdiv.f32 d16, d17, d16 9337 /// becomes: 9338 /// vcvt.f32.s32 d16, d16, #3 9339 static SDValue PerformVDIVCombine(SDNode *N, 9340 TargetLowering::DAGCombinerInfo &DCI, 9341 const ARMSubtarget *Subtarget) { 9342 SelectionDAG &DAG = DCI.DAG; 9343 SDValue Op = N->getOperand(0); 9344 unsigned OpOpcode = Op.getNode()->getOpcode(); 9345 9346 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() || 9347 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 9348 return SDValue(); 9349 9350 uint64_t C; 9351 SDValue ConstVec = N->getOperand(1); 9352 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 9353 9354 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 9355 !isConstVecPow2(ConstVec, isSigned, C)) 9356 return SDValue(); 9357 9358 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType(); 9359 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType(); 9360 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) { 9361 // These instructions only exist converting from i32 to f32. We can handle 9362 // smaller integers by generating an extra extend, but larger ones would 9363 // be lossy. 9364 return SDValue(); 9365 } 9366 9367 SDValue ConvInput = Op.getOperand(0); 9368 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 9369 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits()) 9370 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 9371 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 9372 ConvInput); 9373 9374 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 9375 Intrinsic::arm_neon_vcvtfxu2fp; 9376 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), 9377 Op.getValueType(), 9378 DAG.getConstant(IntrinsicOpcode, MVT::i32), 9379 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32)); 9380 } 9381 9382 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 9383 /// operand of a vector shift operation, where all the elements of the 9384 /// build_vector must have the same constant integer value. 9385 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 9386 // Ignore bit_converts. 9387 while (Op.getOpcode() == ISD::BITCAST) 9388 Op = Op.getOperand(0); 9389 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 9390 APInt SplatBits, SplatUndef; 9391 unsigned SplatBitSize; 9392 bool HasAnyUndefs; 9393 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 9394 HasAnyUndefs, ElementBits) || 9395 SplatBitSize > ElementBits) 9396 return false; 9397 Cnt = SplatBits.getSExtValue(); 9398 return true; 9399 } 9400 9401 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 9402 /// operand of a vector shift left operation. That value must be in the range: 9403 /// 0 <= Value < ElementBits for a left shift; or 9404 /// 0 <= Value <= ElementBits for a long left shift. 9405 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 9406 assert(VT.isVector() && "vector shift count is not a vector type"); 9407 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 9408 if (! getVShiftImm(Op, ElementBits, Cnt)) 9409 return false; 9410 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits); 9411 } 9412 9413 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 9414 /// operand of a vector shift right operation. For a shift opcode, the value 9415 /// is positive, but for an intrinsic the value count must be negative. The 9416 /// absolute value must be in the range: 9417 /// 1 <= |Value| <= ElementBits for a right shift; or 9418 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 9419 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 9420 int64_t &Cnt) { 9421 assert(VT.isVector() && "vector shift count is not a vector type"); 9422 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 9423 if (! getVShiftImm(Op, ElementBits, Cnt)) 9424 return false; 9425 if (isIntrinsic) 9426 Cnt = -Cnt; 9427 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits)); 9428 } 9429 9430 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 9431 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 9432 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 9433 switch (IntNo) { 9434 default: 9435 // Don't do anything for most intrinsics. 9436 break; 9437 9438 // Vector shifts: check for immediate versions and lower them. 9439 // Note: This is done during DAG combining instead of DAG legalizing because 9440 // the build_vectors for 64-bit vector element shift counts are generally 9441 // not legal, and it is hard to see their values after they get legalized to 9442 // loads from a constant pool. 9443 case Intrinsic::arm_neon_vshifts: 9444 case Intrinsic::arm_neon_vshiftu: 9445 case Intrinsic::arm_neon_vrshifts: 9446 case Intrinsic::arm_neon_vrshiftu: 9447 case Intrinsic::arm_neon_vrshiftn: 9448 case Intrinsic::arm_neon_vqshifts: 9449 case Intrinsic::arm_neon_vqshiftu: 9450 case Intrinsic::arm_neon_vqshiftsu: 9451 case Intrinsic::arm_neon_vqshiftns: 9452 case Intrinsic::arm_neon_vqshiftnu: 9453 case Intrinsic::arm_neon_vqshiftnsu: 9454 case Intrinsic::arm_neon_vqrshiftns: 9455 case Intrinsic::arm_neon_vqrshiftnu: 9456 case Intrinsic::arm_neon_vqrshiftnsu: { 9457 EVT VT = N->getOperand(1).getValueType(); 9458 int64_t Cnt; 9459 unsigned VShiftOpc = 0; 9460 9461 switch (IntNo) { 9462 case Intrinsic::arm_neon_vshifts: 9463 case Intrinsic::arm_neon_vshiftu: 9464 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 9465 VShiftOpc = ARMISD::VSHL; 9466 break; 9467 } 9468 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 9469 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? 9470 ARMISD::VSHRs : ARMISD::VSHRu); 9471 break; 9472 } 9473 return SDValue(); 9474 9475 case Intrinsic::arm_neon_vrshifts: 9476 case Intrinsic::arm_neon_vrshiftu: 9477 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 9478 break; 9479 return SDValue(); 9480 9481 case Intrinsic::arm_neon_vqshifts: 9482 case Intrinsic::arm_neon_vqshiftu: 9483 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 9484 break; 9485 return SDValue(); 9486 9487 case Intrinsic::arm_neon_vqshiftsu: 9488 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 9489 break; 9490 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 9491 9492 case Intrinsic::arm_neon_vrshiftn: 9493 case Intrinsic::arm_neon_vqshiftns: 9494 case Intrinsic::arm_neon_vqshiftnu: 9495 case Intrinsic::arm_neon_vqshiftnsu: 9496 case Intrinsic::arm_neon_vqrshiftns: 9497 case Intrinsic::arm_neon_vqrshiftnu: 9498 case Intrinsic::arm_neon_vqrshiftnsu: 9499 // Narrowing shifts require an immediate right shift. 9500 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 9501 break; 9502 llvm_unreachable("invalid shift count for narrowing vector shift " 9503 "intrinsic"); 9504 9505 default: 9506 llvm_unreachable("unhandled vector shift"); 9507 } 9508 9509 switch (IntNo) { 9510 case Intrinsic::arm_neon_vshifts: 9511 case Intrinsic::arm_neon_vshiftu: 9512 // Opcode already set above. 9513 break; 9514 case Intrinsic::arm_neon_vrshifts: 9515 VShiftOpc = ARMISD::VRSHRs; break; 9516 case Intrinsic::arm_neon_vrshiftu: 9517 VShiftOpc = ARMISD::VRSHRu; break; 9518 case Intrinsic::arm_neon_vrshiftn: 9519 VShiftOpc = ARMISD::VRSHRN; break; 9520 case Intrinsic::arm_neon_vqshifts: 9521 VShiftOpc = ARMISD::VQSHLs; break; 9522 case Intrinsic::arm_neon_vqshiftu: 9523 VShiftOpc = ARMISD::VQSHLu; break; 9524 case Intrinsic::arm_neon_vqshiftsu: 9525 VShiftOpc = ARMISD::VQSHLsu; break; 9526 case Intrinsic::arm_neon_vqshiftns: 9527 VShiftOpc = ARMISD::VQSHRNs; break; 9528 case Intrinsic::arm_neon_vqshiftnu: 9529 VShiftOpc = ARMISD::VQSHRNu; break; 9530 case Intrinsic::arm_neon_vqshiftnsu: 9531 VShiftOpc = ARMISD::VQSHRNsu; break; 9532 case Intrinsic::arm_neon_vqrshiftns: 9533 VShiftOpc = ARMISD::VQRSHRNs; break; 9534 case Intrinsic::arm_neon_vqrshiftnu: 9535 VShiftOpc = ARMISD::VQRSHRNu; break; 9536 case Intrinsic::arm_neon_vqrshiftnsu: 9537 VShiftOpc = ARMISD::VQRSHRNsu; break; 9538 } 9539 9540 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0), 9541 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32)); 9542 } 9543 9544 case Intrinsic::arm_neon_vshiftins: { 9545 EVT VT = N->getOperand(1).getValueType(); 9546 int64_t Cnt; 9547 unsigned VShiftOpc = 0; 9548 9549 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 9550 VShiftOpc = ARMISD::VSLI; 9551 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 9552 VShiftOpc = ARMISD::VSRI; 9553 else { 9554 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 9555 } 9556 9557 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0), 9558 N->getOperand(1), N->getOperand(2), 9559 DAG.getConstant(Cnt, MVT::i32)); 9560 } 9561 9562 case Intrinsic::arm_neon_vqrshifts: 9563 case Intrinsic::arm_neon_vqrshiftu: 9564 // No immediate versions of these to check for. 9565 break; 9566 } 9567 9568 return SDValue(); 9569 } 9570 9571 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 9572 /// lowers them. As with the vector shift intrinsics, this is done during DAG 9573 /// combining instead of DAG legalizing because the build_vectors for 64-bit 9574 /// vector element shift counts are generally not legal, and it is hard to see 9575 /// their values after they get legalized to loads from a constant pool. 9576 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG, 9577 const ARMSubtarget *ST) { 9578 EVT VT = N->getValueType(0); 9579 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) { 9580 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high 9581 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16. 9582 SDValue N1 = N->getOperand(1); 9583 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 9584 SDValue N0 = N->getOperand(0); 9585 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP && 9586 DAG.MaskedValueIsZero(N0.getOperand(0), 9587 APInt::getHighBitsSet(32, 16))) 9588 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1); 9589 } 9590 } 9591 9592 // Nothing to be done for scalar shifts. 9593 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9594 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 9595 return SDValue(); 9596 9597 assert(ST->hasNEON() && "unexpected vector shift"); 9598 int64_t Cnt; 9599 9600 switch (N->getOpcode()) { 9601 default: llvm_unreachable("unexpected shift opcode"); 9602 9603 case ISD::SHL: 9604 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) 9605 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0), 9606 DAG.getConstant(Cnt, MVT::i32)); 9607 break; 9608 9609 case ISD::SRA: 9610 case ISD::SRL: 9611 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 9612 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ? 9613 ARMISD::VSHRs : ARMISD::VSHRu); 9614 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0), 9615 DAG.getConstant(Cnt, MVT::i32)); 9616 } 9617 } 9618 return SDValue(); 9619 } 9620 9621 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 9622 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 9623 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 9624 const ARMSubtarget *ST) { 9625 SDValue N0 = N->getOperand(0); 9626 9627 // Check for sign- and zero-extensions of vector extract operations of 8- 9628 // and 16-bit vector elements. NEON supports these directly. They are 9629 // handled during DAG combining because type legalization will promote them 9630 // to 32-bit types and it is messy to recognize the operations after that. 9631 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 9632 SDValue Vec = N0.getOperand(0); 9633 SDValue Lane = N0.getOperand(1); 9634 EVT VT = N->getValueType(0); 9635 EVT EltVT = N0.getValueType(); 9636 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9637 9638 if (VT == MVT::i32 && 9639 (EltVT == MVT::i8 || EltVT == MVT::i16) && 9640 TLI.isTypeLegal(Vec.getValueType()) && 9641 isa<ConstantSDNode>(Lane)) { 9642 9643 unsigned Opc = 0; 9644 switch (N->getOpcode()) { 9645 default: llvm_unreachable("unexpected opcode"); 9646 case ISD::SIGN_EXTEND: 9647 Opc = ARMISD::VGETLANEs; 9648 break; 9649 case ISD::ZERO_EXTEND: 9650 case ISD::ANY_EXTEND: 9651 Opc = ARMISD::VGETLANEu; 9652 break; 9653 } 9654 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane); 9655 } 9656 } 9657 9658 return SDValue(); 9659 } 9660 9661 /// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC 9662 /// to match f32 max/min patterns to use NEON vmax/vmin instructions. 9663 static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG, 9664 const ARMSubtarget *ST) { 9665 // If the target supports NEON, try to use vmax/vmin instructions for f32 9666 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set, 9667 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is 9668 // a NaN; only do the transformation when it matches that behavior. 9669 9670 // For now only do this when using NEON for FP operations; if using VFP, it 9671 // is not obvious that the benefit outweighs the cost of switching to the 9672 // NEON pipeline. 9673 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() || 9674 N->getValueType(0) != MVT::f32) 9675 return SDValue(); 9676 9677 SDValue CondLHS = N->getOperand(0); 9678 SDValue CondRHS = N->getOperand(1); 9679 SDValue LHS = N->getOperand(2); 9680 SDValue RHS = N->getOperand(3); 9681 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 9682 9683 unsigned Opcode = 0; 9684 bool IsReversed; 9685 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) { 9686 IsReversed = false; // x CC y ? x : y 9687 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) { 9688 IsReversed = true ; // x CC y ? y : x 9689 } else { 9690 return SDValue(); 9691 } 9692 9693 bool IsUnordered; 9694 switch (CC) { 9695 default: break; 9696 case ISD::SETOLT: 9697 case ISD::SETOLE: 9698 case ISD::SETLT: 9699 case ISD::SETLE: 9700 case ISD::SETULT: 9701 case ISD::SETULE: 9702 // If LHS is NaN, an ordered comparison will be false and the result will 9703 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS 9704 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 9705 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE); 9706 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 9707 break; 9708 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin 9709 // will return -0, so vmin can only be used for unsafe math or if one of 9710 // the operands is known to be nonzero. 9711 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) && 9712 !DAG.getTarget().Options.UnsafeFPMath && 9713 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 9714 break; 9715 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN; 9716 break; 9717 9718 case ISD::SETOGT: 9719 case ISD::SETOGE: 9720 case ISD::SETGT: 9721 case ISD::SETGE: 9722 case ISD::SETUGT: 9723 case ISD::SETUGE: 9724 // If LHS is NaN, an ordered comparison will be false and the result will 9725 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS 9726 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 9727 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE); 9728 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 9729 break; 9730 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax 9731 // will return +0, so vmax can only be used for unsafe math or if one of 9732 // the operands is known to be nonzero. 9733 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) && 9734 !DAG.getTarget().Options.UnsafeFPMath && 9735 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 9736 break; 9737 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX; 9738 break; 9739 } 9740 9741 if (!Opcode) 9742 return SDValue(); 9743 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS); 9744 } 9745 9746 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 9747 SDValue 9748 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 9749 SDValue Cmp = N->getOperand(4); 9750 if (Cmp.getOpcode() != ARMISD::CMPZ) 9751 // Only looking at EQ and NE cases. 9752 return SDValue(); 9753 9754 EVT VT = N->getValueType(0); 9755 SDLoc dl(N); 9756 SDValue LHS = Cmp.getOperand(0); 9757 SDValue RHS = Cmp.getOperand(1); 9758 SDValue FalseVal = N->getOperand(0); 9759 SDValue TrueVal = N->getOperand(1); 9760 SDValue ARMcc = N->getOperand(2); 9761 ARMCC::CondCodes CC = 9762 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 9763 9764 // Simplify 9765 // mov r1, r0 9766 // cmp r1, x 9767 // mov r0, y 9768 // moveq r0, x 9769 // to 9770 // cmp r0, x 9771 // movne r0, y 9772 // 9773 // mov r1, r0 9774 // cmp r1, x 9775 // mov r0, x 9776 // movne r0, y 9777 // to 9778 // cmp r0, x 9779 // movne r0, y 9780 /// FIXME: Turn this into a target neutral optimization? 9781 SDValue Res; 9782 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 9783 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 9784 N->getOperand(3), Cmp); 9785 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 9786 SDValue ARMcc; 9787 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 9788 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 9789 N->getOperand(3), NewCmp); 9790 } 9791 9792 if (Res.getNode()) { 9793 APInt KnownZero, KnownOne; 9794 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne); 9795 // Capture demanded bits information that would be otherwise lost. 9796 if (KnownZero == 0xfffffffe) 9797 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9798 DAG.getValueType(MVT::i1)); 9799 else if (KnownZero == 0xffffff00) 9800 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9801 DAG.getValueType(MVT::i8)); 9802 else if (KnownZero == 0xffff0000) 9803 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9804 DAG.getValueType(MVT::i16)); 9805 } 9806 9807 return Res; 9808 } 9809 9810 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 9811 DAGCombinerInfo &DCI) const { 9812 switch (N->getOpcode()) { 9813 default: break; 9814 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget); 9815 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 9816 case ISD::SUB: return PerformSUBCombine(N, DCI); 9817 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 9818 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 9819 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget); 9820 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget); 9821 case ARMISD::BFI: return PerformBFICombine(N, DCI); 9822 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget); 9823 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 9824 case ISD::STORE: return PerformSTORECombine(N, DCI); 9825 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget); 9826 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 9827 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 9828 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 9829 case ISD::FP_TO_SINT: 9830 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget); 9831 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget); 9832 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 9833 case ISD::SHL: 9834 case ISD::SRA: 9835 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget); 9836 case ISD::SIGN_EXTEND: 9837 case ISD::ZERO_EXTEND: 9838 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 9839 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget); 9840 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 9841 case ARMISD::VLD2DUP: 9842 case ARMISD::VLD3DUP: 9843 case ARMISD::VLD4DUP: 9844 return CombineBaseUpdate(N, DCI); 9845 case ARMISD::BUILD_VECTOR: 9846 return PerformARMBUILD_VECTORCombine(N, DCI); 9847 case ISD::INTRINSIC_VOID: 9848 case ISD::INTRINSIC_W_CHAIN: 9849 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 9850 case Intrinsic::arm_neon_vld1: 9851 case Intrinsic::arm_neon_vld2: 9852 case Intrinsic::arm_neon_vld3: 9853 case Intrinsic::arm_neon_vld4: 9854 case Intrinsic::arm_neon_vld2lane: 9855 case Intrinsic::arm_neon_vld3lane: 9856 case Intrinsic::arm_neon_vld4lane: 9857 case Intrinsic::arm_neon_vst1: 9858 case Intrinsic::arm_neon_vst2: 9859 case Intrinsic::arm_neon_vst3: 9860 case Intrinsic::arm_neon_vst4: 9861 case Intrinsic::arm_neon_vst2lane: 9862 case Intrinsic::arm_neon_vst3lane: 9863 case Intrinsic::arm_neon_vst4lane: 9864 return CombineBaseUpdate(N, DCI); 9865 default: break; 9866 } 9867 break; 9868 } 9869 return SDValue(); 9870 } 9871 9872 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 9873 EVT VT) const { 9874 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 9875 } 9876 9877 bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 9878 unsigned, 9879 unsigned, 9880 bool *Fast) const { 9881 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus 9882 bool AllowsUnaligned = Subtarget->allowsUnalignedMem(); 9883 9884 switch (VT.getSimpleVT().SimpleTy) { 9885 default: 9886 return false; 9887 case MVT::i8: 9888 case MVT::i16: 9889 case MVT::i32: { 9890 // Unaligned access can use (for example) LRDB, LRDH, LDR 9891 if (AllowsUnaligned) { 9892 if (Fast) 9893 *Fast = Subtarget->hasV7Ops(); 9894 return true; 9895 } 9896 return false; 9897 } 9898 case MVT::f64: 9899 case MVT::v2f64: { 9900 // For any little-endian targets with neon, we can support unaligned ld/st 9901 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8. 9902 // A big-endian target may also explicitly support unaligned accesses 9903 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) { 9904 if (Fast) 9905 *Fast = true; 9906 return true; 9907 } 9908 return false; 9909 } 9910 } 9911 } 9912 9913 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 9914 unsigned AlignCheck) { 9915 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 9916 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 9917 } 9918 9919 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size, 9920 unsigned DstAlign, unsigned SrcAlign, 9921 bool IsMemset, bool ZeroMemset, 9922 bool MemcpyStrSrc, 9923 MachineFunction &MF) const { 9924 const Function *F = MF.getFunction(); 9925 9926 // See if we can use NEON instructions for this... 9927 if ((!IsMemset || ZeroMemset) && 9928 Subtarget->hasNEON() && 9929 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 9930 Attribute::NoImplicitFloat)) { 9931 bool Fast; 9932 if (Size >= 16 && 9933 (memOpAlign(SrcAlign, DstAlign, 16) || 9934 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) { 9935 return MVT::v2f64; 9936 } else if (Size >= 8 && 9937 (memOpAlign(SrcAlign, DstAlign, 8) || 9938 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) && 9939 Fast))) { 9940 return MVT::f64; 9941 } 9942 } 9943 9944 // Lowering to i32/i16 if the size permits. 9945 if (Size >= 4) 9946 return MVT::i32; 9947 else if (Size >= 2) 9948 return MVT::i16; 9949 9950 // Let the target-independent logic figure it out. 9951 return MVT::Other; 9952 } 9953 9954 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 9955 if (Val.getOpcode() != ISD::LOAD) 9956 return false; 9957 9958 EVT VT1 = Val.getValueType(); 9959 if (!VT1.isSimple() || !VT1.isInteger() || 9960 !VT2.isSimple() || !VT2.isInteger()) 9961 return false; 9962 9963 switch (VT1.getSimpleVT().SimpleTy) { 9964 default: break; 9965 case MVT::i1: 9966 case MVT::i8: 9967 case MVT::i16: 9968 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits. 9969 return true; 9970 } 9971 9972 return false; 9973 } 9974 9975 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const { 9976 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 9977 return false; 9978 9979 if (!isTypeLegal(EVT::getEVT(Ty1))) 9980 return false; 9981 9982 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop"); 9983 9984 // Assuming the caller doesn't have a zeroext or signext return parameter, 9985 // truncation all the way down to i1 is valid. 9986 return true; 9987 } 9988 9989 9990 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 9991 if (V < 0) 9992 return false; 9993 9994 unsigned Scale = 1; 9995 switch (VT.getSimpleVT().SimpleTy) { 9996 default: return false; 9997 case MVT::i1: 9998 case MVT::i8: 9999 // Scale == 1; 10000 break; 10001 case MVT::i16: 10002 // Scale == 2; 10003 Scale = 2; 10004 break; 10005 case MVT::i32: 10006 // Scale == 4; 10007 Scale = 4; 10008 break; 10009 } 10010 10011 if ((V & (Scale - 1)) != 0) 10012 return false; 10013 V /= Scale; 10014 return V == (V & ((1LL << 5) - 1)); 10015 } 10016 10017 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 10018 const ARMSubtarget *Subtarget) { 10019 bool isNeg = false; 10020 if (V < 0) { 10021 isNeg = true; 10022 V = - V; 10023 } 10024 10025 switch (VT.getSimpleVT().SimpleTy) { 10026 default: return false; 10027 case MVT::i1: 10028 case MVT::i8: 10029 case MVT::i16: 10030 case MVT::i32: 10031 // + imm12 or - imm8 10032 if (isNeg) 10033 return V == (V & ((1LL << 8) - 1)); 10034 return V == (V & ((1LL << 12) - 1)); 10035 case MVT::f32: 10036 case MVT::f64: 10037 // Same as ARM mode. FIXME: NEON? 10038 if (!Subtarget->hasVFP2()) 10039 return false; 10040 if ((V & 3) != 0) 10041 return false; 10042 V >>= 2; 10043 return V == (V & ((1LL << 8) - 1)); 10044 } 10045 } 10046 10047 /// isLegalAddressImmediate - Return true if the integer value can be used 10048 /// as the offset of the target addressing mode for load / store of the 10049 /// given type. 10050 static bool isLegalAddressImmediate(int64_t V, EVT VT, 10051 const ARMSubtarget *Subtarget) { 10052 if (V == 0) 10053 return true; 10054 10055 if (!VT.isSimple()) 10056 return false; 10057 10058 if (Subtarget->isThumb1Only()) 10059 return isLegalT1AddressImmediate(V, VT); 10060 else if (Subtarget->isThumb2()) 10061 return isLegalT2AddressImmediate(V, VT, Subtarget); 10062 10063 // ARM mode. 10064 if (V < 0) 10065 V = - V; 10066 switch (VT.getSimpleVT().SimpleTy) { 10067 default: return false; 10068 case MVT::i1: 10069 case MVT::i8: 10070 case MVT::i32: 10071 // +- imm12 10072 return V == (V & ((1LL << 12) - 1)); 10073 case MVT::i16: 10074 // +- imm8 10075 return V == (V & ((1LL << 8) - 1)); 10076 case MVT::f32: 10077 case MVT::f64: 10078 if (!Subtarget->hasVFP2()) // FIXME: NEON? 10079 return false; 10080 if ((V & 3) != 0) 10081 return false; 10082 V >>= 2; 10083 return V == (V & ((1LL << 8) - 1)); 10084 } 10085 } 10086 10087 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 10088 EVT VT) const { 10089 int Scale = AM.Scale; 10090 if (Scale < 0) 10091 return false; 10092 10093 switch (VT.getSimpleVT().SimpleTy) { 10094 default: return false; 10095 case MVT::i1: 10096 case MVT::i8: 10097 case MVT::i16: 10098 case MVT::i32: 10099 if (Scale == 1) 10100 return true; 10101 // r + r << imm 10102 Scale = Scale & ~1; 10103 return Scale == 2 || Scale == 4 || Scale == 8; 10104 case MVT::i64: 10105 // r + r 10106 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 10107 return true; 10108 return false; 10109 case MVT::isVoid: 10110 // Note, we allow "void" uses (basically, uses that aren't loads or 10111 // stores), because arm allows folding a scale into many arithmetic 10112 // operations. This should be made more precise and revisited later. 10113 10114 // Allow r << imm, but the imm has to be a multiple of two. 10115 if (Scale & 1) return false; 10116 return isPowerOf2_32(Scale); 10117 } 10118 } 10119 10120 /// isLegalAddressingMode - Return true if the addressing mode represented 10121 /// by AM is legal for this target, for a load/store of the specified type. 10122 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM, 10123 Type *Ty) const { 10124 EVT VT = getValueType(Ty, true); 10125 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 10126 return false; 10127 10128 // Can never fold addr of global into load/store. 10129 if (AM.BaseGV) 10130 return false; 10131 10132 switch (AM.Scale) { 10133 case 0: // no scale reg, must be "r+i" or "r", or "i". 10134 break; 10135 case 1: 10136 if (Subtarget->isThumb1Only()) 10137 return false; 10138 // FALL THROUGH. 10139 default: 10140 // ARM doesn't support any R+R*scale+imm addr modes. 10141 if (AM.BaseOffs) 10142 return false; 10143 10144 if (!VT.isSimple()) 10145 return false; 10146 10147 if (Subtarget->isThumb2()) 10148 return isLegalT2ScaledAddressingMode(AM, VT); 10149 10150 int Scale = AM.Scale; 10151 switch (VT.getSimpleVT().SimpleTy) { 10152 default: return false; 10153 case MVT::i1: 10154 case MVT::i8: 10155 case MVT::i32: 10156 if (Scale < 0) Scale = -Scale; 10157 if (Scale == 1) 10158 return true; 10159 // r + r << imm 10160 return isPowerOf2_32(Scale & ~1); 10161 case MVT::i16: 10162 case MVT::i64: 10163 // r + r 10164 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 10165 return true; 10166 return false; 10167 10168 case MVT::isVoid: 10169 // Note, we allow "void" uses (basically, uses that aren't loads or 10170 // stores), because arm allows folding a scale into many arithmetic 10171 // operations. This should be made more precise and revisited later. 10172 10173 // Allow r << imm, but the imm has to be a multiple of two. 10174 if (Scale & 1) return false; 10175 return isPowerOf2_32(Scale); 10176 } 10177 } 10178 return true; 10179 } 10180 10181 /// isLegalICmpImmediate - Return true if the specified immediate is legal 10182 /// icmp immediate, that is the target has icmp instructions which can compare 10183 /// a register against the immediate without having to materialize the 10184 /// immediate into a register. 10185 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 10186 // Thumb2 and ARM modes can use cmn for negative immediates. 10187 if (!Subtarget->isThumb()) 10188 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1; 10189 if (Subtarget->isThumb2()) 10190 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1; 10191 // Thumb1 doesn't have cmn, and only 8-bit immediates. 10192 return Imm >= 0 && Imm <= 255; 10193 } 10194 10195 /// isLegalAddImmediate - Return true if the specified immediate is a legal add 10196 /// *or sub* immediate, that is the target has add or sub instructions which can 10197 /// add a register with the immediate without having to materialize the 10198 /// immediate into a register. 10199 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 10200 // Same encoding for add/sub, just flip the sign. 10201 int64_t AbsImm = llvm::abs64(Imm); 10202 if (!Subtarget->isThumb()) 10203 return ARM_AM::getSOImmVal(AbsImm) != -1; 10204 if (Subtarget->isThumb2()) 10205 return ARM_AM::getT2SOImmVal(AbsImm) != -1; 10206 // Thumb1 only has 8-bit unsigned immediate. 10207 return AbsImm >= 0 && AbsImm <= 255; 10208 } 10209 10210 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 10211 bool isSEXTLoad, SDValue &Base, 10212 SDValue &Offset, bool &isInc, 10213 SelectionDAG &DAG) { 10214 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 10215 return false; 10216 10217 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 10218 // AddressingMode 3 10219 Base = Ptr->getOperand(0); 10220 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10221 int RHSC = (int)RHS->getZExtValue(); 10222 if (RHSC < 0 && RHSC > -256) { 10223 assert(Ptr->getOpcode() == ISD::ADD); 10224 isInc = false; 10225 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 10226 return true; 10227 } 10228 } 10229 isInc = (Ptr->getOpcode() == ISD::ADD); 10230 Offset = Ptr->getOperand(1); 10231 return true; 10232 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 10233 // AddressingMode 2 10234 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10235 int RHSC = (int)RHS->getZExtValue(); 10236 if (RHSC < 0 && RHSC > -0x1000) { 10237 assert(Ptr->getOpcode() == ISD::ADD); 10238 isInc = false; 10239 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 10240 Base = Ptr->getOperand(0); 10241 return true; 10242 } 10243 } 10244 10245 if (Ptr->getOpcode() == ISD::ADD) { 10246 isInc = true; 10247 ARM_AM::ShiftOpc ShOpcVal= 10248 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 10249 if (ShOpcVal != ARM_AM::no_shift) { 10250 Base = Ptr->getOperand(1); 10251 Offset = Ptr->getOperand(0); 10252 } else { 10253 Base = Ptr->getOperand(0); 10254 Offset = Ptr->getOperand(1); 10255 } 10256 return true; 10257 } 10258 10259 isInc = (Ptr->getOpcode() == ISD::ADD); 10260 Base = Ptr->getOperand(0); 10261 Offset = Ptr->getOperand(1); 10262 return true; 10263 } 10264 10265 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 10266 return false; 10267 } 10268 10269 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 10270 bool isSEXTLoad, SDValue &Base, 10271 SDValue &Offset, bool &isInc, 10272 SelectionDAG &DAG) { 10273 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 10274 return false; 10275 10276 Base = Ptr->getOperand(0); 10277 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10278 int RHSC = (int)RHS->getZExtValue(); 10279 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 10280 assert(Ptr->getOpcode() == ISD::ADD); 10281 isInc = false; 10282 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0)); 10283 return true; 10284 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 10285 isInc = Ptr->getOpcode() == ISD::ADD; 10286 Offset = DAG.getConstant(RHSC, RHS->getValueType(0)); 10287 return true; 10288 } 10289 } 10290 10291 return false; 10292 } 10293 10294 /// getPreIndexedAddressParts - returns true by value, base pointer and 10295 /// offset pointer and addressing mode by reference if the node's address 10296 /// can be legally represented as pre-indexed load / store address. 10297 bool 10298 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 10299 SDValue &Offset, 10300 ISD::MemIndexedMode &AM, 10301 SelectionDAG &DAG) const { 10302 if (Subtarget->isThumb1Only()) 10303 return false; 10304 10305 EVT VT; 10306 SDValue Ptr; 10307 bool isSEXTLoad = false; 10308 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 10309 Ptr = LD->getBasePtr(); 10310 VT = LD->getMemoryVT(); 10311 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 10312 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 10313 Ptr = ST->getBasePtr(); 10314 VT = ST->getMemoryVT(); 10315 } else 10316 return false; 10317 10318 bool isInc; 10319 bool isLegal = false; 10320 if (Subtarget->isThumb2()) 10321 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 10322 Offset, isInc, DAG); 10323 else 10324 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 10325 Offset, isInc, DAG); 10326 if (!isLegal) 10327 return false; 10328 10329 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 10330 return true; 10331 } 10332 10333 /// getPostIndexedAddressParts - returns true by value, base pointer and 10334 /// offset pointer and addressing mode by reference if this node can be 10335 /// combined with a load / store to form a post-indexed load / store. 10336 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 10337 SDValue &Base, 10338 SDValue &Offset, 10339 ISD::MemIndexedMode &AM, 10340 SelectionDAG &DAG) const { 10341 if (Subtarget->isThumb1Only()) 10342 return false; 10343 10344 EVT VT; 10345 SDValue Ptr; 10346 bool isSEXTLoad = false; 10347 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 10348 VT = LD->getMemoryVT(); 10349 Ptr = LD->getBasePtr(); 10350 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 10351 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 10352 VT = ST->getMemoryVT(); 10353 Ptr = ST->getBasePtr(); 10354 } else 10355 return false; 10356 10357 bool isInc; 10358 bool isLegal = false; 10359 if (Subtarget->isThumb2()) 10360 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 10361 isInc, DAG); 10362 else 10363 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 10364 isInc, DAG); 10365 if (!isLegal) 10366 return false; 10367 10368 if (Ptr != Base) { 10369 // Swap base ptr and offset to catch more post-index load / store when 10370 // it's legal. In Thumb2 mode, offset must be an immediate. 10371 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 10372 !Subtarget->isThumb2()) 10373 std::swap(Base, Offset); 10374 10375 // Post-indexed load / store update the base pointer. 10376 if (Ptr != Base) 10377 return false; 10378 } 10379 10380 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 10381 return true; 10382 } 10383 10384 void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 10385 APInt &KnownZero, 10386 APInt &KnownOne, 10387 const SelectionDAG &DAG, 10388 unsigned Depth) const { 10389 unsigned BitWidth = KnownOne.getBitWidth(); 10390 KnownZero = KnownOne = APInt(BitWidth, 0); 10391 switch (Op.getOpcode()) { 10392 default: break; 10393 case ARMISD::ADDC: 10394 case ARMISD::ADDE: 10395 case ARMISD::SUBC: 10396 case ARMISD::SUBE: 10397 // These nodes' second result is a boolean 10398 if (Op.getResNo() == 0) 10399 break; 10400 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 10401 break; 10402 case ARMISD::CMOV: { 10403 // Bits are known zero/one if known on the LHS and RHS. 10404 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 10405 if (KnownZero == 0 && KnownOne == 0) return; 10406 10407 APInt KnownZeroRHS, KnownOneRHS; 10408 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1); 10409 KnownZero &= KnownZeroRHS; 10410 KnownOne &= KnownOneRHS; 10411 return; 10412 } 10413 case ISD::INTRINSIC_W_CHAIN: { 10414 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1)); 10415 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue()); 10416 switch (IntID) { 10417 default: return; 10418 case Intrinsic::arm_ldaex: 10419 case Intrinsic::arm_ldrex: { 10420 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT(); 10421 unsigned MemBits = VT.getScalarType().getSizeInBits(); 10422 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); 10423 return; 10424 } 10425 } 10426 } 10427 } 10428 } 10429 10430 //===----------------------------------------------------------------------===// 10431 // ARM Inline Assembly Support 10432 //===----------------------------------------------------------------------===// 10433 10434 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 10435 // Looking for "rev" which is V6+. 10436 if (!Subtarget->hasV6Ops()) 10437 return false; 10438 10439 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 10440 std::string AsmStr = IA->getAsmString(); 10441 SmallVector<StringRef, 4> AsmPieces; 10442 SplitString(AsmStr, AsmPieces, ";\n"); 10443 10444 switch (AsmPieces.size()) { 10445 default: return false; 10446 case 1: 10447 AsmStr = AsmPieces[0]; 10448 AsmPieces.clear(); 10449 SplitString(AsmStr, AsmPieces, " \t,"); 10450 10451 // rev $0, $1 10452 if (AsmPieces.size() == 3 && 10453 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 10454 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 10455 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 10456 if (Ty && Ty->getBitWidth() == 32) 10457 return IntrinsicLowering::LowerToByteSwap(CI); 10458 } 10459 break; 10460 } 10461 10462 return false; 10463 } 10464 10465 /// getConstraintType - Given a constraint letter, return the type of 10466 /// constraint it is for this target. 10467 ARMTargetLowering::ConstraintType 10468 ARMTargetLowering::getConstraintType(const std::string &Constraint) const { 10469 if (Constraint.size() == 1) { 10470 switch (Constraint[0]) { 10471 default: break; 10472 case 'l': return C_RegisterClass; 10473 case 'w': return C_RegisterClass; 10474 case 'h': return C_RegisterClass; 10475 case 'x': return C_RegisterClass; 10476 case 't': return C_RegisterClass; 10477 case 'j': return C_Other; // Constant for movw. 10478 // An address with a single base register. Due to the way we 10479 // currently handle addresses it is the same as an 'r' memory constraint. 10480 case 'Q': return C_Memory; 10481 } 10482 } else if (Constraint.size() == 2) { 10483 switch (Constraint[0]) { 10484 default: break; 10485 // All 'U+' constraints are addresses. 10486 case 'U': return C_Memory; 10487 } 10488 } 10489 return TargetLowering::getConstraintType(Constraint); 10490 } 10491 10492 /// Examine constraint type and operand type and determine a weight value. 10493 /// This object must already have been set up with the operand type 10494 /// and the current alternative constraint selected. 10495 TargetLowering::ConstraintWeight 10496 ARMTargetLowering::getSingleConstraintMatchWeight( 10497 AsmOperandInfo &info, const char *constraint) const { 10498 ConstraintWeight weight = CW_Invalid; 10499 Value *CallOperandVal = info.CallOperandVal; 10500 // If we don't have a value, we can't do a match, 10501 // but allow it at the lowest weight. 10502 if (!CallOperandVal) 10503 return CW_Default; 10504 Type *type = CallOperandVal->getType(); 10505 // Look at the constraint type. 10506 switch (*constraint) { 10507 default: 10508 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 10509 break; 10510 case 'l': 10511 if (type->isIntegerTy()) { 10512 if (Subtarget->isThumb()) 10513 weight = CW_SpecificReg; 10514 else 10515 weight = CW_Register; 10516 } 10517 break; 10518 case 'w': 10519 if (type->isFloatingPointTy()) 10520 weight = CW_Register; 10521 break; 10522 } 10523 return weight; 10524 } 10525 10526 typedef std::pair<unsigned, const TargetRegisterClass*> RCPair; 10527 RCPair 10528 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, 10529 MVT VT) const { 10530 if (Constraint.size() == 1) { 10531 // GCC ARM Constraint Letters 10532 switch (Constraint[0]) { 10533 case 'l': // Low regs or general regs. 10534 if (Subtarget->isThumb()) 10535 return RCPair(0U, &ARM::tGPRRegClass); 10536 return RCPair(0U, &ARM::GPRRegClass); 10537 case 'h': // High regs or no regs. 10538 if (Subtarget->isThumb()) 10539 return RCPair(0U, &ARM::hGPRRegClass); 10540 break; 10541 case 'r': 10542 return RCPair(0U, &ARM::GPRRegClass); 10543 case 'w': 10544 if (VT == MVT::Other) 10545 break; 10546 if (VT == MVT::f32) 10547 return RCPair(0U, &ARM::SPRRegClass); 10548 if (VT.getSizeInBits() == 64) 10549 return RCPair(0U, &ARM::DPRRegClass); 10550 if (VT.getSizeInBits() == 128) 10551 return RCPair(0U, &ARM::QPRRegClass); 10552 break; 10553 case 'x': 10554 if (VT == MVT::Other) 10555 break; 10556 if (VT == MVT::f32) 10557 return RCPair(0U, &ARM::SPR_8RegClass); 10558 if (VT.getSizeInBits() == 64) 10559 return RCPair(0U, &ARM::DPR_8RegClass); 10560 if (VT.getSizeInBits() == 128) 10561 return RCPair(0U, &ARM::QPR_8RegClass); 10562 break; 10563 case 't': 10564 if (VT == MVT::f32) 10565 return RCPair(0U, &ARM::SPRRegClass); 10566 break; 10567 } 10568 } 10569 if (StringRef("{cc}").equals_lower(Constraint)) 10570 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass); 10571 10572 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 10573 } 10574 10575 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 10576 /// vector. If it is invalid, don't add anything to Ops. 10577 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 10578 std::string &Constraint, 10579 std::vector<SDValue>&Ops, 10580 SelectionDAG &DAG) const { 10581 SDValue Result; 10582 10583 // Currently only support length 1 constraints. 10584 if (Constraint.length() != 1) return; 10585 10586 char ConstraintLetter = Constraint[0]; 10587 switch (ConstraintLetter) { 10588 default: break; 10589 case 'j': 10590 case 'I': case 'J': case 'K': case 'L': 10591 case 'M': case 'N': case 'O': 10592 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 10593 if (!C) 10594 return; 10595 10596 int64_t CVal64 = C->getSExtValue(); 10597 int CVal = (int) CVal64; 10598 // None of these constraints allow values larger than 32 bits. Check 10599 // that the value fits in an int. 10600 if (CVal != CVal64) 10601 return; 10602 10603 switch (ConstraintLetter) { 10604 case 'j': 10605 // Constant suitable for movw, must be between 0 and 10606 // 65535. 10607 if (Subtarget->hasV6T2Ops()) 10608 if (CVal >= 0 && CVal <= 65535) 10609 break; 10610 return; 10611 case 'I': 10612 if (Subtarget->isThumb1Only()) { 10613 // This must be a constant between 0 and 255, for ADD 10614 // immediates. 10615 if (CVal >= 0 && CVal <= 255) 10616 break; 10617 } else if (Subtarget->isThumb2()) { 10618 // A constant that can be used as an immediate value in a 10619 // data-processing instruction. 10620 if (ARM_AM::getT2SOImmVal(CVal) != -1) 10621 break; 10622 } else { 10623 // A constant that can be used as an immediate value in a 10624 // data-processing instruction. 10625 if (ARM_AM::getSOImmVal(CVal) != -1) 10626 break; 10627 } 10628 return; 10629 10630 case 'J': 10631 if (Subtarget->isThumb()) { // FIXME thumb2 10632 // This must be a constant between -255 and -1, for negated ADD 10633 // immediates. This can be used in GCC with an "n" modifier that 10634 // prints the negated value, for use with SUB instructions. It is 10635 // not useful otherwise but is implemented for compatibility. 10636 if (CVal >= -255 && CVal <= -1) 10637 break; 10638 } else { 10639 // This must be a constant between -4095 and 4095. It is not clear 10640 // what this constraint is intended for. Implemented for 10641 // compatibility with GCC. 10642 if (CVal >= -4095 && CVal <= 4095) 10643 break; 10644 } 10645 return; 10646 10647 case 'K': 10648 if (Subtarget->isThumb1Only()) { 10649 // A 32-bit value where only one byte has a nonzero value. Exclude 10650 // zero to match GCC. This constraint is used by GCC internally for 10651 // constants that can be loaded with a move/shift combination. 10652 // It is not useful otherwise but is implemented for compatibility. 10653 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 10654 break; 10655 } else if (Subtarget->isThumb2()) { 10656 // A constant whose bitwise inverse can be used as an immediate 10657 // value in a data-processing instruction. This can be used in GCC 10658 // with a "B" modifier that prints the inverted value, for use with 10659 // BIC and MVN instructions. It is not useful otherwise but is 10660 // implemented for compatibility. 10661 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 10662 break; 10663 } else { 10664 // A constant whose bitwise inverse can be used as an immediate 10665 // value in a data-processing instruction. This can be used in GCC 10666 // with a "B" modifier that prints the inverted value, for use with 10667 // BIC and MVN instructions. It is not useful otherwise but is 10668 // implemented for compatibility. 10669 if (ARM_AM::getSOImmVal(~CVal) != -1) 10670 break; 10671 } 10672 return; 10673 10674 case 'L': 10675 if (Subtarget->isThumb1Only()) { 10676 // This must be a constant between -7 and 7, 10677 // for 3-operand ADD/SUB immediate instructions. 10678 if (CVal >= -7 && CVal < 7) 10679 break; 10680 } else if (Subtarget->isThumb2()) { 10681 // A constant whose negation can be used as an immediate value in a 10682 // data-processing instruction. This can be used in GCC with an "n" 10683 // modifier that prints the negated value, for use with SUB 10684 // instructions. It is not useful otherwise but is implemented for 10685 // compatibility. 10686 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 10687 break; 10688 } else { 10689 // A constant whose negation can be used as an immediate value in a 10690 // data-processing instruction. This can be used in GCC with an "n" 10691 // modifier that prints the negated value, for use with SUB 10692 // instructions. It is not useful otherwise but is implemented for 10693 // compatibility. 10694 if (ARM_AM::getSOImmVal(-CVal) != -1) 10695 break; 10696 } 10697 return; 10698 10699 case 'M': 10700 if (Subtarget->isThumb()) { // FIXME thumb2 10701 // This must be a multiple of 4 between 0 and 1020, for 10702 // ADD sp + immediate. 10703 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 10704 break; 10705 } else { 10706 // A power of two or a constant between 0 and 32. This is used in 10707 // GCC for the shift amount on shifted register operands, but it is 10708 // useful in general for any shift amounts. 10709 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 10710 break; 10711 } 10712 return; 10713 10714 case 'N': 10715 if (Subtarget->isThumb()) { // FIXME thumb2 10716 // This must be a constant between 0 and 31, for shift amounts. 10717 if (CVal >= 0 && CVal <= 31) 10718 break; 10719 } 10720 return; 10721 10722 case 'O': 10723 if (Subtarget->isThumb()) { // FIXME thumb2 10724 // This must be a multiple of 4 between -508 and 508, for 10725 // ADD/SUB sp = sp + immediate. 10726 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 10727 break; 10728 } 10729 return; 10730 } 10731 Result = DAG.getTargetConstant(CVal, Op.getValueType()); 10732 break; 10733 } 10734 10735 if (Result.getNode()) { 10736 Ops.push_back(Result); 10737 return; 10738 } 10739 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 10740 } 10741 10742 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const { 10743 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only"); 10744 unsigned Opcode = Op->getOpcode(); 10745 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) && 10746 "Invalid opcode for Div/Rem lowering"); 10747 bool isSigned = (Opcode == ISD::SDIVREM); 10748 EVT VT = Op->getValueType(0); 10749 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 10750 10751 RTLIB::Libcall LC; 10752 switch (VT.getSimpleVT().SimpleTy) { 10753 default: llvm_unreachable("Unexpected request for libcall!"); 10754 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 10755 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 10756 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 10757 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 10758 } 10759 10760 SDValue InChain = DAG.getEntryNode(); 10761 10762 TargetLowering::ArgListTy Args; 10763 TargetLowering::ArgListEntry Entry; 10764 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) { 10765 EVT ArgVT = Op->getOperand(i).getValueType(); 10766 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 10767 Entry.Node = Op->getOperand(i); 10768 Entry.Ty = ArgTy; 10769 Entry.isSExt = isSigned; 10770 Entry.isZExt = !isSigned; 10771 Args.push_back(Entry); 10772 } 10773 10774 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 10775 getPointerTy()); 10776 10777 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL); 10778 10779 SDLoc dl(Op); 10780 TargetLowering::CallLoweringInfo CLI(DAG); 10781 CLI.setDebugLoc(dl).setChain(InChain) 10782 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0) 10783 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned); 10784 10785 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 10786 return CallInfo.first; 10787 } 10788 10789 SDValue 10790 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { 10791 assert(Subtarget->isTargetWindows() && "unsupported target platform"); 10792 SDLoc DL(Op); 10793 10794 // Get the inputs. 10795 SDValue Chain = Op.getOperand(0); 10796 SDValue Size = Op.getOperand(1); 10797 10798 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size, 10799 DAG.getConstant(2, MVT::i32)); 10800 10801 SDValue Flag; 10802 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag); 10803 Flag = Chain.getValue(1); 10804 10805 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 10806 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag); 10807 10808 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32); 10809 Chain = NewSP.getValue(1); 10810 10811 SDValue Ops[2] = { NewSP, Chain }; 10812 return DAG.getMergeValues(Ops, DL); 10813 } 10814 10815 SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { 10816 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() && 10817 "Unexpected type for custom-lowering FP_EXTEND"); 10818 10819 RTLIB::Libcall LC; 10820 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType()); 10821 10822 SDValue SrcVal = Op.getOperand(0); 10823 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1, 10824 /*isSigned*/ false, SDLoc(Op)).first; 10825 } 10826 10827 SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 10828 assert(Op.getOperand(0).getValueType() == MVT::f64 && 10829 Subtarget->isFPOnlySP() && 10830 "Unexpected type for custom-lowering FP_ROUND"); 10831 10832 RTLIB::Libcall LC; 10833 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType()); 10834 10835 SDValue SrcVal = Op.getOperand(0); 10836 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1, 10837 /*isSigned*/ false, SDLoc(Op)).first; 10838 } 10839 10840 bool 10841 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 10842 // The ARM target isn't yet aware of offsets. 10843 return false; 10844 } 10845 10846 bool ARM::isBitFieldInvertedMask(unsigned v) { 10847 if (v == 0xffffffff) 10848 return false; 10849 10850 // there can be 1's on either or both "outsides", all the "inside" 10851 // bits must be 0's 10852 unsigned TO = CountTrailingOnes_32(v); 10853 unsigned LO = CountLeadingOnes_32(v); 10854 v = (v >> TO) << TO; 10855 v = (v << LO) >> LO; 10856 return v == 0; 10857 } 10858 10859 /// isFPImmLegal - Returns true if the target can instruction select the 10860 /// specified FP immediate natively. If false, the legalizer will 10861 /// materialize the FP immediate as a load from a constant pool. 10862 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 10863 if (!Subtarget->hasVFP3()) 10864 return false; 10865 if (VT == MVT::f32) 10866 return ARM_AM::getFP32Imm(Imm) != -1; 10867 if (VT == MVT::f64 && !Subtarget->isFPOnlySP()) 10868 return ARM_AM::getFP64Imm(Imm) != -1; 10869 return false; 10870 } 10871 10872 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 10873 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 10874 /// specified in the intrinsic calls. 10875 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 10876 const CallInst &I, 10877 unsigned Intrinsic) const { 10878 switch (Intrinsic) { 10879 case Intrinsic::arm_neon_vld1: 10880 case Intrinsic::arm_neon_vld2: 10881 case Intrinsic::arm_neon_vld3: 10882 case Intrinsic::arm_neon_vld4: 10883 case Intrinsic::arm_neon_vld2lane: 10884 case Intrinsic::arm_neon_vld3lane: 10885 case Intrinsic::arm_neon_vld4lane: { 10886 Info.opc = ISD::INTRINSIC_W_CHAIN; 10887 // Conservatively set memVT to the entire set of vectors loaded. 10888 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8; 10889 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 10890 Info.ptrVal = I.getArgOperand(0); 10891 Info.offset = 0; 10892 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 10893 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 10894 Info.vol = false; // volatile loads with NEON intrinsics not supported 10895 Info.readMem = true; 10896 Info.writeMem = false; 10897 return true; 10898 } 10899 case Intrinsic::arm_neon_vst1: 10900 case Intrinsic::arm_neon_vst2: 10901 case Intrinsic::arm_neon_vst3: 10902 case Intrinsic::arm_neon_vst4: 10903 case Intrinsic::arm_neon_vst2lane: 10904 case Intrinsic::arm_neon_vst3lane: 10905 case Intrinsic::arm_neon_vst4lane: { 10906 Info.opc = ISD::INTRINSIC_VOID; 10907 // Conservatively set memVT to the entire set of vectors stored. 10908 unsigned NumElts = 0; 10909 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 10910 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 10911 if (!ArgTy->isVectorTy()) 10912 break; 10913 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8; 10914 } 10915 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 10916 Info.ptrVal = I.getArgOperand(0); 10917 Info.offset = 0; 10918 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 10919 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 10920 Info.vol = false; // volatile stores with NEON intrinsics not supported 10921 Info.readMem = false; 10922 Info.writeMem = true; 10923 return true; 10924 } 10925 case Intrinsic::arm_ldaex: 10926 case Intrinsic::arm_ldrex: { 10927 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); 10928 Info.opc = ISD::INTRINSIC_W_CHAIN; 10929 Info.memVT = MVT::getVT(PtrTy->getElementType()); 10930 Info.ptrVal = I.getArgOperand(0); 10931 Info.offset = 0; 10932 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType()); 10933 Info.vol = true; 10934 Info.readMem = true; 10935 Info.writeMem = false; 10936 return true; 10937 } 10938 case Intrinsic::arm_stlex: 10939 case Intrinsic::arm_strex: { 10940 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType()); 10941 Info.opc = ISD::INTRINSIC_W_CHAIN; 10942 Info.memVT = MVT::getVT(PtrTy->getElementType()); 10943 Info.ptrVal = I.getArgOperand(1); 10944 Info.offset = 0; 10945 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType()); 10946 Info.vol = true; 10947 Info.readMem = false; 10948 Info.writeMem = true; 10949 return true; 10950 } 10951 case Intrinsic::arm_stlexd: 10952 case Intrinsic::arm_strexd: { 10953 Info.opc = ISD::INTRINSIC_W_CHAIN; 10954 Info.memVT = MVT::i64; 10955 Info.ptrVal = I.getArgOperand(2); 10956 Info.offset = 0; 10957 Info.align = 8; 10958 Info.vol = true; 10959 Info.readMem = false; 10960 Info.writeMem = true; 10961 return true; 10962 } 10963 case Intrinsic::arm_ldaexd: 10964 case Intrinsic::arm_ldrexd: { 10965 Info.opc = ISD::INTRINSIC_W_CHAIN; 10966 Info.memVT = MVT::i64; 10967 Info.ptrVal = I.getArgOperand(0); 10968 Info.offset = 0; 10969 Info.align = 8; 10970 Info.vol = true; 10971 Info.readMem = true; 10972 Info.writeMem = false; 10973 return true; 10974 } 10975 default: 10976 break; 10977 } 10978 10979 return false; 10980 } 10981 10982 /// \brief Returns true if it is beneficial to convert a load of a constant 10983 /// to just the constant itself. 10984 bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 10985 Type *Ty) const { 10986 assert(Ty->isIntegerTy()); 10987 10988 unsigned Bits = Ty->getPrimitiveSizeInBits(); 10989 if (Bits == 0 || Bits > 32) 10990 return false; 10991 return true; 10992 } 10993 10994 bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; } 10995 10996 Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder, 10997 ARM_MB::MemBOpt Domain) const { 10998 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 10999 11000 // First, if the target has no DMB, see what fallback we can use. 11001 if (!Subtarget->hasDataBarrier()) { 11002 // Some ARMv6 cpus can support data barriers with an mcr instruction. 11003 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 11004 // here. 11005 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) { 11006 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr); 11007 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0), 11008 Builder.getInt32(0), Builder.getInt32(7), 11009 Builder.getInt32(10), Builder.getInt32(5)}; 11010 return Builder.CreateCall(MCR, args); 11011 } else { 11012 // Instead of using barriers, atomic accesses on these subtargets use 11013 // libcalls. 11014 llvm_unreachable("makeDMB on a target so old that it has no barriers"); 11015 } 11016 } else { 11017 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb); 11018 // Only a full system barrier exists in the M-class architectures. 11019 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain; 11020 Constant *CDomain = Builder.getInt32(Domain); 11021 return Builder.CreateCall(DMB, CDomain); 11022 } 11023 } 11024 11025 // Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html 11026 Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder, 11027 AtomicOrdering Ord, bool IsStore, 11028 bool IsLoad) const { 11029 if (!getInsertFencesForAtomic()) 11030 return nullptr; 11031 11032 switch (Ord) { 11033 case NotAtomic: 11034 case Unordered: 11035 llvm_unreachable("Invalid fence: unordered/non-atomic"); 11036 case Monotonic: 11037 case Acquire: 11038 return nullptr; // Nothing to do 11039 case SequentiallyConsistent: 11040 if (!IsStore) 11041 return nullptr; // Nothing to do 11042 /*FALLTHROUGH*/ 11043 case Release: 11044 case AcquireRelease: 11045 if (Subtarget->isSwift()) 11046 return makeDMB(Builder, ARM_MB::ISHST); 11047 // FIXME: add a comment with a link to documentation justifying this. 11048 else 11049 return makeDMB(Builder, ARM_MB::ISH); 11050 } 11051 llvm_unreachable("Unknown fence ordering in emitLeadingFence"); 11052 } 11053 11054 Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder, 11055 AtomicOrdering Ord, bool IsStore, 11056 bool IsLoad) const { 11057 if (!getInsertFencesForAtomic()) 11058 return nullptr; 11059 11060 switch (Ord) { 11061 case NotAtomic: 11062 case Unordered: 11063 llvm_unreachable("Invalid fence: unordered/not-atomic"); 11064 case Monotonic: 11065 case Release: 11066 return nullptr; // Nothing to do 11067 case Acquire: 11068 case AcquireRelease: 11069 case SequentiallyConsistent: 11070 return makeDMB(Builder, ARM_MB::ISH); 11071 } 11072 llvm_unreachable("Unknown fence ordering in emitTrailingFence"); 11073 } 11074 11075 // Loads and stores less than 64-bits are already atomic; ones above that 11076 // are doomed anyway, so defer to the default libcall and blame the OS when 11077 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 11078 // anything for those. 11079 bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const { 11080 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits(); 11081 return (Size == 64) && !Subtarget->isMClass(); 11082 } 11083 11084 // Loads and stores less than 64-bits are already atomic; ones above that 11085 // are doomed anyway, so defer to the default libcall and blame the OS when 11086 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 11087 // anything for those. 11088 // FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that 11089 // guarantee, see DDI0406C ARM architecture reference manual, 11090 // sections A8.8.72-74 LDRD) 11091 bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const { 11092 unsigned Size = LI->getType()->getPrimitiveSizeInBits(); 11093 return (Size == 64) && !Subtarget->isMClass(); 11094 } 11095 11096 // For the real atomic operations, we have ldrex/strex up to 32 bits, 11097 // and up to 64 bits on the non-M profiles 11098 bool ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 11099 unsigned Size = AI->getType()->getPrimitiveSizeInBits(); 11100 return Size <= (Subtarget->isMClass() ? 32U : 64U); 11101 } 11102 11103 // This has so far only been implemented for MachO. 11104 bool ARMTargetLowering::useLoadStackGuardNode() const { 11105 return Subtarget->getTargetTriple().getObjectFormat() == Triple::MachO; 11106 } 11107 11108 Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr, 11109 AtomicOrdering Ord) const { 11110 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 11111 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType(); 11112 bool IsAcquire = isAtLeastAcquire(Ord); 11113 11114 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd 11115 // intrinsic must return {i32, i32} and we have to recombine them into a 11116 // single i64 here. 11117 if (ValTy->getPrimitiveSizeInBits() == 64) { 11118 Intrinsic::ID Int = 11119 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd; 11120 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int); 11121 11122 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 11123 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi"); 11124 11125 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo"); 11126 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi"); 11127 if (!Subtarget->isLittle()) 11128 std::swap (Lo, Hi); 11129 Lo = Builder.CreateZExt(Lo, ValTy, "lo64"); 11130 Hi = Builder.CreateZExt(Hi, ValTy, "hi64"); 11131 return Builder.CreateOr( 11132 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64"); 11133 } 11134 11135 Type *Tys[] = { Addr->getType() }; 11136 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex; 11137 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys); 11138 11139 return Builder.CreateTruncOrBitCast( 11140 Builder.CreateCall(Ldrex, Addr), 11141 cast<PointerType>(Addr->getType())->getElementType()); 11142 } 11143 11144 Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val, 11145 Value *Addr, 11146 AtomicOrdering Ord) const { 11147 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 11148 bool IsRelease = isAtLeastRelease(Ord); 11149 11150 // Since the intrinsics must have legal type, the i64 intrinsics take two 11151 // parameters: "i32, i32". We must marshal Val into the appropriate form 11152 // before the call. 11153 if (Val->getType()->getPrimitiveSizeInBits() == 64) { 11154 Intrinsic::ID Int = 11155 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd; 11156 Function *Strex = Intrinsic::getDeclaration(M, Int); 11157 Type *Int32Ty = Type::getInt32Ty(M->getContext()); 11158 11159 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo"); 11160 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi"); 11161 if (!Subtarget->isLittle()) 11162 std::swap (Lo, Hi); 11163 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 11164 return Builder.CreateCall3(Strex, Lo, Hi, Addr); 11165 } 11166 11167 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex; 11168 Type *Tys[] = { Addr->getType() }; 11169 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys); 11170 11171 return Builder.CreateCall2( 11172 Strex, Builder.CreateZExtOrBitCast( 11173 Val, Strex->getFunctionType()->getParamType(0)), 11174 Addr); 11175 } 11176 11177 enum HABaseType { 11178 HA_UNKNOWN = 0, 11179 HA_FLOAT, 11180 HA_DOUBLE, 11181 HA_VECT64, 11182 HA_VECT128 11183 }; 11184 11185 static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base, 11186 uint64_t &Members) { 11187 if (const StructType *ST = dyn_cast<StructType>(Ty)) { 11188 for (unsigned i = 0; i < ST->getNumElements(); ++i) { 11189 uint64_t SubMembers = 0; 11190 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers)) 11191 return false; 11192 Members += SubMembers; 11193 } 11194 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { 11195 uint64_t SubMembers = 0; 11196 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers)) 11197 return false; 11198 Members += SubMembers * AT->getNumElements(); 11199 } else if (Ty->isFloatTy()) { 11200 if (Base != HA_UNKNOWN && Base != HA_FLOAT) 11201 return false; 11202 Members = 1; 11203 Base = HA_FLOAT; 11204 } else if (Ty->isDoubleTy()) { 11205 if (Base != HA_UNKNOWN && Base != HA_DOUBLE) 11206 return false; 11207 Members = 1; 11208 Base = HA_DOUBLE; 11209 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) { 11210 Members = 1; 11211 switch (Base) { 11212 case HA_FLOAT: 11213 case HA_DOUBLE: 11214 return false; 11215 case HA_VECT64: 11216 return VT->getBitWidth() == 64; 11217 case HA_VECT128: 11218 return VT->getBitWidth() == 128; 11219 case HA_UNKNOWN: 11220 switch (VT->getBitWidth()) { 11221 case 64: 11222 Base = HA_VECT64; 11223 return true; 11224 case 128: 11225 Base = HA_VECT128; 11226 return true; 11227 default: 11228 return false; 11229 } 11230 } 11231 } 11232 11233 return (Members > 0 && Members <= 4); 11234 } 11235 11236 /// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate. 11237 bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters( 11238 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { 11239 if (getEffectiveCallingConv(CallConv, isVarArg) != 11240 CallingConv::ARM_AAPCS_VFP) 11241 return false; 11242 11243 HABaseType Base = HA_UNKNOWN; 11244 uint64_t Members = 0; 11245 bool result = isHomogeneousAggregate(Ty, Base, Members); 11246 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump()); 11247 return result; 11248 } 11249