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/ADT/StringSwitch.h" 27 #include "llvm/CodeGen/CallingConvLower.h" 28 #include "llvm/CodeGen/IntrinsicLowering.h" 29 #include "llvm/CodeGen/MachineBasicBlock.h" 30 #include "llvm/CodeGen/MachineFrameInfo.h" 31 #include "llvm/CodeGen/MachineFunction.h" 32 #include "llvm/CodeGen/MachineInstrBuilder.h" 33 #include "llvm/CodeGen/MachineJumpTableInfo.h" 34 #include "llvm/CodeGen/MachineModuleInfo.h" 35 #include "llvm/CodeGen/MachineRegisterInfo.h" 36 #include "llvm/CodeGen/SelectionDAG.h" 37 #include "llvm/IR/CallingConv.h" 38 #include "llvm/IR/Constants.h" 39 #include "llvm/IR/Function.h" 40 #include "llvm/IR/GlobalValue.h" 41 #include "llvm/IR/IRBuilder.h" 42 #include "llvm/IR/Instruction.h" 43 #include "llvm/IR/Instructions.h" 44 #include "llvm/IR/IntrinsicInst.h" 45 #include "llvm/IR/Intrinsics.h" 46 #include "llvm/IR/Type.h" 47 #include "llvm/MC/MCSectionMachO.h" 48 #include "llvm/Support/CommandLine.h" 49 #include "llvm/Support/Debug.h" 50 #include "llvm/Support/ErrorHandling.h" 51 #include "llvm/Support/MathExtras.h" 52 #include "llvm/Support/raw_ostream.h" 53 #include "llvm/Target/TargetOptions.h" 54 #include <utility> 55 using namespace llvm; 56 57 #define DEBUG_TYPE "arm-isel" 58 59 STATISTIC(NumTailCalls, "Number of tail calls"); 60 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt"); 61 STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments"); 62 63 cl::opt<bool> 64 EnableARMLongCalls("arm-long-calls", cl::Hidden, 65 cl::desc("Generate calls via indirect call instructions"), 66 cl::init(false)); 67 68 static cl::opt<bool> 69 ARMInterworking("arm-interworking", cl::Hidden, 70 cl::desc("Enable / disable ARM interworking (for debugging only)"), 71 cl::init(true)); 72 73 namespace { 74 class ARMCCState : public CCState { 75 public: 76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, 77 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C, 78 ParmContext PC) 79 : CCState(CC, isVarArg, MF, locs, C) { 80 assert(((PC == Call) || (PC == Prologue)) && 81 "ARMCCState users must specify whether their context is call" 82 "or prologue generation."); 83 CallOrPrologue = PC; 84 } 85 }; 86 } 87 88 // The APCS parameter registers. 89 static const MCPhysReg GPRArgRegs[] = { 90 ARM::R0, ARM::R1, ARM::R2, ARM::R3 91 }; 92 93 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT, 94 MVT PromotedBitwiseVT) { 95 if (VT != PromotedLdStVT) { 96 setOperationAction(ISD::LOAD, VT, Promote); 97 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT); 98 99 setOperationAction(ISD::STORE, VT, Promote); 100 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT); 101 } 102 103 MVT ElemTy = VT.getVectorElementType(); 104 if (ElemTy != MVT::i64 && ElemTy != MVT::f64) 105 setOperationAction(ISD::SETCC, VT, Custom); 106 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 107 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 108 if (ElemTy == MVT::i32) { 109 setOperationAction(ISD::SINT_TO_FP, VT, Custom); 110 setOperationAction(ISD::UINT_TO_FP, VT, Custom); 111 setOperationAction(ISD::FP_TO_SINT, VT, Custom); 112 setOperationAction(ISD::FP_TO_UINT, VT, Custom); 113 } else { 114 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 115 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 116 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 117 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 118 } 119 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 120 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 121 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal); 122 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); 123 setOperationAction(ISD::SELECT, VT, Expand); 124 setOperationAction(ISD::SELECT_CC, VT, Expand); 125 setOperationAction(ISD::VSELECT, VT, Expand); 126 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 127 if (VT.isInteger()) { 128 setOperationAction(ISD::SHL, VT, Custom); 129 setOperationAction(ISD::SRA, VT, Custom); 130 setOperationAction(ISD::SRL, VT, Custom); 131 } 132 133 // Promote all bit-wise operations. 134 if (VT.isInteger() && VT != PromotedBitwiseVT) { 135 setOperationAction(ISD::AND, VT, Promote); 136 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT); 137 setOperationAction(ISD::OR, VT, Promote); 138 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT); 139 setOperationAction(ISD::XOR, VT, Promote); 140 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT); 141 } 142 143 // Neon does not support vector divide/remainder operations. 144 setOperationAction(ISD::SDIV, VT, Expand); 145 setOperationAction(ISD::UDIV, VT, Expand); 146 setOperationAction(ISD::FDIV, VT, Expand); 147 setOperationAction(ISD::SREM, VT, Expand); 148 setOperationAction(ISD::UREM, VT, Expand); 149 setOperationAction(ISD::FREM, VT, Expand); 150 } 151 152 void ARMTargetLowering::addDRTypeForNEON(MVT VT) { 153 addRegisterClass(VT, &ARM::DPRRegClass); 154 addTypeForNEON(VT, MVT::f64, MVT::v2i32); 155 } 156 157 void ARMTargetLowering::addQRTypeForNEON(MVT VT) { 158 addRegisterClass(VT, &ARM::DPairRegClass); 159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32); 160 } 161 162 ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM, 163 const ARMSubtarget &STI) 164 : TargetLowering(TM), Subtarget(&STI) { 165 RegInfo = Subtarget->getRegisterInfo(); 166 Itins = Subtarget->getInstrItineraryData(); 167 168 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); 169 170 if (Subtarget->isTargetMachO()) { 171 // Uses VFP for Thumb libfuncs if available. 172 if (Subtarget->isThumb() && Subtarget->hasVFP2() && 173 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) { 174 // Single-precision floating-point arithmetic. 175 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp"); 176 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp"); 177 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp"); 178 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp"); 179 180 // Double-precision floating-point arithmetic. 181 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp"); 182 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp"); 183 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp"); 184 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp"); 185 186 // Single-precision comparisons. 187 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp"); 188 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp"); 189 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp"); 190 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp"); 191 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp"); 192 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp"); 193 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp"); 194 setLibcallName(RTLIB::O_F32, "__unordsf2vfp"); 195 196 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE); 197 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE); 198 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE); 199 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE); 200 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE); 201 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE); 202 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE); 203 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ); 204 205 // Double-precision comparisons. 206 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp"); 207 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp"); 208 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp"); 209 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp"); 210 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp"); 211 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp"); 212 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp"); 213 setLibcallName(RTLIB::O_F64, "__unorddf2vfp"); 214 215 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE); 216 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE); 217 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE); 218 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE); 219 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE); 220 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE); 221 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE); 222 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ); 223 224 // Floating-point to integer conversions. 225 // i64 conversions are done via library routines even when generating VFP 226 // instructions, so use the same ones. 227 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp"); 228 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp"); 229 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp"); 230 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp"); 231 232 // Conversions between floating types. 233 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp"); 234 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp"); 235 236 // Integer to floating-point conversions. 237 // i64 conversions are done via library routines even when generating VFP 238 // instructions, so use the same ones. 239 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 240 // e.g., __floatunsidf vs. __floatunssidfvfp. 241 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp"); 242 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp"); 243 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp"); 244 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp"); 245 } 246 } 247 248 // These libcalls are not available in 32-bit. 249 setLibcallName(RTLIB::SHL_I128, nullptr); 250 setLibcallName(RTLIB::SRL_I128, nullptr); 251 setLibcallName(RTLIB::SRA_I128, nullptr); 252 253 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() && 254 !Subtarget->isTargetWindows()) { 255 static const struct { 256 const RTLIB::Libcall Op; 257 const char * const Name; 258 const CallingConv::ID CC; 259 const ISD::CondCode Cond; 260 } LibraryCalls[] = { 261 // Double-precision floating-point arithmetic helper functions 262 // RTABI chapter 4.1.2, Table 2 263 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 264 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 265 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 266 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 267 268 // Double-precision floating-point comparison helper functions 269 // RTABI chapter 4.1.2, Table 3 270 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 271 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 272 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 273 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 274 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 275 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 276 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 277 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 278 279 // Single-precision floating-point arithmetic helper functions 280 // RTABI chapter 4.1.2, Table 4 281 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 282 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 283 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 284 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 285 286 // Single-precision floating-point comparison helper functions 287 // RTABI chapter 4.1.2, Table 5 288 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 289 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 290 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 291 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 292 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 293 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 294 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 295 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 296 297 // Floating-point to integer conversions. 298 // RTABI chapter 4.1.2, Table 6 299 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 300 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 301 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 302 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 303 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 304 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 305 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 306 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 307 308 // Conversions between floating types. 309 // RTABI chapter 4.1.2, Table 7 310 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 311 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 312 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 313 314 // Integer to floating-point conversions. 315 // RTABI chapter 4.1.2, Table 8 316 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 317 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 318 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 319 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 320 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 321 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 322 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 323 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 324 325 // Long long helper functions 326 // RTABI chapter 4.2, Table 9 327 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 328 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 329 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 330 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 331 332 // Integer division functions 333 // RTABI chapter 4.3.1 334 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 335 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 336 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 337 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 338 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 339 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 340 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 341 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 342 343 // Memory operations 344 // RTABI chapter 4.3.4 345 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 346 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 347 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 348 }; 349 350 for (const auto &LC : LibraryCalls) { 351 setLibcallName(LC.Op, LC.Name); 352 setLibcallCallingConv(LC.Op, LC.CC); 353 if (LC.Cond != ISD::SETCC_INVALID) 354 setCmpLibcallCC(LC.Op, LC.Cond); 355 } 356 } 357 358 if (Subtarget->isTargetWindows()) { 359 static const struct { 360 const RTLIB::Libcall Op; 361 const char * const Name; 362 const CallingConv::ID CC; 363 } LibraryCalls[] = { 364 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP }, 365 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP }, 366 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP }, 367 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP }, 368 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP }, 369 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP }, 370 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP }, 371 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP }, 372 }; 373 374 for (const auto &LC : LibraryCalls) { 375 setLibcallName(LC.Op, LC.Name); 376 setLibcallCallingConv(LC.Op, LC.CC); 377 } 378 } 379 380 // Use divmod compiler-rt calls for iOS 5.0 and later. 381 if (Subtarget->getTargetTriple().isiOS() && 382 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) { 383 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 384 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 385 } 386 387 // The half <-> float conversion functions are always soft-float, but are 388 // needed for some targets which use a hard-float calling convention by 389 // default. 390 if (Subtarget->isAAPCS_ABI()) { 391 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS); 392 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS); 393 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS); 394 } else { 395 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS); 396 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS); 397 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS); 398 } 399 400 if (Subtarget->isThumb1Only()) 401 addRegisterClass(MVT::i32, &ARM::tGPRRegClass); 402 else 403 addRegisterClass(MVT::i32, &ARM::GPRRegClass); 404 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 405 !Subtarget->isThumb1Only()) { 406 addRegisterClass(MVT::f32, &ARM::SPRRegClass); 407 addRegisterClass(MVT::f64, &ARM::DPRRegClass); 408 } 409 410 for (MVT VT : MVT::vector_valuetypes()) { 411 for (MVT InnerVT : MVT::vector_valuetypes()) { 412 setTruncStoreAction(VT, InnerVT, Expand); 413 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 414 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 415 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 416 } 417 418 setOperationAction(ISD::MULHS, VT, Expand); 419 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 420 setOperationAction(ISD::MULHU, VT, Expand); 421 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 422 423 setOperationAction(ISD::BSWAP, VT, Expand); 424 } 425 426 setOperationAction(ISD::ConstantFP, MVT::f32, Custom); 427 setOperationAction(ISD::ConstantFP, MVT::f64, Custom); 428 429 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom); 430 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom); 431 432 if (Subtarget->hasNEON()) { 433 addDRTypeForNEON(MVT::v2f32); 434 addDRTypeForNEON(MVT::v8i8); 435 addDRTypeForNEON(MVT::v4i16); 436 addDRTypeForNEON(MVT::v2i32); 437 addDRTypeForNEON(MVT::v1i64); 438 439 addQRTypeForNEON(MVT::v4f32); 440 addQRTypeForNEON(MVT::v2f64); 441 addQRTypeForNEON(MVT::v16i8); 442 addQRTypeForNEON(MVT::v8i16); 443 addQRTypeForNEON(MVT::v4i32); 444 addQRTypeForNEON(MVT::v2i64); 445 446 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 447 // neither Neon nor VFP support any arithmetic operations on it. 448 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively 449 // supported for v4f32. 450 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 451 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 452 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 453 // FIXME: Code duplication: FDIV and FREM are expanded always, see 454 // ARMTargetLowering::addTypeForNEON method for details. 455 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 456 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 457 // FIXME: Create unittest. 458 // In another words, find a way when "copysign" appears in DAG with vector 459 // operands. 460 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 461 // FIXME: Code duplication: SETCC has custom operation action, see 462 // ARMTargetLowering::addTypeForNEON method for details. 463 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 464 // FIXME: Create unittest for FNEG and for FABS. 465 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 466 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 467 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 468 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 469 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 470 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand); 471 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 472 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 473 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 474 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 475 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 476 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 477 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR. 478 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 479 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 480 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 481 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 482 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 483 setOperationAction(ISD::FMA, MVT::v2f64, Expand); 484 485 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 486 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 487 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 488 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand); 489 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 490 setOperationAction(ISD::FLOG, MVT::v4f32, Expand); 491 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand); 492 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand); 493 setOperationAction(ISD::FEXP, MVT::v4f32, Expand); 494 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand); 495 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand); 496 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand); 497 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 498 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 499 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand); 500 501 // Mark v2f32 intrinsics. 502 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand); 503 setOperationAction(ISD::FSIN, MVT::v2f32, Expand); 504 setOperationAction(ISD::FCOS, MVT::v2f32, Expand); 505 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand); 506 setOperationAction(ISD::FPOW, MVT::v2f32, Expand); 507 setOperationAction(ISD::FLOG, MVT::v2f32, Expand); 508 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand); 509 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand); 510 setOperationAction(ISD::FEXP, MVT::v2f32, Expand); 511 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand); 512 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand); 513 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand); 514 setOperationAction(ISD::FRINT, MVT::v2f32, Expand); 515 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand); 516 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand); 517 518 // Neon does not support some operations on v1i64 and v2i64 types. 519 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 520 // Custom handling for some quad-vector types to detect VMULL. 521 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 522 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 523 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 524 // Custom handling for some vector types to avoid expensive expansions 525 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 526 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 527 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 528 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 529 setOperationAction(ISD::SETCC, MVT::v1i64, Expand); 530 setOperationAction(ISD::SETCC, MVT::v2i64, Expand); 531 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 532 // a destination type that is wider than the source, and nor does 533 // it have a FP_TO_[SU]INT instruction with a narrower destination than 534 // source. 535 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 536 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 537 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 538 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 539 540 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 541 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand); 542 543 // NEON does not have single instruction CTPOP for vectors with element 544 // types wider than 8-bits. However, custom lowering can leverage the 545 // v8i8/v16i8 vcnt instruction. 546 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom); 547 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom); 548 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom); 549 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom); 550 551 // NEON only has FMA instructions as of VFP4. 552 if (!Subtarget->hasVFP4()) { 553 setOperationAction(ISD::FMA, MVT::v2f32, Expand); 554 setOperationAction(ISD::FMA, MVT::v4f32, Expand); 555 } 556 557 setTargetDAGCombine(ISD::INTRINSIC_VOID); 558 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 559 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 560 setTargetDAGCombine(ISD::SHL); 561 setTargetDAGCombine(ISD::SRL); 562 setTargetDAGCombine(ISD::SRA); 563 setTargetDAGCombine(ISD::SIGN_EXTEND); 564 setTargetDAGCombine(ISD::ZERO_EXTEND); 565 setTargetDAGCombine(ISD::ANY_EXTEND); 566 setTargetDAGCombine(ISD::SELECT_CC); 567 setTargetDAGCombine(ISD::BUILD_VECTOR); 568 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 569 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 570 setTargetDAGCombine(ISD::STORE); 571 setTargetDAGCombine(ISD::FP_TO_SINT); 572 setTargetDAGCombine(ISD::FP_TO_UINT); 573 setTargetDAGCombine(ISD::FDIV); 574 setTargetDAGCombine(ISD::LOAD); 575 576 // It is legal to extload from v4i8 to v4i16 or v4i32. 577 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16, 578 MVT::v2i32}) { 579 for (MVT VT : MVT::integer_vector_valuetypes()) { 580 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal); 581 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal); 582 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal); 583 } 584 } 585 } 586 587 // ARM and Thumb2 support UMLAL/SMLAL. 588 if (!Subtarget->isThumb1Only()) 589 setTargetDAGCombine(ISD::ADDC); 590 591 if (Subtarget->isFPOnlySP()) { 592 // When targetting a floating-point unit with only single-precision 593 // operations, f64 is legal for the few double-precision instructions which 594 // are present However, no double-precision operations other than moves, 595 // loads and stores are provided by the hardware. 596 setOperationAction(ISD::FADD, MVT::f64, Expand); 597 setOperationAction(ISD::FSUB, MVT::f64, Expand); 598 setOperationAction(ISD::FMUL, MVT::f64, Expand); 599 setOperationAction(ISD::FMA, MVT::f64, Expand); 600 setOperationAction(ISD::FDIV, MVT::f64, Expand); 601 setOperationAction(ISD::FREM, MVT::f64, Expand); 602 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 603 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand); 604 setOperationAction(ISD::FNEG, MVT::f64, Expand); 605 setOperationAction(ISD::FABS, MVT::f64, Expand); 606 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 607 setOperationAction(ISD::FSIN, MVT::f64, Expand); 608 setOperationAction(ISD::FCOS, MVT::f64, Expand); 609 setOperationAction(ISD::FPOWI, MVT::f64, Expand); 610 setOperationAction(ISD::FPOW, MVT::f64, Expand); 611 setOperationAction(ISD::FLOG, MVT::f64, Expand); 612 setOperationAction(ISD::FLOG2, MVT::f64, Expand); 613 setOperationAction(ISD::FLOG10, MVT::f64, Expand); 614 setOperationAction(ISD::FEXP, MVT::f64, Expand); 615 setOperationAction(ISD::FEXP2, MVT::f64, Expand); 616 setOperationAction(ISD::FCEIL, MVT::f64, Expand); 617 setOperationAction(ISD::FTRUNC, MVT::f64, Expand); 618 setOperationAction(ISD::FRINT, MVT::f64, Expand); 619 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand); 620 setOperationAction(ISD::FFLOOR, MVT::f64, Expand); 621 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 622 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 623 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 624 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 625 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom); 626 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom); 627 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); 628 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom); 629 } 630 631 computeRegisterProperties(Subtarget->getRegisterInfo()); 632 633 // ARM does not have floating-point extending loads. 634 for (MVT VT : MVT::fp_valuetypes()) { 635 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); 636 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand); 637 } 638 639 // ... or truncating stores 640 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 641 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 642 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 643 644 // ARM does not have i1 sign extending load. 645 for (MVT VT : MVT::integer_valuetypes()) 646 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 647 648 // ARM supports all 4 flavors of integer indexed load / store. 649 if (!Subtarget->isThumb1Only()) { 650 for (unsigned im = (unsigned)ISD::PRE_INC; 651 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 652 setIndexedLoadAction(im, MVT::i1, Legal); 653 setIndexedLoadAction(im, MVT::i8, Legal); 654 setIndexedLoadAction(im, MVT::i16, Legal); 655 setIndexedLoadAction(im, MVT::i32, Legal); 656 setIndexedStoreAction(im, MVT::i1, Legal); 657 setIndexedStoreAction(im, MVT::i8, Legal); 658 setIndexedStoreAction(im, MVT::i16, Legal); 659 setIndexedStoreAction(im, MVT::i32, Legal); 660 } 661 } 662 663 setOperationAction(ISD::SADDO, MVT::i32, Custom); 664 setOperationAction(ISD::UADDO, MVT::i32, Custom); 665 setOperationAction(ISD::SSUBO, MVT::i32, Custom); 666 setOperationAction(ISD::USUBO, MVT::i32, Custom); 667 668 // i64 operation support. 669 setOperationAction(ISD::MUL, MVT::i64, Expand); 670 setOperationAction(ISD::MULHU, MVT::i32, Expand); 671 if (Subtarget->isThumb1Only()) { 672 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 673 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 674 } 675 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 676 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP())) 677 setOperationAction(ISD::MULHS, MVT::i32, Expand); 678 679 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 680 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 681 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 682 setOperationAction(ISD::SRL, MVT::i64, Custom); 683 setOperationAction(ISD::SRA, MVT::i64, Custom); 684 685 if (!Subtarget->isThumb1Only()) { 686 // FIXME: We should do this for Thumb1 as well. 687 setOperationAction(ISD::ADDC, MVT::i32, Custom); 688 setOperationAction(ISD::ADDE, MVT::i32, Custom); 689 setOperationAction(ISD::SUBC, MVT::i32, Custom); 690 setOperationAction(ISD::SUBE, MVT::i32, Custom); 691 } 692 693 // ARM does not have ROTL. 694 setOperationAction(ISD::ROTL, MVT::i32, Expand); 695 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 696 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 697 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) 698 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 699 700 // These just redirect to CTTZ and CTLZ on ARM. 701 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand); 702 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand); 703 704 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom); 705 706 // Only ARMv6 has BSWAP. 707 if (!Subtarget->hasV6Ops()) 708 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 709 710 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) && 711 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) { 712 // These are expanded into libcalls if the cpu doesn't have HW divider. 713 setOperationAction(ISD::SDIV, MVT::i32, Expand); 714 setOperationAction(ISD::UDIV, MVT::i32, Expand); 715 } 716 717 // FIXME: Also set divmod for SREM on EABI 718 setOperationAction(ISD::SREM, MVT::i32, Expand); 719 setOperationAction(ISD::UREM, MVT::i32, Expand); 720 // Register based DivRem for AEABI (RTABI 4.2) 721 if (Subtarget->isTargetAEABI()) { 722 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod"); 723 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod"); 724 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod"); 725 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod"); 726 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod"); 727 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod"); 728 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod"); 729 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod"); 730 731 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS); 732 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS); 733 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS); 734 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS); 735 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS); 736 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS); 737 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS); 738 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS); 739 740 setOperationAction(ISD::SDIVREM, MVT::i32, Custom); 741 setOperationAction(ISD::UDIVREM, MVT::i32, Custom); 742 } else { 743 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 744 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 745 } 746 747 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 748 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 749 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom); 750 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 751 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 752 753 setOperationAction(ISD::TRAP, MVT::Other, Legal); 754 755 // Use the default implementation. 756 setOperationAction(ISD::VASTART, MVT::Other, Custom); 757 setOperationAction(ISD::VAARG, MVT::Other, Expand); 758 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 759 setOperationAction(ISD::VAEND, MVT::Other, Expand); 760 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 761 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 762 763 if (!Subtarget->isTargetMachO()) { 764 // Non-MachO platforms may return values in these registers via the 765 // personality function. 766 setExceptionPointerRegister(ARM::R0); 767 setExceptionSelectorRegister(ARM::R1); 768 } 769 770 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment()) 771 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 772 else 773 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 774 775 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 776 // the default expansion. If we are targeting a single threaded system, 777 // then set them all for expand so we can lower them later into their 778 // non-atomic form. 779 if (TM.Options.ThreadModel == ThreadModel::Single) 780 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand); 781 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) { 782 // ATOMIC_FENCE needs custom lowering; the others should have been expanded 783 // to ldrex/strex loops already. 784 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 785 786 // On v8, we have particularly efficient implementations of atomic fences 787 // if they can be combined with nearby atomic loads and stores. 788 if (!Subtarget->hasV8Ops()) { 789 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc. 790 setInsertFencesForAtomic(true); 791 } 792 } else { 793 // If there's anything we can use as a barrier, go through custom lowering 794 // for ATOMIC_FENCE. 795 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, 796 Subtarget->hasAnyDataBarrier() ? Custom : Expand); 797 798 // Set them all for expansion, which will force libcalls. 799 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 800 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 801 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 802 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 803 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 804 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 805 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 806 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 807 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 808 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 809 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 810 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 811 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 812 // Unordered/Monotonic case. 813 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 814 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 815 } 816 817 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 818 819 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 820 if (!Subtarget->hasV6Ops()) { 821 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 822 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 823 } 824 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 825 826 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 827 !Subtarget->isThumb1Only()) { 828 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 829 // iff target supports vfp2. 830 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 831 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 832 } 833 834 // We want to custom lower some of our intrinsics. 835 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 836 if (Subtarget->isTargetDarwin()) { 837 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 838 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 839 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 840 } 841 842 setOperationAction(ISD::SETCC, MVT::i32, Expand); 843 setOperationAction(ISD::SETCC, MVT::f32, Expand); 844 setOperationAction(ISD::SETCC, MVT::f64, Expand); 845 setOperationAction(ISD::SELECT, MVT::i32, Custom); 846 setOperationAction(ISD::SELECT, MVT::f32, Custom); 847 setOperationAction(ISD::SELECT, MVT::f64, Custom); 848 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 849 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 850 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 851 852 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 853 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 854 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 855 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 856 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 857 858 // We don't support sin/cos/fmod/copysign/pow 859 setOperationAction(ISD::FSIN, MVT::f64, Expand); 860 setOperationAction(ISD::FSIN, MVT::f32, Expand); 861 setOperationAction(ISD::FCOS, MVT::f32, Expand); 862 setOperationAction(ISD::FCOS, MVT::f64, Expand); 863 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 864 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 865 setOperationAction(ISD::FREM, MVT::f64, Expand); 866 setOperationAction(ISD::FREM, MVT::f32, Expand); 867 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 868 !Subtarget->isThumb1Only()) { 869 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 870 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 871 } 872 setOperationAction(ISD::FPOW, MVT::f64, Expand); 873 setOperationAction(ISD::FPOW, MVT::f32, Expand); 874 875 if (!Subtarget->hasVFP4()) { 876 setOperationAction(ISD::FMA, MVT::f64, Expand); 877 setOperationAction(ISD::FMA, MVT::f32, Expand); 878 } 879 880 // Various VFP goodness 881 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) { 882 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded. 883 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) { 884 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 885 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand); 886 } 887 888 // fp16 is a special v7 extension that adds f16 <-> f32 conversions. 889 if (!Subtarget->hasFP16()) { 890 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand); 891 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand); 892 } 893 } 894 895 // Combine sin / cos into one node or libcall if possible. 896 if (Subtarget->hasSinCos()) { 897 setLibcallName(RTLIB::SINCOS_F32, "sincosf"); 898 setLibcallName(RTLIB::SINCOS_F64, "sincos"); 899 if (Subtarget->getTargetTriple().isiOS()) { 900 // For iOS, we don't want to the normal expansion of a libcall to 901 // sincos. We want to issue a libcall to __sincos_stret. 902 setOperationAction(ISD::FSINCOS, MVT::f64, Custom); 903 setOperationAction(ISD::FSINCOS, MVT::f32, Custom); 904 } 905 } 906 907 // FP-ARMv8 implements a lot of rounding-like FP operations. 908 if (Subtarget->hasFPARMv8()) { 909 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 910 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 911 setOperationAction(ISD::FROUND, MVT::f32, Legal); 912 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 913 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal); 914 setOperationAction(ISD::FRINT, MVT::f32, Legal); 915 if (!Subtarget->isFPOnlySP()) { 916 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 917 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 918 setOperationAction(ISD::FROUND, MVT::f64, Legal); 919 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 920 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal); 921 setOperationAction(ISD::FRINT, MVT::f64, Legal); 922 } 923 } 924 // We have target-specific dag combine patterns for the following nodes: 925 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 926 setTargetDAGCombine(ISD::ADD); 927 setTargetDAGCombine(ISD::SUB); 928 setTargetDAGCombine(ISD::MUL); 929 setTargetDAGCombine(ISD::AND); 930 setTargetDAGCombine(ISD::OR); 931 setTargetDAGCombine(ISD::XOR); 932 933 if (Subtarget->hasV6Ops()) 934 setTargetDAGCombine(ISD::SRL); 935 936 setStackPointerRegisterToSaveRestore(ARM::SP); 937 938 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() || 939 !Subtarget->hasVFP2()) 940 setSchedulingPreference(Sched::RegPressure); 941 else 942 setSchedulingPreference(Sched::Hybrid); 943 944 //// temporary - rewrite interface to use type 945 MaxStoresPerMemset = 8; 946 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4; 947 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores 948 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2; 949 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores 950 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2; 951 952 // On ARM arguments smaller than 4 bytes are extended, so all arguments 953 // are at least 4 bytes aligned. 954 setMinStackArgumentAlignment(4); 955 956 // Prefer likely predicted branches to selects on out-of-order cores. 957 PredictableSelectIsExpensive = Subtarget->isLikeA9(); 958 959 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2); 960 } 961 962 bool ARMTargetLowering::useSoftFloat() const { 963 return Subtarget->useSoftFloat(); 964 } 965 966 // FIXME: It might make sense to define the representative register class as the 967 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 968 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 969 // SPR's representative would be DPR_VFP2. This should work well if register 970 // pressure tracking were modified such that a register use would increment the 971 // pressure of the register class's representative and all of it's super 972 // classes' representatives transitively. We have not implemented this because 973 // of the difficulty prior to coalescing of modeling operand register classes 974 // due to the common occurrence of cross class copies and subregister insertions 975 // and extractions. 976 std::pair<const TargetRegisterClass *, uint8_t> 977 ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI, 978 MVT VT) const { 979 const TargetRegisterClass *RRC = nullptr; 980 uint8_t Cost = 1; 981 switch (VT.SimpleTy) { 982 default: 983 return TargetLowering::findRepresentativeClass(TRI, VT); 984 // Use DPR as representative register class for all floating point 985 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 986 // the cost is 1 for both f32 and f64. 987 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 988 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 989 RRC = &ARM::DPRRegClass; 990 // When NEON is used for SP, only half of the register file is available 991 // because operations that define both SP and DP results will be constrained 992 // to the VFP2 class (D0-D15). We currently model this constraint prior to 993 // coalescing by double-counting the SP regs. See the FIXME above. 994 if (Subtarget->useNEONForSinglePrecisionFP()) 995 Cost = 2; 996 break; 997 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 998 case MVT::v4f32: case MVT::v2f64: 999 RRC = &ARM::DPRRegClass; 1000 Cost = 2; 1001 break; 1002 case MVT::v4i64: 1003 RRC = &ARM::DPRRegClass; 1004 Cost = 4; 1005 break; 1006 case MVT::v8i64: 1007 RRC = &ARM::DPRRegClass; 1008 Cost = 8; 1009 break; 1010 } 1011 return std::make_pair(RRC, Cost); 1012 } 1013 1014 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 1015 switch ((ARMISD::NodeType)Opcode) { 1016 case ARMISD::FIRST_NUMBER: break; 1017 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 1018 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 1019 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 1020 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL"; 1021 case ARMISD::CALL: return "ARMISD::CALL"; 1022 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 1023 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 1024 case ARMISD::tCALL: return "ARMISD::tCALL"; 1025 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 1026 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 1027 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 1028 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 1029 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG"; 1030 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 1031 case ARMISD::CMP: return "ARMISD::CMP"; 1032 case ARMISD::CMN: return "ARMISD::CMN"; 1033 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 1034 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 1035 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 1036 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 1037 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 1038 1039 case ARMISD::CMOV: return "ARMISD::CMOV"; 1040 1041 case ARMISD::RBIT: return "ARMISD::RBIT"; 1042 1043 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 1044 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 1045 case ARMISD::RRX: return "ARMISD::RRX"; 1046 1047 case ARMISD::ADDC: return "ARMISD::ADDC"; 1048 case ARMISD::ADDE: return "ARMISD::ADDE"; 1049 case ARMISD::SUBC: return "ARMISD::SUBC"; 1050 case ARMISD::SUBE: return "ARMISD::SUBE"; 1051 1052 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 1053 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 1054 1055 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 1056 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP"; 1057 1058 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 1059 1060 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 1061 1062 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 1063 1064 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 1065 1066 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 1067 1068 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK"; 1069 1070 case ARMISD::VCEQ: return "ARMISD::VCEQ"; 1071 case ARMISD::VCEQZ: return "ARMISD::VCEQZ"; 1072 case ARMISD::VCGE: return "ARMISD::VCGE"; 1073 case ARMISD::VCGEZ: return "ARMISD::VCGEZ"; 1074 case ARMISD::VCLEZ: return "ARMISD::VCLEZ"; 1075 case ARMISD::VCGEU: return "ARMISD::VCGEU"; 1076 case ARMISD::VCGT: return "ARMISD::VCGT"; 1077 case ARMISD::VCGTZ: return "ARMISD::VCGTZ"; 1078 case ARMISD::VCLTZ: return "ARMISD::VCLTZ"; 1079 case ARMISD::VCGTU: return "ARMISD::VCGTU"; 1080 case ARMISD::VTST: return "ARMISD::VTST"; 1081 1082 case ARMISD::VSHL: return "ARMISD::VSHL"; 1083 case ARMISD::VSHRs: return "ARMISD::VSHRs"; 1084 case ARMISD::VSHRu: return "ARMISD::VSHRu"; 1085 case ARMISD::VRSHRs: return "ARMISD::VRSHRs"; 1086 case ARMISD::VRSHRu: return "ARMISD::VRSHRu"; 1087 case ARMISD::VRSHRN: return "ARMISD::VRSHRN"; 1088 case ARMISD::VQSHLs: return "ARMISD::VQSHLs"; 1089 case ARMISD::VQSHLu: return "ARMISD::VQSHLu"; 1090 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu"; 1091 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs"; 1092 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu"; 1093 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu"; 1094 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs"; 1095 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu"; 1096 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu"; 1097 case ARMISD::VSLI: return "ARMISD::VSLI"; 1098 case ARMISD::VSRI: return "ARMISD::VSRI"; 1099 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 1100 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 1101 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 1102 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 1103 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 1104 case ARMISD::VDUP: return "ARMISD::VDUP"; 1105 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 1106 case ARMISD::VEXT: return "ARMISD::VEXT"; 1107 case ARMISD::VREV64: return "ARMISD::VREV64"; 1108 case ARMISD::VREV32: return "ARMISD::VREV32"; 1109 case ARMISD::VREV16: return "ARMISD::VREV16"; 1110 case ARMISD::VZIP: return "ARMISD::VZIP"; 1111 case ARMISD::VUZP: return "ARMISD::VUZP"; 1112 case ARMISD::VTRN: return "ARMISD::VTRN"; 1113 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 1114 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 1115 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 1116 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 1117 case ARMISD::UMLAL: return "ARMISD::UMLAL"; 1118 case ARMISD::SMLAL: return "ARMISD::SMLAL"; 1119 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 1120 case ARMISD::FMAX: return "ARMISD::FMAX"; 1121 case ARMISD::FMIN: return "ARMISD::FMIN"; 1122 case ARMISD::VMAXNM: return "ARMISD::VMAX"; 1123 case ARMISD::VMINNM: return "ARMISD::VMIN"; 1124 case ARMISD::BFI: return "ARMISD::BFI"; 1125 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 1126 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 1127 case ARMISD::VBSL: return "ARMISD::VBSL"; 1128 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 1129 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 1130 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 1131 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 1132 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 1133 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 1134 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 1135 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 1136 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 1137 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 1138 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 1139 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 1140 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 1141 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 1142 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 1143 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 1144 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 1145 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 1146 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 1147 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 1148 } 1149 return nullptr; 1150 } 1151 1152 EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { 1153 if (!VT.isVector()) return getPointerTy(); 1154 return VT.changeVectorElementTypeToInteger(); 1155 } 1156 1157 /// getRegClassFor - Return the register class that should be used for the 1158 /// specified value type. 1159 const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const { 1160 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 1161 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 1162 // load / store 4 to 8 consecutive D registers. 1163 if (Subtarget->hasNEON()) { 1164 if (VT == MVT::v4i64) 1165 return &ARM::QQPRRegClass; 1166 if (VT == MVT::v8i64) 1167 return &ARM::QQQQPRRegClass; 1168 } 1169 return TargetLowering::getRegClassFor(VT); 1170 } 1171 1172 // memcpy, and other memory intrinsics, typically tries to use LDM/STM if the 1173 // source/dest is aligned and the copy size is large enough. We therefore want 1174 // to align such objects passed to memory intrinsics. 1175 bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize, 1176 unsigned &PrefAlign) const { 1177 if (!isa<MemIntrinsic>(CI)) 1178 return false; 1179 MinSize = 8; 1180 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1 1181 // cycle faster than 4-byte aligned LDM. 1182 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4); 1183 return true; 1184 } 1185 1186 // Create a fast isel object. 1187 FastISel * 1188 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 1189 const TargetLibraryInfo *libInfo) const { 1190 return ARM::createFastISel(funcInfo, libInfo); 1191 } 1192 1193 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 1194 unsigned NumVals = N->getNumValues(); 1195 if (!NumVals) 1196 return Sched::RegPressure; 1197 1198 for (unsigned i = 0; i != NumVals; ++i) { 1199 EVT VT = N->getValueType(i); 1200 if (VT == MVT::Glue || VT == MVT::Other) 1201 continue; 1202 if (VT.isFloatingPoint() || VT.isVector()) 1203 return Sched::ILP; 1204 } 1205 1206 if (!N->isMachineOpcode()) 1207 return Sched::RegPressure; 1208 1209 // Load are scheduled for latency even if there instruction itinerary 1210 // is not available. 1211 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 1212 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1213 1214 if (MCID.getNumDefs() == 0) 1215 return Sched::RegPressure; 1216 if (!Itins->isEmpty() && 1217 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1218 return Sched::ILP; 1219 1220 return Sched::RegPressure; 1221 } 1222 1223 //===----------------------------------------------------------------------===// 1224 // Lowering Code 1225 //===----------------------------------------------------------------------===// 1226 1227 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1228 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1229 switch (CC) { 1230 default: llvm_unreachable("Unknown condition code!"); 1231 case ISD::SETNE: return ARMCC::NE; 1232 case ISD::SETEQ: return ARMCC::EQ; 1233 case ISD::SETGT: return ARMCC::GT; 1234 case ISD::SETGE: return ARMCC::GE; 1235 case ISD::SETLT: return ARMCC::LT; 1236 case ISD::SETLE: return ARMCC::LE; 1237 case ISD::SETUGT: return ARMCC::HI; 1238 case ISD::SETUGE: return ARMCC::HS; 1239 case ISD::SETULT: return ARMCC::LO; 1240 case ISD::SETULE: return ARMCC::LS; 1241 } 1242 } 1243 1244 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1245 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1246 ARMCC::CondCodes &CondCode2) { 1247 CondCode2 = ARMCC::AL; 1248 switch (CC) { 1249 default: llvm_unreachable("Unknown FP condition!"); 1250 case ISD::SETEQ: 1251 case ISD::SETOEQ: CondCode = ARMCC::EQ; break; 1252 case ISD::SETGT: 1253 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1254 case ISD::SETGE: 1255 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1256 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1257 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1258 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break; 1259 case ISD::SETO: CondCode = ARMCC::VC; break; 1260 case ISD::SETUO: CondCode = ARMCC::VS; break; 1261 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break; 1262 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1263 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1264 case ISD::SETLT: 1265 case ISD::SETULT: CondCode = ARMCC::LT; break; 1266 case ISD::SETLE: 1267 case ISD::SETULE: CondCode = ARMCC::LE; break; 1268 case ISD::SETNE: 1269 case ISD::SETUNE: CondCode = ARMCC::NE; break; 1270 } 1271 } 1272 1273 //===----------------------------------------------------------------------===// 1274 // Calling Convention Implementation 1275 //===----------------------------------------------------------------------===// 1276 1277 #include "ARMGenCallingConv.inc" 1278 1279 /// getEffectiveCallingConv - Get the effective calling convention, taking into 1280 /// account presence of floating point hardware and calling convention 1281 /// limitations, such as support for variadic functions. 1282 CallingConv::ID 1283 ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC, 1284 bool isVarArg) const { 1285 switch (CC) { 1286 default: 1287 llvm_unreachable("Unsupported calling convention"); 1288 case CallingConv::ARM_AAPCS: 1289 case CallingConv::ARM_APCS: 1290 case CallingConv::GHC: 1291 return CC; 1292 case CallingConv::ARM_AAPCS_VFP: 1293 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP; 1294 case CallingConv::C: 1295 if (!Subtarget->isAAPCS_ABI()) 1296 return CallingConv::ARM_APCS; 1297 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && 1298 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1299 !isVarArg) 1300 return CallingConv::ARM_AAPCS_VFP; 1301 else 1302 return CallingConv::ARM_AAPCS; 1303 case CallingConv::Fast: 1304 if (!Subtarget->isAAPCS_ABI()) { 1305 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg) 1306 return CallingConv::Fast; 1307 return CallingConv::ARM_APCS; 1308 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg) 1309 return CallingConv::ARM_AAPCS_VFP; 1310 else 1311 return CallingConv::ARM_AAPCS; 1312 } 1313 } 1314 1315 /// CCAssignFnForNode - Selects the correct CCAssignFn for the given 1316 /// CallingConvention. 1317 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1318 bool Return, 1319 bool isVarArg) const { 1320 switch (getEffectiveCallingConv(CC, isVarArg)) { 1321 default: 1322 llvm_unreachable("Unsupported calling convention"); 1323 case CallingConv::ARM_APCS: 1324 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1325 case CallingConv::ARM_AAPCS: 1326 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1327 case CallingConv::ARM_AAPCS_VFP: 1328 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1329 case CallingConv::Fast: 1330 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1331 case CallingConv::GHC: 1332 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC); 1333 } 1334 } 1335 1336 /// LowerCallResult - Lower the result values of a call into the 1337 /// appropriate copies out of appropriate physical registers. 1338 SDValue 1339 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 1340 CallingConv::ID CallConv, bool isVarArg, 1341 const SmallVectorImpl<ISD::InputArg> &Ins, 1342 SDLoc dl, SelectionDAG &DAG, 1343 SmallVectorImpl<SDValue> &InVals, 1344 bool isThisReturn, SDValue ThisVal) const { 1345 1346 // Assign locations to each value returned by this call. 1347 SmallVector<CCValAssign, 16> RVLocs; 1348 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1349 *DAG.getContext(), Call); 1350 CCInfo.AnalyzeCallResult(Ins, 1351 CCAssignFnForNode(CallConv, /* Return*/ true, 1352 isVarArg)); 1353 1354 // Copy all of the result registers out of their specified physreg. 1355 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1356 CCValAssign VA = RVLocs[i]; 1357 1358 // Pass 'this' value directly from the argument to return value, to avoid 1359 // reg unit interference 1360 if (i == 0 && isThisReturn) { 1361 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 && 1362 "unexpected return calling convention register assignment"); 1363 InVals.push_back(ThisVal); 1364 continue; 1365 } 1366 1367 SDValue Val; 1368 if (VA.needsCustom()) { 1369 // Handle f64 or half of a v2f64. 1370 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1371 InFlag); 1372 Chain = Lo.getValue(1); 1373 InFlag = Lo.getValue(2); 1374 VA = RVLocs[++i]; // skip ahead to next loc 1375 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1376 InFlag); 1377 Chain = Hi.getValue(1); 1378 InFlag = Hi.getValue(2); 1379 if (!Subtarget->isLittle()) 1380 std::swap (Lo, Hi); 1381 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1382 1383 if (VA.getLocVT() == MVT::v2f64) { 1384 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 1385 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1386 DAG.getConstant(0, dl, MVT::i32)); 1387 1388 VA = RVLocs[++i]; // skip ahead to next loc 1389 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1390 Chain = Lo.getValue(1); 1391 InFlag = Lo.getValue(2); 1392 VA = RVLocs[++i]; // skip ahead to next loc 1393 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1394 Chain = Hi.getValue(1); 1395 InFlag = Hi.getValue(2); 1396 if (!Subtarget->isLittle()) 1397 std::swap (Lo, Hi); 1398 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1399 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1400 DAG.getConstant(1, dl, MVT::i32)); 1401 } 1402 } else { 1403 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1404 InFlag); 1405 Chain = Val.getValue(1); 1406 InFlag = Val.getValue(2); 1407 } 1408 1409 switch (VA.getLocInfo()) { 1410 default: llvm_unreachable("Unknown loc info!"); 1411 case CCValAssign::Full: break; 1412 case CCValAssign::BCvt: 1413 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1414 break; 1415 } 1416 1417 InVals.push_back(Val); 1418 } 1419 1420 return Chain; 1421 } 1422 1423 /// LowerMemOpCallTo - Store the argument to the stack. 1424 SDValue 1425 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, 1426 SDValue StackPtr, SDValue Arg, 1427 SDLoc dl, SelectionDAG &DAG, 1428 const CCValAssign &VA, 1429 ISD::ArgFlagsTy Flags) const { 1430 unsigned LocMemOffset = VA.getLocMemOffset(); 1431 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 1432 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); 1433 return DAG.getStore(Chain, dl, Arg, PtrOff, 1434 MachinePointerInfo::getStack(LocMemOffset), 1435 false, false, 0); 1436 } 1437 1438 void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG, 1439 SDValue Chain, SDValue &Arg, 1440 RegsToPassVector &RegsToPass, 1441 CCValAssign &VA, CCValAssign &NextVA, 1442 SDValue &StackPtr, 1443 SmallVectorImpl<SDValue> &MemOpChains, 1444 ISD::ArgFlagsTy Flags) const { 1445 1446 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1447 DAG.getVTList(MVT::i32, MVT::i32), Arg); 1448 unsigned id = Subtarget->isLittle() ? 0 : 1; 1449 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id))); 1450 1451 if (NextVA.isRegLoc()) 1452 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id))); 1453 else { 1454 assert(NextVA.isMemLoc()); 1455 if (!StackPtr.getNode()) 1456 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1457 1458 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id), 1459 dl, DAG, NextVA, 1460 Flags)); 1461 } 1462 } 1463 1464 /// LowerCall - Lowering a call into a callseq_start <- 1465 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 1466 /// nodes. 1467 SDValue 1468 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 1469 SmallVectorImpl<SDValue> &InVals) const { 1470 SelectionDAG &DAG = CLI.DAG; 1471 SDLoc &dl = CLI.DL; 1472 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1473 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1474 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1475 SDValue Chain = CLI.Chain; 1476 SDValue Callee = CLI.Callee; 1477 bool &isTailCall = CLI.IsTailCall; 1478 CallingConv::ID CallConv = CLI.CallConv; 1479 bool doesNotRet = CLI.DoesNotReturn; 1480 bool isVarArg = CLI.IsVarArg; 1481 1482 MachineFunction &MF = DAG.getMachineFunction(); 1483 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 1484 bool isThisReturn = false; 1485 bool isSibCall = false; 1486 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls"); 1487 1488 // Disable tail calls if they're not supported. 1489 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true") 1490 isTailCall = false; 1491 1492 if (isTailCall) { 1493 // Check if it's really possible to do a tail call. 1494 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, 1495 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(), 1496 Outs, OutVals, Ins, DAG); 1497 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall()) 1498 report_fatal_error("failed to perform tail call elimination on a call " 1499 "site marked musttail"); 1500 // We don't support GuaranteedTailCallOpt for ARM, only automatically 1501 // detected sibcalls. 1502 if (isTailCall) { 1503 ++NumTailCalls; 1504 isSibCall = true; 1505 } 1506 } 1507 1508 // Analyze operands of the call, assigning locations to each operand. 1509 SmallVector<CCValAssign, 16> ArgLocs; 1510 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1511 *DAG.getContext(), Call); 1512 CCInfo.AnalyzeCallOperands(Outs, 1513 CCAssignFnForNode(CallConv, /* Return*/ false, 1514 isVarArg)); 1515 1516 // Get a count of how many bytes are to be pushed on the stack. 1517 unsigned NumBytes = CCInfo.getNextStackOffset(); 1518 1519 // For tail calls, memory operands are available in our caller's stack. 1520 if (isSibCall) 1521 NumBytes = 0; 1522 1523 // Adjust the stack pointer for the new arguments... 1524 // These operations are automatically eliminated by the prolog/epilog pass 1525 if (!isSibCall) 1526 Chain = DAG.getCALLSEQ_START(Chain, 1527 DAG.getIntPtrConstant(NumBytes, dl, true), dl); 1528 1529 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy()); 1530 1531 RegsToPassVector RegsToPass; 1532 SmallVector<SDValue, 8> MemOpChains; 1533 1534 // Walk the register/memloc assignments, inserting copies/loads. In the case 1535 // of tail call optimization, arguments are handled later. 1536 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1537 i != e; 1538 ++i, ++realArgIdx) { 1539 CCValAssign &VA = ArgLocs[i]; 1540 SDValue Arg = OutVals[realArgIdx]; 1541 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1542 bool isByVal = Flags.isByVal(); 1543 1544 // Promote the value if needed. 1545 switch (VA.getLocInfo()) { 1546 default: llvm_unreachable("Unknown loc info!"); 1547 case CCValAssign::Full: break; 1548 case CCValAssign::SExt: 1549 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 1550 break; 1551 case CCValAssign::ZExt: 1552 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 1553 break; 1554 case CCValAssign::AExt: 1555 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1556 break; 1557 case CCValAssign::BCvt: 1558 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1559 break; 1560 } 1561 1562 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 1563 if (VA.needsCustom()) { 1564 if (VA.getLocVT() == MVT::v2f64) { 1565 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1566 DAG.getConstant(0, dl, MVT::i32)); 1567 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1568 DAG.getConstant(1, dl, MVT::i32)); 1569 1570 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 1571 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1572 1573 VA = ArgLocs[++i]; // skip ahead to next loc 1574 if (VA.isRegLoc()) { 1575 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 1576 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1577 } else { 1578 assert(VA.isMemLoc()); 1579 1580 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 1581 dl, DAG, VA, Flags)); 1582 } 1583 } else { 1584 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 1585 StackPtr, MemOpChains, Flags); 1586 } 1587 } else if (VA.isRegLoc()) { 1588 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) { 1589 assert(VA.getLocVT() == MVT::i32 && 1590 "unexpected calling convention register assignment"); 1591 assert(!Ins.empty() && Ins[0].VT == MVT::i32 && 1592 "unexpected use of 'returned'"); 1593 isThisReturn = true; 1594 } 1595 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1596 } else if (isByVal) { 1597 assert(VA.isMemLoc()); 1598 unsigned offset = 0; 1599 1600 // True if this byval aggregate will be split between registers 1601 // and memory. 1602 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount(); 1603 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed(); 1604 1605 if (CurByValIdx < ByValArgsCount) { 1606 1607 unsigned RegBegin, RegEnd; 1608 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd); 1609 1610 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 1611 unsigned int i, j; 1612 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) { 1613 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32); 1614 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 1615 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 1616 MachinePointerInfo(), 1617 false, false, false, 1618 DAG.InferPtrAlignment(AddArg)); 1619 MemOpChains.push_back(Load.getValue(1)); 1620 RegsToPass.push_back(std::make_pair(j, Load)); 1621 } 1622 1623 // If parameter size outsides register area, "offset" value 1624 // helps us to calculate stack slot for remained part properly. 1625 offset = RegEnd - RegBegin; 1626 1627 CCInfo.nextInRegsParam(); 1628 } 1629 1630 if (Flags.getByValSize() > 4*offset) { 1631 unsigned LocMemOffset = VA.getLocMemOffset(); 1632 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 1633 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, 1634 StkPtrOff); 1635 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl); 1636 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset); 1637 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl, 1638 MVT::i32); 1639 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl, 1640 MVT::i32); 1641 1642 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 1643 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode}; 1644 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, 1645 Ops)); 1646 } 1647 } else if (!isSibCall) { 1648 assert(VA.isMemLoc()); 1649 1650 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 1651 dl, DAG, VA, Flags)); 1652 } 1653 } 1654 1655 if (!MemOpChains.empty()) 1656 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 1657 1658 // Build a sequence of copy-to-reg nodes chained together with token chain 1659 // and flag operands which copy the outgoing args into the appropriate regs. 1660 SDValue InFlag; 1661 // Tail call byval lowering might overwrite argument registers so in case of 1662 // tail call optimization the copies to registers are lowered later. 1663 if (!isTailCall) 1664 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1665 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1666 RegsToPass[i].second, InFlag); 1667 InFlag = Chain.getValue(1); 1668 } 1669 1670 // For tail calls lower the arguments to the 'real' stack slot. 1671 if (isTailCall) { 1672 // Force all the incoming stack arguments to be loaded from the stack 1673 // before any new outgoing arguments are stored to the stack, because the 1674 // outgoing stack slots may alias the incoming argument stack slots, and 1675 // the alias isn't otherwise explicit. This is slightly more conservative 1676 // than necessary, because it means that each store effectively depends 1677 // on every argument instead of just those arguments it would clobber. 1678 1679 // Do not flag preceding copytoreg stuff together with the following stuff. 1680 InFlag = SDValue(); 1681 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1682 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1683 RegsToPass[i].second, InFlag); 1684 InFlag = Chain.getValue(1); 1685 } 1686 InFlag = SDValue(); 1687 } 1688 1689 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 1690 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 1691 // node so that legalize doesn't hack it. 1692 bool isDirect = false; 1693 bool isARMFunc = false; 1694 bool isLocalARMFunc = false; 1695 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1696 1697 if (EnableARMLongCalls) { 1698 assert((Subtarget->isTargetWindows() || 1699 getTargetMachine().getRelocationModel() == Reloc::Static) && 1700 "long-calls with non-static relocation model!"); 1701 // Handle a global address or an external symbol. If it's not one of 1702 // those, the target's already in a register, so we don't need to do 1703 // anything extra. 1704 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1705 const GlobalValue *GV = G->getGlobal(); 1706 // Create a constant pool entry for the callee address 1707 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1708 ARMConstantPoolValue *CPV = 1709 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 1710 1711 // Get the address of the callee into a register 1712 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1713 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1714 Callee = DAG.getLoad(getPointerTy(), dl, 1715 DAG.getEntryNode(), CPAddr, 1716 MachinePointerInfo::getConstantPool(), 1717 false, false, false, 0); 1718 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 1719 const char *Sym = S->getSymbol(); 1720 1721 // Create a constant pool entry for the callee address 1722 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1723 ARMConstantPoolValue *CPV = 1724 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1725 ARMPCLabelIndex, 0); 1726 // Get the address of the callee into a register 1727 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1728 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1729 Callee = DAG.getLoad(getPointerTy(), dl, 1730 DAG.getEntryNode(), CPAddr, 1731 MachinePointerInfo::getConstantPool(), 1732 false, false, false, 0); 1733 } 1734 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1735 const GlobalValue *GV = G->getGlobal(); 1736 isDirect = true; 1737 bool isExt = GV->isDeclaration() || GV->isWeakForLinker(); 1738 bool isStub = (isExt && Subtarget->isTargetMachO()) && 1739 getTargetMachine().getRelocationModel() != Reloc::Static; 1740 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 1741 // ARM call to a local ARM function is predicable. 1742 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking); 1743 // tBX takes a register source operand. 1744 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1745 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?"); 1746 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(), 1747 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 1748 0, ARMII::MO_NONLAZY)); 1749 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee, 1750 MachinePointerInfo::getGOT(), false, false, true, 0); 1751 } else if (Subtarget->isTargetCOFF()) { 1752 assert(Subtarget->isTargetWindows() && 1753 "Windows is the only supported COFF target"); 1754 unsigned TargetFlags = GV->hasDLLImportStorageClass() 1755 ? ARMII::MO_DLLIMPORT 1756 : ARMII::MO_NO_FLAG; 1757 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0, 1758 TargetFlags); 1759 if (GV->hasDLLImportStorageClass()) 1760 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), 1761 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(), 1762 Callee), MachinePointerInfo::getGOT(), 1763 false, false, false, 0); 1764 } else { 1765 // On ELF targets for PIC code, direct calls should go through the PLT 1766 unsigned OpFlags = 0; 1767 if (Subtarget->isTargetELF() && 1768 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1769 OpFlags = ARMII::MO_PLT; 1770 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags); 1771 } 1772 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1773 isDirect = true; 1774 bool isStub = Subtarget->isTargetMachO() && 1775 getTargetMachine().getRelocationModel() != Reloc::Static; 1776 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 1777 // tBX takes a register source operand. 1778 const char *Sym = S->getSymbol(); 1779 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1780 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1781 ARMConstantPoolValue *CPV = 1782 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1783 ARMPCLabelIndex, 4); 1784 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4); 1785 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1786 Callee = DAG.getLoad(getPointerTy(), dl, 1787 DAG.getEntryNode(), CPAddr, 1788 MachinePointerInfo::getConstantPool(), 1789 false, false, false, 0); 1790 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 1791 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 1792 getPointerTy(), Callee, PICLabel); 1793 } else { 1794 unsigned OpFlags = 0; 1795 // On ELF targets for PIC code, direct calls should go through the PLT 1796 if (Subtarget->isTargetELF() && 1797 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1798 OpFlags = ARMII::MO_PLT; 1799 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags); 1800 } 1801 } 1802 1803 // FIXME: handle tail calls differently. 1804 unsigned CallOpc; 1805 bool HasMinSizeAttr = MF.getFunction()->hasFnAttribute(Attribute::MinSize); 1806 if (Subtarget->isThumb()) { 1807 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 1808 CallOpc = ARMISD::CALL_NOLINK; 1809 else 1810 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL; 1811 } else { 1812 if (!isDirect && !Subtarget->hasV5TOps()) 1813 CallOpc = ARMISD::CALL_NOLINK; 1814 else if (doesNotRet && isDirect && Subtarget->hasRAS() && 1815 // Emit regular call when code size is the priority 1816 !HasMinSizeAttr) 1817 // "mov lr, pc; b _foo" to avoid confusing the RSP 1818 CallOpc = ARMISD::CALL_NOLINK; 1819 else 1820 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL; 1821 } 1822 1823 std::vector<SDValue> Ops; 1824 Ops.push_back(Chain); 1825 Ops.push_back(Callee); 1826 1827 // Add argument registers to the end of the list so that they are known live 1828 // into the call. 1829 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1830 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1831 RegsToPass[i].second.getValueType())); 1832 1833 // Add a register mask operand representing the call-preserved registers. 1834 if (!isTailCall) { 1835 const uint32_t *Mask; 1836 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo(); 1837 if (isThisReturn) { 1838 // For 'this' returns, use the R0-preserving mask if applicable 1839 Mask = ARI->getThisReturnPreservedMask(MF, CallConv); 1840 if (!Mask) { 1841 // Set isThisReturn to false if the calling convention is not one that 1842 // allows 'returned' to be modeled in this way, so LowerCallResult does 1843 // not try to pass 'this' straight through 1844 isThisReturn = false; 1845 Mask = ARI->getCallPreservedMask(MF, CallConv); 1846 } 1847 } else 1848 Mask = ARI->getCallPreservedMask(MF, CallConv); 1849 1850 assert(Mask && "Missing call preserved mask for calling convention"); 1851 Ops.push_back(DAG.getRegisterMask(Mask)); 1852 } 1853 1854 if (InFlag.getNode()) 1855 Ops.push_back(InFlag); 1856 1857 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1858 if (isTailCall) { 1859 MF.getFrameInfo()->setHasTailCall(); 1860 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops); 1861 } 1862 1863 // Returns a chain and a flag for retval copy to use. 1864 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); 1865 InFlag = Chain.getValue(1); 1866 1867 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 1868 DAG.getIntPtrConstant(0, dl, true), InFlag, dl); 1869 if (!Ins.empty()) 1870 InFlag = Chain.getValue(1); 1871 1872 // Handle result values, copying them out of physregs into vregs that we 1873 // return. 1874 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG, 1875 InVals, isThisReturn, 1876 isThisReturn ? OutVals[0] : SDValue()); 1877 } 1878 1879 /// HandleByVal - Every parameter *after* a byval parameter is passed 1880 /// on the stack. Remember the next parameter register to allocate, 1881 /// and then confiscate the rest of the parameter registers to insure 1882 /// this. 1883 void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size, 1884 unsigned Align) const { 1885 assert((State->getCallOrPrologue() == Prologue || 1886 State->getCallOrPrologue() == Call) && 1887 "unhandled ParmContext"); 1888 1889 // Byval (as with any stack) slots are always at least 4 byte aligned. 1890 Align = std::max(Align, 4U); 1891 1892 unsigned Reg = State->AllocateReg(GPRArgRegs); 1893 if (!Reg) 1894 return; 1895 1896 unsigned AlignInRegs = Align / 4; 1897 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs; 1898 for (unsigned i = 0; i < Waste; ++i) 1899 Reg = State->AllocateReg(GPRArgRegs); 1900 1901 if (!Reg) 1902 return; 1903 1904 unsigned Excess = 4 * (ARM::R4 - Reg); 1905 1906 // Special case when NSAA != SP and parameter size greater than size of 1907 // all remained GPR regs. In that case we can't split parameter, we must 1908 // send it to stack. We also must set NCRN to R4, so waste all 1909 // remained registers. 1910 const unsigned NSAAOffset = State->getNextStackOffset(); 1911 if (NSAAOffset != 0 && Size > Excess) { 1912 while (State->AllocateReg(GPRArgRegs)) 1913 ; 1914 return; 1915 } 1916 1917 // First register for byval parameter is the first register that wasn't 1918 // allocated before this method call, so it would be "reg". 1919 // If parameter is small enough to be saved in range [reg, r4), then 1920 // the end (first after last) register would be reg + param-size-in-regs, 1921 // else parameter would be splitted between registers and stack, 1922 // end register would be r4 in this case. 1923 unsigned ByValRegBegin = Reg; 1924 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4); 1925 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd); 1926 // Note, first register is allocated in the beginning of function already, 1927 // allocate remained amount of registers we need. 1928 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i) 1929 State->AllocateReg(GPRArgRegs); 1930 // A byval parameter that is split between registers and memory needs its 1931 // size truncated here. 1932 // In the case where the entire structure fits in registers, we set the 1933 // size in memory to zero. 1934 Size = std::max<int>(Size - Excess, 0); 1935 } 1936 1937 /// MatchingStackOffset - Return true if the given stack call argument is 1938 /// already available in the same position (relatively) of the caller's 1939 /// incoming argument stack. 1940 static 1941 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 1942 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI, 1943 const TargetInstrInfo *TII) { 1944 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8; 1945 int FI = INT_MAX; 1946 if (Arg.getOpcode() == ISD::CopyFromReg) { 1947 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 1948 if (!TargetRegisterInfo::isVirtualRegister(VR)) 1949 return false; 1950 MachineInstr *Def = MRI->getVRegDef(VR); 1951 if (!Def) 1952 return false; 1953 if (!Flags.isByVal()) { 1954 if (!TII->isLoadFromStackSlot(Def, FI)) 1955 return false; 1956 } else { 1957 return false; 1958 } 1959 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 1960 if (Flags.isByVal()) 1961 // ByVal argument is passed in as a pointer but it's now being 1962 // dereferenced. e.g. 1963 // define @foo(%struct.X* %A) { 1964 // tail call @bar(%struct.X* byval %A) 1965 // } 1966 return false; 1967 SDValue Ptr = Ld->getBasePtr(); 1968 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 1969 if (!FINode) 1970 return false; 1971 FI = FINode->getIndex(); 1972 } else 1973 return false; 1974 1975 assert(FI != INT_MAX); 1976 if (!MFI->isFixedObjectIndex(FI)) 1977 return false; 1978 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI); 1979 } 1980 1981 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 1982 /// for tail call optimization. Targets which want to do tail call 1983 /// optimization should implement this function. 1984 bool 1985 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 1986 CallingConv::ID CalleeCC, 1987 bool isVarArg, 1988 bool isCalleeStructRet, 1989 bool isCallerStructRet, 1990 const SmallVectorImpl<ISD::OutputArg> &Outs, 1991 const SmallVectorImpl<SDValue> &OutVals, 1992 const SmallVectorImpl<ISD::InputArg> &Ins, 1993 SelectionDAG& DAG) const { 1994 const Function *CallerF = DAG.getMachineFunction().getFunction(); 1995 CallingConv::ID CallerCC = CallerF->getCallingConv(); 1996 bool CCMatch = CallerCC == CalleeCC; 1997 1998 // Look for obvious safe cases to perform tail call optimization that do not 1999 // require ABI changes. This is what gcc calls sibcall. 2000 2001 // Do not sibcall optimize vararg calls unless the call site is not passing 2002 // any arguments. 2003 if (isVarArg && !Outs.empty()) 2004 return false; 2005 2006 // Exception-handling functions need a special set of instructions to indicate 2007 // a return to the hardware. Tail-calling another function would probably 2008 // break this. 2009 if (CallerF->hasFnAttribute("interrupt")) 2010 return false; 2011 2012 // Also avoid sibcall optimization if either caller or callee uses struct 2013 // return semantics. 2014 if (isCalleeStructRet || isCallerStructRet) 2015 return false; 2016 2017 // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo:: 2018 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as 2019 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation 2020 // support in the assembler and linker to be used. This would need to be 2021 // fixed to fully support tail calls in Thumb1. 2022 // 2023 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take 2024 // LR. This means if we need to reload LR, it takes an extra instructions, 2025 // which outweighs the value of the tail call; but here we don't know yet 2026 // whether LR is going to be used. Probably the right approach is to 2027 // generate the tail call here and turn it back into CALL/RET in 2028 // emitEpilogue if LR is used. 2029 2030 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls, 2031 // but we need to make sure there are enough registers; the only valid 2032 // registers are the 4 used for parameters. We don't currently do this 2033 // case. 2034 if (Subtarget->isThumb1Only()) 2035 return false; 2036 2037 // Externally-defined functions with weak linkage should not be 2038 // tail-called on ARM when the OS does not support dynamic 2039 // pre-emption of symbols, as the AAELF spec requires normal calls 2040 // to undefined weak functions to be replaced with a NOP or jump to the 2041 // next instruction. The behaviour of branch instructions in this 2042 // situation (as used for tail calls) is implementation-defined, so we 2043 // cannot rely on the linker replacing the tail call with a return. 2044 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 2045 const GlobalValue *GV = G->getGlobal(); 2046 const Triple TT(getTargetMachine().getTargetTriple()); 2047 if (GV->hasExternalWeakLinkage() && 2048 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO())) 2049 return false; 2050 } 2051 2052 // If the calling conventions do not match, then we'd better make sure the 2053 // results are returned in the same way as what the caller expects. 2054 if (!CCMatch) { 2055 SmallVector<CCValAssign, 16> RVLocs1; 2056 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1, 2057 *DAG.getContext(), Call); 2058 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg)); 2059 2060 SmallVector<CCValAssign, 16> RVLocs2; 2061 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2, 2062 *DAG.getContext(), Call); 2063 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg)); 2064 2065 if (RVLocs1.size() != RVLocs2.size()) 2066 return false; 2067 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) { 2068 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc()) 2069 return false; 2070 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo()) 2071 return false; 2072 if (RVLocs1[i].isRegLoc()) { 2073 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg()) 2074 return false; 2075 } else { 2076 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset()) 2077 return false; 2078 } 2079 } 2080 } 2081 2082 // If Caller's vararg or byval argument has been split between registers and 2083 // stack, do not perform tail call, since part of the argument is in caller's 2084 // local frame. 2085 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction(). 2086 getInfo<ARMFunctionInfo>(); 2087 if (AFI_Caller->getArgRegsSaveSize()) 2088 return false; 2089 2090 // If the callee takes no arguments then go on to check the results of the 2091 // call. 2092 if (!Outs.empty()) { 2093 // Check if stack adjustment is needed. For now, do not do this if any 2094 // argument is passed on the stack. 2095 SmallVector<CCValAssign, 16> ArgLocs; 2096 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs, 2097 *DAG.getContext(), Call); 2098 CCInfo.AnalyzeCallOperands(Outs, 2099 CCAssignFnForNode(CalleeCC, false, isVarArg)); 2100 if (CCInfo.getNextStackOffset()) { 2101 MachineFunction &MF = DAG.getMachineFunction(); 2102 2103 // Check if the arguments are already laid out in the right way as 2104 // the caller's fixed stack objects. 2105 MachineFrameInfo *MFI = MF.getFrameInfo(); 2106 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 2107 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 2108 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 2109 i != e; 2110 ++i, ++realArgIdx) { 2111 CCValAssign &VA = ArgLocs[i]; 2112 EVT RegVT = VA.getLocVT(); 2113 SDValue Arg = OutVals[realArgIdx]; 2114 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2115 if (VA.getLocInfo() == CCValAssign::Indirect) 2116 return false; 2117 if (VA.needsCustom()) { 2118 // f64 and vector types are split into multiple registers or 2119 // register/stack-slot combinations. The types will not match 2120 // the registers; give up on memory f64 refs until we figure 2121 // out what to do about this. 2122 if (!VA.isRegLoc()) 2123 return false; 2124 if (!ArgLocs[++i].isRegLoc()) 2125 return false; 2126 if (RegVT == MVT::v2f64) { 2127 if (!ArgLocs[++i].isRegLoc()) 2128 return false; 2129 if (!ArgLocs[++i].isRegLoc()) 2130 return false; 2131 } 2132 } else if (!VA.isRegLoc()) { 2133 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 2134 MFI, MRI, TII)) 2135 return false; 2136 } 2137 } 2138 } 2139 } 2140 2141 return true; 2142 } 2143 2144 bool 2145 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 2146 MachineFunction &MF, bool isVarArg, 2147 const SmallVectorImpl<ISD::OutputArg> &Outs, 2148 LLVMContext &Context) const { 2149 SmallVector<CCValAssign, 16> RVLocs; 2150 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 2151 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true, 2152 isVarArg)); 2153 } 2154 2155 static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps, 2156 SDLoc DL, SelectionDAG &DAG) { 2157 const MachineFunction &MF = DAG.getMachineFunction(); 2158 const Function *F = MF.getFunction(); 2159 2160 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString(); 2161 2162 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset 2163 // version of the "preferred return address". These offsets affect the return 2164 // instruction if this is a return from PL1 without hypervisor extensions. 2165 // IRQ/FIQ: +4 "subs pc, lr, #4" 2166 // SWI: 0 "subs pc, lr, #0" 2167 // ABORT: +4 "subs pc, lr, #4" 2168 // UNDEF: +4/+2 "subs pc, lr, #0" 2169 // UNDEF varies depending on where the exception came from ARM or Thumb 2170 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0. 2171 2172 int64_t LROffset; 2173 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" || 2174 IntKind == "ABORT") 2175 LROffset = 4; 2176 else if (IntKind == "SWI" || IntKind == "UNDEF") 2177 LROffset = 0; 2178 else 2179 report_fatal_error("Unsupported interrupt attribute. If present, value " 2180 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF"); 2181 2182 RetOps.insert(RetOps.begin() + 1, 2183 DAG.getConstant(LROffset, DL, MVT::i32, false)); 2184 2185 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps); 2186 } 2187 2188 SDValue 2189 ARMTargetLowering::LowerReturn(SDValue Chain, 2190 CallingConv::ID CallConv, bool isVarArg, 2191 const SmallVectorImpl<ISD::OutputArg> &Outs, 2192 const SmallVectorImpl<SDValue> &OutVals, 2193 SDLoc dl, SelectionDAG &DAG) const { 2194 2195 // CCValAssign - represent the assignment of the return value to a location. 2196 SmallVector<CCValAssign, 16> RVLocs; 2197 2198 // CCState - Info about the registers and stack slots. 2199 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2200 *DAG.getContext(), Call); 2201 2202 // Analyze outgoing return values. 2203 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true, 2204 isVarArg)); 2205 2206 SDValue Flag; 2207 SmallVector<SDValue, 4> RetOps; 2208 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2209 bool isLittleEndian = Subtarget->isLittle(); 2210 2211 MachineFunction &MF = DAG.getMachineFunction(); 2212 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2213 AFI->setReturnRegsCount(RVLocs.size()); 2214 2215 // Copy the result values into the output registers. 2216 for (unsigned i = 0, realRVLocIdx = 0; 2217 i != RVLocs.size(); 2218 ++i, ++realRVLocIdx) { 2219 CCValAssign &VA = RVLocs[i]; 2220 assert(VA.isRegLoc() && "Can only return in registers!"); 2221 2222 SDValue Arg = OutVals[realRVLocIdx]; 2223 2224 switch (VA.getLocInfo()) { 2225 default: llvm_unreachable("Unknown loc info!"); 2226 case CCValAssign::Full: break; 2227 case CCValAssign::BCvt: 2228 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 2229 break; 2230 } 2231 2232 if (VA.needsCustom()) { 2233 if (VA.getLocVT() == MVT::v2f64) { 2234 // Extract the first half and return it in two registers. 2235 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2236 DAG.getConstant(0, dl, MVT::i32)); 2237 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 2238 DAG.getVTList(MVT::i32, MVT::i32), Half); 2239 2240 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2241 HalfGPRs.getValue(isLittleEndian ? 0 : 1), 2242 Flag); 2243 Flag = Chain.getValue(1); 2244 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2245 VA = RVLocs[++i]; // skip ahead to next loc 2246 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2247 HalfGPRs.getValue(isLittleEndian ? 1 : 0), 2248 Flag); 2249 Flag = Chain.getValue(1); 2250 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2251 VA = RVLocs[++i]; // skip ahead to next loc 2252 2253 // Extract the 2nd half and fall through to handle it as an f64 value. 2254 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2255 DAG.getConstant(1, dl, MVT::i32)); 2256 } 2257 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 2258 // available. 2259 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 2260 DAG.getVTList(MVT::i32, MVT::i32), Arg); 2261 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2262 fmrrd.getValue(isLittleEndian ? 0 : 1), 2263 Flag); 2264 Flag = Chain.getValue(1); 2265 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2266 VA = RVLocs[++i]; // skip ahead to next loc 2267 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2268 fmrrd.getValue(isLittleEndian ? 1 : 0), 2269 Flag); 2270 } else 2271 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 2272 2273 // Guarantee that all emitted copies are 2274 // stuck together, avoiding something bad. 2275 Flag = Chain.getValue(1); 2276 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2277 } 2278 2279 // Update chain and glue. 2280 RetOps[0] = Chain; 2281 if (Flag.getNode()) 2282 RetOps.push_back(Flag); 2283 2284 // CPUs which aren't M-class use a special sequence to return from 2285 // exceptions (roughly, any instruction setting pc and cpsr simultaneously, 2286 // though we use "subs pc, lr, #N"). 2287 // 2288 // M-class CPUs actually use a normal return sequence with a special 2289 // (hardware-provided) value in LR, so the normal code path works. 2290 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") && 2291 !Subtarget->isMClass()) { 2292 if (Subtarget->isThumb1Only()) 2293 report_fatal_error("interrupt attribute is not supported in Thumb1"); 2294 return LowerInterruptReturn(RetOps, dl, DAG); 2295 } 2296 2297 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps); 2298 } 2299 2300 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { 2301 if (N->getNumValues() != 1) 2302 return false; 2303 if (!N->hasNUsesOfValue(1, 0)) 2304 return false; 2305 2306 SDValue TCChain = Chain; 2307 SDNode *Copy = *N->use_begin(); 2308 if (Copy->getOpcode() == ISD::CopyToReg) { 2309 // If the copy has a glue operand, we conservatively assume it isn't safe to 2310 // perform a tail call. 2311 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2312 return false; 2313 TCChain = Copy->getOperand(0); 2314 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) { 2315 SDNode *VMov = Copy; 2316 // f64 returned in a pair of GPRs. 2317 SmallPtrSet<SDNode*, 2> Copies; 2318 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2319 UI != UE; ++UI) { 2320 if (UI->getOpcode() != ISD::CopyToReg) 2321 return false; 2322 Copies.insert(*UI); 2323 } 2324 if (Copies.size() > 2) 2325 return false; 2326 2327 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2328 UI != UE; ++UI) { 2329 SDValue UseChain = UI->getOperand(0); 2330 if (Copies.count(UseChain.getNode())) 2331 // Second CopyToReg 2332 Copy = *UI; 2333 else { 2334 // We are at the top of this chain. 2335 // If the copy has a glue operand, we conservatively assume it 2336 // isn't safe to perform a tail call. 2337 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue) 2338 return false; 2339 // First CopyToReg 2340 TCChain = UseChain; 2341 } 2342 } 2343 } else if (Copy->getOpcode() == ISD::BITCAST) { 2344 // f32 returned in a single GPR. 2345 if (!Copy->hasOneUse()) 2346 return false; 2347 Copy = *Copy->use_begin(); 2348 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0)) 2349 return false; 2350 // If the copy has a glue operand, we conservatively assume it isn't safe to 2351 // perform a tail call. 2352 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2353 return false; 2354 TCChain = Copy->getOperand(0); 2355 } else { 2356 return false; 2357 } 2358 2359 bool HasRet = false; 2360 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 2361 UI != UE; ++UI) { 2362 if (UI->getOpcode() != ARMISD::RET_FLAG && 2363 UI->getOpcode() != ARMISD::INTRET_FLAG) 2364 return false; 2365 HasRet = true; 2366 } 2367 2368 if (!HasRet) 2369 return false; 2370 2371 Chain = TCChain; 2372 return true; 2373 } 2374 2375 bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 2376 if (!Subtarget->supportsTailCall()) 2377 return false; 2378 2379 auto Attr = 2380 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls"); 2381 if (!CI->isTailCall() || Attr.getValueAsString() == "true") 2382 return false; 2383 2384 return !Subtarget->isThumb1Only(); 2385 } 2386 2387 // Trying to write a 64 bit value so need to split into two 32 bit values first, 2388 // and pass the lower and high parts through. 2389 static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) { 2390 SDLoc DL(Op); 2391 SDValue WriteValue = Op->getOperand(2); 2392 2393 // This function is only supposed to be called for i64 type argument. 2394 assert(WriteValue.getValueType() == MVT::i64 2395 && "LowerWRITE_REGISTER called for non-i64 type argument."); 2396 2397 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 2398 DAG.getConstant(0, DL, MVT::i32)); 2399 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 2400 DAG.getConstant(1, DL, MVT::i32)); 2401 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi }; 2402 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops); 2403 } 2404 2405 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 2406 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 2407 // one of the above mentioned nodes. It has to be wrapped because otherwise 2408 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 2409 // be used to form addressing mode. These wrapped nodes will be selected 2410 // into MOVi. 2411 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) { 2412 EVT PtrVT = Op.getValueType(); 2413 // FIXME there is no actual debug info here 2414 SDLoc dl(Op); 2415 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2416 SDValue Res; 2417 if (CP->isMachineConstantPoolEntry()) 2418 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 2419 CP->getAlignment()); 2420 else 2421 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 2422 CP->getAlignment()); 2423 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 2424 } 2425 2426 unsigned ARMTargetLowering::getJumpTableEncoding() const { 2427 return MachineJumpTableInfo::EK_Inline; 2428 } 2429 2430 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 2431 SelectionDAG &DAG) const { 2432 MachineFunction &MF = DAG.getMachineFunction(); 2433 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2434 unsigned ARMPCLabelIndex = 0; 2435 SDLoc DL(Op); 2436 EVT PtrVT = getPointerTy(); 2437 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 2438 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2439 SDValue CPAddr; 2440 if (RelocM == Reloc::Static) { 2441 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 2442 } else { 2443 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2444 ARMPCLabelIndex = AFI->createPICLabelUId(); 2445 ARMConstantPoolValue *CPV = 2446 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 2447 ARMCP::CPBlockAddress, PCAdj); 2448 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2449 } 2450 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 2451 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr, 2452 MachinePointerInfo::getConstantPool(), 2453 false, false, false, 0); 2454 if (RelocM == Reloc::Static) 2455 return Result; 2456 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32); 2457 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 2458 } 2459 2460 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 2461 SDValue 2462 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 2463 SelectionDAG &DAG) const { 2464 SDLoc dl(GA); 2465 EVT PtrVT = getPointerTy(); 2466 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2467 MachineFunction &MF = DAG.getMachineFunction(); 2468 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2469 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2470 ARMConstantPoolValue *CPV = 2471 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2472 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 2473 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2474 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 2475 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, 2476 MachinePointerInfo::getConstantPool(), 2477 false, false, false, 0); 2478 SDValue Chain = Argument.getValue(1); 2479 2480 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2481 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 2482 2483 // call __tls_get_addr. 2484 ArgListTy Args; 2485 ArgListEntry Entry; 2486 Entry.Node = Argument; 2487 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 2488 Args.push_back(Entry); 2489 2490 // FIXME: is there useful debug info available here? 2491 TargetLowering::CallLoweringInfo CLI(DAG); 2492 CLI.setDebugLoc(dl).setChain(Chain) 2493 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()), 2494 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args), 2495 0); 2496 2497 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2498 return CallResult.first; 2499 } 2500 2501 // Lower ISD::GlobalTLSAddress using the "initial exec" or 2502 // "local exec" model. 2503 SDValue 2504 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 2505 SelectionDAG &DAG, 2506 TLSModel::Model model) const { 2507 const GlobalValue *GV = GA->getGlobal(); 2508 SDLoc dl(GA); 2509 SDValue Offset; 2510 SDValue Chain = DAG.getEntryNode(); 2511 EVT PtrVT = getPointerTy(); 2512 // Get the Thread Pointer 2513 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2514 2515 if (model == TLSModel::InitialExec) { 2516 MachineFunction &MF = DAG.getMachineFunction(); 2517 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2518 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2519 // Initial exec model. 2520 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2521 ARMConstantPoolValue *CPV = 2522 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2523 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 2524 true); 2525 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2526 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2527 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2528 MachinePointerInfo::getConstantPool(), 2529 false, false, false, 0); 2530 Chain = Offset.getValue(1); 2531 2532 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2533 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 2534 2535 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2536 MachinePointerInfo::getConstantPool(), 2537 false, false, false, 0); 2538 } else { 2539 // local exec model 2540 assert(model == TLSModel::LocalExec); 2541 ARMConstantPoolValue *CPV = 2542 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 2543 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2544 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2545 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, 2546 MachinePointerInfo::getConstantPool(), 2547 false, false, false, 0); 2548 } 2549 2550 // The address of the thread local variable is the add of the thread 2551 // pointer with the offset of the variable. 2552 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 2553 } 2554 2555 SDValue 2556 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 2557 // TODO: implement the "local dynamic" model 2558 assert(Subtarget->isTargetELF() && 2559 "TLS not implemented for non-ELF targets"); 2560 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2561 2562 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal()); 2563 2564 switch (model) { 2565 case TLSModel::GeneralDynamic: 2566 case TLSModel::LocalDynamic: 2567 return LowerToTLSGeneralDynamicModel(GA, DAG); 2568 case TLSModel::InitialExec: 2569 case TLSModel::LocalExec: 2570 return LowerToTLSExecModels(GA, DAG, model); 2571 } 2572 llvm_unreachable("bogus TLS model"); 2573 } 2574 2575 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 2576 SelectionDAG &DAG) const { 2577 EVT PtrVT = getPointerTy(); 2578 SDLoc dl(Op); 2579 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2580 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 2581 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility(); 2582 ARMConstantPoolValue *CPV = 2583 ARMConstantPoolConstant::Create(GV, 2584 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT); 2585 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2586 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2587 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 2588 CPAddr, 2589 MachinePointerInfo::getConstantPool(), 2590 false, false, false, 0); 2591 SDValue Chain = Result.getValue(1); 2592 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT); 2593 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT); 2594 if (!UseGOTOFF) 2595 Result = DAG.getLoad(PtrVT, dl, Chain, Result, 2596 MachinePointerInfo::getGOT(), 2597 false, false, false, 0); 2598 return Result; 2599 } 2600 2601 // If we have T2 ops, we can materialize the address directly via movt/movw 2602 // pair. This is always cheaper. 2603 if (Subtarget->useMovt(DAG.getMachineFunction())) { 2604 ++NumMovwMovt; 2605 // FIXME: Once remat is capable of dealing with instructions with register 2606 // operands, expand this into two nodes. 2607 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2608 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2609 } else { 2610 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2611 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2612 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2613 MachinePointerInfo::getConstantPool(), 2614 false, false, false, 0); 2615 } 2616 } 2617 2618 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 2619 SelectionDAG &DAG) const { 2620 EVT PtrVT = getPointerTy(); 2621 SDLoc dl(Op); 2622 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2623 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2624 2625 if (Subtarget->useMovt(DAG.getMachineFunction())) 2626 ++NumMovwMovt; 2627 2628 // FIXME: Once remat is capable of dealing with instructions with register 2629 // operands, expand this into multiple nodes 2630 unsigned Wrapper = 2631 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper; 2632 2633 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY); 2634 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G); 2635 2636 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2637 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 2638 MachinePointerInfo::getGOT(), false, false, false, 0); 2639 return Result; 2640 } 2641 2642 SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op, 2643 SelectionDAG &DAG) const { 2644 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported"); 2645 assert(Subtarget->useMovt(DAG.getMachineFunction()) && 2646 "Windows on ARM expects to use movw/movt"); 2647 2648 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2649 const ARMII::TOF TargetFlags = 2650 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG); 2651 EVT PtrVT = getPointerTy(); 2652 SDValue Result; 2653 SDLoc DL(Op); 2654 2655 ++NumMovwMovt; 2656 2657 // FIXME: Once remat is capable of dealing with instructions with register 2658 // operands, expand this into two nodes. 2659 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, 2660 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0, 2661 TargetFlags)); 2662 if (GV->hasDLLImportStorageClass()) 2663 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, 2664 MachinePointerInfo::getGOT(), false, false, false, 0); 2665 return Result; 2666 } 2667 2668 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, 2669 SelectionDAG &DAG) const { 2670 assert(Subtarget->isTargetELF() && 2671 "GLOBAL OFFSET TABLE not implemented for non-ELF targets"); 2672 MachineFunction &MF = DAG.getMachineFunction(); 2673 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2674 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2675 EVT PtrVT = getPointerTy(); 2676 SDLoc dl(Op); 2677 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2678 ARMConstantPoolValue *CPV = 2679 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_", 2680 ARMPCLabelIndex, PCAdj); 2681 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2682 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2683 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2684 MachinePointerInfo::getConstantPool(), 2685 false, false, false, 0); 2686 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2687 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2688 } 2689 2690 SDValue 2691 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 2692 SDLoc dl(Op); 2693 SDValue Val = DAG.getConstant(0, dl, MVT::i32); 2694 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 2695 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 2696 Op.getOperand(1), Val); 2697 } 2698 2699 SDValue 2700 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 2701 SDLoc dl(Op); 2702 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 2703 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32)); 2704 } 2705 2706 SDValue 2707 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 2708 const ARMSubtarget *Subtarget) const { 2709 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2710 SDLoc dl(Op); 2711 switch (IntNo) { 2712 default: return SDValue(); // Don't custom lower most intrinsics. 2713 case Intrinsic::arm_rbit: { 2714 assert(Op.getOperand(1).getValueType() == MVT::i32 && 2715 "RBIT intrinsic must have i32 type!"); 2716 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1)); 2717 } 2718 case Intrinsic::arm_thread_pointer: { 2719 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2720 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2721 } 2722 case Intrinsic::eh_sjlj_lsda: { 2723 MachineFunction &MF = DAG.getMachineFunction(); 2724 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2725 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2726 EVT PtrVT = getPointerTy(); 2727 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2728 SDValue CPAddr; 2729 unsigned PCAdj = (RelocM != Reloc::PIC_) 2730 ? 0 : (Subtarget->isThumb() ? 4 : 8); 2731 ARMConstantPoolValue *CPV = 2732 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex, 2733 ARMCP::CPLSDA, PCAdj); 2734 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2735 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2736 SDValue Result = 2737 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, 2738 MachinePointerInfo::getConstantPool(), 2739 false, false, false, 0); 2740 2741 if (RelocM == Reloc::PIC_) { 2742 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2743 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2744 } 2745 return Result; 2746 } 2747 case Intrinsic::arm_neon_vmulls: 2748 case Intrinsic::arm_neon_vmullu: { 2749 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 2750 ? ARMISD::VMULLs : ARMISD::VMULLu; 2751 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 2752 Op.getOperand(1), Op.getOperand(2)); 2753 } 2754 } 2755 } 2756 2757 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 2758 const ARMSubtarget *Subtarget) { 2759 // FIXME: handle "fence singlethread" more efficiently. 2760 SDLoc dl(Op); 2761 if (!Subtarget->hasDataBarrier()) { 2762 // Some ARMv6 cpus can support data barriers with an mcr instruction. 2763 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 2764 // here. 2765 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 2766 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!"); 2767 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 2768 DAG.getConstant(0, dl, MVT::i32)); 2769 } 2770 2771 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1)); 2772 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue()); 2773 ARM_MB::MemBOpt Domain = ARM_MB::ISH; 2774 if (Subtarget->isMClass()) { 2775 // Only a full system barrier exists in the M-class architectures. 2776 Domain = ARM_MB::SY; 2777 } else if (Subtarget->isSwift() && Ord == Release) { 2778 // Swift happens to implement ISHST barriers in a way that's compatible with 2779 // Release semantics but weaker than ISH so we'd be fools not to use 2780 // it. Beware: other processors probably don't! 2781 Domain = ARM_MB::ISHST; 2782 } 2783 2784 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0), 2785 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32), 2786 DAG.getConstant(Domain, dl, MVT::i32)); 2787 } 2788 2789 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 2790 const ARMSubtarget *Subtarget) { 2791 // ARM pre v5TE and Thumb1 does not have preload instructions. 2792 if (!(Subtarget->isThumb2() || 2793 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 2794 // Just preserve the chain. 2795 return Op.getOperand(0); 2796 2797 SDLoc dl(Op); 2798 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 2799 if (!isRead && 2800 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 2801 // ARMv7 with MP extension has PLDW. 2802 return Op.getOperand(0); 2803 2804 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 2805 if (Subtarget->isThumb()) { 2806 // Invert the bits. 2807 isRead = ~isRead & 1; 2808 isData = ~isData & 1; 2809 } 2810 2811 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 2812 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32), 2813 DAG.getConstant(isData, dl, MVT::i32)); 2814 } 2815 2816 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 2817 MachineFunction &MF = DAG.getMachineFunction(); 2818 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 2819 2820 // vastart just stores the address of the VarArgsFrameIndex slot into the 2821 // memory location argument. 2822 SDLoc dl(Op); 2823 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); 2824 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 2825 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 2826 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 2827 MachinePointerInfo(SV), false, false, 0); 2828 } 2829 2830 SDValue 2831 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA, 2832 SDValue &Root, SelectionDAG &DAG, 2833 SDLoc dl) const { 2834 MachineFunction &MF = DAG.getMachineFunction(); 2835 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2836 2837 const TargetRegisterClass *RC; 2838 if (AFI->isThumb1OnlyFunction()) 2839 RC = &ARM::tGPRRegClass; 2840 else 2841 RC = &ARM::GPRRegClass; 2842 2843 // Transform the arguments stored in physical registers into virtual ones. 2844 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 2845 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2846 2847 SDValue ArgValue2; 2848 if (NextVA.isMemLoc()) { 2849 MachineFrameInfo *MFI = MF.getFrameInfo(); 2850 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true); 2851 2852 // Create load node to retrieve arguments from the stack. 2853 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 2854 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, 2855 MachinePointerInfo::getFixedStack(FI), 2856 false, false, false, 0); 2857 } else { 2858 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 2859 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 2860 } 2861 if (!Subtarget->isLittle()) 2862 std::swap (ArgValue, ArgValue2); 2863 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 2864 } 2865 2866 // The remaining GPRs hold either the beginning of variable-argument 2867 // data, or the beginning of an aggregate passed by value (usually 2868 // byval). Either way, we allocate stack slots adjacent to the data 2869 // provided by our caller, and store the unallocated registers there. 2870 // If this is a variadic function, the va_list pointer will begin with 2871 // these values; otherwise, this reassembles a (byval) structure that 2872 // was split between registers and memory. 2873 // Return: The frame index registers were stored into. 2874 int 2875 ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG, 2876 SDLoc dl, SDValue &Chain, 2877 const Value *OrigArg, 2878 unsigned InRegsParamRecordIdx, 2879 int ArgOffset, 2880 unsigned ArgSize) const { 2881 // Currently, two use-cases possible: 2882 // Case #1. Non-var-args function, and we meet first byval parameter. 2883 // Setup first unallocated register as first byval register; 2884 // eat all remained registers 2885 // (these two actions are performed by HandleByVal method). 2886 // Then, here, we initialize stack frame with 2887 // "store-reg" instructions. 2888 // Case #2. Var-args function, that doesn't contain byval parameters. 2889 // The same: eat all remained unallocated registers, 2890 // initialize stack frame. 2891 2892 MachineFunction &MF = DAG.getMachineFunction(); 2893 MachineFrameInfo *MFI = MF.getFrameInfo(); 2894 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2895 unsigned RBegin, REnd; 2896 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 2897 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 2898 } else { 2899 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 2900 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx]; 2901 REnd = ARM::R4; 2902 } 2903 2904 if (REnd != RBegin) 2905 ArgOffset = -4 * (ARM::R4 - RBegin); 2906 2907 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false); 2908 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy()); 2909 2910 SmallVector<SDValue, 4> MemOps; 2911 const TargetRegisterClass *RC = 2912 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 2913 2914 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) { 2915 unsigned VReg = MF.addLiveIn(Reg, RC); 2916 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 2917 SDValue Store = 2918 DAG.getStore(Val.getValue(1), dl, Val, FIN, 2919 MachinePointerInfo(OrigArg, 4 * i), false, false, 0); 2920 MemOps.push_back(Store); 2921 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN, 2922 DAG.getConstant(4, dl, getPointerTy())); 2923 } 2924 2925 if (!MemOps.empty()) 2926 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 2927 return FrameIndex; 2928 } 2929 2930 // Setup stack frame, the va_list pointer will start from. 2931 void 2932 ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 2933 SDLoc dl, SDValue &Chain, 2934 unsigned ArgOffset, 2935 unsigned TotalArgRegsSaveSize, 2936 bool ForceMutable) const { 2937 MachineFunction &MF = DAG.getMachineFunction(); 2938 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2939 2940 // Try to store any remaining integer argument regs 2941 // to their spots on the stack so that they may be loaded by deferencing 2942 // the result of va_next. 2943 // If there is no regs to be stored, just point address after last 2944 // argument passed via stack. 2945 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr, 2946 CCInfo.getInRegsParamsCount(), 2947 CCInfo.getNextStackOffset(), 4); 2948 AFI->setVarArgsFrameIndex(FrameIndex); 2949 } 2950 2951 SDValue 2952 ARMTargetLowering::LowerFormalArguments(SDValue Chain, 2953 CallingConv::ID CallConv, bool isVarArg, 2954 const SmallVectorImpl<ISD::InputArg> 2955 &Ins, 2956 SDLoc dl, SelectionDAG &DAG, 2957 SmallVectorImpl<SDValue> &InVals) 2958 const { 2959 MachineFunction &MF = DAG.getMachineFunction(); 2960 MachineFrameInfo *MFI = MF.getFrameInfo(); 2961 2962 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2963 2964 // Assign locations to all of the incoming arguments. 2965 SmallVector<CCValAssign, 16> ArgLocs; 2966 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 2967 *DAG.getContext(), Prologue); 2968 CCInfo.AnalyzeFormalArguments(Ins, 2969 CCAssignFnForNode(CallConv, /* Return*/ false, 2970 isVarArg)); 2971 2972 SmallVector<SDValue, 16> ArgValues; 2973 SDValue ArgValue; 2974 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin(); 2975 unsigned CurArgIdx = 0; 2976 2977 // Initially ArgRegsSaveSize is zero. 2978 // Then we increase this value each time we meet byval parameter. 2979 // We also increase this value in case of varargs function. 2980 AFI->setArgRegsSaveSize(0); 2981 2982 // Calculate the amount of stack space that we need to allocate to store 2983 // byval and variadic arguments that are passed in registers. 2984 // We need to know this before we allocate the first byval or variadic 2985 // argument, as they will be allocated a stack slot below the CFA (Canonical 2986 // Frame Address, the stack pointer at entry to the function). 2987 unsigned ArgRegBegin = ARM::R4; 2988 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 2989 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount()) 2990 break; 2991 2992 CCValAssign &VA = ArgLocs[i]; 2993 unsigned Index = VA.getValNo(); 2994 ISD::ArgFlagsTy Flags = Ins[Index].Flags; 2995 if (!Flags.isByVal()) 2996 continue; 2997 2998 assert(VA.isMemLoc() && "unexpected byval pointer in reg"); 2999 unsigned RBegin, REnd; 3000 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd); 3001 ArgRegBegin = std::min(ArgRegBegin, RBegin); 3002 3003 CCInfo.nextInRegsParam(); 3004 } 3005 CCInfo.rewindByValRegsInfo(); 3006 3007 int lastInsIndex = -1; 3008 if (isVarArg && MFI->hasVAStart()) { 3009 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 3010 if (RegIdx != array_lengthof(GPRArgRegs)) 3011 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]); 3012 } 3013 3014 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin); 3015 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize); 3016 3017 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3018 CCValAssign &VA = ArgLocs[i]; 3019 if (Ins[VA.getValNo()].isOrigArg()) { 3020 std::advance(CurOrigArg, 3021 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx); 3022 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex(); 3023 } 3024 // Arguments stored in registers. 3025 if (VA.isRegLoc()) { 3026 EVT RegVT = VA.getLocVT(); 3027 3028 if (VA.needsCustom()) { 3029 // f64 and vector types are split up into multiple registers or 3030 // combinations of registers and stack slots. 3031 if (VA.getLocVT() == MVT::v2f64) { 3032 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 3033 Chain, DAG, dl); 3034 VA = ArgLocs[++i]; // skip ahead to next loc 3035 SDValue ArgValue2; 3036 if (VA.isMemLoc()) { 3037 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true); 3038 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 3039 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN, 3040 MachinePointerInfo::getFixedStack(FI), 3041 false, false, false, 0); 3042 } else { 3043 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 3044 Chain, DAG, dl); 3045 } 3046 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 3047 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3048 ArgValue, ArgValue1, 3049 DAG.getIntPtrConstant(0, dl)); 3050 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3051 ArgValue, ArgValue2, 3052 DAG.getIntPtrConstant(1, dl)); 3053 } else 3054 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 3055 3056 } else { 3057 const TargetRegisterClass *RC; 3058 3059 if (RegVT == MVT::f32) 3060 RC = &ARM::SPRRegClass; 3061 else if (RegVT == MVT::f64) 3062 RC = &ARM::DPRRegClass; 3063 else if (RegVT == MVT::v2f64) 3064 RC = &ARM::QPRRegClass; 3065 else if (RegVT == MVT::i32) 3066 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass 3067 : &ARM::GPRRegClass; 3068 else 3069 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 3070 3071 // Transform the arguments in physical registers into virtual ones. 3072 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3073 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 3074 } 3075 3076 // If this is an 8 or 16-bit value, it is really passed promoted 3077 // to 32 bits. Insert an assert[sz]ext to capture this, then 3078 // truncate to the right size. 3079 switch (VA.getLocInfo()) { 3080 default: llvm_unreachable("Unknown loc info!"); 3081 case CCValAssign::Full: break; 3082 case CCValAssign::BCvt: 3083 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 3084 break; 3085 case CCValAssign::SExt: 3086 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 3087 DAG.getValueType(VA.getValVT())); 3088 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3089 break; 3090 case CCValAssign::ZExt: 3091 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 3092 DAG.getValueType(VA.getValVT())); 3093 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3094 break; 3095 } 3096 3097 InVals.push_back(ArgValue); 3098 3099 } else { // VA.isRegLoc() 3100 3101 // sanity check 3102 assert(VA.isMemLoc()); 3103 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 3104 3105 int index = VA.getValNo(); 3106 3107 // Some Ins[] entries become multiple ArgLoc[] entries. 3108 // Process them only once. 3109 if (index != lastInsIndex) 3110 { 3111 ISD::ArgFlagsTy Flags = Ins[index].Flags; 3112 // FIXME: For now, all byval parameter objects are marked mutable. 3113 // This can be changed with more analysis. 3114 // In case of tail call optimization mark all arguments mutable. 3115 // Since they could be overwritten by lowering of arguments in case of 3116 // a tail call. 3117 if (Flags.isByVal()) { 3118 assert(Ins[index].isOrigArg() && 3119 "Byval arguments cannot be implicit"); 3120 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed(); 3121 3122 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, CurOrigArg, 3123 CurByValIndex, VA.getLocMemOffset(), 3124 Flags.getByValSize()); 3125 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy())); 3126 CCInfo.nextInRegsParam(); 3127 } else { 3128 unsigned FIOffset = VA.getLocMemOffset(); 3129 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 3130 FIOffset, true); 3131 3132 // Create load nodes to retrieve arguments from the stack. 3133 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 3134 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, 3135 MachinePointerInfo::getFixedStack(FI), 3136 false, false, false, 0)); 3137 } 3138 lastInsIndex = index; 3139 } 3140 } 3141 } 3142 3143 // varargs 3144 if (isVarArg && MFI->hasVAStart()) 3145 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 3146 CCInfo.getNextStackOffset(), 3147 TotalArgRegsSaveSize); 3148 3149 AFI->setArgumentStackSize(CCInfo.getNextStackOffset()); 3150 3151 return Chain; 3152 } 3153 3154 /// isFloatingPointZero - Return true if this is +0.0. 3155 static bool isFloatingPointZero(SDValue Op) { 3156 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 3157 return CFP->getValueAPF().isPosZero(); 3158 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 3159 // Maybe this has already been legalized into the constant pool? 3160 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 3161 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 3162 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 3163 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 3164 return CFP->getValueAPF().isPosZero(); 3165 } 3166 } else if (Op->getOpcode() == ISD::BITCAST && 3167 Op->getValueType(0) == MVT::f64) { 3168 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64) 3169 // created by LowerConstantFP(). 3170 SDValue BitcastOp = Op->getOperand(0); 3171 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) { 3172 SDValue MoveOp = BitcastOp->getOperand(0); 3173 if (MoveOp->getOpcode() == ISD::TargetConstant && 3174 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) { 3175 return true; 3176 } 3177 } 3178 } 3179 return false; 3180 } 3181 3182 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 3183 /// the given operands. 3184 SDValue 3185 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 3186 SDValue &ARMcc, SelectionDAG &DAG, 3187 SDLoc dl) const { 3188 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 3189 unsigned C = RHSC->getZExtValue(); 3190 if (!isLegalICmpImmediate(C)) { 3191 // Constant does not fit, try adjusting it by one? 3192 switch (CC) { 3193 default: break; 3194 case ISD::SETLT: 3195 case ISD::SETGE: 3196 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 3197 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 3198 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 3199 } 3200 break; 3201 case ISD::SETULT: 3202 case ISD::SETUGE: 3203 if (C != 0 && isLegalICmpImmediate(C-1)) { 3204 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 3205 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 3206 } 3207 break; 3208 case ISD::SETLE: 3209 case ISD::SETGT: 3210 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 3211 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 3212 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 3213 } 3214 break; 3215 case ISD::SETULE: 3216 case ISD::SETUGT: 3217 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 3218 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 3219 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 3220 } 3221 break; 3222 } 3223 } 3224 } 3225 3226 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3227 ARMISD::NodeType CompareType; 3228 switch (CondCode) { 3229 default: 3230 CompareType = ARMISD::CMP; 3231 break; 3232 case ARMCC::EQ: 3233 case ARMCC::NE: 3234 // Uses only Z Flag 3235 CompareType = ARMISD::CMPZ; 3236 break; 3237 } 3238 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3239 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 3240 } 3241 3242 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 3243 SDValue 3244 ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG, 3245 SDLoc dl) const { 3246 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64); 3247 SDValue Cmp; 3248 if (!isFloatingPointZero(RHS)) 3249 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS); 3250 else 3251 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS); 3252 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 3253 } 3254 3255 /// duplicateCmp - Glue values can have only one use, so this function 3256 /// duplicates a comparison node. 3257 SDValue 3258 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 3259 unsigned Opc = Cmp.getOpcode(); 3260 SDLoc DL(Cmp); 3261 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 3262 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3263 3264 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 3265 Cmp = Cmp.getOperand(0); 3266 Opc = Cmp.getOpcode(); 3267 if (Opc == ARMISD::CMPFP) 3268 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3269 else { 3270 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 3271 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0)); 3272 } 3273 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 3274 } 3275 3276 std::pair<SDValue, SDValue> 3277 ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG, 3278 SDValue &ARMcc) const { 3279 assert(Op.getValueType() == MVT::i32 && "Unsupported value type"); 3280 3281 SDValue Value, OverflowCmp; 3282 SDValue LHS = Op.getOperand(0); 3283 SDValue RHS = Op.getOperand(1); 3284 SDLoc dl(Op); 3285 3286 // FIXME: We are currently always generating CMPs because we don't support 3287 // generating CMN through the backend. This is not as good as the natural 3288 // CMP case because it causes a register dependency and cannot be folded 3289 // later. 3290 3291 switch (Op.getOpcode()) { 3292 default: 3293 llvm_unreachable("Unknown overflow instruction!"); 3294 case ISD::SADDO: 3295 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 3296 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS); 3297 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 3298 break; 3299 case ISD::UADDO: 3300 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 3301 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS); 3302 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 3303 break; 3304 case ISD::SSUBO: 3305 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 3306 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 3307 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 3308 break; 3309 case ISD::USUBO: 3310 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 3311 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 3312 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 3313 break; 3314 } // switch (...) 3315 3316 return std::make_pair(Value, OverflowCmp); 3317 } 3318 3319 3320 SDValue 3321 ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const { 3322 // Let legalize expand this if it isn't a legal type yet. 3323 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 3324 return SDValue(); 3325 3326 SDValue Value, OverflowCmp; 3327 SDValue ARMcc; 3328 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc); 3329 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3330 SDLoc dl(Op); 3331 // We use 0 and 1 as false and true values. 3332 SDValue TVal = DAG.getConstant(1, dl, MVT::i32); 3333 SDValue FVal = DAG.getConstant(0, dl, MVT::i32); 3334 EVT VT = Op.getValueType(); 3335 3336 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal, 3337 ARMcc, CCR, OverflowCmp); 3338 3339 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); 3340 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 3341 } 3342 3343 3344 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 3345 SDValue Cond = Op.getOperand(0); 3346 SDValue SelectTrue = Op.getOperand(1); 3347 SDValue SelectFalse = Op.getOperand(2); 3348 SDLoc dl(Op); 3349 unsigned Opc = Cond.getOpcode(); 3350 3351 if (Cond.getResNo() == 1 && 3352 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 3353 Opc == ISD::USUBO)) { 3354 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 3355 return SDValue(); 3356 3357 SDValue Value, OverflowCmp; 3358 SDValue ARMcc; 3359 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 3360 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3361 EVT VT = Op.getValueType(); 3362 3363 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR, 3364 OverflowCmp, DAG); 3365 } 3366 3367 // Convert: 3368 // 3369 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 3370 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 3371 // 3372 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 3373 const ConstantSDNode *CMOVTrue = 3374 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 3375 const ConstantSDNode *CMOVFalse = 3376 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 3377 3378 if (CMOVTrue && CMOVFalse) { 3379 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 3380 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 3381 3382 SDValue True; 3383 SDValue False; 3384 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 3385 True = SelectTrue; 3386 False = SelectFalse; 3387 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 3388 True = SelectFalse; 3389 False = SelectTrue; 3390 } 3391 3392 if (True.getNode() && False.getNode()) { 3393 EVT VT = Op.getValueType(); 3394 SDValue ARMcc = Cond.getOperand(2); 3395 SDValue CCR = Cond.getOperand(3); 3396 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 3397 assert(True.getValueType() == VT); 3398 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG); 3399 } 3400 } 3401 } 3402 3403 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the 3404 // undefined bits before doing a full-word comparison with zero. 3405 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond, 3406 DAG.getConstant(1, dl, Cond.getValueType())); 3407 3408 return DAG.getSelectCC(dl, Cond, 3409 DAG.getConstant(0, dl, Cond.getValueType()), 3410 SelectTrue, SelectFalse, ISD::SETNE); 3411 } 3412 3413 static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 3414 bool &swpCmpOps, bool &swpVselOps) { 3415 // Start by selecting the GE condition code for opcodes that return true for 3416 // 'equality' 3417 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE || 3418 CC == ISD::SETULE) 3419 CondCode = ARMCC::GE; 3420 3421 // and GT for opcodes that return false for 'equality'. 3422 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT || 3423 CC == ISD::SETULT) 3424 CondCode = ARMCC::GT; 3425 3426 // Since we are constrained to GE/GT, if the opcode contains 'less', we need 3427 // to swap the compare operands. 3428 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT || 3429 CC == ISD::SETULT) 3430 swpCmpOps = true; 3431 3432 // Both GT and GE are ordered comparisons, and return false for 'unordered'. 3433 // If we have an unordered opcode, we need to swap the operands to the VSEL 3434 // instruction (effectively negating the condition). 3435 // 3436 // This also has the effect of swapping which one of 'less' or 'greater' 3437 // returns true, so we also swap the compare operands. It also switches 3438 // whether we return true for 'equality', so we compensate by picking the 3439 // opposite condition code to our original choice. 3440 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE || 3441 CC == ISD::SETUGT) { 3442 swpCmpOps = !swpCmpOps; 3443 swpVselOps = !swpVselOps; 3444 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT; 3445 } 3446 3447 // 'ordered' is 'anything but unordered', so use the VS condition code and 3448 // swap the VSEL operands. 3449 if (CC == ISD::SETO) { 3450 CondCode = ARMCC::VS; 3451 swpVselOps = true; 3452 } 3453 3454 // 'unordered or not equal' is 'anything but equal', so use the EQ condition 3455 // code and swap the VSEL operands. 3456 if (CC == ISD::SETUNE) { 3457 CondCode = ARMCC::EQ; 3458 swpVselOps = true; 3459 } 3460 } 3461 3462 SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal, 3463 SDValue TrueVal, SDValue ARMcc, SDValue CCR, 3464 SDValue Cmp, SelectionDAG &DAG) const { 3465 if (Subtarget->isFPOnlySP() && VT == MVT::f64) { 3466 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl, 3467 DAG.getVTList(MVT::i32, MVT::i32), FalseVal); 3468 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl, 3469 DAG.getVTList(MVT::i32, MVT::i32), TrueVal); 3470 3471 SDValue TrueLow = TrueVal.getValue(0); 3472 SDValue TrueHigh = TrueVal.getValue(1); 3473 SDValue FalseLow = FalseVal.getValue(0); 3474 SDValue FalseHigh = FalseVal.getValue(1); 3475 3476 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow, 3477 ARMcc, CCR, Cmp); 3478 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh, 3479 ARMcc, CCR, duplicateCmp(Cmp, DAG)); 3480 3481 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High); 3482 } else { 3483 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, 3484 Cmp); 3485 } 3486 } 3487 3488 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 3489 EVT VT = Op.getValueType(); 3490 SDValue LHS = Op.getOperand(0); 3491 SDValue RHS = Op.getOperand(1); 3492 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 3493 SDValue TrueVal = Op.getOperand(2); 3494 SDValue FalseVal = Op.getOperand(3); 3495 SDLoc dl(Op); 3496 3497 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) { 3498 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 3499 dl); 3500 3501 // If softenSetCCOperands only returned one value, we should compare it to 3502 // zero. 3503 if (!RHS.getNode()) { 3504 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 3505 CC = ISD::SETNE; 3506 } 3507 } 3508 3509 if (LHS.getValueType() == MVT::i32) { 3510 // Try to generate VSEL on ARMv8. 3511 // The VSEL instruction can't use all the usual ARM condition 3512 // codes: it only has two bits to select the condition code, so it's 3513 // constrained to use only GE, GT, VS and EQ. 3514 // 3515 // To implement all the various ISD::SETXXX opcodes, we sometimes need to 3516 // swap the operands of the previous compare instruction (effectively 3517 // inverting the compare condition, swapping 'less' and 'greater') and 3518 // sometimes need to swap the operands to the VSEL (which inverts the 3519 // condition in the sense of firing whenever the previous condition didn't) 3520 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 || 3521 TrueVal.getValueType() == MVT::f64)) { 3522 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3523 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE || 3524 CondCode == ARMCC::VC || CondCode == ARMCC::NE) { 3525 CC = ISD::getSetCCInverse(CC, true); 3526 std::swap(TrueVal, FalseVal); 3527 } 3528 } 3529 3530 SDValue ARMcc; 3531 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3532 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3533 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 3534 } 3535 3536 ARMCC::CondCodes CondCode, CondCode2; 3537 FPCCToARMCC(CC, CondCode, CondCode2); 3538 3539 // Try to generate VMAXNM/VMINNM on ARMv8. 3540 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 || 3541 TrueVal.getValueType() == MVT::f64)) { 3542 // We can use VMAXNM/VMINNM for a compare followed by a select with the 3543 // same operands, as follows: 3544 // c = fcmp [?gt, ?ge, ?lt, ?le] a, b 3545 // select c, a, b 3546 // In NoNaNsFPMath the CC will have been changed from, e.g., 'ogt' to 'gt'. 3547 bool swapSides = false; 3548 if (!getTargetMachine().Options.NoNaNsFPMath) { 3549 // transformability may depend on which way around we compare 3550 switch (CC) { 3551 default: 3552 break; 3553 case ISD::SETOGT: 3554 case ISD::SETOGE: 3555 case ISD::SETOLT: 3556 case ISD::SETOLE: 3557 // the non-NaN should be RHS 3558 swapSides = DAG.isKnownNeverNaN(LHS) && !DAG.isKnownNeverNaN(RHS); 3559 break; 3560 case ISD::SETUGT: 3561 case ISD::SETUGE: 3562 case ISD::SETULT: 3563 case ISD::SETULE: 3564 // the non-NaN should be LHS 3565 swapSides = DAG.isKnownNeverNaN(RHS) && !DAG.isKnownNeverNaN(LHS); 3566 break; 3567 } 3568 } 3569 swapSides = swapSides || (LHS == FalseVal && RHS == TrueVal); 3570 if (swapSides) { 3571 CC = ISD::getSetCCSwappedOperands(CC); 3572 std::swap(LHS, RHS); 3573 } 3574 if (LHS == TrueVal && RHS == FalseVal) { 3575 bool canTransform = true; 3576 // FIXME: FastMathFlags::noSignedZeros() doesn't appear reachable from here 3577 if (!getTargetMachine().Options.UnsafeFPMath && 3578 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) { 3579 const ConstantFPSDNode *Zero; 3580 switch (CC) { 3581 default: 3582 break; 3583 case ISD::SETOGT: 3584 case ISD::SETUGT: 3585 case ISD::SETGT: 3586 // RHS must not be -0 3587 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) && 3588 !Zero->isNegative(); 3589 break; 3590 case ISD::SETOGE: 3591 case ISD::SETUGE: 3592 case ISD::SETGE: 3593 // LHS must not be -0 3594 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) && 3595 !Zero->isNegative(); 3596 break; 3597 case ISD::SETOLT: 3598 case ISD::SETULT: 3599 case ISD::SETLT: 3600 // RHS must not be +0 3601 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) && 3602 Zero->isNegative(); 3603 break; 3604 case ISD::SETOLE: 3605 case ISD::SETULE: 3606 case ISD::SETLE: 3607 // LHS must not be +0 3608 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) && 3609 Zero->isNegative(); 3610 break; 3611 } 3612 } 3613 if (canTransform) { 3614 // Note: If one of the elements in a pair is a number and the other 3615 // element is NaN, the corresponding result element is the number. 3616 // This is consistent with the IEEE 754-2008 standard. 3617 // Therefore, a > b ? a : b <=> vmax(a,b), if b is constant and a is NaN 3618 switch (CC) { 3619 default: 3620 break; 3621 case ISD::SETOGT: 3622 case ISD::SETOGE: 3623 if (!DAG.isKnownNeverNaN(RHS)) 3624 break; 3625 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS); 3626 case ISD::SETUGT: 3627 case ISD::SETUGE: 3628 if (!DAG.isKnownNeverNaN(LHS)) 3629 break; 3630 case ISD::SETGT: 3631 case ISD::SETGE: 3632 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS); 3633 case ISD::SETOLT: 3634 case ISD::SETOLE: 3635 if (!DAG.isKnownNeverNaN(RHS)) 3636 break; 3637 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS); 3638 case ISD::SETULT: 3639 case ISD::SETULE: 3640 if (!DAG.isKnownNeverNaN(LHS)) 3641 break; 3642 case ISD::SETLT: 3643 case ISD::SETLE: 3644 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS); 3645 } 3646 } 3647 } 3648 3649 bool swpCmpOps = false; 3650 bool swpVselOps = false; 3651 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps); 3652 3653 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE || 3654 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) { 3655 if (swpCmpOps) 3656 std::swap(LHS, RHS); 3657 if (swpVselOps) 3658 std::swap(TrueVal, FalseVal); 3659 } 3660 } 3661 3662 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3663 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3664 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3665 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 3666 if (CondCode2 != ARMCC::AL) { 3667 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32); 3668 // FIXME: Needs another CMP because flag can have but one use. 3669 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl); 3670 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG); 3671 } 3672 return Result; 3673 } 3674 3675 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 3676 /// to morph to an integer compare sequence. 3677 static bool canChangeToInt(SDValue Op, bool &SeenZero, 3678 const ARMSubtarget *Subtarget) { 3679 SDNode *N = Op.getNode(); 3680 if (!N->hasOneUse()) 3681 // Otherwise it requires moving the value from fp to integer registers. 3682 return false; 3683 if (!N->getNumValues()) 3684 return false; 3685 EVT VT = Op.getValueType(); 3686 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 3687 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 3688 // vmrs are very slow, e.g. cortex-a8. 3689 return false; 3690 3691 if (isFloatingPointZero(Op)) { 3692 SeenZero = true; 3693 return true; 3694 } 3695 return ISD::isNormalLoad(N); 3696 } 3697 3698 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 3699 if (isFloatingPointZero(Op)) 3700 return DAG.getConstant(0, SDLoc(Op), MVT::i32); 3701 3702 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 3703 return DAG.getLoad(MVT::i32, SDLoc(Op), 3704 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(), 3705 Ld->isVolatile(), Ld->isNonTemporal(), 3706 Ld->isInvariant(), Ld->getAlignment()); 3707 3708 llvm_unreachable("Unknown VFP cmp argument!"); 3709 } 3710 3711 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 3712 SDValue &RetVal1, SDValue &RetVal2) { 3713 SDLoc dl(Op); 3714 3715 if (isFloatingPointZero(Op)) { 3716 RetVal1 = DAG.getConstant(0, dl, MVT::i32); 3717 RetVal2 = DAG.getConstant(0, dl, MVT::i32); 3718 return; 3719 } 3720 3721 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 3722 SDValue Ptr = Ld->getBasePtr(); 3723 RetVal1 = DAG.getLoad(MVT::i32, dl, 3724 Ld->getChain(), Ptr, 3725 Ld->getPointerInfo(), 3726 Ld->isVolatile(), Ld->isNonTemporal(), 3727 Ld->isInvariant(), Ld->getAlignment()); 3728 3729 EVT PtrType = Ptr.getValueType(); 3730 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 3731 SDValue NewPtr = DAG.getNode(ISD::ADD, dl, 3732 PtrType, Ptr, DAG.getConstant(4, dl, PtrType)); 3733 RetVal2 = DAG.getLoad(MVT::i32, dl, 3734 Ld->getChain(), NewPtr, 3735 Ld->getPointerInfo().getWithOffset(4), 3736 Ld->isVolatile(), Ld->isNonTemporal(), 3737 Ld->isInvariant(), NewAlign); 3738 return; 3739 } 3740 3741 llvm_unreachable("Unknown VFP cmp argument!"); 3742 } 3743 3744 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 3745 /// f32 and even f64 comparisons to integer ones. 3746 SDValue 3747 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 3748 SDValue Chain = Op.getOperand(0); 3749 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3750 SDValue LHS = Op.getOperand(2); 3751 SDValue RHS = Op.getOperand(3); 3752 SDValue Dest = Op.getOperand(4); 3753 SDLoc dl(Op); 3754 3755 bool LHSSeenZero = false; 3756 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget); 3757 bool RHSSeenZero = false; 3758 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget); 3759 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) { 3760 // If unsafe fp math optimization is enabled and there are no other uses of 3761 // the CMP operands, and the condition code is EQ or NE, we can optimize it 3762 // to an integer comparison. 3763 if (CC == ISD::SETOEQ) 3764 CC = ISD::SETEQ; 3765 else if (CC == ISD::SETUNE) 3766 CC = ISD::SETNE; 3767 3768 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32); 3769 SDValue ARMcc; 3770 if (LHS.getValueType() == MVT::f32) { 3771 LHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3772 bitcastf32Toi32(LHS, DAG), Mask); 3773 RHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3774 bitcastf32Toi32(RHS, DAG), Mask); 3775 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3776 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3777 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3778 Chain, Dest, ARMcc, CCR, Cmp); 3779 } 3780 3781 SDValue LHS1, LHS2; 3782 SDValue RHS1, RHS2; 3783 expandf64Toi32(LHS, DAG, LHS1, LHS2); 3784 expandf64Toi32(RHS, DAG, RHS1, RHS2); 3785 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask); 3786 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask); 3787 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3788 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3789 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3790 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 3791 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops); 3792 } 3793 3794 return SDValue(); 3795 } 3796 3797 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 3798 SDValue Chain = Op.getOperand(0); 3799 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3800 SDValue LHS = Op.getOperand(2); 3801 SDValue RHS = Op.getOperand(3); 3802 SDValue Dest = Op.getOperand(4); 3803 SDLoc dl(Op); 3804 3805 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) { 3806 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 3807 dl); 3808 3809 // If softenSetCCOperands only returned one value, we should compare it to 3810 // zero. 3811 if (!RHS.getNode()) { 3812 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 3813 CC = ISD::SETNE; 3814 } 3815 } 3816 3817 if (LHS.getValueType() == MVT::i32) { 3818 SDValue ARMcc; 3819 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3820 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3821 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3822 Chain, Dest, ARMcc, CCR, Cmp); 3823 } 3824 3825 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 3826 3827 if (getTargetMachine().Options.UnsafeFPMath && 3828 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 3829 CC == ISD::SETNE || CC == ISD::SETUNE)) { 3830 SDValue Result = OptimizeVFPBrcond(Op, DAG); 3831 if (Result.getNode()) 3832 return Result; 3833 } 3834 3835 ARMCC::CondCodes CondCode, CondCode2; 3836 FPCCToARMCC(CC, CondCode, CondCode2); 3837 3838 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3839 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3840 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3841 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3842 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 3843 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 3844 if (CondCode2 != ARMCC::AL) { 3845 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32); 3846 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 3847 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 3848 } 3849 return Res; 3850 } 3851 3852 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 3853 SDValue Chain = Op.getOperand(0); 3854 SDValue Table = Op.getOperand(1); 3855 SDValue Index = Op.getOperand(2); 3856 SDLoc dl(Op); 3857 3858 EVT PTy = getPointerTy(); 3859 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 3860 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 3861 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI); 3862 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy)); 3863 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); 3864 if (Subtarget->isThumb2()) { 3865 // Thumb2 uses a two-level jump. That is, it jumps into the jump table 3866 // which does another jump to the destination. This also makes it easier 3867 // to translate it to TBB / TBH later. 3868 // FIXME: This might not work if the function is extremely large. 3869 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 3870 Addr, Op.getOperand(2), JTI); 3871 } 3872 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 3873 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 3874 MachinePointerInfo::getJumpTable(), 3875 false, false, false, 0); 3876 Chain = Addr.getValue(1); 3877 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table); 3878 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 3879 } else { 3880 Addr = DAG.getLoad(PTy, dl, Chain, Addr, 3881 MachinePointerInfo::getJumpTable(), 3882 false, false, false, 0); 3883 Chain = Addr.getValue(1); 3884 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 3885 } 3886 } 3887 3888 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 3889 EVT VT = Op.getValueType(); 3890 SDLoc dl(Op); 3891 3892 if (Op.getValueType().getVectorElementType() == MVT::i32) { 3893 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 3894 return Op; 3895 return DAG.UnrollVectorOp(Op.getNode()); 3896 } 3897 3898 assert(Op.getOperand(0).getValueType() == MVT::v4f32 && 3899 "Invalid type for custom lowering!"); 3900 if (VT != MVT::v4i16) 3901 return DAG.UnrollVectorOp(Op.getNode()); 3902 3903 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0)); 3904 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op); 3905 } 3906 3907 SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const { 3908 EVT VT = Op.getValueType(); 3909 if (VT.isVector()) 3910 return LowerVectorFP_TO_INT(Op, DAG); 3911 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) { 3912 RTLIB::Libcall LC; 3913 if (Op.getOpcode() == ISD::FP_TO_SINT) 3914 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), 3915 Op.getValueType()); 3916 else 3917 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), 3918 Op.getValueType()); 3919 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1, 3920 /*isSigned*/ false, SDLoc(Op)).first; 3921 } 3922 3923 return Op; 3924 } 3925 3926 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 3927 EVT VT = Op.getValueType(); 3928 SDLoc dl(Op); 3929 3930 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 3931 if (VT.getVectorElementType() == MVT::f32) 3932 return Op; 3933 return DAG.UnrollVectorOp(Op.getNode()); 3934 } 3935 3936 assert(Op.getOperand(0).getValueType() == MVT::v4i16 && 3937 "Invalid type for custom lowering!"); 3938 if (VT != MVT::v4f32) 3939 return DAG.UnrollVectorOp(Op.getNode()); 3940 3941 unsigned CastOpc; 3942 unsigned Opc; 3943 switch (Op.getOpcode()) { 3944 default: llvm_unreachable("Invalid opcode!"); 3945 case ISD::SINT_TO_FP: 3946 CastOpc = ISD::SIGN_EXTEND; 3947 Opc = ISD::SINT_TO_FP; 3948 break; 3949 case ISD::UINT_TO_FP: 3950 CastOpc = ISD::ZERO_EXTEND; 3951 Opc = ISD::UINT_TO_FP; 3952 break; 3953 } 3954 3955 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0)); 3956 return DAG.getNode(Opc, dl, VT, Op); 3957 } 3958 3959 SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const { 3960 EVT VT = Op.getValueType(); 3961 if (VT.isVector()) 3962 return LowerVectorINT_TO_FP(Op, DAG); 3963 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) { 3964 RTLIB::Libcall LC; 3965 if (Op.getOpcode() == ISD::SINT_TO_FP) 3966 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), 3967 Op.getValueType()); 3968 else 3969 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), 3970 Op.getValueType()); 3971 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1, 3972 /*isSigned*/ false, SDLoc(Op)).first; 3973 } 3974 3975 return Op; 3976 } 3977 3978 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 3979 // Implement fcopysign with a fabs and a conditional fneg. 3980 SDValue Tmp0 = Op.getOperand(0); 3981 SDValue Tmp1 = Op.getOperand(1); 3982 SDLoc dl(Op); 3983 EVT VT = Op.getValueType(); 3984 EVT SrcVT = Tmp1.getValueType(); 3985 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 3986 Tmp0.getOpcode() == ARMISD::VMOVDRR; 3987 bool UseNEON = !InGPR && Subtarget->hasNEON(); 3988 3989 if (UseNEON) { 3990 // Use VBSL to copy the sign bit. 3991 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80); 3992 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 3993 DAG.getTargetConstant(EncodedVal, dl, MVT::i32)); 3994 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 3995 if (VT == MVT::f64) 3996 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT, 3997 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 3998 DAG.getConstant(32, dl, MVT::i32)); 3999 else /*if (VT == MVT::f32)*/ 4000 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 4001 if (SrcVT == MVT::f32) { 4002 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 4003 if (VT == MVT::f64) 4004 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4005 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 4006 DAG.getConstant(32, dl, MVT::i32)); 4007 } else if (VT == MVT::f32) 4008 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64, 4009 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 4010 DAG.getConstant(32, dl, MVT::i32)); 4011 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 4012 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 4013 4014 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff), 4015 dl, MVT::i32); 4016 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 4017 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 4018 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 4019 4020 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 4021 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 4022 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 4023 if (VT == MVT::f32) { 4024 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 4025 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 4026 DAG.getConstant(0, dl, MVT::i32)); 4027 } else { 4028 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 4029 } 4030 4031 return Res; 4032 } 4033 4034 // Bitcast operand 1 to i32. 4035 if (SrcVT == MVT::f64) 4036 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 4037 Tmp1).getValue(1); 4038 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 4039 4040 // Or in the signbit with integer operations. 4041 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32); 4042 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32); 4043 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 4044 if (VT == MVT::f32) { 4045 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 4046 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 4047 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 4048 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 4049 } 4050 4051 // f64: Or the high part with signbit and then combine two parts. 4052 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 4053 Tmp0); 4054 SDValue Lo = Tmp0.getValue(0); 4055 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 4056 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 4057 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 4058 } 4059 4060 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 4061 MachineFunction &MF = DAG.getMachineFunction(); 4062 MachineFrameInfo *MFI = MF.getFrameInfo(); 4063 MFI->setReturnAddressIsTaken(true); 4064 4065 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 4066 return SDValue(); 4067 4068 EVT VT = Op.getValueType(); 4069 SDLoc dl(Op); 4070 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4071 if (Depth) { 4072 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 4073 SDValue Offset = DAG.getConstant(4, dl, MVT::i32); 4074 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 4075 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 4076 MachinePointerInfo(), false, false, false, 0); 4077 } 4078 4079 // Return LR, which contains the return address. Mark it an implicit live-in. 4080 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 4081 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 4082 } 4083 4084 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 4085 const ARMBaseRegisterInfo &ARI = 4086 *static_cast<const ARMBaseRegisterInfo*>(RegInfo); 4087 MachineFunction &MF = DAG.getMachineFunction(); 4088 MachineFrameInfo *MFI = MF.getFrameInfo(); 4089 MFI->setFrameAddressIsTaken(true); 4090 4091 EVT VT = Op.getValueType(); 4092 SDLoc dl(Op); // FIXME probably not meaningful 4093 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4094 unsigned FrameReg = ARI.getFrameRegister(MF); 4095 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 4096 while (Depth--) 4097 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 4098 MachinePointerInfo(), 4099 false, false, false, 0); 4100 return FrameAddr; 4101 } 4102 4103 // FIXME? Maybe this could be a TableGen attribute on some registers and 4104 // this table could be generated automatically from RegInfo. 4105 unsigned ARMTargetLowering::getRegisterByName(const char* RegName, 4106 EVT VT) const { 4107 unsigned Reg = StringSwitch<unsigned>(RegName) 4108 .Case("sp", ARM::SP) 4109 .Default(0); 4110 if (Reg) 4111 return Reg; 4112 report_fatal_error(Twine("Invalid register name \"" 4113 + StringRef(RegName) + "\".")); 4114 } 4115 4116 // Result is 64 bit value so split into two 32 bit values and return as a 4117 // pair of values. 4118 static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results, 4119 SelectionDAG &DAG) { 4120 SDLoc DL(N); 4121 4122 // This function is only supposed to be called for i64 type destination. 4123 assert(N->getValueType(0) == MVT::i64 4124 && "ExpandREAD_REGISTER called for non-i64 type result."); 4125 4126 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL, 4127 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other), 4128 N->getOperand(0), 4129 N->getOperand(1)); 4130 4131 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0), 4132 Read.getValue(1))); 4133 Results.push_back(Read.getOperand(0)); 4134 } 4135 4136 /// ExpandBITCAST - If the target supports VFP, this function is called to 4137 /// expand a bit convert where either the source or destination type is i64 to 4138 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 4139 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 4140 /// vectors), since the legalizer won't know what to do with that. 4141 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) { 4142 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4143 SDLoc dl(N); 4144 SDValue Op = N->getOperand(0); 4145 4146 // This function is only supposed to be called for i64 types, either as the 4147 // source or destination of the bit convert. 4148 EVT SrcVT = Op.getValueType(); 4149 EVT DstVT = N->getValueType(0); 4150 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) && 4151 "ExpandBITCAST called for non-i64 type"); 4152 4153 // Turn i64->f64 into VMOVDRR. 4154 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 4155 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 4156 DAG.getConstant(0, dl, MVT::i32)); 4157 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 4158 DAG.getConstant(1, dl, MVT::i32)); 4159 return DAG.getNode(ISD::BITCAST, dl, DstVT, 4160 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 4161 } 4162 4163 // Turn f64->i64 into VMOVRRD. 4164 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 4165 SDValue Cvt; 4166 if (TLI.isBigEndian() && SrcVT.isVector() && 4167 SrcVT.getVectorNumElements() > 1) 4168 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 4169 DAG.getVTList(MVT::i32, MVT::i32), 4170 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op)); 4171 else 4172 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 4173 DAG.getVTList(MVT::i32, MVT::i32), Op); 4174 // Merge the pieces into a single i64 value. 4175 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 4176 } 4177 4178 return SDValue(); 4179 } 4180 4181 /// getZeroVector - Returns a vector of specified type with all zero elements. 4182 /// Zero vectors are used to represent vector negation and in those cases 4183 /// will be implemented with the NEON VNEG instruction. However, VNEG does 4184 /// not support i64 elements, so sometimes the zero vectors will need to be 4185 /// explicitly constructed. Regardless, use a canonical VMOV to create the 4186 /// zero vector. 4187 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) { 4188 assert(VT.isVector() && "Expected a vector type"); 4189 // The canonical modified immediate encoding of a zero vector is....0! 4190 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32); 4191 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 4192 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 4193 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 4194 } 4195 4196 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 4197 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 4198 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 4199 SelectionDAG &DAG) const { 4200 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4201 EVT VT = Op.getValueType(); 4202 unsigned VTBits = VT.getSizeInBits(); 4203 SDLoc dl(Op); 4204 SDValue ShOpLo = Op.getOperand(0); 4205 SDValue ShOpHi = Op.getOperand(1); 4206 SDValue ShAmt = Op.getOperand(2); 4207 SDValue ARMcc; 4208 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 4209 4210 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 4211 4212 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 4213 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 4214 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 4215 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 4216 DAG.getConstant(VTBits, dl, MVT::i32)); 4217 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 4218 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 4219 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 4220 4221 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4222 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 4223 ISD::SETGE, ARMcc, DAG, dl); 4224 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 4225 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, 4226 CCR, Cmp); 4227 4228 SDValue Ops[2] = { Lo, Hi }; 4229 return DAG.getMergeValues(Ops, dl); 4230 } 4231 4232 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 4233 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 4234 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 4235 SelectionDAG &DAG) const { 4236 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4237 EVT VT = Op.getValueType(); 4238 unsigned VTBits = VT.getSizeInBits(); 4239 SDLoc dl(Op); 4240 SDValue ShOpLo = Op.getOperand(0); 4241 SDValue ShOpHi = Op.getOperand(1); 4242 SDValue ShAmt = Op.getOperand(2); 4243 SDValue ARMcc; 4244 4245 assert(Op.getOpcode() == ISD::SHL_PARTS); 4246 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 4247 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 4248 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 4249 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 4250 DAG.getConstant(VTBits, dl, MVT::i32)); 4251 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 4252 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 4253 4254 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 4255 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4256 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 4257 ISD::SETGE, ARMcc, DAG, dl); 4258 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 4259 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc, 4260 CCR, Cmp); 4261 4262 SDValue Ops[2] = { Lo, Hi }; 4263 return DAG.getMergeValues(Ops, dl); 4264 } 4265 4266 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 4267 SelectionDAG &DAG) const { 4268 // The rounding mode is in bits 23:22 of the FPSCR. 4269 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 4270 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 4271 // so that the shift + and get folded into a bitfield extract. 4272 SDLoc dl(Op); 4273 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, 4274 DAG.getConstant(Intrinsic::arm_get_fpscr, dl, 4275 MVT::i32)); 4276 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 4277 DAG.getConstant(1U << 22, dl, MVT::i32)); 4278 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 4279 DAG.getConstant(22, dl, MVT::i32)); 4280 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 4281 DAG.getConstant(3, dl, MVT::i32)); 4282 } 4283 4284 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 4285 const ARMSubtarget *ST) { 4286 EVT VT = N->getValueType(0); 4287 SDLoc dl(N); 4288 4289 if (!ST->hasV6T2Ops()) 4290 return SDValue(); 4291 4292 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0)); 4293 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 4294 } 4295 4296 /// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count 4297 /// for each 16-bit element from operand, repeated. The basic idea is to 4298 /// leverage vcnt to get the 8-bit counts, gather and add the results. 4299 /// 4300 /// Trace for v4i16: 4301 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 4302 /// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element) 4303 /// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi) 4304 /// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6] 4305 /// [b0 b1 b2 b3 b4 b5 b6 b7] 4306 /// +[b1 b0 b3 b2 b5 b4 b7 b6] 4307 /// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0, 4308 /// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits) 4309 static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) { 4310 EVT VT = N->getValueType(0); 4311 SDLoc DL(N); 4312 4313 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 4314 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0)); 4315 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0); 4316 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1); 4317 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2); 4318 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3); 4319 } 4320 4321 /// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the 4322 /// bit-count for each 16-bit element from the operand. We need slightly 4323 /// different sequencing for v4i16 and v8i16 to stay within NEON's available 4324 /// 64/128-bit registers. 4325 /// 4326 /// Trace for v4i16: 4327 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 4328 /// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi) 4329 /// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ] 4330 /// v4i16:Extracted = [k0 k1 k2 k3 ] 4331 static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) { 4332 EVT VT = N->getValueType(0); 4333 SDLoc DL(N); 4334 4335 SDValue BitCounts = getCTPOP16BitCounts(N, DAG); 4336 if (VT.is64BitVector()) { 4337 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts); 4338 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended, 4339 DAG.getIntPtrConstant(0, DL)); 4340 } else { 4341 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8, 4342 BitCounts, DAG.getIntPtrConstant(0, DL)); 4343 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted); 4344 } 4345 } 4346 4347 /// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the 4348 /// bit-count for each 32-bit element from the operand. The idea here is 4349 /// to split the vector into 16-bit elements, leverage the 16-bit count 4350 /// routine, and then combine the results. 4351 /// 4352 /// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged): 4353 /// input = [v0 v1 ] (vi: 32-bit elements) 4354 /// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1]) 4355 /// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi) 4356 /// vrev: N0 = [k1 k0 k3 k2 ] 4357 /// [k0 k1 k2 k3 ] 4358 /// N1 =+[k1 k0 k3 k2 ] 4359 /// [k0 k2 k1 k3 ] 4360 /// N2 =+[k1 k3 k0 k2 ] 4361 /// [k0 k2 k1 k3 ] 4362 /// Extended =+[k1 k3 k0 k2 ] 4363 /// [k0 k2 ] 4364 /// Extracted=+[k1 k3 ] 4365 /// 4366 static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) { 4367 EVT VT = N->getValueType(0); 4368 SDLoc DL(N); 4369 4370 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16; 4371 4372 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0)); 4373 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG); 4374 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16); 4375 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0); 4376 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1); 4377 4378 if (VT.is64BitVector()) { 4379 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2); 4380 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended, 4381 DAG.getIntPtrConstant(0, DL)); 4382 } else { 4383 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2, 4384 DAG.getIntPtrConstant(0, DL)); 4385 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted); 4386 } 4387 } 4388 4389 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG, 4390 const ARMSubtarget *ST) { 4391 EVT VT = N->getValueType(0); 4392 4393 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON."); 4394 assert((VT == MVT::v2i32 || VT == MVT::v4i32 || 4395 VT == MVT::v4i16 || VT == MVT::v8i16) && 4396 "Unexpected type for custom ctpop lowering"); 4397 4398 if (VT.getVectorElementType() == MVT::i32) 4399 return lowerCTPOP32BitElements(N, DAG); 4400 else 4401 return lowerCTPOP16BitElements(N, DAG); 4402 } 4403 4404 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 4405 const ARMSubtarget *ST) { 4406 EVT VT = N->getValueType(0); 4407 SDLoc dl(N); 4408 4409 if (!VT.isVector()) 4410 return SDValue(); 4411 4412 // Lower vector shifts on NEON to use VSHL. 4413 assert(ST->hasNEON() && "unexpected vector shift"); 4414 4415 // Left shifts translate directly to the vshiftu intrinsic. 4416 if (N->getOpcode() == ISD::SHL) 4417 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4418 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl, 4419 MVT::i32), 4420 N->getOperand(0), N->getOperand(1)); 4421 4422 assert((N->getOpcode() == ISD::SRA || 4423 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode"); 4424 4425 // NEON uses the same intrinsics for both left and right shifts. For 4426 // right shifts, the shift amounts are negative, so negate the vector of 4427 // shift amounts. 4428 EVT ShiftVT = N->getOperand(1).getValueType(); 4429 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT, 4430 getZeroVector(ShiftVT, DAG, dl), 4431 N->getOperand(1)); 4432 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ? 4433 Intrinsic::arm_neon_vshifts : 4434 Intrinsic::arm_neon_vshiftu); 4435 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4436 DAG.getConstant(vshiftInt, dl, MVT::i32), 4437 N->getOperand(0), NegatedCount); 4438 } 4439 4440 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 4441 const ARMSubtarget *ST) { 4442 EVT VT = N->getValueType(0); 4443 SDLoc dl(N); 4444 4445 // We can get here for a node like i32 = ISD::SHL i32, i64 4446 if (VT != MVT::i64) 4447 return SDValue(); 4448 4449 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && 4450 "Unknown shift to lower!"); 4451 4452 // We only lower SRA, SRL of 1 here, all others use generic lowering. 4453 if (!isa<ConstantSDNode>(N->getOperand(1)) || 4454 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1) 4455 return SDValue(); 4456 4457 // If we are in thumb mode, we don't have RRX. 4458 if (ST->isThumb1Only()) return SDValue(); 4459 4460 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 4461 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 4462 DAG.getConstant(0, dl, MVT::i32)); 4463 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 4464 DAG.getConstant(1, dl, MVT::i32)); 4465 4466 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 4467 // captures the result into a carry flag. 4468 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 4469 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi); 4470 4471 // The low part is an ARMISD::RRX operand, which shifts the carry in. 4472 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 4473 4474 // Merge the pieces into a single i64 value. 4475 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 4476 } 4477 4478 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 4479 SDValue TmpOp0, TmpOp1; 4480 bool Invert = false; 4481 bool Swap = false; 4482 unsigned Opc = 0; 4483 4484 SDValue Op0 = Op.getOperand(0); 4485 SDValue Op1 = Op.getOperand(1); 4486 SDValue CC = Op.getOperand(2); 4487 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger(); 4488 EVT VT = Op.getValueType(); 4489 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 4490 SDLoc dl(Op); 4491 4492 if (Op1.getValueType().isFloatingPoint()) { 4493 switch (SetCCOpcode) { 4494 default: llvm_unreachable("Illegal FP comparison"); 4495 case ISD::SETUNE: 4496 case ISD::SETNE: Invert = true; // Fallthrough 4497 case ISD::SETOEQ: 4498 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 4499 case ISD::SETOLT: 4500 case ISD::SETLT: Swap = true; // Fallthrough 4501 case ISD::SETOGT: 4502 case ISD::SETGT: Opc = ARMISD::VCGT; break; 4503 case ISD::SETOLE: 4504 case ISD::SETLE: Swap = true; // Fallthrough 4505 case ISD::SETOGE: 4506 case ISD::SETGE: Opc = ARMISD::VCGE; break; 4507 case ISD::SETUGE: Swap = true; // Fallthrough 4508 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break; 4509 case ISD::SETUGT: Swap = true; // Fallthrough 4510 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break; 4511 case ISD::SETUEQ: Invert = true; // Fallthrough 4512 case ISD::SETONE: 4513 // Expand this to (OLT | OGT). 4514 TmpOp0 = Op0; 4515 TmpOp1 = Op1; 4516 Opc = ISD::OR; 4517 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0); 4518 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1); 4519 break; 4520 case ISD::SETUO: Invert = true; // Fallthrough 4521 case ISD::SETO: 4522 // Expand this to (OLT | OGE). 4523 TmpOp0 = Op0; 4524 TmpOp1 = Op1; 4525 Opc = ISD::OR; 4526 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0); 4527 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1); 4528 break; 4529 } 4530 } else { 4531 // Integer comparisons. 4532 switch (SetCCOpcode) { 4533 default: llvm_unreachable("Illegal integer comparison"); 4534 case ISD::SETNE: Invert = true; 4535 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 4536 case ISD::SETLT: Swap = true; 4537 case ISD::SETGT: Opc = ARMISD::VCGT; break; 4538 case ISD::SETLE: Swap = true; 4539 case ISD::SETGE: Opc = ARMISD::VCGE; break; 4540 case ISD::SETULT: Swap = true; 4541 case ISD::SETUGT: Opc = ARMISD::VCGTU; break; 4542 case ISD::SETULE: Swap = true; 4543 case ISD::SETUGE: Opc = ARMISD::VCGEU; break; 4544 } 4545 4546 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 4547 if (Opc == ARMISD::VCEQ) { 4548 4549 SDValue AndOp; 4550 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 4551 AndOp = Op0; 4552 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 4553 AndOp = Op1; 4554 4555 // Ignore bitconvert. 4556 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 4557 AndOp = AndOp.getOperand(0); 4558 4559 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 4560 Opc = ARMISD::VTST; 4561 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0)); 4562 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1)); 4563 Invert = !Invert; 4564 } 4565 } 4566 } 4567 4568 if (Swap) 4569 std::swap(Op0, Op1); 4570 4571 // If one of the operands is a constant vector zero, attempt to fold the 4572 // comparison to a specialized compare-against-zero form. 4573 SDValue SingleOp; 4574 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 4575 SingleOp = Op0; 4576 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 4577 if (Opc == ARMISD::VCGE) 4578 Opc = ARMISD::VCLEZ; 4579 else if (Opc == ARMISD::VCGT) 4580 Opc = ARMISD::VCLTZ; 4581 SingleOp = Op1; 4582 } 4583 4584 SDValue Result; 4585 if (SingleOp.getNode()) { 4586 switch (Opc) { 4587 case ARMISD::VCEQ: 4588 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break; 4589 case ARMISD::VCGE: 4590 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break; 4591 case ARMISD::VCLEZ: 4592 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break; 4593 case ARMISD::VCGT: 4594 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break; 4595 case ARMISD::VCLTZ: 4596 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break; 4597 default: 4598 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1); 4599 } 4600 } else { 4601 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1); 4602 } 4603 4604 Result = DAG.getSExtOrTrunc(Result, dl, VT); 4605 4606 if (Invert) 4607 Result = DAG.getNOT(dl, Result, VT); 4608 4609 return Result; 4610 } 4611 4612 /// isNEONModifiedImm - Check if the specified splat value corresponds to a 4613 /// valid vector constant for a NEON instruction with a "modified immediate" 4614 /// operand (e.g., VMOV). If so, return the encoded value. 4615 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 4616 unsigned SplatBitSize, SelectionDAG &DAG, 4617 SDLoc dl, EVT &VT, bool is128Bits, 4618 NEONModImmType type) { 4619 unsigned OpCmode, Imm; 4620 4621 // SplatBitSize is set to the smallest size that splats the vector, so a 4622 // zero vector will always have SplatBitSize == 8. However, NEON modified 4623 // immediate instructions others than VMOV do not support the 8-bit encoding 4624 // of a zero vector, and the default encoding of zero is supposed to be the 4625 // 32-bit version. 4626 if (SplatBits == 0) 4627 SplatBitSize = 32; 4628 4629 switch (SplatBitSize) { 4630 case 8: 4631 if (type != VMOVModImm) 4632 return SDValue(); 4633 // Any 1-byte value is OK. Op=0, Cmode=1110. 4634 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 4635 OpCmode = 0xe; 4636 Imm = SplatBits; 4637 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 4638 break; 4639 4640 case 16: 4641 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 4642 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 4643 if ((SplatBits & ~0xff) == 0) { 4644 // Value = 0x00nn: Op=x, Cmode=100x. 4645 OpCmode = 0x8; 4646 Imm = SplatBits; 4647 break; 4648 } 4649 if ((SplatBits & ~0xff00) == 0) { 4650 // Value = 0xnn00: Op=x, Cmode=101x. 4651 OpCmode = 0xa; 4652 Imm = SplatBits >> 8; 4653 break; 4654 } 4655 return SDValue(); 4656 4657 case 32: 4658 // NEON's 32-bit VMOV supports splat values where: 4659 // * only one byte is nonzero, or 4660 // * the least significant byte is 0xff and the second byte is nonzero, or 4661 // * the least significant 2 bytes are 0xff and the third is nonzero. 4662 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 4663 if ((SplatBits & ~0xff) == 0) { 4664 // Value = 0x000000nn: Op=x, Cmode=000x. 4665 OpCmode = 0; 4666 Imm = SplatBits; 4667 break; 4668 } 4669 if ((SplatBits & ~0xff00) == 0) { 4670 // Value = 0x0000nn00: Op=x, Cmode=001x. 4671 OpCmode = 0x2; 4672 Imm = SplatBits >> 8; 4673 break; 4674 } 4675 if ((SplatBits & ~0xff0000) == 0) { 4676 // Value = 0x00nn0000: Op=x, Cmode=010x. 4677 OpCmode = 0x4; 4678 Imm = SplatBits >> 16; 4679 break; 4680 } 4681 if ((SplatBits & ~0xff000000) == 0) { 4682 // Value = 0xnn000000: Op=x, Cmode=011x. 4683 OpCmode = 0x6; 4684 Imm = SplatBits >> 24; 4685 break; 4686 } 4687 4688 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 4689 if (type == OtherModImm) return SDValue(); 4690 4691 if ((SplatBits & ~0xffff) == 0 && 4692 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 4693 // Value = 0x0000nnff: Op=x, Cmode=1100. 4694 OpCmode = 0xc; 4695 Imm = SplatBits >> 8; 4696 break; 4697 } 4698 4699 if ((SplatBits & ~0xffffff) == 0 && 4700 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 4701 // Value = 0x00nnffff: Op=x, Cmode=1101. 4702 OpCmode = 0xd; 4703 Imm = SplatBits >> 16; 4704 break; 4705 } 4706 4707 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 4708 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 4709 // VMOV.I32. A (very) minor optimization would be to replicate the value 4710 // and fall through here to test for a valid 64-bit splat. But, then the 4711 // caller would also need to check and handle the change in size. 4712 return SDValue(); 4713 4714 case 64: { 4715 if (type != VMOVModImm) 4716 return SDValue(); 4717 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 4718 uint64_t BitMask = 0xff; 4719 uint64_t Val = 0; 4720 unsigned ImmMask = 1; 4721 Imm = 0; 4722 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 4723 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 4724 Val |= BitMask; 4725 Imm |= ImmMask; 4726 } else if ((SplatBits & BitMask) != 0) { 4727 return SDValue(); 4728 } 4729 BitMask <<= 8; 4730 ImmMask <<= 1; 4731 } 4732 4733 if (DAG.getTargetLoweringInfo().isBigEndian()) 4734 // swap higher and lower 32 bit word 4735 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4); 4736 4737 // Op=1, Cmode=1110. 4738 OpCmode = 0x1e; 4739 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 4740 break; 4741 } 4742 4743 default: 4744 llvm_unreachable("unexpected size for isNEONModifiedImm"); 4745 } 4746 4747 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm); 4748 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32); 4749 } 4750 4751 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG, 4752 const ARMSubtarget *ST) const { 4753 if (!ST->hasVFP3()) 4754 return SDValue(); 4755 4756 bool IsDouble = Op.getValueType() == MVT::f64; 4757 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op); 4758 4759 // Use the default (constant pool) lowering for double constants when we have 4760 // an SP-only FPU 4761 if (IsDouble && Subtarget->isFPOnlySP()) 4762 return SDValue(); 4763 4764 // Try splatting with a VMOV.f32... 4765 APFloat FPVal = CFP->getValueAPF(); 4766 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal); 4767 4768 if (ImmVal != -1) { 4769 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) { 4770 // We have code in place to select a valid ConstantFP already, no need to 4771 // do any mangling. 4772 return Op; 4773 } 4774 4775 // It's a float and we are trying to use NEON operations where 4776 // possible. Lower it to a splat followed by an extract. 4777 SDLoc DL(Op); 4778 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32); 4779 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, 4780 NewVal); 4781 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant, 4782 DAG.getConstant(0, DL, MVT::i32)); 4783 } 4784 4785 // The rest of our options are NEON only, make sure that's allowed before 4786 // proceeding.. 4787 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP())) 4788 return SDValue(); 4789 4790 EVT VMovVT; 4791 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue(); 4792 4793 // It wouldn't really be worth bothering for doubles except for one very 4794 // important value, which does happen to match: 0.0. So make sure we don't do 4795 // anything stupid. 4796 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32)) 4797 return SDValue(); 4798 4799 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too). 4800 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), 4801 VMovVT, false, VMOVModImm); 4802 if (NewVal != SDValue()) { 4803 SDLoc DL(Op); 4804 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT, 4805 NewVal); 4806 if (IsDouble) 4807 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 4808 4809 // It's a float: cast and extract a vector element. 4810 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 4811 VecConstant); 4812 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 4813 DAG.getConstant(0, DL, MVT::i32)); 4814 } 4815 4816 // Finally, try a VMVN.i32 4817 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT, 4818 false, VMVNModImm); 4819 if (NewVal != SDValue()) { 4820 SDLoc DL(Op); 4821 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal); 4822 4823 if (IsDouble) 4824 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 4825 4826 // It's a float: cast and extract a vector element. 4827 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 4828 VecConstant); 4829 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 4830 DAG.getConstant(0, DL, MVT::i32)); 4831 } 4832 4833 return SDValue(); 4834 } 4835 4836 // check if an VEXT instruction can handle the shuffle mask when the 4837 // vector sources of the shuffle are the same. 4838 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) { 4839 unsigned NumElts = VT.getVectorNumElements(); 4840 4841 // Assume that the first shuffle index is not UNDEF. Fail if it is. 4842 if (M[0] < 0) 4843 return false; 4844 4845 Imm = M[0]; 4846 4847 // If this is a VEXT shuffle, the immediate value is the index of the first 4848 // element. The other shuffle indices must be the successive elements after 4849 // the first one. 4850 unsigned ExpectedElt = Imm; 4851 for (unsigned i = 1; i < NumElts; ++i) { 4852 // Increment the expected index. If it wraps around, just follow it 4853 // back to index zero and keep going. 4854 ++ExpectedElt; 4855 if (ExpectedElt == NumElts) 4856 ExpectedElt = 0; 4857 4858 if (M[i] < 0) continue; // ignore UNDEF indices 4859 if (ExpectedElt != static_cast<unsigned>(M[i])) 4860 return false; 4861 } 4862 4863 return true; 4864 } 4865 4866 4867 static bool isVEXTMask(ArrayRef<int> M, EVT VT, 4868 bool &ReverseVEXT, unsigned &Imm) { 4869 unsigned NumElts = VT.getVectorNumElements(); 4870 ReverseVEXT = false; 4871 4872 // Assume that the first shuffle index is not UNDEF. Fail if it is. 4873 if (M[0] < 0) 4874 return false; 4875 4876 Imm = M[0]; 4877 4878 // If this is a VEXT shuffle, the immediate value is the index of the first 4879 // element. The other shuffle indices must be the successive elements after 4880 // the first one. 4881 unsigned ExpectedElt = Imm; 4882 for (unsigned i = 1; i < NumElts; ++i) { 4883 // Increment the expected index. If it wraps around, it may still be 4884 // a VEXT but the source vectors must be swapped. 4885 ExpectedElt += 1; 4886 if (ExpectedElt == NumElts * 2) { 4887 ExpectedElt = 0; 4888 ReverseVEXT = true; 4889 } 4890 4891 if (M[i] < 0) continue; // ignore UNDEF indices 4892 if (ExpectedElt != static_cast<unsigned>(M[i])) 4893 return false; 4894 } 4895 4896 // Adjust the index value if the source operands will be swapped. 4897 if (ReverseVEXT) 4898 Imm -= NumElts; 4899 4900 return true; 4901 } 4902 4903 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 4904 /// instruction with the specified blocksize. (The order of the elements 4905 /// within each block of the vector is reversed.) 4906 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 4907 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 4908 "Only possible block sizes for VREV are: 16, 32, 64"); 4909 4910 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4911 if (EltSz == 64) 4912 return false; 4913 4914 unsigned NumElts = VT.getVectorNumElements(); 4915 unsigned BlockElts = M[0] + 1; 4916 // If the first shuffle index is UNDEF, be optimistic. 4917 if (M[0] < 0) 4918 BlockElts = BlockSize / EltSz; 4919 4920 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 4921 return false; 4922 4923 for (unsigned i = 0; i < NumElts; ++i) { 4924 if (M[i] < 0) continue; // ignore UNDEF indices 4925 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 4926 return false; 4927 } 4928 4929 return true; 4930 } 4931 4932 static bool isVTBLMask(ArrayRef<int> M, EVT VT) { 4933 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 4934 // range, then 0 is placed into the resulting vector. So pretty much any mask 4935 // of 8 elements can work here. 4936 return VT == MVT::v8i8 && M.size() == 8; 4937 } 4938 4939 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4940 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4941 if (EltSz == 64) 4942 return false; 4943 4944 unsigned NumElts = VT.getVectorNumElements(); 4945 WhichResult = (M[0] == 0 ? 0 : 1); 4946 for (unsigned i = 0; i < NumElts; i += 2) { 4947 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 4948 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult)) 4949 return false; 4950 } 4951 return true; 4952 } 4953 4954 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 4955 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4956 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 4957 static bool isVTRN_v_undef_Mask(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 += 2) { 4965 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) || 4966 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult)) 4967 return false; 4968 } 4969 return true; 4970 } 4971 4972 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 4973 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4974 if (EltSz == 64) 4975 return false; 4976 4977 unsigned NumElts = VT.getVectorNumElements(); 4978 WhichResult = (M[0] == 0 ? 0 : 1); 4979 for (unsigned i = 0; i != NumElts; ++i) { 4980 if (M[i] < 0) continue; // ignore UNDEF indices 4981 if ((unsigned) M[i] != 2 * i + WhichResult) 4982 return false; 4983 } 4984 4985 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 4986 if (VT.is64BitVector() && EltSz == 32) 4987 return false; 4988 4989 return true; 4990 } 4991 4992 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 4993 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 4994 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 4995 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 4996 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 4997 if (EltSz == 64) 4998 return false; 4999 5000 unsigned Half = VT.getVectorNumElements() / 2; 5001 WhichResult = (M[0] == 0 ? 0 : 1); 5002 for (unsigned j = 0; j != 2; ++j) { 5003 unsigned Idx = WhichResult; 5004 for (unsigned i = 0; i != Half; ++i) { 5005 int MIdx = M[i + j * Half]; 5006 if (MIdx >= 0 && (unsigned) MIdx != Idx) 5007 return false; 5008 Idx += 2; 5009 } 5010 } 5011 5012 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5013 if (VT.is64BitVector() && EltSz == 32) 5014 return false; 5015 5016 return true; 5017 } 5018 5019 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5020 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5021 if (EltSz == 64) 5022 return false; 5023 5024 unsigned NumElts = VT.getVectorNumElements(); 5025 WhichResult = (M[0] == 0 ? 0 : 1); 5026 unsigned Idx = WhichResult * NumElts / 2; 5027 for (unsigned i = 0; i != NumElts; i += 2) { 5028 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 5029 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts)) 5030 return false; 5031 Idx += 1; 5032 } 5033 5034 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5035 if (VT.is64BitVector() && EltSz == 32) 5036 return false; 5037 5038 return true; 5039 } 5040 5041 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 5042 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 5043 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 5044 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 5045 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5046 if (EltSz == 64) 5047 return false; 5048 5049 unsigned NumElts = VT.getVectorNumElements(); 5050 WhichResult = (M[0] == 0 ? 0 : 1); 5051 unsigned Idx = WhichResult * NumElts / 2; 5052 for (unsigned i = 0; i != NumElts; i += 2) { 5053 if ((M[i] >= 0 && (unsigned) M[i] != Idx) || 5054 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx)) 5055 return false; 5056 Idx += 1; 5057 } 5058 5059 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5060 if (VT.is64BitVector() && EltSz == 32) 5061 return false; 5062 5063 return true; 5064 } 5065 5066 /// \return true if this is a reverse operation on an vector. 5067 static bool isReverseMask(ArrayRef<int> M, EVT VT) { 5068 unsigned NumElts = VT.getVectorNumElements(); 5069 // Make sure the mask has the right size. 5070 if (NumElts != M.size()) 5071 return false; 5072 5073 // Look for <15, ..., 3, -1, 1, 0>. 5074 for (unsigned i = 0; i != NumElts; ++i) 5075 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i)) 5076 return false; 5077 5078 return true; 5079 } 5080 5081 // If N is an integer constant that can be moved into a register in one 5082 // instruction, return an SDValue of such a constant (will become a MOV 5083 // instruction). Otherwise return null. 5084 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 5085 const ARMSubtarget *ST, SDLoc dl) { 5086 uint64_t Val; 5087 if (!isa<ConstantSDNode>(N)) 5088 return SDValue(); 5089 Val = cast<ConstantSDNode>(N)->getZExtValue(); 5090 5091 if (ST->isThumb1Only()) { 5092 if (Val <= 255 || ~Val <= 255) 5093 return DAG.getConstant(Val, dl, MVT::i32); 5094 } else { 5095 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 5096 return DAG.getConstant(Val, dl, MVT::i32); 5097 } 5098 return SDValue(); 5099 } 5100 5101 // If this is a case we can't handle, return null and let the default 5102 // expansion code take care of it. 5103 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 5104 const ARMSubtarget *ST) const { 5105 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 5106 SDLoc dl(Op); 5107 EVT VT = Op.getValueType(); 5108 5109 APInt SplatBits, SplatUndef; 5110 unsigned SplatBitSize; 5111 bool HasAnyUndefs; 5112 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 5113 if (SplatBitSize <= 64) { 5114 // Check if an immediate VMOV works. 5115 EVT VmovVT; 5116 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 5117 SplatUndef.getZExtValue(), SplatBitSize, 5118 DAG, dl, VmovVT, VT.is128BitVector(), 5119 VMOVModImm); 5120 if (Val.getNode()) { 5121 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 5122 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5123 } 5124 5125 // Try an immediate VMVN. 5126 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 5127 Val = isNEONModifiedImm(NegatedImm, 5128 SplatUndef.getZExtValue(), SplatBitSize, 5129 DAG, dl, VmovVT, VT.is128BitVector(), 5130 VMVNModImm); 5131 if (Val.getNode()) { 5132 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 5133 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5134 } 5135 5136 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 5137 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) { 5138 int ImmVal = ARM_AM::getFP32Imm(SplatBits); 5139 if (ImmVal != -1) { 5140 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32); 5141 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 5142 } 5143 } 5144 } 5145 } 5146 5147 // Scan through the operands to see if only one value is used. 5148 // 5149 // As an optimisation, even if more than one value is used it may be more 5150 // profitable to splat with one value then change some lanes. 5151 // 5152 // Heuristically we decide to do this if the vector has a "dominant" value, 5153 // defined as splatted to more than half of the lanes. 5154 unsigned NumElts = VT.getVectorNumElements(); 5155 bool isOnlyLowElement = true; 5156 bool usesOnlyOneValue = true; 5157 bool hasDominantValue = false; 5158 bool isConstant = true; 5159 5160 // Map of the number of times a particular SDValue appears in the 5161 // element list. 5162 DenseMap<SDValue, unsigned> ValueCounts; 5163 SDValue Value; 5164 for (unsigned i = 0; i < NumElts; ++i) { 5165 SDValue V = Op.getOperand(i); 5166 if (V.getOpcode() == ISD::UNDEF) 5167 continue; 5168 if (i > 0) 5169 isOnlyLowElement = false; 5170 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 5171 isConstant = false; 5172 5173 ValueCounts.insert(std::make_pair(V, 0)); 5174 unsigned &Count = ValueCounts[V]; 5175 5176 // Is this value dominant? (takes up more than half of the lanes) 5177 if (++Count > (NumElts / 2)) { 5178 hasDominantValue = true; 5179 Value = V; 5180 } 5181 } 5182 if (ValueCounts.size() != 1) 5183 usesOnlyOneValue = false; 5184 if (!Value.getNode() && ValueCounts.size() > 0) 5185 Value = ValueCounts.begin()->first; 5186 5187 if (ValueCounts.size() == 0) 5188 return DAG.getUNDEF(VT); 5189 5190 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR. 5191 // Keep going if we are hitting this case. 5192 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode())) 5193 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 5194 5195 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5196 5197 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 5198 // i32 and try again. 5199 if (hasDominantValue && EltSize <= 32) { 5200 if (!isConstant) { 5201 SDValue N; 5202 5203 // If we are VDUPing a value that comes directly from a vector, that will 5204 // cause an unnecessary move to and from a GPR, where instead we could 5205 // just use VDUPLANE. We can only do this if the lane being extracted 5206 // is at a constant index, as the VDUP from lane instructions only have 5207 // constant-index forms. 5208 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5209 isa<ConstantSDNode>(Value->getOperand(1))) { 5210 // We need to create a new undef vector to use for the VDUPLANE if the 5211 // size of the vector from which we get the value is different than the 5212 // size of the vector that we need to create. We will insert the element 5213 // such that the register coalescer will remove unnecessary copies. 5214 if (VT != Value->getOperand(0).getValueType()) { 5215 ConstantSDNode *constIndex; 5216 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)); 5217 assert(constIndex && "The index is not a constant!"); 5218 unsigned index = constIndex->getAPIntValue().getLimitedValue() % 5219 VT.getVectorNumElements(); 5220 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5221 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT), 5222 Value, DAG.getConstant(index, dl, MVT::i32)), 5223 DAG.getConstant(index, dl, MVT::i32)); 5224 } else 5225 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5226 Value->getOperand(0), Value->getOperand(1)); 5227 } else 5228 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value); 5229 5230 if (!usesOnlyOneValue) { 5231 // The dominant value was splatted as 'N', but we now have to insert 5232 // all differing elements. 5233 for (unsigned I = 0; I < NumElts; ++I) { 5234 if (Op.getOperand(I) == Value) 5235 continue; 5236 SmallVector<SDValue, 3> Ops; 5237 Ops.push_back(N); 5238 Ops.push_back(Op.getOperand(I)); 5239 Ops.push_back(DAG.getConstant(I, dl, MVT::i32)); 5240 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops); 5241 } 5242 } 5243 return N; 5244 } 5245 if (VT.getVectorElementType().isFloatingPoint()) { 5246 SmallVector<SDValue, 8> Ops; 5247 for (unsigned i = 0; i < NumElts; ++i) 5248 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32, 5249 Op.getOperand(i))); 5250 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 5251 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops); 5252 Val = LowerBUILD_VECTOR(Val, DAG, ST); 5253 if (Val.getNode()) 5254 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5255 } 5256 if (usesOnlyOneValue) { 5257 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 5258 if (isConstant && Val.getNode()) 5259 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 5260 } 5261 } 5262 5263 // If all elements are constants and the case above didn't get hit, fall back 5264 // to the default expansion, which will generate a load from the constant 5265 // pool. 5266 if (isConstant) 5267 return SDValue(); 5268 5269 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 5270 if (NumElts >= 4) { 5271 SDValue shuffle = ReconstructShuffle(Op, DAG); 5272 if (shuffle != SDValue()) 5273 return shuffle; 5274 } 5275 5276 // Vectors with 32- or 64-bit elements can be built by directly assigning 5277 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 5278 // will be legalized. 5279 if (EltSize >= 32) { 5280 // Do the expansion with floating-point types, since that is what the VFP 5281 // registers are defined to use, and since i64 is not legal. 5282 EVT EltVT = EVT::getFloatingPointVT(EltSize); 5283 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 5284 SmallVector<SDValue, 8> Ops; 5285 for (unsigned i = 0; i < NumElts; ++i) 5286 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 5287 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 5288 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5289 } 5290 5291 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 5292 // know the default expansion would otherwise fall back on something even 5293 // worse. For a vector with one or two non-undef values, that's 5294 // scalar_to_vector for the elements followed by a shuffle (provided the 5295 // shuffle is valid for the target) and materialization element by element 5296 // on the stack followed by a load for everything else. 5297 if (!isConstant && !usesOnlyOneValue) { 5298 SDValue Vec = DAG.getUNDEF(VT); 5299 for (unsigned i = 0 ; i < NumElts; ++i) { 5300 SDValue V = Op.getOperand(i); 5301 if (V.getOpcode() == ISD::UNDEF) 5302 continue; 5303 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32); 5304 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx); 5305 } 5306 return Vec; 5307 } 5308 5309 return SDValue(); 5310 } 5311 5312 // Gather data to see if the operation can be modelled as a 5313 // shuffle in combination with VEXTs. 5314 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 5315 SelectionDAG &DAG) const { 5316 SDLoc dl(Op); 5317 EVT VT = Op.getValueType(); 5318 unsigned NumElts = VT.getVectorNumElements(); 5319 5320 SmallVector<SDValue, 2> SourceVecs; 5321 SmallVector<unsigned, 2> MinElts; 5322 SmallVector<unsigned, 2> MaxElts; 5323 5324 for (unsigned i = 0; i < NumElts; ++i) { 5325 SDValue V = Op.getOperand(i); 5326 if (V.getOpcode() == ISD::UNDEF) 5327 continue; 5328 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 5329 // A shuffle can only come from building a vector from various 5330 // elements of other vectors. 5331 return SDValue(); 5332 } else if (V.getOperand(0).getValueType().getVectorElementType() != 5333 VT.getVectorElementType()) { 5334 // This code doesn't know how to handle shuffles where the vector 5335 // element types do not match (this happens because type legalization 5336 // promotes the return type of EXTRACT_VECTOR_ELT). 5337 // FIXME: It might be appropriate to extend this code to handle 5338 // mismatched types. 5339 return SDValue(); 5340 } 5341 5342 // Record this extraction against the appropriate vector if possible... 5343 SDValue SourceVec = V.getOperand(0); 5344 // If the element number isn't a constant, we can't effectively 5345 // analyze what's going on. 5346 if (!isa<ConstantSDNode>(V.getOperand(1))) 5347 return SDValue(); 5348 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 5349 bool FoundSource = false; 5350 for (unsigned j = 0; j < SourceVecs.size(); ++j) { 5351 if (SourceVecs[j] == SourceVec) { 5352 if (MinElts[j] > EltNo) 5353 MinElts[j] = EltNo; 5354 if (MaxElts[j] < EltNo) 5355 MaxElts[j] = EltNo; 5356 FoundSource = true; 5357 break; 5358 } 5359 } 5360 5361 // Or record a new source if not... 5362 if (!FoundSource) { 5363 SourceVecs.push_back(SourceVec); 5364 MinElts.push_back(EltNo); 5365 MaxElts.push_back(EltNo); 5366 } 5367 } 5368 5369 // Currently only do something sane when at most two source vectors 5370 // involved. 5371 if (SourceVecs.size() > 2) 5372 return SDValue(); 5373 5374 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) }; 5375 int VEXTOffsets[2] = {0, 0}; 5376 5377 // This loop extracts the usage patterns of the source vectors 5378 // and prepares appropriate SDValues for a shuffle if possible. 5379 for (unsigned i = 0; i < SourceVecs.size(); ++i) { 5380 if (SourceVecs[i].getValueType() == VT) { 5381 // No VEXT necessary 5382 ShuffleSrcs[i] = SourceVecs[i]; 5383 VEXTOffsets[i] = 0; 5384 continue; 5385 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) { 5386 // It probably isn't worth padding out a smaller vector just to 5387 // break it down again in a shuffle. 5388 return SDValue(); 5389 } 5390 5391 // Since only 64-bit and 128-bit vectors are legal on ARM and 5392 // we've eliminated the other cases... 5393 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts && 5394 "unexpected vector sizes in ReconstructShuffle"); 5395 5396 if (MaxElts[i] - MinElts[i] >= NumElts) { 5397 // Span too large for a VEXT to cope 5398 return SDValue(); 5399 } 5400 5401 if (MinElts[i] >= NumElts) { 5402 // The extraction can just take the second half 5403 VEXTOffsets[i] = NumElts; 5404 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5405 SourceVecs[i], 5406 DAG.getIntPtrConstant(NumElts, dl)); 5407 } else if (MaxElts[i] < NumElts) { 5408 // The extraction can just take the first half 5409 VEXTOffsets[i] = 0; 5410 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5411 SourceVecs[i], 5412 DAG.getIntPtrConstant(0, dl)); 5413 } else { 5414 // An actual VEXT is needed 5415 VEXTOffsets[i] = MinElts[i]; 5416 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5417 SourceVecs[i], 5418 DAG.getIntPtrConstant(0, dl)); 5419 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, 5420 SourceVecs[i], 5421 DAG.getIntPtrConstant(NumElts, dl)); 5422 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2, 5423 DAG.getConstant(VEXTOffsets[i], dl, 5424 MVT::i32)); 5425 } 5426 } 5427 5428 SmallVector<int, 8> Mask; 5429 5430 for (unsigned i = 0; i < NumElts; ++i) { 5431 SDValue Entry = Op.getOperand(i); 5432 if (Entry.getOpcode() == ISD::UNDEF) { 5433 Mask.push_back(-1); 5434 continue; 5435 } 5436 5437 SDValue ExtractVec = Entry.getOperand(0); 5438 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i) 5439 .getOperand(1))->getSExtValue(); 5440 if (ExtractVec == SourceVecs[0]) { 5441 Mask.push_back(ExtractElt - VEXTOffsets[0]); 5442 } else { 5443 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]); 5444 } 5445 } 5446 5447 // Final check before we try to produce nonsense... 5448 if (isShuffleMaskLegal(Mask, VT)) 5449 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1], 5450 &Mask[0]); 5451 5452 return SDValue(); 5453 } 5454 5455 /// isShuffleMaskLegal - Targets can use this to indicate that they only 5456 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 5457 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 5458 /// are assumed to be legal. 5459 bool 5460 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, 5461 EVT VT) const { 5462 if (VT.getVectorNumElements() == 4 && 5463 (VT.is128BitVector() || VT.is64BitVector())) { 5464 unsigned PFIndexes[4]; 5465 for (unsigned i = 0; i != 4; ++i) { 5466 if (M[i] < 0) 5467 PFIndexes[i] = 8; 5468 else 5469 PFIndexes[i] = M[i]; 5470 } 5471 5472 // Compute the index in the perfect shuffle table. 5473 unsigned PFTableIndex = 5474 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 5475 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 5476 unsigned Cost = (PFEntry >> 30); 5477 5478 if (Cost <= 4) 5479 return true; 5480 } 5481 5482 bool ReverseVEXT; 5483 unsigned Imm, WhichResult; 5484 5485 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5486 return (EltSize >= 32 || 5487 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 5488 isVREVMask(M, VT, 64) || 5489 isVREVMask(M, VT, 32) || 5490 isVREVMask(M, VT, 16) || 5491 isVEXTMask(M, VT, ReverseVEXT, Imm) || 5492 isVTBLMask(M, VT) || 5493 isVTRNMask(M, VT, WhichResult) || 5494 isVUZPMask(M, VT, WhichResult) || 5495 isVZIPMask(M, VT, WhichResult) || 5496 isVTRN_v_undef_Mask(M, VT, WhichResult) || 5497 isVUZP_v_undef_Mask(M, VT, WhichResult) || 5498 isVZIP_v_undef_Mask(M, VT, WhichResult) || 5499 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT))); 5500 } 5501 5502 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 5503 /// the specified operations to build the shuffle. 5504 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 5505 SDValue RHS, SelectionDAG &DAG, 5506 SDLoc dl) { 5507 unsigned OpNum = (PFEntry >> 26) & 0x0F; 5508 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 5509 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 5510 5511 enum { 5512 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 5513 OP_VREV, 5514 OP_VDUP0, 5515 OP_VDUP1, 5516 OP_VDUP2, 5517 OP_VDUP3, 5518 OP_VEXT1, 5519 OP_VEXT2, 5520 OP_VEXT3, 5521 OP_VUZPL, // VUZP, left result 5522 OP_VUZPR, // VUZP, right result 5523 OP_VZIPL, // VZIP, left result 5524 OP_VZIPR, // VZIP, right result 5525 OP_VTRNL, // VTRN, left result 5526 OP_VTRNR // VTRN, right result 5527 }; 5528 5529 if (OpNum == OP_COPY) { 5530 if (LHSID == (1*9+2)*9+3) return LHS; 5531 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 5532 return RHS; 5533 } 5534 5535 SDValue OpLHS, OpRHS; 5536 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 5537 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 5538 EVT VT = OpLHS.getValueType(); 5539 5540 switch (OpNum) { 5541 default: llvm_unreachable("Unknown shuffle opcode!"); 5542 case OP_VREV: 5543 // VREV divides the vector in half and swaps within the half. 5544 if (VT.getVectorElementType() == MVT::i32 || 5545 VT.getVectorElementType() == MVT::f32) 5546 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 5547 // vrev <4 x i16> -> VREV32 5548 if (VT.getVectorElementType() == MVT::i16) 5549 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 5550 // vrev <4 x i8> -> VREV16 5551 assert(VT.getVectorElementType() == MVT::i8); 5552 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 5553 case OP_VDUP0: 5554 case OP_VDUP1: 5555 case OP_VDUP2: 5556 case OP_VDUP3: 5557 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5558 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32)); 5559 case OP_VEXT1: 5560 case OP_VEXT2: 5561 case OP_VEXT3: 5562 return DAG.getNode(ARMISD::VEXT, dl, VT, 5563 OpLHS, OpRHS, 5564 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32)); 5565 case OP_VUZPL: 5566 case OP_VUZPR: 5567 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5568 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 5569 case OP_VZIPL: 5570 case OP_VZIPR: 5571 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5572 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 5573 case OP_VTRNL: 5574 case OP_VTRNR: 5575 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5576 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 5577 } 5578 } 5579 5580 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 5581 ArrayRef<int> ShuffleMask, 5582 SelectionDAG &DAG) { 5583 // Check to see if we can use the VTBL instruction. 5584 SDValue V1 = Op.getOperand(0); 5585 SDValue V2 = Op.getOperand(1); 5586 SDLoc DL(Op); 5587 5588 SmallVector<SDValue, 8> VTBLMask; 5589 for (ArrayRef<int>::iterator 5590 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 5591 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32)); 5592 5593 if (V2.getNode()->getOpcode() == ISD::UNDEF) 5594 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 5595 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask)); 5596 5597 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 5598 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask)); 5599 } 5600 5601 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op, 5602 SelectionDAG &DAG) { 5603 SDLoc DL(Op); 5604 SDValue OpLHS = Op.getOperand(0); 5605 EVT VT = OpLHS.getValueType(); 5606 5607 assert((VT == MVT::v8i16 || VT == MVT::v16i8) && 5608 "Expect an v8i16/v16i8 type"); 5609 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS); 5610 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now, 5611 // extract the first 8 bytes into the top double word and the last 8 bytes 5612 // into the bottom double word. The v8i16 case is similar. 5613 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4; 5614 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS, 5615 DAG.getConstant(ExtractNum, DL, MVT::i32)); 5616 } 5617 5618 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 5619 SDValue V1 = Op.getOperand(0); 5620 SDValue V2 = Op.getOperand(1); 5621 SDLoc dl(Op); 5622 EVT VT = Op.getValueType(); 5623 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 5624 5625 // Convert shuffles that are directly supported on NEON to target-specific 5626 // DAG nodes, instead of keeping them as shuffles and matching them again 5627 // during code selection. This is more efficient and avoids the possibility 5628 // of inconsistencies between legalization and selection. 5629 // FIXME: floating-point vectors should be canonicalized to integer vectors 5630 // of the same time so that they get CSEd properly. 5631 ArrayRef<int> ShuffleMask = SVN->getMask(); 5632 5633 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5634 if (EltSize <= 32) { 5635 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) { 5636 int Lane = SVN->getSplatIndex(); 5637 // If this is undef splat, generate it via "just" vdup, if possible. 5638 if (Lane == -1) Lane = 0; 5639 5640 // Test if V1 is a SCALAR_TO_VECTOR. 5641 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 5642 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 5643 } 5644 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 5645 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 5646 // reaches it). 5647 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 5648 !isa<ConstantSDNode>(V1.getOperand(0))) { 5649 bool IsScalarToVector = true; 5650 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 5651 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) { 5652 IsScalarToVector = false; 5653 break; 5654 } 5655 if (IsScalarToVector) 5656 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 5657 } 5658 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 5659 DAG.getConstant(Lane, dl, MVT::i32)); 5660 } 5661 5662 bool ReverseVEXT; 5663 unsigned Imm; 5664 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 5665 if (ReverseVEXT) 5666 std::swap(V1, V2); 5667 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 5668 DAG.getConstant(Imm, dl, MVT::i32)); 5669 } 5670 5671 if (isVREVMask(ShuffleMask, VT, 64)) 5672 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 5673 if (isVREVMask(ShuffleMask, VT, 32)) 5674 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 5675 if (isVREVMask(ShuffleMask, VT, 16)) 5676 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 5677 5678 if (V2->getOpcode() == ISD::UNDEF && 5679 isSingletonVEXTMask(ShuffleMask, VT, Imm)) { 5680 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1, 5681 DAG.getConstant(Imm, dl, MVT::i32)); 5682 } 5683 5684 // Check for Neon shuffles that modify both input vectors in place. 5685 // If both results are used, i.e., if there are two shuffles with the same 5686 // source operands and with masks corresponding to both results of one of 5687 // these operations, DAG memoization will ensure that a single node is 5688 // used for both shuffles. 5689 unsigned WhichResult; 5690 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 5691 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5692 V1, V2).getValue(WhichResult); 5693 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 5694 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5695 V1, V2).getValue(WhichResult); 5696 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 5697 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5698 V1, V2).getValue(WhichResult); 5699 5700 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5701 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 5702 V1, V1).getValue(WhichResult); 5703 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5704 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 5705 V1, V1).getValue(WhichResult); 5706 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5707 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 5708 V1, V1).getValue(WhichResult); 5709 } 5710 5711 // If the shuffle is not directly supported and it has 4 elements, use 5712 // the PerfectShuffle-generated table to synthesize it from other shuffles. 5713 unsigned NumElts = VT.getVectorNumElements(); 5714 if (NumElts == 4) { 5715 unsigned PFIndexes[4]; 5716 for (unsigned i = 0; i != 4; ++i) { 5717 if (ShuffleMask[i] < 0) 5718 PFIndexes[i] = 8; 5719 else 5720 PFIndexes[i] = ShuffleMask[i]; 5721 } 5722 5723 // Compute the index in the perfect shuffle table. 5724 unsigned PFTableIndex = 5725 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 5726 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 5727 unsigned Cost = (PFEntry >> 30); 5728 5729 if (Cost <= 4) 5730 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 5731 } 5732 5733 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 5734 if (EltSize >= 32) { 5735 // Do the expansion with floating-point types, since that is what the VFP 5736 // registers are defined to use, and since i64 is not legal. 5737 EVT EltVT = EVT::getFloatingPointVT(EltSize); 5738 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 5739 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 5740 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 5741 SmallVector<SDValue, 8> Ops; 5742 for (unsigned i = 0; i < NumElts; ++i) { 5743 if (ShuffleMask[i] < 0) 5744 Ops.push_back(DAG.getUNDEF(EltVT)); 5745 else 5746 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 5747 ShuffleMask[i] < (int)NumElts ? V1 : V2, 5748 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 5749 dl, MVT::i32))); 5750 } 5751 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 5752 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5753 } 5754 5755 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT)) 5756 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG); 5757 5758 if (VT == MVT::v8i8) { 5759 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG); 5760 if (NewOp.getNode()) 5761 return NewOp; 5762 } 5763 5764 return SDValue(); 5765 } 5766 5767 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 5768 // INSERT_VECTOR_ELT is legal only for immediate indexes. 5769 SDValue Lane = Op.getOperand(2); 5770 if (!isa<ConstantSDNode>(Lane)) 5771 return SDValue(); 5772 5773 return Op; 5774 } 5775 5776 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 5777 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 5778 SDValue Lane = Op.getOperand(1); 5779 if (!isa<ConstantSDNode>(Lane)) 5780 return SDValue(); 5781 5782 SDValue Vec = Op.getOperand(0); 5783 if (Op.getValueType() == MVT::i32 && 5784 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) { 5785 SDLoc dl(Op); 5786 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 5787 } 5788 5789 return Op; 5790 } 5791 5792 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 5793 // The only time a CONCAT_VECTORS operation can have legal types is when 5794 // two 64-bit vectors are concatenated to a 128-bit vector. 5795 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 5796 "unexpected CONCAT_VECTORS"); 5797 SDLoc dl(Op); 5798 SDValue Val = DAG.getUNDEF(MVT::v2f64); 5799 SDValue Op0 = Op.getOperand(0); 5800 SDValue Op1 = Op.getOperand(1); 5801 if (Op0.getOpcode() != ISD::UNDEF) 5802 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 5803 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 5804 DAG.getIntPtrConstant(0, dl)); 5805 if (Op1.getOpcode() != ISD::UNDEF) 5806 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 5807 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 5808 DAG.getIntPtrConstant(1, dl)); 5809 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 5810 } 5811 5812 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 5813 /// element has been zero/sign-extended, depending on the isSigned parameter, 5814 /// from an integer type half its size. 5815 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 5816 bool isSigned) { 5817 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 5818 EVT VT = N->getValueType(0); 5819 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 5820 SDNode *BVN = N->getOperand(0).getNode(); 5821 if (BVN->getValueType(0) != MVT::v4i32 || 5822 BVN->getOpcode() != ISD::BUILD_VECTOR) 5823 return false; 5824 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 5825 unsigned HiElt = 1 - LoElt; 5826 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 5827 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 5828 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 5829 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 5830 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 5831 return false; 5832 if (isSigned) { 5833 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 5834 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 5835 return true; 5836 } else { 5837 if (Hi0->isNullValue() && Hi1->isNullValue()) 5838 return true; 5839 } 5840 return false; 5841 } 5842 5843 if (N->getOpcode() != ISD::BUILD_VECTOR) 5844 return false; 5845 5846 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 5847 SDNode *Elt = N->getOperand(i).getNode(); 5848 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 5849 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5850 unsigned HalfSize = EltSize / 2; 5851 if (isSigned) { 5852 if (!isIntN(HalfSize, C->getSExtValue())) 5853 return false; 5854 } else { 5855 if (!isUIntN(HalfSize, C->getZExtValue())) 5856 return false; 5857 } 5858 continue; 5859 } 5860 return false; 5861 } 5862 5863 return true; 5864 } 5865 5866 /// isSignExtended - Check if a node is a vector value that is sign-extended 5867 /// or a constant BUILD_VECTOR with sign-extended elements. 5868 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 5869 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 5870 return true; 5871 if (isExtendedBUILD_VECTOR(N, DAG, true)) 5872 return true; 5873 return false; 5874 } 5875 5876 /// isZeroExtended - Check if a node is a vector value that is zero-extended 5877 /// or a constant BUILD_VECTOR with zero-extended elements. 5878 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 5879 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 5880 return true; 5881 if (isExtendedBUILD_VECTOR(N, DAG, false)) 5882 return true; 5883 return false; 5884 } 5885 5886 static EVT getExtensionTo64Bits(const EVT &OrigVT) { 5887 if (OrigVT.getSizeInBits() >= 64) 5888 return OrigVT; 5889 5890 assert(OrigVT.isSimple() && "Expecting a simple value type"); 5891 5892 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy; 5893 switch (OrigSimpleTy) { 5894 default: llvm_unreachable("Unexpected Vector Type"); 5895 case MVT::v2i8: 5896 case MVT::v2i16: 5897 return MVT::v2i32; 5898 case MVT::v4i8: 5899 return MVT::v4i16; 5900 } 5901 } 5902 5903 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total 5904 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL. 5905 /// We insert the required extension here to get the vector to fill a D register. 5906 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG, 5907 const EVT &OrigTy, 5908 const EVT &ExtTy, 5909 unsigned ExtOpcode) { 5910 // The vector originally had a size of OrigTy. It was then extended to ExtTy. 5911 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than 5912 // 64-bits we need to insert a new extension so that it will be 64-bits. 5913 assert(ExtTy.is128BitVector() && "Unexpected extension size"); 5914 if (OrigTy.getSizeInBits() >= 64) 5915 return N; 5916 5917 // Must extend size to at least 64 bits to be used as an operand for VMULL. 5918 EVT NewVT = getExtensionTo64Bits(OrigTy); 5919 5920 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N); 5921 } 5922 5923 /// SkipLoadExtensionForVMULL - return a load of the original vector size that 5924 /// does not do any sign/zero extension. If the original vector is less 5925 /// than 64 bits, an appropriate extension will be added after the load to 5926 /// reach a total size of 64 bits. We have to add the extension separately 5927 /// because ARM does not have a sign/zero extending load for vectors. 5928 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) { 5929 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT()); 5930 5931 // The load already has the right type. 5932 if (ExtendedTy == LD->getMemoryVT()) 5933 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(), 5934 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(), 5935 LD->isNonTemporal(), LD->isInvariant(), 5936 LD->getAlignment()); 5937 5938 // We need to create a zextload/sextload. We cannot just create a load 5939 // followed by a zext/zext node because LowerMUL is also run during normal 5940 // operation legalization where we can't create illegal types. 5941 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy, 5942 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(), 5943 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(), 5944 LD->isNonTemporal(), LD->getAlignment()); 5945 } 5946 5947 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND, 5948 /// extending load, or BUILD_VECTOR with extended elements, return the 5949 /// unextended value. The unextended vector should be 64 bits so that it can 5950 /// be used as an operand to a VMULL instruction. If the original vector size 5951 /// before extension is less than 64 bits we add a an extension to resize 5952 /// the vector to 64 bits. 5953 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) { 5954 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 5955 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG, 5956 N->getOperand(0)->getValueType(0), 5957 N->getValueType(0), 5958 N->getOpcode()); 5959 5960 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) 5961 return SkipLoadExtensionForVMULL(LD, DAG); 5962 5963 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 5964 // have been legalized as a BITCAST from v4i32. 5965 if (N->getOpcode() == ISD::BITCAST) { 5966 SDNode *BVN = N->getOperand(0).getNode(); 5967 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 5968 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 5969 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0; 5970 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32, 5971 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2)); 5972 } 5973 // Construct a new BUILD_VECTOR with elements truncated to half the size. 5974 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 5975 EVT VT = N->getValueType(0); 5976 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2; 5977 unsigned NumElts = VT.getVectorNumElements(); 5978 MVT TruncVT = MVT::getIntegerVT(EltSize); 5979 SmallVector<SDValue, 8> Ops; 5980 SDLoc dl(N); 5981 for (unsigned i = 0; i != NumElts; ++i) { 5982 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 5983 const APInt &CInt = C->getAPIntValue(); 5984 // Element types smaller than 32 bits are not legal, so use i32 elements. 5985 // The values are implicitly truncated so sext vs. zext doesn't matter. 5986 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32)); 5987 } 5988 return DAG.getNode(ISD::BUILD_VECTOR, dl, 5989 MVT::getVectorVT(TruncVT, NumElts), Ops); 5990 } 5991 5992 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 5993 unsigned Opcode = N->getOpcode(); 5994 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 5995 SDNode *N0 = N->getOperand(0).getNode(); 5996 SDNode *N1 = N->getOperand(1).getNode(); 5997 return N0->hasOneUse() && N1->hasOneUse() && 5998 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 5999 } 6000 return false; 6001 } 6002 6003 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 6004 unsigned Opcode = N->getOpcode(); 6005 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 6006 SDNode *N0 = N->getOperand(0).getNode(); 6007 SDNode *N1 = N->getOperand(1).getNode(); 6008 return N0->hasOneUse() && N1->hasOneUse() && 6009 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 6010 } 6011 return false; 6012 } 6013 6014 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 6015 // Multiplications are only custom-lowered for 128-bit vectors so that 6016 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 6017 EVT VT = Op.getValueType(); 6018 assert(VT.is128BitVector() && VT.isInteger() && 6019 "unexpected type for custom-lowering ISD::MUL"); 6020 SDNode *N0 = Op.getOperand(0).getNode(); 6021 SDNode *N1 = Op.getOperand(1).getNode(); 6022 unsigned NewOpc = 0; 6023 bool isMLA = false; 6024 bool isN0SExt = isSignExtended(N0, DAG); 6025 bool isN1SExt = isSignExtended(N1, DAG); 6026 if (isN0SExt && isN1SExt) 6027 NewOpc = ARMISD::VMULLs; 6028 else { 6029 bool isN0ZExt = isZeroExtended(N0, DAG); 6030 bool isN1ZExt = isZeroExtended(N1, DAG); 6031 if (isN0ZExt && isN1ZExt) 6032 NewOpc = ARMISD::VMULLu; 6033 else if (isN1SExt || isN1ZExt) { 6034 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 6035 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 6036 if (isN1SExt && isAddSubSExt(N0, DAG)) { 6037 NewOpc = ARMISD::VMULLs; 6038 isMLA = true; 6039 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 6040 NewOpc = ARMISD::VMULLu; 6041 isMLA = true; 6042 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 6043 std::swap(N0, N1); 6044 NewOpc = ARMISD::VMULLu; 6045 isMLA = true; 6046 } 6047 } 6048 6049 if (!NewOpc) { 6050 if (VT == MVT::v2i64) 6051 // Fall through to expand this. It is not legal. 6052 return SDValue(); 6053 else 6054 // Other vector multiplications are legal. 6055 return Op; 6056 } 6057 } 6058 6059 // Legalize to a VMULL instruction. 6060 SDLoc DL(Op); 6061 SDValue Op0; 6062 SDValue Op1 = SkipExtensionForVMULL(N1, DAG); 6063 if (!isMLA) { 6064 Op0 = SkipExtensionForVMULL(N0, DAG); 6065 assert(Op0.getValueType().is64BitVector() && 6066 Op1.getValueType().is64BitVector() && 6067 "unexpected types for extended operands to VMULL"); 6068 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 6069 } 6070 6071 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 6072 // isel lowering to take advantage of no-stall back to back vmul + vmla. 6073 // vmull q0, d4, d6 6074 // vmlal q0, d5, d6 6075 // is faster than 6076 // vaddl q0, d4, d5 6077 // vmovl q1, d6 6078 // vmul q0, q0, q1 6079 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG); 6080 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG); 6081 EVT Op1VT = Op1.getValueType(); 6082 return DAG.getNode(N0->getOpcode(), DL, VT, 6083 DAG.getNode(NewOpc, DL, VT, 6084 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 6085 DAG.getNode(NewOpc, DL, VT, 6086 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 6087 } 6088 6089 static SDValue 6090 LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) { 6091 // Convert to float 6092 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 6093 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 6094 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 6095 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 6096 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 6097 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 6098 // Get reciprocal estimate. 6099 // float4 recip = vrecpeq_f32(yf); 6100 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6101 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 6102 Y); 6103 // Because char has a smaller range than uchar, we can actually get away 6104 // without any newton steps. This requires that we use a weird bias 6105 // of 0xb000, however (again, this has been exhaustively tested). 6106 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 6107 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 6108 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 6109 Y = DAG.getConstant(0xb000, dl, MVT::i32); 6110 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y); 6111 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 6112 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 6113 // Convert back to short. 6114 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 6115 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 6116 return X; 6117 } 6118 6119 static SDValue 6120 LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) { 6121 SDValue N2; 6122 // Convert to float. 6123 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 6124 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 6125 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 6126 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 6127 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 6128 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 6129 6130 // Use reciprocal estimate and one refinement step. 6131 // float4 recip = vrecpeq_f32(yf); 6132 // recip *= vrecpsq_f32(yf, recip); 6133 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6134 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 6135 N1); 6136 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6137 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 6138 N1, N2); 6139 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6140 // Because short has a smaller range than ushort, we can actually get away 6141 // with only a single newton step. This requires that we use a weird bias 6142 // of 89, however (again, this has been exhaustively tested). 6143 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 6144 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 6145 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 6146 N1 = DAG.getConstant(0x89, dl, MVT::i32); 6147 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 6148 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 6149 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 6150 // Convert back to integer and return. 6151 // return vmovn_s32(vcvt_s32_f32(result)); 6152 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 6153 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 6154 return N0; 6155 } 6156 6157 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) { 6158 EVT VT = Op.getValueType(); 6159 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 6160 "unexpected type for custom-lowering ISD::SDIV"); 6161 6162 SDLoc dl(Op); 6163 SDValue N0 = Op.getOperand(0); 6164 SDValue N1 = Op.getOperand(1); 6165 SDValue N2, N3; 6166 6167 if (VT == MVT::v8i8) { 6168 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 6169 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 6170 6171 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6172 DAG.getIntPtrConstant(4, dl)); 6173 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6174 DAG.getIntPtrConstant(4, dl)); 6175 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6176 DAG.getIntPtrConstant(0, dl)); 6177 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6178 DAG.getIntPtrConstant(0, dl)); 6179 6180 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 6181 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 6182 6183 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 6184 N0 = LowerCONCAT_VECTORS(N0, DAG); 6185 6186 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 6187 return N0; 6188 } 6189 return LowerSDIV_v4i16(N0, N1, dl, DAG); 6190 } 6191 6192 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) { 6193 EVT VT = Op.getValueType(); 6194 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 6195 "unexpected type for custom-lowering ISD::UDIV"); 6196 6197 SDLoc dl(Op); 6198 SDValue N0 = Op.getOperand(0); 6199 SDValue N1 = Op.getOperand(1); 6200 SDValue N2, N3; 6201 6202 if (VT == MVT::v8i8) { 6203 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 6204 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 6205 6206 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6207 DAG.getIntPtrConstant(4, dl)); 6208 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6209 DAG.getIntPtrConstant(4, dl)); 6210 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6211 DAG.getIntPtrConstant(0, dl)); 6212 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6213 DAG.getIntPtrConstant(0, dl)); 6214 6215 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 6216 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 6217 6218 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 6219 N0 = LowerCONCAT_VECTORS(N0, DAG); 6220 6221 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 6222 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl, 6223 MVT::i32), 6224 N0); 6225 return N0; 6226 } 6227 6228 // v4i16 sdiv ... Convert to float. 6229 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 6230 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 6231 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 6232 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 6233 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 6234 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 6235 6236 // Use reciprocal estimate and two refinement steps. 6237 // float4 recip = vrecpeq_f32(yf); 6238 // recip *= vrecpsq_f32(yf, recip); 6239 // recip *= vrecpsq_f32(yf, recip); 6240 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6241 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 6242 BN1); 6243 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6244 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 6245 BN1, N2); 6246 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6247 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6248 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 6249 BN1, N2); 6250 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6251 // Simply multiplying by the reciprocal estimate can leave us a few ulps 6252 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 6253 // and that it will never cause us to return an answer too large). 6254 // float4 result = as_float4(as_int4(xf*recip) + 2); 6255 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 6256 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 6257 N1 = DAG.getConstant(2, dl, MVT::i32); 6258 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1); 6259 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 6260 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 6261 // Convert back to integer and return. 6262 // return vmovn_u32(vcvt_s32_f32(result)); 6263 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 6264 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 6265 return N0; 6266 } 6267 6268 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { 6269 EVT VT = Op.getNode()->getValueType(0); 6270 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 6271 6272 unsigned Opc; 6273 bool ExtraOp = false; 6274 switch (Op.getOpcode()) { 6275 default: llvm_unreachable("Invalid code"); 6276 case ISD::ADDC: Opc = ARMISD::ADDC; break; 6277 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break; 6278 case ISD::SUBC: Opc = ARMISD::SUBC; break; 6279 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break; 6280 } 6281 6282 if (!ExtraOp) 6283 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 6284 Op.getOperand(1)); 6285 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 6286 Op.getOperand(1), Op.getOperand(2)); 6287 } 6288 6289 SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const { 6290 assert(Subtarget->isTargetDarwin()); 6291 6292 // For iOS, we want to call an alternative entry point: __sincos_stret, 6293 // return values are passed via sret. 6294 SDLoc dl(Op); 6295 SDValue Arg = Op.getOperand(0); 6296 EVT ArgVT = Arg.getValueType(); 6297 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 6298 6299 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 6300 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 6301 6302 // Pair of floats / doubles used to pass the result. 6303 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr); 6304 6305 // Create stack object for sret. 6306 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy); 6307 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy); 6308 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false); 6309 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); 6310 6311 ArgListTy Args; 6312 ArgListEntry Entry; 6313 6314 Entry.Node = SRet; 6315 Entry.Ty = RetTy->getPointerTo(); 6316 Entry.isSExt = false; 6317 Entry.isZExt = false; 6318 Entry.isSRet = true; 6319 Args.push_back(Entry); 6320 6321 Entry.Node = Arg; 6322 Entry.Ty = ArgTy; 6323 Entry.isSExt = false; 6324 Entry.isZExt = false; 6325 Args.push_back(Entry); 6326 6327 const char *LibcallName = (ArgVT == MVT::f64) 6328 ? "__sincos_stret" : "__sincosf_stret"; 6329 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy()); 6330 6331 TargetLowering::CallLoweringInfo CLI(DAG); 6332 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode()) 6333 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee, 6334 std::move(Args), 0) 6335 .setDiscardResult(); 6336 6337 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 6338 6339 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet, 6340 MachinePointerInfo(), false, false, false, 0); 6341 6342 // Address of cos field. 6343 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet, 6344 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl)); 6345 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, 6346 MachinePointerInfo(), false, false, false, 0); 6347 6348 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT); 6349 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, 6350 LoadSin.getValue(0), LoadCos.getValue(0)); 6351 } 6352 6353 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 6354 // Monotonic load/store is legal for all targets 6355 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic) 6356 return Op; 6357 6358 // Acquire/Release load/store is not legal for targets without a 6359 // dmb or equivalent available. 6360 return SDValue(); 6361 } 6362 6363 static void ReplaceREADCYCLECOUNTER(SDNode *N, 6364 SmallVectorImpl<SDValue> &Results, 6365 SelectionDAG &DAG, 6366 const ARMSubtarget *Subtarget) { 6367 SDLoc DL(N); 6368 SDValue Cycles32, OutChain; 6369 6370 if (Subtarget->hasPerfMon()) { 6371 // Under Power Management extensions, the cycle-count is: 6372 // mrc p15, #0, <Rt>, c9, c13, #0 6373 SDValue Ops[] = { N->getOperand(0), // Chain 6374 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32), 6375 DAG.getConstant(15, DL, MVT::i32), 6376 DAG.getConstant(0, DL, MVT::i32), 6377 DAG.getConstant(9, DL, MVT::i32), 6378 DAG.getConstant(13, DL, MVT::i32), 6379 DAG.getConstant(0, DL, MVT::i32) 6380 }; 6381 6382 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 6383 DAG.getVTList(MVT::i32, MVT::Other), Ops); 6384 OutChain = Cycles32.getValue(1); 6385 } else { 6386 // Intrinsic is defined to return 0 on unsupported platforms. Technically 6387 // there are older ARM CPUs that have implementation-specific ways of 6388 // obtaining this information (FIXME!). 6389 Cycles32 = DAG.getConstant(0, DL, MVT::i32); 6390 OutChain = DAG.getEntryNode(); 6391 } 6392 6393 6394 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, 6395 Cycles32, DAG.getConstant(0, DL, MVT::i32)); 6396 Results.push_back(Cycles64); 6397 Results.push_back(OutChain); 6398 } 6399 6400 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 6401 switch (Op.getOpcode()) { 6402 default: llvm_unreachable("Don't know how to custom lower this!"); 6403 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG); 6404 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 6405 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 6406 case ISD::GlobalAddress: 6407 switch (Subtarget->getTargetTriple().getObjectFormat()) { 6408 default: llvm_unreachable("unknown object format"); 6409 case Triple::COFF: 6410 return LowerGlobalAddressWindows(Op, DAG); 6411 case Triple::ELF: 6412 return LowerGlobalAddressELF(Op, DAG); 6413 case Triple::MachO: 6414 return LowerGlobalAddressDarwin(Op, DAG); 6415 } 6416 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 6417 case ISD::SELECT: return LowerSELECT(Op, DAG); 6418 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 6419 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 6420 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 6421 case ISD::VASTART: return LowerVASTART(Op, DAG); 6422 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 6423 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 6424 case ISD::SINT_TO_FP: 6425 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 6426 case ISD::FP_TO_SINT: 6427 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 6428 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 6429 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 6430 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 6431 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG); 6432 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 6433 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 6434 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 6435 Subtarget); 6436 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG); 6437 case ISD::SHL: 6438 case ISD::SRL: 6439 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 6440 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 6441 case ISD::SRL_PARTS: 6442 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 6443 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 6444 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget); 6445 case ISD::SETCC: return LowerVSETCC(Op, DAG); 6446 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget); 6447 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 6448 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 6449 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 6450 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 6451 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 6452 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 6453 case ISD::MUL: return LowerMUL(Op, DAG); 6454 case ISD::SDIV: return LowerSDIV(Op, DAG); 6455 case ISD::UDIV: return LowerUDIV(Op, DAG); 6456 case ISD::ADDC: 6457 case ISD::ADDE: 6458 case ISD::SUBC: 6459 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); 6460 case ISD::SADDO: 6461 case ISD::UADDO: 6462 case ISD::SSUBO: 6463 case ISD::USUBO: 6464 return LowerXALUO(Op, DAG); 6465 case ISD::ATOMIC_LOAD: 6466 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 6467 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG); 6468 case ISD::SDIVREM: 6469 case ISD::UDIVREM: return LowerDivRem(Op, DAG); 6470 case ISD::DYNAMIC_STACKALLOC: 6471 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment()) 6472 return LowerDYNAMIC_STACKALLOC(Op, DAG); 6473 llvm_unreachable("Don't know how to custom lower this!"); 6474 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG); 6475 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG); 6476 } 6477 } 6478 6479 /// ReplaceNodeResults - Replace the results of node with an illegal result 6480 /// type with new values built out of custom code. 6481 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 6482 SmallVectorImpl<SDValue>&Results, 6483 SelectionDAG &DAG) const { 6484 SDValue Res; 6485 switch (N->getOpcode()) { 6486 default: 6487 llvm_unreachable("Don't know how to custom expand this!"); 6488 case ISD::READ_REGISTER: 6489 ExpandREAD_REGISTER(N, Results, DAG); 6490 break; 6491 case ISD::BITCAST: 6492 Res = ExpandBITCAST(N, DAG); 6493 break; 6494 case ISD::SRL: 6495 case ISD::SRA: 6496 Res = Expand64BitShift(N, DAG, Subtarget); 6497 break; 6498 case ISD::READCYCLECOUNTER: 6499 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget); 6500 return; 6501 } 6502 if (Res.getNode()) 6503 Results.push_back(Res); 6504 } 6505 6506 //===----------------------------------------------------------------------===// 6507 // ARM Scheduler Hooks 6508 //===----------------------------------------------------------------------===// 6509 6510 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 6511 /// registers the function context. 6512 void ARMTargetLowering:: 6513 SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB, 6514 MachineBasicBlock *DispatchBB, int FI) const { 6515 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 6516 DebugLoc dl = MI->getDebugLoc(); 6517 MachineFunction *MF = MBB->getParent(); 6518 MachineRegisterInfo *MRI = &MF->getRegInfo(); 6519 MachineConstantPool *MCP = MF->getConstantPool(); 6520 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 6521 const Function *F = MF->getFunction(); 6522 6523 bool isThumb = Subtarget->isThumb(); 6524 bool isThumb2 = Subtarget->isThumb2(); 6525 6526 unsigned PCLabelId = AFI->createPICLabelUId(); 6527 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 6528 ARMConstantPoolValue *CPV = 6529 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj); 6530 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 6531 6532 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass 6533 : &ARM::GPRRegClass; 6534 6535 // Grab constant pool and fixed stack memory operands. 6536 MachineMemOperand *CPMMO = 6537 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(), 6538 MachineMemOperand::MOLoad, 4, 4); 6539 6540 MachineMemOperand *FIMMOSt = 6541 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 6542 MachineMemOperand::MOStore, 4, 4); 6543 6544 // Load the address of the dispatch MBB into the jump buffer. 6545 if (isThumb2) { 6546 // Incoming value: jbuf 6547 // ldr.n r5, LCPI1_1 6548 // orr r5, r5, #1 6549 // add r5, pc 6550 // str r5, [$jbuf, #+4] ; &jbuf[1] 6551 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6552 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 6553 .addConstantPoolIndex(CPI) 6554 .addMemOperand(CPMMO)); 6555 // Set the low bit because of thumb mode. 6556 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6557 AddDefaultCC( 6558 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 6559 .addReg(NewVReg1, RegState::Kill) 6560 .addImm(0x01))); 6561 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6562 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 6563 .addReg(NewVReg2, RegState::Kill) 6564 .addImm(PCLabelId); 6565 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 6566 .addReg(NewVReg3, RegState::Kill) 6567 .addFrameIndex(FI) 6568 .addImm(36) // &jbuf[1] :: pc 6569 .addMemOperand(FIMMOSt)); 6570 } else if (isThumb) { 6571 // Incoming value: jbuf 6572 // ldr.n r1, LCPI1_4 6573 // add r1, pc 6574 // mov r2, #1 6575 // orrs r1, r2 6576 // add r2, $jbuf, #+4 ; &jbuf[1] 6577 // str r1, [r2] 6578 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6579 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 6580 .addConstantPoolIndex(CPI) 6581 .addMemOperand(CPMMO)); 6582 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6583 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 6584 .addReg(NewVReg1, RegState::Kill) 6585 .addImm(PCLabelId); 6586 // Set the low bit because of thumb mode. 6587 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6588 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 6589 .addReg(ARM::CPSR, RegState::Define) 6590 .addImm(1)); 6591 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6592 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 6593 .addReg(ARM::CPSR, RegState::Define) 6594 .addReg(NewVReg2, RegState::Kill) 6595 .addReg(NewVReg3, RegState::Kill)); 6596 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6597 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5) 6598 .addFrameIndex(FI) 6599 .addImm(36); // &jbuf[1] :: pc 6600 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 6601 .addReg(NewVReg4, RegState::Kill) 6602 .addReg(NewVReg5, RegState::Kill) 6603 .addImm(0) 6604 .addMemOperand(FIMMOSt)); 6605 } else { 6606 // Incoming value: jbuf 6607 // ldr r1, LCPI1_1 6608 // add r1, pc, r1 6609 // str r1, [$jbuf, #+4] ; &jbuf[1] 6610 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6611 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 6612 .addConstantPoolIndex(CPI) 6613 .addImm(0) 6614 .addMemOperand(CPMMO)); 6615 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6616 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 6617 .addReg(NewVReg1, RegState::Kill) 6618 .addImm(PCLabelId)); 6619 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 6620 .addReg(NewVReg2, RegState::Kill) 6621 .addFrameIndex(FI) 6622 .addImm(36) // &jbuf[1] :: pc 6623 .addMemOperand(FIMMOSt)); 6624 } 6625 } 6626 6627 void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI, 6628 MachineBasicBlock *MBB) const { 6629 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 6630 DebugLoc dl = MI->getDebugLoc(); 6631 MachineFunction *MF = MBB->getParent(); 6632 MachineRegisterInfo *MRI = &MF->getRegInfo(); 6633 MachineFrameInfo *MFI = MF->getFrameInfo(); 6634 int FI = MFI->getFunctionContextIndex(); 6635 6636 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass 6637 : &ARM::GPRnopcRegClass; 6638 6639 // Get a mapping of the call site numbers to all of the landing pads they're 6640 // associated with. 6641 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad; 6642 unsigned MaxCSNum = 0; 6643 MachineModuleInfo &MMI = MF->getMMI(); 6644 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; 6645 ++BB) { 6646 if (!BB->isLandingPad()) continue; 6647 6648 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 6649 // pad. 6650 for (MachineBasicBlock::iterator 6651 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 6652 if (!II->isEHLabel()) continue; 6653 6654 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 6655 if (!MMI.hasCallSiteLandingPad(Sym)) continue; 6656 6657 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym); 6658 for (SmallVectorImpl<unsigned>::iterator 6659 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 6660 CSI != CSE; ++CSI) { 6661 CallSiteNumToLPad[*CSI].push_back(BB); 6662 MaxCSNum = std::max(MaxCSNum, *CSI); 6663 } 6664 break; 6665 } 6666 } 6667 6668 // Get an ordered list of the machine basic blocks for the jump table. 6669 std::vector<MachineBasicBlock*> LPadList; 6670 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs; 6671 LPadList.reserve(CallSiteNumToLPad.size()); 6672 for (unsigned I = 1; I <= MaxCSNum; ++I) { 6673 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 6674 for (SmallVectorImpl<MachineBasicBlock*>::iterator 6675 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 6676 LPadList.push_back(*II); 6677 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 6678 } 6679 } 6680 6681 assert(!LPadList.empty() && 6682 "No landing pad destinations for the dispatch jump table!"); 6683 6684 // Create the jump table and associated information. 6685 MachineJumpTableInfo *JTI = 6686 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 6687 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 6688 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 6689 6690 // Create the MBBs for the dispatch code. 6691 6692 // Shove the dispatch's address into the return slot in the function context. 6693 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 6694 DispatchBB->setIsLandingPad(); 6695 6696 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 6697 unsigned trap_opcode; 6698 if (Subtarget->isThumb()) 6699 trap_opcode = ARM::tTRAP; 6700 else 6701 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP; 6702 6703 BuildMI(TrapBB, dl, TII->get(trap_opcode)); 6704 DispatchBB->addSuccessor(TrapBB); 6705 6706 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 6707 DispatchBB->addSuccessor(DispContBB); 6708 6709 // Insert and MBBs. 6710 MF->insert(MF->end(), DispatchBB); 6711 MF->insert(MF->end(), DispContBB); 6712 MF->insert(MF->end(), TrapBB); 6713 6714 // Insert code into the entry block that creates and registers the function 6715 // context. 6716 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 6717 6718 MachineMemOperand *FIMMOLd = 6719 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 6720 MachineMemOperand::MOLoad | 6721 MachineMemOperand::MOVolatile, 4, 4); 6722 6723 MachineInstrBuilder MIB; 6724 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup)); 6725 6726 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 6727 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 6728 6729 // Add a register mask with no preserved registers. This results in all 6730 // registers being marked as clobbered. 6731 MIB.addRegMask(RI.getNoPreservedMask()); 6732 6733 unsigned NumLPads = LPadList.size(); 6734 if (Subtarget->isThumb2()) { 6735 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6736 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 6737 .addFrameIndex(FI) 6738 .addImm(4) 6739 .addMemOperand(FIMMOLd)); 6740 6741 if (NumLPads < 256) { 6742 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 6743 .addReg(NewVReg1) 6744 .addImm(LPadList.size())); 6745 } else { 6746 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6747 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 6748 .addImm(NumLPads & 0xFFFF)); 6749 6750 unsigned VReg2 = VReg1; 6751 if ((NumLPads & 0xFFFF0000) != 0) { 6752 VReg2 = MRI->createVirtualRegister(TRC); 6753 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 6754 .addReg(VReg1) 6755 .addImm(NumLPads >> 16)); 6756 } 6757 6758 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 6759 .addReg(NewVReg1) 6760 .addReg(VReg2)); 6761 } 6762 6763 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 6764 .addMBB(TrapBB) 6765 .addImm(ARMCC::HI) 6766 .addReg(ARM::CPSR); 6767 6768 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6769 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3) 6770 .addJumpTableIndex(MJTI)); 6771 6772 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6773 AddDefaultCC( 6774 AddDefaultPred( 6775 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 6776 .addReg(NewVReg3, RegState::Kill) 6777 .addReg(NewVReg1) 6778 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 6779 6780 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 6781 .addReg(NewVReg4, RegState::Kill) 6782 .addReg(NewVReg1) 6783 .addJumpTableIndex(MJTI); 6784 } else if (Subtarget->isThumb()) { 6785 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6786 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 6787 .addFrameIndex(FI) 6788 .addImm(1) 6789 .addMemOperand(FIMMOLd)); 6790 6791 if (NumLPads < 256) { 6792 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 6793 .addReg(NewVReg1) 6794 .addImm(NumLPads)); 6795 } else { 6796 MachineConstantPool *ConstantPool = MF->getConstantPool(); 6797 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 6798 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 6799 6800 // MachineConstantPool wants an explicit alignment. 6801 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 6802 if (Align == 0) 6803 Align = getDataLayout()->getTypeAllocSize(C->getType()); 6804 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 6805 6806 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6807 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 6808 .addReg(VReg1, RegState::Define) 6809 .addConstantPoolIndex(Idx)); 6810 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 6811 .addReg(NewVReg1) 6812 .addReg(VReg1)); 6813 } 6814 6815 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 6816 .addMBB(TrapBB) 6817 .addImm(ARMCC::HI) 6818 .addReg(ARM::CPSR); 6819 6820 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 6821 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 6822 .addReg(ARM::CPSR, RegState::Define) 6823 .addReg(NewVReg1) 6824 .addImm(2)); 6825 6826 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6827 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 6828 .addJumpTableIndex(MJTI)); 6829 6830 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6831 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 6832 .addReg(ARM::CPSR, RegState::Define) 6833 .addReg(NewVReg2, RegState::Kill) 6834 .addReg(NewVReg3)); 6835 6836 MachineMemOperand *JTMMOLd = 6837 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 6838 MachineMemOperand::MOLoad, 4, 4); 6839 6840 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6841 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 6842 .addReg(NewVReg4, RegState::Kill) 6843 .addImm(0) 6844 .addMemOperand(JTMMOLd)); 6845 6846 unsigned NewVReg6 = NewVReg5; 6847 if (RelocM == Reloc::PIC_) { 6848 NewVReg6 = MRI->createVirtualRegister(TRC); 6849 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 6850 .addReg(ARM::CPSR, RegState::Define) 6851 .addReg(NewVReg5, RegState::Kill) 6852 .addReg(NewVReg3)); 6853 } 6854 6855 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 6856 .addReg(NewVReg6, RegState::Kill) 6857 .addJumpTableIndex(MJTI); 6858 } else { 6859 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 6860 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 6861 .addFrameIndex(FI) 6862 .addImm(4) 6863 .addMemOperand(FIMMOLd)); 6864 6865 if (NumLPads < 256) { 6866 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 6867 .addReg(NewVReg1) 6868 .addImm(NumLPads)); 6869 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 6870 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6871 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 6872 .addImm(NumLPads & 0xFFFF)); 6873 6874 unsigned VReg2 = VReg1; 6875 if ((NumLPads & 0xFFFF0000) != 0) { 6876 VReg2 = MRI->createVirtualRegister(TRC); 6877 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 6878 .addReg(VReg1) 6879 .addImm(NumLPads >> 16)); 6880 } 6881 6882 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 6883 .addReg(NewVReg1) 6884 .addReg(VReg2)); 6885 } else { 6886 MachineConstantPool *ConstantPool = MF->getConstantPool(); 6887 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 6888 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 6889 6890 // MachineConstantPool wants an explicit alignment. 6891 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 6892 if (Align == 0) 6893 Align = getDataLayout()->getTypeAllocSize(C->getType()); 6894 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 6895 6896 unsigned VReg1 = MRI->createVirtualRegister(TRC); 6897 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 6898 .addReg(VReg1, RegState::Define) 6899 .addConstantPoolIndex(Idx) 6900 .addImm(0)); 6901 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 6902 .addReg(NewVReg1) 6903 .addReg(VReg1, RegState::Kill)); 6904 } 6905 6906 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 6907 .addMBB(TrapBB) 6908 .addImm(ARMCC::HI) 6909 .addReg(ARM::CPSR); 6910 6911 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 6912 AddDefaultCC( 6913 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 6914 .addReg(NewVReg1) 6915 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 6916 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 6917 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 6918 .addJumpTableIndex(MJTI)); 6919 6920 MachineMemOperand *JTMMOLd = 6921 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(), 6922 MachineMemOperand::MOLoad, 4, 4); 6923 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 6924 AddDefaultPred( 6925 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 6926 .addReg(NewVReg3, RegState::Kill) 6927 .addReg(NewVReg4) 6928 .addImm(0) 6929 .addMemOperand(JTMMOLd)); 6930 6931 if (RelocM == Reloc::PIC_) { 6932 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 6933 .addReg(NewVReg5, RegState::Kill) 6934 .addReg(NewVReg4) 6935 .addJumpTableIndex(MJTI); 6936 } else { 6937 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr)) 6938 .addReg(NewVReg5, RegState::Kill) 6939 .addJumpTableIndex(MJTI); 6940 } 6941 } 6942 6943 // Add the jump table entries as successors to the MBB. 6944 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs; 6945 for (std::vector<MachineBasicBlock*>::iterator 6946 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 6947 MachineBasicBlock *CurMBB = *I; 6948 if (SeenMBBs.insert(CurMBB).second) 6949 DispContBB->addSuccessor(CurMBB); 6950 } 6951 6952 // N.B. the order the invoke BBs are processed in doesn't matter here. 6953 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF); 6954 SmallVector<MachineBasicBlock*, 64> MBBLPads; 6955 for (MachineBasicBlock *BB : InvokeBBs) { 6956 6957 // Remove the landing pad successor from the invoke block and replace it 6958 // with the new dispatch block. 6959 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 6960 BB->succ_end()); 6961 while (!Successors.empty()) { 6962 MachineBasicBlock *SMBB = Successors.pop_back_val(); 6963 if (SMBB->isLandingPad()) { 6964 BB->removeSuccessor(SMBB); 6965 MBBLPads.push_back(SMBB); 6966 } 6967 } 6968 6969 BB->addSuccessor(DispatchBB); 6970 6971 // Find the invoke call and mark all of the callee-saved registers as 6972 // 'implicit defined' so that they're spilled. This prevents code from 6973 // moving instructions to before the EH block, where they will never be 6974 // executed. 6975 for (MachineBasicBlock::reverse_iterator 6976 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 6977 if (!II->isCall()) continue; 6978 6979 DenseMap<unsigned, bool> DefRegs; 6980 for (MachineInstr::mop_iterator 6981 OI = II->operands_begin(), OE = II->operands_end(); 6982 OI != OE; ++OI) { 6983 if (!OI->isReg()) continue; 6984 DefRegs[OI->getReg()] = true; 6985 } 6986 6987 MachineInstrBuilder MIB(*MF, &*II); 6988 6989 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 6990 unsigned Reg = SavedRegs[i]; 6991 if (Subtarget->isThumb2() && 6992 !ARM::tGPRRegClass.contains(Reg) && 6993 !ARM::hGPRRegClass.contains(Reg)) 6994 continue; 6995 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg)) 6996 continue; 6997 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg)) 6998 continue; 6999 if (!DefRegs[Reg]) 7000 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 7001 } 7002 7003 break; 7004 } 7005 } 7006 7007 // Mark all former landing pads as non-landing pads. The dispatch is the only 7008 // landing pad now. 7009 for (SmallVectorImpl<MachineBasicBlock*>::iterator 7010 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 7011 (*I)->setIsLandingPad(false); 7012 7013 // The instruction is gone now. 7014 MI->eraseFromParent(); 7015 } 7016 7017 static 7018 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 7019 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 7020 E = MBB->succ_end(); I != E; ++I) 7021 if (*I != Succ) 7022 return *I; 7023 llvm_unreachable("Expecting a BB with two successors!"); 7024 } 7025 7026 /// Return the load opcode for a given load size. If load size >= 8, 7027 /// neon opcode will be returned. 7028 static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) { 7029 if (LdSize >= 8) 7030 return LdSize == 16 ? ARM::VLD1q32wb_fixed 7031 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0; 7032 if (IsThumb1) 7033 return LdSize == 4 ? ARM::tLDRi 7034 : LdSize == 2 ? ARM::tLDRHi 7035 : LdSize == 1 ? ARM::tLDRBi : 0; 7036 if (IsThumb2) 7037 return LdSize == 4 ? ARM::t2LDR_POST 7038 : LdSize == 2 ? ARM::t2LDRH_POST 7039 : LdSize == 1 ? ARM::t2LDRB_POST : 0; 7040 return LdSize == 4 ? ARM::LDR_POST_IMM 7041 : LdSize == 2 ? ARM::LDRH_POST 7042 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0; 7043 } 7044 7045 /// Return the store opcode for a given store size. If store size >= 8, 7046 /// neon opcode will be returned. 7047 static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) { 7048 if (StSize >= 8) 7049 return StSize == 16 ? ARM::VST1q32wb_fixed 7050 : StSize == 8 ? ARM::VST1d32wb_fixed : 0; 7051 if (IsThumb1) 7052 return StSize == 4 ? ARM::tSTRi 7053 : StSize == 2 ? ARM::tSTRHi 7054 : StSize == 1 ? ARM::tSTRBi : 0; 7055 if (IsThumb2) 7056 return StSize == 4 ? ARM::t2STR_POST 7057 : StSize == 2 ? ARM::t2STRH_POST 7058 : StSize == 1 ? ARM::t2STRB_POST : 0; 7059 return StSize == 4 ? ARM::STR_POST_IMM 7060 : StSize == 2 ? ARM::STRH_POST 7061 : StSize == 1 ? ARM::STRB_POST_IMM : 0; 7062 } 7063 7064 /// Emit a post-increment load operation with given size. The instructions 7065 /// will be added to BB at Pos. 7066 static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos, 7067 const TargetInstrInfo *TII, DebugLoc dl, 7068 unsigned LdSize, unsigned Data, unsigned AddrIn, 7069 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 7070 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2); 7071 assert(LdOpc != 0 && "Should have a load opcode"); 7072 if (LdSize >= 8) { 7073 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7074 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7075 .addImm(0)); 7076 } else if (IsThumb1) { 7077 // load + update AddrIn 7078 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7079 .addReg(AddrIn).addImm(0)); 7080 MachineInstrBuilder MIB = 7081 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut); 7082 MIB = AddDefaultT1CC(MIB); 7083 MIB.addReg(AddrIn).addImm(LdSize); 7084 AddDefaultPred(MIB); 7085 } else if (IsThumb2) { 7086 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7087 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7088 .addImm(LdSize)); 7089 } else { // arm 7090 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7091 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7092 .addReg(0).addImm(LdSize)); 7093 } 7094 } 7095 7096 /// Emit a post-increment store operation with given size. The instructions 7097 /// will be added to BB at Pos. 7098 static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos, 7099 const TargetInstrInfo *TII, DebugLoc dl, 7100 unsigned StSize, unsigned Data, unsigned AddrIn, 7101 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 7102 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2); 7103 assert(StOpc != 0 && "Should have a store opcode"); 7104 if (StSize >= 8) { 7105 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7106 .addReg(AddrIn).addImm(0).addReg(Data)); 7107 } else if (IsThumb1) { 7108 // store + update AddrIn 7109 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data) 7110 .addReg(AddrIn).addImm(0)); 7111 MachineInstrBuilder MIB = 7112 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut); 7113 MIB = AddDefaultT1CC(MIB); 7114 MIB.addReg(AddrIn).addImm(StSize); 7115 AddDefaultPred(MIB); 7116 } else if (IsThumb2) { 7117 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7118 .addReg(Data).addReg(AddrIn).addImm(StSize)); 7119 } else { // arm 7120 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7121 .addReg(Data).addReg(AddrIn).addReg(0) 7122 .addImm(StSize)); 7123 } 7124 } 7125 7126 MachineBasicBlock * 7127 ARMTargetLowering::EmitStructByval(MachineInstr *MI, 7128 MachineBasicBlock *BB) const { 7129 // This pseudo instruction has 3 operands: dst, src, size 7130 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold(). 7131 // Otherwise, we will generate unrolled scalar copies. 7132 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 7133 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7134 MachineFunction::iterator It = BB; 7135 ++It; 7136 7137 unsigned dest = MI->getOperand(0).getReg(); 7138 unsigned src = MI->getOperand(1).getReg(); 7139 unsigned SizeVal = MI->getOperand(2).getImm(); 7140 unsigned Align = MI->getOperand(3).getImm(); 7141 DebugLoc dl = MI->getDebugLoc(); 7142 7143 MachineFunction *MF = BB->getParent(); 7144 MachineRegisterInfo &MRI = MF->getRegInfo(); 7145 unsigned UnitSize = 0; 7146 const TargetRegisterClass *TRC = nullptr; 7147 const TargetRegisterClass *VecTRC = nullptr; 7148 7149 bool IsThumb1 = Subtarget->isThumb1Only(); 7150 bool IsThumb2 = Subtarget->isThumb2(); 7151 7152 if (Align & 1) { 7153 UnitSize = 1; 7154 } else if (Align & 2) { 7155 UnitSize = 2; 7156 } else { 7157 // Check whether we can use NEON instructions. 7158 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) && 7159 Subtarget->hasNEON()) { 7160 if ((Align % 16 == 0) && SizeVal >= 16) 7161 UnitSize = 16; 7162 else if ((Align % 8 == 0) && SizeVal >= 8) 7163 UnitSize = 8; 7164 } 7165 // Can't use NEON instructions. 7166 if (UnitSize == 0) 7167 UnitSize = 4; 7168 } 7169 7170 // Select the correct opcode and register class for unit size load/store 7171 bool IsNeon = UnitSize >= 8; 7172 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 7173 if (IsNeon) 7174 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass 7175 : UnitSize == 8 ? &ARM::DPRRegClass 7176 : nullptr; 7177 7178 unsigned BytesLeft = SizeVal % UnitSize; 7179 unsigned LoopSize = SizeVal - BytesLeft; 7180 7181 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) { 7182 // Use LDR and STR to copy. 7183 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize) 7184 // [destOut] = STR_POST(scratch, destIn, UnitSize) 7185 unsigned srcIn = src; 7186 unsigned destIn = dest; 7187 for (unsigned i = 0; i < LoopSize; i+=UnitSize) { 7188 unsigned srcOut = MRI.createVirtualRegister(TRC); 7189 unsigned destOut = MRI.createVirtualRegister(TRC); 7190 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 7191 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut, 7192 IsThumb1, IsThumb2); 7193 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut, 7194 IsThumb1, IsThumb2); 7195 srcIn = srcOut; 7196 destIn = destOut; 7197 } 7198 7199 // Handle the leftover bytes with LDRB and STRB. 7200 // [scratch, srcOut] = LDRB_POST(srcIn, 1) 7201 // [destOut] = STRB_POST(scratch, destIn, 1) 7202 for (unsigned i = 0; i < BytesLeft; i++) { 7203 unsigned srcOut = MRI.createVirtualRegister(TRC); 7204 unsigned destOut = MRI.createVirtualRegister(TRC); 7205 unsigned scratch = MRI.createVirtualRegister(TRC); 7206 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut, 7207 IsThumb1, IsThumb2); 7208 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut, 7209 IsThumb1, IsThumb2); 7210 srcIn = srcOut; 7211 destIn = destOut; 7212 } 7213 MI->eraseFromParent(); // The instruction is gone now. 7214 return BB; 7215 } 7216 7217 // Expand the pseudo op to a loop. 7218 // thisMBB: 7219 // ... 7220 // movw varEnd, # --> with thumb2 7221 // movt varEnd, # 7222 // ldrcp varEnd, idx --> without thumb2 7223 // fallthrough --> loopMBB 7224 // loopMBB: 7225 // PHI varPhi, varEnd, varLoop 7226 // PHI srcPhi, src, srcLoop 7227 // PHI destPhi, dst, destLoop 7228 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 7229 // [destLoop] = STR_POST(scratch, destPhi, UnitSize) 7230 // subs varLoop, varPhi, #UnitSize 7231 // bne loopMBB 7232 // fallthrough --> exitMBB 7233 // exitMBB: 7234 // epilogue to handle left-over bytes 7235 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 7236 // [destOut] = STRB_POST(scratch, destLoop, 1) 7237 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 7238 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 7239 MF->insert(It, loopMBB); 7240 MF->insert(It, exitMBB); 7241 7242 // Transfer the remainder of BB and its successor edges to exitMBB. 7243 exitMBB->splice(exitMBB->begin(), BB, 7244 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7245 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 7246 7247 // Load an immediate to varEnd. 7248 unsigned varEnd = MRI.createVirtualRegister(TRC); 7249 if (Subtarget->useMovt(*MF)) { 7250 unsigned Vtmp = varEnd; 7251 if ((LoopSize & 0xFFFF0000) != 0) 7252 Vtmp = MRI.createVirtualRegister(TRC); 7253 AddDefaultPred(BuildMI(BB, dl, 7254 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16), 7255 Vtmp).addImm(LoopSize & 0xFFFF)); 7256 7257 if ((LoopSize & 0xFFFF0000) != 0) 7258 AddDefaultPred(BuildMI(BB, dl, 7259 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16), 7260 varEnd) 7261 .addReg(Vtmp) 7262 .addImm(LoopSize >> 16)); 7263 } else { 7264 MachineConstantPool *ConstantPool = MF->getConstantPool(); 7265 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 7266 const Constant *C = ConstantInt::get(Int32Ty, LoopSize); 7267 7268 // MachineConstantPool wants an explicit alignment. 7269 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty); 7270 if (Align == 0) 7271 Align = getDataLayout()->getTypeAllocSize(C->getType()); 7272 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 7273 7274 if (IsThumb1) 7275 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg( 7276 varEnd, RegState::Define).addConstantPoolIndex(Idx)); 7277 else 7278 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg( 7279 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0)); 7280 } 7281 BB->addSuccessor(loopMBB); 7282 7283 // Generate the loop body: 7284 // varPhi = PHI(varLoop, varEnd) 7285 // srcPhi = PHI(srcLoop, src) 7286 // destPhi = PHI(destLoop, dst) 7287 MachineBasicBlock *entryBB = BB; 7288 BB = loopMBB; 7289 unsigned varLoop = MRI.createVirtualRegister(TRC); 7290 unsigned varPhi = MRI.createVirtualRegister(TRC); 7291 unsigned srcLoop = MRI.createVirtualRegister(TRC); 7292 unsigned srcPhi = MRI.createVirtualRegister(TRC); 7293 unsigned destLoop = MRI.createVirtualRegister(TRC); 7294 unsigned destPhi = MRI.createVirtualRegister(TRC); 7295 7296 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi) 7297 .addReg(varLoop).addMBB(loopMBB) 7298 .addReg(varEnd).addMBB(entryBB); 7299 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi) 7300 .addReg(srcLoop).addMBB(loopMBB) 7301 .addReg(src).addMBB(entryBB); 7302 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi) 7303 .addReg(destLoop).addMBB(loopMBB) 7304 .addReg(dest).addMBB(entryBB); 7305 7306 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 7307 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz) 7308 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 7309 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop, 7310 IsThumb1, IsThumb2); 7311 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop, 7312 IsThumb1, IsThumb2); 7313 7314 // Decrement loop variable by UnitSize. 7315 if (IsThumb1) { 7316 MachineInstrBuilder MIB = 7317 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop); 7318 MIB = AddDefaultT1CC(MIB); 7319 MIB.addReg(varPhi).addImm(UnitSize); 7320 AddDefaultPred(MIB); 7321 } else { 7322 MachineInstrBuilder MIB = 7323 BuildMI(*BB, BB->end(), dl, 7324 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop); 7325 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize))); 7326 MIB->getOperand(5).setReg(ARM::CPSR); 7327 MIB->getOperand(5).setIsDef(true); 7328 } 7329 BuildMI(*BB, BB->end(), dl, 7330 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc)) 7331 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 7332 7333 // loopMBB can loop back to loopMBB or fall through to exitMBB. 7334 BB->addSuccessor(loopMBB); 7335 BB->addSuccessor(exitMBB); 7336 7337 // Add epilogue to handle BytesLeft. 7338 BB = exitMBB; 7339 MachineInstr *StartOfExit = exitMBB->begin(); 7340 7341 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 7342 // [destOut] = STRB_POST(scratch, destLoop, 1) 7343 unsigned srcIn = srcLoop; 7344 unsigned destIn = destLoop; 7345 for (unsigned i = 0; i < BytesLeft; i++) { 7346 unsigned srcOut = MRI.createVirtualRegister(TRC); 7347 unsigned destOut = MRI.createVirtualRegister(TRC); 7348 unsigned scratch = MRI.createVirtualRegister(TRC); 7349 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut, 7350 IsThumb1, IsThumb2); 7351 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut, 7352 IsThumb1, IsThumb2); 7353 srcIn = srcOut; 7354 destIn = destOut; 7355 } 7356 7357 MI->eraseFromParent(); // The instruction is gone now. 7358 return BB; 7359 } 7360 7361 MachineBasicBlock * 7362 ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI, 7363 MachineBasicBlock *MBB) const { 7364 const TargetMachine &TM = getTargetMachine(); 7365 const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); 7366 DebugLoc DL = MI->getDebugLoc(); 7367 7368 assert(Subtarget->isTargetWindows() && 7369 "__chkstk is only supported on Windows"); 7370 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode"); 7371 7372 // __chkstk takes the number of words to allocate on the stack in R4, and 7373 // returns the stack adjustment in number of bytes in R4. This will not 7374 // clober any other registers (other than the obvious lr). 7375 // 7376 // Although, technically, IP should be considered a register which may be 7377 // clobbered, the call itself will not touch it. Windows on ARM is a pure 7378 // thumb-2 environment, so there is no interworking required. As a result, we 7379 // do not expect a veneer to be emitted by the linker, clobbering IP. 7380 // 7381 // Each module receives its own copy of __chkstk, so no import thunk is 7382 // required, again, ensuring that IP is not clobbered. 7383 // 7384 // Finally, although some linkers may theoretically provide a trampoline for 7385 // out of range calls (which is quite common due to a 32M range limitation of 7386 // branches for Thumb), we can generate the long-call version via 7387 // -mcmodel=large, alleviating the need for the trampoline which may clobber 7388 // IP. 7389 7390 switch (TM.getCodeModel()) { 7391 case CodeModel::Small: 7392 case CodeModel::Medium: 7393 case CodeModel::Default: 7394 case CodeModel::Kernel: 7395 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL)) 7396 .addImm((unsigned)ARMCC::AL).addReg(0) 7397 .addExternalSymbol("__chkstk") 7398 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 7399 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 7400 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead); 7401 break; 7402 case CodeModel::Large: 7403 case CodeModel::JITDefault: { 7404 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 7405 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass); 7406 7407 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg) 7408 .addExternalSymbol("__chkstk"); 7409 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr)) 7410 .addImm((unsigned)ARMCC::AL).addReg(0) 7411 .addReg(Reg, RegState::Kill) 7412 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 7413 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 7414 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead); 7415 break; 7416 } 7417 } 7418 7419 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr), 7420 ARM::SP) 7421 .addReg(ARM::SP).addReg(ARM::R4))); 7422 7423 MI->eraseFromParent(); 7424 return MBB; 7425 } 7426 7427 MachineBasicBlock * 7428 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 7429 MachineBasicBlock *BB) const { 7430 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 7431 DebugLoc dl = MI->getDebugLoc(); 7432 bool isThumb2 = Subtarget->isThumb2(); 7433 switch (MI->getOpcode()) { 7434 default: { 7435 MI->dump(); 7436 llvm_unreachable("Unexpected instr type to insert"); 7437 } 7438 // The Thumb2 pre-indexed stores have the same MI operands, they just 7439 // define them differently in the .td files from the isel patterns, so 7440 // they need pseudos. 7441 case ARM::t2STR_preidx: 7442 MI->setDesc(TII->get(ARM::t2STR_PRE)); 7443 return BB; 7444 case ARM::t2STRB_preidx: 7445 MI->setDesc(TII->get(ARM::t2STRB_PRE)); 7446 return BB; 7447 case ARM::t2STRH_preidx: 7448 MI->setDesc(TII->get(ARM::t2STRH_PRE)); 7449 return BB; 7450 7451 case ARM::STRi_preidx: 7452 case ARM::STRBi_preidx: { 7453 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ? 7454 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM; 7455 // Decode the offset. 7456 unsigned Offset = MI->getOperand(4).getImm(); 7457 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 7458 Offset = ARM_AM::getAM2Offset(Offset); 7459 if (isSub) 7460 Offset = -Offset; 7461 7462 MachineMemOperand *MMO = *MI->memoperands_begin(); 7463 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 7464 .addOperand(MI->getOperand(0)) // Rn_wb 7465 .addOperand(MI->getOperand(1)) // Rt 7466 .addOperand(MI->getOperand(2)) // Rn 7467 .addImm(Offset) // offset (skip GPR==zero_reg) 7468 .addOperand(MI->getOperand(5)) // pred 7469 .addOperand(MI->getOperand(6)) 7470 .addMemOperand(MMO); 7471 MI->eraseFromParent(); 7472 return BB; 7473 } 7474 case ARM::STRr_preidx: 7475 case ARM::STRBr_preidx: 7476 case ARM::STRH_preidx: { 7477 unsigned NewOpc; 7478 switch (MI->getOpcode()) { 7479 default: llvm_unreachable("unexpected opcode!"); 7480 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 7481 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 7482 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 7483 } 7484 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 7485 for (unsigned i = 0; i < MI->getNumOperands(); ++i) 7486 MIB.addOperand(MI->getOperand(i)); 7487 MI->eraseFromParent(); 7488 return BB; 7489 } 7490 7491 case ARM::tMOVCCr_pseudo: { 7492 // To "insert" a SELECT_CC instruction, we actually have to insert the 7493 // diamond control-flow pattern. The incoming instruction knows the 7494 // destination vreg to set, the condition code register to branch on, the 7495 // true/false values to select between, and a branch opcode to use. 7496 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7497 MachineFunction::iterator It = BB; 7498 ++It; 7499 7500 // thisMBB: 7501 // ... 7502 // TrueVal = ... 7503 // cmpTY ccX, r1, r2 7504 // bCC copy1MBB 7505 // fallthrough --> copy0MBB 7506 MachineBasicBlock *thisMBB = BB; 7507 MachineFunction *F = BB->getParent(); 7508 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 7509 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 7510 F->insert(It, copy0MBB); 7511 F->insert(It, sinkMBB); 7512 7513 // Transfer the remainder of BB and its successor edges to sinkMBB. 7514 sinkMBB->splice(sinkMBB->begin(), BB, 7515 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7516 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 7517 7518 BB->addSuccessor(copy0MBB); 7519 BB->addSuccessor(sinkMBB); 7520 7521 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB) 7522 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg()); 7523 7524 // copy0MBB: 7525 // %FalseValue = ... 7526 // # fallthrough to sinkMBB 7527 BB = copy0MBB; 7528 7529 // Update machine-CFG edges 7530 BB->addSuccessor(sinkMBB); 7531 7532 // sinkMBB: 7533 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 7534 // ... 7535 BB = sinkMBB; 7536 BuildMI(*BB, BB->begin(), dl, 7537 TII->get(ARM::PHI), MI->getOperand(0).getReg()) 7538 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) 7539 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 7540 7541 MI->eraseFromParent(); // The pseudo instruction is gone now. 7542 return BB; 7543 } 7544 7545 case ARM::BCCi64: 7546 case ARM::BCCZi64: { 7547 // If there is an unconditional branch to the other successor, remove it. 7548 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7549 7550 // Compare both parts that make up the double comparison separately for 7551 // equality. 7552 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64; 7553 7554 unsigned LHS1 = MI->getOperand(1).getReg(); 7555 unsigned LHS2 = MI->getOperand(2).getReg(); 7556 if (RHSisZero) { 7557 AddDefaultPred(BuildMI(BB, dl, 7558 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7559 .addReg(LHS1).addImm(0)); 7560 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7561 .addReg(LHS2).addImm(0) 7562 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 7563 } else { 7564 unsigned RHS1 = MI->getOperand(3).getReg(); 7565 unsigned RHS2 = MI->getOperand(4).getReg(); 7566 AddDefaultPred(BuildMI(BB, dl, 7567 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 7568 .addReg(LHS1).addReg(RHS1)); 7569 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 7570 .addReg(LHS2).addReg(RHS2) 7571 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 7572 } 7573 7574 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB(); 7575 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 7576 if (MI->getOperand(0).getImm() == ARMCC::NE) 7577 std::swap(destMBB, exitMBB); 7578 7579 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 7580 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 7581 if (isThumb2) 7582 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB)); 7583 else 7584 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 7585 7586 MI->eraseFromParent(); // The pseudo instruction is gone now. 7587 return BB; 7588 } 7589 7590 case ARM::Int_eh_sjlj_setjmp: 7591 case ARM::Int_eh_sjlj_setjmp_nofp: 7592 case ARM::tInt_eh_sjlj_setjmp: 7593 case ARM::t2Int_eh_sjlj_setjmp: 7594 case ARM::t2Int_eh_sjlj_setjmp_nofp: 7595 EmitSjLjDispatchBlock(MI, BB); 7596 return BB; 7597 7598 case ARM::ABS: 7599 case ARM::t2ABS: { 7600 // To insert an ABS instruction, we have to insert the 7601 // diamond control-flow pattern. The incoming instruction knows the 7602 // source vreg to test against 0, the destination vreg to set, 7603 // the condition code register to branch on, the 7604 // true/false values to select between, and a branch opcode to use. 7605 // It transforms 7606 // V1 = ABS V0 7607 // into 7608 // V2 = MOVS V0 7609 // BCC (branch to SinkBB if V0 >= 0) 7610 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 7611 // SinkBB: V1 = PHI(V2, V3) 7612 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7613 MachineFunction::iterator BBI = BB; 7614 ++BBI; 7615 MachineFunction *Fn = BB->getParent(); 7616 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 7617 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 7618 Fn->insert(BBI, RSBBB); 7619 Fn->insert(BBI, SinkBB); 7620 7621 unsigned int ABSSrcReg = MI->getOperand(1).getReg(); 7622 unsigned int ABSDstReg = MI->getOperand(0).getReg(); 7623 bool ABSSrcKIll = MI->getOperand(1).isKill(); 7624 bool isThumb2 = Subtarget->isThumb2(); 7625 MachineRegisterInfo &MRI = Fn->getRegInfo(); 7626 // In Thumb mode S must not be specified if source register is the SP or 7627 // PC and if destination register is the SP, so restrict register class 7628 unsigned NewRsbDstReg = 7629 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass); 7630 7631 // Transfer the remainder of BB and its successor edges to sinkMBB. 7632 SinkBB->splice(SinkBB->begin(), BB, 7633 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7634 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 7635 7636 BB->addSuccessor(RSBBB); 7637 BB->addSuccessor(SinkBB); 7638 7639 // fall through to SinkMBB 7640 RSBBB->addSuccessor(SinkBB); 7641 7642 // insert a cmp at the end of BB 7643 AddDefaultPred(BuildMI(BB, dl, 7644 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 7645 .addReg(ABSSrcReg).addImm(0)); 7646 7647 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 7648 BuildMI(BB, dl, 7649 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 7650 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 7651 7652 // insert rsbri in RSBBB 7653 // Note: BCC and rsbri will be converted into predicated rsbmi 7654 // by if-conversion pass 7655 BuildMI(*RSBBB, RSBBB->begin(), dl, 7656 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 7657 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0) 7658 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 7659 7660 // insert PHI in SinkBB, 7661 // reuse ABSDstReg to not change uses of ABS instruction 7662 BuildMI(*SinkBB, SinkBB->begin(), dl, 7663 TII->get(ARM::PHI), ABSDstReg) 7664 .addReg(NewRsbDstReg).addMBB(RSBBB) 7665 .addReg(ABSSrcReg).addMBB(BB); 7666 7667 // remove ABS instruction 7668 MI->eraseFromParent(); 7669 7670 // return last added BB 7671 return SinkBB; 7672 } 7673 case ARM::COPY_STRUCT_BYVAL_I32: 7674 ++NumLoopByVals; 7675 return EmitStructByval(MI, BB); 7676 case ARM::WIN__CHKSTK: 7677 return EmitLowered__chkstk(MI, BB); 7678 } 7679 } 7680 7681 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, 7682 SDNode *Node) const { 7683 const MCInstrDesc *MCID = &MI->getDesc(); 7684 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 7685 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 7686 // operand is still set to noreg. If needed, set the optional operand's 7687 // register to CPSR, and remove the redundant implicit def. 7688 // 7689 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>). 7690 7691 // Rename pseudo opcodes. 7692 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode()); 7693 if (NewOpc) { 7694 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo(); 7695 MCID = &TII->get(NewOpc); 7696 7697 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 && 7698 "converted opcode should be the same except for cc_out"); 7699 7700 MI->setDesc(*MCID); 7701 7702 // Add the optional cc_out operand 7703 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 7704 } 7705 unsigned ccOutIdx = MCID->getNumOperands() - 1; 7706 7707 // Any ARM instruction that sets the 's' bit should specify an optional 7708 // "cc_out" operand in the last operand position. 7709 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 7710 assert(!NewOpc && "Optional cc_out operand required"); 7711 return; 7712 } 7713 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 7714 // since we already have an optional CPSR def. 7715 bool definesCPSR = false; 7716 bool deadCPSR = false; 7717 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands(); 7718 i != e; ++i) { 7719 const MachineOperand &MO = MI->getOperand(i); 7720 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 7721 definesCPSR = true; 7722 if (MO.isDead()) 7723 deadCPSR = true; 7724 MI->RemoveOperand(i); 7725 break; 7726 } 7727 } 7728 if (!definesCPSR) { 7729 assert(!NewOpc && "Optional cc_out operand required"); 7730 return; 7731 } 7732 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 7733 if (deadCPSR) { 7734 assert(!MI->getOperand(ccOutIdx).getReg() && 7735 "expect uninitialized optional cc_out operand"); 7736 return; 7737 } 7738 7739 // If this instruction was defined with an optional CPSR def and its dag node 7740 // had a live implicit CPSR def, then activate the optional CPSR def. 7741 MachineOperand &MO = MI->getOperand(ccOutIdx); 7742 MO.setReg(ARM::CPSR); 7743 MO.setIsDef(true); 7744 } 7745 7746 //===----------------------------------------------------------------------===// 7747 // ARM Optimization Hooks 7748 //===----------------------------------------------------------------------===// 7749 7750 // Helper function that checks if N is a null or all ones constant. 7751 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) { 7752 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N); 7753 if (!C) 7754 return false; 7755 return AllOnes ? C->isAllOnesValue() : C->isNullValue(); 7756 } 7757 7758 // Return true if N is conditionally 0 or all ones. 7759 // Detects these expressions where cc is an i1 value: 7760 // 7761 // (select cc 0, y) [AllOnes=0] 7762 // (select cc y, 0) [AllOnes=0] 7763 // (zext cc) [AllOnes=0] 7764 // (sext cc) [AllOnes=0/1] 7765 // (select cc -1, y) [AllOnes=1] 7766 // (select cc y, -1) [AllOnes=1] 7767 // 7768 // Invert is set when N is the null/all ones constant when CC is false. 7769 // OtherOp is set to the alternative value of N. 7770 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, 7771 SDValue &CC, bool &Invert, 7772 SDValue &OtherOp, 7773 SelectionDAG &DAG) { 7774 switch (N->getOpcode()) { 7775 default: return false; 7776 case ISD::SELECT: { 7777 CC = N->getOperand(0); 7778 SDValue N1 = N->getOperand(1); 7779 SDValue N2 = N->getOperand(2); 7780 if (isZeroOrAllOnes(N1, AllOnes)) { 7781 Invert = false; 7782 OtherOp = N2; 7783 return true; 7784 } 7785 if (isZeroOrAllOnes(N2, AllOnes)) { 7786 Invert = true; 7787 OtherOp = N1; 7788 return true; 7789 } 7790 return false; 7791 } 7792 case ISD::ZERO_EXTEND: 7793 // (zext cc) can never be the all ones value. 7794 if (AllOnes) 7795 return false; 7796 // Fall through. 7797 case ISD::SIGN_EXTEND: { 7798 SDLoc dl(N); 7799 EVT VT = N->getValueType(0); 7800 CC = N->getOperand(0); 7801 if (CC.getValueType() != MVT::i1) 7802 return false; 7803 Invert = !AllOnes; 7804 if (AllOnes) 7805 // When looking for an AllOnes constant, N is an sext, and the 'other' 7806 // value is 0. 7807 OtherOp = DAG.getConstant(0, dl, VT); 7808 else if (N->getOpcode() == ISD::ZERO_EXTEND) 7809 // When looking for a 0 constant, N can be zext or sext. 7810 OtherOp = DAG.getConstant(1, dl, VT); 7811 else 7812 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, 7813 VT); 7814 return true; 7815 } 7816 } 7817 } 7818 7819 // Combine a constant select operand into its use: 7820 // 7821 // (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 7822 // (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 7823 // (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1] 7824 // (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 7825 // (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 7826 // 7827 // The transform is rejected if the select doesn't have a constant operand that 7828 // is null, or all ones when AllOnes is set. 7829 // 7830 // Also recognize sext/zext from i1: 7831 // 7832 // (add (zext cc), x) -> (select cc (add x, 1), x) 7833 // (add (sext cc), x) -> (select cc (add x, -1), x) 7834 // 7835 // These transformations eventually create predicated instructions. 7836 // 7837 // @param N The node to transform. 7838 // @param Slct The N operand that is a select. 7839 // @param OtherOp The other N operand (x above). 7840 // @param DCI Context. 7841 // @param AllOnes Require the select constant to be all ones instead of null. 7842 // @returns The new node, or SDValue() on failure. 7843 static 7844 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 7845 TargetLowering::DAGCombinerInfo &DCI, 7846 bool AllOnes = false) { 7847 SelectionDAG &DAG = DCI.DAG; 7848 EVT VT = N->getValueType(0); 7849 SDValue NonConstantVal; 7850 SDValue CCOp; 7851 bool SwapSelectOps; 7852 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps, 7853 NonConstantVal, DAG)) 7854 return SDValue(); 7855 7856 // Slct is now know to be the desired identity constant when CC is true. 7857 SDValue TrueVal = OtherOp; 7858 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 7859 OtherOp, NonConstantVal); 7860 // Unless SwapSelectOps says CC should be false. 7861 if (SwapSelectOps) 7862 std::swap(TrueVal, FalseVal); 7863 7864 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, 7865 CCOp, TrueVal, FalseVal); 7866 } 7867 7868 // Attempt combineSelectAndUse on each operand of a commutative operator N. 7869 static 7870 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes, 7871 TargetLowering::DAGCombinerInfo &DCI) { 7872 SDValue N0 = N->getOperand(0); 7873 SDValue N1 = N->getOperand(1); 7874 if (N0.getNode()->hasOneUse()) { 7875 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes); 7876 if (Result.getNode()) 7877 return Result; 7878 } 7879 if (N1.getNode()->hasOneUse()) { 7880 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes); 7881 if (Result.getNode()) 7882 return Result; 7883 } 7884 return SDValue(); 7885 } 7886 7887 // AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction 7888 // (only after legalization). 7889 static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1, 7890 TargetLowering::DAGCombinerInfo &DCI, 7891 const ARMSubtarget *Subtarget) { 7892 7893 // Only perform optimization if after legalize, and if NEON is available. We 7894 // also expected both operands to be BUILD_VECTORs. 7895 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 7896 || N0.getOpcode() != ISD::BUILD_VECTOR 7897 || N1.getOpcode() != ISD::BUILD_VECTOR) 7898 return SDValue(); 7899 7900 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 7901 EVT VT = N->getValueType(0); 7902 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 7903 return SDValue(); 7904 7905 // Check that the vector operands are of the right form. 7906 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 7907 // operands, where N is the size of the formed vector. 7908 // Each EXTRACT_VECTOR should have the same input vector and odd or even 7909 // index such that we have a pair wise add pattern. 7910 7911 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 7912 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 7913 return SDValue(); 7914 SDValue Vec = N0->getOperand(0)->getOperand(0); 7915 SDNode *V = Vec.getNode(); 7916 unsigned nextIndex = 0; 7917 7918 // For each operands to the ADD which are BUILD_VECTORs, 7919 // check to see if each of their operands are an EXTRACT_VECTOR with 7920 // the same vector and appropriate index. 7921 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 7922 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 7923 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 7924 7925 SDValue ExtVec0 = N0->getOperand(i); 7926 SDValue ExtVec1 = N1->getOperand(i); 7927 7928 // First operand is the vector, verify its the same. 7929 if (V != ExtVec0->getOperand(0).getNode() || 7930 V != ExtVec1->getOperand(0).getNode()) 7931 return SDValue(); 7932 7933 // Second is the constant, verify its correct. 7934 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 7935 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 7936 7937 // For the constant, we want to see all the even or all the odd. 7938 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 7939 || C1->getZExtValue() != nextIndex+1) 7940 return SDValue(); 7941 7942 // Increment index. 7943 nextIndex+=2; 7944 } else 7945 return SDValue(); 7946 } 7947 7948 // Create VPADDL node. 7949 SelectionDAG &DAG = DCI.DAG; 7950 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7951 7952 SDLoc dl(N); 7953 7954 // Build operand list. 7955 SmallVector<SDValue, 8> Ops; 7956 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl, 7957 TLI.getPointerTy())); 7958 7959 // Input is the vector. 7960 Ops.push_back(Vec); 7961 7962 // Get widened type and narrowed type. 7963 MVT widenType; 7964 unsigned numElem = VT.getVectorNumElements(); 7965 7966 EVT inputLaneType = Vec.getValueType().getVectorElementType(); 7967 switch (inputLaneType.getSimpleVT().SimpleTy) { 7968 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 7969 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 7970 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 7971 default: 7972 llvm_unreachable("Invalid vector element type for padd optimization."); 7973 } 7974 7975 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops); 7976 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE; 7977 return DAG.getNode(ExtOp, dl, VT, tmp); 7978 } 7979 7980 static SDValue findMUL_LOHI(SDValue V) { 7981 if (V->getOpcode() == ISD::UMUL_LOHI || 7982 V->getOpcode() == ISD::SMUL_LOHI) 7983 return V; 7984 return SDValue(); 7985 } 7986 7987 static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode, 7988 TargetLowering::DAGCombinerInfo &DCI, 7989 const ARMSubtarget *Subtarget) { 7990 7991 if (Subtarget->isThumb1Only()) return SDValue(); 7992 7993 // Only perform the checks after legalize when the pattern is available. 7994 if (DCI.isBeforeLegalize()) return SDValue(); 7995 7996 // Look for multiply add opportunities. 7997 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where 7998 // each add nodes consumes a value from ISD::UMUL_LOHI and there is 7999 // a glue link from the first add to the second add. 8000 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by 8001 // a S/UMLAL instruction. 8002 // UMUL_LOHI 8003 // / :lo \ :hi 8004 // / \ [no multiline comment] 8005 // loAdd -> ADDE | 8006 // \ :glue / 8007 // \ / 8008 // ADDC <- hiAdd 8009 // 8010 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC"); 8011 SDValue AddcOp0 = AddcNode->getOperand(0); 8012 SDValue AddcOp1 = AddcNode->getOperand(1); 8013 8014 // Check if the two operands are from the same mul_lohi node. 8015 if (AddcOp0.getNode() == AddcOp1.getNode()) 8016 return SDValue(); 8017 8018 assert(AddcNode->getNumValues() == 2 && 8019 AddcNode->getValueType(0) == MVT::i32 && 8020 "Expect ADDC with two result values. First: i32"); 8021 8022 // Check that we have a glued ADDC node. 8023 if (AddcNode->getValueType(1) != MVT::Glue) 8024 return SDValue(); 8025 8026 // Check that the ADDC adds the low result of the S/UMUL_LOHI. 8027 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI && 8028 AddcOp0->getOpcode() != ISD::SMUL_LOHI && 8029 AddcOp1->getOpcode() != ISD::UMUL_LOHI && 8030 AddcOp1->getOpcode() != ISD::SMUL_LOHI) 8031 return SDValue(); 8032 8033 // Look for the glued ADDE. 8034 SDNode* AddeNode = AddcNode->getGluedUser(); 8035 if (!AddeNode) 8036 return SDValue(); 8037 8038 // Make sure it is really an ADDE. 8039 if (AddeNode->getOpcode() != ISD::ADDE) 8040 return SDValue(); 8041 8042 assert(AddeNode->getNumOperands() == 3 && 8043 AddeNode->getOperand(2).getValueType() == MVT::Glue && 8044 "ADDE node has the wrong inputs"); 8045 8046 // Check for the triangle shape. 8047 SDValue AddeOp0 = AddeNode->getOperand(0); 8048 SDValue AddeOp1 = AddeNode->getOperand(1); 8049 8050 // Make sure that the ADDE operands are not coming from the same node. 8051 if (AddeOp0.getNode() == AddeOp1.getNode()) 8052 return SDValue(); 8053 8054 // Find the MUL_LOHI node walking up ADDE's operands. 8055 bool IsLeftOperandMUL = false; 8056 SDValue MULOp = findMUL_LOHI(AddeOp0); 8057 if (MULOp == SDValue()) 8058 MULOp = findMUL_LOHI(AddeOp1); 8059 else 8060 IsLeftOperandMUL = true; 8061 if (MULOp == SDValue()) 8062 return SDValue(); 8063 8064 // Figure out the right opcode. 8065 unsigned Opc = MULOp->getOpcode(); 8066 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL; 8067 8068 // Figure out the high and low input values to the MLAL node. 8069 SDValue* HiAdd = nullptr; 8070 SDValue* LoMul = nullptr; 8071 SDValue* LowAdd = nullptr; 8072 8073 // Ensure that ADDE is from high result of ISD::SMUL_LOHI. 8074 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1))) 8075 return SDValue(); 8076 8077 if (IsLeftOperandMUL) 8078 HiAdd = &AddeOp1; 8079 else 8080 HiAdd = &AddeOp0; 8081 8082 8083 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node 8084 // whose low result is fed to the ADDC we are checking. 8085 8086 if (AddcOp0 == MULOp.getValue(0)) { 8087 LoMul = &AddcOp0; 8088 LowAdd = &AddcOp1; 8089 } 8090 if (AddcOp1 == MULOp.getValue(0)) { 8091 LoMul = &AddcOp1; 8092 LowAdd = &AddcOp0; 8093 } 8094 8095 if (!LoMul) 8096 return SDValue(); 8097 8098 // Create the merged node. 8099 SelectionDAG &DAG = DCI.DAG; 8100 8101 // Build operand list. 8102 SmallVector<SDValue, 8> Ops; 8103 Ops.push_back(LoMul->getOperand(0)); 8104 Ops.push_back(LoMul->getOperand(1)); 8105 Ops.push_back(*LowAdd); 8106 Ops.push_back(*HiAdd); 8107 8108 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode), 8109 DAG.getVTList(MVT::i32, MVT::i32), Ops); 8110 8111 // Replace the ADDs' nodes uses by the MLA node's values. 8112 SDValue HiMLALResult(MLALNode.getNode(), 1); 8113 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult); 8114 8115 SDValue LoMLALResult(MLALNode.getNode(), 0); 8116 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult); 8117 8118 // Return original node to notify the driver to stop replacing. 8119 SDValue resNode(AddcNode, 0); 8120 return resNode; 8121 } 8122 8123 /// PerformADDCCombine - Target-specific dag combine transform from 8124 /// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL. 8125 static SDValue PerformADDCCombine(SDNode *N, 8126 TargetLowering::DAGCombinerInfo &DCI, 8127 const ARMSubtarget *Subtarget) { 8128 8129 return AddCombineTo64bitMLAL(N, DCI, Subtarget); 8130 8131 } 8132 8133 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 8134 /// operands N0 and N1. This is a helper for PerformADDCombine that is 8135 /// called with the default operands, and if that fails, with commuted 8136 /// operands. 8137 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 8138 TargetLowering::DAGCombinerInfo &DCI, 8139 const ARMSubtarget *Subtarget){ 8140 8141 // Attempt to create vpaddl for this add. 8142 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget); 8143 if (Result.getNode()) 8144 return Result; 8145 8146 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 8147 if (N0.getNode()->hasOneUse()) { 8148 SDValue Result = combineSelectAndUse(N, N0, N1, DCI); 8149 if (Result.getNode()) return Result; 8150 } 8151 return SDValue(); 8152 } 8153 8154 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 8155 /// 8156 static SDValue PerformADDCombine(SDNode *N, 8157 TargetLowering::DAGCombinerInfo &DCI, 8158 const ARMSubtarget *Subtarget) { 8159 SDValue N0 = N->getOperand(0); 8160 SDValue N1 = N->getOperand(1); 8161 8162 // First try with the default operand order. 8163 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget); 8164 if (Result.getNode()) 8165 return Result; 8166 8167 // If that didn't work, try again with the operands commuted. 8168 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 8169 } 8170 8171 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 8172 /// 8173 static SDValue PerformSUBCombine(SDNode *N, 8174 TargetLowering::DAGCombinerInfo &DCI) { 8175 SDValue N0 = N->getOperand(0); 8176 SDValue N1 = N->getOperand(1); 8177 8178 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 8179 if (N1.getNode()->hasOneUse()) { 8180 SDValue Result = combineSelectAndUse(N, N1, N0, DCI); 8181 if (Result.getNode()) return Result; 8182 } 8183 8184 return SDValue(); 8185 } 8186 8187 /// PerformVMULCombine 8188 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 8189 /// special multiplier accumulator forwarding. 8190 /// vmul d3, d0, d2 8191 /// vmla d3, d1, d2 8192 /// is faster than 8193 /// vadd d3, d0, d1 8194 /// vmul d3, d3, d2 8195 // However, for (A + B) * (A + B), 8196 // vadd d2, d0, d1 8197 // vmul d3, d0, d2 8198 // vmla d3, d1, d2 8199 // is slower than 8200 // vadd d2, d0, d1 8201 // vmul d3, d2, d2 8202 static SDValue PerformVMULCombine(SDNode *N, 8203 TargetLowering::DAGCombinerInfo &DCI, 8204 const ARMSubtarget *Subtarget) { 8205 if (!Subtarget->hasVMLxForwarding()) 8206 return SDValue(); 8207 8208 SelectionDAG &DAG = DCI.DAG; 8209 SDValue N0 = N->getOperand(0); 8210 SDValue N1 = N->getOperand(1); 8211 unsigned Opcode = N0.getOpcode(); 8212 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 8213 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 8214 Opcode = N1.getOpcode(); 8215 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 8216 Opcode != ISD::FADD && Opcode != ISD::FSUB) 8217 return SDValue(); 8218 std::swap(N0, N1); 8219 } 8220 8221 if (N0 == N1) 8222 return SDValue(); 8223 8224 EVT VT = N->getValueType(0); 8225 SDLoc DL(N); 8226 SDValue N00 = N0->getOperand(0); 8227 SDValue N01 = N0->getOperand(1); 8228 return DAG.getNode(Opcode, DL, VT, 8229 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 8230 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 8231 } 8232 8233 static SDValue PerformMULCombine(SDNode *N, 8234 TargetLowering::DAGCombinerInfo &DCI, 8235 const ARMSubtarget *Subtarget) { 8236 SelectionDAG &DAG = DCI.DAG; 8237 8238 if (Subtarget->isThumb1Only()) 8239 return SDValue(); 8240 8241 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 8242 return SDValue(); 8243 8244 EVT VT = N->getValueType(0); 8245 if (VT.is64BitVector() || VT.is128BitVector()) 8246 return PerformVMULCombine(N, DCI, Subtarget); 8247 if (VT != MVT::i32) 8248 return SDValue(); 8249 8250 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 8251 if (!C) 8252 return SDValue(); 8253 8254 int64_t MulAmt = C->getSExtValue(); 8255 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt); 8256 8257 ShiftAmt = ShiftAmt & (32 - 1); 8258 SDValue V = N->getOperand(0); 8259 SDLoc DL(N); 8260 8261 SDValue Res; 8262 MulAmt >>= ShiftAmt; 8263 8264 if (MulAmt >= 0) { 8265 if (isPowerOf2_32(MulAmt - 1)) { 8266 // (mul x, 2^N + 1) => (add (shl x, N), x) 8267 Res = DAG.getNode(ISD::ADD, DL, VT, 8268 V, 8269 DAG.getNode(ISD::SHL, DL, VT, 8270 V, 8271 DAG.getConstant(Log2_32(MulAmt - 1), DL, 8272 MVT::i32))); 8273 } else if (isPowerOf2_32(MulAmt + 1)) { 8274 // (mul x, 2^N - 1) => (sub (shl x, N), x) 8275 Res = DAG.getNode(ISD::SUB, DL, VT, 8276 DAG.getNode(ISD::SHL, DL, VT, 8277 V, 8278 DAG.getConstant(Log2_32(MulAmt + 1), DL, 8279 MVT::i32)), 8280 V); 8281 } else 8282 return SDValue(); 8283 } else { 8284 uint64_t MulAmtAbs = -MulAmt; 8285 if (isPowerOf2_32(MulAmtAbs + 1)) { 8286 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 8287 Res = DAG.getNode(ISD::SUB, DL, VT, 8288 V, 8289 DAG.getNode(ISD::SHL, DL, VT, 8290 V, 8291 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL, 8292 MVT::i32))); 8293 } else if (isPowerOf2_32(MulAmtAbs - 1)) { 8294 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 8295 Res = DAG.getNode(ISD::ADD, DL, VT, 8296 V, 8297 DAG.getNode(ISD::SHL, DL, VT, 8298 V, 8299 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL, 8300 MVT::i32))); 8301 Res = DAG.getNode(ISD::SUB, DL, VT, 8302 DAG.getConstant(0, DL, MVT::i32), Res); 8303 8304 } else 8305 return SDValue(); 8306 } 8307 8308 if (ShiftAmt != 0) 8309 Res = DAG.getNode(ISD::SHL, DL, VT, 8310 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32)); 8311 8312 // Do not add new nodes to DAG combiner worklist. 8313 DCI.CombineTo(N, Res, false); 8314 return SDValue(); 8315 } 8316 8317 static SDValue PerformANDCombine(SDNode *N, 8318 TargetLowering::DAGCombinerInfo &DCI, 8319 const ARMSubtarget *Subtarget) { 8320 8321 // Attempt to use immediate-form VBIC 8322 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 8323 SDLoc dl(N); 8324 EVT VT = N->getValueType(0); 8325 SelectionDAG &DAG = DCI.DAG; 8326 8327 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8328 return SDValue(); 8329 8330 APInt SplatBits, SplatUndef; 8331 unsigned SplatBitSize; 8332 bool HasAnyUndefs; 8333 if (BVN && 8334 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 8335 if (SplatBitSize <= 64) { 8336 EVT VbicVT; 8337 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(), 8338 SplatUndef.getZExtValue(), SplatBitSize, 8339 DAG, dl, VbicVT, VT.is128BitVector(), 8340 OtherModImm); 8341 if (Val.getNode()) { 8342 SDValue Input = 8343 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 8344 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 8345 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 8346 } 8347 } 8348 } 8349 8350 if (!Subtarget->isThumb1Only()) { 8351 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) 8352 SDValue Result = combineSelectAndUseCommutative(N, true, DCI); 8353 if (Result.getNode()) 8354 return Result; 8355 } 8356 8357 return SDValue(); 8358 } 8359 8360 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 8361 static SDValue PerformORCombine(SDNode *N, 8362 TargetLowering::DAGCombinerInfo &DCI, 8363 const ARMSubtarget *Subtarget) { 8364 // Attempt to use immediate-form VORR 8365 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 8366 SDLoc dl(N); 8367 EVT VT = N->getValueType(0); 8368 SelectionDAG &DAG = DCI.DAG; 8369 8370 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8371 return SDValue(); 8372 8373 APInt SplatBits, SplatUndef; 8374 unsigned SplatBitSize; 8375 bool HasAnyUndefs; 8376 if (BVN && Subtarget->hasNEON() && 8377 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 8378 if (SplatBitSize <= 64) { 8379 EVT VorrVT; 8380 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 8381 SplatUndef.getZExtValue(), SplatBitSize, 8382 DAG, dl, VorrVT, VT.is128BitVector(), 8383 OtherModImm); 8384 if (Val.getNode()) { 8385 SDValue Input = 8386 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 8387 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 8388 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 8389 } 8390 } 8391 } 8392 8393 if (!Subtarget->isThumb1Only()) { 8394 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 8395 SDValue Result = combineSelectAndUseCommutative(N, false, DCI); 8396 if (Result.getNode()) 8397 return Result; 8398 } 8399 8400 // The code below optimizes (or (and X, Y), Z). 8401 // The AND operand needs to have a single user to make these optimizations 8402 // profitable. 8403 SDValue N0 = N->getOperand(0); 8404 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) 8405 return SDValue(); 8406 SDValue N1 = N->getOperand(1); 8407 8408 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 8409 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 8410 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 8411 APInt SplatUndef; 8412 unsigned SplatBitSize; 8413 bool HasAnyUndefs; 8414 8415 APInt SplatBits0, SplatBits1; 8416 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 8417 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 8418 // Ensure that the second operand of both ands are constants 8419 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 8420 HasAnyUndefs) && !HasAnyUndefs) { 8421 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 8422 HasAnyUndefs) && !HasAnyUndefs) { 8423 // Ensure that the bit width of the constants are the same and that 8424 // the splat arguments are logical inverses as per the pattern we 8425 // are trying to simplify. 8426 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() && 8427 SplatBits0 == ~SplatBits1) { 8428 // Canonicalize the vector type to make instruction selection 8429 // simpler. 8430 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 8431 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 8432 N0->getOperand(1), 8433 N0->getOperand(0), 8434 N1->getOperand(0)); 8435 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 8436 } 8437 } 8438 } 8439 } 8440 8441 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 8442 // reasonable. 8443 8444 // BFI is only available on V6T2+ 8445 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 8446 return SDValue(); 8447 8448 SDLoc DL(N); 8449 // 1) or (and A, mask), val => ARMbfi A, val, mask 8450 // iff (val & mask) == val 8451 // 8452 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 8453 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 8454 // && mask == ~mask2 8455 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 8456 // && ~mask == mask2 8457 // (i.e., copy a bitfield value into another bitfield of the same width) 8458 8459 if (VT != MVT::i32) 8460 return SDValue(); 8461 8462 SDValue N00 = N0.getOperand(0); 8463 8464 // The value and the mask need to be constants so we can verify this is 8465 // actually a bitfield set. If the mask is 0xffff, we can do better 8466 // via a movt instruction, so don't use BFI in that case. 8467 SDValue MaskOp = N0.getOperand(1); 8468 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 8469 if (!MaskC) 8470 return SDValue(); 8471 unsigned Mask = MaskC->getZExtValue(); 8472 if (Mask == 0xffff) 8473 return SDValue(); 8474 SDValue Res; 8475 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 8476 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 8477 if (N1C) { 8478 unsigned Val = N1C->getZExtValue(); 8479 if ((Val & ~Mask) != Val) 8480 return SDValue(); 8481 8482 if (ARM::isBitFieldInvertedMask(Mask)) { 8483 Val >>= countTrailingZeros(~Mask); 8484 8485 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 8486 DAG.getConstant(Val, DL, MVT::i32), 8487 DAG.getConstant(Mask, DL, MVT::i32)); 8488 8489 // Do not add new nodes to DAG combiner worklist. 8490 DCI.CombineTo(N, Res, false); 8491 return SDValue(); 8492 } 8493 } else if (N1.getOpcode() == ISD::AND) { 8494 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 8495 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 8496 if (!N11C) 8497 return SDValue(); 8498 unsigned Mask2 = N11C->getZExtValue(); 8499 8500 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 8501 // as is to match. 8502 if (ARM::isBitFieldInvertedMask(Mask) && 8503 (Mask == ~Mask2)) { 8504 // The pack halfword instruction works better for masks that fit it, 8505 // so use that when it's available. 8506 if (Subtarget->hasT2ExtractPack() && 8507 (Mask == 0xffff || Mask == 0xffff0000)) 8508 return SDValue(); 8509 // 2a 8510 unsigned amt = countTrailingZeros(Mask2); 8511 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 8512 DAG.getConstant(amt, DL, MVT::i32)); 8513 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 8514 DAG.getConstant(Mask, DL, MVT::i32)); 8515 // Do not add new nodes to DAG combiner worklist. 8516 DCI.CombineTo(N, Res, false); 8517 return SDValue(); 8518 } else if (ARM::isBitFieldInvertedMask(~Mask) && 8519 (~Mask == Mask2)) { 8520 // The pack halfword instruction works better for masks that fit it, 8521 // so use that when it's available. 8522 if (Subtarget->hasT2ExtractPack() && 8523 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 8524 return SDValue(); 8525 // 2b 8526 unsigned lsb = countTrailingZeros(Mask); 8527 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 8528 DAG.getConstant(lsb, DL, MVT::i32)); 8529 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 8530 DAG.getConstant(Mask2, DL, MVT::i32)); 8531 // Do not add new nodes to DAG combiner worklist. 8532 DCI.CombineTo(N, Res, false); 8533 return SDValue(); 8534 } 8535 } 8536 8537 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 8538 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 8539 ARM::isBitFieldInvertedMask(~Mask)) { 8540 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 8541 // where lsb(mask) == #shamt and masked bits of B are known zero. 8542 SDValue ShAmt = N00.getOperand(1); 8543 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 8544 unsigned LSB = countTrailingZeros(Mask); 8545 if (ShAmtC != LSB) 8546 return SDValue(); 8547 8548 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 8549 DAG.getConstant(~Mask, DL, MVT::i32)); 8550 8551 // Do not add new nodes to DAG combiner worklist. 8552 DCI.CombineTo(N, Res, false); 8553 } 8554 8555 return SDValue(); 8556 } 8557 8558 static SDValue PerformXORCombine(SDNode *N, 8559 TargetLowering::DAGCombinerInfo &DCI, 8560 const ARMSubtarget *Subtarget) { 8561 EVT VT = N->getValueType(0); 8562 SelectionDAG &DAG = DCI.DAG; 8563 8564 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8565 return SDValue(); 8566 8567 if (!Subtarget->isThumb1Only()) { 8568 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 8569 SDValue Result = combineSelectAndUseCommutative(N, false, DCI); 8570 if (Result.getNode()) 8571 return Result; 8572 } 8573 8574 return SDValue(); 8575 } 8576 8577 /// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 8578 /// the bits being cleared by the AND are not demanded by the BFI. 8579 static SDValue PerformBFICombine(SDNode *N, 8580 TargetLowering::DAGCombinerInfo &DCI) { 8581 SDValue N1 = N->getOperand(1); 8582 if (N1.getOpcode() == ISD::AND) { 8583 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 8584 if (!N11C) 8585 return SDValue(); 8586 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 8587 unsigned LSB = countTrailingZeros(~InvMask); 8588 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB; 8589 assert(Width < 8590 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) && 8591 "undefined behavior"); 8592 unsigned Mask = (1u << Width) - 1; 8593 unsigned Mask2 = N11C->getZExtValue(); 8594 if ((Mask & (~Mask2)) == 0) 8595 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0), 8596 N->getOperand(0), N1.getOperand(0), 8597 N->getOperand(2)); 8598 } 8599 return SDValue(); 8600 } 8601 8602 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 8603 /// ARMISD::VMOVRRD. 8604 static SDValue PerformVMOVRRDCombine(SDNode *N, 8605 TargetLowering::DAGCombinerInfo &DCI, 8606 const ARMSubtarget *Subtarget) { 8607 // vmovrrd(vmovdrr x, y) -> x,y 8608 SDValue InDouble = N->getOperand(0); 8609 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP()) 8610 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 8611 8612 // vmovrrd(load f64) -> (load i32), (load i32) 8613 SDNode *InNode = InDouble.getNode(); 8614 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 8615 InNode->getValueType(0) == MVT::f64 && 8616 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 8617 !cast<LoadSDNode>(InNode)->isVolatile()) { 8618 // TODO: Should this be done for non-FrameIndex operands? 8619 LoadSDNode *LD = cast<LoadSDNode>(InNode); 8620 8621 SelectionDAG &DAG = DCI.DAG; 8622 SDLoc DL(LD); 8623 SDValue BasePtr = LD->getBasePtr(); 8624 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, 8625 LD->getPointerInfo(), LD->isVolatile(), 8626 LD->isNonTemporal(), LD->isInvariant(), 8627 LD->getAlignment()); 8628 8629 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 8630 DAG.getConstant(4, DL, MVT::i32)); 8631 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, 8632 LD->getPointerInfo(), LD->isVolatile(), 8633 LD->isNonTemporal(), LD->isInvariant(), 8634 std::min(4U, LD->getAlignment() / 2)); 8635 8636 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 8637 if (DCI.DAG.getTargetLoweringInfo().isBigEndian()) 8638 std::swap (NewLD1, NewLD2); 8639 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 8640 return Result; 8641 } 8642 8643 return SDValue(); 8644 } 8645 8646 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 8647 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 8648 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 8649 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 8650 SDValue Op0 = N->getOperand(0); 8651 SDValue Op1 = N->getOperand(1); 8652 if (Op0.getOpcode() == ISD::BITCAST) 8653 Op0 = Op0.getOperand(0); 8654 if (Op1.getOpcode() == ISD::BITCAST) 8655 Op1 = Op1.getOperand(0); 8656 if (Op0.getOpcode() == ARMISD::VMOVRRD && 8657 Op0.getNode() == Op1.getNode() && 8658 Op0.getResNo() == 0 && Op1.getResNo() == 1) 8659 return DAG.getNode(ISD::BITCAST, SDLoc(N), 8660 N->getValueType(0), Op0.getOperand(0)); 8661 return SDValue(); 8662 } 8663 8664 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 8665 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 8666 /// i64 vector to have f64 elements, since the value can then be loaded 8667 /// directly into a VFP register. 8668 static bool hasNormalLoadOperand(SDNode *N) { 8669 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 8670 for (unsigned i = 0; i < NumElts; ++i) { 8671 SDNode *Elt = N->getOperand(i).getNode(); 8672 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 8673 return true; 8674 } 8675 return false; 8676 } 8677 8678 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 8679 /// ISD::BUILD_VECTOR. 8680 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 8681 TargetLowering::DAGCombinerInfo &DCI, 8682 const ARMSubtarget *Subtarget) { 8683 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 8684 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 8685 // into a pair of GPRs, which is fine when the value is used as a scalar, 8686 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 8687 SelectionDAG &DAG = DCI.DAG; 8688 if (N->getNumOperands() == 2) { 8689 SDValue RV = PerformVMOVDRRCombine(N, DAG); 8690 if (RV.getNode()) 8691 return RV; 8692 } 8693 8694 // Load i64 elements as f64 values so that type legalization does not split 8695 // them up into i32 values. 8696 EVT VT = N->getValueType(0); 8697 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 8698 return SDValue(); 8699 SDLoc dl(N); 8700 SmallVector<SDValue, 8> Ops; 8701 unsigned NumElts = VT.getVectorNumElements(); 8702 for (unsigned i = 0; i < NumElts; ++i) { 8703 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 8704 Ops.push_back(V); 8705 // Make the DAGCombiner fold the bitcast. 8706 DCI.AddToWorklist(V.getNode()); 8707 } 8708 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 8709 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops); 8710 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 8711 } 8712 8713 /// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR. 8714 static SDValue 8715 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 8716 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR. 8717 // At that time, we may have inserted bitcasts from integer to float. 8718 // If these bitcasts have survived DAGCombine, change the lowering of this 8719 // BUILD_VECTOR in something more vector friendly, i.e., that does not 8720 // force to use floating point types. 8721 8722 // Make sure we can change the type of the vector. 8723 // This is possible iff: 8724 // 1. The vector is only used in a bitcast to a integer type. I.e., 8725 // 1.1. Vector is used only once. 8726 // 1.2. Use is a bit convert to an integer type. 8727 // 2. The size of its operands are 32-bits (64-bits are not legal). 8728 EVT VT = N->getValueType(0); 8729 EVT EltVT = VT.getVectorElementType(); 8730 8731 // Check 1.1. and 2. 8732 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse()) 8733 return SDValue(); 8734 8735 // By construction, the input type must be float. 8736 assert(EltVT == MVT::f32 && "Unexpected type!"); 8737 8738 // Check 1.2. 8739 SDNode *Use = *N->use_begin(); 8740 if (Use->getOpcode() != ISD::BITCAST || 8741 Use->getValueType(0).isFloatingPoint()) 8742 return SDValue(); 8743 8744 // Check profitability. 8745 // Model is, if more than half of the relevant operands are bitcast from 8746 // i32, turn the build_vector into a sequence of insert_vector_elt. 8747 // Relevant operands are everything that is not statically 8748 // (i.e., at compile time) bitcasted. 8749 unsigned NumOfBitCastedElts = 0; 8750 unsigned NumElts = VT.getVectorNumElements(); 8751 unsigned NumOfRelevantElts = NumElts; 8752 for (unsigned Idx = 0; Idx < NumElts; ++Idx) { 8753 SDValue Elt = N->getOperand(Idx); 8754 if (Elt->getOpcode() == ISD::BITCAST) { 8755 // Assume only bit cast to i32 will go away. 8756 if (Elt->getOperand(0).getValueType() == MVT::i32) 8757 ++NumOfBitCastedElts; 8758 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt)) 8759 // Constants are statically casted, thus do not count them as 8760 // relevant operands. 8761 --NumOfRelevantElts; 8762 } 8763 8764 // Check if more than half of the elements require a non-free bitcast. 8765 if (NumOfBitCastedElts <= NumOfRelevantElts / 2) 8766 return SDValue(); 8767 8768 SelectionDAG &DAG = DCI.DAG; 8769 // Create the new vector type. 8770 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 8771 // Check if the type is legal. 8772 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8773 if (!TLI.isTypeLegal(VecVT)) 8774 return SDValue(); 8775 8776 // Combine: 8777 // ARMISD::BUILD_VECTOR E1, E2, ..., EN. 8778 // => BITCAST INSERT_VECTOR_ELT 8779 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1), 8780 // (BITCAST EN), N. 8781 SDValue Vec = DAG.getUNDEF(VecVT); 8782 SDLoc dl(N); 8783 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) { 8784 SDValue V = N->getOperand(Idx); 8785 if (V.getOpcode() == ISD::UNDEF) 8786 continue; 8787 if (V.getOpcode() == ISD::BITCAST && 8788 V->getOperand(0).getValueType() == MVT::i32) 8789 // Fold obvious case. 8790 V = V.getOperand(0); 8791 else { 8792 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V); 8793 // Make the DAGCombiner fold the bitcasts. 8794 DCI.AddToWorklist(V.getNode()); 8795 } 8796 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32); 8797 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx); 8798 } 8799 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec); 8800 // Make the DAGCombiner fold the bitcasts. 8801 DCI.AddToWorklist(Vec.getNode()); 8802 return Vec; 8803 } 8804 8805 /// PerformInsertEltCombine - Target-specific dag combine xforms for 8806 /// ISD::INSERT_VECTOR_ELT. 8807 static SDValue PerformInsertEltCombine(SDNode *N, 8808 TargetLowering::DAGCombinerInfo &DCI) { 8809 // Bitcast an i64 load inserted into a vector to f64. 8810 // Otherwise, the i64 value will be legalized to a pair of i32 values. 8811 EVT VT = N->getValueType(0); 8812 SDNode *Elt = N->getOperand(1).getNode(); 8813 if (VT.getVectorElementType() != MVT::i64 || 8814 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 8815 return SDValue(); 8816 8817 SelectionDAG &DAG = DCI.DAG; 8818 SDLoc dl(N); 8819 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 8820 VT.getVectorNumElements()); 8821 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 8822 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 8823 // Make the DAGCombiner fold the bitcasts. 8824 DCI.AddToWorklist(Vec.getNode()); 8825 DCI.AddToWorklist(V.getNode()); 8826 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 8827 Vec, V, N->getOperand(2)); 8828 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 8829 } 8830 8831 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 8832 /// ISD::VECTOR_SHUFFLE. 8833 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 8834 // The LLVM shufflevector instruction does not require the shuffle mask 8835 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 8836 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 8837 // operands do not match the mask length, they are extended by concatenating 8838 // them with undef vectors. That is probably the right thing for other 8839 // targets, but for NEON it is better to concatenate two double-register 8840 // size vector operands into a single quad-register size vector. Do that 8841 // transformation here: 8842 // shuffle(concat(v1, undef), concat(v2, undef)) -> 8843 // shuffle(concat(v1, v2), undef) 8844 SDValue Op0 = N->getOperand(0); 8845 SDValue Op1 = N->getOperand(1); 8846 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 8847 Op1.getOpcode() != ISD::CONCAT_VECTORS || 8848 Op0.getNumOperands() != 2 || 8849 Op1.getNumOperands() != 2) 8850 return SDValue(); 8851 SDValue Concat0Op1 = Op0.getOperand(1); 8852 SDValue Concat1Op1 = Op1.getOperand(1); 8853 if (Concat0Op1.getOpcode() != ISD::UNDEF || 8854 Concat1Op1.getOpcode() != ISD::UNDEF) 8855 return SDValue(); 8856 // Skip the transformation if any of the types are illegal. 8857 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8858 EVT VT = N->getValueType(0); 8859 if (!TLI.isTypeLegal(VT) || 8860 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 8861 !TLI.isTypeLegal(Concat1Op1.getValueType())) 8862 return SDValue(); 8863 8864 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, 8865 Op0.getOperand(0), Op1.getOperand(0)); 8866 // Translate the shuffle mask. 8867 SmallVector<int, 16> NewMask; 8868 unsigned NumElts = VT.getVectorNumElements(); 8869 unsigned HalfElts = NumElts/2; 8870 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 8871 for (unsigned n = 0; n < NumElts; ++n) { 8872 int MaskElt = SVN->getMaskElt(n); 8873 int NewElt = -1; 8874 if (MaskElt < (int)HalfElts) 8875 NewElt = MaskElt; 8876 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 8877 NewElt = HalfElts + MaskElt - NumElts; 8878 NewMask.push_back(NewElt); 8879 } 8880 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat, 8881 DAG.getUNDEF(VT), NewMask.data()); 8882 } 8883 8884 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP, 8885 /// NEON load/store intrinsics, and generic vector load/stores, to merge 8886 /// base address updates. 8887 /// For generic load/stores, the memory type is assumed to be a vector. 8888 /// The caller is assumed to have checked legality. 8889 static SDValue CombineBaseUpdate(SDNode *N, 8890 TargetLowering::DAGCombinerInfo &DCI) { 8891 SelectionDAG &DAG = DCI.DAG; 8892 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 8893 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 8894 const bool isStore = N->getOpcode() == ISD::STORE; 8895 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1); 8896 SDValue Addr = N->getOperand(AddrOpIdx); 8897 MemSDNode *MemN = cast<MemSDNode>(N); 8898 SDLoc dl(N); 8899 8900 // Search for a use of the address operand that is an increment. 8901 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 8902 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 8903 SDNode *User = *UI; 8904 if (User->getOpcode() != ISD::ADD || 8905 UI.getUse().getResNo() != Addr.getResNo()) 8906 continue; 8907 8908 // Check that the add is independent of the load/store. Otherwise, folding 8909 // it would create a cycle. 8910 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 8911 continue; 8912 8913 // Find the new opcode for the updating load/store. 8914 bool isLoadOp = true; 8915 bool isLaneOp = false; 8916 unsigned NewOpc = 0; 8917 unsigned NumVecs = 0; 8918 if (isIntrinsic) { 8919 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 8920 switch (IntNo) { 8921 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 8922 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 8923 NumVecs = 1; break; 8924 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 8925 NumVecs = 2; break; 8926 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 8927 NumVecs = 3; break; 8928 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 8929 NumVecs = 4; break; 8930 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 8931 NumVecs = 2; isLaneOp = true; break; 8932 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 8933 NumVecs = 3; isLaneOp = true; break; 8934 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 8935 NumVecs = 4; isLaneOp = true; break; 8936 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 8937 NumVecs = 1; isLoadOp = false; break; 8938 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 8939 NumVecs = 2; isLoadOp = false; break; 8940 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 8941 NumVecs = 3; isLoadOp = false; break; 8942 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 8943 NumVecs = 4; isLoadOp = false; break; 8944 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 8945 NumVecs = 2; isLoadOp = false; isLaneOp = true; break; 8946 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 8947 NumVecs = 3; isLoadOp = false; isLaneOp = true; break; 8948 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 8949 NumVecs = 4; isLoadOp = false; isLaneOp = true; break; 8950 } 8951 } else { 8952 isLaneOp = true; 8953 switch (N->getOpcode()) { 8954 default: llvm_unreachable("unexpected opcode for Neon base update"); 8955 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 8956 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 8957 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 8958 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD; 8959 NumVecs = 1; isLaneOp = false; break; 8960 case ISD::STORE: NewOpc = ARMISD::VST1_UPD; 8961 NumVecs = 1; isLaneOp = false; isLoadOp = false; break; 8962 } 8963 } 8964 8965 // Find the size of memory referenced by the load/store. 8966 EVT VecTy; 8967 if (isLoadOp) { 8968 VecTy = N->getValueType(0); 8969 } else if (isIntrinsic) { 8970 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 8971 } else { 8972 assert(isStore && "Node has to be a load, a store, or an intrinsic!"); 8973 VecTy = N->getOperand(1).getValueType(); 8974 } 8975 8976 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 8977 if (isLaneOp) 8978 NumBytes /= VecTy.getVectorNumElements(); 8979 8980 // If the increment is a constant, it must match the memory ref size. 8981 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 8982 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 8983 uint64_t IncVal = CInc->getZExtValue(); 8984 if (IncVal != NumBytes) 8985 continue; 8986 } else if (NumBytes >= 3 * 16) { 8987 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 8988 // separate instructions that make it harder to use a non-constant update. 8989 continue; 8990 } 8991 8992 // OK, we found an ADD we can fold into the base update. 8993 // Now, create a _UPD node, taking care of not breaking alignment. 8994 8995 EVT AlignedVecTy = VecTy; 8996 unsigned Alignment = MemN->getAlignment(); 8997 8998 // If this is a less-than-standard-aligned load/store, change the type to 8999 // match the standard alignment. 9000 // The alignment is overlooked when selecting _UPD variants; and it's 9001 // easier to introduce bitcasts here than fix that. 9002 // There are 3 ways to get to this base-update combine: 9003 // - intrinsics: they are assumed to be properly aligned (to the standard 9004 // alignment of the memory type), so we don't need to do anything. 9005 // - ARMISD::VLDx nodes: they are only generated from the aforementioned 9006 // intrinsics, so, likewise, there's nothing to do. 9007 // - generic load/store instructions: the alignment is specified as an 9008 // explicit operand, rather than implicitly as the standard alignment 9009 // of the memory type (like the intrisics). We need to change the 9010 // memory type to match the explicit alignment. That way, we don't 9011 // generate non-standard-aligned ARMISD::VLDx nodes. 9012 if (isa<LSBaseSDNode>(N)) { 9013 if (Alignment == 0) 9014 Alignment = 1; 9015 if (Alignment < VecTy.getScalarSizeInBits() / 8) { 9016 MVT EltTy = MVT::getIntegerVT(Alignment * 8); 9017 assert(NumVecs == 1 && "Unexpected multi-element generic load/store."); 9018 assert(!isLaneOp && "Unexpected generic load/store lane."); 9019 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8); 9020 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts); 9021 } 9022 // Don't set an explicit alignment on regular load/stores that we want 9023 // to transform to VLD/VST 1_UPD nodes. 9024 // This matches the behavior of regular load/stores, which only get an 9025 // explicit alignment if the MMO alignment is larger than the standard 9026 // alignment of the memory type. 9027 // Intrinsics, however, always get an explicit alignment, set to the 9028 // alignment of the MMO. 9029 Alignment = 1; 9030 } 9031 9032 // Create the new updating load/store node. 9033 // First, create an SDVTList for the new updating node's results. 9034 EVT Tys[6]; 9035 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0); 9036 unsigned n; 9037 for (n = 0; n < NumResultVecs; ++n) 9038 Tys[n] = AlignedVecTy; 9039 Tys[n++] = MVT::i32; 9040 Tys[n] = MVT::Other; 9041 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2)); 9042 9043 // Then, gather the new node's operands. 9044 SmallVector<SDValue, 8> Ops; 9045 Ops.push_back(N->getOperand(0)); // incoming chain 9046 Ops.push_back(N->getOperand(AddrOpIdx)); 9047 Ops.push_back(Inc); 9048 9049 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) { 9050 // Try to match the intrinsic's signature 9051 Ops.push_back(StN->getValue()); 9052 } else { 9053 // Loads (and of course intrinsics) match the intrinsics' signature, 9054 // so just add all but the alignment operand. 9055 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i) 9056 Ops.push_back(N->getOperand(i)); 9057 } 9058 9059 // For all node types, the alignment operand is always the last one. 9060 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32)); 9061 9062 // If this is a non-standard-aligned STORE, the penultimate operand is the 9063 // stored value. Bitcast it to the aligned type. 9064 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) { 9065 SDValue &StVal = Ops[Ops.size()-2]; 9066 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal); 9067 } 9068 9069 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys, 9070 Ops, AlignedVecTy, 9071 MemN->getMemOperand()); 9072 9073 // Update the uses. 9074 SmallVector<SDValue, 5> NewResults; 9075 for (unsigned i = 0; i < NumResultVecs; ++i) 9076 NewResults.push_back(SDValue(UpdN.getNode(), i)); 9077 9078 // If this is an non-standard-aligned LOAD, the first result is the loaded 9079 // value. Bitcast it to the expected result type. 9080 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) { 9081 SDValue &LdVal = NewResults[0]; 9082 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal); 9083 } 9084 9085 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 9086 DCI.CombineTo(N, NewResults); 9087 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 9088 9089 break; 9090 } 9091 return SDValue(); 9092 } 9093 9094 static SDValue PerformVLDCombine(SDNode *N, 9095 TargetLowering::DAGCombinerInfo &DCI) { 9096 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 9097 return SDValue(); 9098 9099 return CombineBaseUpdate(N, DCI); 9100 } 9101 9102 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 9103 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 9104 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 9105 /// return true. 9106 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 9107 SelectionDAG &DAG = DCI.DAG; 9108 EVT VT = N->getValueType(0); 9109 // vldN-dup instructions only support 64-bit vectors for N > 1. 9110 if (!VT.is64BitVector()) 9111 return false; 9112 9113 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 9114 SDNode *VLD = N->getOperand(0).getNode(); 9115 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 9116 return false; 9117 unsigned NumVecs = 0; 9118 unsigned NewOpc = 0; 9119 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 9120 if (IntNo == Intrinsic::arm_neon_vld2lane) { 9121 NumVecs = 2; 9122 NewOpc = ARMISD::VLD2DUP; 9123 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 9124 NumVecs = 3; 9125 NewOpc = ARMISD::VLD3DUP; 9126 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 9127 NumVecs = 4; 9128 NewOpc = ARMISD::VLD4DUP; 9129 } else { 9130 return false; 9131 } 9132 9133 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 9134 // numbers match the load. 9135 unsigned VLDLaneNo = 9136 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 9137 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 9138 UI != UE; ++UI) { 9139 // Ignore uses of the chain result. 9140 if (UI.getUse().getResNo() == NumVecs) 9141 continue; 9142 SDNode *User = *UI; 9143 if (User->getOpcode() != ARMISD::VDUPLANE || 9144 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 9145 return false; 9146 } 9147 9148 // Create the vldN-dup node. 9149 EVT Tys[5]; 9150 unsigned n; 9151 for (n = 0; n < NumVecs; ++n) 9152 Tys[n] = VT; 9153 Tys[n] = MVT::Other; 9154 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1)); 9155 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 9156 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 9157 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, 9158 Ops, VLDMemInt->getMemoryVT(), 9159 VLDMemInt->getMemOperand()); 9160 9161 // Update the uses. 9162 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 9163 UI != UE; ++UI) { 9164 unsigned ResNo = UI.getUse().getResNo(); 9165 // Ignore uses of the chain result. 9166 if (ResNo == NumVecs) 9167 continue; 9168 SDNode *User = *UI; 9169 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 9170 } 9171 9172 // Now the vldN-lane intrinsic is dead except for its chain result. 9173 // Update uses of the chain. 9174 std::vector<SDValue> VLDDupResults; 9175 for (unsigned n = 0; n < NumVecs; ++n) 9176 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 9177 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 9178 DCI.CombineTo(VLD, VLDDupResults); 9179 9180 return true; 9181 } 9182 9183 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 9184 /// ARMISD::VDUPLANE. 9185 static SDValue PerformVDUPLANECombine(SDNode *N, 9186 TargetLowering::DAGCombinerInfo &DCI) { 9187 SDValue Op = N->getOperand(0); 9188 9189 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 9190 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 9191 if (CombineVLDDUP(N, DCI)) 9192 return SDValue(N, 0); 9193 9194 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 9195 // redundant. Ignore bit_converts for now; element sizes are checked below. 9196 while (Op.getOpcode() == ISD::BITCAST) 9197 Op = Op.getOperand(0); 9198 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 9199 return SDValue(); 9200 9201 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 9202 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits(); 9203 // The canonical VMOV for a zero vector uses a 32-bit element size. 9204 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 9205 unsigned EltBits; 9206 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0) 9207 EltSize = 8; 9208 EVT VT = N->getValueType(0); 9209 if (EltSize > VT.getVectorElementType().getSizeInBits()) 9210 return SDValue(); 9211 9212 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 9213 } 9214 9215 static SDValue PerformLOADCombine(SDNode *N, 9216 TargetLowering::DAGCombinerInfo &DCI) { 9217 EVT VT = N->getValueType(0); 9218 9219 // If this is a legal vector load, try to combine it into a VLD1_UPD. 9220 if (ISD::isNormalLoad(N) && VT.isVector() && 9221 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 9222 return CombineBaseUpdate(N, DCI); 9223 9224 return SDValue(); 9225 } 9226 9227 /// PerformSTORECombine - Target-specific dag combine xforms for 9228 /// ISD::STORE. 9229 static SDValue PerformSTORECombine(SDNode *N, 9230 TargetLowering::DAGCombinerInfo &DCI) { 9231 StoreSDNode *St = cast<StoreSDNode>(N); 9232 if (St->isVolatile()) 9233 return SDValue(); 9234 9235 // Optimize trunc store (of multiple scalars) to shuffle and store. First, 9236 // pack all of the elements in one place. Next, store to memory in fewer 9237 // chunks. 9238 SDValue StVal = St->getValue(); 9239 EVT VT = StVal.getValueType(); 9240 if (St->isTruncatingStore() && VT.isVector()) { 9241 SelectionDAG &DAG = DCI.DAG; 9242 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9243 EVT StVT = St->getMemoryVT(); 9244 unsigned NumElems = VT.getVectorNumElements(); 9245 assert(StVT != VT && "Cannot truncate to the same type"); 9246 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits(); 9247 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits(); 9248 9249 // From, To sizes and ElemCount must be pow of two 9250 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue(); 9251 9252 // We are going to use the original vector elt for storing. 9253 // Accumulated smaller vector elements must be a multiple of the store size. 9254 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue(); 9255 9256 unsigned SizeRatio = FromEltSz / ToEltSz; 9257 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits()); 9258 9259 // Create a type on which we perform the shuffle. 9260 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(), 9261 NumElems*SizeRatio); 9262 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); 9263 9264 SDLoc DL(St); 9265 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal); 9266 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); 9267 for (unsigned i = 0; i < NumElems; ++i) 9268 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio; 9269 9270 // Can't shuffle using an illegal type. 9271 if (!TLI.isTypeLegal(WideVecVT)) return SDValue(); 9272 9273 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec, 9274 DAG.getUNDEF(WideVec.getValueType()), 9275 ShuffleVec.data()); 9276 // At this point all of the data is stored at the bottom of the 9277 // register. We now need to save it to mem. 9278 9279 // Find the largest store unit 9280 MVT StoreType = MVT::i8; 9281 for (MVT Tp : MVT::integer_valuetypes()) { 9282 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz) 9283 StoreType = Tp; 9284 } 9285 // Didn't find a legal store type. 9286 if (!TLI.isTypeLegal(StoreType)) 9287 return SDValue(); 9288 9289 // Bitcast the original vector into a vector of store-size units 9290 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(), 9291 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits()); 9292 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); 9293 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff); 9294 SmallVector<SDValue, 8> Chains; 9295 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8, DL, 9296 TLI.getPointerTy()); 9297 SDValue BasePtr = St->getBasePtr(); 9298 9299 // Perform one or more big stores into memory. 9300 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits(); 9301 for (unsigned I = 0; I < E; I++) { 9302 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, 9303 StoreType, ShuffWide, 9304 DAG.getIntPtrConstant(I, DL)); 9305 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr, 9306 St->getPointerInfo(), St->isVolatile(), 9307 St->isNonTemporal(), St->getAlignment()); 9308 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, 9309 Increment); 9310 Chains.push_back(Ch); 9311 } 9312 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 9313 } 9314 9315 if (!ISD::isNormalStore(St)) 9316 return SDValue(); 9317 9318 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and 9319 // ARM stores of arguments in the same cache line. 9320 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 9321 StVal.getNode()->hasOneUse()) { 9322 SelectionDAG &DAG = DCI.DAG; 9323 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian(); 9324 SDLoc DL(St); 9325 SDValue BasePtr = St->getBasePtr(); 9326 SDValue NewST1 = DAG.getStore(St->getChain(), DL, 9327 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ), 9328 BasePtr, St->getPointerInfo(), St->isVolatile(), 9329 St->isNonTemporal(), St->getAlignment()); 9330 9331 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 9332 DAG.getConstant(4, DL, MVT::i32)); 9333 return DAG.getStore(NewST1.getValue(0), DL, 9334 StVal.getNode()->getOperand(isBigEndian ? 0 : 1), 9335 OffsetPtr, St->getPointerInfo(), St->isVolatile(), 9336 St->isNonTemporal(), 9337 std::min(4U, St->getAlignment() / 2)); 9338 } 9339 9340 if (StVal.getValueType() == MVT::i64 && 9341 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 9342 9343 // Bitcast an i64 store extracted from a vector to f64. 9344 // Otherwise, the i64 value will be legalized to a pair of i32 values. 9345 SelectionDAG &DAG = DCI.DAG; 9346 SDLoc dl(StVal); 9347 SDValue IntVec = StVal.getOperand(0); 9348 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 9349 IntVec.getValueType().getVectorNumElements()); 9350 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 9351 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 9352 Vec, StVal.getOperand(1)); 9353 dl = SDLoc(N); 9354 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 9355 // Make the DAGCombiner fold the bitcasts. 9356 DCI.AddToWorklist(Vec.getNode()); 9357 DCI.AddToWorklist(ExtElt.getNode()); 9358 DCI.AddToWorklist(V.getNode()); 9359 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 9360 St->getPointerInfo(), St->isVolatile(), 9361 St->isNonTemporal(), St->getAlignment(), 9362 St->getAAInfo()); 9363 } 9364 9365 // If this is a legal vector store, try to combine it into a VST1_UPD. 9366 if (ISD::isNormalStore(N) && VT.isVector() && 9367 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 9368 return CombineBaseUpdate(N, DCI); 9369 9370 return SDValue(); 9371 } 9372 9373 // isConstVecPow2 - Return true if each vector element is a power of 2, all 9374 // elements are the same constant, C, and Log2(C) ranges from 1 to 32. 9375 static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C) 9376 { 9377 integerPart cN; 9378 integerPart c0 = 0; 9379 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements(); 9380 I != E; I++) { 9381 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I)); 9382 if (!C) 9383 return false; 9384 9385 bool isExact; 9386 APFloat APF = C->getValueAPF(); 9387 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact) 9388 != APFloat::opOK || !isExact) 9389 return false; 9390 9391 c0 = (I == 0) ? cN : c0; 9392 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32) 9393 return false; 9394 } 9395 C = c0; 9396 return true; 9397 } 9398 9399 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 9400 /// can replace combinations of VMUL and VCVT (floating-point to integer) 9401 /// when the VMUL has a constant operand that is a power of 2. 9402 /// 9403 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 9404 /// vmul.f32 d16, d17, d16 9405 /// vcvt.s32.f32 d16, d16 9406 /// becomes: 9407 /// vcvt.s32.f32 d16, d16, #3 9408 static SDValue PerformVCVTCombine(SDNode *N, 9409 TargetLowering::DAGCombinerInfo &DCI, 9410 const ARMSubtarget *Subtarget) { 9411 SelectionDAG &DAG = DCI.DAG; 9412 SDValue Op = N->getOperand(0); 9413 9414 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() || 9415 Op.getOpcode() != ISD::FMUL) 9416 return SDValue(); 9417 9418 uint64_t C; 9419 SDValue N0 = Op->getOperand(0); 9420 SDValue ConstVec = Op->getOperand(1); 9421 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 9422 9423 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 9424 !isConstVecPow2(ConstVec, isSigned, C)) 9425 return SDValue(); 9426 9427 MVT FloatTy = Op.getSimpleValueType().getVectorElementType(); 9428 MVT IntTy = N->getSimpleValueType(0).getVectorElementType(); 9429 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 9430 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 || 9431 NumLanes > 4) { 9432 // These instructions only exist converting from f32 to i32. We can handle 9433 // smaller integers by generating an extra truncate, but larger ones would 9434 // be lossy. We also can't handle more then 4 lanes, since these intructions 9435 // only support v2i32/v4i32 types. 9436 return SDValue(); 9437 } 9438 9439 SDLoc dl(N); 9440 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 9441 Intrinsic::arm_neon_vcvtfp2fxu; 9442 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, 9443 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 9444 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), 9445 N0, 9446 DAG.getConstant(Log2_64(C), dl, MVT::i32)); 9447 9448 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits()) 9449 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv); 9450 9451 return FixConv; 9452 } 9453 9454 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 9455 /// can replace combinations of VCVT (integer to floating-point) and VDIV 9456 /// when the VDIV has a constant operand that is a power of 2. 9457 /// 9458 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 9459 /// vcvt.f32.s32 d16, d16 9460 /// vdiv.f32 d16, d17, d16 9461 /// becomes: 9462 /// vcvt.f32.s32 d16, d16, #3 9463 static SDValue PerformVDIVCombine(SDNode *N, 9464 TargetLowering::DAGCombinerInfo &DCI, 9465 const ARMSubtarget *Subtarget) { 9466 SelectionDAG &DAG = DCI.DAG; 9467 SDValue Op = N->getOperand(0); 9468 unsigned OpOpcode = Op.getNode()->getOpcode(); 9469 9470 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() || 9471 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 9472 return SDValue(); 9473 9474 uint64_t C; 9475 SDValue ConstVec = N->getOperand(1); 9476 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 9477 9478 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR || 9479 !isConstVecPow2(ConstVec, isSigned, C)) 9480 return SDValue(); 9481 9482 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType(); 9483 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType(); 9484 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) { 9485 // These instructions only exist converting from i32 to f32. We can handle 9486 // smaller integers by generating an extra extend, but larger ones would 9487 // be lossy. 9488 return SDValue(); 9489 } 9490 9491 SDLoc dl(N); 9492 SDValue ConvInput = Op.getOperand(0); 9493 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 9494 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits()) 9495 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 9496 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 9497 ConvInput); 9498 9499 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 9500 Intrinsic::arm_neon_vcvtfxu2fp; 9501 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, 9502 Op.getValueType(), 9503 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), 9504 ConvInput, DAG.getConstant(Log2_64(C), dl, MVT::i32)); 9505 } 9506 9507 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 9508 /// operand of a vector shift operation, where all the elements of the 9509 /// build_vector must have the same constant integer value. 9510 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 9511 // Ignore bit_converts. 9512 while (Op.getOpcode() == ISD::BITCAST) 9513 Op = Op.getOperand(0); 9514 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 9515 APInt SplatBits, SplatUndef; 9516 unsigned SplatBitSize; 9517 bool HasAnyUndefs; 9518 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 9519 HasAnyUndefs, ElementBits) || 9520 SplatBitSize > ElementBits) 9521 return false; 9522 Cnt = SplatBits.getSExtValue(); 9523 return true; 9524 } 9525 9526 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 9527 /// operand of a vector shift left operation. That value must be in the range: 9528 /// 0 <= Value < ElementBits for a left shift; or 9529 /// 0 <= Value <= ElementBits for a long left shift. 9530 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 9531 assert(VT.isVector() && "vector shift count is not a vector type"); 9532 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 9533 if (! getVShiftImm(Op, ElementBits, Cnt)) 9534 return false; 9535 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits); 9536 } 9537 9538 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 9539 /// operand of a vector shift right operation. For a shift opcode, the value 9540 /// is positive, but for an intrinsic the value count must be negative. The 9541 /// absolute value must be in the range: 9542 /// 1 <= |Value| <= ElementBits for a right shift; or 9543 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 9544 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 9545 int64_t &Cnt) { 9546 assert(VT.isVector() && "vector shift count is not a vector type"); 9547 unsigned ElementBits = VT.getVectorElementType().getSizeInBits(); 9548 if (! getVShiftImm(Op, ElementBits, Cnt)) 9549 return false; 9550 if (isIntrinsic) 9551 Cnt = -Cnt; 9552 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits)); 9553 } 9554 9555 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 9556 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 9557 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 9558 switch (IntNo) { 9559 default: 9560 // Don't do anything for most intrinsics. 9561 break; 9562 9563 // Vector shifts: check for immediate versions and lower them. 9564 // Note: This is done during DAG combining instead of DAG legalizing because 9565 // the build_vectors for 64-bit vector element shift counts are generally 9566 // not legal, and it is hard to see their values after they get legalized to 9567 // loads from a constant pool. 9568 case Intrinsic::arm_neon_vshifts: 9569 case Intrinsic::arm_neon_vshiftu: 9570 case Intrinsic::arm_neon_vrshifts: 9571 case Intrinsic::arm_neon_vrshiftu: 9572 case Intrinsic::arm_neon_vrshiftn: 9573 case Intrinsic::arm_neon_vqshifts: 9574 case Intrinsic::arm_neon_vqshiftu: 9575 case Intrinsic::arm_neon_vqshiftsu: 9576 case Intrinsic::arm_neon_vqshiftns: 9577 case Intrinsic::arm_neon_vqshiftnu: 9578 case Intrinsic::arm_neon_vqshiftnsu: 9579 case Intrinsic::arm_neon_vqrshiftns: 9580 case Intrinsic::arm_neon_vqrshiftnu: 9581 case Intrinsic::arm_neon_vqrshiftnsu: { 9582 EVT VT = N->getOperand(1).getValueType(); 9583 int64_t Cnt; 9584 unsigned VShiftOpc = 0; 9585 9586 switch (IntNo) { 9587 case Intrinsic::arm_neon_vshifts: 9588 case Intrinsic::arm_neon_vshiftu: 9589 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 9590 VShiftOpc = ARMISD::VSHL; 9591 break; 9592 } 9593 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 9594 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? 9595 ARMISD::VSHRs : ARMISD::VSHRu); 9596 break; 9597 } 9598 return SDValue(); 9599 9600 case Intrinsic::arm_neon_vrshifts: 9601 case Intrinsic::arm_neon_vrshiftu: 9602 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 9603 break; 9604 return SDValue(); 9605 9606 case Intrinsic::arm_neon_vqshifts: 9607 case Intrinsic::arm_neon_vqshiftu: 9608 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 9609 break; 9610 return SDValue(); 9611 9612 case Intrinsic::arm_neon_vqshiftsu: 9613 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 9614 break; 9615 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 9616 9617 case Intrinsic::arm_neon_vrshiftn: 9618 case Intrinsic::arm_neon_vqshiftns: 9619 case Intrinsic::arm_neon_vqshiftnu: 9620 case Intrinsic::arm_neon_vqshiftnsu: 9621 case Intrinsic::arm_neon_vqrshiftns: 9622 case Intrinsic::arm_neon_vqrshiftnu: 9623 case Intrinsic::arm_neon_vqrshiftnsu: 9624 // Narrowing shifts require an immediate right shift. 9625 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 9626 break; 9627 llvm_unreachable("invalid shift count for narrowing vector shift " 9628 "intrinsic"); 9629 9630 default: 9631 llvm_unreachable("unhandled vector shift"); 9632 } 9633 9634 switch (IntNo) { 9635 case Intrinsic::arm_neon_vshifts: 9636 case Intrinsic::arm_neon_vshiftu: 9637 // Opcode already set above. 9638 break; 9639 case Intrinsic::arm_neon_vrshifts: 9640 VShiftOpc = ARMISD::VRSHRs; break; 9641 case Intrinsic::arm_neon_vrshiftu: 9642 VShiftOpc = ARMISD::VRSHRu; break; 9643 case Intrinsic::arm_neon_vrshiftn: 9644 VShiftOpc = ARMISD::VRSHRN; break; 9645 case Intrinsic::arm_neon_vqshifts: 9646 VShiftOpc = ARMISD::VQSHLs; break; 9647 case Intrinsic::arm_neon_vqshiftu: 9648 VShiftOpc = ARMISD::VQSHLu; break; 9649 case Intrinsic::arm_neon_vqshiftsu: 9650 VShiftOpc = ARMISD::VQSHLsu; break; 9651 case Intrinsic::arm_neon_vqshiftns: 9652 VShiftOpc = ARMISD::VQSHRNs; break; 9653 case Intrinsic::arm_neon_vqshiftnu: 9654 VShiftOpc = ARMISD::VQSHRNu; break; 9655 case Intrinsic::arm_neon_vqshiftnsu: 9656 VShiftOpc = ARMISD::VQSHRNsu; break; 9657 case Intrinsic::arm_neon_vqrshiftns: 9658 VShiftOpc = ARMISD::VQRSHRNs; break; 9659 case Intrinsic::arm_neon_vqrshiftnu: 9660 VShiftOpc = ARMISD::VQRSHRNu; break; 9661 case Intrinsic::arm_neon_vqrshiftnsu: 9662 VShiftOpc = ARMISD::VQRSHRNsu; break; 9663 } 9664 9665 SDLoc dl(N); 9666 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 9667 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32)); 9668 } 9669 9670 case Intrinsic::arm_neon_vshiftins: { 9671 EVT VT = N->getOperand(1).getValueType(); 9672 int64_t Cnt; 9673 unsigned VShiftOpc = 0; 9674 9675 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 9676 VShiftOpc = ARMISD::VSLI; 9677 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 9678 VShiftOpc = ARMISD::VSRI; 9679 else { 9680 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 9681 } 9682 9683 SDLoc dl(N); 9684 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 9685 N->getOperand(1), N->getOperand(2), 9686 DAG.getConstant(Cnt, dl, MVT::i32)); 9687 } 9688 9689 case Intrinsic::arm_neon_vqrshifts: 9690 case Intrinsic::arm_neon_vqrshiftu: 9691 // No immediate versions of these to check for. 9692 break; 9693 } 9694 9695 return SDValue(); 9696 } 9697 9698 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 9699 /// lowers them. As with the vector shift intrinsics, this is done during DAG 9700 /// combining instead of DAG legalizing because the build_vectors for 64-bit 9701 /// vector element shift counts are generally not legal, and it is hard to see 9702 /// their values after they get legalized to loads from a constant pool. 9703 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG, 9704 const ARMSubtarget *ST) { 9705 EVT VT = N->getValueType(0); 9706 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) { 9707 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high 9708 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16. 9709 SDValue N1 = N->getOperand(1); 9710 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 9711 SDValue N0 = N->getOperand(0); 9712 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP && 9713 DAG.MaskedValueIsZero(N0.getOperand(0), 9714 APInt::getHighBitsSet(32, 16))) 9715 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1); 9716 } 9717 } 9718 9719 // Nothing to be done for scalar shifts. 9720 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9721 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 9722 return SDValue(); 9723 9724 assert(ST->hasNEON() && "unexpected vector shift"); 9725 int64_t Cnt; 9726 9727 switch (N->getOpcode()) { 9728 default: llvm_unreachable("unexpected shift opcode"); 9729 9730 case ISD::SHL: 9731 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) { 9732 SDLoc dl(N); 9733 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0), 9734 DAG.getConstant(Cnt, dl, MVT::i32)); 9735 } 9736 break; 9737 9738 case ISD::SRA: 9739 case ISD::SRL: 9740 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 9741 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ? 9742 ARMISD::VSHRs : ARMISD::VSHRu); 9743 SDLoc dl(N); 9744 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0), 9745 DAG.getConstant(Cnt, dl, MVT::i32)); 9746 } 9747 } 9748 return SDValue(); 9749 } 9750 9751 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 9752 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 9753 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 9754 const ARMSubtarget *ST) { 9755 SDValue N0 = N->getOperand(0); 9756 9757 // Check for sign- and zero-extensions of vector extract operations of 8- 9758 // and 16-bit vector elements. NEON supports these directly. They are 9759 // handled during DAG combining because type legalization will promote them 9760 // to 32-bit types and it is messy to recognize the operations after that. 9761 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 9762 SDValue Vec = N0.getOperand(0); 9763 SDValue Lane = N0.getOperand(1); 9764 EVT VT = N->getValueType(0); 9765 EVT EltVT = N0.getValueType(); 9766 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9767 9768 if (VT == MVT::i32 && 9769 (EltVT == MVT::i8 || EltVT == MVT::i16) && 9770 TLI.isTypeLegal(Vec.getValueType()) && 9771 isa<ConstantSDNode>(Lane)) { 9772 9773 unsigned Opc = 0; 9774 switch (N->getOpcode()) { 9775 default: llvm_unreachable("unexpected opcode"); 9776 case ISD::SIGN_EXTEND: 9777 Opc = ARMISD::VGETLANEs; 9778 break; 9779 case ISD::ZERO_EXTEND: 9780 case ISD::ANY_EXTEND: 9781 Opc = ARMISD::VGETLANEu; 9782 break; 9783 } 9784 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane); 9785 } 9786 } 9787 9788 return SDValue(); 9789 } 9790 9791 /// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC 9792 /// to match f32 max/min patterns to use NEON vmax/vmin instructions. 9793 static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG, 9794 const ARMSubtarget *ST) { 9795 // If the target supports NEON, try to use vmax/vmin instructions for f32 9796 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set, 9797 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is 9798 // a NaN; only do the transformation when it matches that behavior. 9799 9800 // For now only do this when using NEON for FP operations; if using VFP, it 9801 // is not obvious that the benefit outweighs the cost of switching to the 9802 // NEON pipeline. 9803 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() || 9804 N->getValueType(0) != MVT::f32) 9805 return SDValue(); 9806 9807 SDValue CondLHS = N->getOperand(0); 9808 SDValue CondRHS = N->getOperand(1); 9809 SDValue LHS = N->getOperand(2); 9810 SDValue RHS = N->getOperand(3); 9811 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); 9812 9813 unsigned Opcode = 0; 9814 bool IsReversed; 9815 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) { 9816 IsReversed = false; // x CC y ? x : y 9817 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) { 9818 IsReversed = true ; // x CC y ? y : x 9819 } else { 9820 return SDValue(); 9821 } 9822 9823 bool IsUnordered; 9824 switch (CC) { 9825 default: break; 9826 case ISD::SETOLT: 9827 case ISD::SETOLE: 9828 case ISD::SETLT: 9829 case ISD::SETLE: 9830 case ISD::SETULT: 9831 case ISD::SETULE: 9832 // If LHS is NaN, an ordered comparison will be false and the result will 9833 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS 9834 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 9835 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE); 9836 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 9837 break; 9838 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin 9839 // will return -0, so vmin can only be used for unsafe math or if one of 9840 // the operands is known to be nonzero. 9841 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) && 9842 !DAG.getTarget().Options.UnsafeFPMath && 9843 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 9844 break; 9845 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN; 9846 break; 9847 9848 case ISD::SETOGT: 9849 case ISD::SETOGE: 9850 case ISD::SETGT: 9851 case ISD::SETGE: 9852 case ISD::SETUGT: 9853 case ISD::SETUGE: 9854 // If LHS is NaN, an ordered comparison will be false and the result will 9855 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS 9856 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN. 9857 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE); 9858 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS)) 9859 break; 9860 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax 9861 // will return +0, so vmax can only be used for unsafe math or if one of 9862 // the operands is known to be nonzero. 9863 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) && 9864 !DAG.getTarget().Options.UnsafeFPMath && 9865 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) 9866 break; 9867 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX; 9868 break; 9869 } 9870 9871 if (!Opcode) 9872 return SDValue(); 9873 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS); 9874 } 9875 9876 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 9877 SDValue 9878 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 9879 SDValue Cmp = N->getOperand(4); 9880 if (Cmp.getOpcode() != ARMISD::CMPZ) 9881 // Only looking at EQ and NE cases. 9882 return SDValue(); 9883 9884 EVT VT = N->getValueType(0); 9885 SDLoc dl(N); 9886 SDValue LHS = Cmp.getOperand(0); 9887 SDValue RHS = Cmp.getOperand(1); 9888 SDValue FalseVal = N->getOperand(0); 9889 SDValue TrueVal = N->getOperand(1); 9890 SDValue ARMcc = N->getOperand(2); 9891 ARMCC::CondCodes CC = 9892 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 9893 9894 // Simplify 9895 // mov r1, r0 9896 // cmp r1, x 9897 // mov r0, y 9898 // moveq r0, x 9899 // to 9900 // cmp r0, x 9901 // movne r0, y 9902 // 9903 // mov r1, r0 9904 // cmp r1, x 9905 // mov r0, x 9906 // movne r0, y 9907 // to 9908 // cmp r0, x 9909 // movne r0, y 9910 /// FIXME: Turn this into a target neutral optimization? 9911 SDValue Res; 9912 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 9913 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 9914 N->getOperand(3), Cmp); 9915 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 9916 SDValue ARMcc; 9917 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 9918 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 9919 N->getOperand(3), NewCmp); 9920 } 9921 9922 if (Res.getNode()) { 9923 APInt KnownZero, KnownOne; 9924 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne); 9925 // Capture demanded bits information that would be otherwise lost. 9926 if (KnownZero == 0xfffffffe) 9927 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9928 DAG.getValueType(MVT::i1)); 9929 else if (KnownZero == 0xffffff00) 9930 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9931 DAG.getValueType(MVT::i8)); 9932 else if (KnownZero == 0xffff0000) 9933 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 9934 DAG.getValueType(MVT::i16)); 9935 } 9936 9937 return Res; 9938 } 9939 9940 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 9941 DAGCombinerInfo &DCI) const { 9942 switch (N->getOpcode()) { 9943 default: break; 9944 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget); 9945 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 9946 case ISD::SUB: return PerformSUBCombine(N, DCI); 9947 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 9948 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 9949 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget); 9950 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget); 9951 case ARMISD::BFI: return PerformBFICombine(N, DCI); 9952 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget); 9953 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 9954 case ISD::STORE: return PerformSTORECombine(N, DCI); 9955 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget); 9956 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 9957 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 9958 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 9959 case ISD::FP_TO_SINT: 9960 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget); 9961 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget); 9962 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 9963 case ISD::SHL: 9964 case ISD::SRA: 9965 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget); 9966 case ISD::SIGN_EXTEND: 9967 case ISD::ZERO_EXTEND: 9968 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 9969 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget); 9970 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 9971 case ISD::LOAD: return PerformLOADCombine(N, DCI); 9972 case ARMISD::VLD2DUP: 9973 case ARMISD::VLD3DUP: 9974 case ARMISD::VLD4DUP: 9975 return PerformVLDCombine(N, DCI); 9976 case ARMISD::BUILD_VECTOR: 9977 return PerformARMBUILD_VECTORCombine(N, DCI); 9978 case ISD::INTRINSIC_VOID: 9979 case ISD::INTRINSIC_W_CHAIN: 9980 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 9981 case Intrinsic::arm_neon_vld1: 9982 case Intrinsic::arm_neon_vld2: 9983 case Intrinsic::arm_neon_vld3: 9984 case Intrinsic::arm_neon_vld4: 9985 case Intrinsic::arm_neon_vld2lane: 9986 case Intrinsic::arm_neon_vld3lane: 9987 case Intrinsic::arm_neon_vld4lane: 9988 case Intrinsic::arm_neon_vst1: 9989 case Intrinsic::arm_neon_vst2: 9990 case Intrinsic::arm_neon_vst3: 9991 case Intrinsic::arm_neon_vst4: 9992 case Intrinsic::arm_neon_vst2lane: 9993 case Intrinsic::arm_neon_vst3lane: 9994 case Intrinsic::arm_neon_vst4lane: 9995 return PerformVLDCombine(N, DCI); 9996 default: break; 9997 } 9998 break; 9999 } 10000 return SDValue(); 10001 } 10002 10003 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 10004 EVT VT) const { 10005 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 10006 } 10007 10008 bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 10009 unsigned, 10010 unsigned, 10011 bool *Fast) const { 10012 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus 10013 bool AllowsUnaligned = Subtarget->allowsUnalignedMem(); 10014 10015 switch (VT.getSimpleVT().SimpleTy) { 10016 default: 10017 return false; 10018 case MVT::i8: 10019 case MVT::i16: 10020 case MVT::i32: { 10021 // Unaligned access can use (for example) LRDB, LRDH, LDR 10022 if (AllowsUnaligned) { 10023 if (Fast) 10024 *Fast = Subtarget->hasV7Ops(); 10025 return true; 10026 } 10027 return false; 10028 } 10029 case MVT::f64: 10030 case MVT::v2f64: { 10031 // For any little-endian targets with neon, we can support unaligned ld/st 10032 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8. 10033 // A big-endian target may also explicitly support unaligned accesses 10034 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) { 10035 if (Fast) 10036 *Fast = true; 10037 return true; 10038 } 10039 return false; 10040 } 10041 } 10042 } 10043 10044 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 10045 unsigned AlignCheck) { 10046 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 10047 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 10048 } 10049 10050 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size, 10051 unsigned DstAlign, unsigned SrcAlign, 10052 bool IsMemset, bool ZeroMemset, 10053 bool MemcpyStrSrc, 10054 MachineFunction &MF) const { 10055 const Function *F = MF.getFunction(); 10056 10057 // See if we can use NEON instructions for this... 10058 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() && 10059 !F->hasFnAttribute(Attribute::NoImplicitFloat)) { 10060 bool Fast; 10061 if (Size >= 16 && 10062 (memOpAlign(SrcAlign, DstAlign, 16) || 10063 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) { 10064 return MVT::v2f64; 10065 } else if (Size >= 8 && 10066 (memOpAlign(SrcAlign, DstAlign, 8) || 10067 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) && 10068 Fast))) { 10069 return MVT::f64; 10070 } 10071 } 10072 10073 // Lowering to i32/i16 if the size permits. 10074 if (Size >= 4) 10075 return MVT::i32; 10076 else if (Size >= 2) 10077 return MVT::i16; 10078 10079 // Let the target-independent logic figure it out. 10080 return MVT::Other; 10081 } 10082 10083 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 10084 if (Val.getOpcode() != ISD::LOAD) 10085 return false; 10086 10087 EVT VT1 = Val.getValueType(); 10088 if (!VT1.isSimple() || !VT1.isInteger() || 10089 !VT2.isSimple() || !VT2.isInteger()) 10090 return false; 10091 10092 switch (VT1.getSimpleVT().SimpleTy) { 10093 default: break; 10094 case MVT::i1: 10095 case MVT::i8: 10096 case MVT::i16: 10097 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits. 10098 return true; 10099 } 10100 10101 return false; 10102 } 10103 10104 bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const { 10105 EVT VT = ExtVal.getValueType(); 10106 10107 if (!isTypeLegal(VT)) 10108 return false; 10109 10110 // Don't create a loadext if we can fold the extension into a wide/long 10111 // instruction. 10112 // If there's more than one user instruction, the loadext is desirable no 10113 // matter what. There can be two uses by the same instruction. 10114 if (ExtVal->use_empty() || 10115 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode())) 10116 return true; 10117 10118 SDNode *U = *ExtVal->use_begin(); 10119 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB || 10120 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL)) 10121 return false; 10122 10123 return true; 10124 } 10125 10126 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const { 10127 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 10128 return false; 10129 10130 if (!isTypeLegal(EVT::getEVT(Ty1))) 10131 return false; 10132 10133 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop"); 10134 10135 // Assuming the caller doesn't have a zeroext or signext return parameter, 10136 // truncation all the way down to i1 is valid. 10137 return true; 10138 } 10139 10140 10141 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 10142 if (V < 0) 10143 return false; 10144 10145 unsigned Scale = 1; 10146 switch (VT.getSimpleVT().SimpleTy) { 10147 default: return false; 10148 case MVT::i1: 10149 case MVT::i8: 10150 // Scale == 1; 10151 break; 10152 case MVT::i16: 10153 // Scale == 2; 10154 Scale = 2; 10155 break; 10156 case MVT::i32: 10157 // Scale == 4; 10158 Scale = 4; 10159 break; 10160 } 10161 10162 if ((V & (Scale - 1)) != 0) 10163 return false; 10164 V /= Scale; 10165 return V == (V & ((1LL << 5) - 1)); 10166 } 10167 10168 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 10169 const ARMSubtarget *Subtarget) { 10170 bool isNeg = false; 10171 if (V < 0) { 10172 isNeg = true; 10173 V = - V; 10174 } 10175 10176 switch (VT.getSimpleVT().SimpleTy) { 10177 default: return false; 10178 case MVT::i1: 10179 case MVT::i8: 10180 case MVT::i16: 10181 case MVT::i32: 10182 // + imm12 or - imm8 10183 if (isNeg) 10184 return V == (V & ((1LL << 8) - 1)); 10185 return V == (V & ((1LL << 12) - 1)); 10186 case MVT::f32: 10187 case MVT::f64: 10188 // Same as ARM mode. FIXME: NEON? 10189 if (!Subtarget->hasVFP2()) 10190 return false; 10191 if ((V & 3) != 0) 10192 return false; 10193 V >>= 2; 10194 return V == (V & ((1LL << 8) - 1)); 10195 } 10196 } 10197 10198 /// isLegalAddressImmediate - Return true if the integer value can be used 10199 /// as the offset of the target addressing mode for load / store of the 10200 /// given type. 10201 static bool isLegalAddressImmediate(int64_t V, EVT VT, 10202 const ARMSubtarget *Subtarget) { 10203 if (V == 0) 10204 return true; 10205 10206 if (!VT.isSimple()) 10207 return false; 10208 10209 if (Subtarget->isThumb1Only()) 10210 return isLegalT1AddressImmediate(V, VT); 10211 else if (Subtarget->isThumb2()) 10212 return isLegalT2AddressImmediate(V, VT, Subtarget); 10213 10214 // ARM mode. 10215 if (V < 0) 10216 V = - V; 10217 switch (VT.getSimpleVT().SimpleTy) { 10218 default: return false; 10219 case MVT::i1: 10220 case MVT::i8: 10221 case MVT::i32: 10222 // +- imm12 10223 return V == (V & ((1LL << 12) - 1)); 10224 case MVT::i16: 10225 // +- imm8 10226 return V == (V & ((1LL << 8) - 1)); 10227 case MVT::f32: 10228 case MVT::f64: 10229 if (!Subtarget->hasVFP2()) // FIXME: NEON? 10230 return false; 10231 if ((V & 3) != 0) 10232 return false; 10233 V >>= 2; 10234 return V == (V & ((1LL << 8) - 1)); 10235 } 10236 } 10237 10238 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 10239 EVT VT) const { 10240 int Scale = AM.Scale; 10241 if (Scale < 0) 10242 return false; 10243 10244 switch (VT.getSimpleVT().SimpleTy) { 10245 default: return false; 10246 case MVT::i1: 10247 case MVT::i8: 10248 case MVT::i16: 10249 case MVT::i32: 10250 if (Scale == 1) 10251 return true; 10252 // r + r << imm 10253 Scale = Scale & ~1; 10254 return Scale == 2 || Scale == 4 || Scale == 8; 10255 case MVT::i64: 10256 // r + r 10257 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 10258 return true; 10259 return false; 10260 case MVT::isVoid: 10261 // Note, we allow "void" uses (basically, uses that aren't loads or 10262 // stores), because arm allows folding a scale into many arithmetic 10263 // operations. This should be made more precise and revisited later. 10264 10265 // Allow r << imm, but the imm has to be a multiple of two. 10266 if (Scale & 1) return false; 10267 return isPowerOf2_32(Scale); 10268 } 10269 } 10270 10271 /// isLegalAddressingMode - Return true if the addressing mode represented 10272 /// by AM is legal for this target, for a load/store of the specified type. 10273 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM, 10274 Type *Ty, 10275 unsigned AS) const { 10276 EVT VT = getValueType(Ty, true); 10277 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 10278 return false; 10279 10280 // Can never fold addr of global into load/store. 10281 if (AM.BaseGV) 10282 return false; 10283 10284 switch (AM.Scale) { 10285 case 0: // no scale reg, must be "r+i" or "r", or "i". 10286 break; 10287 case 1: 10288 if (Subtarget->isThumb1Only()) 10289 return false; 10290 // FALL THROUGH. 10291 default: 10292 // ARM doesn't support any R+R*scale+imm addr modes. 10293 if (AM.BaseOffs) 10294 return false; 10295 10296 if (!VT.isSimple()) 10297 return false; 10298 10299 if (Subtarget->isThumb2()) 10300 return isLegalT2ScaledAddressingMode(AM, VT); 10301 10302 int Scale = AM.Scale; 10303 switch (VT.getSimpleVT().SimpleTy) { 10304 default: return false; 10305 case MVT::i1: 10306 case MVT::i8: 10307 case MVT::i32: 10308 if (Scale < 0) Scale = -Scale; 10309 if (Scale == 1) 10310 return true; 10311 // r + r << imm 10312 return isPowerOf2_32(Scale & ~1); 10313 case MVT::i16: 10314 case MVT::i64: 10315 // r + r 10316 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 10317 return true; 10318 return false; 10319 10320 case MVT::isVoid: 10321 // Note, we allow "void" uses (basically, uses that aren't loads or 10322 // stores), because arm allows folding a scale into many arithmetic 10323 // operations. This should be made more precise and revisited later. 10324 10325 // Allow r << imm, but the imm has to be a multiple of two. 10326 if (Scale & 1) return false; 10327 return isPowerOf2_32(Scale); 10328 } 10329 } 10330 return true; 10331 } 10332 10333 /// isLegalICmpImmediate - Return true if the specified immediate is legal 10334 /// icmp immediate, that is the target has icmp instructions which can compare 10335 /// a register against the immediate without having to materialize the 10336 /// immediate into a register. 10337 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 10338 // Thumb2 and ARM modes can use cmn for negative immediates. 10339 if (!Subtarget->isThumb()) 10340 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1; 10341 if (Subtarget->isThumb2()) 10342 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1; 10343 // Thumb1 doesn't have cmn, and only 8-bit immediates. 10344 return Imm >= 0 && Imm <= 255; 10345 } 10346 10347 /// isLegalAddImmediate - Return true if the specified immediate is a legal add 10348 /// *or sub* immediate, that is the target has add or sub instructions which can 10349 /// add a register with the immediate without having to materialize the 10350 /// immediate into a register. 10351 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 10352 // Same encoding for add/sub, just flip the sign. 10353 int64_t AbsImm = std::abs(Imm); 10354 if (!Subtarget->isThumb()) 10355 return ARM_AM::getSOImmVal(AbsImm) != -1; 10356 if (Subtarget->isThumb2()) 10357 return ARM_AM::getT2SOImmVal(AbsImm) != -1; 10358 // Thumb1 only has 8-bit unsigned immediate. 10359 return AbsImm >= 0 && AbsImm <= 255; 10360 } 10361 10362 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 10363 bool isSEXTLoad, SDValue &Base, 10364 SDValue &Offset, bool &isInc, 10365 SelectionDAG &DAG) { 10366 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 10367 return false; 10368 10369 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 10370 // AddressingMode 3 10371 Base = Ptr->getOperand(0); 10372 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10373 int RHSC = (int)RHS->getZExtValue(); 10374 if (RHSC < 0 && RHSC > -256) { 10375 assert(Ptr->getOpcode() == ISD::ADD); 10376 isInc = false; 10377 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 10378 return true; 10379 } 10380 } 10381 isInc = (Ptr->getOpcode() == ISD::ADD); 10382 Offset = Ptr->getOperand(1); 10383 return true; 10384 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 10385 // AddressingMode 2 10386 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10387 int RHSC = (int)RHS->getZExtValue(); 10388 if (RHSC < 0 && RHSC > -0x1000) { 10389 assert(Ptr->getOpcode() == ISD::ADD); 10390 isInc = false; 10391 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 10392 Base = Ptr->getOperand(0); 10393 return true; 10394 } 10395 } 10396 10397 if (Ptr->getOpcode() == ISD::ADD) { 10398 isInc = true; 10399 ARM_AM::ShiftOpc ShOpcVal= 10400 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 10401 if (ShOpcVal != ARM_AM::no_shift) { 10402 Base = Ptr->getOperand(1); 10403 Offset = Ptr->getOperand(0); 10404 } else { 10405 Base = Ptr->getOperand(0); 10406 Offset = Ptr->getOperand(1); 10407 } 10408 return true; 10409 } 10410 10411 isInc = (Ptr->getOpcode() == ISD::ADD); 10412 Base = Ptr->getOperand(0); 10413 Offset = Ptr->getOperand(1); 10414 return true; 10415 } 10416 10417 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 10418 return false; 10419 } 10420 10421 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 10422 bool isSEXTLoad, SDValue &Base, 10423 SDValue &Offset, bool &isInc, 10424 SelectionDAG &DAG) { 10425 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 10426 return false; 10427 10428 Base = Ptr->getOperand(0); 10429 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 10430 int RHSC = (int)RHS->getZExtValue(); 10431 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 10432 assert(Ptr->getOpcode() == ISD::ADD); 10433 isInc = false; 10434 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 10435 return true; 10436 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 10437 isInc = Ptr->getOpcode() == ISD::ADD; 10438 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0)); 10439 return true; 10440 } 10441 } 10442 10443 return false; 10444 } 10445 10446 /// getPreIndexedAddressParts - returns true by value, base pointer and 10447 /// offset pointer and addressing mode by reference if the node's address 10448 /// can be legally represented as pre-indexed load / store address. 10449 bool 10450 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 10451 SDValue &Offset, 10452 ISD::MemIndexedMode &AM, 10453 SelectionDAG &DAG) const { 10454 if (Subtarget->isThumb1Only()) 10455 return false; 10456 10457 EVT VT; 10458 SDValue Ptr; 10459 bool isSEXTLoad = false; 10460 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 10461 Ptr = LD->getBasePtr(); 10462 VT = LD->getMemoryVT(); 10463 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 10464 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 10465 Ptr = ST->getBasePtr(); 10466 VT = ST->getMemoryVT(); 10467 } else 10468 return false; 10469 10470 bool isInc; 10471 bool isLegal = false; 10472 if (Subtarget->isThumb2()) 10473 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 10474 Offset, isInc, DAG); 10475 else 10476 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 10477 Offset, isInc, DAG); 10478 if (!isLegal) 10479 return false; 10480 10481 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 10482 return true; 10483 } 10484 10485 /// getPostIndexedAddressParts - returns true by value, base pointer and 10486 /// offset pointer and addressing mode by reference if this node can be 10487 /// combined with a load / store to form a post-indexed load / store. 10488 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 10489 SDValue &Base, 10490 SDValue &Offset, 10491 ISD::MemIndexedMode &AM, 10492 SelectionDAG &DAG) const { 10493 if (Subtarget->isThumb1Only()) 10494 return false; 10495 10496 EVT VT; 10497 SDValue Ptr; 10498 bool isSEXTLoad = false; 10499 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 10500 VT = LD->getMemoryVT(); 10501 Ptr = LD->getBasePtr(); 10502 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 10503 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 10504 VT = ST->getMemoryVT(); 10505 Ptr = ST->getBasePtr(); 10506 } else 10507 return false; 10508 10509 bool isInc; 10510 bool isLegal = false; 10511 if (Subtarget->isThumb2()) 10512 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 10513 isInc, DAG); 10514 else 10515 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 10516 isInc, DAG); 10517 if (!isLegal) 10518 return false; 10519 10520 if (Ptr != Base) { 10521 // Swap base ptr and offset to catch more post-index load / store when 10522 // it's legal. In Thumb2 mode, offset must be an immediate. 10523 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 10524 !Subtarget->isThumb2()) 10525 std::swap(Base, Offset); 10526 10527 // Post-indexed load / store update the base pointer. 10528 if (Ptr != Base) 10529 return false; 10530 } 10531 10532 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 10533 return true; 10534 } 10535 10536 void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 10537 APInt &KnownZero, 10538 APInt &KnownOne, 10539 const SelectionDAG &DAG, 10540 unsigned Depth) const { 10541 unsigned BitWidth = KnownOne.getBitWidth(); 10542 KnownZero = KnownOne = APInt(BitWidth, 0); 10543 switch (Op.getOpcode()) { 10544 default: break; 10545 case ARMISD::ADDC: 10546 case ARMISD::ADDE: 10547 case ARMISD::SUBC: 10548 case ARMISD::SUBE: 10549 // These nodes' second result is a boolean 10550 if (Op.getResNo() == 0) 10551 break; 10552 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 10553 break; 10554 case ARMISD::CMOV: { 10555 // Bits are known zero/one if known on the LHS and RHS. 10556 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 10557 if (KnownZero == 0 && KnownOne == 0) return; 10558 10559 APInt KnownZeroRHS, KnownOneRHS; 10560 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1); 10561 KnownZero &= KnownZeroRHS; 10562 KnownOne &= KnownOneRHS; 10563 return; 10564 } 10565 case ISD::INTRINSIC_W_CHAIN: { 10566 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1)); 10567 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue()); 10568 switch (IntID) { 10569 default: return; 10570 case Intrinsic::arm_ldaex: 10571 case Intrinsic::arm_ldrex: { 10572 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT(); 10573 unsigned MemBits = VT.getScalarType().getSizeInBits(); 10574 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); 10575 return; 10576 } 10577 } 10578 } 10579 } 10580 } 10581 10582 //===----------------------------------------------------------------------===// 10583 // ARM Inline Assembly Support 10584 //===----------------------------------------------------------------------===// 10585 10586 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 10587 // Looking for "rev" which is V6+. 10588 if (!Subtarget->hasV6Ops()) 10589 return false; 10590 10591 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 10592 std::string AsmStr = IA->getAsmString(); 10593 SmallVector<StringRef, 4> AsmPieces; 10594 SplitString(AsmStr, AsmPieces, ";\n"); 10595 10596 switch (AsmPieces.size()) { 10597 default: return false; 10598 case 1: 10599 AsmStr = AsmPieces[0]; 10600 AsmPieces.clear(); 10601 SplitString(AsmStr, AsmPieces, " \t,"); 10602 10603 // rev $0, $1 10604 if (AsmPieces.size() == 3 && 10605 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 10606 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 10607 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 10608 if (Ty && Ty->getBitWidth() == 32) 10609 return IntrinsicLowering::LowerToByteSwap(CI); 10610 } 10611 break; 10612 } 10613 10614 return false; 10615 } 10616 10617 /// getConstraintType - Given a constraint letter, return the type of 10618 /// constraint it is for this target. 10619 ARMTargetLowering::ConstraintType 10620 ARMTargetLowering::getConstraintType(const std::string &Constraint) const { 10621 if (Constraint.size() == 1) { 10622 switch (Constraint[0]) { 10623 default: break; 10624 case 'l': return C_RegisterClass; 10625 case 'w': return C_RegisterClass; 10626 case 'h': return C_RegisterClass; 10627 case 'x': return C_RegisterClass; 10628 case 't': return C_RegisterClass; 10629 case 'j': return C_Other; // Constant for movw. 10630 // An address with a single base register. Due to the way we 10631 // currently handle addresses it is the same as an 'r' memory constraint. 10632 case 'Q': return C_Memory; 10633 } 10634 } else if (Constraint.size() == 2) { 10635 switch (Constraint[0]) { 10636 default: break; 10637 // All 'U+' constraints are addresses. 10638 case 'U': return C_Memory; 10639 } 10640 } 10641 return TargetLowering::getConstraintType(Constraint); 10642 } 10643 10644 /// Examine constraint type and operand type and determine a weight value. 10645 /// This object must already have been set up with the operand type 10646 /// and the current alternative constraint selected. 10647 TargetLowering::ConstraintWeight 10648 ARMTargetLowering::getSingleConstraintMatchWeight( 10649 AsmOperandInfo &info, const char *constraint) const { 10650 ConstraintWeight weight = CW_Invalid; 10651 Value *CallOperandVal = info.CallOperandVal; 10652 // If we don't have a value, we can't do a match, 10653 // but allow it at the lowest weight. 10654 if (!CallOperandVal) 10655 return CW_Default; 10656 Type *type = CallOperandVal->getType(); 10657 // Look at the constraint type. 10658 switch (*constraint) { 10659 default: 10660 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 10661 break; 10662 case 'l': 10663 if (type->isIntegerTy()) { 10664 if (Subtarget->isThumb()) 10665 weight = CW_SpecificReg; 10666 else 10667 weight = CW_Register; 10668 } 10669 break; 10670 case 'w': 10671 if (type->isFloatingPointTy()) 10672 weight = CW_Register; 10673 break; 10674 } 10675 return weight; 10676 } 10677 10678 typedef std::pair<unsigned, const TargetRegisterClass*> RCPair; 10679 RCPair 10680 ARMTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 10681 const std::string &Constraint, 10682 MVT VT) const { 10683 if (Constraint.size() == 1) { 10684 // GCC ARM Constraint Letters 10685 switch (Constraint[0]) { 10686 case 'l': // Low regs or general regs. 10687 if (Subtarget->isThumb()) 10688 return RCPair(0U, &ARM::tGPRRegClass); 10689 return RCPair(0U, &ARM::GPRRegClass); 10690 case 'h': // High regs or no regs. 10691 if (Subtarget->isThumb()) 10692 return RCPair(0U, &ARM::hGPRRegClass); 10693 break; 10694 case 'r': 10695 if (Subtarget->isThumb1Only()) 10696 return RCPair(0U, &ARM::tGPRRegClass); 10697 return RCPair(0U, &ARM::GPRRegClass); 10698 case 'w': 10699 if (VT == MVT::Other) 10700 break; 10701 if (VT == MVT::f32) 10702 return RCPair(0U, &ARM::SPRRegClass); 10703 if (VT.getSizeInBits() == 64) 10704 return RCPair(0U, &ARM::DPRRegClass); 10705 if (VT.getSizeInBits() == 128) 10706 return RCPair(0U, &ARM::QPRRegClass); 10707 break; 10708 case 'x': 10709 if (VT == MVT::Other) 10710 break; 10711 if (VT == MVT::f32) 10712 return RCPair(0U, &ARM::SPR_8RegClass); 10713 if (VT.getSizeInBits() == 64) 10714 return RCPair(0U, &ARM::DPR_8RegClass); 10715 if (VT.getSizeInBits() == 128) 10716 return RCPair(0U, &ARM::QPR_8RegClass); 10717 break; 10718 case 't': 10719 if (VT == MVT::f32) 10720 return RCPair(0U, &ARM::SPRRegClass); 10721 break; 10722 } 10723 } 10724 if (StringRef("{cc}").equals_lower(Constraint)) 10725 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass); 10726 10727 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 10728 } 10729 10730 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 10731 /// vector. If it is invalid, don't add anything to Ops. 10732 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 10733 std::string &Constraint, 10734 std::vector<SDValue>&Ops, 10735 SelectionDAG &DAG) const { 10736 SDValue Result; 10737 10738 // Currently only support length 1 constraints. 10739 if (Constraint.length() != 1) return; 10740 10741 char ConstraintLetter = Constraint[0]; 10742 switch (ConstraintLetter) { 10743 default: break; 10744 case 'j': 10745 case 'I': case 'J': case 'K': case 'L': 10746 case 'M': case 'N': case 'O': 10747 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 10748 if (!C) 10749 return; 10750 10751 int64_t CVal64 = C->getSExtValue(); 10752 int CVal = (int) CVal64; 10753 // None of these constraints allow values larger than 32 bits. Check 10754 // that the value fits in an int. 10755 if (CVal != CVal64) 10756 return; 10757 10758 switch (ConstraintLetter) { 10759 case 'j': 10760 // Constant suitable for movw, must be between 0 and 10761 // 65535. 10762 if (Subtarget->hasV6T2Ops()) 10763 if (CVal >= 0 && CVal <= 65535) 10764 break; 10765 return; 10766 case 'I': 10767 if (Subtarget->isThumb1Only()) { 10768 // This must be a constant between 0 and 255, for ADD 10769 // immediates. 10770 if (CVal >= 0 && CVal <= 255) 10771 break; 10772 } else if (Subtarget->isThumb2()) { 10773 // A constant that can be used as an immediate value in a 10774 // data-processing instruction. 10775 if (ARM_AM::getT2SOImmVal(CVal) != -1) 10776 break; 10777 } else { 10778 // A constant that can be used as an immediate value in a 10779 // data-processing instruction. 10780 if (ARM_AM::getSOImmVal(CVal) != -1) 10781 break; 10782 } 10783 return; 10784 10785 case 'J': 10786 if (Subtarget->isThumb()) { // FIXME thumb2 10787 // This must be a constant between -255 and -1, for negated ADD 10788 // immediates. This can be used in GCC with an "n" modifier that 10789 // prints the negated value, for use with SUB instructions. It is 10790 // not useful otherwise but is implemented for compatibility. 10791 if (CVal >= -255 && CVal <= -1) 10792 break; 10793 } else { 10794 // This must be a constant between -4095 and 4095. It is not clear 10795 // what this constraint is intended for. Implemented for 10796 // compatibility with GCC. 10797 if (CVal >= -4095 && CVal <= 4095) 10798 break; 10799 } 10800 return; 10801 10802 case 'K': 10803 if (Subtarget->isThumb1Only()) { 10804 // A 32-bit value where only one byte has a nonzero value. Exclude 10805 // zero to match GCC. This constraint is used by GCC internally for 10806 // constants that can be loaded with a move/shift combination. 10807 // It is not useful otherwise but is implemented for compatibility. 10808 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 10809 break; 10810 } else if (Subtarget->isThumb2()) { 10811 // A constant whose bitwise inverse can be used as an immediate 10812 // value in a data-processing instruction. This can be used in GCC 10813 // with a "B" modifier that prints the inverted value, for use with 10814 // BIC and MVN instructions. It is not useful otherwise but is 10815 // implemented for compatibility. 10816 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 10817 break; 10818 } else { 10819 // A constant whose bitwise inverse can be used as an immediate 10820 // value in a data-processing instruction. This can be used in GCC 10821 // with a "B" modifier that prints the inverted value, for use with 10822 // BIC and MVN instructions. It is not useful otherwise but is 10823 // implemented for compatibility. 10824 if (ARM_AM::getSOImmVal(~CVal) != -1) 10825 break; 10826 } 10827 return; 10828 10829 case 'L': 10830 if (Subtarget->isThumb1Only()) { 10831 // This must be a constant between -7 and 7, 10832 // for 3-operand ADD/SUB immediate instructions. 10833 if (CVal >= -7 && CVal < 7) 10834 break; 10835 } else if (Subtarget->isThumb2()) { 10836 // A constant whose negation can be used as an immediate value in a 10837 // data-processing instruction. This can be used in GCC with an "n" 10838 // modifier that prints the negated value, for use with SUB 10839 // instructions. It is not useful otherwise but is implemented for 10840 // compatibility. 10841 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 10842 break; 10843 } else { 10844 // A constant whose negation can be used as an immediate value in a 10845 // data-processing instruction. This can be used in GCC with an "n" 10846 // modifier that prints the negated value, for use with SUB 10847 // instructions. It is not useful otherwise but is implemented for 10848 // compatibility. 10849 if (ARM_AM::getSOImmVal(-CVal) != -1) 10850 break; 10851 } 10852 return; 10853 10854 case 'M': 10855 if (Subtarget->isThumb()) { // FIXME thumb2 10856 // This must be a multiple of 4 between 0 and 1020, for 10857 // ADD sp + immediate. 10858 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 10859 break; 10860 } else { 10861 // A power of two or a constant between 0 and 32. This is used in 10862 // GCC for the shift amount on shifted register operands, but it is 10863 // useful in general for any shift amounts. 10864 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 10865 break; 10866 } 10867 return; 10868 10869 case 'N': 10870 if (Subtarget->isThumb()) { // FIXME thumb2 10871 // This must be a constant between 0 and 31, for shift amounts. 10872 if (CVal >= 0 && CVal <= 31) 10873 break; 10874 } 10875 return; 10876 10877 case 'O': 10878 if (Subtarget->isThumb()) { // FIXME thumb2 10879 // This must be a multiple of 4 between -508 and 508, for 10880 // ADD/SUB sp = sp + immediate. 10881 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 10882 break; 10883 } 10884 return; 10885 } 10886 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType()); 10887 break; 10888 } 10889 10890 if (Result.getNode()) { 10891 Ops.push_back(Result); 10892 return; 10893 } 10894 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 10895 } 10896 10897 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const { 10898 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only"); 10899 unsigned Opcode = Op->getOpcode(); 10900 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) && 10901 "Invalid opcode for Div/Rem lowering"); 10902 bool isSigned = (Opcode == ISD::SDIVREM); 10903 EVT VT = Op->getValueType(0); 10904 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 10905 10906 RTLIB::Libcall LC; 10907 switch (VT.getSimpleVT().SimpleTy) { 10908 default: llvm_unreachable("Unexpected request for libcall!"); 10909 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 10910 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 10911 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 10912 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 10913 } 10914 10915 SDValue InChain = DAG.getEntryNode(); 10916 10917 TargetLowering::ArgListTy Args; 10918 TargetLowering::ArgListEntry Entry; 10919 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) { 10920 EVT ArgVT = Op->getOperand(i).getValueType(); 10921 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 10922 Entry.Node = Op->getOperand(i); 10923 Entry.Ty = ArgTy; 10924 Entry.isSExt = isSigned; 10925 Entry.isZExt = !isSigned; 10926 Args.push_back(Entry); 10927 } 10928 10929 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 10930 getPointerTy()); 10931 10932 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr); 10933 10934 SDLoc dl(Op); 10935 TargetLowering::CallLoweringInfo CLI(DAG); 10936 CLI.setDebugLoc(dl).setChain(InChain) 10937 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0) 10938 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned); 10939 10940 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 10941 return CallInfo.first; 10942 } 10943 10944 SDValue 10945 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { 10946 assert(Subtarget->isTargetWindows() && "unsupported target platform"); 10947 SDLoc DL(Op); 10948 10949 // Get the inputs. 10950 SDValue Chain = Op.getOperand(0); 10951 SDValue Size = Op.getOperand(1); 10952 10953 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size, 10954 DAG.getConstant(2, DL, MVT::i32)); 10955 10956 SDValue Flag; 10957 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag); 10958 Flag = Chain.getValue(1); 10959 10960 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 10961 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag); 10962 10963 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32); 10964 Chain = NewSP.getValue(1); 10965 10966 SDValue Ops[2] = { NewSP, Chain }; 10967 return DAG.getMergeValues(Ops, DL); 10968 } 10969 10970 SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { 10971 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() && 10972 "Unexpected type for custom-lowering FP_EXTEND"); 10973 10974 RTLIB::Libcall LC; 10975 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType()); 10976 10977 SDValue SrcVal = Op.getOperand(0); 10978 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1, 10979 /*isSigned*/ false, SDLoc(Op)).first; 10980 } 10981 10982 SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 10983 assert(Op.getOperand(0).getValueType() == MVT::f64 && 10984 Subtarget->isFPOnlySP() && 10985 "Unexpected type for custom-lowering FP_ROUND"); 10986 10987 RTLIB::Libcall LC; 10988 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType()); 10989 10990 SDValue SrcVal = Op.getOperand(0); 10991 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1, 10992 /*isSigned*/ false, SDLoc(Op)).first; 10993 } 10994 10995 bool 10996 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 10997 // The ARM target isn't yet aware of offsets. 10998 return false; 10999 } 11000 11001 bool ARM::isBitFieldInvertedMask(unsigned v) { 11002 if (v == 0xffffffff) 11003 return false; 11004 11005 // there can be 1's on either or both "outsides", all the "inside" 11006 // bits must be 0's 11007 return isShiftedMask_32(~v); 11008 } 11009 11010 /// isFPImmLegal - Returns true if the target can instruction select the 11011 /// specified FP immediate natively. If false, the legalizer will 11012 /// materialize the FP immediate as a load from a constant pool. 11013 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 11014 if (!Subtarget->hasVFP3()) 11015 return false; 11016 if (VT == MVT::f32) 11017 return ARM_AM::getFP32Imm(Imm) != -1; 11018 if (VT == MVT::f64 && !Subtarget->isFPOnlySP()) 11019 return ARM_AM::getFP64Imm(Imm) != -1; 11020 return false; 11021 } 11022 11023 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 11024 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 11025 /// specified in the intrinsic calls. 11026 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 11027 const CallInst &I, 11028 unsigned Intrinsic) const { 11029 switch (Intrinsic) { 11030 case Intrinsic::arm_neon_vld1: 11031 case Intrinsic::arm_neon_vld2: 11032 case Intrinsic::arm_neon_vld3: 11033 case Intrinsic::arm_neon_vld4: 11034 case Intrinsic::arm_neon_vld2lane: 11035 case Intrinsic::arm_neon_vld3lane: 11036 case Intrinsic::arm_neon_vld4lane: { 11037 Info.opc = ISD::INTRINSIC_W_CHAIN; 11038 // Conservatively set memVT to the entire set of vectors loaded. 11039 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8; 11040 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 11041 Info.ptrVal = I.getArgOperand(0); 11042 Info.offset = 0; 11043 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 11044 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 11045 Info.vol = false; // volatile loads with NEON intrinsics not supported 11046 Info.readMem = true; 11047 Info.writeMem = false; 11048 return true; 11049 } 11050 case Intrinsic::arm_neon_vst1: 11051 case Intrinsic::arm_neon_vst2: 11052 case Intrinsic::arm_neon_vst3: 11053 case Intrinsic::arm_neon_vst4: 11054 case Intrinsic::arm_neon_vst2lane: 11055 case Intrinsic::arm_neon_vst3lane: 11056 case Intrinsic::arm_neon_vst4lane: { 11057 Info.opc = ISD::INTRINSIC_VOID; 11058 // Conservatively set memVT to the entire set of vectors stored. 11059 unsigned NumElts = 0; 11060 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 11061 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 11062 if (!ArgTy->isVectorTy()) 11063 break; 11064 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8; 11065 } 11066 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 11067 Info.ptrVal = I.getArgOperand(0); 11068 Info.offset = 0; 11069 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 11070 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 11071 Info.vol = false; // volatile stores with NEON intrinsics not supported 11072 Info.readMem = false; 11073 Info.writeMem = true; 11074 return true; 11075 } 11076 case Intrinsic::arm_ldaex: 11077 case Intrinsic::arm_ldrex: { 11078 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); 11079 Info.opc = ISD::INTRINSIC_W_CHAIN; 11080 Info.memVT = MVT::getVT(PtrTy->getElementType()); 11081 Info.ptrVal = I.getArgOperand(0); 11082 Info.offset = 0; 11083 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType()); 11084 Info.vol = true; 11085 Info.readMem = true; 11086 Info.writeMem = false; 11087 return true; 11088 } 11089 case Intrinsic::arm_stlex: 11090 case Intrinsic::arm_strex: { 11091 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType()); 11092 Info.opc = ISD::INTRINSIC_W_CHAIN; 11093 Info.memVT = MVT::getVT(PtrTy->getElementType()); 11094 Info.ptrVal = I.getArgOperand(1); 11095 Info.offset = 0; 11096 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType()); 11097 Info.vol = true; 11098 Info.readMem = false; 11099 Info.writeMem = true; 11100 return true; 11101 } 11102 case Intrinsic::arm_stlexd: 11103 case Intrinsic::arm_strexd: { 11104 Info.opc = ISD::INTRINSIC_W_CHAIN; 11105 Info.memVT = MVT::i64; 11106 Info.ptrVal = I.getArgOperand(2); 11107 Info.offset = 0; 11108 Info.align = 8; 11109 Info.vol = true; 11110 Info.readMem = false; 11111 Info.writeMem = true; 11112 return true; 11113 } 11114 case Intrinsic::arm_ldaexd: 11115 case Intrinsic::arm_ldrexd: { 11116 Info.opc = ISD::INTRINSIC_W_CHAIN; 11117 Info.memVT = MVT::i64; 11118 Info.ptrVal = I.getArgOperand(0); 11119 Info.offset = 0; 11120 Info.align = 8; 11121 Info.vol = true; 11122 Info.readMem = true; 11123 Info.writeMem = false; 11124 return true; 11125 } 11126 default: 11127 break; 11128 } 11129 11130 return false; 11131 } 11132 11133 /// \brief Returns true if it is beneficial to convert a load of a constant 11134 /// to just the constant itself. 11135 bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 11136 Type *Ty) const { 11137 assert(Ty->isIntegerTy()); 11138 11139 unsigned Bits = Ty->getPrimitiveSizeInBits(); 11140 if (Bits == 0 || Bits > 32) 11141 return false; 11142 return true; 11143 } 11144 11145 bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; } 11146 11147 Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder, 11148 ARM_MB::MemBOpt Domain) const { 11149 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 11150 11151 // First, if the target has no DMB, see what fallback we can use. 11152 if (!Subtarget->hasDataBarrier()) { 11153 // Some ARMv6 cpus can support data barriers with an mcr instruction. 11154 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 11155 // here. 11156 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) { 11157 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr); 11158 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0), 11159 Builder.getInt32(0), Builder.getInt32(7), 11160 Builder.getInt32(10), Builder.getInt32(5)}; 11161 return Builder.CreateCall(MCR, args); 11162 } else { 11163 // Instead of using barriers, atomic accesses on these subtargets use 11164 // libcalls. 11165 llvm_unreachable("makeDMB on a target so old that it has no barriers"); 11166 } 11167 } else { 11168 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb); 11169 // Only a full system barrier exists in the M-class architectures. 11170 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain; 11171 Constant *CDomain = Builder.getInt32(Domain); 11172 return Builder.CreateCall(DMB, CDomain); 11173 } 11174 } 11175 11176 // Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html 11177 Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder, 11178 AtomicOrdering Ord, bool IsStore, 11179 bool IsLoad) const { 11180 if (!getInsertFencesForAtomic()) 11181 return nullptr; 11182 11183 switch (Ord) { 11184 case NotAtomic: 11185 case Unordered: 11186 llvm_unreachable("Invalid fence: unordered/non-atomic"); 11187 case Monotonic: 11188 case Acquire: 11189 return nullptr; // Nothing to do 11190 case SequentiallyConsistent: 11191 if (!IsStore) 11192 return nullptr; // Nothing to do 11193 /*FALLTHROUGH*/ 11194 case Release: 11195 case AcquireRelease: 11196 if (Subtarget->isSwift()) 11197 return makeDMB(Builder, ARM_MB::ISHST); 11198 // FIXME: add a comment with a link to documentation justifying this. 11199 else 11200 return makeDMB(Builder, ARM_MB::ISH); 11201 } 11202 llvm_unreachable("Unknown fence ordering in emitLeadingFence"); 11203 } 11204 11205 Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder, 11206 AtomicOrdering Ord, bool IsStore, 11207 bool IsLoad) const { 11208 if (!getInsertFencesForAtomic()) 11209 return nullptr; 11210 11211 switch (Ord) { 11212 case NotAtomic: 11213 case Unordered: 11214 llvm_unreachable("Invalid fence: unordered/not-atomic"); 11215 case Monotonic: 11216 case Release: 11217 return nullptr; // Nothing to do 11218 case Acquire: 11219 case AcquireRelease: 11220 case SequentiallyConsistent: 11221 return makeDMB(Builder, ARM_MB::ISH); 11222 } 11223 llvm_unreachable("Unknown fence ordering in emitTrailingFence"); 11224 } 11225 11226 // Loads and stores less than 64-bits are already atomic; ones above that 11227 // are doomed anyway, so defer to the default libcall and blame the OS when 11228 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 11229 // anything for those. 11230 bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const { 11231 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits(); 11232 return (Size == 64) && !Subtarget->isMClass(); 11233 } 11234 11235 // Loads and stores less than 64-bits are already atomic; ones above that 11236 // are doomed anyway, so defer to the default libcall and blame the OS when 11237 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 11238 // anything for those. 11239 // FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that 11240 // guarantee, see DDI0406C ARM architecture reference manual, 11241 // sections A8.8.72-74 LDRD) 11242 bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const { 11243 unsigned Size = LI->getType()->getPrimitiveSizeInBits(); 11244 return (Size == 64) && !Subtarget->isMClass(); 11245 } 11246 11247 // For the real atomic operations, we have ldrex/strex up to 32 bits, 11248 // and up to 64 bits on the non-M profiles 11249 TargetLoweringBase::AtomicRMWExpansionKind 11250 ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 11251 unsigned Size = AI->getType()->getPrimitiveSizeInBits(); 11252 return (Size <= (Subtarget->isMClass() ? 32U : 64U)) 11253 ? AtomicRMWExpansionKind::LLSC 11254 : AtomicRMWExpansionKind::None; 11255 } 11256 11257 // This has so far only been implemented for MachO. 11258 bool ARMTargetLowering::useLoadStackGuardNode() const { 11259 return Subtarget->isTargetMachO(); 11260 } 11261 11262 bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx, 11263 unsigned &Cost) const { 11264 // If we do not have NEON, vector types are not natively supported. 11265 if (!Subtarget->hasNEON()) 11266 return false; 11267 11268 // Floating point values and vector values map to the same register file. 11269 // Therefore, althought we could do a store extract of a vector type, this is 11270 // better to leave at float as we have more freedom in the addressing mode for 11271 // those. 11272 if (VectorTy->isFPOrFPVectorTy()) 11273 return false; 11274 11275 // If the index is unknown at compile time, this is very expensive to lower 11276 // and it is not possible to combine the store with the extract. 11277 if (!isa<ConstantInt>(Idx)) 11278 return false; 11279 11280 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type"); 11281 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth(); 11282 // We can do a store + vector extract on any vector that fits perfectly in a D 11283 // or Q register. 11284 if (BitWidth == 64 || BitWidth == 128) { 11285 Cost = 0; 11286 return true; 11287 } 11288 return false; 11289 } 11290 11291 Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr, 11292 AtomicOrdering Ord) const { 11293 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 11294 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType(); 11295 bool IsAcquire = isAtLeastAcquire(Ord); 11296 11297 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd 11298 // intrinsic must return {i32, i32} and we have to recombine them into a 11299 // single i64 here. 11300 if (ValTy->getPrimitiveSizeInBits() == 64) { 11301 Intrinsic::ID Int = 11302 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd; 11303 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int); 11304 11305 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 11306 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi"); 11307 11308 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo"); 11309 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi"); 11310 if (!Subtarget->isLittle()) 11311 std::swap (Lo, Hi); 11312 Lo = Builder.CreateZExt(Lo, ValTy, "lo64"); 11313 Hi = Builder.CreateZExt(Hi, ValTy, "hi64"); 11314 return Builder.CreateOr( 11315 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64"); 11316 } 11317 11318 Type *Tys[] = { Addr->getType() }; 11319 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex; 11320 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys); 11321 11322 return Builder.CreateTruncOrBitCast( 11323 Builder.CreateCall(Ldrex, Addr), 11324 cast<PointerType>(Addr->getType())->getElementType()); 11325 } 11326 11327 Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val, 11328 Value *Addr, 11329 AtomicOrdering Ord) const { 11330 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 11331 bool IsRelease = isAtLeastRelease(Ord); 11332 11333 // Since the intrinsics must have legal type, the i64 intrinsics take two 11334 // parameters: "i32, i32". We must marshal Val into the appropriate form 11335 // before the call. 11336 if (Val->getType()->getPrimitiveSizeInBits() == 64) { 11337 Intrinsic::ID Int = 11338 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd; 11339 Function *Strex = Intrinsic::getDeclaration(M, Int); 11340 Type *Int32Ty = Type::getInt32Ty(M->getContext()); 11341 11342 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo"); 11343 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi"); 11344 if (!Subtarget->isLittle()) 11345 std::swap (Lo, Hi); 11346 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 11347 return Builder.CreateCall(Strex, {Lo, Hi, Addr}); 11348 } 11349 11350 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex; 11351 Type *Tys[] = { Addr->getType() }; 11352 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys); 11353 11354 return Builder.CreateCall( 11355 Strex, {Builder.CreateZExtOrBitCast( 11356 Val, Strex->getFunctionType()->getParamType(0)), 11357 Addr}); 11358 } 11359 11360 enum HABaseType { 11361 HA_UNKNOWN = 0, 11362 HA_FLOAT, 11363 HA_DOUBLE, 11364 HA_VECT64, 11365 HA_VECT128 11366 }; 11367 11368 static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base, 11369 uint64_t &Members) { 11370 if (const StructType *ST = dyn_cast<StructType>(Ty)) { 11371 for (unsigned i = 0; i < ST->getNumElements(); ++i) { 11372 uint64_t SubMembers = 0; 11373 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers)) 11374 return false; 11375 Members += SubMembers; 11376 } 11377 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { 11378 uint64_t SubMembers = 0; 11379 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers)) 11380 return false; 11381 Members += SubMembers * AT->getNumElements(); 11382 } else if (Ty->isFloatTy()) { 11383 if (Base != HA_UNKNOWN && Base != HA_FLOAT) 11384 return false; 11385 Members = 1; 11386 Base = HA_FLOAT; 11387 } else if (Ty->isDoubleTy()) { 11388 if (Base != HA_UNKNOWN && Base != HA_DOUBLE) 11389 return false; 11390 Members = 1; 11391 Base = HA_DOUBLE; 11392 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) { 11393 Members = 1; 11394 switch (Base) { 11395 case HA_FLOAT: 11396 case HA_DOUBLE: 11397 return false; 11398 case HA_VECT64: 11399 return VT->getBitWidth() == 64; 11400 case HA_VECT128: 11401 return VT->getBitWidth() == 128; 11402 case HA_UNKNOWN: 11403 switch (VT->getBitWidth()) { 11404 case 64: 11405 Base = HA_VECT64; 11406 return true; 11407 case 128: 11408 Base = HA_VECT128; 11409 return true; 11410 default: 11411 return false; 11412 } 11413 } 11414 } 11415 11416 return (Members > 0 && Members <= 4); 11417 } 11418 11419 /// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of 11420 /// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when 11421 /// passing according to AAPCS rules. 11422 bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters( 11423 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { 11424 if (getEffectiveCallingConv(CallConv, isVarArg) != 11425 CallingConv::ARM_AAPCS_VFP) 11426 return false; 11427 11428 HABaseType Base = HA_UNKNOWN; 11429 uint64_t Members = 0; 11430 bool IsHA = isHomogeneousAggregate(Ty, Base, Members); 11431 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump()); 11432 11433 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy(); 11434 return IsHA || IsIntArray; 11435 } 11436