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 static cl::opt<bool> 64 ARMInterworking("arm-interworking", cl::Hidden, 65 cl::desc("Enable / disable ARM interworking (for debugging only)"), 66 cl::init(true)); 67 68 namespace { 69 class ARMCCState : public CCState { 70 public: 71 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF, 72 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C, 73 ParmContext PC) 74 : CCState(CC, isVarArg, MF, locs, C) { 75 assert(((PC == Call) || (PC == Prologue)) && 76 "ARMCCState users must specify whether their context is call" 77 "or prologue generation."); 78 CallOrPrologue = PC; 79 } 80 }; 81 } 82 83 // The APCS parameter registers. 84 static const MCPhysReg GPRArgRegs[] = { 85 ARM::R0, ARM::R1, ARM::R2, ARM::R3 86 }; 87 88 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT, 89 MVT PromotedBitwiseVT) { 90 if (VT != PromotedLdStVT) { 91 setOperationAction(ISD::LOAD, VT, Promote); 92 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT); 93 94 setOperationAction(ISD::STORE, VT, Promote); 95 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT); 96 } 97 98 MVT ElemTy = VT.getVectorElementType(); 99 if (ElemTy != MVT::i64 && ElemTy != MVT::f64) 100 setOperationAction(ISD::SETCC, VT, Custom); 101 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); 102 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); 103 if (ElemTy == MVT::i32) { 104 setOperationAction(ISD::SINT_TO_FP, VT, Custom); 105 setOperationAction(ISD::UINT_TO_FP, VT, Custom); 106 setOperationAction(ISD::FP_TO_SINT, VT, Custom); 107 setOperationAction(ISD::FP_TO_UINT, VT, Custom); 108 } else { 109 setOperationAction(ISD::SINT_TO_FP, VT, Expand); 110 setOperationAction(ISD::UINT_TO_FP, VT, Expand); 111 setOperationAction(ISD::FP_TO_SINT, VT, Expand); 112 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 113 } 114 setOperationAction(ISD::BUILD_VECTOR, VT, Custom); 115 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); 116 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal); 117 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); 118 setOperationAction(ISD::SELECT, VT, Expand); 119 setOperationAction(ISD::SELECT_CC, VT, Expand); 120 setOperationAction(ISD::VSELECT, VT, Expand); 121 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 122 if (VT.isInteger()) { 123 setOperationAction(ISD::SHL, VT, Custom); 124 setOperationAction(ISD::SRA, VT, Custom); 125 setOperationAction(ISD::SRL, VT, Custom); 126 } 127 128 // Promote all bit-wise operations. 129 if (VT.isInteger() && VT != PromotedBitwiseVT) { 130 setOperationAction(ISD::AND, VT, Promote); 131 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT); 132 setOperationAction(ISD::OR, VT, Promote); 133 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT); 134 setOperationAction(ISD::XOR, VT, Promote); 135 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT); 136 } 137 138 // Neon does not support vector divide/remainder operations. 139 setOperationAction(ISD::SDIV, VT, Expand); 140 setOperationAction(ISD::UDIV, VT, Expand); 141 setOperationAction(ISD::FDIV, VT, Expand); 142 setOperationAction(ISD::SREM, VT, Expand); 143 setOperationAction(ISD::UREM, VT, Expand); 144 setOperationAction(ISD::FREM, VT, Expand); 145 146 if (!VT.isFloatingPoint() && 147 VT != MVT::v2i64 && VT != MVT::v1i64) 148 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX}) 149 setOperationAction(Opcode, VT, Legal); 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 static const struct { 175 const RTLIB::Libcall Op; 176 const char * const Name; 177 const ISD::CondCode Cond; 178 } LibraryCalls[] = { 179 // Single-precision floating-point arithmetic. 180 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID }, 181 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID }, 182 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID }, 183 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID }, 184 185 // Double-precision floating-point arithmetic. 186 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID }, 187 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID }, 188 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID }, 189 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID }, 190 191 // Single-precision comparisons. 192 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE }, 193 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE }, 194 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE }, 195 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE }, 196 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE }, 197 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE }, 198 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE }, 199 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ }, 200 201 // Double-precision comparisons. 202 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE }, 203 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE }, 204 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE }, 205 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE }, 206 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE }, 207 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE }, 208 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE }, 209 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ }, 210 211 // Floating-point to integer conversions. 212 // i64 conversions are done via library routines even when generating VFP 213 // instructions, so use the same ones. 214 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID }, 215 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID }, 216 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID }, 217 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID }, 218 219 // Conversions between floating types. 220 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID }, 221 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID }, 222 223 // Integer to floating-point conversions. 224 // i64 conversions are done via library routines even when generating VFP 225 // instructions, so use the same ones. 226 // FIXME: There appears to be some naming inconsistency in ARM libgcc: 227 // e.g., __floatunsidf vs. __floatunssidfvfp. 228 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID }, 229 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID }, 230 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID }, 231 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID }, 232 }; 233 234 for (const auto &LC : LibraryCalls) { 235 setLibcallName(LC.Op, LC.Name); 236 if (LC.Cond != ISD::SETCC_INVALID) 237 setCmpLibcallCC(LC.Op, LC.Cond); 238 } 239 } 240 241 // Set the correct calling convention for ARMv7k WatchOS. It's just 242 // AAPCS_VFP for functions as simple as libcalls. 243 if (Subtarget->isTargetWatchABI()) { 244 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) 245 setLibcallCallingConv((RTLIB::Libcall)i, CallingConv::ARM_AAPCS_VFP); 246 } 247 } 248 249 // These libcalls are not available in 32-bit. 250 setLibcallName(RTLIB::SHL_I128, nullptr); 251 setLibcallName(RTLIB::SRL_I128, nullptr); 252 setLibcallName(RTLIB::SRA_I128, nullptr); 253 254 // RTLIB 255 if (Subtarget->isAAPCS_ABI() && 256 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() || 257 Subtarget->isTargetAndroid())) { 258 static const struct { 259 const RTLIB::Libcall Op; 260 const char * const Name; 261 const CallingConv::ID CC; 262 const ISD::CondCode Cond; 263 } LibraryCalls[] = { 264 // Double-precision floating-point arithmetic helper functions 265 // RTABI chapter 4.1.2, Table 2 266 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 267 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 268 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 269 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 270 271 // Double-precision floating-point comparison helper functions 272 // RTABI chapter 4.1.2, Table 3 273 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 274 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 275 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 276 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 277 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 278 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 279 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 280 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 281 282 // Single-precision floating-point arithmetic helper functions 283 // RTABI chapter 4.1.2, Table 4 284 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 285 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 286 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 287 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 288 289 // Single-precision floating-point comparison helper functions 290 // RTABI chapter 4.1.2, Table 5 291 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE }, 292 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ }, 293 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE }, 294 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE }, 295 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE }, 296 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE }, 297 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE }, 298 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ }, 299 300 // Floating-point to integer conversions. 301 // RTABI chapter 4.1.2, Table 6 302 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 303 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 304 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 305 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 306 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 307 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 308 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 309 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 310 311 // Conversions between floating types. 312 // RTABI chapter 4.1.2, Table 7 313 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 314 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 315 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 316 317 // Integer to floating-point conversions. 318 // RTABI chapter 4.1.2, Table 8 319 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 320 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 321 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 322 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 323 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 324 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 325 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 326 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 327 328 // Long long helper functions 329 // RTABI chapter 4.2, Table 9 330 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 331 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 332 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 333 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 334 335 // Integer division functions 336 // RTABI chapter 4.3.1 337 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 338 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 339 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 340 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 341 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 342 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 343 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 344 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 345 }; 346 347 for (const auto &LC : LibraryCalls) { 348 setLibcallName(LC.Op, LC.Name); 349 setLibcallCallingConv(LC.Op, LC.CC); 350 if (LC.Cond != ISD::SETCC_INVALID) 351 setCmpLibcallCC(LC.Op, LC.Cond); 352 } 353 354 // EABI dependent RTLIB 355 if (TM.Options.EABIVersion == EABI::EABI4 || 356 TM.Options.EABIVersion == EABI::EABI5) { 357 static const struct { 358 const RTLIB::Libcall Op; 359 const char *const Name; 360 const CallingConv::ID CC; 361 const ISD::CondCode Cond; 362 } MemOpsLibraryCalls[] = { 363 // Memory operations 364 // RTABI chapter 4.3.4 365 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 366 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 367 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID }, 368 }; 369 370 for (const auto &LC : MemOpsLibraryCalls) { 371 setLibcallName(LC.Op, LC.Name); 372 setLibcallCallingConv(LC.Op, LC.CC); 373 if (LC.Cond != ISD::SETCC_INVALID) 374 setCmpLibcallCC(LC.Op, LC.Cond); 375 } 376 } 377 } 378 379 if (Subtarget->isTargetWindows()) { 380 static const struct { 381 const RTLIB::Libcall Op; 382 const char * const Name; 383 const CallingConv::ID CC; 384 } LibraryCalls[] = { 385 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP }, 386 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP }, 387 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP }, 388 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP }, 389 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP }, 390 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP }, 391 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP }, 392 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP }, 393 }; 394 395 for (const auto &LC : LibraryCalls) { 396 setLibcallName(LC.Op, LC.Name); 397 setLibcallCallingConv(LC.Op, LC.CC); 398 } 399 } 400 401 // Use divmod compiler-rt calls for iOS 5.0 and later. 402 if (Subtarget->isTargetWatchOS() || 403 (Subtarget->isTargetIOS() && 404 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) { 405 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4"); 406 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4"); 407 } 408 409 // The half <-> float conversion functions are always soft-float, but are 410 // needed for some targets which use a hard-float calling convention by 411 // default. 412 if (Subtarget->isAAPCS_ABI()) { 413 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS); 414 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS); 415 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS); 416 } else { 417 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS); 418 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS); 419 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS); 420 } 421 422 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have 423 // a __gnu_ prefix (which is the default). 424 if (Subtarget->isTargetAEABI()) { 425 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h"); 426 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h"); 427 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f"); 428 } 429 430 if (Subtarget->isThumb1Only()) 431 addRegisterClass(MVT::i32, &ARM::tGPRRegClass); 432 else 433 addRegisterClass(MVT::i32, &ARM::GPRRegClass); 434 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 435 !Subtarget->isThumb1Only()) { 436 addRegisterClass(MVT::f32, &ARM::SPRRegClass); 437 addRegisterClass(MVT::f64, &ARM::DPRRegClass); 438 } 439 440 for (MVT VT : MVT::vector_valuetypes()) { 441 for (MVT InnerVT : MVT::vector_valuetypes()) { 442 setTruncStoreAction(VT, InnerVT, Expand); 443 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); 444 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); 445 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); 446 } 447 448 setOperationAction(ISD::MULHS, VT, Expand); 449 setOperationAction(ISD::SMUL_LOHI, VT, Expand); 450 setOperationAction(ISD::MULHU, VT, Expand); 451 setOperationAction(ISD::UMUL_LOHI, VT, Expand); 452 453 setOperationAction(ISD::BSWAP, VT, Expand); 454 } 455 456 setOperationAction(ISD::ConstantFP, MVT::f32, Custom); 457 setOperationAction(ISD::ConstantFP, MVT::f64, Custom); 458 459 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom); 460 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom); 461 462 if (Subtarget->hasNEON()) { 463 addDRTypeForNEON(MVT::v2f32); 464 addDRTypeForNEON(MVT::v8i8); 465 addDRTypeForNEON(MVT::v4i16); 466 addDRTypeForNEON(MVT::v2i32); 467 addDRTypeForNEON(MVT::v1i64); 468 469 addQRTypeForNEON(MVT::v4f32); 470 addQRTypeForNEON(MVT::v2f64); 471 addQRTypeForNEON(MVT::v16i8); 472 addQRTypeForNEON(MVT::v8i16); 473 addQRTypeForNEON(MVT::v4i32); 474 addQRTypeForNEON(MVT::v2i64); 475 476 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but 477 // neither Neon nor VFP support any arithmetic operations on it. 478 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively 479 // supported for v4f32. 480 setOperationAction(ISD::FADD, MVT::v2f64, Expand); 481 setOperationAction(ISD::FSUB, MVT::v2f64, Expand); 482 setOperationAction(ISD::FMUL, MVT::v2f64, Expand); 483 // FIXME: Code duplication: FDIV and FREM are expanded always, see 484 // ARMTargetLowering::addTypeForNEON method for details. 485 setOperationAction(ISD::FDIV, MVT::v2f64, Expand); 486 setOperationAction(ISD::FREM, MVT::v2f64, Expand); 487 // FIXME: Create unittest. 488 // In another words, find a way when "copysign" appears in DAG with vector 489 // operands. 490 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand); 491 // FIXME: Code duplication: SETCC has custom operation action, see 492 // ARMTargetLowering::addTypeForNEON method for details. 493 setOperationAction(ISD::SETCC, MVT::v2f64, Expand); 494 // FIXME: Create unittest for FNEG and for FABS. 495 setOperationAction(ISD::FNEG, MVT::v2f64, Expand); 496 setOperationAction(ISD::FABS, MVT::v2f64, Expand); 497 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand); 498 setOperationAction(ISD::FSIN, MVT::v2f64, Expand); 499 setOperationAction(ISD::FCOS, MVT::v2f64, Expand); 500 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand); 501 setOperationAction(ISD::FPOW, MVT::v2f64, Expand); 502 setOperationAction(ISD::FLOG, MVT::v2f64, Expand); 503 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand); 504 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand); 505 setOperationAction(ISD::FEXP, MVT::v2f64, Expand); 506 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand); 507 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR. 508 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand); 509 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand); 510 setOperationAction(ISD::FRINT, MVT::v2f64, Expand); 511 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand); 512 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand); 513 setOperationAction(ISD::FMA, MVT::v2f64, Expand); 514 515 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); 516 setOperationAction(ISD::FSIN, MVT::v4f32, Expand); 517 setOperationAction(ISD::FCOS, MVT::v4f32, Expand); 518 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand); 519 setOperationAction(ISD::FPOW, MVT::v4f32, Expand); 520 setOperationAction(ISD::FLOG, MVT::v4f32, Expand); 521 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand); 522 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand); 523 setOperationAction(ISD::FEXP, MVT::v4f32, Expand); 524 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand); 525 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand); 526 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand); 527 setOperationAction(ISD::FRINT, MVT::v4f32, Expand); 528 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand); 529 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand); 530 531 // Mark v2f32 intrinsics. 532 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand); 533 setOperationAction(ISD::FSIN, MVT::v2f32, Expand); 534 setOperationAction(ISD::FCOS, MVT::v2f32, Expand); 535 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand); 536 setOperationAction(ISD::FPOW, MVT::v2f32, Expand); 537 setOperationAction(ISD::FLOG, MVT::v2f32, Expand); 538 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand); 539 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand); 540 setOperationAction(ISD::FEXP, MVT::v2f32, Expand); 541 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand); 542 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand); 543 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand); 544 setOperationAction(ISD::FRINT, MVT::v2f32, Expand); 545 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand); 546 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand); 547 548 // Neon does not support some operations on v1i64 and v2i64 types. 549 setOperationAction(ISD::MUL, MVT::v1i64, Expand); 550 // Custom handling for some quad-vector types to detect VMULL. 551 setOperationAction(ISD::MUL, MVT::v8i16, Custom); 552 setOperationAction(ISD::MUL, MVT::v4i32, Custom); 553 setOperationAction(ISD::MUL, MVT::v2i64, Custom); 554 // Custom handling for some vector types to avoid expensive expansions 555 setOperationAction(ISD::SDIV, MVT::v4i16, Custom); 556 setOperationAction(ISD::SDIV, MVT::v8i8, Custom); 557 setOperationAction(ISD::UDIV, MVT::v4i16, Custom); 558 setOperationAction(ISD::UDIV, MVT::v8i8, Custom); 559 setOperationAction(ISD::SETCC, MVT::v1i64, Expand); 560 setOperationAction(ISD::SETCC, MVT::v2i64, Expand); 561 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with 562 // a destination type that is wider than the source, and nor does 563 // it have a FP_TO_[SU]INT instruction with a narrower destination than 564 // source. 565 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom); 566 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); 567 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom); 568 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom); 569 570 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 571 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand); 572 573 // NEON does not have single instruction CTPOP for vectors with element 574 // types wider than 8-bits. However, custom lowering can leverage the 575 // v8i8/v16i8 vcnt instruction. 576 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom); 577 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom); 578 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom); 579 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom); 580 setOperationAction(ISD::CTPOP, MVT::v1i64, Expand); 581 setOperationAction(ISD::CTPOP, MVT::v2i64, Expand); 582 583 // NEON does not have single instruction CTTZ for vectors. 584 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom); 585 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom); 586 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom); 587 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom); 588 589 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom); 590 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom); 591 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom); 592 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom); 593 594 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom); 595 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom); 596 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom); 597 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom); 598 599 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom); 600 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom); 601 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom); 602 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom); 603 604 // NEON only has FMA instructions as of VFP4. 605 if (!Subtarget->hasVFP4()) { 606 setOperationAction(ISD::FMA, MVT::v2f32, Expand); 607 setOperationAction(ISD::FMA, MVT::v4f32, Expand); 608 } 609 610 setTargetDAGCombine(ISD::INTRINSIC_VOID); 611 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN); 612 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); 613 setTargetDAGCombine(ISD::SHL); 614 setTargetDAGCombine(ISD::SRL); 615 setTargetDAGCombine(ISD::SRA); 616 setTargetDAGCombine(ISD::SIGN_EXTEND); 617 setTargetDAGCombine(ISD::ZERO_EXTEND); 618 setTargetDAGCombine(ISD::ANY_EXTEND); 619 setTargetDAGCombine(ISD::BUILD_VECTOR); 620 setTargetDAGCombine(ISD::VECTOR_SHUFFLE); 621 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); 622 setTargetDAGCombine(ISD::STORE); 623 setTargetDAGCombine(ISD::FP_TO_SINT); 624 setTargetDAGCombine(ISD::FP_TO_UINT); 625 setTargetDAGCombine(ISD::FDIV); 626 setTargetDAGCombine(ISD::LOAD); 627 628 // It is legal to extload from v4i8 to v4i16 or v4i32. 629 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16, 630 MVT::v2i32}) { 631 for (MVT VT : MVT::integer_vector_valuetypes()) { 632 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal); 633 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal); 634 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal); 635 } 636 } 637 } 638 639 // ARM and Thumb2 support UMLAL/SMLAL. 640 if (!Subtarget->isThumb1Only()) 641 setTargetDAGCombine(ISD::ADDC); 642 643 if (Subtarget->isFPOnlySP()) { 644 // When targeting a floating-point unit with only single-precision 645 // operations, f64 is legal for the few double-precision instructions which 646 // are present However, no double-precision operations other than moves, 647 // loads and stores are provided by the hardware. 648 setOperationAction(ISD::FADD, MVT::f64, Expand); 649 setOperationAction(ISD::FSUB, MVT::f64, Expand); 650 setOperationAction(ISD::FMUL, MVT::f64, Expand); 651 setOperationAction(ISD::FMA, MVT::f64, Expand); 652 setOperationAction(ISD::FDIV, MVT::f64, Expand); 653 setOperationAction(ISD::FREM, MVT::f64, Expand); 654 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 655 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand); 656 setOperationAction(ISD::FNEG, MVT::f64, Expand); 657 setOperationAction(ISD::FABS, MVT::f64, Expand); 658 setOperationAction(ISD::FSQRT, MVT::f64, Expand); 659 setOperationAction(ISD::FSIN, MVT::f64, Expand); 660 setOperationAction(ISD::FCOS, MVT::f64, Expand); 661 setOperationAction(ISD::FPOWI, MVT::f64, Expand); 662 setOperationAction(ISD::FPOW, MVT::f64, Expand); 663 setOperationAction(ISD::FLOG, MVT::f64, Expand); 664 setOperationAction(ISD::FLOG2, MVT::f64, Expand); 665 setOperationAction(ISD::FLOG10, MVT::f64, Expand); 666 setOperationAction(ISD::FEXP, MVT::f64, Expand); 667 setOperationAction(ISD::FEXP2, MVT::f64, Expand); 668 setOperationAction(ISD::FCEIL, MVT::f64, Expand); 669 setOperationAction(ISD::FTRUNC, MVT::f64, Expand); 670 setOperationAction(ISD::FRINT, MVT::f64, Expand); 671 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand); 672 setOperationAction(ISD::FFLOOR, MVT::f64, Expand); 673 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 674 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); 675 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 676 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); 677 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom); 678 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom); 679 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); 680 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom); 681 } 682 683 computeRegisterProperties(Subtarget->getRegisterInfo()); 684 685 // ARM does not have floating-point extending loads. 686 for (MVT VT : MVT::fp_valuetypes()) { 687 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); 688 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand); 689 } 690 691 // ... or truncating stores 692 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 693 setTruncStoreAction(MVT::f32, MVT::f16, Expand); 694 setTruncStoreAction(MVT::f64, MVT::f16, Expand); 695 696 // ARM does not have i1 sign extending load. 697 for (MVT VT : MVT::integer_valuetypes()) 698 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); 699 700 // ARM supports all 4 flavors of integer indexed load / store. 701 if (!Subtarget->isThumb1Only()) { 702 for (unsigned im = (unsigned)ISD::PRE_INC; 703 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) { 704 setIndexedLoadAction(im, MVT::i1, Legal); 705 setIndexedLoadAction(im, MVT::i8, Legal); 706 setIndexedLoadAction(im, MVT::i16, Legal); 707 setIndexedLoadAction(im, MVT::i32, Legal); 708 setIndexedStoreAction(im, MVT::i1, Legal); 709 setIndexedStoreAction(im, MVT::i8, Legal); 710 setIndexedStoreAction(im, MVT::i16, Legal); 711 setIndexedStoreAction(im, MVT::i32, Legal); 712 } 713 } 714 715 setOperationAction(ISD::SADDO, MVT::i32, Custom); 716 setOperationAction(ISD::UADDO, MVT::i32, Custom); 717 setOperationAction(ISD::SSUBO, MVT::i32, Custom); 718 setOperationAction(ISD::USUBO, MVT::i32, Custom); 719 720 // i64 operation support. 721 setOperationAction(ISD::MUL, MVT::i64, Expand); 722 setOperationAction(ISD::MULHU, MVT::i32, Expand); 723 if (Subtarget->isThumb1Only()) { 724 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 725 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 726 } 727 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops() 728 || (Subtarget->isThumb2() && !Subtarget->hasDSP())) 729 setOperationAction(ISD::MULHS, MVT::i32, Expand); 730 731 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); 732 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); 733 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); 734 setOperationAction(ISD::SRL, MVT::i64, Custom); 735 setOperationAction(ISD::SRA, MVT::i64, Custom); 736 737 if (!Subtarget->isThumb1Only()) { 738 // FIXME: We should do this for Thumb1 as well. 739 setOperationAction(ISD::ADDC, MVT::i32, Custom); 740 setOperationAction(ISD::ADDE, MVT::i32, Custom); 741 setOperationAction(ISD::SUBC, MVT::i32, Custom); 742 setOperationAction(ISD::SUBE, MVT::i32, Custom); 743 } 744 745 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) 746 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 747 748 // ARM does not have ROTL. 749 setOperationAction(ISD::ROTL, MVT::i32, Expand); 750 for (MVT VT : MVT::vector_valuetypes()) { 751 setOperationAction(ISD::ROTL, VT, Expand); 752 setOperationAction(ISD::ROTR, VT, Expand); 753 } 754 setOperationAction(ISD::CTTZ, MVT::i32, Custom); 755 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 756 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) 757 setOperationAction(ISD::CTLZ, MVT::i32, Expand); 758 759 // These just redirect to CTTZ and CTLZ on ARM. 760 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand); 761 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand); 762 763 // @llvm.readcyclecounter requires the Performance Monitors extension. 764 // Default to the 0 expansion on unsupported platforms. 765 // FIXME: Technically there are older ARM CPUs that have 766 // implementation-specific ways of obtaining this information. 767 if (Subtarget->hasPerfMon()) 768 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom); 769 770 // Only ARMv6 has BSWAP. 771 if (!Subtarget->hasV6Ops()) 772 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 773 774 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivide() 775 : Subtarget->hasDivideInARMMode(); 776 if (!hasDivide) { 777 // These are expanded into libcalls if the cpu doesn't have HW divider. 778 setOperationAction(ISD::SDIV, MVT::i32, LibCall); 779 setOperationAction(ISD::UDIV, MVT::i32, LibCall); 780 } 781 782 if (Subtarget->isTargetWindows() && !Subtarget->hasDivide()) { 783 setOperationAction(ISD::SDIV, MVT::i32, Custom); 784 setOperationAction(ISD::UDIV, MVT::i32, Custom); 785 786 setOperationAction(ISD::SDIV, MVT::i64, Custom); 787 setOperationAction(ISD::UDIV, MVT::i64, Custom); 788 } 789 790 setOperationAction(ISD::SREM, MVT::i32, Expand); 791 setOperationAction(ISD::UREM, MVT::i32, Expand); 792 // Register based DivRem for AEABI (RTABI 4.2) 793 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() || 794 Subtarget->isTargetGNUAEABI()) { 795 setOperationAction(ISD::SREM, MVT::i64, Custom); 796 setOperationAction(ISD::UREM, MVT::i64, Custom); 797 798 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod"); 799 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod"); 800 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod"); 801 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod"); 802 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod"); 803 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod"); 804 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod"); 805 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod"); 806 807 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS); 808 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS); 809 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS); 810 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS); 811 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS); 812 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS); 813 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS); 814 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS); 815 816 setOperationAction(ISD::SDIVREM, MVT::i32, Custom); 817 setOperationAction(ISD::UDIVREM, MVT::i32, Custom); 818 setOperationAction(ISD::SDIVREM, MVT::i64, Custom); 819 setOperationAction(ISD::UDIVREM, MVT::i64, Custom); 820 } else { 821 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 822 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 823 } 824 825 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 826 setOperationAction(ISD::ConstantPool, MVT::i32, Custom); 827 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); 828 setOperationAction(ISD::BlockAddress, MVT::i32, Custom); 829 830 setOperationAction(ISD::TRAP, MVT::Other, Legal); 831 832 // Use the default implementation. 833 setOperationAction(ISD::VASTART, MVT::Other, Custom); 834 setOperationAction(ISD::VAARG, MVT::Other, Expand); 835 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 836 setOperationAction(ISD::VAEND, MVT::Other, Expand); 837 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 838 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 839 840 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment()) 841 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom); 842 else 843 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand); 844 845 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use 846 // the default expansion. 847 InsertFencesForAtomic = false; 848 if (Subtarget->hasAnyDataBarrier() && 849 (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) { 850 // ATOMIC_FENCE needs custom lowering; the others should have been expanded 851 // to ldrex/strex loops already. 852 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom); 853 854 // On v8, we have particularly efficient implementations of atomic fences 855 // if they can be combined with nearby atomic loads and stores. 856 if (!Subtarget->hasV8Ops()) { 857 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc. 858 InsertFencesForAtomic = true; 859 } 860 } else { 861 // If there's anything we can use as a barrier, go through custom lowering 862 // for ATOMIC_FENCE. 863 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, 864 Subtarget->hasAnyDataBarrier() ? Custom : Expand); 865 866 // Set them all for expansion, which will force libcalls. 867 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand); 868 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand); 869 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand); 870 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand); 871 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand); 872 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand); 873 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand); 874 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand); 875 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand); 876 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand); 877 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand); 878 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand); 879 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the 880 // Unordered/Monotonic case. 881 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom); 882 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom); 883 } 884 885 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 886 887 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes. 888 if (!Subtarget->hasV6Ops()) { 889 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 890 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand); 891 } 892 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 893 894 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 895 !Subtarget->isThumb1Only()) { 896 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR 897 // iff target supports vfp2. 898 setOperationAction(ISD::BITCAST, MVT::i64, Custom); 899 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); 900 } 901 902 // We want to custom lower some of our intrinsics. 903 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 904 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); 905 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); 906 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom); 907 if (Subtarget->useSjLjEH()) 908 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); 909 910 setOperationAction(ISD::SETCC, MVT::i32, Expand); 911 setOperationAction(ISD::SETCC, MVT::f32, Expand); 912 setOperationAction(ISD::SETCC, MVT::f64, Expand); 913 setOperationAction(ISD::SELECT, MVT::i32, Custom); 914 setOperationAction(ISD::SELECT, MVT::f32, Custom); 915 setOperationAction(ISD::SELECT, MVT::f64, Custom); 916 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 917 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 918 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 919 920 // Thumb-1 cannot currently select ARMISD::SUBE. 921 if (!Subtarget->isThumb1Only()) 922 setOperationAction(ISD::SETCCE, MVT::i32, Custom); 923 924 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 925 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 926 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 927 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 928 setOperationAction(ISD::BR_JT, MVT::Other, Custom); 929 930 // We don't support sin/cos/fmod/copysign/pow 931 setOperationAction(ISD::FSIN, MVT::f64, Expand); 932 setOperationAction(ISD::FSIN, MVT::f32, Expand); 933 setOperationAction(ISD::FCOS, MVT::f32, Expand); 934 setOperationAction(ISD::FCOS, MVT::f64, Expand); 935 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 936 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 937 setOperationAction(ISD::FREM, MVT::f64, Expand); 938 setOperationAction(ISD::FREM, MVT::f32, Expand); 939 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() && 940 !Subtarget->isThumb1Only()) { 941 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); 942 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); 943 } 944 setOperationAction(ISD::FPOW, MVT::f64, Expand); 945 setOperationAction(ISD::FPOW, MVT::f32, Expand); 946 947 if (!Subtarget->hasVFP4()) { 948 setOperationAction(ISD::FMA, MVT::f64, Expand); 949 setOperationAction(ISD::FMA, MVT::f32, Expand); 950 } 951 952 // Various VFP goodness 953 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) { 954 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded. 955 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) { 956 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand); 957 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand); 958 } 959 960 // fp16 is a special v7 extension that adds f16 <-> f32 conversions. 961 if (!Subtarget->hasFP16()) { 962 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand); 963 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand); 964 } 965 } 966 967 // Combine sin / cos into one node or libcall if possible. 968 if (Subtarget->hasSinCos()) { 969 setLibcallName(RTLIB::SINCOS_F32, "sincosf"); 970 setLibcallName(RTLIB::SINCOS_F64, "sincos"); 971 if (Subtarget->isTargetWatchABI()) { 972 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP); 973 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP); 974 } 975 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) { 976 // For iOS, we don't want to the normal expansion of a libcall to 977 // sincos. We want to issue a libcall to __sincos_stret. 978 setOperationAction(ISD::FSINCOS, MVT::f64, Custom); 979 setOperationAction(ISD::FSINCOS, MVT::f32, Custom); 980 } 981 } 982 983 // FP-ARMv8 implements a lot of rounding-like FP operations. 984 if (Subtarget->hasFPARMv8()) { 985 setOperationAction(ISD::FFLOOR, MVT::f32, Legal); 986 setOperationAction(ISD::FCEIL, MVT::f32, Legal); 987 setOperationAction(ISD::FROUND, MVT::f32, Legal); 988 setOperationAction(ISD::FTRUNC, MVT::f32, Legal); 989 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal); 990 setOperationAction(ISD::FRINT, MVT::f32, Legal); 991 setOperationAction(ISD::FMINNUM, MVT::f32, Legal); 992 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); 993 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal); 994 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal); 995 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal); 996 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal); 997 998 if (!Subtarget->isFPOnlySP()) { 999 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 1000 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 1001 setOperationAction(ISD::FROUND, MVT::f64, Legal); 1002 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 1003 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal); 1004 setOperationAction(ISD::FRINT, MVT::f64, Legal); 1005 setOperationAction(ISD::FMINNUM, MVT::f64, Legal); 1006 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); 1007 } 1008 } 1009 1010 if (Subtarget->hasNEON()) { 1011 // vmin and vmax aren't available in a scalar form, so we use 1012 // a NEON instruction with an undef lane instead. 1013 setOperationAction(ISD::FMINNAN, MVT::f32, Legal); 1014 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal); 1015 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal); 1016 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal); 1017 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal); 1018 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal); 1019 } 1020 1021 // We have target-specific dag combine patterns for the following nodes: 1022 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine 1023 setTargetDAGCombine(ISD::ADD); 1024 setTargetDAGCombine(ISD::SUB); 1025 setTargetDAGCombine(ISD::MUL); 1026 setTargetDAGCombine(ISD::AND); 1027 setTargetDAGCombine(ISD::OR); 1028 setTargetDAGCombine(ISD::XOR); 1029 1030 if (Subtarget->hasV6Ops()) 1031 setTargetDAGCombine(ISD::SRL); 1032 1033 setStackPointerRegisterToSaveRestore(ARM::SP); 1034 1035 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() || 1036 !Subtarget->hasVFP2()) 1037 setSchedulingPreference(Sched::RegPressure); 1038 else 1039 setSchedulingPreference(Sched::Hybrid); 1040 1041 //// temporary - rewrite interface to use type 1042 MaxStoresPerMemset = 8; 1043 MaxStoresPerMemsetOptSize = 4; 1044 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores 1045 MaxStoresPerMemcpyOptSize = 2; 1046 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores 1047 MaxStoresPerMemmoveOptSize = 2; 1048 1049 // On ARM arguments smaller than 4 bytes are extended, so all arguments 1050 // are at least 4 bytes aligned. 1051 setMinStackArgumentAlignment(4); 1052 1053 // Prefer likely predicted branches to selects on out-of-order cores. 1054 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder(); 1055 1056 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2); 1057 } 1058 1059 bool ARMTargetLowering::useSoftFloat() const { 1060 return Subtarget->useSoftFloat(); 1061 } 1062 1063 // FIXME: It might make sense to define the representative register class as the 1064 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is 1065 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently, 1066 // SPR's representative would be DPR_VFP2. This should work well if register 1067 // pressure tracking were modified such that a register use would increment the 1068 // pressure of the register class's representative and all of it's super 1069 // classes' representatives transitively. We have not implemented this because 1070 // of the difficulty prior to coalescing of modeling operand register classes 1071 // due to the common occurrence of cross class copies and subregister insertions 1072 // and extractions. 1073 std::pair<const TargetRegisterClass *, uint8_t> 1074 ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI, 1075 MVT VT) const { 1076 const TargetRegisterClass *RRC = nullptr; 1077 uint8_t Cost = 1; 1078 switch (VT.SimpleTy) { 1079 default: 1080 return TargetLowering::findRepresentativeClass(TRI, VT); 1081 // Use DPR as representative register class for all floating point 1082 // and vector types. Since there are 32 SPR registers and 32 DPR registers so 1083 // the cost is 1 for both f32 and f64. 1084 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16: 1085 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32: 1086 RRC = &ARM::DPRRegClass; 1087 // When NEON is used for SP, only half of the register file is available 1088 // because operations that define both SP and DP results will be constrained 1089 // to the VFP2 class (D0-D15). We currently model this constraint prior to 1090 // coalescing by double-counting the SP regs. See the FIXME above. 1091 if (Subtarget->useNEONForSinglePrecisionFP()) 1092 Cost = 2; 1093 break; 1094 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: 1095 case MVT::v4f32: case MVT::v2f64: 1096 RRC = &ARM::DPRRegClass; 1097 Cost = 2; 1098 break; 1099 case MVT::v4i64: 1100 RRC = &ARM::DPRRegClass; 1101 Cost = 4; 1102 break; 1103 case MVT::v8i64: 1104 RRC = &ARM::DPRRegClass; 1105 Cost = 8; 1106 break; 1107 } 1108 return std::make_pair(RRC, Cost); 1109 } 1110 1111 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const { 1112 switch ((ARMISD::NodeType)Opcode) { 1113 case ARMISD::FIRST_NUMBER: break; 1114 case ARMISD::Wrapper: return "ARMISD::Wrapper"; 1115 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC"; 1116 case ARMISD::WrapperJT: return "ARMISD::WrapperJT"; 1117 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL"; 1118 case ARMISD::CALL: return "ARMISD::CALL"; 1119 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED"; 1120 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK"; 1121 case ARMISD::tCALL: return "ARMISD::tCALL"; 1122 case ARMISD::BRCOND: return "ARMISD::BRCOND"; 1123 case ARMISD::BR_JT: return "ARMISD::BR_JT"; 1124 case ARMISD::BR2_JT: return "ARMISD::BR2_JT"; 1125 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG"; 1126 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG"; 1127 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD"; 1128 case ARMISD::CMP: return "ARMISD::CMP"; 1129 case ARMISD::CMN: return "ARMISD::CMN"; 1130 case ARMISD::CMPZ: return "ARMISD::CMPZ"; 1131 case ARMISD::CMPFP: return "ARMISD::CMPFP"; 1132 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0"; 1133 case ARMISD::BCC_i64: return "ARMISD::BCC_i64"; 1134 case ARMISD::FMSTAT: return "ARMISD::FMSTAT"; 1135 1136 case ARMISD::CMOV: return "ARMISD::CMOV"; 1137 1138 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG"; 1139 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG"; 1140 case ARMISD::RRX: return "ARMISD::RRX"; 1141 1142 case ARMISD::ADDC: return "ARMISD::ADDC"; 1143 case ARMISD::ADDE: return "ARMISD::ADDE"; 1144 case ARMISD::SUBC: return "ARMISD::SUBC"; 1145 case ARMISD::SUBE: return "ARMISD::SUBE"; 1146 1147 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD"; 1148 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR"; 1149 1150 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP"; 1151 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP"; 1152 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH"; 1153 1154 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN"; 1155 1156 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER"; 1157 1158 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC"; 1159 1160 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR"; 1161 1162 case ARMISD::PRELOAD: return "ARMISD::PRELOAD"; 1163 1164 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK"; 1165 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK"; 1166 1167 case ARMISD::VCEQ: return "ARMISD::VCEQ"; 1168 case ARMISD::VCEQZ: return "ARMISD::VCEQZ"; 1169 case ARMISD::VCGE: return "ARMISD::VCGE"; 1170 case ARMISD::VCGEZ: return "ARMISD::VCGEZ"; 1171 case ARMISD::VCLEZ: return "ARMISD::VCLEZ"; 1172 case ARMISD::VCGEU: return "ARMISD::VCGEU"; 1173 case ARMISD::VCGT: return "ARMISD::VCGT"; 1174 case ARMISD::VCGTZ: return "ARMISD::VCGTZ"; 1175 case ARMISD::VCLTZ: return "ARMISD::VCLTZ"; 1176 case ARMISD::VCGTU: return "ARMISD::VCGTU"; 1177 case ARMISD::VTST: return "ARMISD::VTST"; 1178 1179 case ARMISD::VSHL: return "ARMISD::VSHL"; 1180 case ARMISD::VSHRs: return "ARMISD::VSHRs"; 1181 case ARMISD::VSHRu: return "ARMISD::VSHRu"; 1182 case ARMISD::VRSHRs: return "ARMISD::VRSHRs"; 1183 case ARMISD::VRSHRu: return "ARMISD::VRSHRu"; 1184 case ARMISD::VRSHRN: return "ARMISD::VRSHRN"; 1185 case ARMISD::VQSHLs: return "ARMISD::VQSHLs"; 1186 case ARMISD::VQSHLu: return "ARMISD::VQSHLu"; 1187 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu"; 1188 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs"; 1189 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu"; 1190 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu"; 1191 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs"; 1192 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu"; 1193 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu"; 1194 case ARMISD::VSLI: return "ARMISD::VSLI"; 1195 case ARMISD::VSRI: return "ARMISD::VSRI"; 1196 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu"; 1197 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs"; 1198 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM"; 1199 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM"; 1200 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM"; 1201 case ARMISD::VDUP: return "ARMISD::VDUP"; 1202 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE"; 1203 case ARMISD::VEXT: return "ARMISD::VEXT"; 1204 case ARMISD::VREV64: return "ARMISD::VREV64"; 1205 case ARMISD::VREV32: return "ARMISD::VREV32"; 1206 case ARMISD::VREV16: return "ARMISD::VREV16"; 1207 case ARMISD::VZIP: return "ARMISD::VZIP"; 1208 case ARMISD::VUZP: return "ARMISD::VUZP"; 1209 case ARMISD::VTRN: return "ARMISD::VTRN"; 1210 case ARMISD::VTBL1: return "ARMISD::VTBL1"; 1211 case ARMISD::VTBL2: return "ARMISD::VTBL2"; 1212 case ARMISD::VMULLs: return "ARMISD::VMULLs"; 1213 case ARMISD::VMULLu: return "ARMISD::VMULLu"; 1214 case ARMISD::UMLAL: return "ARMISD::UMLAL"; 1215 case ARMISD::SMLAL: return "ARMISD::SMLAL"; 1216 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR"; 1217 case ARMISD::BFI: return "ARMISD::BFI"; 1218 case ARMISD::VORRIMM: return "ARMISD::VORRIMM"; 1219 case ARMISD::VBICIMM: return "ARMISD::VBICIMM"; 1220 case ARMISD::VBSL: return "ARMISD::VBSL"; 1221 case ARMISD::MEMCPY: return "ARMISD::MEMCPY"; 1222 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP"; 1223 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP"; 1224 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP"; 1225 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD"; 1226 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD"; 1227 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD"; 1228 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD"; 1229 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD"; 1230 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD"; 1231 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD"; 1232 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD"; 1233 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD"; 1234 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD"; 1235 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD"; 1236 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD"; 1237 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD"; 1238 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD"; 1239 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD"; 1240 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD"; 1241 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD"; 1242 } 1243 return nullptr; 1244 } 1245 1246 EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &, 1247 EVT VT) const { 1248 if (!VT.isVector()) 1249 return getPointerTy(DL); 1250 return VT.changeVectorElementTypeToInteger(); 1251 } 1252 1253 /// getRegClassFor - Return the register class that should be used for the 1254 /// specified value type. 1255 const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const { 1256 // Map v4i64 to QQ registers but do not make the type legal. Similarly map 1257 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to 1258 // load / store 4 to 8 consecutive D registers. 1259 if (Subtarget->hasNEON()) { 1260 if (VT == MVT::v4i64) 1261 return &ARM::QQPRRegClass; 1262 if (VT == MVT::v8i64) 1263 return &ARM::QQQQPRRegClass; 1264 } 1265 return TargetLowering::getRegClassFor(VT); 1266 } 1267 1268 // memcpy, and other memory intrinsics, typically tries to use LDM/STM if the 1269 // source/dest is aligned and the copy size is large enough. We therefore want 1270 // to align such objects passed to memory intrinsics. 1271 bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize, 1272 unsigned &PrefAlign) const { 1273 if (!isa<MemIntrinsic>(CI)) 1274 return false; 1275 MinSize = 8; 1276 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1 1277 // cycle faster than 4-byte aligned LDM. 1278 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4); 1279 return true; 1280 } 1281 1282 // Create a fast isel object. 1283 FastISel * 1284 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, 1285 const TargetLibraryInfo *libInfo) const { 1286 return ARM::createFastISel(funcInfo, libInfo); 1287 } 1288 1289 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const { 1290 unsigned NumVals = N->getNumValues(); 1291 if (!NumVals) 1292 return Sched::RegPressure; 1293 1294 for (unsigned i = 0; i != NumVals; ++i) { 1295 EVT VT = N->getValueType(i); 1296 if (VT == MVT::Glue || VT == MVT::Other) 1297 continue; 1298 if (VT.isFloatingPoint() || VT.isVector()) 1299 return Sched::ILP; 1300 } 1301 1302 if (!N->isMachineOpcode()) 1303 return Sched::RegPressure; 1304 1305 // Load are scheduled for latency even if there instruction itinerary 1306 // is not available. 1307 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 1308 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); 1309 1310 if (MCID.getNumDefs() == 0) 1311 return Sched::RegPressure; 1312 if (!Itins->isEmpty() && 1313 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2) 1314 return Sched::ILP; 1315 1316 return Sched::RegPressure; 1317 } 1318 1319 //===----------------------------------------------------------------------===// 1320 // Lowering Code 1321 //===----------------------------------------------------------------------===// 1322 1323 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC 1324 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) { 1325 switch (CC) { 1326 default: llvm_unreachable("Unknown condition code!"); 1327 case ISD::SETNE: return ARMCC::NE; 1328 case ISD::SETEQ: return ARMCC::EQ; 1329 case ISD::SETGT: return ARMCC::GT; 1330 case ISD::SETGE: return ARMCC::GE; 1331 case ISD::SETLT: return ARMCC::LT; 1332 case ISD::SETLE: return ARMCC::LE; 1333 case ISD::SETUGT: return ARMCC::HI; 1334 case ISD::SETUGE: return ARMCC::HS; 1335 case ISD::SETULT: return ARMCC::LO; 1336 case ISD::SETULE: return ARMCC::LS; 1337 } 1338 } 1339 1340 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. 1341 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 1342 ARMCC::CondCodes &CondCode2) { 1343 CondCode2 = ARMCC::AL; 1344 switch (CC) { 1345 default: llvm_unreachable("Unknown FP condition!"); 1346 case ISD::SETEQ: 1347 case ISD::SETOEQ: CondCode = ARMCC::EQ; break; 1348 case ISD::SETGT: 1349 case ISD::SETOGT: CondCode = ARMCC::GT; break; 1350 case ISD::SETGE: 1351 case ISD::SETOGE: CondCode = ARMCC::GE; break; 1352 case ISD::SETOLT: CondCode = ARMCC::MI; break; 1353 case ISD::SETOLE: CondCode = ARMCC::LS; break; 1354 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break; 1355 case ISD::SETO: CondCode = ARMCC::VC; break; 1356 case ISD::SETUO: CondCode = ARMCC::VS; break; 1357 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break; 1358 case ISD::SETUGT: CondCode = ARMCC::HI; break; 1359 case ISD::SETUGE: CondCode = ARMCC::PL; break; 1360 case ISD::SETLT: 1361 case ISD::SETULT: CondCode = ARMCC::LT; break; 1362 case ISD::SETLE: 1363 case ISD::SETULE: CondCode = ARMCC::LE; break; 1364 case ISD::SETNE: 1365 case ISD::SETUNE: CondCode = ARMCC::NE; break; 1366 } 1367 } 1368 1369 //===----------------------------------------------------------------------===// 1370 // Calling Convention Implementation 1371 //===----------------------------------------------------------------------===// 1372 1373 #include "ARMGenCallingConv.inc" 1374 1375 /// getEffectiveCallingConv - Get the effective calling convention, taking into 1376 /// account presence of floating point hardware and calling convention 1377 /// limitations, such as support for variadic functions. 1378 CallingConv::ID 1379 ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC, 1380 bool isVarArg) const { 1381 switch (CC) { 1382 default: 1383 llvm_unreachable("Unsupported calling convention"); 1384 case CallingConv::ARM_AAPCS: 1385 case CallingConv::ARM_APCS: 1386 case CallingConv::GHC: 1387 return CC; 1388 case CallingConv::PreserveMost: 1389 return CallingConv::PreserveMost; 1390 case CallingConv::ARM_AAPCS_VFP: 1391 case CallingConv::Swift: 1392 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP; 1393 case CallingConv::C: 1394 if (!Subtarget->isAAPCS_ABI()) 1395 return CallingConv::ARM_APCS; 1396 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && 1397 getTargetMachine().Options.FloatABIType == FloatABI::Hard && 1398 !isVarArg) 1399 return CallingConv::ARM_AAPCS_VFP; 1400 else 1401 return CallingConv::ARM_AAPCS; 1402 case CallingConv::Fast: 1403 case CallingConv::CXX_FAST_TLS: 1404 if (!Subtarget->isAAPCS_ABI()) { 1405 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg) 1406 return CallingConv::Fast; 1407 return CallingConv::ARM_APCS; 1408 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg) 1409 return CallingConv::ARM_AAPCS_VFP; 1410 else 1411 return CallingConv::ARM_AAPCS; 1412 } 1413 } 1414 1415 /// CCAssignFnForNode - Selects the correct CCAssignFn for the given 1416 /// CallingConvention. 1417 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC, 1418 bool Return, 1419 bool isVarArg) const { 1420 switch (getEffectiveCallingConv(CC, isVarArg)) { 1421 default: 1422 llvm_unreachable("Unsupported calling convention"); 1423 case CallingConv::ARM_APCS: 1424 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS); 1425 case CallingConv::ARM_AAPCS: 1426 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1427 case CallingConv::ARM_AAPCS_VFP: 1428 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP); 1429 case CallingConv::Fast: 1430 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS); 1431 case CallingConv::GHC: 1432 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC); 1433 case CallingConv::PreserveMost: 1434 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS); 1435 } 1436 } 1437 1438 /// LowerCallResult - Lower the result values of a call into the 1439 /// appropriate copies out of appropriate physical registers. 1440 SDValue 1441 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, 1442 CallingConv::ID CallConv, bool isVarArg, 1443 const SmallVectorImpl<ISD::InputArg> &Ins, 1444 SDLoc dl, SelectionDAG &DAG, 1445 SmallVectorImpl<SDValue> &InVals, 1446 bool isThisReturn, SDValue ThisVal) const { 1447 1448 // Assign locations to each value returned by this call. 1449 SmallVector<CCValAssign, 16> RVLocs; 1450 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1451 *DAG.getContext(), Call); 1452 CCInfo.AnalyzeCallResult(Ins, 1453 CCAssignFnForNode(CallConv, /* Return*/ true, 1454 isVarArg)); 1455 1456 // Copy all of the result registers out of their specified physreg. 1457 for (unsigned i = 0; i != RVLocs.size(); ++i) { 1458 CCValAssign VA = RVLocs[i]; 1459 1460 // Pass 'this' value directly from the argument to return value, to avoid 1461 // reg unit interference 1462 if (i == 0 && isThisReturn) { 1463 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 && 1464 "unexpected return calling convention register assignment"); 1465 InVals.push_back(ThisVal); 1466 continue; 1467 } 1468 1469 SDValue Val; 1470 if (VA.needsCustom()) { 1471 // Handle f64 or half of a v2f64. 1472 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1473 InFlag); 1474 Chain = Lo.getValue(1); 1475 InFlag = Lo.getValue(2); 1476 VA = RVLocs[++i]; // skip ahead to next loc 1477 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, 1478 InFlag); 1479 Chain = Hi.getValue(1); 1480 InFlag = Hi.getValue(2); 1481 if (!Subtarget->isLittle()) 1482 std::swap (Lo, Hi); 1483 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1484 1485 if (VA.getLocVT() == MVT::v2f64) { 1486 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 1487 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1488 DAG.getConstant(0, dl, MVT::i32)); 1489 1490 VA = RVLocs[++i]; // skip ahead to next loc 1491 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1492 Chain = Lo.getValue(1); 1493 InFlag = Lo.getValue(2); 1494 VA = RVLocs[++i]; // skip ahead to next loc 1495 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag); 1496 Chain = Hi.getValue(1); 1497 InFlag = Hi.getValue(2); 1498 if (!Subtarget->isLittle()) 1499 std::swap (Lo, Hi); 1500 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 1501 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val, 1502 DAG.getConstant(1, dl, MVT::i32)); 1503 } 1504 } else { 1505 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(), 1506 InFlag); 1507 Chain = Val.getValue(1); 1508 InFlag = Val.getValue(2); 1509 } 1510 1511 switch (VA.getLocInfo()) { 1512 default: llvm_unreachable("Unknown loc info!"); 1513 case CCValAssign::Full: break; 1514 case CCValAssign::BCvt: 1515 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val); 1516 break; 1517 } 1518 1519 InVals.push_back(Val); 1520 } 1521 1522 return Chain; 1523 } 1524 1525 /// LowerMemOpCallTo - Store the argument to the stack. 1526 SDValue 1527 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, 1528 SDValue StackPtr, SDValue Arg, 1529 SDLoc dl, SelectionDAG &DAG, 1530 const CCValAssign &VA, 1531 ISD::ArgFlagsTy Flags) const { 1532 unsigned LocMemOffset = VA.getLocMemOffset(); 1533 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 1534 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()), 1535 StackPtr, PtrOff); 1536 return DAG.getStore( 1537 Chain, dl, Arg, PtrOff, 1538 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset), 1539 false, false, 0); 1540 } 1541 1542 void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG, 1543 SDValue Chain, SDValue &Arg, 1544 RegsToPassVector &RegsToPass, 1545 CCValAssign &VA, CCValAssign &NextVA, 1546 SDValue &StackPtr, 1547 SmallVectorImpl<SDValue> &MemOpChains, 1548 ISD::ArgFlagsTy Flags) const { 1549 1550 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 1551 DAG.getVTList(MVT::i32, MVT::i32), Arg); 1552 unsigned id = Subtarget->isLittle() ? 0 : 1; 1553 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id))); 1554 1555 if (NextVA.isRegLoc()) 1556 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id))); 1557 else { 1558 assert(NextVA.isMemLoc()); 1559 if (!StackPtr.getNode()) 1560 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, 1561 getPointerTy(DAG.getDataLayout())); 1562 1563 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id), 1564 dl, DAG, NextVA, 1565 Flags)); 1566 } 1567 } 1568 1569 /// LowerCall - Lowering a call into a callseq_start <- 1570 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter 1571 /// nodes. 1572 SDValue 1573 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 1574 SmallVectorImpl<SDValue> &InVals) const { 1575 SelectionDAG &DAG = CLI.DAG; 1576 SDLoc &dl = CLI.DL; 1577 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 1578 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 1579 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 1580 SDValue Chain = CLI.Chain; 1581 SDValue Callee = CLI.Callee; 1582 bool &isTailCall = CLI.IsTailCall; 1583 CallingConv::ID CallConv = CLI.CallConv; 1584 bool doesNotRet = CLI.DoesNotReturn; 1585 bool isVarArg = CLI.IsVarArg; 1586 1587 MachineFunction &MF = DAG.getMachineFunction(); 1588 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet(); 1589 bool isThisReturn = false; 1590 bool isSibCall = false; 1591 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls"); 1592 1593 // Disable tail calls if they're not supported. 1594 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true") 1595 isTailCall = false; 1596 1597 if (isTailCall) { 1598 // Check if it's really possible to do a tail call. 1599 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, 1600 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(), 1601 Outs, OutVals, Ins, DAG); 1602 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall()) 1603 report_fatal_error("failed to perform tail call elimination on a call " 1604 "site marked musttail"); 1605 // We don't support GuaranteedTailCallOpt for ARM, only automatically 1606 // detected sibcalls. 1607 if (isTailCall) { 1608 ++NumTailCalls; 1609 isSibCall = true; 1610 } 1611 } 1612 1613 // Analyze operands of the call, assigning locations to each operand. 1614 SmallVector<CCValAssign, 16> ArgLocs; 1615 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1616 *DAG.getContext(), Call); 1617 CCInfo.AnalyzeCallOperands(Outs, 1618 CCAssignFnForNode(CallConv, /* Return*/ false, 1619 isVarArg)); 1620 1621 // Get a count of how many bytes are to be pushed on the stack. 1622 unsigned NumBytes = CCInfo.getNextStackOffset(); 1623 1624 // For tail calls, memory operands are available in our caller's stack. 1625 if (isSibCall) 1626 NumBytes = 0; 1627 1628 // Adjust the stack pointer for the new arguments... 1629 // These operations are automatically eliminated by the prolog/epilog pass 1630 if (!isSibCall) 1631 Chain = DAG.getCALLSEQ_START(Chain, 1632 DAG.getIntPtrConstant(NumBytes, dl, true), dl); 1633 1634 SDValue StackPtr = 1635 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout())); 1636 1637 RegsToPassVector RegsToPass; 1638 SmallVector<SDValue, 8> MemOpChains; 1639 1640 // Walk the register/memloc assignments, inserting copies/loads. In the case 1641 // of tail call optimization, arguments are handled later. 1642 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 1643 i != e; 1644 ++i, ++realArgIdx) { 1645 CCValAssign &VA = ArgLocs[i]; 1646 SDValue Arg = OutVals[realArgIdx]; 1647 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 1648 bool isByVal = Flags.isByVal(); 1649 1650 // Promote the value if needed. 1651 switch (VA.getLocInfo()) { 1652 default: llvm_unreachable("Unknown loc info!"); 1653 case CCValAssign::Full: break; 1654 case CCValAssign::SExt: 1655 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 1656 break; 1657 case CCValAssign::ZExt: 1658 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 1659 break; 1660 case CCValAssign::AExt: 1661 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 1662 break; 1663 case CCValAssign::BCvt: 1664 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 1665 break; 1666 } 1667 1668 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces 1669 if (VA.needsCustom()) { 1670 if (VA.getLocVT() == MVT::v2f64) { 1671 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1672 DAG.getConstant(0, dl, MVT::i32)); 1673 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 1674 DAG.getConstant(1, dl, MVT::i32)); 1675 1676 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass, 1677 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1678 1679 VA = ArgLocs[++i]; // skip ahead to next loc 1680 if (VA.isRegLoc()) { 1681 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass, 1682 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags); 1683 } else { 1684 assert(VA.isMemLoc()); 1685 1686 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1, 1687 dl, DAG, VA, Flags)); 1688 } 1689 } else { 1690 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i], 1691 StackPtr, MemOpChains, Flags); 1692 } 1693 } else if (VA.isRegLoc()) { 1694 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) { 1695 assert(VA.getLocVT() == MVT::i32 && 1696 "unexpected calling convention register assignment"); 1697 assert(!Ins.empty() && Ins[0].VT == MVT::i32 && 1698 "unexpected use of 'returned'"); 1699 isThisReturn = true; 1700 } 1701 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 1702 } else if (isByVal) { 1703 assert(VA.isMemLoc()); 1704 unsigned offset = 0; 1705 1706 // True if this byval aggregate will be split between registers 1707 // and memory. 1708 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount(); 1709 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed(); 1710 1711 if (CurByValIdx < ByValArgsCount) { 1712 1713 unsigned RegBegin, RegEnd; 1714 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd); 1715 1716 EVT PtrVT = 1717 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 1718 unsigned int i, j; 1719 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) { 1720 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32); 1721 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); 1722 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, 1723 MachinePointerInfo(), 1724 false, false, false, 1725 DAG.InferPtrAlignment(AddArg)); 1726 MemOpChains.push_back(Load.getValue(1)); 1727 RegsToPass.push_back(std::make_pair(j, Load)); 1728 } 1729 1730 // If parameter size outsides register area, "offset" value 1731 // helps us to calculate stack slot for remained part properly. 1732 offset = RegEnd - RegBegin; 1733 1734 CCInfo.nextInRegsParam(); 1735 } 1736 1737 if (Flags.getByValSize() > 4*offset) { 1738 auto PtrVT = getPointerTy(DAG.getDataLayout()); 1739 unsigned LocMemOffset = VA.getLocMemOffset(); 1740 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); 1741 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff); 1742 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl); 1743 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset); 1744 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl, 1745 MVT::i32); 1746 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl, 1747 MVT::i32); 1748 1749 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); 1750 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode}; 1751 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs, 1752 Ops)); 1753 } 1754 } else if (!isSibCall) { 1755 assert(VA.isMemLoc()); 1756 1757 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, 1758 dl, DAG, VA, Flags)); 1759 } 1760 } 1761 1762 if (!MemOpChains.empty()) 1763 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); 1764 1765 // Build a sequence of copy-to-reg nodes chained together with token chain 1766 // and flag operands which copy the outgoing args into the appropriate regs. 1767 SDValue InFlag; 1768 // Tail call byval lowering might overwrite argument registers so in case of 1769 // tail call optimization the copies to registers are lowered later. 1770 if (!isTailCall) 1771 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1772 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1773 RegsToPass[i].second, InFlag); 1774 InFlag = Chain.getValue(1); 1775 } 1776 1777 // For tail calls lower the arguments to the 'real' stack slot. 1778 if (isTailCall) { 1779 // Force all the incoming stack arguments to be loaded from the stack 1780 // before any new outgoing arguments are stored to the stack, because the 1781 // outgoing stack slots may alias the incoming argument stack slots, and 1782 // the alias isn't otherwise explicit. This is slightly more conservative 1783 // than necessary, because it means that each store effectively depends 1784 // on every argument instead of just those arguments it would clobber. 1785 1786 // Do not flag preceding copytoreg stuff together with the following stuff. 1787 InFlag = SDValue(); 1788 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 1789 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 1790 RegsToPass[i].second, InFlag); 1791 InFlag = Chain.getValue(1); 1792 } 1793 InFlag = SDValue(); 1794 } 1795 1796 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every 1797 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 1798 // node so that legalize doesn't hack it. 1799 bool isDirect = false; 1800 bool isARMFunc = false; 1801 bool isLocalARMFunc = false; 1802 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1803 auto PtrVt = getPointerTy(DAG.getDataLayout()); 1804 1805 if (Subtarget->genLongCalls()) { 1806 assert((Subtarget->isTargetWindows() || 1807 getTargetMachine().getRelocationModel() == Reloc::Static) && 1808 "long-calls with non-static relocation model!"); 1809 // Handle a global address or an external symbol. If it's not one of 1810 // those, the target's already in a register, so we don't need to do 1811 // anything extra. 1812 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1813 const GlobalValue *GV = G->getGlobal(); 1814 // Create a constant pool entry for the callee address 1815 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1816 ARMConstantPoolValue *CPV = 1817 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0); 1818 1819 // Get the address of the callee into a register 1820 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 1821 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1822 Callee = DAG.getLoad( 1823 PtrVt, dl, DAG.getEntryNode(), CPAddr, 1824 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 1825 false, false, 0); 1826 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) { 1827 const char *Sym = S->getSymbol(); 1828 1829 // Create a constant pool entry for the callee address 1830 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1831 ARMConstantPoolValue *CPV = 1832 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1833 ARMPCLabelIndex, 0); 1834 // Get the address of the callee into a register 1835 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 1836 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1837 Callee = DAG.getLoad( 1838 PtrVt, dl, DAG.getEntryNode(), CPAddr, 1839 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 1840 false, false, 0); 1841 } 1842 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 1843 const GlobalValue *GV = G->getGlobal(); 1844 isDirect = true; 1845 bool isDef = GV->isStrongDefinitionForLinker(); 1846 bool isStub = (!isDef && Subtarget->isTargetMachO()) && 1847 getTargetMachine().getRelocationModel() != Reloc::Static; 1848 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 1849 // ARM call to a local ARM function is predicable. 1850 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking); 1851 // tBX takes a register source operand. 1852 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1853 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?"); 1854 Callee = DAG.getNode( 1855 ARMISD::WrapperPIC, dl, PtrVt, 1856 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY)); 1857 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee, 1858 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 1859 false, false, true, 0); 1860 } else if (Subtarget->isTargetCOFF()) { 1861 assert(Subtarget->isTargetWindows() && 1862 "Windows is the only supported COFF target"); 1863 unsigned TargetFlags = GV->hasDLLImportStorageClass() 1864 ? ARMII::MO_DLLIMPORT 1865 : ARMII::MO_NO_FLAG; 1866 Callee = 1867 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags); 1868 if (GV->hasDLLImportStorageClass()) 1869 Callee = 1870 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), 1871 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee), 1872 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 1873 false, false, false, 0); 1874 } else { 1875 // On ELF targets for PIC code, direct calls should go through the PLT 1876 unsigned OpFlags = 0; 1877 if (Subtarget->isTargetELF() && 1878 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1879 OpFlags = ARMII::MO_PLT; 1880 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags); 1881 } 1882 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { 1883 isDirect = true; 1884 bool isStub = Subtarget->isTargetMachO() && 1885 getTargetMachine().getRelocationModel() != Reloc::Static; 1886 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass()); 1887 // tBX takes a register source operand. 1888 const char *Sym = S->getSymbol(); 1889 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) { 1890 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 1891 ARMConstantPoolValue *CPV = 1892 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym, 1893 ARMPCLabelIndex, 4); 1894 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4); 1895 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 1896 Callee = DAG.getLoad( 1897 PtrVt, dl, DAG.getEntryNode(), CPAddr, 1898 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 1899 false, false, 0); 1900 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 1901 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel); 1902 } else { 1903 unsigned OpFlags = 0; 1904 // On ELF targets for PIC code, direct calls should go through the PLT 1905 if (Subtarget->isTargetELF() && 1906 getTargetMachine().getRelocationModel() == Reloc::PIC_) 1907 OpFlags = ARMII::MO_PLT; 1908 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags); 1909 } 1910 } 1911 1912 // FIXME: handle tail calls differently. 1913 unsigned CallOpc; 1914 if (Subtarget->isThumb()) { 1915 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps()) 1916 CallOpc = ARMISD::CALL_NOLINK; 1917 else 1918 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL; 1919 } else { 1920 if (!isDirect && !Subtarget->hasV5TOps()) 1921 CallOpc = ARMISD::CALL_NOLINK; 1922 else if (doesNotRet && isDirect && Subtarget->hasRAS() && 1923 // Emit regular call when code size is the priority 1924 !MF.getFunction()->optForMinSize()) 1925 // "mov lr, pc; b _foo" to avoid confusing the RSP 1926 CallOpc = ARMISD::CALL_NOLINK; 1927 else 1928 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL; 1929 } 1930 1931 std::vector<SDValue> Ops; 1932 Ops.push_back(Chain); 1933 Ops.push_back(Callee); 1934 1935 // Add argument registers to the end of the list so that they are known live 1936 // into the call. 1937 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 1938 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 1939 RegsToPass[i].second.getValueType())); 1940 1941 // Add a register mask operand representing the call-preserved registers. 1942 if (!isTailCall) { 1943 const uint32_t *Mask; 1944 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo(); 1945 if (isThisReturn) { 1946 // For 'this' returns, use the R0-preserving mask if applicable 1947 Mask = ARI->getThisReturnPreservedMask(MF, CallConv); 1948 if (!Mask) { 1949 // Set isThisReturn to false if the calling convention is not one that 1950 // allows 'returned' to be modeled in this way, so LowerCallResult does 1951 // not try to pass 'this' straight through 1952 isThisReturn = false; 1953 Mask = ARI->getCallPreservedMask(MF, CallConv); 1954 } 1955 } else 1956 Mask = ARI->getCallPreservedMask(MF, CallConv); 1957 1958 assert(Mask && "Missing call preserved mask for calling convention"); 1959 Ops.push_back(DAG.getRegisterMask(Mask)); 1960 } 1961 1962 if (InFlag.getNode()) 1963 Ops.push_back(InFlag); 1964 1965 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1966 if (isTailCall) { 1967 MF.getFrameInfo()->setHasTailCall(); 1968 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops); 1969 } 1970 1971 // Returns a chain and a flag for retval copy to use. 1972 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops); 1973 InFlag = Chain.getValue(1); 1974 1975 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true), 1976 DAG.getIntPtrConstant(0, dl, true), InFlag, dl); 1977 if (!Ins.empty()) 1978 InFlag = Chain.getValue(1); 1979 1980 // Handle result values, copying them out of physregs into vregs that we 1981 // return. 1982 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG, 1983 InVals, isThisReturn, 1984 isThisReturn ? OutVals[0] : SDValue()); 1985 } 1986 1987 /// HandleByVal - Every parameter *after* a byval parameter is passed 1988 /// on the stack. Remember the next parameter register to allocate, 1989 /// and then confiscate the rest of the parameter registers to insure 1990 /// this. 1991 void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size, 1992 unsigned Align) const { 1993 assert((State->getCallOrPrologue() == Prologue || 1994 State->getCallOrPrologue() == Call) && 1995 "unhandled ParmContext"); 1996 1997 // Byval (as with any stack) slots are always at least 4 byte aligned. 1998 Align = std::max(Align, 4U); 1999 2000 unsigned Reg = State->AllocateReg(GPRArgRegs); 2001 if (!Reg) 2002 return; 2003 2004 unsigned AlignInRegs = Align / 4; 2005 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs; 2006 for (unsigned i = 0; i < Waste; ++i) 2007 Reg = State->AllocateReg(GPRArgRegs); 2008 2009 if (!Reg) 2010 return; 2011 2012 unsigned Excess = 4 * (ARM::R4 - Reg); 2013 2014 // Special case when NSAA != SP and parameter size greater than size of 2015 // all remained GPR regs. In that case we can't split parameter, we must 2016 // send it to stack. We also must set NCRN to R4, so waste all 2017 // remained registers. 2018 const unsigned NSAAOffset = State->getNextStackOffset(); 2019 if (NSAAOffset != 0 && Size > Excess) { 2020 while (State->AllocateReg(GPRArgRegs)) 2021 ; 2022 return; 2023 } 2024 2025 // First register for byval parameter is the first register that wasn't 2026 // allocated before this method call, so it would be "reg". 2027 // If parameter is small enough to be saved in range [reg, r4), then 2028 // the end (first after last) register would be reg + param-size-in-regs, 2029 // else parameter would be splitted between registers and stack, 2030 // end register would be r4 in this case. 2031 unsigned ByValRegBegin = Reg; 2032 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4); 2033 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd); 2034 // Note, first register is allocated in the beginning of function already, 2035 // allocate remained amount of registers we need. 2036 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i) 2037 State->AllocateReg(GPRArgRegs); 2038 // A byval parameter that is split between registers and memory needs its 2039 // size truncated here. 2040 // In the case where the entire structure fits in registers, we set the 2041 // size in memory to zero. 2042 Size = std::max<int>(Size - Excess, 0); 2043 } 2044 2045 /// MatchingStackOffset - Return true if the given stack call argument is 2046 /// already available in the same position (relatively) of the caller's 2047 /// incoming argument stack. 2048 static 2049 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, 2050 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI, 2051 const TargetInstrInfo *TII) { 2052 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8; 2053 int FI = INT_MAX; 2054 if (Arg.getOpcode() == ISD::CopyFromReg) { 2055 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); 2056 if (!TargetRegisterInfo::isVirtualRegister(VR)) 2057 return false; 2058 MachineInstr *Def = MRI->getVRegDef(VR); 2059 if (!Def) 2060 return false; 2061 if (!Flags.isByVal()) { 2062 if (!TII->isLoadFromStackSlot(Def, FI)) 2063 return false; 2064 } else { 2065 return false; 2066 } 2067 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { 2068 if (Flags.isByVal()) 2069 // ByVal argument is passed in as a pointer but it's now being 2070 // dereferenced. e.g. 2071 // define @foo(%struct.X* %A) { 2072 // tail call @bar(%struct.X* byval %A) 2073 // } 2074 return false; 2075 SDValue Ptr = Ld->getBasePtr(); 2076 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); 2077 if (!FINode) 2078 return false; 2079 FI = FINode->getIndex(); 2080 } else 2081 return false; 2082 2083 assert(FI != INT_MAX); 2084 if (!MFI->isFixedObjectIndex(FI)) 2085 return false; 2086 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI); 2087 } 2088 2089 /// IsEligibleForTailCallOptimization - Check whether the call is eligible 2090 /// for tail call optimization. Targets which want to do tail call 2091 /// optimization should implement this function. 2092 bool 2093 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, 2094 CallingConv::ID CalleeCC, 2095 bool isVarArg, 2096 bool isCalleeStructRet, 2097 bool isCallerStructRet, 2098 const SmallVectorImpl<ISD::OutputArg> &Outs, 2099 const SmallVectorImpl<SDValue> &OutVals, 2100 const SmallVectorImpl<ISD::InputArg> &Ins, 2101 SelectionDAG& DAG) const { 2102 MachineFunction &MF = DAG.getMachineFunction(); 2103 const Function *CallerF = MF.getFunction(); 2104 CallingConv::ID CallerCC = CallerF->getCallingConv(); 2105 2106 assert(Subtarget->supportsTailCall()); 2107 2108 // Look for obvious safe cases to perform tail call optimization that do not 2109 // require ABI changes. This is what gcc calls sibcall. 2110 2111 // Do not sibcall optimize vararg calls unless the call site is not passing 2112 // any arguments. 2113 if (isVarArg && !Outs.empty()) 2114 return false; 2115 2116 // Exception-handling functions need a special set of instructions to indicate 2117 // a return to the hardware. Tail-calling another function would probably 2118 // break this. 2119 if (CallerF->hasFnAttribute("interrupt")) 2120 return false; 2121 2122 // Also avoid sibcall optimization if either caller or callee uses struct 2123 // return semantics. 2124 if (isCalleeStructRet || isCallerStructRet) 2125 return false; 2126 2127 // Externally-defined functions with weak linkage should not be 2128 // tail-called on ARM when the OS does not support dynamic 2129 // pre-emption of symbols, as the AAELF spec requires normal calls 2130 // to undefined weak functions to be replaced with a NOP or jump to the 2131 // next instruction. The behaviour of branch instructions in this 2132 // situation (as used for tail calls) is implementation-defined, so we 2133 // cannot rely on the linker replacing the tail call with a return. 2134 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 2135 const GlobalValue *GV = G->getGlobal(); 2136 const Triple &TT = getTargetMachine().getTargetTriple(); 2137 if (GV->hasExternalWeakLinkage() && 2138 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO())) 2139 return false; 2140 } 2141 2142 // Check that the call results are passed in the same way. 2143 LLVMContext &C = *DAG.getContext(); 2144 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins, 2145 CCAssignFnForNode(CalleeCC, true, isVarArg), 2146 CCAssignFnForNode(CallerCC, true, isVarArg))) 2147 return false; 2148 // The callee has to preserve all registers the caller needs to preserve. 2149 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2150 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 2151 if (CalleeCC != CallerCC) { 2152 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); 2153 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) 2154 return false; 2155 } 2156 2157 // If Caller's vararg or byval argument has been split between registers and 2158 // stack, do not perform tail call, since part of the argument is in caller's 2159 // local frame. 2160 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>(); 2161 if (AFI_Caller->getArgRegsSaveSize()) 2162 return false; 2163 2164 // If the callee takes no arguments then go on to check the results of the 2165 // call. 2166 if (!Outs.empty()) { 2167 // Check if stack adjustment is needed. For now, do not do this if any 2168 // argument is passed on the stack. 2169 SmallVector<CCValAssign, 16> ArgLocs; 2170 ARMCCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C, Call); 2171 CCInfo.AnalyzeCallOperands(Outs, 2172 CCAssignFnForNode(CalleeCC, false, isVarArg)); 2173 if (CCInfo.getNextStackOffset()) { 2174 // Check if the arguments are already laid out in the right way as 2175 // the caller's fixed stack objects. 2176 MachineFrameInfo *MFI = MF.getFrameInfo(); 2177 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 2178 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 2179 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); 2180 i != e; 2181 ++i, ++realArgIdx) { 2182 CCValAssign &VA = ArgLocs[i]; 2183 EVT RegVT = VA.getLocVT(); 2184 SDValue Arg = OutVals[realArgIdx]; 2185 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2186 if (VA.getLocInfo() == CCValAssign::Indirect) 2187 return false; 2188 if (VA.needsCustom()) { 2189 // f64 and vector types are split into multiple registers or 2190 // register/stack-slot combinations. The types will not match 2191 // the registers; give up on memory f64 refs until we figure 2192 // out what to do about this. 2193 if (!VA.isRegLoc()) 2194 return false; 2195 if (!ArgLocs[++i].isRegLoc()) 2196 return false; 2197 if (RegVT == MVT::v2f64) { 2198 if (!ArgLocs[++i].isRegLoc()) 2199 return false; 2200 if (!ArgLocs[++i].isRegLoc()) 2201 return false; 2202 } 2203 } else if (!VA.isRegLoc()) { 2204 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, 2205 MFI, MRI, TII)) 2206 return false; 2207 } 2208 } 2209 } 2210 2211 const MachineRegisterInfo &MRI = MF.getRegInfo(); 2212 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals)) 2213 return false; 2214 } 2215 2216 return true; 2217 } 2218 2219 bool 2220 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv, 2221 MachineFunction &MF, bool isVarArg, 2222 const SmallVectorImpl<ISD::OutputArg> &Outs, 2223 LLVMContext &Context) const { 2224 SmallVector<CCValAssign, 16> RVLocs; 2225 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); 2226 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true, 2227 isVarArg)); 2228 } 2229 2230 static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps, 2231 SDLoc DL, SelectionDAG &DAG) { 2232 const MachineFunction &MF = DAG.getMachineFunction(); 2233 const Function *F = MF.getFunction(); 2234 2235 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString(); 2236 2237 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset 2238 // version of the "preferred return address". These offsets affect the return 2239 // instruction if this is a return from PL1 without hypervisor extensions. 2240 // IRQ/FIQ: +4 "subs pc, lr, #4" 2241 // SWI: 0 "subs pc, lr, #0" 2242 // ABORT: +4 "subs pc, lr, #4" 2243 // UNDEF: +4/+2 "subs pc, lr, #0" 2244 // UNDEF varies depending on where the exception came from ARM or Thumb 2245 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0. 2246 2247 int64_t LROffset; 2248 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" || 2249 IntKind == "ABORT") 2250 LROffset = 4; 2251 else if (IntKind == "SWI" || IntKind == "UNDEF") 2252 LROffset = 0; 2253 else 2254 report_fatal_error("Unsupported interrupt attribute. If present, value " 2255 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF"); 2256 2257 RetOps.insert(RetOps.begin() + 1, 2258 DAG.getConstant(LROffset, DL, MVT::i32, false)); 2259 2260 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps); 2261 } 2262 2263 SDValue 2264 ARMTargetLowering::LowerReturn(SDValue Chain, 2265 CallingConv::ID CallConv, bool isVarArg, 2266 const SmallVectorImpl<ISD::OutputArg> &Outs, 2267 const SmallVectorImpl<SDValue> &OutVals, 2268 SDLoc dl, SelectionDAG &DAG) const { 2269 2270 // CCValAssign - represent the assignment of the return value to a location. 2271 SmallVector<CCValAssign, 16> RVLocs; 2272 2273 // CCState - Info about the registers and stack slots. 2274 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2275 *DAG.getContext(), Call); 2276 2277 // Analyze outgoing return values. 2278 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true, 2279 isVarArg)); 2280 2281 SDValue Flag; 2282 SmallVector<SDValue, 4> RetOps; 2283 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2284 bool isLittleEndian = Subtarget->isLittle(); 2285 2286 MachineFunction &MF = DAG.getMachineFunction(); 2287 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2288 AFI->setReturnRegsCount(RVLocs.size()); 2289 2290 // Copy the result values into the output registers. 2291 for (unsigned i = 0, realRVLocIdx = 0; 2292 i != RVLocs.size(); 2293 ++i, ++realRVLocIdx) { 2294 CCValAssign &VA = RVLocs[i]; 2295 assert(VA.isRegLoc() && "Can only return in registers!"); 2296 2297 SDValue Arg = OutVals[realRVLocIdx]; 2298 2299 switch (VA.getLocInfo()) { 2300 default: llvm_unreachable("Unknown loc info!"); 2301 case CCValAssign::Full: break; 2302 case CCValAssign::BCvt: 2303 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 2304 break; 2305 } 2306 2307 if (VA.needsCustom()) { 2308 if (VA.getLocVT() == MVT::v2f64) { 2309 // Extract the first half and return it in two registers. 2310 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2311 DAG.getConstant(0, dl, MVT::i32)); 2312 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl, 2313 DAG.getVTList(MVT::i32, MVT::i32), Half); 2314 2315 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2316 HalfGPRs.getValue(isLittleEndian ? 0 : 1), 2317 Flag); 2318 Flag = Chain.getValue(1); 2319 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2320 VA = RVLocs[++i]; // skip ahead to next loc 2321 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2322 HalfGPRs.getValue(isLittleEndian ? 1 : 0), 2323 Flag); 2324 Flag = Chain.getValue(1); 2325 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2326 VA = RVLocs[++i]; // skip ahead to next loc 2327 2328 // Extract the 2nd half and fall through to handle it as an f64 value. 2329 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg, 2330 DAG.getConstant(1, dl, MVT::i32)); 2331 } 2332 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is 2333 // available. 2334 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl, 2335 DAG.getVTList(MVT::i32, MVT::i32), Arg); 2336 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2337 fmrrd.getValue(isLittleEndian ? 0 : 1), 2338 Flag); 2339 Flag = Chain.getValue(1); 2340 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2341 VA = RVLocs[++i]; // skip ahead to next loc 2342 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 2343 fmrrd.getValue(isLittleEndian ? 1 : 0), 2344 Flag); 2345 } else 2346 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); 2347 2348 // Guarantee that all emitted copies are 2349 // stuck together, avoiding something bad. 2350 Flag = Chain.getValue(1); 2351 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2352 } 2353 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2354 const MCPhysReg *I = 2355 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); 2356 if (I) { 2357 for (; *I; ++I) { 2358 if (ARM::GPRRegClass.contains(*I)) 2359 RetOps.push_back(DAG.getRegister(*I, MVT::i32)); 2360 else if (ARM::DPRRegClass.contains(*I)) 2361 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64))); 2362 else 2363 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 2364 } 2365 } 2366 2367 // Update chain and glue. 2368 RetOps[0] = Chain; 2369 if (Flag.getNode()) 2370 RetOps.push_back(Flag); 2371 2372 // CPUs which aren't M-class use a special sequence to return from 2373 // exceptions (roughly, any instruction setting pc and cpsr simultaneously, 2374 // though we use "subs pc, lr, #N"). 2375 // 2376 // M-class CPUs actually use a normal return sequence with a special 2377 // (hardware-provided) value in LR, so the normal code path works. 2378 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") && 2379 !Subtarget->isMClass()) { 2380 if (Subtarget->isThumb1Only()) 2381 report_fatal_error("interrupt attribute is not supported in Thumb1"); 2382 return LowerInterruptReturn(RetOps, dl, DAG); 2383 } 2384 2385 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps); 2386 } 2387 2388 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { 2389 if (N->getNumValues() != 1) 2390 return false; 2391 if (!N->hasNUsesOfValue(1, 0)) 2392 return false; 2393 2394 SDValue TCChain = Chain; 2395 SDNode *Copy = *N->use_begin(); 2396 if (Copy->getOpcode() == ISD::CopyToReg) { 2397 // If the copy has a glue operand, we conservatively assume it isn't safe to 2398 // perform a tail call. 2399 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2400 return false; 2401 TCChain = Copy->getOperand(0); 2402 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) { 2403 SDNode *VMov = Copy; 2404 // f64 returned in a pair of GPRs. 2405 SmallPtrSet<SDNode*, 2> Copies; 2406 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2407 UI != UE; ++UI) { 2408 if (UI->getOpcode() != ISD::CopyToReg) 2409 return false; 2410 Copies.insert(*UI); 2411 } 2412 if (Copies.size() > 2) 2413 return false; 2414 2415 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end(); 2416 UI != UE; ++UI) { 2417 SDValue UseChain = UI->getOperand(0); 2418 if (Copies.count(UseChain.getNode())) 2419 // Second CopyToReg 2420 Copy = *UI; 2421 else { 2422 // We are at the top of this chain. 2423 // If the copy has a glue operand, we conservatively assume it 2424 // isn't safe to perform a tail call. 2425 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue) 2426 return false; 2427 // First CopyToReg 2428 TCChain = UseChain; 2429 } 2430 } 2431 } else if (Copy->getOpcode() == ISD::BITCAST) { 2432 // f32 returned in a single GPR. 2433 if (!Copy->hasOneUse()) 2434 return false; 2435 Copy = *Copy->use_begin(); 2436 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0)) 2437 return false; 2438 // If the copy has a glue operand, we conservatively assume it isn't safe to 2439 // perform a tail call. 2440 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) 2441 return false; 2442 TCChain = Copy->getOperand(0); 2443 } else { 2444 return false; 2445 } 2446 2447 bool HasRet = false; 2448 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); 2449 UI != UE; ++UI) { 2450 if (UI->getOpcode() != ARMISD::RET_FLAG && 2451 UI->getOpcode() != ARMISD::INTRET_FLAG) 2452 return false; 2453 HasRet = true; 2454 } 2455 2456 if (!HasRet) 2457 return false; 2458 2459 Chain = TCChain; 2460 return true; 2461 } 2462 2463 bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 2464 if (!Subtarget->supportsTailCall()) 2465 return false; 2466 2467 auto Attr = 2468 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls"); 2469 if (!CI->isTailCall() || Attr.getValueAsString() == "true") 2470 return false; 2471 2472 return true; 2473 } 2474 2475 // Trying to write a 64 bit value so need to split into two 32 bit values first, 2476 // and pass the lower and high parts through. 2477 static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) { 2478 SDLoc DL(Op); 2479 SDValue WriteValue = Op->getOperand(2); 2480 2481 // This function is only supposed to be called for i64 type argument. 2482 assert(WriteValue.getValueType() == MVT::i64 2483 && "LowerWRITE_REGISTER called for non-i64 type argument."); 2484 2485 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 2486 DAG.getConstant(0, DL, MVT::i32)); 2487 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue, 2488 DAG.getConstant(1, DL, MVT::i32)); 2489 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi }; 2490 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops); 2491 } 2492 2493 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 2494 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is 2495 // one of the above mentioned nodes. It has to be wrapped because otherwise 2496 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only 2497 // be used to form addressing mode. These wrapped nodes will be selected 2498 // into MOVi. 2499 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) { 2500 EVT PtrVT = Op.getValueType(); 2501 // FIXME there is no actual debug info here 2502 SDLoc dl(Op); 2503 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); 2504 SDValue Res; 2505 if (CP->isMachineConstantPoolEntry()) 2506 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 2507 CP->getAlignment()); 2508 else 2509 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 2510 CP->getAlignment()); 2511 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res); 2512 } 2513 2514 unsigned ARMTargetLowering::getJumpTableEncoding() const { 2515 return MachineJumpTableInfo::EK_Inline; 2516 } 2517 2518 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, 2519 SelectionDAG &DAG) const { 2520 MachineFunction &MF = DAG.getMachineFunction(); 2521 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2522 unsigned ARMPCLabelIndex = 0; 2523 SDLoc DL(Op); 2524 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2525 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); 2526 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2527 SDValue CPAddr; 2528 if (RelocM == Reloc::Static) { 2529 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4); 2530 } else { 2531 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2532 ARMPCLabelIndex = AFI->createPICLabelUId(); 2533 ARMConstantPoolValue *CPV = 2534 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex, 2535 ARMCP::CPBlockAddress, PCAdj); 2536 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2537 } 2538 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr); 2539 SDValue Result = 2540 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr, 2541 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 2542 false, false, false, 0); 2543 if (RelocM == Reloc::Static) 2544 return Result; 2545 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32); 2546 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel); 2547 } 2548 2549 /// \brief Convert a TLS address reference into the correct sequence of loads 2550 /// and calls to compute the variable's address for Darwin, and return an 2551 /// SDValue containing the final node. 2552 2553 /// Darwin only has one TLS scheme which must be capable of dealing with the 2554 /// fully general situation, in the worst case. This means: 2555 /// + "extern __thread" declaration. 2556 /// + Defined in a possibly unknown dynamic library. 2557 /// 2558 /// The general system is that each __thread variable has a [3 x i32] descriptor 2559 /// which contains information used by the runtime to calculate the address. The 2560 /// only part of this the compiler needs to know about is the first word, which 2561 /// contains a function pointer that must be called with the address of the 2562 /// entire descriptor in "r0". 2563 /// 2564 /// Since this descriptor may be in a different unit, in general access must 2565 /// proceed along the usual ARM rules. A common sequence to produce is: 2566 /// 2567 /// movw rT1, :lower16:_var$non_lazy_ptr 2568 /// movt rT1, :upper16:_var$non_lazy_ptr 2569 /// ldr r0, [rT1] 2570 /// ldr rT2, [r0] 2571 /// blx rT2 2572 /// [...address now in r0...] 2573 SDValue 2574 ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op, 2575 SelectionDAG &DAG) const { 2576 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin"); 2577 SDLoc DL(Op); 2578 2579 // First step is to get the address of the actua global symbol. This is where 2580 // the TLS descriptor lives. 2581 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG); 2582 2583 // The first entry in the descriptor is a function pointer that we must call 2584 // to obtain the address of the variable. 2585 SDValue Chain = DAG.getEntryNode(); 2586 SDValue FuncTLVGet = 2587 DAG.getLoad(MVT::i32, DL, Chain, DescAddr, 2588 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2589 false, true, true, 4); 2590 Chain = FuncTLVGet.getValue(1); 2591 2592 MachineFunction &F = DAG.getMachineFunction(); 2593 MachineFrameInfo *MFI = F.getFrameInfo(); 2594 MFI->setAdjustsStack(true); 2595 2596 // TLS calls preserve all registers except those that absolutely must be 2597 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be 2598 // silly). 2599 auto TRI = 2600 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo(); 2601 auto ARI = static_cast<const ARMRegisterInfo *>(TRI); 2602 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction()); 2603 2604 // Finally, we can make the call. This is just a degenerate version of a 2605 // normal AArch64 call node: r0 takes the address of the descriptor, and 2606 // returns the address of the variable in this thread. 2607 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue()); 2608 Chain = 2609 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue), 2610 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32), 2611 DAG.getRegisterMask(Mask), Chain.getValue(1)); 2612 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1)); 2613 } 2614 2615 SDValue 2616 ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op, 2617 SelectionDAG &DAG) const { 2618 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering"); 2619 SDValue Chain = DAG.getEntryNode(); 2620 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2621 SDLoc DL(Op); 2622 2623 // Load the current TEB (thread environment block) 2624 SDValue Ops[] = {Chain, 2625 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32), 2626 DAG.getConstant(15, DL, MVT::i32), 2627 DAG.getConstant(0, DL, MVT::i32), 2628 DAG.getConstant(13, DL, MVT::i32), 2629 DAG.getConstant(0, DL, MVT::i32), 2630 DAG.getConstant(2, DL, MVT::i32)}; 2631 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 2632 DAG.getVTList(MVT::i32, MVT::Other), Ops); 2633 2634 SDValue TEB = CurrentTEB.getValue(0); 2635 Chain = CurrentTEB.getValue(1); 2636 2637 // Load the ThreadLocalStoragePointer from the TEB 2638 // A pointer to the TLS array is located at offset 0x2c from the TEB. 2639 SDValue TLSArray = 2640 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL)); 2641 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo(), 2642 false, false, false, 0); 2643 2644 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4 2645 // offset into the TLSArray. 2646 2647 // Load the TLS index from the C runtime 2648 SDValue TLSIndex = 2649 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG); 2650 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex); 2651 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo(), 2652 false, false, false, 0); 2653 2654 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex, 2655 DAG.getConstant(2, DL, MVT::i32)); 2656 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain, 2657 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot), 2658 MachinePointerInfo(), false, false, false, 0); 2659 2660 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, 2661 LowerGlobalAddressWindows(Op, DAG)); 2662 } 2663 2664 // Lower ISD::GlobalTLSAddress using the "general dynamic" model 2665 SDValue 2666 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, 2667 SelectionDAG &DAG) const { 2668 SDLoc dl(GA); 2669 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2670 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2671 MachineFunction &MF = DAG.getMachineFunction(); 2672 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2673 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2674 ARMConstantPoolValue *CPV = 2675 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2676 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true); 2677 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2678 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument); 2679 Argument = 2680 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, 2681 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 2682 false, false, false, 0); 2683 SDValue Chain = Argument.getValue(1); 2684 2685 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2686 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel); 2687 2688 // call __tls_get_addr. 2689 ArgListTy Args; 2690 ArgListEntry Entry; 2691 Entry.Node = Argument; 2692 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext()); 2693 Args.push_back(Entry); 2694 2695 // FIXME: is there useful debug info available here? 2696 TargetLowering::CallLoweringInfo CLI(DAG); 2697 CLI.setDebugLoc(dl).setChain(Chain) 2698 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()), 2699 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args), 2700 0); 2701 2702 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 2703 return CallResult.first; 2704 } 2705 2706 // Lower ISD::GlobalTLSAddress using the "initial exec" or 2707 // "local exec" model. 2708 SDValue 2709 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA, 2710 SelectionDAG &DAG, 2711 TLSModel::Model model) const { 2712 const GlobalValue *GV = GA->getGlobal(); 2713 SDLoc dl(GA); 2714 SDValue Offset; 2715 SDValue Chain = DAG.getEntryNode(); 2716 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2717 // Get the Thread Pointer 2718 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2719 2720 if (model == TLSModel::InitialExec) { 2721 MachineFunction &MF = DAG.getMachineFunction(); 2722 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2723 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2724 // Initial exec model. 2725 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8; 2726 ARMConstantPoolValue *CPV = 2727 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex, 2728 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, 2729 true); 2730 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2731 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2732 Offset = DAG.getLoad( 2733 PtrVT, dl, Chain, Offset, 2734 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2735 false, false, 0); 2736 Chain = Offset.getValue(1); 2737 2738 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2739 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel); 2740 2741 Offset = DAG.getLoad( 2742 PtrVT, dl, Chain, Offset, 2743 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2744 false, false, 0); 2745 } else { 2746 // local exec model 2747 assert(model == TLSModel::LocalExec); 2748 ARMConstantPoolValue *CPV = 2749 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF); 2750 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2751 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset); 2752 Offset = DAG.getLoad( 2753 PtrVT, dl, Chain, Offset, 2754 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2755 false, false, 0); 2756 } 2757 2758 // The address of the thread local variable is the add of the thread 2759 // pointer with the offset of the variable. 2760 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); 2761 } 2762 2763 SDValue 2764 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { 2765 if (Subtarget->isTargetDarwin()) 2766 return LowerGlobalTLSAddressDarwin(Op, DAG); 2767 2768 if (Subtarget->isTargetWindows()) 2769 return LowerGlobalTLSAddressWindows(Op, DAG); 2770 2771 // TODO: implement the "local dynamic" model 2772 assert(Subtarget->isTargetELF() && "Only ELF implemented here"); 2773 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); 2774 if (DAG.getTarget().Options.EmulatedTLS) 2775 return LowerToTLSEmulatedModel(GA, DAG); 2776 2777 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal()); 2778 2779 switch (model) { 2780 case TLSModel::GeneralDynamic: 2781 case TLSModel::LocalDynamic: 2782 return LowerToTLSGeneralDynamicModel(GA, DAG); 2783 case TLSModel::InitialExec: 2784 case TLSModel::LocalExec: 2785 return LowerToTLSExecModels(GA, DAG, model); 2786 } 2787 llvm_unreachable("bogus TLS model"); 2788 } 2789 2790 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op, 2791 SelectionDAG &DAG) const { 2792 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2793 SDLoc dl(Op); 2794 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2795 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 2796 bool UseGOT_PREL = 2797 !(GV->hasHiddenVisibility() || GV->hasLocalLinkage()); 2798 2799 MachineFunction &MF = DAG.getMachineFunction(); 2800 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2801 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2802 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2803 SDLoc dl(Op); 2804 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8; 2805 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create( 2806 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj, 2807 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier, 2808 /*AddCurrentAddress=*/UseGOT_PREL); 2809 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2810 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2811 SDValue Result = DAG.getLoad( 2812 PtrVT, dl, DAG.getEntryNode(), CPAddr, 2813 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2814 false, false, 0); 2815 SDValue Chain = Result.getValue(1); 2816 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2817 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2818 if (UseGOT_PREL) 2819 Result = DAG.getLoad(PtrVT, dl, Chain, Result, 2820 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2821 false, false, false, 0); 2822 return Result; 2823 } 2824 2825 // If we have T2 ops, we can materialize the address directly via movt/movw 2826 // pair. This is always cheaper. 2827 if (Subtarget->useMovt(DAG.getMachineFunction())) { 2828 ++NumMovwMovt; 2829 // FIXME: Once remat is capable of dealing with instructions with register 2830 // operands, expand this into two nodes. 2831 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT, 2832 DAG.getTargetGlobalAddress(GV, dl, PtrVT)); 2833 } else { 2834 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4); 2835 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2836 return DAG.getLoad( 2837 PtrVT, dl, DAG.getEntryNode(), CPAddr, 2838 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2839 false, false, 0); 2840 } 2841 } 2842 2843 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op, 2844 SelectionDAG &DAG) const { 2845 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2846 SDLoc dl(Op); 2847 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2848 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2849 2850 if (Subtarget->useMovt(DAG.getMachineFunction())) 2851 ++NumMovwMovt; 2852 2853 // FIXME: Once remat is capable of dealing with instructions with register 2854 // operands, expand this into multiple nodes 2855 unsigned Wrapper = 2856 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper; 2857 2858 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY); 2859 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G); 2860 2861 if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) 2862 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, 2863 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2864 false, false, false, 0); 2865 return Result; 2866 } 2867 2868 SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op, 2869 SelectionDAG &DAG) const { 2870 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported"); 2871 assert(Subtarget->useMovt(DAG.getMachineFunction()) && 2872 "Windows on ARM expects to use movw/movt"); 2873 2874 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); 2875 const ARMII::TOF TargetFlags = 2876 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG); 2877 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2878 SDValue Result; 2879 SDLoc DL(Op); 2880 2881 ++NumMovwMovt; 2882 2883 // FIXME: Once remat is capable of dealing with instructions with register 2884 // operands, expand this into two nodes. 2885 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, 2886 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0, 2887 TargetFlags)); 2888 if (GV->hasDLLImportStorageClass()) 2889 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, 2890 MachinePointerInfo::getGOT(DAG.getMachineFunction()), 2891 false, false, false, 0); 2892 return Result; 2893 } 2894 2895 SDValue 2896 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const { 2897 SDLoc dl(Op); 2898 SDValue Val = DAG.getConstant(0, dl, MVT::i32); 2899 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, 2900 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0), 2901 Op.getOperand(1), Val); 2902 } 2903 2904 SDValue 2905 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const { 2906 SDLoc dl(Op); 2907 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0), 2908 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32)); 2909 } 2910 2911 SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op, 2912 SelectionDAG &DAG) const { 2913 SDLoc dl(Op); 2914 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other, 2915 Op.getOperand(0)); 2916 } 2917 2918 SDValue 2919 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG, 2920 const ARMSubtarget *Subtarget) const { 2921 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2922 SDLoc dl(Op); 2923 switch (IntNo) { 2924 default: return SDValue(); // Don't custom lower most intrinsics. 2925 case Intrinsic::arm_rbit: { 2926 assert(Op.getOperand(1).getValueType() == MVT::i32 && 2927 "RBIT intrinsic must have i32 type!"); 2928 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1)); 2929 } 2930 case Intrinsic::arm_thread_pointer: { 2931 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2932 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT); 2933 } 2934 case Intrinsic::eh_sjlj_lsda: { 2935 MachineFunction &MF = DAG.getMachineFunction(); 2936 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 2937 unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); 2938 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 2939 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 2940 SDValue CPAddr; 2941 unsigned PCAdj = (RelocM != Reloc::PIC_) 2942 ? 0 : (Subtarget->isThumb() ? 4 : 8); 2943 ARMConstantPoolValue *CPV = 2944 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex, 2945 ARMCP::CPLSDA, PCAdj); 2946 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4); 2947 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr); 2948 SDValue Result = DAG.getLoad( 2949 PtrVT, dl, DAG.getEntryNode(), CPAddr, 2950 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, 2951 false, false, 0); 2952 2953 if (RelocM == Reloc::PIC_) { 2954 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32); 2955 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel); 2956 } 2957 return Result; 2958 } 2959 case Intrinsic::arm_neon_vmulls: 2960 case Intrinsic::arm_neon_vmullu: { 2961 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls) 2962 ? ARMISD::VMULLs : ARMISD::VMULLu; 2963 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 2964 Op.getOperand(1), Op.getOperand(2)); 2965 } 2966 case Intrinsic::arm_neon_vminnm: 2967 case Intrinsic::arm_neon_vmaxnm: { 2968 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm) 2969 ? ISD::FMINNUM : ISD::FMAXNUM; 2970 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 2971 Op.getOperand(1), Op.getOperand(2)); 2972 } 2973 case Intrinsic::arm_neon_vminu: 2974 case Intrinsic::arm_neon_vmaxu: { 2975 if (Op.getValueType().isFloatingPoint()) 2976 return SDValue(); 2977 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu) 2978 ? ISD::UMIN : ISD::UMAX; 2979 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 2980 Op.getOperand(1), Op.getOperand(2)); 2981 } 2982 case Intrinsic::arm_neon_vmins: 2983 case Intrinsic::arm_neon_vmaxs: { 2984 // v{min,max}s is overloaded between signed integers and floats. 2985 if (!Op.getValueType().isFloatingPoint()) { 2986 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins) 2987 ? ISD::SMIN : ISD::SMAX; 2988 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 2989 Op.getOperand(1), Op.getOperand(2)); 2990 } 2991 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins) 2992 ? ISD::FMINNAN : ISD::FMAXNAN; 2993 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(), 2994 Op.getOperand(1), Op.getOperand(2)); 2995 } 2996 } 2997 } 2998 2999 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG, 3000 const ARMSubtarget *Subtarget) { 3001 // FIXME: handle "fence singlethread" more efficiently. 3002 SDLoc dl(Op); 3003 if (!Subtarget->hasDataBarrier()) { 3004 // Some ARMv6 cpus can support data barriers with an mcr instruction. 3005 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 3006 // here. 3007 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() && 3008 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!"); 3009 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0), 3010 DAG.getConstant(0, dl, MVT::i32)); 3011 } 3012 3013 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1)); 3014 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue()); 3015 ARM_MB::MemBOpt Domain = ARM_MB::ISH; 3016 if (Subtarget->isMClass()) { 3017 // Only a full system barrier exists in the M-class architectures. 3018 Domain = ARM_MB::SY; 3019 } else if (Subtarget->isSwift() && Ord == AtomicOrdering::Release) { 3020 // Swift happens to implement ISHST barriers in a way that's compatible with 3021 // Release semantics but weaker than ISH so we'd be fools not to use 3022 // it. Beware: other processors probably don't! 3023 Domain = ARM_MB::ISHST; 3024 } 3025 3026 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0), 3027 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32), 3028 DAG.getConstant(Domain, dl, MVT::i32)); 3029 } 3030 3031 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG, 3032 const ARMSubtarget *Subtarget) { 3033 // ARM pre v5TE and Thumb1 does not have preload instructions. 3034 if (!(Subtarget->isThumb2() || 3035 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps()))) 3036 // Just preserve the chain. 3037 return Op.getOperand(0); 3038 3039 SDLoc dl(Op); 3040 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1; 3041 if (!isRead && 3042 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension())) 3043 // ARMv7 with MP extension has PLDW. 3044 return Op.getOperand(0); 3045 3046 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 3047 if (Subtarget->isThumb()) { 3048 // Invert the bits. 3049 isRead = ~isRead & 1; 3050 isData = ~isData & 1; 3051 } 3052 3053 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0), 3054 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32), 3055 DAG.getConstant(isData, dl, MVT::i32)); 3056 } 3057 3058 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) { 3059 MachineFunction &MF = DAG.getMachineFunction(); 3060 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>(); 3061 3062 // vastart just stores the address of the VarArgsFrameIndex slot into the 3063 // memory location argument. 3064 SDLoc dl(Op); 3065 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 3066 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); 3067 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 3068 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), 3069 MachinePointerInfo(SV), false, false, 0); 3070 } 3071 3072 SDValue 3073 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA, 3074 SDValue &Root, SelectionDAG &DAG, 3075 SDLoc dl) const { 3076 MachineFunction &MF = DAG.getMachineFunction(); 3077 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3078 3079 const TargetRegisterClass *RC; 3080 if (AFI->isThumb1OnlyFunction()) 3081 RC = &ARM::tGPRRegClass; 3082 else 3083 RC = &ARM::GPRRegClass; 3084 3085 // Transform the arguments stored in physical registers into virtual ones. 3086 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3087 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 3088 3089 SDValue ArgValue2; 3090 if (NextVA.isMemLoc()) { 3091 MachineFrameInfo *MFI = MF.getFrameInfo(); 3092 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true); 3093 3094 // Create load node to retrieve arguments from the stack. 3095 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 3096 ArgValue2 = DAG.getLoad( 3097 MVT::i32, dl, Root, FIN, 3098 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false, 3099 false, false, 0); 3100 } else { 3101 Reg = MF.addLiveIn(NextVA.getLocReg(), RC); 3102 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32); 3103 } 3104 if (!Subtarget->isLittle()) 3105 std::swap (ArgValue, ArgValue2); 3106 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2); 3107 } 3108 3109 // The remaining GPRs hold either the beginning of variable-argument 3110 // data, or the beginning of an aggregate passed by value (usually 3111 // byval). Either way, we allocate stack slots adjacent to the data 3112 // provided by our caller, and store the unallocated registers there. 3113 // If this is a variadic function, the va_list pointer will begin with 3114 // these values; otherwise, this reassembles a (byval) structure that 3115 // was split between registers and memory. 3116 // Return: The frame index registers were stored into. 3117 int 3118 ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG, 3119 SDLoc dl, SDValue &Chain, 3120 const Value *OrigArg, 3121 unsigned InRegsParamRecordIdx, 3122 int ArgOffset, 3123 unsigned ArgSize) const { 3124 // Currently, two use-cases possible: 3125 // Case #1. Non-var-args function, and we meet first byval parameter. 3126 // Setup first unallocated register as first byval register; 3127 // eat all remained registers 3128 // (these two actions are performed by HandleByVal method). 3129 // Then, here, we initialize stack frame with 3130 // "store-reg" instructions. 3131 // Case #2. Var-args function, that doesn't contain byval parameters. 3132 // The same: eat all remained unallocated registers, 3133 // initialize stack frame. 3134 3135 MachineFunction &MF = DAG.getMachineFunction(); 3136 MachineFrameInfo *MFI = MF.getFrameInfo(); 3137 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3138 unsigned RBegin, REnd; 3139 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) { 3140 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd); 3141 } else { 3142 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 3143 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx]; 3144 REnd = ARM::R4; 3145 } 3146 3147 if (REnd != RBegin) 3148 ArgOffset = -4 * (ARM::R4 - RBegin); 3149 3150 auto PtrVT = getPointerTy(DAG.getDataLayout()); 3151 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false); 3152 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT); 3153 3154 SmallVector<SDValue, 4> MemOps; 3155 const TargetRegisterClass *RC = 3156 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 3157 3158 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) { 3159 unsigned VReg = MF.addLiveIn(Reg, RC); 3160 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 3161 SDValue Store = 3162 DAG.getStore(Val.getValue(1), dl, Val, FIN, 3163 MachinePointerInfo(OrigArg, 4 * i), false, false, 0); 3164 MemOps.push_back(Store); 3165 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT)); 3166 } 3167 3168 if (!MemOps.empty()) 3169 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps); 3170 return FrameIndex; 3171 } 3172 3173 // Setup stack frame, the va_list pointer will start from. 3174 void 3175 ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG, 3176 SDLoc dl, SDValue &Chain, 3177 unsigned ArgOffset, 3178 unsigned TotalArgRegsSaveSize, 3179 bool ForceMutable) const { 3180 MachineFunction &MF = DAG.getMachineFunction(); 3181 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3182 3183 // Try to store any remaining integer argument regs 3184 // to their spots on the stack so that they may be loaded by deferencing 3185 // the result of va_next. 3186 // If there is no regs to be stored, just point address after last 3187 // argument passed via stack. 3188 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr, 3189 CCInfo.getInRegsParamsCount(), 3190 CCInfo.getNextStackOffset(), 4); 3191 AFI->setVarArgsFrameIndex(FrameIndex); 3192 } 3193 3194 SDValue 3195 ARMTargetLowering::LowerFormalArguments(SDValue Chain, 3196 CallingConv::ID CallConv, bool isVarArg, 3197 const SmallVectorImpl<ISD::InputArg> 3198 &Ins, 3199 SDLoc dl, SelectionDAG &DAG, 3200 SmallVectorImpl<SDValue> &InVals) 3201 const { 3202 MachineFunction &MF = DAG.getMachineFunction(); 3203 MachineFrameInfo *MFI = MF.getFrameInfo(); 3204 3205 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3206 3207 // Assign locations to all of the incoming arguments. 3208 SmallVector<CCValAssign, 16> ArgLocs; 3209 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 3210 *DAG.getContext(), Prologue); 3211 CCInfo.AnalyzeFormalArguments(Ins, 3212 CCAssignFnForNode(CallConv, /* Return*/ false, 3213 isVarArg)); 3214 3215 SmallVector<SDValue, 16> ArgValues; 3216 SDValue ArgValue; 3217 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin(); 3218 unsigned CurArgIdx = 0; 3219 3220 // Initially ArgRegsSaveSize is zero. 3221 // Then we increase this value each time we meet byval parameter. 3222 // We also increase this value in case of varargs function. 3223 AFI->setArgRegsSaveSize(0); 3224 3225 // Calculate the amount of stack space that we need to allocate to store 3226 // byval and variadic arguments that are passed in registers. 3227 // We need to know this before we allocate the first byval or variadic 3228 // argument, as they will be allocated a stack slot below the CFA (Canonical 3229 // Frame Address, the stack pointer at entry to the function). 3230 unsigned ArgRegBegin = ARM::R4; 3231 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3232 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount()) 3233 break; 3234 3235 CCValAssign &VA = ArgLocs[i]; 3236 unsigned Index = VA.getValNo(); 3237 ISD::ArgFlagsTy Flags = Ins[Index].Flags; 3238 if (!Flags.isByVal()) 3239 continue; 3240 3241 assert(VA.isMemLoc() && "unexpected byval pointer in reg"); 3242 unsigned RBegin, REnd; 3243 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd); 3244 ArgRegBegin = std::min(ArgRegBegin, RBegin); 3245 3246 CCInfo.nextInRegsParam(); 3247 } 3248 CCInfo.rewindByValRegsInfo(); 3249 3250 int lastInsIndex = -1; 3251 if (isVarArg && MFI->hasVAStart()) { 3252 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs); 3253 if (RegIdx != array_lengthof(GPRArgRegs)) 3254 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]); 3255 } 3256 3257 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin); 3258 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize); 3259 auto PtrVT = getPointerTy(DAG.getDataLayout()); 3260 3261 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 3262 CCValAssign &VA = ArgLocs[i]; 3263 if (Ins[VA.getValNo()].isOrigArg()) { 3264 std::advance(CurOrigArg, 3265 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx); 3266 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex(); 3267 } 3268 // Arguments stored in registers. 3269 if (VA.isRegLoc()) { 3270 EVT RegVT = VA.getLocVT(); 3271 3272 if (VA.needsCustom()) { 3273 // f64 and vector types are split up into multiple registers or 3274 // combinations of registers and stack slots. 3275 if (VA.getLocVT() == MVT::v2f64) { 3276 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i], 3277 Chain, DAG, dl); 3278 VA = ArgLocs[++i]; // skip ahead to next loc 3279 SDValue ArgValue2; 3280 if (VA.isMemLoc()) { 3281 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true); 3282 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3283 ArgValue2 = DAG.getLoad( 3284 MVT::f64, dl, Chain, FIN, 3285 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), 3286 false, false, false, 0); 3287 } else { 3288 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i], 3289 Chain, DAG, dl); 3290 } 3291 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64); 3292 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3293 ArgValue, ArgValue1, 3294 DAG.getIntPtrConstant(0, dl)); 3295 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, 3296 ArgValue, ArgValue2, 3297 DAG.getIntPtrConstant(1, dl)); 3298 } else 3299 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl); 3300 3301 } else { 3302 const TargetRegisterClass *RC; 3303 3304 if (RegVT == MVT::f32) 3305 RC = &ARM::SPRRegClass; 3306 else if (RegVT == MVT::f64) 3307 RC = &ARM::DPRRegClass; 3308 else if (RegVT == MVT::v2f64) 3309 RC = &ARM::QPRRegClass; 3310 else if (RegVT == MVT::i32) 3311 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass 3312 : &ARM::GPRRegClass; 3313 else 3314 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering"); 3315 3316 // Transform the arguments in physical registers into virtual ones. 3317 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); 3318 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); 3319 } 3320 3321 // If this is an 8 or 16-bit value, it is really passed promoted 3322 // to 32 bits. Insert an assert[sz]ext to capture this, then 3323 // truncate to the right size. 3324 switch (VA.getLocInfo()) { 3325 default: llvm_unreachable("Unknown loc info!"); 3326 case CCValAssign::Full: break; 3327 case CCValAssign::BCvt: 3328 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); 3329 break; 3330 case CCValAssign::SExt: 3331 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, 3332 DAG.getValueType(VA.getValVT())); 3333 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3334 break; 3335 case CCValAssign::ZExt: 3336 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, 3337 DAG.getValueType(VA.getValVT())); 3338 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); 3339 break; 3340 } 3341 3342 InVals.push_back(ArgValue); 3343 3344 } else { // VA.isRegLoc() 3345 3346 // sanity check 3347 assert(VA.isMemLoc()); 3348 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered"); 3349 3350 int index = VA.getValNo(); 3351 3352 // Some Ins[] entries become multiple ArgLoc[] entries. 3353 // Process them only once. 3354 if (index != lastInsIndex) 3355 { 3356 ISD::ArgFlagsTy Flags = Ins[index].Flags; 3357 // FIXME: For now, all byval parameter objects are marked mutable. 3358 // This can be changed with more analysis. 3359 // In case of tail call optimization mark all arguments mutable. 3360 // Since they could be overwritten by lowering of arguments in case of 3361 // a tail call. 3362 if (Flags.isByVal()) { 3363 assert(Ins[index].isOrigArg() && 3364 "Byval arguments cannot be implicit"); 3365 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed(); 3366 3367 int FrameIndex = StoreByValRegs( 3368 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex, 3369 VA.getLocMemOffset(), Flags.getByValSize()); 3370 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT)); 3371 CCInfo.nextInRegsParam(); 3372 } else { 3373 unsigned FIOffset = VA.getLocMemOffset(); 3374 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8, 3375 FIOffset, true); 3376 3377 // Create load nodes to retrieve arguments from the stack. 3378 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 3379 InVals.push_back(DAG.getLoad( 3380 VA.getValVT(), dl, Chain, FIN, 3381 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), 3382 false, false, false, 0)); 3383 } 3384 lastInsIndex = index; 3385 } 3386 } 3387 } 3388 3389 // varargs 3390 if (isVarArg && MFI->hasVAStart()) 3391 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 3392 CCInfo.getNextStackOffset(), 3393 TotalArgRegsSaveSize); 3394 3395 AFI->setArgumentStackSize(CCInfo.getNextStackOffset()); 3396 3397 return Chain; 3398 } 3399 3400 /// isFloatingPointZero - Return true if this is +0.0. 3401 static bool isFloatingPointZero(SDValue Op) { 3402 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) 3403 return CFP->getValueAPF().isPosZero(); 3404 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { 3405 // Maybe this has already been legalized into the constant pool? 3406 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) { 3407 SDValue WrapperOp = Op.getOperand(1).getOperand(0); 3408 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp)) 3409 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 3410 return CFP->getValueAPF().isPosZero(); 3411 } 3412 } else if (Op->getOpcode() == ISD::BITCAST && 3413 Op->getValueType(0) == MVT::f64) { 3414 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64) 3415 // created by LowerConstantFP(). 3416 SDValue BitcastOp = Op->getOperand(0); 3417 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM && 3418 isNullConstant(BitcastOp->getOperand(0))) 3419 return true; 3420 } 3421 return false; 3422 } 3423 3424 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for 3425 /// the given operands. 3426 SDValue 3427 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC, 3428 SDValue &ARMcc, SelectionDAG &DAG, 3429 SDLoc dl) const { 3430 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) { 3431 unsigned C = RHSC->getZExtValue(); 3432 if (!isLegalICmpImmediate(C)) { 3433 // Constant does not fit, try adjusting it by one? 3434 switch (CC) { 3435 default: break; 3436 case ISD::SETLT: 3437 case ISD::SETGE: 3438 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) { 3439 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT; 3440 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 3441 } 3442 break; 3443 case ISD::SETULT: 3444 case ISD::SETUGE: 3445 if (C != 0 && isLegalICmpImmediate(C-1)) { 3446 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT; 3447 RHS = DAG.getConstant(C - 1, dl, MVT::i32); 3448 } 3449 break; 3450 case ISD::SETLE: 3451 case ISD::SETGT: 3452 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) { 3453 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE; 3454 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 3455 } 3456 break; 3457 case ISD::SETULE: 3458 case ISD::SETUGT: 3459 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) { 3460 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE; 3461 RHS = DAG.getConstant(C + 1, dl, MVT::i32); 3462 } 3463 break; 3464 } 3465 } 3466 } 3467 3468 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3469 ARMISD::NodeType CompareType; 3470 switch (CondCode) { 3471 default: 3472 CompareType = ARMISD::CMP; 3473 break; 3474 case ARMCC::EQ: 3475 case ARMCC::NE: 3476 // Uses only Z Flag 3477 CompareType = ARMISD::CMPZ; 3478 break; 3479 } 3480 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3481 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS); 3482 } 3483 3484 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands. 3485 SDValue 3486 ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG, 3487 SDLoc dl) const { 3488 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64); 3489 SDValue Cmp; 3490 if (!isFloatingPointZero(RHS)) 3491 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS); 3492 else 3493 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS); 3494 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp); 3495 } 3496 3497 /// duplicateCmp - Glue values can have only one use, so this function 3498 /// duplicates a comparison node. 3499 SDValue 3500 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const { 3501 unsigned Opc = Cmp.getOpcode(); 3502 SDLoc DL(Cmp); 3503 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ) 3504 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3505 3506 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation"); 3507 Cmp = Cmp.getOperand(0); 3508 Opc = Cmp.getOpcode(); 3509 if (Opc == ARMISD::CMPFP) 3510 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1)); 3511 else { 3512 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT"); 3513 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0)); 3514 } 3515 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp); 3516 } 3517 3518 std::pair<SDValue, SDValue> 3519 ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG, 3520 SDValue &ARMcc) const { 3521 assert(Op.getValueType() == MVT::i32 && "Unsupported value type"); 3522 3523 SDValue Value, OverflowCmp; 3524 SDValue LHS = Op.getOperand(0); 3525 SDValue RHS = Op.getOperand(1); 3526 SDLoc dl(Op); 3527 3528 // FIXME: We are currently always generating CMPs because we don't support 3529 // generating CMN through the backend. This is not as good as the natural 3530 // CMP case because it causes a register dependency and cannot be folded 3531 // later. 3532 3533 switch (Op.getOpcode()) { 3534 default: 3535 llvm_unreachable("Unknown overflow instruction!"); 3536 case ISD::SADDO: 3537 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 3538 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS); 3539 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 3540 break; 3541 case ISD::UADDO: 3542 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 3543 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS); 3544 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS); 3545 break; 3546 case ISD::SSUBO: 3547 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32); 3548 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 3549 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 3550 break; 3551 case ISD::USUBO: 3552 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32); 3553 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS); 3554 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS); 3555 break; 3556 } // switch (...) 3557 3558 return std::make_pair(Value, OverflowCmp); 3559 } 3560 3561 3562 SDValue 3563 ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const { 3564 // Let legalize expand this if it isn't a legal type yet. 3565 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType())) 3566 return SDValue(); 3567 3568 SDValue Value, OverflowCmp; 3569 SDValue ARMcc; 3570 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc); 3571 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3572 SDLoc dl(Op); 3573 // We use 0 and 1 as false and true values. 3574 SDValue TVal = DAG.getConstant(1, dl, MVT::i32); 3575 SDValue FVal = DAG.getConstant(0, dl, MVT::i32); 3576 EVT VT = Op.getValueType(); 3577 3578 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal, 3579 ARMcc, CCR, OverflowCmp); 3580 3581 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); 3582 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow); 3583 } 3584 3585 3586 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 3587 SDValue Cond = Op.getOperand(0); 3588 SDValue SelectTrue = Op.getOperand(1); 3589 SDValue SelectFalse = Op.getOperand(2); 3590 SDLoc dl(Op); 3591 unsigned Opc = Cond.getOpcode(); 3592 3593 if (Cond.getResNo() == 1 && 3594 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO || 3595 Opc == ISD::USUBO)) { 3596 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0))) 3597 return SDValue(); 3598 3599 SDValue Value, OverflowCmp; 3600 SDValue ARMcc; 3601 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc); 3602 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3603 EVT VT = Op.getValueType(); 3604 3605 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR, 3606 OverflowCmp, DAG); 3607 } 3608 3609 // Convert: 3610 // 3611 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond) 3612 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond) 3613 // 3614 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) { 3615 const ConstantSDNode *CMOVTrue = 3616 dyn_cast<ConstantSDNode>(Cond.getOperand(0)); 3617 const ConstantSDNode *CMOVFalse = 3618 dyn_cast<ConstantSDNode>(Cond.getOperand(1)); 3619 3620 if (CMOVTrue && CMOVFalse) { 3621 unsigned CMOVTrueVal = CMOVTrue->getZExtValue(); 3622 unsigned CMOVFalseVal = CMOVFalse->getZExtValue(); 3623 3624 SDValue True; 3625 SDValue False; 3626 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) { 3627 True = SelectTrue; 3628 False = SelectFalse; 3629 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) { 3630 True = SelectFalse; 3631 False = SelectTrue; 3632 } 3633 3634 if (True.getNode() && False.getNode()) { 3635 EVT VT = Op.getValueType(); 3636 SDValue ARMcc = Cond.getOperand(2); 3637 SDValue CCR = Cond.getOperand(3); 3638 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG); 3639 assert(True.getValueType() == VT); 3640 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG); 3641 } 3642 } 3643 } 3644 3645 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the 3646 // undefined bits before doing a full-word comparison with zero. 3647 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond, 3648 DAG.getConstant(1, dl, Cond.getValueType())); 3649 3650 return DAG.getSelectCC(dl, Cond, 3651 DAG.getConstant(0, dl, Cond.getValueType()), 3652 SelectTrue, SelectFalse, ISD::SETNE); 3653 } 3654 3655 static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode, 3656 bool &swpCmpOps, bool &swpVselOps) { 3657 // Start by selecting the GE condition code for opcodes that return true for 3658 // 'equality' 3659 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE || 3660 CC == ISD::SETULE) 3661 CondCode = ARMCC::GE; 3662 3663 // and GT for opcodes that return false for 'equality'. 3664 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT || 3665 CC == ISD::SETULT) 3666 CondCode = ARMCC::GT; 3667 3668 // Since we are constrained to GE/GT, if the opcode contains 'less', we need 3669 // to swap the compare operands. 3670 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT || 3671 CC == ISD::SETULT) 3672 swpCmpOps = true; 3673 3674 // Both GT and GE are ordered comparisons, and return false for 'unordered'. 3675 // If we have an unordered opcode, we need to swap the operands to the VSEL 3676 // instruction (effectively negating the condition). 3677 // 3678 // This also has the effect of swapping which one of 'less' or 'greater' 3679 // returns true, so we also swap the compare operands. It also switches 3680 // whether we return true for 'equality', so we compensate by picking the 3681 // opposite condition code to our original choice. 3682 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE || 3683 CC == ISD::SETUGT) { 3684 swpCmpOps = !swpCmpOps; 3685 swpVselOps = !swpVselOps; 3686 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT; 3687 } 3688 3689 // 'ordered' is 'anything but unordered', so use the VS condition code and 3690 // swap the VSEL operands. 3691 if (CC == ISD::SETO) { 3692 CondCode = ARMCC::VS; 3693 swpVselOps = true; 3694 } 3695 3696 // 'unordered or not equal' is 'anything but equal', so use the EQ condition 3697 // code and swap the VSEL operands. 3698 if (CC == ISD::SETUNE) { 3699 CondCode = ARMCC::EQ; 3700 swpVselOps = true; 3701 } 3702 } 3703 3704 SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal, 3705 SDValue TrueVal, SDValue ARMcc, SDValue CCR, 3706 SDValue Cmp, SelectionDAG &DAG) const { 3707 if (Subtarget->isFPOnlySP() && VT == MVT::f64) { 3708 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl, 3709 DAG.getVTList(MVT::i32, MVT::i32), FalseVal); 3710 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl, 3711 DAG.getVTList(MVT::i32, MVT::i32), TrueVal); 3712 3713 SDValue TrueLow = TrueVal.getValue(0); 3714 SDValue TrueHigh = TrueVal.getValue(1); 3715 SDValue FalseLow = FalseVal.getValue(0); 3716 SDValue FalseHigh = FalseVal.getValue(1); 3717 3718 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow, 3719 ARMcc, CCR, Cmp); 3720 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh, 3721 ARMcc, CCR, duplicateCmp(Cmp, DAG)); 3722 3723 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High); 3724 } else { 3725 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, 3726 Cmp); 3727 } 3728 } 3729 3730 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { 3731 EVT VT = Op.getValueType(); 3732 SDValue LHS = Op.getOperand(0); 3733 SDValue RHS = Op.getOperand(1); 3734 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 3735 SDValue TrueVal = Op.getOperand(2); 3736 SDValue FalseVal = Op.getOperand(3); 3737 SDLoc dl(Op); 3738 3739 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) { 3740 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 3741 dl); 3742 3743 // If softenSetCCOperands only returned one value, we should compare it to 3744 // zero. 3745 if (!RHS.getNode()) { 3746 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 3747 CC = ISD::SETNE; 3748 } 3749 } 3750 3751 if (LHS.getValueType() == MVT::i32) { 3752 // Try to generate VSEL on ARMv8. 3753 // The VSEL instruction can't use all the usual ARM condition 3754 // codes: it only has two bits to select the condition code, so it's 3755 // constrained to use only GE, GT, VS and EQ. 3756 // 3757 // To implement all the various ISD::SETXXX opcodes, we sometimes need to 3758 // swap the operands of the previous compare instruction (effectively 3759 // inverting the compare condition, swapping 'less' and 'greater') and 3760 // sometimes need to swap the operands to the VSEL (which inverts the 3761 // condition in the sense of firing whenever the previous condition didn't) 3762 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 || 3763 TrueVal.getValueType() == MVT::f64)) { 3764 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3765 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE || 3766 CondCode == ARMCC::VC || CondCode == ARMCC::NE) { 3767 CC = ISD::getSetCCInverse(CC, true); 3768 std::swap(TrueVal, FalseVal); 3769 } 3770 } 3771 3772 SDValue ARMcc; 3773 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3774 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3775 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 3776 } 3777 3778 ARMCC::CondCodes CondCode, CondCode2; 3779 FPCCToARMCC(CC, CondCode, CondCode2); 3780 3781 // Try to generate VMAXNM/VMINNM on ARMv8. 3782 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 || 3783 TrueVal.getValueType() == MVT::f64)) { 3784 bool swpCmpOps = false; 3785 bool swpVselOps = false; 3786 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps); 3787 3788 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE || 3789 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) { 3790 if (swpCmpOps) 3791 std::swap(LHS, RHS); 3792 if (swpVselOps) 3793 std::swap(TrueVal, FalseVal); 3794 } 3795 } 3796 3797 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3798 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3799 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3800 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG); 3801 if (CondCode2 != ARMCC::AL) { 3802 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32); 3803 // FIXME: Needs another CMP because flag can have but one use. 3804 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl); 3805 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG); 3806 } 3807 return Result; 3808 } 3809 3810 /// canChangeToInt - Given the fp compare operand, return true if it is suitable 3811 /// to morph to an integer compare sequence. 3812 static bool canChangeToInt(SDValue Op, bool &SeenZero, 3813 const ARMSubtarget *Subtarget) { 3814 SDNode *N = Op.getNode(); 3815 if (!N->hasOneUse()) 3816 // Otherwise it requires moving the value from fp to integer registers. 3817 return false; 3818 if (!N->getNumValues()) 3819 return false; 3820 EVT VT = Op.getValueType(); 3821 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow()) 3822 // f32 case is generally profitable. f64 case only makes sense when vcmpe + 3823 // vmrs are very slow, e.g. cortex-a8. 3824 return false; 3825 3826 if (isFloatingPointZero(Op)) { 3827 SeenZero = true; 3828 return true; 3829 } 3830 return ISD::isNormalLoad(N); 3831 } 3832 3833 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) { 3834 if (isFloatingPointZero(Op)) 3835 return DAG.getConstant(0, SDLoc(Op), MVT::i32); 3836 3837 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) 3838 return DAG.getLoad(MVT::i32, SDLoc(Op), 3839 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(), 3840 Ld->isVolatile(), Ld->isNonTemporal(), 3841 Ld->isInvariant(), Ld->getAlignment()); 3842 3843 llvm_unreachable("Unknown VFP cmp argument!"); 3844 } 3845 3846 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG, 3847 SDValue &RetVal1, SDValue &RetVal2) { 3848 SDLoc dl(Op); 3849 3850 if (isFloatingPointZero(Op)) { 3851 RetVal1 = DAG.getConstant(0, dl, MVT::i32); 3852 RetVal2 = DAG.getConstant(0, dl, MVT::i32); 3853 return; 3854 } 3855 3856 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) { 3857 SDValue Ptr = Ld->getBasePtr(); 3858 RetVal1 = DAG.getLoad(MVT::i32, dl, 3859 Ld->getChain(), Ptr, 3860 Ld->getPointerInfo(), 3861 Ld->isVolatile(), Ld->isNonTemporal(), 3862 Ld->isInvariant(), Ld->getAlignment()); 3863 3864 EVT PtrType = Ptr.getValueType(); 3865 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4); 3866 SDValue NewPtr = DAG.getNode(ISD::ADD, dl, 3867 PtrType, Ptr, DAG.getConstant(4, dl, PtrType)); 3868 RetVal2 = DAG.getLoad(MVT::i32, dl, 3869 Ld->getChain(), NewPtr, 3870 Ld->getPointerInfo().getWithOffset(4), 3871 Ld->isVolatile(), Ld->isNonTemporal(), 3872 Ld->isInvariant(), NewAlign); 3873 return; 3874 } 3875 3876 llvm_unreachable("Unknown VFP cmp argument!"); 3877 } 3878 3879 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some 3880 /// f32 and even f64 comparisons to integer ones. 3881 SDValue 3882 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const { 3883 SDValue Chain = Op.getOperand(0); 3884 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3885 SDValue LHS = Op.getOperand(2); 3886 SDValue RHS = Op.getOperand(3); 3887 SDValue Dest = Op.getOperand(4); 3888 SDLoc dl(Op); 3889 3890 bool LHSSeenZero = false; 3891 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget); 3892 bool RHSSeenZero = false; 3893 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget); 3894 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) { 3895 // If unsafe fp math optimization is enabled and there are no other uses of 3896 // the CMP operands, and the condition code is EQ or NE, we can optimize it 3897 // to an integer comparison. 3898 if (CC == ISD::SETOEQ) 3899 CC = ISD::SETEQ; 3900 else if (CC == ISD::SETUNE) 3901 CC = ISD::SETNE; 3902 3903 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32); 3904 SDValue ARMcc; 3905 if (LHS.getValueType() == MVT::f32) { 3906 LHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3907 bitcastf32Toi32(LHS, DAG), Mask); 3908 RHS = DAG.getNode(ISD::AND, dl, MVT::i32, 3909 bitcastf32Toi32(RHS, DAG), Mask); 3910 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3911 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3912 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3913 Chain, Dest, ARMcc, CCR, Cmp); 3914 } 3915 3916 SDValue LHS1, LHS2; 3917 SDValue RHS1, RHS2; 3918 expandf64Toi32(LHS, DAG, LHS1, LHS2); 3919 expandf64Toi32(RHS, DAG, RHS1, RHS2); 3920 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask); 3921 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask); 3922 ARMCC::CondCodes CondCode = IntCCToARMCC(CC); 3923 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3924 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3925 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest }; 3926 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops); 3927 } 3928 3929 return SDValue(); 3930 } 3931 3932 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 3933 SDValue Chain = Op.getOperand(0); 3934 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 3935 SDValue LHS = Op.getOperand(2); 3936 SDValue RHS = Op.getOperand(3); 3937 SDValue Dest = Op.getOperand(4); 3938 SDLoc dl(Op); 3939 3940 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) { 3941 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC, 3942 dl); 3943 3944 // If softenSetCCOperands only returned one value, we should compare it to 3945 // zero. 3946 if (!RHS.getNode()) { 3947 RHS = DAG.getConstant(0, dl, LHS.getValueType()); 3948 CC = ISD::SETNE; 3949 } 3950 } 3951 3952 if (LHS.getValueType() == MVT::i32) { 3953 SDValue ARMcc; 3954 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl); 3955 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3956 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, 3957 Chain, Dest, ARMcc, CCR, Cmp); 3958 } 3959 3960 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64); 3961 3962 if (getTargetMachine().Options.UnsafeFPMath && 3963 (CC == ISD::SETEQ || CC == ISD::SETOEQ || 3964 CC == ISD::SETNE || CC == ISD::SETUNE)) { 3965 if (SDValue Result = OptimizeVFPBrcond(Op, DAG)) 3966 return Result; 3967 } 3968 3969 ARMCC::CondCodes CondCode, CondCode2; 3970 FPCCToARMCC(CC, CondCode, CondCode2); 3971 3972 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32); 3973 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl); 3974 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 3975 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue); 3976 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp }; 3977 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 3978 if (CondCode2 != ARMCC::AL) { 3979 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32); 3980 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) }; 3981 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops); 3982 } 3983 return Res; 3984 } 3985 3986 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const { 3987 SDValue Chain = Op.getOperand(0); 3988 SDValue Table = Op.getOperand(1); 3989 SDValue Index = Op.getOperand(2); 3990 SDLoc dl(Op); 3991 3992 EVT PTy = getPointerTy(DAG.getDataLayout()); 3993 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table); 3994 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy); 3995 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI); 3996 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy)); 3997 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); 3998 if (Subtarget->isThumb2()) { 3999 // Thumb2 uses a two-level jump. That is, it jumps into the jump table 4000 // which does another jump to the destination. This also makes it easier 4001 // to translate it to TBB / TBH later. 4002 // FIXME: This might not work if the function is extremely large. 4003 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain, 4004 Addr, Op.getOperand(2), JTI); 4005 } 4006 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 4007 Addr = 4008 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, 4009 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), 4010 false, false, false, 0); 4011 Chain = Addr.getValue(1); 4012 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table); 4013 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 4014 } else { 4015 Addr = 4016 DAG.getLoad(PTy, dl, Chain, Addr, 4017 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), 4018 false, false, false, 0); 4019 Chain = Addr.getValue(1); 4020 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI); 4021 } 4022 } 4023 4024 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) { 4025 EVT VT = Op.getValueType(); 4026 SDLoc dl(Op); 4027 4028 if (Op.getValueType().getVectorElementType() == MVT::i32) { 4029 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32) 4030 return Op; 4031 return DAG.UnrollVectorOp(Op.getNode()); 4032 } 4033 4034 assert(Op.getOperand(0).getValueType() == MVT::v4f32 && 4035 "Invalid type for custom lowering!"); 4036 if (VT != MVT::v4i16) 4037 return DAG.UnrollVectorOp(Op.getNode()); 4038 4039 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0)); 4040 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op); 4041 } 4042 4043 SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const { 4044 EVT VT = Op.getValueType(); 4045 if (VT.isVector()) 4046 return LowerVectorFP_TO_INT(Op, DAG); 4047 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) { 4048 RTLIB::Libcall LC; 4049 if (Op.getOpcode() == ISD::FP_TO_SINT) 4050 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), 4051 Op.getValueType()); 4052 else 4053 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), 4054 Op.getValueType()); 4055 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0), 4056 /*isSigned*/ false, SDLoc(Op)).first; 4057 } 4058 4059 return Op; 4060 } 4061 4062 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 4063 EVT VT = Op.getValueType(); 4064 SDLoc dl(Op); 4065 4066 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) { 4067 if (VT.getVectorElementType() == MVT::f32) 4068 return Op; 4069 return DAG.UnrollVectorOp(Op.getNode()); 4070 } 4071 4072 assert(Op.getOperand(0).getValueType() == MVT::v4i16 && 4073 "Invalid type for custom lowering!"); 4074 if (VT != MVT::v4f32) 4075 return DAG.UnrollVectorOp(Op.getNode()); 4076 4077 unsigned CastOpc; 4078 unsigned Opc; 4079 switch (Op.getOpcode()) { 4080 default: llvm_unreachable("Invalid opcode!"); 4081 case ISD::SINT_TO_FP: 4082 CastOpc = ISD::SIGN_EXTEND; 4083 Opc = ISD::SINT_TO_FP; 4084 break; 4085 case ISD::UINT_TO_FP: 4086 CastOpc = ISD::ZERO_EXTEND; 4087 Opc = ISD::UINT_TO_FP; 4088 break; 4089 } 4090 4091 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0)); 4092 return DAG.getNode(Opc, dl, VT, Op); 4093 } 4094 4095 SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const { 4096 EVT VT = Op.getValueType(); 4097 if (VT.isVector()) 4098 return LowerVectorINT_TO_FP(Op, DAG); 4099 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) { 4100 RTLIB::Libcall LC; 4101 if (Op.getOpcode() == ISD::SINT_TO_FP) 4102 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), 4103 Op.getValueType()); 4104 else 4105 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), 4106 Op.getValueType()); 4107 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0), 4108 /*isSigned*/ false, SDLoc(Op)).first; 4109 } 4110 4111 return Op; 4112 } 4113 4114 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { 4115 // Implement fcopysign with a fabs and a conditional fneg. 4116 SDValue Tmp0 = Op.getOperand(0); 4117 SDValue Tmp1 = Op.getOperand(1); 4118 SDLoc dl(Op); 4119 EVT VT = Op.getValueType(); 4120 EVT SrcVT = Tmp1.getValueType(); 4121 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST || 4122 Tmp0.getOpcode() == ARMISD::VMOVDRR; 4123 bool UseNEON = !InGPR && Subtarget->hasNEON(); 4124 4125 if (UseNEON) { 4126 // Use VBSL to copy the sign bit. 4127 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80); 4128 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32, 4129 DAG.getTargetConstant(EncodedVal, dl, MVT::i32)); 4130 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64; 4131 if (VT == MVT::f64) 4132 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4133 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask), 4134 DAG.getConstant(32, dl, MVT::i32)); 4135 else /*if (VT == MVT::f32)*/ 4136 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0); 4137 if (SrcVT == MVT::f32) { 4138 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1); 4139 if (VT == MVT::f64) 4140 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT, 4141 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1), 4142 DAG.getConstant(32, dl, MVT::i32)); 4143 } else if (VT == MVT::f32) 4144 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64, 4145 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1), 4146 DAG.getConstant(32, dl, MVT::i32)); 4147 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0); 4148 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1); 4149 4150 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff), 4151 dl, MVT::i32); 4152 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes); 4153 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask, 4154 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes)); 4155 4156 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT, 4157 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask), 4158 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot)); 4159 if (VT == MVT::f32) { 4160 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res); 4161 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, 4162 DAG.getConstant(0, dl, MVT::i32)); 4163 } else { 4164 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res); 4165 } 4166 4167 return Res; 4168 } 4169 4170 // Bitcast operand 1 to i32. 4171 if (SrcVT == MVT::f64) 4172 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 4173 Tmp1).getValue(1); 4174 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1); 4175 4176 // Or in the signbit with integer operations. 4177 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32); 4178 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32); 4179 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1); 4180 if (VT == MVT::f32) { 4181 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32, 4182 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2); 4183 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 4184 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1)); 4185 } 4186 4187 // f64: Or the high part with signbit and then combine two parts. 4188 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32), 4189 Tmp0); 4190 SDValue Lo = Tmp0.getValue(0); 4191 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2); 4192 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1); 4193 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi); 4194 } 4195 4196 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{ 4197 MachineFunction &MF = DAG.getMachineFunction(); 4198 MachineFrameInfo *MFI = MF.getFrameInfo(); 4199 MFI->setReturnAddressIsTaken(true); 4200 4201 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 4202 return SDValue(); 4203 4204 EVT VT = Op.getValueType(); 4205 SDLoc dl(Op); 4206 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4207 if (Depth) { 4208 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 4209 SDValue Offset = DAG.getConstant(4, dl, MVT::i32); 4210 return DAG.getLoad(VT, dl, DAG.getEntryNode(), 4211 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset), 4212 MachinePointerInfo(), false, false, false, 0); 4213 } 4214 4215 // Return LR, which contains the return address. Mark it an implicit live-in. 4216 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32)); 4217 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); 4218 } 4219 4220 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { 4221 const ARMBaseRegisterInfo &ARI = 4222 *static_cast<const ARMBaseRegisterInfo*>(RegInfo); 4223 MachineFunction &MF = DAG.getMachineFunction(); 4224 MachineFrameInfo *MFI = MF.getFrameInfo(); 4225 MFI->setFrameAddressIsTaken(true); 4226 4227 EVT VT = Op.getValueType(); 4228 SDLoc dl(Op); // FIXME probably not meaningful 4229 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4230 unsigned FrameReg = ARI.getFrameRegister(MF); 4231 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 4232 while (Depth--) 4233 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, 4234 MachinePointerInfo(), 4235 false, false, false, 0); 4236 return FrameAddr; 4237 } 4238 4239 // FIXME? Maybe this could be a TableGen attribute on some registers and 4240 // this table could be generated automatically from RegInfo. 4241 unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT, 4242 SelectionDAG &DAG) const { 4243 unsigned Reg = StringSwitch<unsigned>(RegName) 4244 .Case("sp", ARM::SP) 4245 .Default(0); 4246 if (Reg) 4247 return Reg; 4248 report_fatal_error(Twine("Invalid register name \"" 4249 + StringRef(RegName) + "\".")); 4250 } 4251 4252 // Result is 64 bit value so split into two 32 bit values and return as a 4253 // pair of values. 4254 static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results, 4255 SelectionDAG &DAG) { 4256 SDLoc DL(N); 4257 4258 // This function is only supposed to be called for i64 type destination. 4259 assert(N->getValueType(0) == MVT::i64 4260 && "ExpandREAD_REGISTER called for non-i64 type result."); 4261 4262 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL, 4263 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other), 4264 N->getOperand(0), 4265 N->getOperand(1)); 4266 4267 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0), 4268 Read.getValue(1))); 4269 Results.push_back(Read.getOperand(0)); 4270 } 4271 4272 /// \p BC is a bitcast that is about to be turned into a VMOVDRR. 4273 /// When \p DstVT, the destination type of \p BC, is on the vector 4274 /// register bank and the source of bitcast, \p Op, operates on the same bank, 4275 /// it might be possible to combine them, such that everything stays on the 4276 /// vector register bank. 4277 /// \p return The node that would replace \p BT, if the combine 4278 /// is possible. 4279 static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC, 4280 SelectionDAG &DAG) { 4281 SDValue Op = BC->getOperand(0); 4282 EVT DstVT = BC->getValueType(0); 4283 4284 // The only vector instruction that can produce a scalar (remember, 4285 // since the bitcast was about to be turned into VMOVDRR, the source 4286 // type is i64) from a vector is EXTRACT_VECTOR_ELT. 4287 // Moreover, we can do this combine only if there is one use. 4288 // Finally, if the destination type is not a vector, there is not 4289 // much point on forcing everything on the vector bank. 4290 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 4291 !Op.hasOneUse()) 4292 return SDValue(); 4293 4294 // If the index is not constant, we will introduce an additional 4295 // multiply that will stick. 4296 // Give up in that case. 4297 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1)); 4298 if (!Index) 4299 return SDValue(); 4300 unsigned DstNumElt = DstVT.getVectorNumElements(); 4301 4302 // Compute the new index. 4303 const APInt &APIntIndex = Index->getAPIntValue(); 4304 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt); 4305 NewIndex *= APIntIndex; 4306 // Check if the new constant index fits into i32. 4307 if (NewIndex.getBitWidth() > 32) 4308 return SDValue(); 4309 4310 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) -> 4311 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M) 4312 SDLoc dl(Op); 4313 SDValue ExtractSrc = Op.getOperand(0); 4314 EVT VecVT = EVT::getVectorVT( 4315 *DAG.getContext(), DstVT.getScalarType(), 4316 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt); 4317 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc); 4318 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast, 4319 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32)); 4320 } 4321 4322 /// ExpandBITCAST - If the target supports VFP, this function is called to 4323 /// expand a bit convert where either the source or destination type is i64 to 4324 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64 4325 /// operand type is illegal (e.g., v2f32 for a target that doesn't support 4326 /// vectors), since the legalizer won't know what to do with that. 4327 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) { 4328 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4329 SDLoc dl(N); 4330 SDValue Op = N->getOperand(0); 4331 4332 // This function is only supposed to be called for i64 types, either as the 4333 // source or destination of the bit convert. 4334 EVT SrcVT = Op.getValueType(); 4335 EVT DstVT = N->getValueType(0); 4336 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) && 4337 "ExpandBITCAST called for non-i64 type"); 4338 4339 // Turn i64->f64 into VMOVDRR. 4340 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) { 4341 // Do not force values to GPRs (this is what VMOVDRR does for the inputs) 4342 // if we can combine the bitcast with its source. 4343 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG)) 4344 return Val; 4345 4346 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 4347 DAG.getConstant(0, dl, MVT::i32)); 4348 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op, 4349 DAG.getConstant(1, dl, MVT::i32)); 4350 return DAG.getNode(ISD::BITCAST, dl, DstVT, 4351 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi)); 4352 } 4353 4354 // Turn f64->i64 into VMOVRRD. 4355 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) { 4356 SDValue Cvt; 4357 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() && 4358 SrcVT.getVectorNumElements() > 1) 4359 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 4360 DAG.getVTList(MVT::i32, MVT::i32), 4361 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op)); 4362 else 4363 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl, 4364 DAG.getVTList(MVT::i32, MVT::i32), Op); 4365 // Merge the pieces into a single i64 value. 4366 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1)); 4367 } 4368 4369 return SDValue(); 4370 } 4371 4372 /// getZeroVector - Returns a vector of specified type with all zero elements. 4373 /// Zero vectors are used to represent vector negation and in those cases 4374 /// will be implemented with the NEON VNEG instruction. However, VNEG does 4375 /// not support i64 elements, so sometimes the zero vectors will need to be 4376 /// explicitly constructed. Regardless, use a canonical VMOV to create the 4377 /// zero vector. 4378 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) { 4379 assert(VT.isVector() && "Expected a vector type"); 4380 // The canonical modified immediate encoding of a zero vector is....0! 4381 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32); 4382 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 4383 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal); 4384 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 4385 } 4386 4387 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two 4388 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 4389 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, 4390 SelectionDAG &DAG) const { 4391 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4392 EVT VT = Op.getValueType(); 4393 unsigned VTBits = VT.getSizeInBits(); 4394 SDLoc dl(Op); 4395 SDValue ShOpLo = Op.getOperand(0); 4396 SDValue ShOpHi = Op.getOperand(1); 4397 SDValue ShAmt = Op.getOperand(2); 4398 SDValue ARMcc; 4399 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL; 4400 4401 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS); 4402 4403 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 4404 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 4405 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt); 4406 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 4407 DAG.getConstant(VTBits, dl, MVT::i32)); 4408 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt); 4409 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 4410 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt); 4411 4412 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4413 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 4414 ISD::SETGE, ARMcc, DAG, dl); 4415 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt); 4416 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, 4417 CCR, Cmp); 4418 4419 SDValue Ops[2] = { Lo, Hi }; 4420 return DAG.getMergeValues(Ops, dl); 4421 } 4422 4423 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two 4424 /// i32 values and take a 2 x i32 value to shift plus a shift amount. 4425 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, 4426 SelectionDAG &DAG) const { 4427 assert(Op.getNumOperands() == 3 && "Not a double-shift!"); 4428 EVT VT = Op.getValueType(); 4429 unsigned VTBits = VT.getSizeInBits(); 4430 SDLoc dl(Op); 4431 SDValue ShOpLo = Op.getOperand(0); 4432 SDValue ShOpHi = Op.getOperand(1); 4433 SDValue ShAmt = Op.getOperand(2); 4434 SDValue ARMcc; 4435 4436 assert(Op.getOpcode() == ISD::SHL_PARTS); 4437 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, 4438 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt); 4439 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt); 4440 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt, 4441 DAG.getConstant(VTBits, dl, MVT::i32)); 4442 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt); 4443 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt); 4444 4445 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); 4446 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4447 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32), 4448 ISD::SETGE, ARMcc, DAG, dl); 4449 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); 4450 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc, 4451 CCR, Cmp); 4452 4453 SDValue Ops[2] = { Lo, Hi }; 4454 return DAG.getMergeValues(Ops, dl); 4455 } 4456 4457 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op, 4458 SelectionDAG &DAG) const { 4459 // The rounding mode is in bits 23:22 of the FPSCR. 4460 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0 4461 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3) 4462 // so that the shift + and get folded into a bitfield extract. 4463 SDLoc dl(Op); 4464 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, 4465 DAG.getConstant(Intrinsic::arm_get_fpscr, dl, 4466 MVT::i32)); 4467 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR, 4468 DAG.getConstant(1U << 22, dl, MVT::i32)); 4469 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds, 4470 DAG.getConstant(22, dl, MVT::i32)); 4471 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE, 4472 DAG.getConstant(3, dl, MVT::i32)); 4473 } 4474 4475 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG, 4476 const ARMSubtarget *ST) { 4477 SDLoc dl(N); 4478 EVT VT = N->getValueType(0); 4479 if (VT.isVector()) { 4480 assert(ST->hasNEON()); 4481 4482 // Compute the least significant set bit: LSB = X & -X 4483 SDValue X = N->getOperand(0); 4484 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X); 4485 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX); 4486 4487 EVT ElemTy = VT.getVectorElementType(); 4488 4489 if (ElemTy == MVT::i8) { 4490 // Compute with: cttz(x) = ctpop(lsb - 1) 4491 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 4492 DAG.getTargetConstant(1, dl, ElemTy)); 4493 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One); 4494 return DAG.getNode(ISD::CTPOP, dl, VT, Bits); 4495 } 4496 4497 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) && 4498 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) { 4499 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0 4500 unsigned NumBits = ElemTy.getSizeInBits(); 4501 SDValue WidthMinus1 = 4502 DAG.getNode(ARMISD::VMOVIMM, dl, VT, 4503 DAG.getTargetConstant(NumBits - 1, dl, ElemTy)); 4504 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB); 4505 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ); 4506 } 4507 4508 // Compute with: cttz(x) = ctpop(lsb - 1) 4509 4510 // Since we can only compute the number of bits in a byte with vcnt.8, we 4511 // have to gather the result with pairwise addition (vpaddl) for i16, i32, 4512 // and i64. 4513 4514 // Compute LSB - 1. 4515 SDValue Bits; 4516 if (ElemTy == MVT::i64) { 4517 // Load constant 0xffff'ffff'ffff'ffff to register. 4518 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 4519 DAG.getTargetConstant(0x1eff, dl, MVT::i32)); 4520 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF); 4521 } else { 4522 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT, 4523 DAG.getTargetConstant(1, dl, ElemTy)); 4524 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One); 4525 } 4526 4527 // Count #bits with vcnt.8. 4528 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 4529 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits); 4530 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8); 4531 4532 // Gather the #bits with vpaddl (pairwise add.) 4533 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16; 4534 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit, 4535 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32), 4536 Cnt8); 4537 if (ElemTy == MVT::i16) 4538 return Cnt16; 4539 4540 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32; 4541 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit, 4542 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32), 4543 Cnt16); 4544 if (ElemTy == MVT::i32) 4545 return Cnt32; 4546 4547 assert(ElemTy == MVT::i64); 4548 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4549 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32), 4550 Cnt32); 4551 return Cnt64; 4552 } 4553 4554 if (!ST->hasV6T2Ops()) 4555 return SDValue(); 4556 4557 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0)); 4558 return DAG.getNode(ISD::CTLZ, dl, VT, rbit); 4559 } 4560 4561 /// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count 4562 /// for each 16-bit element from operand, repeated. The basic idea is to 4563 /// leverage vcnt to get the 8-bit counts, gather and add the results. 4564 /// 4565 /// Trace for v4i16: 4566 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 4567 /// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element) 4568 /// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi) 4569 /// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6] 4570 /// [b0 b1 b2 b3 b4 b5 b6 b7] 4571 /// +[b1 b0 b3 b2 b5 b4 b7 b6] 4572 /// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0, 4573 /// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits) 4574 static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) { 4575 EVT VT = N->getValueType(0); 4576 SDLoc DL(N); 4577 4578 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8; 4579 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0)); 4580 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0); 4581 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1); 4582 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2); 4583 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3); 4584 } 4585 4586 /// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the 4587 /// bit-count for each 16-bit element from the operand. We need slightly 4588 /// different sequencing for v4i16 and v8i16 to stay within NEON's available 4589 /// 64/128-bit registers. 4590 /// 4591 /// Trace for v4i16: 4592 /// input = [v0 v1 v2 v3 ] (vi 16-bit element) 4593 /// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi) 4594 /// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ] 4595 /// v4i16:Extracted = [k0 k1 k2 k3 ] 4596 static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) { 4597 EVT VT = N->getValueType(0); 4598 SDLoc DL(N); 4599 4600 SDValue BitCounts = getCTPOP16BitCounts(N, DAG); 4601 if (VT.is64BitVector()) { 4602 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts); 4603 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended, 4604 DAG.getIntPtrConstant(0, DL)); 4605 } else { 4606 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8, 4607 BitCounts, DAG.getIntPtrConstant(0, DL)); 4608 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted); 4609 } 4610 } 4611 4612 /// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the 4613 /// bit-count for each 32-bit element from the operand. The idea here is 4614 /// to split the vector into 16-bit elements, leverage the 16-bit count 4615 /// routine, and then combine the results. 4616 /// 4617 /// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged): 4618 /// input = [v0 v1 ] (vi: 32-bit elements) 4619 /// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1]) 4620 /// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi) 4621 /// vrev: N0 = [k1 k0 k3 k2 ] 4622 /// [k0 k1 k2 k3 ] 4623 /// N1 =+[k1 k0 k3 k2 ] 4624 /// [k0 k2 k1 k3 ] 4625 /// N2 =+[k1 k3 k0 k2 ] 4626 /// [k0 k2 k1 k3 ] 4627 /// Extended =+[k1 k3 k0 k2 ] 4628 /// [k0 k2 ] 4629 /// Extracted=+[k1 k3 ] 4630 /// 4631 static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) { 4632 EVT VT = N->getValueType(0); 4633 SDLoc DL(N); 4634 4635 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16; 4636 4637 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0)); 4638 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG); 4639 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16); 4640 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0); 4641 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1); 4642 4643 if (VT.is64BitVector()) { 4644 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2); 4645 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended, 4646 DAG.getIntPtrConstant(0, DL)); 4647 } else { 4648 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2, 4649 DAG.getIntPtrConstant(0, DL)); 4650 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted); 4651 } 4652 } 4653 4654 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG, 4655 const ARMSubtarget *ST) { 4656 EVT VT = N->getValueType(0); 4657 4658 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON."); 4659 assert((VT == MVT::v2i32 || VT == MVT::v4i32 || 4660 VT == MVT::v4i16 || VT == MVT::v8i16) && 4661 "Unexpected type for custom ctpop lowering"); 4662 4663 if (VT.getVectorElementType() == MVT::i32) 4664 return lowerCTPOP32BitElements(N, DAG); 4665 else 4666 return lowerCTPOP16BitElements(N, DAG); 4667 } 4668 4669 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG, 4670 const ARMSubtarget *ST) { 4671 EVT VT = N->getValueType(0); 4672 SDLoc dl(N); 4673 4674 if (!VT.isVector()) 4675 return SDValue(); 4676 4677 // Lower vector shifts on NEON to use VSHL. 4678 assert(ST->hasNEON() && "unexpected vector shift"); 4679 4680 // Left shifts translate directly to the vshiftu intrinsic. 4681 if (N->getOpcode() == ISD::SHL) 4682 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4683 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl, 4684 MVT::i32), 4685 N->getOperand(0), N->getOperand(1)); 4686 4687 assert((N->getOpcode() == ISD::SRA || 4688 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode"); 4689 4690 // NEON uses the same intrinsics for both left and right shifts. For 4691 // right shifts, the shift amounts are negative, so negate the vector of 4692 // shift amounts. 4693 EVT ShiftVT = N->getOperand(1).getValueType(); 4694 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT, 4695 getZeroVector(ShiftVT, DAG, dl), 4696 N->getOperand(1)); 4697 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ? 4698 Intrinsic::arm_neon_vshifts : 4699 Intrinsic::arm_neon_vshiftu); 4700 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, 4701 DAG.getConstant(vshiftInt, dl, MVT::i32), 4702 N->getOperand(0), NegatedCount); 4703 } 4704 4705 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG, 4706 const ARMSubtarget *ST) { 4707 EVT VT = N->getValueType(0); 4708 SDLoc dl(N); 4709 4710 // We can get here for a node like i32 = ISD::SHL i32, i64 4711 if (VT != MVT::i64) 4712 return SDValue(); 4713 4714 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && 4715 "Unknown shift to lower!"); 4716 4717 // We only lower SRA, SRL of 1 here, all others use generic lowering. 4718 if (!isOneConstant(N->getOperand(1))) 4719 return SDValue(); 4720 4721 // If we are in thumb mode, we don't have RRX. 4722 if (ST->isThumb1Only()) return SDValue(); 4723 4724 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr. 4725 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 4726 DAG.getConstant(0, dl, MVT::i32)); 4727 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0), 4728 DAG.getConstant(1, dl, MVT::i32)); 4729 4730 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and 4731 // captures the result into a carry flag. 4732 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG; 4733 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi); 4734 4735 // The low part is an ARMISD::RRX operand, which shifts the carry in. 4736 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1)); 4737 4738 // Merge the pieces into a single i64 value. 4739 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); 4740 } 4741 4742 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) { 4743 SDValue TmpOp0, TmpOp1; 4744 bool Invert = false; 4745 bool Swap = false; 4746 unsigned Opc = 0; 4747 4748 SDValue Op0 = Op.getOperand(0); 4749 SDValue Op1 = Op.getOperand(1); 4750 SDValue CC = Op.getOperand(2); 4751 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger(); 4752 EVT VT = Op.getValueType(); 4753 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); 4754 SDLoc dl(Op); 4755 4756 if (CmpVT.getVectorElementType() == MVT::i64) 4757 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom, 4758 // but it's possible that our operands are 64-bit but our result is 32-bit. 4759 // Bail in this case. 4760 return SDValue(); 4761 4762 if (Op1.getValueType().isFloatingPoint()) { 4763 switch (SetCCOpcode) { 4764 default: llvm_unreachable("Illegal FP comparison"); 4765 case ISD::SETUNE: 4766 case ISD::SETNE: Invert = true; // Fallthrough 4767 case ISD::SETOEQ: 4768 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 4769 case ISD::SETOLT: 4770 case ISD::SETLT: Swap = true; // Fallthrough 4771 case ISD::SETOGT: 4772 case ISD::SETGT: Opc = ARMISD::VCGT; break; 4773 case ISD::SETOLE: 4774 case ISD::SETLE: Swap = true; // Fallthrough 4775 case ISD::SETOGE: 4776 case ISD::SETGE: Opc = ARMISD::VCGE; break; 4777 case ISD::SETUGE: Swap = true; // Fallthrough 4778 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break; 4779 case ISD::SETUGT: Swap = true; // Fallthrough 4780 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break; 4781 case ISD::SETUEQ: Invert = true; // Fallthrough 4782 case ISD::SETONE: 4783 // Expand this to (OLT | OGT). 4784 TmpOp0 = Op0; 4785 TmpOp1 = Op1; 4786 Opc = ISD::OR; 4787 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0); 4788 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1); 4789 break; 4790 case ISD::SETUO: Invert = true; // Fallthrough 4791 case ISD::SETO: 4792 // Expand this to (OLT | OGE). 4793 TmpOp0 = Op0; 4794 TmpOp1 = Op1; 4795 Opc = ISD::OR; 4796 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0); 4797 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1); 4798 break; 4799 } 4800 } else { 4801 // Integer comparisons. 4802 switch (SetCCOpcode) { 4803 default: llvm_unreachable("Illegal integer comparison"); 4804 case ISD::SETNE: Invert = true; 4805 case ISD::SETEQ: Opc = ARMISD::VCEQ; break; 4806 case ISD::SETLT: Swap = true; 4807 case ISD::SETGT: Opc = ARMISD::VCGT; break; 4808 case ISD::SETLE: Swap = true; 4809 case ISD::SETGE: Opc = ARMISD::VCGE; break; 4810 case ISD::SETULT: Swap = true; 4811 case ISD::SETUGT: Opc = ARMISD::VCGTU; break; 4812 case ISD::SETULE: Swap = true; 4813 case ISD::SETUGE: Opc = ARMISD::VCGEU; break; 4814 } 4815 4816 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero). 4817 if (Opc == ARMISD::VCEQ) { 4818 4819 SDValue AndOp; 4820 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 4821 AndOp = Op0; 4822 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) 4823 AndOp = Op1; 4824 4825 // Ignore bitconvert. 4826 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST) 4827 AndOp = AndOp.getOperand(0); 4828 4829 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) { 4830 Opc = ARMISD::VTST; 4831 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0)); 4832 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1)); 4833 Invert = !Invert; 4834 } 4835 } 4836 } 4837 4838 if (Swap) 4839 std::swap(Op0, Op1); 4840 4841 // If one of the operands is a constant vector zero, attempt to fold the 4842 // comparison to a specialized compare-against-zero form. 4843 SDValue SingleOp; 4844 if (ISD::isBuildVectorAllZeros(Op1.getNode())) 4845 SingleOp = Op0; 4846 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) { 4847 if (Opc == ARMISD::VCGE) 4848 Opc = ARMISD::VCLEZ; 4849 else if (Opc == ARMISD::VCGT) 4850 Opc = ARMISD::VCLTZ; 4851 SingleOp = Op1; 4852 } 4853 4854 SDValue Result; 4855 if (SingleOp.getNode()) { 4856 switch (Opc) { 4857 case ARMISD::VCEQ: 4858 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break; 4859 case ARMISD::VCGE: 4860 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break; 4861 case ARMISD::VCLEZ: 4862 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break; 4863 case ARMISD::VCGT: 4864 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break; 4865 case ARMISD::VCLTZ: 4866 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break; 4867 default: 4868 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1); 4869 } 4870 } else { 4871 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1); 4872 } 4873 4874 Result = DAG.getSExtOrTrunc(Result, dl, VT); 4875 4876 if (Invert) 4877 Result = DAG.getNOT(dl, Result, VT); 4878 4879 return Result; 4880 } 4881 4882 static SDValue LowerSETCCE(SDValue Op, SelectionDAG &DAG) { 4883 SDValue LHS = Op.getOperand(0); 4884 SDValue RHS = Op.getOperand(1); 4885 SDValue Carry = Op.getOperand(2); 4886 SDValue Cond = Op.getOperand(3); 4887 SDLoc DL(Op); 4888 4889 assert(LHS.getSimpleValueType().isInteger() && "SETCCE is integer only."); 4890 4891 assert(Carry.getOpcode() != ISD::CARRY_FALSE); 4892 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32); 4893 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry); 4894 4895 SDValue FVal = DAG.getConstant(0, DL, MVT::i32); 4896 SDValue TVal = DAG.getConstant(1, DL, MVT::i32); 4897 SDValue ARMcc = DAG.getConstant( 4898 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32); 4899 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32); 4900 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR, 4901 Cmp.getValue(1), SDValue()); 4902 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc, 4903 CCR, Chain.getValue(1)); 4904 } 4905 4906 /// isNEONModifiedImm - Check if the specified splat value corresponds to a 4907 /// valid vector constant for a NEON instruction with a "modified immediate" 4908 /// operand (e.g., VMOV). If so, return the encoded value. 4909 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef, 4910 unsigned SplatBitSize, SelectionDAG &DAG, 4911 SDLoc dl, EVT &VT, bool is128Bits, 4912 NEONModImmType type) { 4913 unsigned OpCmode, Imm; 4914 4915 // SplatBitSize is set to the smallest size that splats the vector, so a 4916 // zero vector will always have SplatBitSize == 8. However, NEON modified 4917 // immediate instructions others than VMOV do not support the 8-bit encoding 4918 // of a zero vector, and the default encoding of zero is supposed to be the 4919 // 32-bit version. 4920 if (SplatBits == 0) 4921 SplatBitSize = 32; 4922 4923 switch (SplatBitSize) { 4924 case 8: 4925 if (type != VMOVModImm) 4926 return SDValue(); 4927 // Any 1-byte value is OK. Op=0, Cmode=1110. 4928 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big"); 4929 OpCmode = 0xe; 4930 Imm = SplatBits; 4931 VT = is128Bits ? MVT::v16i8 : MVT::v8i8; 4932 break; 4933 4934 case 16: 4935 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero. 4936 VT = is128Bits ? MVT::v8i16 : MVT::v4i16; 4937 if ((SplatBits & ~0xff) == 0) { 4938 // Value = 0x00nn: Op=x, Cmode=100x. 4939 OpCmode = 0x8; 4940 Imm = SplatBits; 4941 break; 4942 } 4943 if ((SplatBits & ~0xff00) == 0) { 4944 // Value = 0xnn00: Op=x, Cmode=101x. 4945 OpCmode = 0xa; 4946 Imm = SplatBits >> 8; 4947 break; 4948 } 4949 return SDValue(); 4950 4951 case 32: 4952 // NEON's 32-bit VMOV supports splat values where: 4953 // * only one byte is nonzero, or 4954 // * the least significant byte is 0xff and the second byte is nonzero, or 4955 // * the least significant 2 bytes are 0xff and the third is nonzero. 4956 VT = is128Bits ? MVT::v4i32 : MVT::v2i32; 4957 if ((SplatBits & ~0xff) == 0) { 4958 // Value = 0x000000nn: Op=x, Cmode=000x. 4959 OpCmode = 0; 4960 Imm = SplatBits; 4961 break; 4962 } 4963 if ((SplatBits & ~0xff00) == 0) { 4964 // Value = 0x0000nn00: Op=x, Cmode=001x. 4965 OpCmode = 0x2; 4966 Imm = SplatBits >> 8; 4967 break; 4968 } 4969 if ((SplatBits & ~0xff0000) == 0) { 4970 // Value = 0x00nn0000: Op=x, Cmode=010x. 4971 OpCmode = 0x4; 4972 Imm = SplatBits >> 16; 4973 break; 4974 } 4975 if ((SplatBits & ~0xff000000) == 0) { 4976 // Value = 0xnn000000: Op=x, Cmode=011x. 4977 OpCmode = 0x6; 4978 Imm = SplatBits >> 24; 4979 break; 4980 } 4981 4982 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC 4983 if (type == OtherModImm) return SDValue(); 4984 4985 if ((SplatBits & ~0xffff) == 0 && 4986 ((SplatBits | SplatUndef) & 0xff) == 0xff) { 4987 // Value = 0x0000nnff: Op=x, Cmode=1100. 4988 OpCmode = 0xc; 4989 Imm = SplatBits >> 8; 4990 break; 4991 } 4992 4993 if ((SplatBits & ~0xffffff) == 0 && 4994 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) { 4995 // Value = 0x00nnffff: Op=x, Cmode=1101. 4996 OpCmode = 0xd; 4997 Imm = SplatBits >> 16; 4998 break; 4999 } 5000 5001 // Note: there are a few 32-bit splat values (specifically: 00ffff00, 5002 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not 5003 // VMOV.I32. A (very) minor optimization would be to replicate the value 5004 // and fall through here to test for a valid 64-bit splat. But, then the 5005 // caller would also need to check and handle the change in size. 5006 return SDValue(); 5007 5008 case 64: { 5009 if (type != VMOVModImm) 5010 return SDValue(); 5011 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff. 5012 uint64_t BitMask = 0xff; 5013 uint64_t Val = 0; 5014 unsigned ImmMask = 1; 5015 Imm = 0; 5016 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) { 5017 if (((SplatBits | SplatUndef) & BitMask) == BitMask) { 5018 Val |= BitMask; 5019 Imm |= ImmMask; 5020 } else if ((SplatBits & BitMask) != 0) { 5021 return SDValue(); 5022 } 5023 BitMask <<= 8; 5024 ImmMask <<= 1; 5025 } 5026 5027 if (DAG.getDataLayout().isBigEndian()) 5028 // swap higher and lower 32 bit word 5029 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4); 5030 5031 // Op=1, Cmode=1110. 5032 OpCmode = 0x1e; 5033 VT = is128Bits ? MVT::v2i64 : MVT::v1i64; 5034 break; 5035 } 5036 5037 default: 5038 llvm_unreachable("unexpected size for isNEONModifiedImm"); 5039 } 5040 5041 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm); 5042 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32); 5043 } 5044 5045 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG, 5046 const ARMSubtarget *ST) const { 5047 if (!ST->hasVFP3()) 5048 return SDValue(); 5049 5050 bool IsDouble = Op.getValueType() == MVT::f64; 5051 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op); 5052 5053 // Use the default (constant pool) lowering for double constants when we have 5054 // an SP-only FPU 5055 if (IsDouble && Subtarget->isFPOnlySP()) 5056 return SDValue(); 5057 5058 // Try splatting with a VMOV.f32... 5059 APFloat FPVal = CFP->getValueAPF(); 5060 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal); 5061 5062 if (ImmVal != -1) { 5063 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) { 5064 // We have code in place to select a valid ConstantFP already, no need to 5065 // do any mangling. 5066 return Op; 5067 } 5068 5069 // It's a float and we are trying to use NEON operations where 5070 // possible. Lower it to a splat followed by an extract. 5071 SDLoc DL(Op); 5072 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32); 5073 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, 5074 NewVal); 5075 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant, 5076 DAG.getConstant(0, DL, MVT::i32)); 5077 } 5078 5079 // The rest of our options are NEON only, make sure that's allowed before 5080 // proceeding.. 5081 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP())) 5082 return SDValue(); 5083 5084 EVT VMovVT; 5085 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue(); 5086 5087 // It wouldn't really be worth bothering for doubles except for one very 5088 // important value, which does happen to match: 0.0. So make sure we don't do 5089 // anything stupid. 5090 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32)) 5091 return SDValue(); 5092 5093 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too). 5094 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), 5095 VMovVT, false, VMOVModImm); 5096 if (NewVal != SDValue()) { 5097 SDLoc DL(Op); 5098 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT, 5099 NewVal); 5100 if (IsDouble) 5101 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 5102 5103 // It's a float: cast and extract a vector element. 5104 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 5105 VecConstant); 5106 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 5107 DAG.getConstant(0, DL, MVT::i32)); 5108 } 5109 5110 // Finally, try a VMVN.i32 5111 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT, 5112 false, VMVNModImm); 5113 if (NewVal != SDValue()) { 5114 SDLoc DL(Op); 5115 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal); 5116 5117 if (IsDouble) 5118 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant); 5119 5120 // It's a float: cast and extract a vector element. 5121 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32, 5122 VecConstant); 5123 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant, 5124 DAG.getConstant(0, DL, MVT::i32)); 5125 } 5126 5127 return SDValue(); 5128 } 5129 5130 // check if an VEXT instruction can handle the shuffle mask when the 5131 // vector sources of the shuffle are the same. 5132 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) { 5133 unsigned NumElts = VT.getVectorNumElements(); 5134 5135 // Assume that the first shuffle index is not UNDEF. Fail if it is. 5136 if (M[0] < 0) 5137 return false; 5138 5139 Imm = M[0]; 5140 5141 // If this is a VEXT shuffle, the immediate value is the index of the first 5142 // element. The other shuffle indices must be the successive elements after 5143 // the first one. 5144 unsigned ExpectedElt = Imm; 5145 for (unsigned i = 1; i < NumElts; ++i) { 5146 // Increment the expected index. If it wraps around, just follow it 5147 // back to index zero and keep going. 5148 ++ExpectedElt; 5149 if (ExpectedElt == NumElts) 5150 ExpectedElt = 0; 5151 5152 if (M[i] < 0) continue; // ignore UNDEF indices 5153 if (ExpectedElt != static_cast<unsigned>(M[i])) 5154 return false; 5155 } 5156 5157 return true; 5158 } 5159 5160 5161 static bool isVEXTMask(ArrayRef<int> M, EVT VT, 5162 bool &ReverseVEXT, unsigned &Imm) { 5163 unsigned NumElts = VT.getVectorNumElements(); 5164 ReverseVEXT = false; 5165 5166 // Assume that the first shuffle index is not UNDEF. Fail if it is. 5167 if (M[0] < 0) 5168 return false; 5169 5170 Imm = M[0]; 5171 5172 // If this is a VEXT shuffle, the immediate value is the index of the first 5173 // element. The other shuffle indices must be the successive elements after 5174 // the first one. 5175 unsigned ExpectedElt = Imm; 5176 for (unsigned i = 1; i < NumElts; ++i) { 5177 // Increment the expected index. If it wraps around, it may still be 5178 // a VEXT but the source vectors must be swapped. 5179 ExpectedElt += 1; 5180 if (ExpectedElt == NumElts * 2) { 5181 ExpectedElt = 0; 5182 ReverseVEXT = true; 5183 } 5184 5185 if (M[i] < 0) continue; // ignore UNDEF indices 5186 if (ExpectedElt != static_cast<unsigned>(M[i])) 5187 return false; 5188 } 5189 5190 // Adjust the index value if the source operands will be swapped. 5191 if (ReverseVEXT) 5192 Imm -= NumElts; 5193 5194 return true; 5195 } 5196 5197 /// isVREVMask - Check if a vector shuffle corresponds to a VREV 5198 /// instruction with the specified blocksize. (The order of the elements 5199 /// within each block of the vector is reversed.) 5200 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) { 5201 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) && 5202 "Only possible block sizes for VREV are: 16, 32, 64"); 5203 5204 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5205 if (EltSz == 64) 5206 return false; 5207 5208 unsigned NumElts = VT.getVectorNumElements(); 5209 unsigned BlockElts = M[0] + 1; 5210 // If the first shuffle index is UNDEF, be optimistic. 5211 if (M[0] < 0) 5212 BlockElts = BlockSize / EltSz; 5213 5214 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz) 5215 return false; 5216 5217 for (unsigned i = 0; i < NumElts; ++i) { 5218 if (M[i] < 0) continue; // ignore UNDEF indices 5219 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts)) 5220 return false; 5221 } 5222 5223 return true; 5224 } 5225 5226 static bool isVTBLMask(ArrayRef<int> M, EVT VT) { 5227 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of 5228 // range, then 0 is placed into the resulting vector. So pretty much any mask 5229 // of 8 elements can work here. 5230 return VT == MVT::v8i8 && M.size() == 8; 5231 } 5232 5233 // Checks whether the shuffle mask represents a vector transpose (VTRN) by 5234 // checking that pairs of elements in the shuffle mask represent the same index 5235 // in each vector, incrementing the expected index by 2 at each step. 5236 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6] 5237 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g} 5238 // v2={e,f,g,h} 5239 // WhichResult gives the offset for each element in the mask based on which 5240 // of the two results it belongs to. 5241 // 5242 // The transpose can be represented either as: 5243 // result1 = shufflevector v1, v2, result1_shuffle_mask 5244 // result2 = shufflevector v1, v2, result2_shuffle_mask 5245 // where v1/v2 and the shuffle masks have the same number of elements 5246 // (here WhichResult (see below) indicates which result is being checked) 5247 // 5248 // or as: 5249 // results = shufflevector v1, v2, shuffle_mask 5250 // where both results are returned in one vector and the shuffle mask has twice 5251 // as many elements as v1/v2 (here WhichResult will always be 0 if true) here we 5252 // want to check the low half and high half of the shuffle mask as if it were 5253 // the other case 5254 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5255 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5256 if (EltSz == 64) 5257 return false; 5258 5259 unsigned NumElts = VT.getVectorNumElements(); 5260 if (M.size() != NumElts && M.size() != NumElts*2) 5261 return false; 5262 5263 // If the mask is twice as long as the input vector then we need to check the 5264 // upper and lower parts of the mask with a matching value for WhichResult 5265 // FIXME: A mask with only even values will be rejected in case the first 5266 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only 5267 // M[0] is used to determine WhichResult 5268 for (unsigned i = 0; i < M.size(); i += NumElts) { 5269 if (M.size() == NumElts * 2) 5270 WhichResult = i / NumElts; 5271 else 5272 WhichResult = M[i] == 0 ? 0 : 1; 5273 for (unsigned j = 0; j < NumElts; j += 2) { 5274 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) || 5275 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult)) 5276 return false; 5277 } 5278 } 5279 5280 if (M.size() == NumElts*2) 5281 WhichResult = 0; 5282 5283 return true; 5284 } 5285 5286 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of 5287 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 5288 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>. 5289 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 5290 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5291 if (EltSz == 64) 5292 return false; 5293 5294 unsigned NumElts = VT.getVectorNumElements(); 5295 if (M.size() != NumElts && M.size() != NumElts*2) 5296 return false; 5297 5298 for (unsigned i = 0; i < M.size(); i += NumElts) { 5299 if (M.size() == NumElts * 2) 5300 WhichResult = i / NumElts; 5301 else 5302 WhichResult = M[i] == 0 ? 0 : 1; 5303 for (unsigned j = 0; j < NumElts; j += 2) { 5304 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) || 5305 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult)) 5306 return false; 5307 } 5308 } 5309 5310 if (M.size() == NumElts*2) 5311 WhichResult = 0; 5312 5313 return true; 5314 } 5315 5316 // Checks whether the shuffle mask represents a vector unzip (VUZP) by checking 5317 // that the mask elements are either all even and in steps of size 2 or all odd 5318 // and in steps of size 2. 5319 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6] 5320 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g} 5321 // v2={e,f,g,h} 5322 // Requires similar checks to that of isVTRNMask with 5323 // respect the how results are returned. 5324 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5325 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5326 if (EltSz == 64) 5327 return false; 5328 5329 unsigned NumElts = VT.getVectorNumElements(); 5330 if (M.size() != NumElts && M.size() != NumElts*2) 5331 return false; 5332 5333 for (unsigned i = 0; i < M.size(); i += NumElts) { 5334 WhichResult = M[i] == 0 ? 0 : 1; 5335 for (unsigned j = 0; j < NumElts; ++j) { 5336 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult) 5337 return false; 5338 } 5339 } 5340 5341 if (M.size() == NumElts*2) 5342 WhichResult = 0; 5343 5344 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5345 if (VT.is64BitVector() && EltSz == 32) 5346 return false; 5347 5348 return true; 5349 } 5350 5351 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of 5352 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 5353 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>, 5354 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 5355 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5356 if (EltSz == 64) 5357 return false; 5358 5359 unsigned NumElts = VT.getVectorNumElements(); 5360 if (M.size() != NumElts && M.size() != NumElts*2) 5361 return false; 5362 5363 unsigned Half = NumElts / 2; 5364 for (unsigned i = 0; i < M.size(); i += NumElts) { 5365 WhichResult = M[i] == 0 ? 0 : 1; 5366 for (unsigned j = 0; j < NumElts; j += Half) { 5367 unsigned Idx = WhichResult; 5368 for (unsigned k = 0; k < Half; ++k) { 5369 int MIdx = M[i + j + k]; 5370 if (MIdx >= 0 && (unsigned) MIdx != Idx) 5371 return false; 5372 Idx += 2; 5373 } 5374 } 5375 } 5376 5377 if (M.size() == NumElts*2) 5378 WhichResult = 0; 5379 5380 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5381 if (VT.is64BitVector() && EltSz == 32) 5382 return false; 5383 5384 return true; 5385 } 5386 5387 // Checks whether the shuffle mask represents a vector zip (VZIP) by checking 5388 // that pairs of elements of the shufflemask represent the same index in each 5389 // vector incrementing sequentially through the vectors. 5390 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5] 5391 // v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f} 5392 // v2={e,f,g,h} 5393 // Requires similar checks to that of isVTRNMask with respect the how results 5394 // are returned. 5395 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) { 5396 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5397 if (EltSz == 64) 5398 return false; 5399 5400 unsigned NumElts = VT.getVectorNumElements(); 5401 if (M.size() != NumElts && M.size() != NumElts*2) 5402 return false; 5403 5404 for (unsigned i = 0; i < M.size(); i += NumElts) { 5405 WhichResult = M[i] == 0 ? 0 : 1; 5406 unsigned Idx = WhichResult * NumElts / 2; 5407 for (unsigned j = 0; j < NumElts; j += 2) { 5408 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) || 5409 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts)) 5410 return false; 5411 Idx += 1; 5412 } 5413 } 5414 5415 if (M.size() == NumElts*2) 5416 WhichResult = 0; 5417 5418 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5419 if (VT.is64BitVector() && EltSz == 32) 5420 return false; 5421 5422 return true; 5423 } 5424 5425 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of 5426 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef". 5427 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>. 5428 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){ 5429 unsigned EltSz = VT.getVectorElementType().getSizeInBits(); 5430 if (EltSz == 64) 5431 return false; 5432 5433 unsigned NumElts = VT.getVectorNumElements(); 5434 if (M.size() != NumElts && M.size() != NumElts*2) 5435 return false; 5436 5437 for (unsigned i = 0; i < M.size(); i += NumElts) { 5438 WhichResult = M[i] == 0 ? 0 : 1; 5439 unsigned Idx = WhichResult * NumElts / 2; 5440 for (unsigned j = 0; j < NumElts; j += 2) { 5441 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) || 5442 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx)) 5443 return false; 5444 Idx += 1; 5445 } 5446 } 5447 5448 if (M.size() == NumElts*2) 5449 WhichResult = 0; 5450 5451 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32. 5452 if (VT.is64BitVector() && EltSz == 32) 5453 return false; 5454 5455 return true; 5456 } 5457 5458 /// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN), 5459 /// and return the corresponding ARMISD opcode if it is, or 0 if it isn't. 5460 static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT, 5461 unsigned &WhichResult, 5462 bool &isV_UNDEF) { 5463 isV_UNDEF = false; 5464 if (isVTRNMask(ShuffleMask, VT, WhichResult)) 5465 return ARMISD::VTRN; 5466 if (isVUZPMask(ShuffleMask, VT, WhichResult)) 5467 return ARMISD::VUZP; 5468 if (isVZIPMask(ShuffleMask, VT, WhichResult)) 5469 return ARMISD::VZIP; 5470 5471 isV_UNDEF = true; 5472 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5473 return ARMISD::VTRN; 5474 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5475 return ARMISD::VUZP; 5476 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult)) 5477 return ARMISD::VZIP; 5478 5479 return 0; 5480 } 5481 5482 /// \return true if this is a reverse operation on an vector. 5483 static bool isReverseMask(ArrayRef<int> M, EVT VT) { 5484 unsigned NumElts = VT.getVectorNumElements(); 5485 // Make sure the mask has the right size. 5486 if (NumElts != M.size()) 5487 return false; 5488 5489 // Look for <15, ..., 3, -1, 1, 0>. 5490 for (unsigned i = 0; i != NumElts; ++i) 5491 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i)) 5492 return false; 5493 5494 return true; 5495 } 5496 5497 // If N is an integer constant that can be moved into a register in one 5498 // instruction, return an SDValue of such a constant (will become a MOV 5499 // instruction). Otherwise return null. 5500 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG, 5501 const ARMSubtarget *ST, SDLoc dl) { 5502 uint64_t Val; 5503 if (!isa<ConstantSDNode>(N)) 5504 return SDValue(); 5505 Val = cast<ConstantSDNode>(N)->getZExtValue(); 5506 5507 if (ST->isThumb1Only()) { 5508 if (Val <= 255 || ~Val <= 255) 5509 return DAG.getConstant(Val, dl, MVT::i32); 5510 } else { 5511 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1) 5512 return DAG.getConstant(Val, dl, MVT::i32); 5513 } 5514 return SDValue(); 5515 } 5516 5517 // If this is a case we can't handle, return null and let the default 5518 // expansion code take care of it. 5519 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG, 5520 const ARMSubtarget *ST) const { 5521 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode()); 5522 SDLoc dl(Op); 5523 EVT VT = Op.getValueType(); 5524 5525 APInt SplatBits, SplatUndef; 5526 unsigned SplatBitSize; 5527 bool HasAnyUndefs; 5528 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 5529 if (SplatBitSize <= 64) { 5530 // Check if an immediate VMOV works. 5531 EVT VmovVT; 5532 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 5533 SplatUndef.getZExtValue(), SplatBitSize, 5534 DAG, dl, VmovVT, VT.is128BitVector(), 5535 VMOVModImm); 5536 if (Val.getNode()) { 5537 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val); 5538 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5539 } 5540 5541 // Try an immediate VMVN. 5542 uint64_t NegatedImm = (~SplatBits).getZExtValue(); 5543 Val = isNEONModifiedImm(NegatedImm, 5544 SplatUndef.getZExtValue(), SplatBitSize, 5545 DAG, dl, VmovVT, VT.is128BitVector(), 5546 VMVNModImm); 5547 if (Val.getNode()) { 5548 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val); 5549 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov); 5550 } 5551 5552 // Use vmov.f32 to materialize other v2f32 and v4f32 splats. 5553 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) { 5554 int ImmVal = ARM_AM::getFP32Imm(SplatBits); 5555 if (ImmVal != -1) { 5556 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32); 5557 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val); 5558 } 5559 } 5560 } 5561 } 5562 5563 // Scan through the operands to see if only one value is used. 5564 // 5565 // As an optimisation, even if more than one value is used it may be more 5566 // profitable to splat with one value then change some lanes. 5567 // 5568 // Heuristically we decide to do this if the vector has a "dominant" value, 5569 // defined as splatted to more than half of the lanes. 5570 unsigned NumElts = VT.getVectorNumElements(); 5571 bool isOnlyLowElement = true; 5572 bool usesOnlyOneValue = true; 5573 bool hasDominantValue = false; 5574 bool isConstant = true; 5575 5576 // Map of the number of times a particular SDValue appears in the 5577 // element list. 5578 DenseMap<SDValue, unsigned> ValueCounts; 5579 SDValue Value; 5580 for (unsigned i = 0; i < NumElts; ++i) { 5581 SDValue V = Op.getOperand(i); 5582 if (V.isUndef()) 5583 continue; 5584 if (i > 0) 5585 isOnlyLowElement = false; 5586 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 5587 isConstant = false; 5588 5589 ValueCounts.insert(std::make_pair(V, 0)); 5590 unsigned &Count = ValueCounts[V]; 5591 5592 // Is this value dominant? (takes up more than half of the lanes) 5593 if (++Count > (NumElts / 2)) { 5594 hasDominantValue = true; 5595 Value = V; 5596 } 5597 } 5598 if (ValueCounts.size() != 1) 5599 usesOnlyOneValue = false; 5600 if (!Value.getNode() && ValueCounts.size() > 0) 5601 Value = ValueCounts.begin()->first; 5602 5603 if (ValueCounts.size() == 0) 5604 return DAG.getUNDEF(VT); 5605 5606 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR. 5607 // Keep going if we are hitting this case. 5608 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode())) 5609 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value); 5610 5611 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5612 5613 // Use VDUP for non-constant splats. For f32 constant splats, reduce to 5614 // i32 and try again. 5615 if (hasDominantValue && EltSize <= 32) { 5616 if (!isConstant) { 5617 SDValue N; 5618 5619 // If we are VDUPing a value that comes directly from a vector, that will 5620 // cause an unnecessary move to and from a GPR, where instead we could 5621 // just use VDUPLANE. We can only do this if the lane being extracted 5622 // is at a constant index, as the VDUP from lane instructions only have 5623 // constant-index forms. 5624 ConstantSDNode *constIndex; 5625 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT && 5626 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) { 5627 // We need to create a new undef vector to use for the VDUPLANE if the 5628 // size of the vector from which we get the value is different than the 5629 // size of the vector that we need to create. We will insert the element 5630 // such that the register coalescer will remove unnecessary copies. 5631 if (VT != Value->getOperand(0).getValueType()) { 5632 unsigned index = constIndex->getAPIntValue().getLimitedValue() % 5633 VT.getVectorNumElements(); 5634 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5635 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT), 5636 Value, DAG.getConstant(index, dl, MVT::i32)), 5637 DAG.getConstant(index, dl, MVT::i32)); 5638 } else 5639 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT, 5640 Value->getOperand(0), Value->getOperand(1)); 5641 } else 5642 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value); 5643 5644 if (!usesOnlyOneValue) { 5645 // The dominant value was splatted as 'N', but we now have to insert 5646 // all differing elements. 5647 for (unsigned I = 0; I < NumElts; ++I) { 5648 if (Op.getOperand(I) == Value) 5649 continue; 5650 SmallVector<SDValue, 3> Ops; 5651 Ops.push_back(N); 5652 Ops.push_back(Op.getOperand(I)); 5653 Ops.push_back(DAG.getConstant(I, dl, MVT::i32)); 5654 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops); 5655 } 5656 } 5657 return N; 5658 } 5659 if (VT.getVectorElementType().isFloatingPoint()) { 5660 SmallVector<SDValue, 8> Ops; 5661 for (unsigned i = 0; i < NumElts; ++i) 5662 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32, 5663 Op.getOperand(i))); 5664 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 5665 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops); 5666 Val = LowerBUILD_VECTOR(Val, DAG, ST); 5667 if (Val.getNode()) 5668 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5669 } 5670 if (usesOnlyOneValue) { 5671 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl); 5672 if (isConstant && Val.getNode()) 5673 return DAG.getNode(ARMISD::VDUP, dl, VT, Val); 5674 } 5675 } 5676 5677 // If all elements are constants and the case above didn't get hit, fall back 5678 // to the default expansion, which will generate a load from the constant 5679 // pool. 5680 if (isConstant) 5681 return SDValue(); 5682 5683 // Empirical tests suggest this is rarely worth it for vectors of length <= 2. 5684 if (NumElts >= 4) { 5685 SDValue shuffle = ReconstructShuffle(Op, DAG); 5686 if (shuffle != SDValue()) 5687 return shuffle; 5688 } 5689 5690 // Vectors with 32- or 64-bit elements can be built by directly assigning 5691 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands 5692 // will be legalized. 5693 if (EltSize >= 32) { 5694 // Do the expansion with floating-point types, since that is what the VFP 5695 // registers are defined to use, and since i64 is not legal. 5696 EVT EltVT = EVT::getFloatingPointVT(EltSize); 5697 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 5698 SmallVector<SDValue, 8> Ops; 5699 for (unsigned i = 0; i < NumElts; ++i) 5700 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i))); 5701 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 5702 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 5703 } 5704 5705 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we 5706 // know the default expansion would otherwise fall back on something even 5707 // worse. For a vector with one or two non-undef values, that's 5708 // scalar_to_vector for the elements followed by a shuffle (provided the 5709 // shuffle is valid for the target) and materialization element by element 5710 // on the stack followed by a load for everything else. 5711 if (!isConstant && !usesOnlyOneValue) { 5712 SDValue Vec = DAG.getUNDEF(VT); 5713 for (unsigned i = 0 ; i < NumElts; ++i) { 5714 SDValue V = Op.getOperand(i); 5715 if (V.isUndef()) 5716 continue; 5717 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32); 5718 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx); 5719 } 5720 return Vec; 5721 } 5722 5723 return SDValue(); 5724 } 5725 5726 // Gather data to see if the operation can be modelled as a 5727 // shuffle in combination with VEXTs. 5728 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, 5729 SelectionDAG &DAG) const { 5730 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!"); 5731 SDLoc dl(Op); 5732 EVT VT = Op.getValueType(); 5733 unsigned NumElts = VT.getVectorNumElements(); 5734 5735 struct ShuffleSourceInfo { 5736 SDValue Vec; 5737 unsigned MinElt; 5738 unsigned MaxElt; 5739 5740 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to 5741 // be compatible with the shuffle we intend to construct. As a result 5742 // ShuffleVec will be some sliding window into the original Vec. 5743 SDValue ShuffleVec; 5744 5745 // Code should guarantee that element i in Vec starts at element "WindowBase 5746 // + i * WindowScale in ShuffleVec". 5747 int WindowBase; 5748 int WindowScale; 5749 5750 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; } 5751 ShuffleSourceInfo(SDValue Vec) 5752 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0), 5753 WindowScale(1) {} 5754 }; 5755 5756 // First gather all vectors used as an immediate source for this BUILD_VECTOR 5757 // node. 5758 SmallVector<ShuffleSourceInfo, 2> Sources; 5759 for (unsigned i = 0; i < NumElts; ++i) { 5760 SDValue V = Op.getOperand(i); 5761 if (V.isUndef()) 5762 continue; 5763 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) { 5764 // A shuffle can only come from building a vector from various 5765 // elements of other vectors. 5766 return SDValue(); 5767 } else if (!isa<ConstantSDNode>(V.getOperand(1))) { 5768 // Furthermore, shuffles require a constant mask, whereas extractelts 5769 // accept variable indices. 5770 return SDValue(); 5771 } 5772 5773 // Add this element source to the list if it's not already there. 5774 SDValue SourceVec = V.getOperand(0); 5775 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec); 5776 if (Source == Sources.end()) 5777 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec)); 5778 5779 // Update the minimum and maximum lane number seen. 5780 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue(); 5781 Source->MinElt = std::min(Source->MinElt, EltNo); 5782 Source->MaxElt = std::max(Source->MaxElt, EltNo); 5783 } 5784 5785 // Currently only do something sane when at most two source vectors 5786 // are involved. 5787 if (Sources.size() > 2) 5788 return SDValue(); 5789 5790 // Find out the smallest element size among result and two sources, and use 5791 // it as element size to build the shuffle_vector. 5792 EVT SmallestEltTy = VT.getVectorElementType(); 5793 for (auto &Source : Sources) { 5794 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType(); 5795 if (SrcEltTy.bitsLT(SmallestEltTy)) 5796 SmallestEltTy = SrcEltTy; 5797 } 5798 unsigned ResMultiplier = 5799 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits(); 5800 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits(); 5801 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts); 5802 5803 // If the source vector is too wide or too narrow, we may nevertheless be able 5804 // to construct a compatible shuffle either by concatenating it with UNDEF or 5805 // extracting a suitable range of elements. 5806 for (auto &Src : Sources) { 5807 EVT SrcVT = Src.ShuffleVec.getValueType(); 5808 5809 if (SrcVT.getSizeInBits() == VT.getSizeInBits()) 5810 continue; 5811 5812 // This stage of the search produces a source with the same element type as 5813 // the original, but with a total width matching the BUILD_VECTOR output. 5814 EVT EltVT = SrcVT.getVectorElementType(); 5815 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits(); 5816 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts); 5817 5818 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) { 5819 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits()) 5820 return SDValue(); 5821 // We can pad out the smaller vector for free, so if it's part of a 5822 // shuffle... 5823 Src.ShuffleVec = 5824 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec, 5825 DAG.getUNDEF(Src.ShuffleVec.getValueType())); 5826 continue; 5827 } 5828 5829 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits()) 5830 return SDValue(); 5831 5832 if (Src.MaxElt - Src.MinElt >= NumSrcElts) { 5833 // Span too large for a VEXT to cope 5834 return SDValue(); 5835 } 5836 5837 if (Src.MinElt >= NumSrcElts) { 5838 // The extraction can just take the second half 5839 Src.ShuffleVec = 5840 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 5841 DAG.getConstant(NumSrcElts, dl, MVT::i32)); 5842 Src.WindowBase = -NumSrcElts; 5843 } else if (Src.MaxElt < NumSrcElts) { 5844 // The extraction can just take the first half 5845 Src.ShuffleVec = 5846 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 5847 DAG.getConstant(0, dl, MVT::i32)); 5848 } else { 5849 // An actual VEXT is needed 5850 SDValue VEXTSrc1 = 5851 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 5852 DAG.getConstant(0, dl, MVT::i32)); 5853 SDValue VEXTSrc2 = 5854 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec, 5855 DAG.getConstant(NumSrcElts, dl, MVT::i32)); 5856 5857 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1, 5858 VEXTSrc2, 5859 DAG.getConstant(Src.MinElt, dl, MVT::i32)); 5860 Src.WindowBase = -Src.MinElt; 5861 } 5862 } 5863 5864 // Another possible incompatibility occurs from the vector element types. We 5865 // can fix this by bitcasting the source vectors to the same type we intend 5866 // for the shuffle. 5867 for (auto &Src : Sources) { 5868 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType(); 5869 if (SrcEltTy == SmallestEltTy) 5870 continue; 5871 assert(ShuffleVT.getVectorElementType() == SmallestEltTy); 5872 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec); 5873 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits(); 5874 Src.WindowBase *= Src.WindowScale; 5875 } 5876 5877 // Final sanity check before we try to actually produce a shuffle. 5878 DEBUG( 5879 for (auto Src : Sources) 5880 assert(Src.ShuffleVec.getValueType() == ShuffleVT); 5881 ); 5882 5883 // The stars all align, our next step is to produce the mask for the shuffle. 5884 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1); 5885 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits(); 5886 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 5887 SDValue Entry = Op.getOperand(i); 5888 if (Entry.isUndef()) 5889 continue; 5890 5891 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0)); 5892 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue(); 5893 5894 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit 5895 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this 5896 // segment. 5897 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType(); 5898 int BitsDefined = std::min(OrigEltTy.getSizeInBits(), 5899 VT.getVectorElementType().getSizeInBits()); 5900 int LanesDefined = BitsDefined / BitsPerShuffleLane; 5901 5902 // This source is expected to fill ResMultiplier lanes of the final shuffle, 5903 // starting at the appropriate offset. 5904 int *LaneMask = &Mask[i * ResMultiplier]; 5905 5906 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase; 5907 ExtractBase += NumElts * (Src - Sources.begin()); 5908 for (int j = 0; j < LanesDefined; ++j) 5909 LaneMask[j] = ExtractBase + j; 5910 } 5911 5912 // Final check before we try to produce nonsense... 5913 if (!isShuffleMaskLegal(Mask, ShuffleVT)) 5914 return SDValue(); 5915 5916 // We can't handle more than two sources. This should have already 5917 // been checked before this point. 5918 assert(Sources.size() <= 2 && "Too many sources!"); 5919 5920 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) }; 5921 for (unsigned i = 0; i < Sources.size(); ++i) 5922 ShuffleOps[i] = Sources[i].ShuffleVec; 5923 5924 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0], 5925 ShuffleOps[1], &Mask[0]); 5926 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle); 5927 } 5928 5929 /// isShuffleMaskLegal - Targets can use this to indicate that they only 5930 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 5931 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 5932 /// are assumed to be legal. 5933 bool 5934 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, 5935 EVT VT) const { 5936 if (VT.getVectorNumElements() == 4 && 5937 (VT.is128BitVector() || VT.is64BitVector())) { 5938 unsigned PFIndexes[4]; 5939 for (unsigned i = 0; i != 4; ++i) { 5940 if (M[i] < 0) 5941 PFIndexes[i] = 8; 5942 else 5943 PFIndexes[i] = M[i]; 5944 } 5945 5946 // Compute the index in the perfect shuffle table. 5947 unsigned PFTableIndex = 5948 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 5949 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 5950 unsigned Cost = (PFEntry >> 30); 5951 5952 if (Cost <= 4) 5953 return true; 5954 } 5955 5956 bool ReverseVEXT, isV_UNDEF; 5957 unsigned Imm, WhichResult; 5958 5959 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 5960 return (EltSize >= 32 || 5961 ShuffleVectorSDNode::isSplatMask(&M[0], VT) || 5962 isVREVMask(M, VT, 64) || 5963 isVREVMask(M, VT, 32) || 5964 isVREVMask(M, VT, 16) || 5965 isVEXTMask(M, VT, ReverseVEXT, Imm) || 5966 isVTBLMask(M, VT) || 5967 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) || 5968 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT))); 5969 } 5970 5971 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit 5972 /// the specified operations to build the shuffle. 5973 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, 5974 SDValue RHS, SelectionDAG &DAG, 5975 SDLoc dl) { 5976 unsigned OpNum = (PFEntry >> 26) & 0x0F; 5977 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); 5978 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); 5979 5980 enum { 5981 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> 5982 OP_VREV, 5983 OP_VDUP0, 5984 OP_VDUP1, 5985 OP_VDUP2, 5986 OP_VDUP3, 5987 OP_VEXT1, 5988 OP_VEXT2, 5989 OP_VEXT3, 5990 OP_VUZPL, // VUZP, left result 5991 OP_VUZPR, // VUZP, right result 5992 OP_VZIPL, // VZIP, left result 5993 OP_VZIPR, // VZIP, right result 5994 OP_VTRNL, // VTRN, left result 5995 OP_VTRNR // VTRN, right result 5996 }; 5997 5998 if (OpNum == OP_COPY) { 5999 if (LHSID == (1*9+2)*9+3) return LHS; 6000 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); 6001 return RHS; 6002 } 6003 6004 SDValue OpLHS, OpRHS; 6005 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); 6006 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); 6007 EVT VT = OpLHS.getValueType(); 6008 6009 switch (OpNum) { 6010 default: llvm_unreachable("Unknown shuffle opcode!"); 6011 case OP_VREV: 6012 // VREV divides the vector in half and swaps within the half. 6013 if (VT.getVectorElementType() == MVT::i32 || 6014 VT.getVectorElementType() == MVT::f32) 6015 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS); 6016 // vrev <4 x i16> -> VREV32 6017 if (VT.getVectorElementType() == MVT::i16) 6018 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS); 6019 // vrev <4 x i8> -> VREV16 6020 assert(VT.getVectorElementType() == MVT::i8); 6021 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS); 6022 case OP_VDUP0: 6023 case OP_VDUP1: 6024 case OP_VDUP2: 6025 case OP_VDUP3: 6026 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, 6027 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32)); 6028 case OP_VEXT1: 6029 case OP_VEXT2: 6030 case OP_VEXT3: 6031 return DAG.getNode(ARMISD::VEXT, dl, VT, 6032 OpLHS, OpRHS, 6033 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32)); 6034 case OP_VUZPL: 6035 case OP_VUZPR: 6036 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT), 6037 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL); 6038 case OP_VZIPL: 6039 case OP_VZIPR: 6040 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT), 6041 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL); 6042 case OP_VTRNL: 6043 case OP_VTRNR: 6044 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT), 6045 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL); 6046 } 6047 } 6048 6049 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op, 6050 ArrayRef<int> ShuffleMask, 6051 SelectionDAG &DAG) { 6052 // Check to see if we can use the VTBL instruction. 6053 SDValue V1 = Op.getOperand(0); 6054 SDValue V2 = Op.getOperand(1); 6055 SDLoc DL(Op); 6056 6057 SmallVector<SDValue, 8> VTBLMask; 6058 for (ArrayRef<int>::iterator 6059 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I) 6060 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32)); 6061 6062 if (V2.getNode()->isUndef()) 6063 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1, 6064 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask)); 6065 6066 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2, 6067 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask)); 6068 } 6069 6070 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op, 6071 SelectionDAG &DAG) { 6072 SDLoc DL(Op); 6073 SDValue OpLHS = Op.getOperand(0); 6074 EVT VT = OpLHS.getValueType(); 6075 6076 assert((VT == MVT::v8i16 || VT == MVT::v16i8) && 6077 "Expect an v8i16/v16i8 type"); 6078 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS); 6079 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now, 6080 // extract the first 8 bytes into the top double word and the last 8 bytes 6081 // into the bottom double word. The v8i16 case is similar. 6082 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4; 6083 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS, 6084 DAG.getConstant(ExtractNum, DL, MVT::i32)); 6085 } 6086 6087 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) { 6088 SDValue V1 = Op.getOperand(0); 6089 SDValue V2 = Op.getOperand(1); 6090 SDLoc dl(Op); 6091 EVT VT = Op.getValueType(); 6092 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode()); 6093 6094 // Convert shuffles that are directly supported on NEON to target-specific 6095 // DAG nodes, instead of keeping them as shuffles and matching them again 6096 // during code selection. This is more efficient and avoids the possibility 6097 // of inconsistencies between legalization and selection. 6098 // FIXME: floating-point vectors should be canonicalized to integer vectors 6099 // of the same time so that they get CSEd properly. 6100 ArrayRef<int> ShuffleMask = SVN->getMask(); 6101 6102 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 6103 if (EltSize <= 32) { 6104 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) { 6105 int Lane = SVN->getSplatIndex(); 6106 // If this is undef splat, generate it via "just" vdup, if possible. 6107 if (Lane == -1) Lane = 0; 6108 6109 // Test if V1 is a SCALAR_TO_VECTOR. 6110 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) { 6111 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 6112 } 6113 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR 6114 // (and probably will turn into a SCALAR_TO_VECTOR once legalization 6115 // reaches it). 6116 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR && 6117 !isa<ConstantSDNode>(V1.getOperand(0))) { 6118 bool IsScalarToVector = true; 6119 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i) 6120 if (!V1.getOperand(i).isUndef()) { 6121 IsScalarToVector = false; 6122 break; 6123 } 6124 if (IsScalarToVector) 6125 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0)); 6126 } 6127 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1, 6128 DAG.getConstant(Lane, dl, MVT::i32)); 6129 } 6130 6131 bool ReverseVEXT; 6132 unsigned Imm; 6133 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) { 6134 if (ReverseVEXT) 6135 std::swap(V1, V2); 6136 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2, 6137 DAG.getConstant(Imm, dl, MVT::i32)); 6138 } 6139 6140 if (isVREVMask(ShuffleMask, VT, 64)) 6141 return DAG.getNode(ARMISD::VREV64, dl, VT, V1); 6142 if (isVREVMask(ShuffleMask, VT, 32)) 6143 return DAG.getNode(ARMISD::VREV32, dl, VT, V1); 6144 if (isVREVMask(ShuffleMask, VT, 16)) 6145 return DAG.getNode(ARMISD::VREV16, dl, VT, V1); 6146 6147 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) { 6148 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1, 6149 DAG.getConstant(Imm, dl, MVT::i32)); 6150 } 6151 6152 // Check for Neon shuffles that modify both input vectors in place. 6153 // If both results are used, i.e., if there are two shuffles with the same 6154 // source operands and with masks corresponding to both results of one of 6155 // these operations, DAG memoization will ensure that a single node is 6156 // used for both shuffles. 6157 unsigned WhichResult; 6158 bool isV_UNDEF; 6159 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask( 6160 ShuffleMask, VT, WhichResult, isV_UNDEF)) { 6161 if (isV_UNDEF) 6162 V2 = V1; 6163 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2) 6164 .getValue(WhichResult); 6165 } 6166 6167 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize 6168 // shuffles that produce a result larger than their operands with: 6169 // shuffle(concat(v1, undef), concat(v2, undef)) 6170 // -> 6171 // shuffle(concat(v1, v2), undef) 6172 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine). 6173 // 6174 // This is useful in the general case, but there are special cases where 6175 // native shuffles produce larger results: the two-result ops. 6176 // 6177 // Look through the concat when lowering them: 6178 // shuffle(concat(v1, v2), undef) 6179 // -> 6180 // concat(VZIP(v1, v2):0, :1) 6181 // 6182 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) { 6183 SDValue SubV1 = V1->getOperand(0); 6184 SDValue SubV2 = V1->getOperand(1); 6185 EVT SubVT = SubV1.getValueType(); 6186 6187 // We expect these to have been canonicalized to -1. 6188 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) { 6189 return i < (int)VT.getVectorNumElements(); 6190 }) && "Unexpected shuffle index into UNDEF operand!"); 6191 6192 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask( 6193 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) { 6194 if (isV_UNDEF) 6195 SubV2 = SubV1; 6196 assert((WhichResult == 0) && 6197 "In-place shuffle of concat can only have one result!"); 6198 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT), 6199 SubV1, SubV2); 6200 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0), 6201 Res.getValue(1)); 6202 } 6203 } 6204 } 6205 6206 // If the shuffle is not directly supported and it has 4 elements, use 6207 // the PerfectShuffle-generated table to synthesize it from other shuffles. 6208 unsigned NumElts = VT.getVectorNumElements(); 6209 if (NumElts == 4) { 6210 unsigned PFIndexes[4]; 6211 for (unsigned i = 0; i != 4; ++i) { 6212 if (ShuffleMask[i] < 0) 6213 PFIndexes[i] = 8; 6214 else 6215 PFIndexes[i] = ShuffleMask[i]; 6216 } 6217 6218 // Compute the index in the perfect shuffle table. 6219 unsigned PFTableIndex = 6220 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; 6221 unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; 6222 unsigned Cost = (PFEntry >> 30); 6223 6224 if (Cost <= 4) 6225 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); 6226 } 6227 6228 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs. 6229 if (EltSize >= 32) { 6230 // Do the expansion with floating-point types, since that is what the VFP 6231 // registers are defined to use, and since i64 is not legal. 6232 EVT EltVT = EVT::getFloatingPointVT(EltSize); 6233 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts); 6234 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1); 6235 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2); 6236 SmallVector<SDValue, 8> Ops; 6237 for (unsigned i = 0; i < NumElts; ++i) { 6238 if (ShuffleMask[i] < 0) 6239 Ops.push_back(DAG.getUNDEF(EltVT)); 6240 else 6241 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, 6242 ShuffleMask[i] < (int)NumElts ? V1 : V2, 6243 DAG.getConstant(ShuffleMask[i] & (NumElts-1), 6244 dl, MVT::i32))); 6245 } 6246 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops); 6247 return DAG.getNode(ISD::BITCAST, dl, VT, Val); 6248 } 6249 6250 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT)) 6251 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG); 6252 6253 if (VT == MVT::v8i8) 6254 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG)) 6255 return NewOp; 6256 6257 return SDValue(); 6258 } 6259 6260 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 6261 // INSERT_VECTOR_ELT is legal only for immediate indexes. 6262 SDValue Lane = Op.getOperand(2); 6263 if (!isa<ConstantSDNode>(Lane)) 6264 return SDValue(); 6265 6266 return Op; 6267 } 6268 6269 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) { 6270 // EXTRACT_VECTOR_ELT is legal only for immediate indexes. 6271 SDValue Lane = Op.getOperand(1); 6272 if (!isa<ConstantSDNode>(Lane)) 6273 return SDValue(); 6274 6275 SDValue Vec = Op.getOperand(0); 6276 if (Op.getValueType() == MVT::i32 && 6277 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) { 6278 SDLoc dl(Op); 6279 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane); 6280 } 6281 6282 return Op; 6283 } 6284 6285 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { 6286 // The only time a CONCAT_VECTORS operation can have legal types is when 6287 // two 64-bit vectors are concatenated to a 128-bit vector. 6288 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 && 6289 "unexpected CONCAT_VECTORS"); 6290 SDLoc dl(Op); 6291 SDValue Val = DAG.getUNDEF(MVT::v2f64); 6292 SDValue Op0 = Op.getOperand(0); 6293 SDValue Op1 = Op.getOperand(1); 6294 if (!Op0.isUndef()) 6295 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 6296 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0), 6297 DAG.getIntPtrConstant(0, dl)); 6298 if (!Op1.isUndef()) 6299 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val, 6300 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1), 6301 DAG.getIntPtrConstant(1, dl)); 6302 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val); 6303 } 6304 6305 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each 6306 /// element has been zero/sign-extended, depending on the isSigned parameter, 6307 /// from an integer type half its size. 6308 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG, 6309 bool isSigned) { 6310 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32. 6311 EVT VT = N->getValueType(0); 6312 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) { 6313 SDNode *BVN = N->getOperand(0).getNode(); 6314 if (BVN->getValueType(0) != MVT::v4i32 || 6315 BVN->getOpcode() != ISD::BUILD_VECTOR) 6316 return false; 6317 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0; 6318 unsigned HiElt = 1 - LoElt; 6319 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt)); 6320 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt)); 6321 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2)); 6322 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2)); 6323 if (!Lo0 || !Hi0 || !Lo1 || !Hi1) 6324 return false; 6325 if (isSigned) { 6326 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 && 6327 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32) 6328 return true; 6329 } else { 6330 if (Hi0->isNullValue() && Hi1->isNullValue()) 6331 return true; 6332 } 6333 return false; 6334 } 6335 6336 if (N->getOpcode() != ISD::BUILD_VECTOR) 6337 return false; 6338 6339 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 6340 SDNode *Elt = N->getOperand(i).getNode(); 6341 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) { 6342 unsigned EltSize = VT.getVectorElementType().getSizeInBits(); 6343 unsigned HalfSize = EltSize / 2; 6344 if (isSigned) { 6345 if (!isIntN(HalfSize, C->getSExtValue())) 6346 return false; 6347 } else { 6348 if (!isUIntN(HalfSize, C->getZExtValue())) 6349 return false; 6350 } 6351 continue; 6352 } 6353 return false; 6354 } 6355 6356 return true; 6357 } 6358 6359 /// isSignExtended - Check if a node is a vector value that is sign-extended 6360 /// or a constant BUILD_VECTOR with sign-extended elements. 6361 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) { 6362 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N)) 6363 return true; 6364 if (isExtendedBUILD_VECTOR(N, DAG, true)) 6365 return true; 6366 return false; 6367 } 6368 6369 /// isZeroExtended - Check if a node is a vector value that is zero-extended 6370 /// or a constant BUILD_VECTOR with zero-extended elements. 6371 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) { 6372 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N)) 6373 return true; 6374 if (isExtendedBUILD_VECTOR(N, DAG, false)) 6375 return true; 6376 return false; 6377 } 6378 6379 static EVT getExtensionTo64Bits(const EVT &OrigVT) { 6380 if (OrigVT.getSizeInBits() >= 64) 6381 return OrigVT; 6382 6383 assert(OrigVT.isSimple() && "Expecting a simple value type"); 6384 6385 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy; 6386 switch (OrigSimpleTy) { 6387 default: llvm_unreachable("Unexpected Vector Type"); 6388 case MVT::v2i8: 6389 case MVT::v2i16: 6390 return MVT::v2i32; 6391 case MVT::v4i8: 6392 return MVT::v4i16; 6393 } 6394 } 6395 6396 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total 6397 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL. 6398 /// We insert the required extension here to get the vector to fill a D register. 6399 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG, 6400 const EVT &OrigTy, 6401 const EVT &ExtTy, 6402 unsigned ExtOpcode) { 6403 // The vector originally had a size of OrigTy. It was then extended to ExtTy. 6404 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than 6405 // 64-bits we need to insert a new extension so that it will be 64-bits. 6406 assert(ExtTy.is128BitVector() && "Unexpected extension size"); 6407 if (OrigTy.getSizeInBits() >= 64) 6408 return N; 6409 6410 // Must extend size to at least 64 bits to be used as an operand for VMULL. 6411 EVT NewVT = getExtensionTo64Bits(OrigTy); 6412 6413 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N); 6414 } 6415 6416 /// SkipLoadExtensionForVMULL - return a load of the original vector size that 6417 /// does not do any sign/zero extension. If the original vector is less 6418 /// than 64 bits, an appropriate extension will be added after the load to 6419 /// reach a total size of 64 bits. We have to add the extension separately 6420 /// because ARM does not have a sign/zero extending load for vectors. 6421 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) { 6422 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT()); 6423 6424 // The load already has the right type. 6425 if (ExtendedTy == LD->getMemoryVT()) 6426 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(), 6427 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(), 6428 LD->isNonTemporal(), LD->isInvariant(), 6429 LD->getAlignment()); 6430 6431 // We need to create a zextload/sextload. We cannot just create a load 6432 // followed by a zext/zext node because LowerMUL is also run during normal 6433 // operation legalization where we can't create illegal types. 6434 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy, 6435 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(), 6436 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(), 6437 LD->isNonTemporal(), LD->getAlignment()); 6438 } 6439 6440 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND, 6441 /// extending load, or BUILD_VECTOR with extended elements, return the 6442 /// unextended value. The unextended vector should be 64 bits so that it can 6443 /// be used as an operand to a VMULL instruction. If the original vector size 6444 /// before extension is less than 64 bits we add a an extension to resize 6445 /// the vector to 64 bits. 6446 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) { 6447 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND) 6448 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG, 6449 N->getOperand(0)->getValueType(0), 6450 N->getValueType(0), 6451 N->getOpcode()); 6452 6453 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) 6454 return SkipLoadExtensionForVMULL(LD, DAG); 6455 6456 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will 6457 // have been legalized as a BITCAST from v4i32. 6458 if (N->getOpcode() == ISD::BITCAST) { 6459 SDNode *BVN = N->getOperand(0).getNode(); 6460 assert(BVN->getOpcode() == ISD::BUILD_VECTOR && 6461 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR"); 6462 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0; 6463 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32, 6464 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2)); 6465 } 6466 // Construct a new BUILD_VECTOR with elements truncated to half the size. 6467 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR"); 6468 EVT VT = N->getValueType(0); 6469 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2; 6470 unsigned NumElts = VT.getVectorNumElements(); 6471 MVT TruncVT = MVT::getIntegerVT(EltSize); 6472 SmallVector<SDValue, 8> Ops; 6473 SDLoc dl(N); 6474 for (unsigned i = 0; i != NumElts; ++i) { 6475 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i)); 6476 const APInt &CInt = C->getAPIntValue(); 6477 // Element types smaller than 32 bits are not legal, so use i32 elements. 6478 // The values are implicitly truncated so sext vs. zext doesn't matter. 6479 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32)); 6480 } 6481 return DAG.getNode(ISD::BUILD_VECTOR, dl, 6482 MVT::getVectorVT(TruncVT, NumElts), Ops); 6483 } 6484 6485 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) { 6486 unsigned Opcode = N->getOpcode(); 6487 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 6488 SDNode *N0 = N->getOperand(0).getNode(); 6489 SDNode *N1 = N->getOperand(1).getNode(); 6490 return N0->hasOneUse() && N1->hasOneUse() && 6491 isSignExtended(N0, DAG) && isSignExtended(N1, DAG); 6492 } 6493 return false; 6494 } 6495 6496 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) { 6497 unsigned Opcode = N->getOpcode(); 6498 if (Opcode == ISD::ADD || Opcode == ISD::SUB) { 6499 SDNode *N0 = N->getOperand(0).getNode(); 6500 SDNode *N1 = N->getOperand(1).getNode(); 6501 return N0->hasOneUse() && N1->hasOneUse() && 6502 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG); 6503 } 6504 return false; 6505 } 6506 6507 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) { 6508 // Multiplications are only custom-lowered for 128-bit vectors so that 6509 // VMULL can be detected. Otherwise v2i64 multiplications are not legal. 6510 EVT VT = Op.getValueType(); 6511 assert(VT.is128BitVector() && VT.isInteger() && 6512 "unexpected type for custom-lowering ISD::MUL"); 6513 SDNode *N0 = Op.getOperand(0).getNode(); 6514 SDNode *N1 = Op.getOperand(1).getNode(); 6515 unsigned NewOpc = 0; 6516 bool isMLA = false; 6517 bool isN0SExt = isSignExtended(N0, DAG); 6518 bool isN1SExt = isSignExtended(N1, DAG); 6519 if (isN0SExt && isN1SExt) 6520 NewOpc = ARMISD::VMULLs; 6521 else { 6522 bool isN0ZExt = isZeroExtended(N0, DAG); 6523 bool isN1ZExt = isZeroExtended(N1, DAG); 6524 if (isN0ZExt && isN1ZExt) 6525 NewOpc = ARMISD::VMULLu; 6526 else if (isN1SExt || isN1ZExt) { 6527 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these 6528 // into (s/zext A * s/zext C) + (s/zext B * s/zext C) 6529 if (isN1SExt && isAddSubSExt(N0, DAG)) { 6530 NewOpc = ARMISD::VMULLs; 6531 isMLA = true; 6532 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) { 6533 NewOpc = ARMISD::VMULLu; 6534 isMLA = true; 6535 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) { 6536 std::swap(N0, N1); 6537 NewOpc = ARMISD::VMULLu; 6538 isMLA = true; 6539 } 6540 } 6541 6542 if (!NewOpc) { 6543 if (VT == MVT::v2i64) 6544 // Fall through to expand this. It is not legal. 6545 return SDValue(); 6546 else 6547 // Other vector multiplications are legal. 6548 return Op; 6549 } 6550 } 6551 6552 // Legalize to a VMULL instruction. 6553 SDLoc DL(Op); 6554 SDValue Op0; 6555 SDValue Op1 = SkipExtensionForVMULL(N1, DAG); 6556 if (!isMLA) { 6557 Op0 = SkipExtensionForVMULL(N0, DAG); 6558 assert(Op0.getValueType().is64BitVector() && 6559 Op1.getValueType().is64BitVector() && 6560 "unexpected types for extended operands to VMULL"); 6561 return DAG.getNode(NewOpc, DL, VT, Op0, Op1); 6562 } 6563 6564 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during 6565 // isel lowering to take advantage of no-stall back to back vmul + vmla. 6566 // vmull q0, d4, d6 6567 // vmlal q0, d5, d6 6568 // is faster than 6569 // vaddl q0, d4, d5 6570 // vmovl q1, d6 6571 // vmul q0, q0, q1 6572 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG); 6573 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG); 6574 EVT Op1VT = Op1.getValueType(); 6575 return DAG.getNode(N0->getOpcode(), DL, VT, 6576 DAG.getNode(NewOpc, DL, VT, 6577 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1), 6578 DAG.getNode(NewOpc, DL, VT, 6579 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1)); 6580 } 6581 6582 static SDValue 6583 LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) { 6584 // TODO: Should this propagate fast-math-flags? 6585 6586 // Convert to float 6587 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo)); 6588 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo)); 6589 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X); 6590 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y); 6591 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X); 6592 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y); 6593 // Get reciprocal estimate. 6594 // float4 recip = vrecpeq_f32(yf); 6595 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6596 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 6597 Y); 6598 // Because char has a smaller range than uchar, we can actually get away 6599 // without any newton steps. This requires that we use a weird bias 6600 // of 0xb000, however (again, this has been exhaustively tested). 6601 // float4 result = as_float4(as_int4(xf*recip) + 0xb000); 6602 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y); 6603 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X); 6604 Y = DAG.getConstant(0xb000, dl, MVT::v4i32); 6605 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y); 6606 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X); 6607 // Convert back to short. 6608 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X); 6609 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X); 6610 return X; 6611 } 6612 6613 static SDValue 6614 LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) { 6615 // TODO: Should this propagate fast-math-flags? 6616 6617 SDValue N2; 6618 // Convert to float. 6619 // float4 yf = vcvt_f32_s32(vmovl_s16(y)); 6620 // float4 xf = vcvt_f32_s32(vmovl_s16(x)); 6621 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0); 6622 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1); 6623 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 6624 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 6625 6626 // Use reciprocal estimate and one refinement step. 6627 // float4 recip = vrecpeq_f32(yf); 6628 // recip *= vrecpsq_f32(yf, recip); 6629 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6630 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 6631 N1); 6632 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6633 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 6634 N1, N2); 6635 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6636 // Because short has a smaller range than ushort, we can actually get away 6637 // with only a single newton step. This requires that we use a weird bias 6638 // of 89, however (again, this has been exhaustively tested). 6639 // float4 result = as_float4(as_int4(xf*recip) + 0x89); 6640 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 6641 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 6642 N1 = DAG.getConstant(0x89, dl, MVT::v4i32); 6643 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 6644 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 6645 // Convert back to integer and return. 6646 // return vmovn_s32(vcvt_s32_f32(result)); 6647 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 6648 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 6649 return N0; 6650 } 6651 6652 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) { 6653 EVT VT = Op.getValueType(); 6654 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 6655 "unexpected type for custom-lowering ISD::SDIV"); 6656 6657 SDLoc dl(Op); 6658 SDValue N0 = Op.getOperand(0); 6659 SDValue N1 = Op.getOperand(1); 6660 SDValue N2, N3; 6661 6662 if (VT == MVT::v8i8) { 6663 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0); 6664 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1); 6665 6666 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6667 DAG.getIntPtrConstant(4, dl)); 6668 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6669 DAG.getIntPtrConstant(4, dl)); 6670 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6671 DAG.getIntPtrConstant(0, dl)); 6672 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6673 DAG.getIntPtrConstant(0, dl)); 6674 6675 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16 6676 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16 6677 6678 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 6679 N0 = LowerCONCAT_VECTORS(N0, DAG); 6680 6681 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0); 6682 return N0; 6683 } 6684 return LowerSDIV_v4i16(N0, N1, dl, DAG); 6685 } 6686 6687 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) { 6688 // TODO: Should this propagate fast-math-flags? 6689 EVT VT = Op.getValueType(); 6690 assert((VT == MVT::v4i16 || VT == MVT::v8i8) && 6691 "unexpected type for custom-lowering ISD::UDIV"); 6692 6693 SDLoc dl(Op); 6694 SDValue N0 = Op.getOperand(0); 6695 SDValue N1 = Op.getOperand(1); 6696 SDValue N2, N3; 6697 6698 if (VT == MVT::v8i8) { 6699 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0); 6700 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1); 6701 6702 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6703 DAG.getIntPtrConstant(4, dl)); 6704 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6705 DAG.getIntPtrConstant(4, dl)); 6706 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0, 6707 DAG.getIntPtrConstant(0, dl)); 6708 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1, 6709 DAG.getIntPtrConstant(0, dl)); 6710 6711 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16 6712 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16 6713 6714 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2); 6715 N0 = LowerCONCAT_VECTORS(N0, DAG); 6716 6717 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8, 6718 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl, 6719 MVT::i32), 6720 N0); 6721 return N0; 6722 } 6723 6724 // v4i16 sdiv ... Convert to float. 6725 // float4 yf = vcvt_f32_s32(vmovl_u16(y)); 6726 // float4 xf = vcvt_f32_s32(vmovl_u16(x)); 6727 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0); 6728 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1); 6729 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0); 6730 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1); 6731 6732 // Use reciprocal estimate and two refinement steps. 6733 // float4 recip = vrecpeq_f32(yf); 6734 // recip *= vrecpsq_f32(yf, recip); 6735 // recip *= vrecpsq_f32(yf, recip); 6736 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6737 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32), 6738 BN1); 6739 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6740 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 6741 BN1, N2); 6742 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6743 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32, 6744 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32), 6745 BN1, N2); 6746 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2); 6747 // Simply multiplying by the reciprocal estimate can leave us a few ulps 6748 // too low, so we add 2 ulps (exhaustive testing shows that this is enough, 6749 // and that it will never cause us to return an answer too large). 6750 // float4 result = as_float4(as_int4(xf*recip) + 2); 6751 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2); 6752 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0); 6753 N1 = DAG.getConstant(2, dl, MVT::v4i32); 6754 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1); 6755 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0); 6756 // Convert back to integer and return. 6757 // return vmovn_u32(vcvt_s32_f32(result)); 6758 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0); 6759 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0); 6760 return N0; 6761 } 6762 6763 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { 6764 EVT VT = Op.getNode()->getValueType(0); 6765 SDVTList VTs = DAG.getVTList(VT, MVT::i32); 6766 6767 unsigned Opc; 6768 bool ExtraOp = false; 6769 switch (Op.getOpcode()) { 6770 default: llvm_unreachable("Invalid code"); 6771 case ISD::ADDC: Opc = ARMISD::ADDC; break; 6772 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break; 6773 case ISD::SUBC: Opc = ARMISD::SUBC; break; 6774 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break; 6775 } 6776 6777 if (!ExtraOp) 6778 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 6779 Op.getOperand(1)); 6780 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), 6781 Op.getOperand(1), Op.getOperand(2)); 6782 } 6783 6784 SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const { 6785 assert(Subtarget->isTargetDarwin()); 6786 6787 // For iOS, we want to call an alternative entry point: __sincos_stret, 6788 // return values are passed via sret. 6789 SDLoc dl(Op); 6790 SDValue Arg = Op.getOperand(0); 6791 EVT ArgVT = Arg.getValueType(); 6792 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 6793 auto PtrVT = getPointerTy(DAG.getDataLayout()); 6794 6795 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); 6796 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 6797 6798 // Pair of floats / doubles used to pass the result. 6799 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr); 6800 auto &DL = DAG.getDataLayout(); 6801 6802 ArgListTy Args; 6803 bool ShouldUseSRet = Subtarget->isAPCS_ABI(); 6804 SDValue SRet; 6805 if (ShouldUseSRet) { 6806 // Create stack object for sret. 6807 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy); 6808 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy); 6809 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false); 6810 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL)); 6811 6812 ArgListEntry Entry; 6813 Entry.Node = SRet; 6814 Entry.Ty = RetTy->getPointerTo(); 6815 Entry.isSExt = false; 6816 Entry.isZExt = false; 6817 Entry.isSRet = true; 6818 Args.push_back(Entry); 6819 RetTy = Type::getVoidTy(*DAG.getContext()); 6820 } 6821 6822 ArgListEntry Entry; 6823 Entry.Node = Arg; 6824 Entry.Ty = ArgTy; 6825 Entry.isSExt = false; 6826 Entry.isZExt = false; 6827 Args.push_back(Entry); 6828 6829 const char *LibcallName = 6830 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret"; 6831 RTLIB::Libcall LC = 6832 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32; 6833 CallingConv::ID CC = getLibcallCallingConv(LC); 6834 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL)); 6835 6836 TargetLowering::CallLoweringInfo CLI(DAG); 6837 CLI.setDebugLoc(dl) 6838 .setChain(DAG.getEntryNode()) 6839 .setCallee(CC, RetTy, Callee, std::move(Args), 0) 6840 .setDiscardResult(ShouldUseSRet); 6841 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 6842 6843 if (!ShouldUseSRet) 6844 return CallResult.first; 6845 6846 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet, 6847 MachinePointerInfo(), false, false, false, 0); 6848 6849 // Address of cos field. 6850 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet, 6851 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl)); 6852 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, 6853 MachinePointerInfo(), false, false, false, 0); 6854 6855 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT); 6856 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, 6857 LoadSin.getValue(0), LoadCos.getValue(0)); 6858 } 6859 6860 SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG, 6861 bool Signed, 6862 SDValue &Chain) const { 6863 EVT VT = Op.getValueType(); 6864 assert((VT == MVT::i32 || VT == MVT::i64) && 6865 "unexpected type for custom lowering DIV"); 6866 SDLoc dl(Op); 6867 6868 const auto &DL = DAG.getDataLayout(); 6869 const auto &TLI = DAG.getTargetLoweringInfo(); 6870 6871 const char *Name = nullptr; 6872 if (Signed) 6873 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64"; 6874 else 6875 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64"; 6876 6877 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL)); 6878 6879 ARMTargetLowering::ArgListTy Args; 6880 6881 for (auto AI : {1, 0}) { 6882 ArgListEntry Arg; 6883 Arg.Node = Op.getOperand(AI); 6884 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext()); 6885 Args.push_back(Arg); 6886 } 6887 6888 CallLoweringInfo CLI(DAG); 6889 CLI.setDebugLoc(dl) 6890 .setChain(Chain) 6891 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()), 6892 ES, std::move(Args), 0); 6893 6894 return LowerCallTo(CLI).first; 6895 } 6896 6897 SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG, 6898 bool Signed) const { 6899 assert(Op.getValueType() == MVT::i32 && 6900 "unexpected type for custom lowering DIV"); 6901 SDLoc dl(Op); 6902 6903 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, 6904 DAG.getEntryNode(), Op.getOperand(1)); 6905 6906 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK); 6907 } 6908 6909 void ARMTargetLowering::ExpandDIV_Windows( 6910 SDValue Op, SelectionDAG &DAG, bool Signed, 6911 SmallVectorImpl<SDValue> &Results) const { 6912 const auto &DL = DAG.getDataLayout(); 6913 const auto &TLI = DAG.getTargetLoweringInfo(); 6914 6915 assert(Op.getValueType() == MVT::i64 && 6916 "unexpected type for custom lowering DIV"); 6917 SDLoc dl(Op); 6918 6919 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1), 6920 DAG.getConstant(0, dl, MVT::i32)); 6921 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1), 6922 DAG.getConstant(1, dl, MVT::i32)); 6923 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi); 6924 6925 SDValue DBZCHK = 6926 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or); 6927 6928 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK); 6929 6930 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result); 6931 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result, 6932 DAG.getConstant(32, dl, TLI.getPointerTy(DL))); 6933 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper); 6934 6935 Results.push_back(Lower); 6936 Results.push_back(Upper); 6937 } 6938 6939 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) { 6940 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering())) 6941 // Acquire/Release load/store is not legal for targets without a dmb or 6942 // equivalent available. 6943 return SDValue(); 6944 6945 // Monotonic load/store is legal for all targets. 6946 return Op; 6947 } 6948 6949 static void ReplaceREADCYCLECOUNTER(SDNode *N, 6950 SmallVectorImpl<SDValue> &Results, 6951 SelectionDAG &DAG, 6952 const ARMSubtarget *Subtarget) { 6953 SDLoc DL(N); 6954 // Under Power Management extensions, the cycle-count is: 6955 // mrc p15, #0, <Rt>, c9, c13, #0 6956 SDValue Ops[] = { N->getOperand(0), // Chain 6957 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32), 6958 DAG.getConstant(15, DL, MVT::i32), 6959 DAG.getConstant(0, DL, MVT::i32), 6960 DAG.getConstant(9, DL, MVT::i32), 6961 DAG.getConstant(13, DL, MVT::i32), 6962 DAG.getConstant(0, DL, MVT::i32) 6963 }; 6964 6965 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, 6966 DAG.getVTList(MVT::i32, MVT::Other), Ops); 6967 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32, 6968 DAG.getConstant(0, DL, MVT::i32))); 6969 Results.push_back(Cycles32.getValue(1)); 6970 } 6971 6972 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 6973 switch (Op.getOpcode()) { 6974 default: llvm_unreachable("Don't know how to custom lower this!"); 6975 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG); 6976 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 6977 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); 6978 case ISD::GlobalAddress: 6979 switch (Subtarget->getTargetTriple().getObjectFormat()) { 6980 default: llvm_unreachable("unknown object format"); 6981 case Triple::COFF: 6982 return LowerGlobalAddressWindows(Op, DAG); 6983 case Triple::ELF: 6984 return LowerGlobalAddressELF(Op, DAG); 6985 case Triple::MachO: 6986 return LowerGlobalAddressDarwin(Op, DAG); 6987 } 6988 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); 6989 case ISD::SELECT: return LowerSELECT(Op, DAG); 6990 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 6991 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 6992 case ISD::BR_JT: return LowerBR_JT(Op, DAG); 6993 case ISD::VASTART: return LowerVASTART(Op, DAG); 6994 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget); 6995 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget); 6996 case ISD::SINT_TO_FP: 6997 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG); 6998 case ISD::FP_TO_SINT: 6999 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); 7000 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); 7001 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 7002 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 7003 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG); 7004 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG); 7005 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG); 7006 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG, 7007 Subtarget); 7008 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG); 7009 case ISD::SHL: 7010 case ISD::SRL: 7011 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget); 7012 case ISD::SREM: return LowerREM(Op.getNode(), DAG); 7013 case ISD::UREM: return LowerREM(Op.getNode(), DAG); 7014 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG); 7015 case ISD::SRL_PARTS: 7016 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG); 7017 case ISD::CTTZ: 7018 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget); 7019 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget); 7020 case ISD::SETCC: return LowerVSETCC(Op, DAG); 7021 case ISD::SETCCE: return LowerSETCCE(Op, DAG); 7022 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget); 7023 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget); 7024 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); 7025 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); 7026 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); 7027 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); 7028 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); 7029 case ISD::MUL: return LowerMUL(Op, DAG); 7030 case ISD::SDIV: 7031 if (Subtarget->isTargetWindows()) 7032 return LowerDIV_Windows(Op, DAG, /* Signed */ true); 7033 return LowerSDIV(Op, DAG); 7034 case ISD::UDIV: 7035 if (Subtarget->isTargetWindows()) 7036 return LowerDIV_Windows(Op, DAG, /* Signed */ false); 7037 return LowerUDIV(Op, DAG); 7038 case ISD::ADDC: 7039 case ISD::ADDE: 7040 case ISD::SUBC: 7041 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); 7042 case ISD::SADDO: 7043 case ISD::UADDO: 7044 case ISD::SSUBO: 7045 case ISD::USUBO: 7046 return LowerXALUO(Op, DAG); 7047 case ISD::ATOMIC_LOAD: 7048 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG); 7049 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG); 7050 case ISD::SDIVREM: 7051 case ISD::UDIVREM: return LowerDivRem(Op, DAG); 7052 case ISD::DYNAMIC_STACKALLOC: 7053 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment()) 7054 return LowerDYNAMIC_STACKALLOC(Op, DAG); 7055 llvm_unreachable("Don't know how to custom lower this!"); 7056 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG); 7057 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG); 7058 case ARMISD::WIN__DBZCHK: return SDValue(); 7059 } 7060 } 7061 7062 /// ReplaceNodeResults - Replace the results of node with an illegal result 7063 /// type with new values built out of custom code. 7064 void ARMTargetLowering::ReplaceNodeResults(SDNode *N, 7065 SmallVectorImpl<SDValue> &Results, 7066 SelectionDAG &DAG) const { 7067 SDValue Res; 7068 switch (N->getOpcode()) { 7069 default: 7070 llvm_unreachable("Don't know how to custom expand this!"); 7071 case ISD::READ_REGISTER: 7072 ExpandREAD_REGISTER(N, Results, DAG); 7073 break; 7074 case ISD::BITCAST: 7075 Res = ExpandBITCAST(N, DAG); 7076 break; 7077 case ISD::SRL: 7078 case ISD::SRA: 7079 Res = Expand64BitShift(N, DAG, Subtarget); 7080 break; 7081 case ISD::SREM: 7082 case ISD::UREM: 7083 Res = LowerREM(N, DAG); 7084 break; 7085 case ISD::SDIVREM: 7086 case ISD::UDIVREM: 7087 Res = LowerDivRem(SDValue(N, 0), DAG); 7088 assert(Res.getNumOperands() == 2 && "DivRem needs two values"); 7089 Results.push_back(Res.getValue(0)); 7090 Results.push_back(Res.getValue(1)); 7091 return; 7092 case ISD::READCYCLECOUNTER: 7093 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget); 7094 return; 7095 case ISD::UDIV: 7096 case ISD::SDIV: 7097 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows"); 7098 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV, 7099 Results); 7100 } 7101 if (Res.getNode()) 7102 Results.push_back(Res); 7103 } 7104 7105 //===----------------------------------------------------------------------===// 7106 // ARM Scheduler Hooks 7107 //===----------------------------------------------------------------------===// 7108 7109 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and 7110 /// registers the function context. 7111 void ARMTargetLowering:: 7112 SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB, 7113 MachineBasicBlock *DispatchBB, int FI) const { 7114 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 7115 DebugLoc dl = MI->getDebugLoc(); 7116 MachineFunction *MF = MBB->getParent(); 7117 MachineRegisterInfo *MRI = &MF->getRegInfo(); 7118 MachineConstantPool *MCP = MF->getConstantPool(); 7119 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>(); 7120 const Function *F = MF->getFunction(); 7121 7122 bool isThumb = Subtarget->isThumb(); 7123 bool isThumb2 = Subtarget->isThumb2(); 7124 7125 unsigned PCLabelId = AFI->createPICLabelUId(); 7126 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8; 7127 ARMConstantPoolValue *CPV = 7128 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj); 7129 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4); 7130 7131 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass 7132 : &ARM::GPRRegClass; 7133 7134 // Grab constant pool and fixed stack memory operands. 7135 MachineMemOperand *CPMMO = 7136 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF), 7137 MachineMemOperand::MOLoad, 4, 4); 7138 7139 MachineMemOperand *FIMMOSt = 7140 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI), 7141 MachineMemOperand::MOStore, 4, 4); 7142 7143 // Load the address of the dispatch MBB into the jump buffer. 7144 if (isThumb2) { 7145 // Incoming value: jbuf 7146 // ldr.n r5, LCPI1_1 7147 // orr r5, r5, #1 7148 // add r5, pc 7149 // str r5, [$jbuf, #+4] ; &jbuf[1] 7150 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 7151 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1) 7152 .addConstantPoolIndex(CPI) 7153 .addMemOperand(CPMMO)); 7154 // Set the low bit because of thumb mode. 7155 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 7156 AddDefaultCC( 7157 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2) 7158 .addReg(NewVReg1, RegState::Kill) 7159 .addImm(0x01))); 7160 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 7161 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3) 7162 .addReg(NewVReg2, RegState::Kill) 7163 .addImm(PCLabelId); 7164 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12)) 7165 .addReg(NewVReg3, RegState::Kill) 7166 .addFrameIndex(FI) 7167 .addImm(36) // &jbuf[1] :: pc 7168 .addMemOperand(FIMMOSt)); 7169 } else if (isThumb) { 7170 // Incoming value: jbuf 7171 // ldr.n r1, LCPI1_4 7172 // add r1, pc 7173 // mov r2, #1 7174 // orrs r1, r2 7175 // add r2, $jbuf, #+4 ; &jbuf[1] 7176 // str r1, [r2] 7177 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 7178 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1) 7179 .addConstantPoolIndex(CPI) 7180 .addMemOperand(CPMMO)); 7181 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 7182 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2) 7183 .addReg(NewVReg1, RegState::Kill) 7184 .addImm(PCLabelId); 7185 // Set the low bit because of thumb mode. 7186 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 7187 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3) 7188 .addReg(ARM::CPSR, RegState::Define) 7189 .addImm(1)); 7190 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 7191 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4) 7192 .addReg(ARM::CPSR, RegState::Define) 7193 .addReg(NewVReg2, RegState::Kill) 7194 .addReg(NewVReg3, RegState::Kill)); 7195 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 7196 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5) 7197 .addFrameIndex(FI) 7198 .addImm(36); // &jbuf[1] :: pc 7199 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi)) 7200 .addReg(NewVReg4, RegState::Kill) 7201 .addReg(NewVReg5, RegState::Kill) 7202 .addImm(0) 7203 .addMemOperand(FIMMOSt)); 7204 } else { 7205 // Incoming value: jbuf 7206 // ldr r1, LCPI1_1 7207 // add r1, pc, r1 7208 // str r1, [$jbuf, #+4] ; &jbuf[1] 7209 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 7210 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1) 7211 .addConstantPoolIndex(CPI) 7212 .addImm(0) 7213 .addMemOperand(CPMMO)); 7214 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 7215 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2) 7216 .addReg(NewVReg1, RegState::Kill) 7217 .addImm(PCLabelId)); 7218 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12)) 7219 .addReg(NewVReg2, RegState::Kill) 7220 .addFrameIndex(FI) 7221 .addImm(36) // &jbuf[1] :: pc 7222 .addMemOperand(FIMMOSt)); 7223 } 7224 } 7225 7226 void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI, 7227 MachineBasicBlock *MBB) const { 7228 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 7229 DebugLoc dl = MI->getDebugLoc(); 7230 MachineFunction *MF = MBB->getParent(); 7231 MachineRegisterInfo *MRI = &MF->getRegInfo(); 7232 MachineFrameInfo *MFI = MF->getFrameInfo(); 7233 int FI = MFI->getFunctionContextIndex(); 7234 7235 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass 7236 : &ARM::GPRnopcRegClass; 7237 7238 // Get a mapping of the call site numbers to all of the landing pads they're 7239 // associated with. 7240 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad; 7241 unsigned MaxCSNum = 0; 7242 MachineModuleInfo &MMI = MF->getMMI(); 7243 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; 7244 ++BB) { 7245 if (!BB->isEHPad()) continue; 7246 7247 // FIXME: We should assert that the EH_LABEL is the first MI in the landing 7248 // pad. 7249 for (MachineBasicBlock::iterator 7250 II = BB->begin(), IE = BB->end(); II != IE; ++II) { 7251 if (!II->isEHLabel()) continue; 7252 7253 MCSymbol *Sym = II->getOperand(0).getMCSymbol(); 7254 if (!MMI.hasCallSiteLandingPad(Sym)) continue; 7255 7256 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym); 7257 for (SmallVectorImpl<unsigned>::iterator 7258 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end(); 7259 CSI != CSE; ++CSI) { 7260 CallSiteNumToLPad[*CSI].push_back(&*BB); 7261 MaxCSNum = std::max(MaxCSNum, *CSI); 7262 } 7263 break; 7264 } 7265 } 7266 7267 // Get an ordered list of the machine basic blocks for the jump table. 7268 std::vector<MachineBasicBlock*> LPadList; 7269 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs; 7270 LPadList.reserve(CallSiteNumToLPad.size()); 7271 for (unsigned I = 1; I <= MaxCSNum; ++I) { 7272 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I]; 7273 for (SmallVectorImpl<MachineBasicBlock*>::iterator 7274 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) { 7275 LPadList.push_back(*II); 7276 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end()); 7277 } 7278 } 7279 7280 assert(!LPadList.empty() && 7281 "No landing pad destinations for the dispatch jump table!"); 7282 7283 // Create the jump table and associated information. 7284 MachineJumpTableInfo *JTI = 7285 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline); 7286 unsigned MJTI = JTI->createJumpTableIndex(LPadList); 7287 Reloc::Model RelocM = getTargetMachine().getRelocationModel(); 7288 7289 // Create the MBBs for the dispatch code. 7290 7291 // Shove the dispatch's address into the return slot in the function context. 7292 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); 7293 DispatchBB->setIsEHPad(); 7294 7295 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 7296 unsigned trap_opcode; 7297 if (Subtarget->isThumb()) 7298 trap_opcode = ARM::tTRAP; 7299 else 7300 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP; 7301 7302 BuildMI(TrapBB, dl, TII->get(trap_opcode)); 7303 DispatchBB->addSuccessor(TrapBB); 7304 7305 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); 7306 DispatchBB->addSuccessor(DispContBB); 7307 7308 // Insert and MBBs. 7309 MF->insert(MF->end(), DispatchBB); 7310 MF->insert(MF->end(), DispContBB); 7311 MF->insert(MF->end(), TrapBB); 7312 7313 // Insert code into the entry block that creates and registers the function 7314 // context. 7315 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI); 7316 7317 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand( 7318 MachinePointerInfo::getFixedStack(*MF, FI), 7319 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4); 7320 7321 MachineInstrBuilder MIB; 7322 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup)); 7323 7324 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII); 7325 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo(); 7326 7327 // Add a register mask with no preserved registers. This results in all 7328 // registers being marked as clobbered. 7329 MIB.addRegMask(RI.getNoPreservedMask()); 7330 7331 unsigned NumLPads = LPadList.size(); 7332 if (Subtarget->isThumb2()) { 7333 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 7334 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1) 7335 .addFrameIndex(FI) 7336 .addImm(4) 7337 .addMemOperand(FIMMOLd)); 7338 7339 if (NumLPads < 256) { 7340 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri)) 7341 .addReg(NewVReg1) 7342 .addImm(LPadList.size())); 7343 } else { 7344 unsigned VReg1 = MRI->createVirtualRegister(TRC); 7345 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1) 7346 .addImm(NumLPads & 0xFFFF)); 7347 7348 unsigned VReg2 = VReg1; 7349 if ((NumLPads & 0xFFFF0000) != 0) { 7350 VReg2 = MRI->createVirtualRegister(TRC); 7351 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2) 7352 .addReg(VReg1) 7353 .addImm(NumLPads >> 16)); 7354 } 7355 7356 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr)) 7357 .addReg(NewVReg1) 7358 .addReg(VReg2)); 7359 } 7360 7361 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc)) 7362 .addMBB(TrapBB) 7363 .addImm(ARMCC::HI) 7364 .addReg(ARM::CPSR); 7365 7366 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 7367 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3) 7368 .addJumpTableIndex(MJTI)); 7369 7370 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 7371 AddDefaultCC( 7372 AddDefaultPred( 7373 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4) 7374 .addReg(NewVReg3, RegState::Kill) 7375 .addReg(NewVReg1) 7376 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 7377 7378 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT)) 7379 .addReg(NewVReg4, RegState::Kill) 7380 .addReg(NewVReg1) 7381 .addJumpTableIndex(MJTI); 7382 } else if (Subtarget->isThumb()) { 7383 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 7384 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1) 7385 .addFrameIndex(FI) 7386 .addImm(1) 7387 .addMemOperand(FIMMOLd)); 7388 7389 if (NumLPads < 256) { 7390 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8)) 7391 .addReg(NewVReg1) 7392 .addImm(NumLPads)); 7393 } else { 7394 MachineConstantPool *ConstantPool = MF->getConstantPool(); 7395 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 7396 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 7397 7398 // MachineConstantPool wants an explicit alignment. 7399 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 7400 if (Align == 0) 7401 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 7402 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 7403 7404 unsigned VReg1 = MRI->createVirtualRegister(TRC); 7405 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci)) 7406 .addReg(VReg1, RegState::Define) 7407 .addConstantPoolIndex(Idx)); 7408 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr)) 7409 .addReg(NewVReg1) 7410 .addReg(VReg1)); 7411 } 7412 7413 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc)) 7414 .addMBB(TrapBB) 7415 .addImm(ARMCC::HI) 7416 .addReg(ARM::CPSR); 7417 7418 unsigned NewVReg2 = MRI->createVirtualRegister(TRC); 7419 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2) 7420 .addReg(ARM::CPSR, RegState::Define) 7421 .addReg(NewVReg1) 7422 .addImm(2)); 7423 7424 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 7425 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3) 7426 .addJumpTableIndex(MJTI)); 7427 7428 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 7429 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4) 7430 .addReg(ARM::CPSR, RegState::Define) 7431 .addReg(NewVReg2, RegState::Kill) 7432 .addReg(NewVReg3)); 7433 7434 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand( 7435 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4); 7436 7437 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 7438 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5) 7439 .addReg(NewVReg4, RegState::Kill) 7440 .addImm(0) 7441 .addMemOperand(JTMMOLd)); 7442 7443 unsigned NewVReg6 = NewVReg5; 7444 if (RelocM == Reloc::PIC_) { 7445 NewVReg6 = MRI->createVirtualRegister(TRC); 7446 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6) 7447 .addReg(ARM::CPSR, RegState::Define) 7448 .addReg(NewVReg5, RegState::Kill) 7449 .addReg(NewVReg3)); 7450 } 7451 7452 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr)) 7453 .addReg(NewVReg6, RegState::Kill) 7454 .addJumpTableIndex(MJTI); 7455 } else { 7456 unsigned NewVReg1 = MRI->createVirtualRegister(TRC); 7457 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1) 7458 .addFrameIndex(FI) 7459 .addImm(4) 7460 .addMemOperand(FIMMOLd)); 7461 7462 if (NumLPads < 256) { 7463 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri)) 7464 .addReg(NewVReg1) 7465 .addImm(NumLPads)); 7466 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) { 7467 unsigned VReg1 = MRI->createVirtualRegister(TRC); 7468 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1) 7469 .addImm(NumLPads & 0xFFFF)); 7470 7471 unsigned VReg2 = VReg1; 7472 if ((NumLPads & 0xFFFF0000) != 0) { 7473 VReg2 = MRI->createVirtualRegister(TRC); 7474 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2) 7475 .addReg(VReg1) 7476 .addImm(NumLPads >> 16)); 7477 } 7478 7479 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 7480 .addReg(NewVReg1) 7481 .addReg(VReg2)); 7482 } else { 7483 MachineConstantPool *ConstantPool = MF->getConstantPool(); 7484 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 7485 const Constant *C = ConstantInt::get(Int32Ty, NumLPads); 7486 7487 // MachineConstantPool wants an explicit alignment. 7488 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 7489 if (Align == 0) 7490 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 7491 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 7492 7493 unsigned VReg1 = MRI->createVirtualRegister(TRC); 7494 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp)) 7495 .addReg(VReg1, RegState::Define) 7496 .addConstantPoolIndex(Idx) 7497 .addImm(0)); 7498 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr)) 7499 .addReg(NewVReg1) 7500 .addReg(VReg1, RegState::Kill)); 7501 } 7502 7503 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc)) 7504 .addMBB(TrapBB) 7505 .addImm(ARMCC::HI) 7506 .addReg(ARM::CPSR); 7507 7508 unsigned NewVReg3 = MRI->createVirtualRegister(TRC); 7509 AddDefaultCC( 7510 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3) 7511 .addReg(NewVReg1) 7512 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2)))); 7513 unsigned NewVReg4 = MRI->createVirtualRegister(TRC); 7514 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4) 7515 .addJumpTableIndex(MJTI)); 7516 7517 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand( 7518 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4); 7519 unsigned NewVReg5 = MRI->createVirtualRegister(TRC); 7520 AddDefaultPred( 7521 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5) 7522 .addReg(NewVReg3, RegState::Kill) 7523 .addReg(NewVReg4) 7524 .addImm(0) 7525 .addMemOperand(JTMMOLd)); 7526 7527 if (RelocM == Reloc::PIC_) { 7528 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd)) 7529 .addReg(NewVReg5, RegState::Kill) 7530 .addReg(NewVReg4) 7531 .addJumpTableIndex(MJTI); 7532 } else { 7533 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr)) 7534 .addReg(NewVReg5, RegState::Kill) 7535 .addJumpTableIndex(MJTI); 7536 } 7537 } 7538 7539 // Add the jump table entries as successors to the MBB. 7540 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs; 7541 for (std::vector<MachineBasicBlock*>::iterator 7542 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) { 7543 MachineBasicBlock *CurMBB = *I; 7544 if (SeenMBBs.insert(CurMBB).second) 7545 DispContBB->addSuccessor(CurMBB); 7546 } 7547 7548 // N.B. the order the invoke BBs are processed in doesn't matter here. 7549 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF); 7550 SmallVector<MachineBasicBlock*, 64> MBBLPads; 7551 for (MachineBasicBlock *BB : InvokeBBs) { 7552 7553 // Remove the landing pad successor from the invoke block and replace it 7554 // with the new dispatch block. 7555 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(), 7556 BB->succ_end()); 7557 while (!Successors.empty()) { 7558 MachineBasicBlock *SMBB = Successors.pop_back_val(); 7559 if (SMBB->isEHPad()) { 7560 BB->removeSuccessor(SMBB); 7561 MBBLPads.push_back(SMBB); 7562 } 7563 } 7564 7565 BB->addSuccessor(DispatchBB, BranchProbability::getZero()); 7566 BB->normalizeSuccProbs(); 7567 7568 // Find the invoke call and mark all of the callee-saved registers as 7569 // 'implicit defined' so that they're spilled. This prevents code from 7570 // moving instructions to before the EH block, where they will never be 7571 // executed. 7572 for (MachineBasicBlock::reverse_iterator 7573 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) { 7574 if (!II->isCall()) continue; 7575 7576 DenseMap<unsigned, bool> DefRegs; 7577 for (MachineInstr::mop_iterator 7578 OI = II->operands_begin(), OE = II->operands_end(); 7579 OI != OE; ++OI) { 7580 if (!OI->isReg()) continue; 7581 DefRegs[OI->getReg()] = true; 7582 } 7583 7584 MachineInstrBuilder MIB(*MF, &*II); 7585 7586 for (unsigned i = 0; SavedRegs[i] != 0; ++i) { 7587 unsigned Reg = SavedRegs[i]; 7588 if (Subtarget->isThumb2() && 7589 !ARM::tGPRRegClass.contains(Reg) && 7590 !ARM::hGPRRegClass.contains(Reg)) 7591 continue; 7592 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg)) 7593 continue; 7594 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg)) 7595 continue; 7596 if (!DefRegs[Reg]) 7597 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); 7598 } 7599 7600 break; 7601 } 7602 } 7603 7604 // Mark all former landing pads as non-landing pads. The dispatch is the only 7605 // landing pad now. 7606 for (SmallVectorImpl<MachineBasicBlock*>::iterator 7607 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I) 7608 (*I)->setIsEHPad(false); 7609 7610 // The instruction is gone now. 7611 MI->eraseFromParent(); 7612 } 7613 7614 static 7615 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) { 7616 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 7617 E = MBB->succ_end(); I != E; ++I) 7618 if (*I != Succ) 7619 return *I; 7620 llvm_unreachable("Expecting a BB with two successors!"); 7621 } 7622 7623 /// Return the load opcode for a given load size. If load size >= 8, 7624 /// neon opcode will be returned. 7625 static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) { 7626 if (LdSize >= 8) 7627 return LdSize == 16 ? ARM::VLD1q32wb_fixed 7628 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0; 7629 if (IsThumb1) 7630 return LdSize == 4 ? ARM::tLDRi 7631 : LdSize == 2 ? ARM::tLDRHi 7632 : LdSize == 1 ? ARM::tLDRBi : 0; 7633 if (IsThumb2) 7634 return LdSize == 4 ? ARM::t2LDR_POST 7635 : LdSize == 2 ? ARM::t2LDRH_POST 7636 : LdSize == 1 ? ARM::t2LDRB_POST : 0; 7637 return LdSize == 4 ? ARM::LDR_POST_IMM 7638 : LdSize == 2 ? ARM::LDRH_POST 7639 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0; 7640 } 7641 7642 /// Return the store opcode for a given store size. If store size >= 8, 7643 /// neon opcode will be returned. 7644 static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) { 7645 if (StSize >= 8) 7646 return StSize == 16 ? ARM::VST1q32wb_fixed 7647 : StSize == 8 ? ARM::VST1d32wb_fixed : 0; 7648 if (IsThumb1) 7649 return StSize == 4 ? ARM::tSTRi 7650 : StSize == 2 ? ARM::tSTRHi 7651 : StSize == 1 ? ARM::tSTRBi : 0; 7652 if (IsThumb2) 7653 return StSize == 4 ? ARM::t2STR_POST 7654 : StSize == 2 ? ARM::t2STRH_POST 7655 : StSize == 1 ? ARM::t2STRB_POST : 0; 7656 return StSize == 4 ? ARM::STR_POST_IMM 7657 : StSize == 2 ? ARM::STRH_POST 7658 : StSize == 1 ? ARM::STRB_POST_IMM : 0; 7659 } 7660 7661 /// Emit a post-increment load operation with given size. The instructions 7662 /// will be added to BB at Pos. 7663 static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos, 7664 const TargetInstrInfo *TII, DebugLoc dl, 7665 unsigned LdSize, unsigned Data, unsigned AddrIn, 7666 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 7667 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2); 7668 assert(LdOpc != 0 && "Should have a load opcode"); 7669 if (LdSize >= 8) { 7670 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7671 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7672 .addImm(0)); 7673 } else if (IsThumb1) { 7674 // load + update AddrIn 7675 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7676 .addReg(AddrIn).addImm(0)); 7677 MachineInstrBuilder MIB = 7678 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut); 7679 MIB = AddDefaultT1CC(MIB); 7680 MIB.addReg(AddrIn).addImm(LdSize); 7681 AddDefaultPred(MIB); 7682 } else if (IsThumb2) { 7683 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7684 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7685 .addImm(LdSize)); 7686 } else { // arm 7687 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data) 7688 .addReg(AddrOut, RegState::Define).addReg(AddrIn) 7689 .addReg(0).addImm(LdSize)); 7690 } 7691 } 7692 7693 /// Emit a post-increment store operation with given size. The instructions 7694 /// will be added to BB at Pos. 7695 static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos, 7696 const TargetInstrInfo *TII, DebugLoc dl, 7697 unsigned StSize, unsigned Data, unsigned AddrIn, 7698 unsigned AddrOut, bool IsThumb1, bool IsThumb2) { 7699 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2); 7700 assert(StOpc != 0 && "Should have a store opcode"); 7701 if (StSize >= 8) { 7702 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7703 .addReg(AddrIn).addImm(0).addReg(Data)); 7704 } else if (IsThumb1) { 7705 // store + update AddrIn 7706 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data) 7707 .addReg(AddrIn).addImm(0)); 7708 MachineInstrBuilder MIB = 7709 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut); 7710 MIB = AddDefaultT1CC(MIB); 7711 MIB.addReg(AddrIn).addImm(StSize); 7712 AddDefaultPred(MIB); 7713 } else if (IsThumb2) { 7714 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7715 .addReg(Data).addReg(AddrIn).addImm(StSize)); 7716 } else { // arm 7717 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut) 7718 .addReg(Data).addReg(AddrIn).addReg(0) 7719 .addImm(StSize)); 7720 } 7721 } 7722 7723 MachineBasicBlock * 7724 ARMTargetLowering::EmitStructByval(MachineInstr *MI, 7725 MachineBasicBlock *BB) const { 7726 // This pseudo instruction has 3 operands: dst, src, size 7727 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold(). 7728 // Otherwise, we will generate unrolled scalar copies. 7729 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 7730 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 7731 MachineFunction::iterator It = ++BB->getIterator(); 7732 7733 unsigned dest = MI->getOperand(0).getReg(); 7734 unsigned src = MI->getOperand(1).getReg(); 7735 unsigned SizeVal = MI->getOperand(2).getImm(); 7736 unsigned Align = MI->getOperand(3).getImm(); 7737 DebugLoc dl = MI->getDebugLoc(); 7738 7739 MachineFunction *MF = BB->getParent(); 7740 MachineRegisterInfo &MRI = MF->getRegInfo(); 7741 unsigned UnitSize = 0; 7742 const TargetRegisterClass *TRC = nullptr; 7743 const TargetRegisterClass *VecTRC = nullptr; 7744 7745 bool IsThumb1 = Subtarget->isThumb1Only(); 7746 bool IsThumb2 = Subtarget->isThumb2(); 7747 7748 if (Align & 1) { 7749 UnitSize = 1; 7750 } else if (Align & 2) { 7751 UnitSize = 2; 7752 } else { 7753 // Check whether we can use NEON instructions. 7754 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) && 7755 Subtarget->hasNEON()) { 7756 if ((Align % 16 == 0) && SizeVal >= 16) 7757 UnitSize = 16; 7758 else if ((Align % 8 == 0) && SizeVal >= 8) 7759 UnitSize = 8; 7760 } 7761 // Can't use NEON instructions. 7762 if (UnitSize == 0) 7763 UnitSize = 4; 7764 } 7765 7766 // Select the correct opcode and register class for unit size load/store 7767 bool IsNeon = UnitSize >= 8; 7768 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass; 7769 if (IsNeon) 7770 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass 7771 : UnitSize == 8 ? &ARM::DPRRegClass 7772 : nullptr; 7773 7774 unsigned BytesLeft = SizeVal % UnitSize; 7775 unsigned LoopSize = SizeVal - BytesLeft; 7776 7777 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) { 7778 // Use LDR and STR to copy. 7779 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize) 7780 // [destOut] = STR_POST(scratch, destIn, UnitSize) 7781 unsigned srcIn = src; 7782 unsigned destIn = dest; 7783 for (unsigned i = 0; i < LoopSize; i+=UnitSize) { 7784 unsigned srcOut = MRI.createVirtualRegister(TRC); 7785 unsigned destOut = MRI.createVirtualRegister(TRC); 7786 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 7787 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut, 7788 IsThumb1, IsThumb2); 7789 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut, 7790 IsThumb1, IsThumb2); 7791 srcIn = srcOut; 7792 destIn = destOut; 7793 } 7794 7795 // Handle the leftover bytes with LDRB and STRB. 7796 // [scratch, srcOut] = LDRB_POST(srcIn, 1) 7797 // [destOut] = STRB_POST(scratch, destIn, 1) 7798 for (unsigned i = 0; i < BytesLeft; i++) { 7799 unsigned srcOut = MRI.createVirtualRegister(TRC); 7800 unsigned destOut = MRI.createVirtualRegister(TRC); 7801 unsigned scratch = MRI.createVirtualRegister(TRC); 7802 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut, 7803 IsThumb1, IsThumb2); 7804 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut, 7805 IsThumb1, IsThumb2); 7806 srcIn = srcOut; 7807 destIn = destOut; 7808 } 7809 MI->eraseFromParent(); // The instruction is gone now. 7810 return BB; 7811 } 7812 7813 // Expand the pseudo op to a loop. 7814 // thisMBB: 7815 // ... 7816 // movw varEnd, # --> with thumb2 7817 // movt varEnd, # 7818 // ldrcp varEnd, idx --> without thumb2 7819 // fallthrough --> loopMBB 7820 // loopMBB: 7821 // PHI varPhi, varEnd, varLoop 7822 // PHI srcPhi, src, srcLoop 7823 // PHI destPhi, dst, destLoop 7824 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 7825 // [destLoop] = STR_POST(scratch, destPhi, UnitSize) 7826 // subs varLoop, varPhi, #UnitSize 7827 // bne loopMBB 7828 // fallthrough --> exitMBB 7829 // exitMBB: 7830 // epilogue to handle left-over bytes 7831 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 7832 // [destOut] = STRB_POST(scratch, destLoop, 1) 7833 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB); 7834 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB); 7835 MF->insert(It, loopMBB); 7836 MF->insert(It, exitMBB); 7837 7838 // Transfer the remainder of BB and its successor edges to exitMBB. 7839 exitMBB->splice(exitMBB->begin(), BB, 7840 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 7841 exitMBB->transferSuccessorsAndUpdatePHIs(BB); 7842 7843 // Load an immediate to varEnd. 7844 unsigned varEnd = MRI.createVirtualRegister(TRC); 7845 if (Subtarget->useMovt(*MF)) { 7846 unsigned Vtmp = varEnd; 7847 if ((LoopSize & 0xFFFF0000) != 0) 7848 Vtmp = MRI.createVirtualRegister(TRC); 7849 AddDefaultPred(BuildMI(BB, dl, 7850 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16), 7851 Vtmp).addImm(LoopSize & 0xFFFF)); 7852 7853 if ((LoopSize & 0xFFFF0000) != 0) 7854 AddDefaultPred(BuildMI(BB, dl, 7855 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16), 7856 varEnd) 7857 .addReg(Vtmp) 7858 .addImm(LoopSize >> 16)); 7859 } else { 7860 MachineConstantPool *ConstantPool = MF->getConstantPool(); 7861 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext()); 7862 const Constant *C = ConstantInt::get(Int32Ty, LoopSize); 7863 7864 // MachineConstantPool wants an explicit alignment. 7865 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty); 7866 if (Align == 0) 7867 Align = MF->getDataLayout().getTypeAllocSize(C->getType()); 7868 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align); 7869 7870 if (IsThumb1) 7871 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg( 7872 varEnd, RegState::Define).addConstantPoolIndex(Idx)); 7873 else 7874 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg( 7875 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0)); 7876 } 7877 BB->addSuccessor(loopMBB); 7878 7879 // Generate the loop body: 7880 // varPhi = PHI(varLoop, varEnd) 7881 // srcPhi = PHI(srcLoop, src) 7882 // destPhi = PHI(destLoop, dst) 7883 MachineBasicBlock *entryBB = BB; 7884 BB = loopMBB; 7885 unsigned varLoop = MRI.createVirtualRegister(TRC); 7886 unsigned varPhi = MRI.createVirtualRegister(TRC); 7887 unsigned srcLoop = MRI.createVirtualRegister(TRC); 7888 unsigned srcPhi = MRI.createVirtualRegister(TRC); 7889 unsigned destLoop = MRI.createVirtualRegister(TRC); 7890 unsigned destPhi = MRI.createVirtualRegister(TRC); 7891 7892 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi) 7893 .addReg(varLoop).addMBB(loopMBB) 7894 .addReg(varEnd).addMBB(entryBB); 7895 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi) 7896 .addReg(srcLoop).addMBB(loopMBB) 7897 .addReg(src).addMBB(entryBB); 7898 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi) 7899 .addReg(destLoop).addMBB(loopMBB) 7900 .addReg(dest).addMBB(entryBB); 7901 7902 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize) 7903 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz) 7904 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC); 7905 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop, 7906 IsThumb1, IsThumb2); 7907 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop, 7908 IsThumb1, IsThumb2); 7909 7910 // Decrement loop variable by UnitSize. 7911 if (IsThumb1) { 7912 MachineInstrBuilder MIB = 7913 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop); 7914 MIB = AddDefaultT1CC(MIB); 7915 MIB.addReg(varPhi).addImm(UnitSize); 7916 AddDefaultPred(MIB); 7917 } else { 7918 MachineInstrBuilder MIB = 7919 BuildMI(*BB, BB->end(), dl, 7920 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop); 7921 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize))); 7922 MIB->getOperand(5).setReg(ARM::CPSR); 7923 MIB->getOperand(5).setIsDef(true); 7924 } 7925 BuildMI(*BB, BB->end(), dl, 7926 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc)) 7927 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR); 7928 7929 // loopMBB can loop back to loopMBB or fall through to exitMBB. 7930 BB->addSuccessor(loopMBB); 7931 BB->addSuccessor(exitMBB); 7932 7933 // Add epilogue to handle BytesLeft. 7934 BB = exitMBB; 7935 MachineInstr *StartOfExit = exitMBB->begin(); 7936 7937 // [scratch, srcOut] = LDRB_POST(srcLoop, 1) 7938 // [destOut] = STRB_POST(scratch, destLoop, 1) 7939 unsigned srcIn = srcLoop; 7940 unsigned destIn = destLoop; 7941 for (unsigned i = 0; i < BytesLeft; i++) { 7942 unsigned srcOut = MRI.createVirtualRegister(TRC); 7943 unsigned destOut = MRI.createVirtualRegister(TRC); 7944 unsigned scratch = MRI.createVirtualRegister(TRC); 7945 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut, 7946 IsThumb1, IsThumb2); 7947 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut, 7948 IsThumb1, IsThumb2); 7949 srcIn = srcOut; 7950 destIn = destOut; 7951 } 7952 7953 MI->eraseFromParent(); // The instruction is gone now. 7954 return BB; 7955 } 7956 7957 MachineBasicBlock * 7958 ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI, 7959 MachineBasicBlock *MBB) const { 7960 const TargetMachine &TM = getTargetMachine(); 7961 const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); 7962 DebugLoc DL = MI->getDebugLoc(); 7963 7964 assert(Subtarget->isTargetWindows() && 7965 "__chkstk is only supported on Windows"); 7966 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode"); 7967 7968 // __chkstk takes the number of words to allocate on the stack in R4, and 7969 // returns the stack adjustment in number of bytes in R4. This will not 7970 // clober any other registers (other than the obvious lr). 7971 // 7972 // Although, technically, IP should be considered a register which may be 7973 // clobbered, the call itself will not touch it. Windows on ARM is a pure 7974 // thumb-2 environment, so there is no interworking required. As a result, we 7975 // do not expect a veneer to be emitted by the linker, clobbering IP. 7976 // 7977 // Each module receives its own copy of __chkstk, so no import thunk is 7978 // required, again, ensuring that IP is not clobbered. 7979 // 7980 // Finally, although some linkers may theoretically provide a trampoline for 7981 // out of range calls (which is quite common due to a 32M range limitation of 7982 // branches for Thumb), we can generate the long-call version via 7983 // -mcmodel=large, alleviating the need for the trampoline which may clobber 7984 // IP. 7985 7986 switch (TM.getCodeModel()) { 7987 case CodeModel::Small: 7988 case CodeModel::Medium: 7989 case CodeModel::Default: 7990 case CodeModel::Kernel: 7991 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL)) 7992 .addImm((unsigned)ARMCC::AL).addReg(0) 7993 .addExternalSymbol("__chkstk") 7994 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 7995 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 7996 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead); 7997 break; 7998 case CodeModel::Large: 7999 case CodeModel::JITDefault: { 8000 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 8001 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass); 8002 8003 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg) 8004 .addExternalSymbol("__chkstk"); 8005 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr)) 8006 .addImm((unsigned)ARMCC::AL).addReg(0) 8007 .addReg(Reg, RegState::Kill) 8008 .addReg(ARM::R4, RegState::Implicit | RegState::Kill) 8009 .addReg(ARM::R4, RegState::Implicit | RegState::Define) 8010 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead); 8011 break; 8012 } 8013 } 8014 8015 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr), 8016 ARM::SP) 8017 .addReg(ARM::SP).addReg(ARM::R4))); 8018 8019 MI->eraseFromParent(); 8020 return MBB; 8021 } 8022 8023 MachineBasicBlock * 8024 ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI, 8025 MachineBasicBlock *MBB) const { 8026 DebugLoc DL = MI->getDebugLoc(); 8027 MachineFunction *MF = MBB->getParent(); 8028 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 8029 8030 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock(); 8031 MF->insert(++MBB->getIterator(), ContBB); 8032 ContBB->splice(ContBB->begin(), MBB, 8033 std::next(MachineBasicBlock::iterator(MI)), MBB->end()); 8034 ContBB->transferSuccessorsAndUpdatePHIs(MBB); 8035 8036 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); 8037 MF->push_back(TrapBB); 8038 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249); 8039 MBB->addSuccessor(TrapBB); 8040 8041 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ)) 8042 .addReg(MI->getOperand(0).getReg()) 8043 .addMBB(TrapBB); 8044 AddDefaultPred(BuildMI(*MBB, MI, DL, TII->get(ARM::t2B)).addMBB(ContBB)); 8045 MBB->addSuccessor(ContBB); 8046 8047 MI->eraseFromParent(); 8048 return ContBB; 8049 } 8050 8051 MachineBasicBlock * 8052 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 8053 MachineBasicBlock *BB) const { 8054 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 8055 DebugLoc dl = MI->getDebugLoc(); 8056 bool isThumb2 = Subtarget->isThumb2(); 8057 switch (MI->getOpcode()) { 8058 default: { 8059 MI->dump(); 8060 llvm_unreachable("Unexpected instr type to insert"); 8061 } 8062 // The Thumb2 pre-indexed stores have the same MI operands, they just 8063 // define them differently in the .td files from the isel patterns, so 8064 // they need pseudos. 8065 case ARM::t2STR_preidx: 8066 MI->setDesc(TII->get(ARM::t2STR_PRE)); 8067 return BB; 8068 case ARM::t2STRB_preidx: 8069 MI->setDesc(TII->get(ARM::t2STRB_PRE)); 8070 return BB; 8071 case ARM::t2STRH_preidx: 8072 MI->setDesc(TII->get(ARM::t2STRH_PRE)); 8073 return BB; 8074 8075 case ARM::STRi_preidx: 8076 case ARM::STRBi_preidx: { 8077 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ? 8078 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM; 8079 // Decode the offset. 8080 unsigned Offset = MI->getOperand(4).getImm(); 8081 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub; 8082 Offset = ARM_AM::getAM2Offset(Offset); 8083 if (isSub) 8084 Offset = -Offset; 8085 8086 MachineMemOperand *MMO = *MI->memoperands_begin(); 8087 BuildMI(*BB, MI, dl, TII->get(NewOpc)) 8088 .addOperand(MI->getOperand(0)) // Rn_wb 8089 .addOperand(MI->getOperand(1)) // Rt 8090 .addOperand(MI->getOperand(2)) // Rn 8091 .addImm(Offset) // offset (skip GPR==zero_reg) 8092 .addOperand(MI->getOperand(5)) // pred 8093 .addOperand(MI->getOperand(6)) 8094 .addMemOperand(MMO); 8095 MI->eraseFromParent(); 8096 return BB; 8097 } 8098 case ARM::STRr_preidx: 8099 case ARM::STRBr_preidx: 8100 case ARM::STRH_preidx: { 8101 unsigned NewOpc; 8102 switch (MI->getOpcode()) { 8103 default: llvm_unreachable("unexpected opcode!"); 8104 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break; 8105 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break; 8106 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break; 8107 } 8108 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc)); 8109 for (unsigned i = 0; i < MI->getNumOperands(); ++i) 8110 MIB.addOperand(MI->getOperand(i)); 8111 MI->eraseFromParent(); 8112 return BB; 8113 } 8114 8115 case ARM::tMOVCCr_pseudo: { 8116 // To "insert" a SELECT_CC instruction, we actually have to insert the 8117 // diamond control-flow pattern. The incoming instruction knows the 8118 // destination vreg to set, the condition code register to branch on, the 8119 // true/false values to select between, and a branch opcode to use. 8120 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 8121 MachineFunction::iterator It = ++BB->getIterator(); 8122 8123 // thisMBB: 8124 // ... 8125 // TrueVal = ... 8126 // cmpTY ccX, r1, r2 8127 // bCC copy1MBB 8128 // fallthrough --> copy0MBB 8129 MachineBasicBlock *thisMBB = BB; 8130 MachineFunction *F = BB->getParent(); 8131 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 8132 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 8133 F->insert(It, copy0MBB); 8134 F->insert(It, sinkMBB); 8135 8136 // Transfer the remainder of BB and its successor edges to sinkMBB. 8137 sinkMBB->splice(sinkMBB->begin(), BB, 8138 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8139 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 8140 8141 BB->addSuccessor(copy0MBB); 8142 BB->addSuccessor(sinkMBB); 8143 8144 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB) 8145 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg()); 8146 8147 // copy0MBB: 8148 // %FalseValue = ... 8149 // # fallthrough to sinkMBB 8150 BB = copy0MBB; 8151 8152 // Update machine-CFG edges 8153 BB->addSuccessor(sinkMBB); 8154 8155 // sinkMBB: 8156 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 8157 // ... 8158 BB = sinkMBB; 8159 BuildMI(*BB, BB->begin(), dl, 8160 TII->get(ARM::PHI), MI->getOperand(0).getReg()) 8161 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) 8162 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); 8163 8164 MI->eraseFromParent(); // The pseudo instruction is gone now. 8165 return BB; 8166 } 8167 8168 case ARM::BCCi64: 8169 case ARM::BCCZi64: { 8170 // If there is an unconditional branch to the other successor, remove it. 8171 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8172 8173 // Compare both parts that make up the double comparison separately for 8174 // equality. 8175 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64; 8176 8177 unsigned LHS1 = MI->getOperand(1).getReg(); 8178 unsigned LHS2 = MI->getOperand(2).getReg(); 8179 if (RHSisZero) { 8180 AddDefaultPred(BuildMI(BB, dl, 8181 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 8182 .addReg(LHS1).addImm(0)); 8183 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 8184 .addReg(LHS2).addImm(0) 8185 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 8186 } else { 8187 unsigned RHS1 = MI->getOperand(3).getReg(); 8188 unsigned RHS2 = MI->getOperand(4).getReg(); 8189 AddDefaultPred(BuildMI(BB, dl, 8190 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 8191 .addReg(LHS1).addReg(RHS1)); 8192 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr)) 8193 .addReg(LHS2).addReg(RHS2) 8194 .addImm(ARMCC::EQ).addReg(ARM::CPSR); 8195 } 8196 8197 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB(); 8198 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB); 8199 if (MI->getOperand(0).getImm() == ARMCC::NE) 8200 std::swap(destMBB, exitMBB); 8201 8202 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)) 8203 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR); 8204 if (isThumb2) 8205 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB)); 8206 else 8207 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB); 8208 8209 MI->eraseFromParent(); // The pseudo instruction is gone now. 8210 return BB; 8211 } 8212 8213 case ARM::Int_eh_sjlj_setjmp: 8214 case ARM::Int_eh_sjlj_setjmp_nofp: 8215 case ARM::tInt_eh_sjlj_setjmp: 8216 case ARM::t2Int_eh_sjlj_setjmp: 8217 case ARM::t2Int_eh_sjlj_setjmp_nofp: 8218 return BB; 8219 8220 case ARM::Int_eh_sjlj_setup_dispatch: 8221 EmitSjLjDispatchBlock(MI, BB); 8222 return BB; 8223 8224 case ARM::ABS: 8225 case ARM::t2ABS: { 8226 // To insert an ABS instruction, we have to insert the 8227 // diamond control-flow pattern. The incoming instruction knows the 8228 // source vreg to test against 0, the destination vreg to set, 8229 // the condition code register to branch on, the 8230 // true/false values to select between, and a branch opcode to use. 8231 // It transforms 8232 // V1 = ABS V0 8233 // into 8234 // V2 = MOVS V0 8235 // BCC (branch to SinkBB if V0 >= 0) 8236 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0) 8237 // SinkBB: V1 = PHI(V2, V3) 8238 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 8239 MachineFunction::iterator BBI = ++BB->getIterator(); 8240 MachineFunction *Fn = BB->getParent(); 8241 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB); 8242 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB); 8243 Fn->insert(BBI, RSBBB); 8244 Fn->insert(BBI, SinkBB); 8245 8246 unsigned int ABSSrcReg = MI->getOperand(1).getReg(); 8247 unsigned int ABSDstReg = MI->getOperand(0).getReg(); 8248 bool ABSSrcKIll = MI->getOperand(1).isKill(); 8249 bool isThumb2 = Subtarget->isThumb2(); 8250 MachineRegisterInfo &MRI = Fn->getRegInfo(); 8251 // In Thumb mode S must not be specified if source register is the SP or 8252 // PC and if destination register is the SP, so restrict register class 8253 unsigned NewRsbDstReg = 8254 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass); 8255 8256 // Transfer the remainder of BB and its successor edges to sinkMBB. 8257 SinkBB->splice(SinkBB->begin(), BB, 8258 std::next(MachineBasicBlock::iterator(MI)), BB->end()); 8259 SinkBB->transferSuccessorsAndUpdatePHIs(BB); 8260 8261 BB->addSuccessor(RSBBB); 8262 BB->addSuccessor(SinkBB); 8263 8264 // fall through to SinkMBB 8265 RSBBB->addSuccessor(SinkBB); 8266 8267 // insert a cmp at the end of BB 8268 AddDefaultPred(BuildMI(BB, dl, 8269 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri)) 8270 .addReg(ABSSrcReg).addImm(0)); 8271 8272 // insert a bcc with opposite CC to ARMCC::MI at the end of BB 8273 BuildMI(BB, dl, 8274 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB) 8275 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR); 8276 8277 // insert rsbri in RSBBB 8278 // Note: BCC and rsbri will be converted into predicated rsbmi 8279 // by if-conversion pass 8280 BuildMI(*RSBBB, RSBBB->begin(), dl, 8281 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg) 8282 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0) 8283 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 8284 8285 // insert PHI in SinkBB, 8286 // reuse ABSDstReg to not change uses of ABS instruction 8287 BuildMI(*SinkBB, SinkBB->begin(), dl, 8288 TII->get(ARM::PHI), ABSDstReg) 8289 .addReg(NewRsbDstReg).addMBB(RSBBB) 8290 .addReg(ABSSrcReg).addMBB(BB); 8291 8292 // remove ABS instruction 8293 MI->eraseFromParent(); 8294 8295 // return last added BB 8296 return SinkBB; 8297 } 8298 case ARM::COPY_STRUCT_BYVAL_I32: 8299 ++NumLoopByVals; 8300 return EmitStructByval(MI, BB); 8301 case ARM::WIN__CHKSTK: 8302 return EmitLowered__chkstk(MI, BB); 8303 case ARM::WIN__DBZCHK: 8304 return EmitLowered__dbzchk(MI, BB); 8305 } 8306 } 8307 8308 /// \brief Attaches vregs to MEMCPY that it will use as scratch registers 8309 /// when it is expanded into LDM/STM. This is done as a post-isel lowering 8310 /// instead of as a custom inserter because we need the use list from the SDNode. 8311 static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget, 8312 MachineInstr *MI, const SDNode *Node) { 8313 bool isThumb1 = Subtarget->isThumb1Only(); 8314 8315 DebugLoc DL = MI->getDebugLoc(); 8316 MachineFunction *MF = MI->getParent()->getParent(); 8317 MachineRegisterInfo &MRI = MF->getRegInfo(); 8318 MachineInstrBuilder MIB(*MF, MI); 8319 8320 // If the new dst/src is unused mark it as dead. 8321 if (!Node->hasAnyUseOfValue(0)) { 8322 MI->getOperand(0).setIsDead(true); 8323 } 8324 if (!Node->hasAnyUseOfValue(1)) { 8325 MI->getOperand(1).setIsDead(true); 8326 } 8327 8328 // The MEMCPY both defines and kills the scratch registers. 8329 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) { 8330 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass 8331 : &ARM::GPRRegClass); 8332 MIB.addReg(TmpReg, RegState::Define|RegState::Dead); 8333 } 8334 } 8335 8336 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, 8337 SDNode *Node) const { 8338 if (MI->getOpcode() == ARM::MEMCPY) { 8339 attachMEMCPYScratchRegs(Subtarget, MI, Node); 8340 return; 8341 } 8342 8343 const MCInstrDesc *MCID = &MI->getDesc(); 8344 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB, 8345 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional 8346 // operand is still set to noreg. If needed, set the optional operand's 8347 // register to CPSR, and remove the redundant implicit def. 8348 // 8349 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>). 8350 8351 // Rename pseudo opcodes. 8352 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode()); 8353 if (NewOpc) { 8354 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo(); 8355 MCID = &TII->get(NewOpc); 8356 8357 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 && 8358 "converted opcode should be the same except for cc_out"); 8359 8360 MI->setDesc(*MCID); 8361 8362 // Add the optional cc_out operand 8363 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true)); 8364 } 8365 unsigned ccOutIdx = MCID->getNumOperands() - 1; 8366 8367 // Any ARM instruction that sets the 's' bit should specify an optional 8368 // "cc_out" operand in the last operand position. 8369 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) { 8370 assert(!NewOpc && "Optional cc_out operand required"); 8371 return; 8372 } 8373 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it 8374 // since we already have an optional CPSR def. 8375 bool definesCPSR = false; 8376 bool deadCPSR = false; 8377 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands(); 8378 i != e; ++i) { 8379 const MachineOperand &MO = MI->getOperand(i); 8380 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) { 8381 definesCPSR = true; 8382 if (MO.isDead()) 8383 deadCPSR = true; 8384 MI->RemoveOperand(i); 8385 break; 8386 } 8387 } 8388 if (!definesCPSR) { 8389 assert(!NewOpc && "Optional cc_out operand required"); 8390 return; 8391 } 8392 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag"); 8393 if (deadCPSR) { 8394 assert(!MI->getOperand(ccOutIdx).getReg() && 8395 "expect uninitialized optional cc_out operand"); 8396 return; 8397 } 8398 8399 // If this instruction was defined with an optional CPSR def and its dag node 8400 // had a live implicit CPSR def, then activate the optional CPSR def. 8401 MachineOperand &MO = MI->getOperand(ccOutIdx); 8402 MO.setReg(ARM::CPSR); 8403 MO.setIsDef(true); 8404 } 8405 8406 //===----------------------------------------------------------------------===// 8407 // ARM Optimization Hooks 8408 //===----------------------------------------------------------------------===// 8409 8410 // Helper function that checks if N is a null or all ones constant. 8411 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) { 8412 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N); 8413 } 8414 8415 // Return true if N is conditionally 0 or all ones. 8416 // Detects these expressions where cc is an i1 value: 8417 // 8418 // (select cc 0, y) [AllOnes=0] 8419 // (select cc y, 0) [AllOnes=0] 8420 // (zext cc) [AllOnes=0] 8421 // (sext cc) [AllOnes=0/1] 8422 // (select cc -1, y) [AllOnes=1] 8423 // (select cc y, -1) [AllOnes=1] 8424 // 8425 // Invert is set when N is the null/all ones constant when CC is false. 8426 // OtherOp is set to the alternative value of N. 8427 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes, 8428 SDValue &CC, bool &Invert, 8429 SDValue &OtherOp, 8430 SelectionDAG &DAG) { 8431 switch (N->getOpcode()) { 8432 default: return false; 8433 case ISD::SELECT: { 8434 CC = N->getOperand(0); 8435 SDValue N1 = N->getOperand(1); 8436 SDValue N2 = N->getOperand(2); 8437 if (isZeroOrAllOnes(N1, AllOnes)) { 8438 Invert = false; 8439 OtherOp = N2; 8440 return true; 8441 } 8442 if (isZeroOrAllOnes(N2, AllOnes)) { 8443 Invert = true; 8444 OtherOp = N1; 8445 return true; 8446 } 8447 return false; 8448 } 8449 case ISD::ZERO_EXTEND: 8450 // (zext cc) can never be the all ones value. 8451 if (AllOnes) 8452 return false; 8453 // Fall through. 8454 case ISD::SIGN_EXTEND: { 8455 SDLoc dl(N); 8456 EVT VT = N->getValueType(0); 8457 CC = N->getOperand(0); 8458 if (CC.getValueType() != MVT::i1) 8459 return false; 8460 Invert = !AllOnes; 8461 if (AllOnes) 8462 // When looking for an AllOnes constant, N is an sext, and the 'other' 8463 // value is 0. 8464 OtherOp = DAG.getConstant(0, dl, VT); 8465 else if (N->getOpcode() == ISD::ZERO_EXTEND) 8466 // When looking for a 0 constant, N can be zext or sext. 8467 OtherOp = DAG.getConstant(1, dl, VT); 8468 else 8469 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, 8470 VT); 8471 return true; 8472 } 8473 } 8474 } 8475 8476 // Combine a constant select operand into its use: 8477 // 8478 // (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 8479 // (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 8480 // (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1] 8481 // (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 8482 // (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 8483 // 8484 // The transform is rejected if the select doesn't have a constant operand that 8485 // is null, or all ones when AllOnes is set. 8486 // 8487 // Also recognize sext/zext from i1: 8488 // 8489 // (add (zext cc), x) -> (select cc (add x, 1), x) 8490 // (add (sext cc), x) -> (select cc (add x, -1), x) 8491 // 8492 // These transformations eventually create predicated instructions. 8493 // 8494 // @param N The node to transform. 8495 // @param Slct The N operand that is a select. 8496 // @param OtherOp The other N operand (x above). 8497 // @param DCI Context. 8498 // @param AllOnes Require the select constant to be all ones instead of null. 8499 // @returns The new node, or SDValue() on failure. 8500 static 8501 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp, 8502 TargetLowering::DAGCombinerInfo &DCI, 8503 bool AllOnes = false) { 8504 SelectionDAG &DAG = DCI.DAG; 8505 EVT VT = N->getValueType(0); 8506 SDValue NonConstantVal; 8507 SDValue CCOp; 8508 bool SwapSelectOps; 8509 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps, 8510 NonConstantVal, DAG)) 8511 return SDValue(); 8512 8513 // Slct is now know to be the desired identity constant when CC is true. 8514 SDValue TrueVal = OtherOp; 8515 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT, 8516 OtherOp, NonConstantVal); 8517 // Unless SwapSelectOps says CC should be false. 8518 if (SwapSelectOps) 8519 std::swap(TrueVal, FalseVal); 8520 8521 return DAG.getNode(ISD::SELECT, SDLoc(N), VT, 8522 CCOp, TrueVal, FalseVal); 8523 } 8524 8525 // Attempt combineSelectAndUse on each operand of a commutative operator N. 8526 static 8527 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes, 8528 TargetLowering::DAGCombinerInfo &DCI) { 8529 SDValue N0 = N->getOperand(0); 8530 SDValue N1 = N->getOperand(1); 8531 if (N0.getNode()->hasOneUse()) 8532 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes)) 8533 return Result; 8534 if (N1.getNode()->hasOneUse()) 8535 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes)) 8536 return Result; 8537 return SDValue(); 8538 } 8539 8540 // AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction 8541 // (only after legalization). 8542 static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1, 8543 TargetLowering::DAGCombinerInfo &DCI, 8544 const ARMSubtarget *Subtarget) { 8545 8546 // Only perform optimization if after legalize, and if NEON is available. We 8547 // also expected both operands to be BUILD_VECTORs. 8548 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON() 8549 || N0.getOpcode() != ISD::BUILD_VECTOR 8550 || N1.getOpcode() != ISD::BUILD_VECTOR) 8551 return SDValue(); 8552 8553 // Check output type since VPADDL operand elements can only be 8, 16, or 32. 8554 EVT VT = N->getValueType(0); 8555 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64) 8556 return SDValue(); 8557 8558 // Check that the vector operands are of the right form. 8559 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR 8560 // operands, where N is the size of the formed vector. 8561 // Each EXTRACT_VECTOR should have the same input vector and odd or even 8562 // index such that we have a pair wise add pattern. 8563 8564 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing. 8565 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT) 8566 return SDValue(); 8567 SDValue Vec = N0->getOperand(0)->getOperand(0); 8568 SDNode *V = Vec.getNode(); 8569 unsigned nextIndex = 0; 8570 8571 // For each operands to the ADD which are BUILD_VECTORs, 8572 // check to see if each of their operands are an EXTRACT_VECTOR with 8573 // the same vector and appropriate index. 8574 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) { 8575 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT 8576 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 8577 8578 SDValue ExtVec0 = N0->getOperand(i); 8579 SDValue ExtVec1 = N1->getOperand(i); 8580 8581 // First operand is the vector, verify its the same. 8582 if (V != ExtVec0->getOperand(0).getNode() || 8583 V != ExtVec1->getOperand(0).getNode()) 8584 return SDValue(); 8585 8586 // Second is the constant, verify its correct. 8587 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1)); 8588 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1)); 8589 8590 // For the constant, we want to see all the even or all the odd. 8591 if (!C0 || !C1 || C0->getZExtValue() != nextIndex 8592 || C1->getZExtValue() != nextIndex+1) 8593 return SDValue(); 8594 8595 // Increment index. 8596 nextIndex+=2; 8597 } else 8598 return SDValue(); 8599 } 8600 8601 // Create VPADDL node. 8602 SelectionDAG &DAG = DCI.DAG; 8603 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8604 8605 SDLoc dl(N); 8606 8607 // Build operand list. 8608 SmallVector<SDValue, 8> Ops; 8609 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl, 8610 TLI.getPointerTy(DAG.getDataLayout()))); 8611 8612 // Input is the vector. 8613 Ops.push_back(Vec); 8614 8615 // Get widened type and narrowed type. 8616 MVT widenType; 8617 unsigned numElem = VT.getVectorNumElements(); 8618 8619 EVT inputLaneType = Vec.getValueType().getVectorElementType(); 8620 switch (inputLaneType.getSimpleVT().SimpleTy) { 8621 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break; 8622 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break; 8623 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break; 8624 default: 8625 llvm_unreachable("Invalid vector element type for padd optimization."); 8626 } 8627 8628 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops); 8629 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE; 8630 return DAG.getNode(ExtOp, dl, VT, tmp); 8631 } 8632 8633 static SDValue findMUL_LOHI(SDValue V) { 8634 if (V->getOpcode() == ISD::UMUL_LOHI || 8635 V->getOpcode() == ISD::SMUL_LOHI) 8636 return V; 8637 return SDValue(); 8638 } 8639 8640 static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode, 8641 TargetLowering::DAGCombinerInfo &DCI, 8642 const ARMSubtarget *Subtarget) { 8643 8644 if (Subtarget->isThumb1Only()) return SDValue(); 8645 8646 // Only perform the checks after legalize when the pattern is available. 8647 if (DCI.isBeforeLegalize()) return SDValue(); 8648 8649 // Look for multiply add opportunities. 8650 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where 8651 // each add nodes consumes a value from ISD::UMUL_LOHI and there is 8652 // a glue link from the first add to the second add. 8653 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by 8654 // a S/UMLAL instruction. 8655 // UMUL_LOHI 8656 // / :lo \ :hi 8657 // / \ [no multiline comment] 8658 // loAdd -> ADDE | 8659 // \ :glue / 8660 // \ / 8661 // ADDC <- hiAdd 8662 // 8663 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC"); 8664 SDValue AddcOp0 = AddcNode->getOperand(0); 8665 SDValue AddcOp1 = AddcNode->getOperand(1); 8666 8667 // Check if the two operands are from the same mul_lohi node. 8668 if (AddcOp0.getNode() == AddcOp1.getNode()) 8669 return SDValue(); 8670 8671 assert(AddcNode->getNumValues() == 2 && 8672 AddcNode->getValueType(0) == MVT::i32 && 8673 "Expect ADDC with two result values. First: i32"); 8674 8675 // Check that we have a glued ADDC node. 8676 if (AddcNode->getValueType(1) != MVT::Glue) 8677 return SDValue(); 8678 8679 // Check that the ADDC adds the low result of the S/UMUL_LOHI. 8680 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI && 8681 AddcOp0->getOpcode() != ISD::SMUL_LOHI && 8682 AddcOp1->getOpcode() != ISD::UMUL_LOHI && 8683 AddcOp1->getOpcode() != ISD::SMUL_LOHI) 8684 return SDValue(); 8685 8686 // Look for the glued ADDE. 8687 SDNode* AddeNode = AddcNode->getGluedUser(); 8688 if (!AddeNode) 8689 return SDValue(); 8690 8691 // Make sure it is really an ADDE. 8692 if (AddeNode->getOpcode() != ISD::ADDE) 8693 return SDValue(); 8694 8695 assert(AddeNode->getNumOperands() == 3 && 8696 AddeNode->getOperand(2).getValueType() == MVT::Glue && 8697 "ADDE node has the wrong inputs"); 8698 8699 // Check for the triangle shape. 8700 SDValue AddeOp0 = AddeNode->getOperand(0); 8701 SDValue AddeOp1 = AddeNode->getOperand(1); 8702 8703 // Make sure that the ADDE operands are not coming from the same node. 8704 if (AddeOp0.getNode() == AddeOp1.getNode()) 8705 return SDValue(); 8706 8707 // Find the MUL_LOHI node walking up ADDE's operands. 8708 bool IsLeftOperandMUL = false; 8709 SDValue MULOp = findMUL_LOHI(AddeOp0); 8710 if (MULOp == SDValue()) 8711 MULOp = findMUL_LOHI(AddeOp1); 8712 else 8713 IsLeftOperandMUL = true; 8714 if (MULOp == SDValue()) 8715 return SDValue(); 8716 8717 // Figure out the right opcode. 8718 unsigned Opc = MULOp->getOpcode(); 8719 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL; 8720 8721 // Figure out the high and low input values to the MLAL node. 8722 SDValue* HiAdd = nullptr; 8723 SDValue* LoMul = nullptr; 8724 SDValue* LowAdd = nullptr; 8725 8726 // Ensure that ADDE is from high result of ISD::SMUL_LOHI. 8727 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1))) 8728 return SDValue(); 8729 8730 if (IsLeftOperandMUL) 8731 HiAdd = &AddeOp1; 8732 else 8733 HiAdd = &AddeOp0; 8734 8735 8736 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node 8737 // whose low result is fed to the ADDC we are checking. 8738 8739 if (AddcOp0 == MULOp.getValue(0)) { 8740 LoMul = &AddcOp0; 8741 LowAdd = &AddcOp1; 8742 } 8743 if (AddcOp1 == MULOp.getValue(0)) { 8744 LoMul = &AddcOp1; 8745 LowAdd = &AddcOp0; 8746 } 8747 8748 if (!LoMul) 8749 return SDValue(); 8750 8751 // Create the merged node. 8752 SelectionDAG &DAG = DCI.DAG; 8753 8754 // Build operand list. 8755 SmallVector<SDValue, 8> Ops; 8756 Ops.push_back(LoMul->getOperand(0)); 8757 Ops.push_back(LoMul->getOperand(1)); 8758 Ops.push_back(*LowAdd); 8759 Ops.push_back(*HiAdd); 8760 8761 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode), 8762 DAG.getVTList(MVT::i32, MVT::i32), Ops); 8763 8764 // Replace the ADDs' nodes uses by the MLA node's values. 8765 SDValue HiMLALResult(MLALNode.getNode(), 1); 8766 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult); 8767 8768 SDValue LoMLALResult(MLALNode.getNode(), 0); 8769 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult); 8770 8771 // Return original node to notify the driver to stop replacing. 8772 SDValue resNode(AddcNode, 0); 8773 return resNode; 8774 } 8775 8776 /// PerformADDCCombine - Target-specific dag combine transform from 8777 /// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL. 8778 static SDValue PerformADDCCombine(SDNode *N, 8779 TargetLowering::DAGCombinerInfo &DCI, 8780 const ARMSubtarget *Subtarget) { 8781 8782 return AddCombineTo64bitMLAL(N, DCI, Subtarget); 8783 8784 } 8785 8786 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with 8787 /// operands N0 and N1. This is a helper for PerformADDCombine that is 8788 /// called with the default operands, and if that fails, with commuted 8789 /// operands. 8790 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, 8791 TargetLowering::DAGCombinerInfo &DCI, 8792 const ARMSubtarget *Subtarget){ 8793 8794 // Attempt to create vpaddl for this add. 8795 if (SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget)) 8796 return Result; 8797 8798 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c)) 8799 if (N0.getNode()->hasOneUse()) 8800 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI)) 8801 return Result; 8802 return SDValue(); 8803 } 8804 8805 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD. 8806 /// 8807 static SDValue PerformADDCombine(SDNode *N, 8808 TargetLowering::DAGCombinerInfo &DCI, 8809 const ARMSubtarget *Subtarget) { 8810 SDValue N0 = N->getOperand(0); 8811 SDValue N1 = N->getOperand(1); 8812 8813 // First try with the default operand order. 8814 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget)) 8815 return Result; 8816 8817 // If that didn't work, try again with the operands commuted. 8818 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget); 8819 } 8820 8821 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB. 8822 /// 8823 static SDValue PerformSUBCombine(SDNode *N, 8824 TargetLowering::DAGCombinerInfo &DCI) { 8825 SDValue N0 = N->getOperand(0); 8826 SDValue N1 = N->getOperand(1); 8827 8828 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c)) 8829 if (N1.getNode()->hasOneUse()) 8830 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI)) 8831 return Result; 8832 8833 return SDValue(); 8834 } 8835 8836 /// PerformVMULCombine 8837 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the 8838 /// special multiplier accumulator forwarding. 8839 /// vmul d3, d0, d2 8840 /// vmla d3, d1, d2 8841 /// is faster than 8842 /// vadd d3, d0, d1 8843 /// vmul d3, d3, d2 8844 // However, for (A + B) * (A + B), 8845 // vadd d2, d0, d1 8846 // vmul d3, d0, d2 8847 // vmla d3, d1, d2 8848 // is slower than 8849 // vadd d2, d0, d1 8850 // vmul d3, d2, d2 8851 static SDValue PerformVMULCombine(SDNode *N, 8852 TargetLowering::DAGCombinerInfo &DCI, 8853 const ARMSubtarget *Subtarget) { 8854 if (!Subtarget->hasVMLxForwarding()) 8855 return SDValue(); 8856 8857 SelectionDAG &DAG = DCI.DAG; 8858 SDValue N0 = N->getOperand(0); 8859 SDValue N1 = N->getOperand(1); 8860 unsigned Opcode = N0.getOpcode(); 8861 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 8862 Opcode != ISD::FADD && Opcode != ISD::FSUB) { 8863 Opcode = N1.getOpcode(); 8864 if (Opcode != ISD::ADD && Opcode != ISD::SUB && 8865 Opcode != ISD::FADD && Opcode != ISD::FSUB) 8866 return SDValue(); 8867 std::swap(N0, N1); 8868 } 8869 8870 if (N0 == N1) 8871 return SDValue(); 8872 8873 EVT VT = N->getValueType(0); 8874 SDLoc DL(N); 8875 SDValue N00 = N0->getOperand(0); 8876 SDValue N01 = N0->getOperand(1); 8877 return DAG.getNode(Opcode, DL, VT, 8878 DAG.getNode(ISD::MUL, DL, VT, N00, N1), 8879 DAG.getNode(ISD::MUL, DL, VT, N01, N1)); 8880 } 8881 8882 static SDValue PerformMULCombine(SDNode *N, 8883 TargetLowering::DAGCombinerInfo &DCI, 8884 const ARMSubtarget *Subtarget) { 8885 SelectionDAG &DAG = DCI.DAG; 8886 8887 if (Subtarget->isThumb1Only()) 8888 return SDValue(); 8889 8890 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 8891 return SDValue(); 8892 8893 EVT VT = N->getValueType(0); 8894 if (VT.is64BitVector() || VT.is128BitVector()) 8895 return PerformVMULCombine(N, DCI, Subtarget); 8896 if (VT != MVT::i32) 8897 return SDValue(); 8898 8899 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 8900 if (!C) 8901 return SDValue(); 8902 8903 int64_t MulAmt = C->getSExtValue(); 8904 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt); 8905 8906 ShiftAmt = ShiftAmt & (32 - 1); 8907 SDValue V = N->getOperand(0); 8908 SDLoc DL(N); 8909 8910 SDValue Res; 8911 MulAmt >>= ShiftAmt; 8912 8913 if (MulAmt >= 0) { 8914 if (isPowerOf2_32(MulAmt - 1)) { 8915 // (mul x, 2^N + 1) => (add (shl x, N), x) 8916 Res = DAG.getNode(ISD::ADD, DL, VT, 8917 V, 8918 DAG.getNode(ISD::SHL, DL, VT, 8919 V, 8920 DAG.getConstant(Log2_32(MulAmt - 1), DL, 8921 MVT::i32))); 8922 } else if (isPowerOf2_32(MulAmt + 1)) { 8923 // (mul x, 2^N - 1) => (sub (shl x, N), x) 8924 Res = DAG.getNode(ISD::SUB, DL, VT, 8925 DAG.getNode(ISD::SHL, DL, VT, 8926 V, 8927 DAG.getConstant(Log2_32(MulAmt + 1), DL, 8928 MVT::i32)), 8929 V); 8930 } else 8931 return SDValue(); 8932 } else { 8933 uint64_t MulAmtAbs = -MulAmt; 8934 if (isPowerOf2_32(MulAmtAbs + 1)) { 8935 // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) 8936 Res = DAG.getNode(ISD::SUB, DL, VT, 8937 V, 8938 DAG.getNode(ISD::SHL, DL, VT, 8939 V, 8940 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL, 8941 MVT::i32))); 8942 } else if (isPowerOf2_32(MulAmtAbs - 1)) { 8943 // (mul x, -(2^N + 1)) => - (add (shl x, N), x) 8944 Res = DAG.getNode(ISD::ADD, DL, VT, 8945 V, 8946 DAG.getNode(ISD::SHL, DL, VT, 8947 V, 8948 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL, 8949 MVT::i32))); 8950 Res = DAG.getNode(ISD::SUB, DL, VT, 8951 DAG.getConstant(0, DL, MVT::i32), Res); 8952 8953 } else 8954 return SDValue(); 8955 } 8956 8957 if (ShiftAmt != 0) 8958 Res = DAG.getNode(ISD::SHL, DL, VT, 8959 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32)); 8960 8961 // Do not add new nodes to DAG combiner worklist. 8962 DCI.CombineTo(N, Res, false); 8963 return SDValue(); 8964 } 8965 8966 static SDValue PerformANDCombine(SDNode *N, 8967 TargetLowering::DAGCombinerInfo &DCI, 8968 const ARMSubtarget *Subtarget) { 8969 8970 // Attempt to use immediate-form VBIC 8971 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 8972 SDLoc dl(N); 8973 EVT VT = N->getValueType(0); 8974 SelectionDAG &DAG = DCI.DAG; 8975 8976 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 8977 return SDValue(); 8978 8979 APInt SplatBits, SplatUndef; 8980 unsigned SplatBitSize; 8981 bool HasAnyUndefs; 8982 if (BVN && 8983 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 8984 if (SplatBitSize <= 64) { 8985 EVT VbicVT; 8986 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(), 8987 SplatUndef.getZExtValue(), SplatBitSize, 8988 DAG, dl, VbicVT, VT.is128BitVector(), 8989 OtherModImm); 8990 if (Val.getNode()) { 8991 SDValue Input = 8992 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0)); 8993 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val); 8994 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic); 8995 } 8996 } 8997 } 8998 8999 if (!Subtarget->isThumb1Only()) { 9000 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) 9001 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI)) 9002 return Result; 9003 } 9004 9005 return SDValue(); 9006 } 9007 9008 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR 9009 static SDValue PerformORCombine(SDNode *N, 9010 TargetLowering::DAGCombinerInfo &DCI, 9011 const ARMSubtarget *Subtarget) { 9012 // Attempt to use immediate-form VORR 9013 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1)); 9014 SDLoc dl(N); 9015 EVT VT = N->getValueType(0); 9016 SelectionDAG &DAG = DCI.DAG; 9017 9018 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 9019 return SDValue(); 9020 9021 APInt SplatBits, SplatUndef; 9022 unsigned SplatBitSize; 9023 bool HasAnyUndefs; 9024 if (BVN && Subtarget->hasNEON() && 9025 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) { 9026 if (SplatBitSize <= 64) { 9027 EVT VorrVT; 9028 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(), 9029 SplatUndef.getZExtValue(), SplatBitSize, 9030 DAG, dl, VorrVT, VT.is128BitVector(), 9031 OtherModImm); 9032 if (Val.getNode()) { 9033 SDValue Input = 9034 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0)); 9035 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val); 9036 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr); 9037 } 9038 } 9039 } 9040 9041 if (!Subtarget->isThumb1Only()) { 9042 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c)) 9043 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI)) 9044 return Result; 9045 } 9046 9047 // The code below optimizes (or (and X, Y), Z). 9048 // The AND operand needs to have a single user to make these optimizations 9049 // profitable. 9050 SDValue N0 = N->getOperand(0); 9051 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) 9052 return SDValue(); 9053 SDValue N1 = N->getOperand(1); 9054 9055 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant. 9056 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() && 9057 DAG.getTargetLoweringInfo().isTypeLegal(VT)) { 9058 APInt SplatUndef; 9059 unsigned SplatBitSize; 9060 bool HasAnyUndefs; 9061 9062 APInt SplatBits0, SplatBits1; 9063 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1)); 9064 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1)); 9065 // Ensure that the second operand of both ands are constants 9066 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize, 9067 HasAnyUndefs) && !HasAnyUndefs) { 9068 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize, 9069 HasAnyUndefs) && !HasAnyUndefs) { 9070 // Ensure that the bit width of the constants are the same and that 9071 // the splat arguments are logical inverses as per the pattern we 9072 // are trying to simplify. 9073 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() && 9074 SplatBits0 == ~SplatBits1) { 9075 // Canonicalize the vector type to make instruction selection 9076 // simpler. 9077 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32; 9078 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT, 9079 N0->getOperand(1), 9080 N0->getOperand(0), 9081 N1->getOperand(0)); 9082 return DAG.getNode(ISD::BITCAST, dl, VT, Result); 9083 } 9084 } 9085 } 9086 } 9087 9088 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when 9089 // reasonable. 9090 9091 // BFI is only available on V6T2+ 9092 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops()) 9093 return SDValue(); 9094 9095 SDLoc DL(N); 9096 // 1) or (and A, mask), val => ARMbfi A, val, mask 9097 // iff (val & mask) == val 9098 // 9099 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 9100 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2) 9101 // && mask == ~mask2 9102 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2) 9103 // && ~mask == mask2 9104 // (i.e., copy a bitfield value into another bitfield of the same width) 9105 9106 if (VT != MVT::i32) 9107 return SDValue(); 9108 9109 SDValue N00 = N0.getOperand(0); 9110 9111 // The value and the mask need to be constants so we can verify this is 9112 // actually a bitfield set. If the mask is 0xffff, we can do better 9113 // via a movt instruction, so don't use BFI in that case. 9114 SDValue MaskOp = N0.getOperand(1); 9115 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp); 9116 if (!MaskC) 9117 return SDValue(); 9118 unsigned Mask = MaskC->getZExtValue(); 9119 if (Mask == 0xffff) 9120 return SDValue(); 9121 SDValue Res; 9122 // Case (1): or (and A, mask), val => ARMbfi A, val, mask 9123 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); 9124 if (N1C) { 9125 unsigned Val = N1C->getZExtValue(); 9126 if ((Val & ~Mask) != Val) 9127 return SDValue(); 9128 9129 if (ARM::isBitFieldInvertedMask(Mask)) { 9130 Val >>= countTrailingZeros(~Mask); 9131 9132 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, 9133 DAG.getConstant(Val, DL, MVT::i32), 9134 DAG.getConstant(Mask, DL, MVT::i32)); 9135 9136 // Do not add new nodes to DAG combiner worklist. 9137 DCI.CombineTo(N, Res, false); 9138 return SDValue(); 9139 } 9140 } else if (N1.getOpcode() == ISD::AND) { 9141 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask 9142 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 9143 if (!N11C) 9144 return SDValue(); 9145 unsigned Mask2 = N11C->getZExtValue(); 9146 9147 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern 9148 // as is to match. 9149 if (ARM::isBitFieldInvertedMask(Mask) && 9150 (Mask == ~Mask2)) { 9151 // The pack halfword instruction works better for masks that fit it, 9152 // so use that when it's available. 9153 if (Subtarget->hasT2ExtractPack() && 9154 (Mask == 0xffff || Mask == 0xffff0000)) 9155 return SDValue(); 9156 // 2a 9157 unsigned amt = countTrailingZeros(Mask2); 9158 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0), 9159 DAG.getConstant(amt, DL, MVT::i32)); 9160 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res, 9161 DAG.getConstant(Mask, DL, MVT::i32)); 9162 // Do not add new nodes to DAG combiner worklist. 9163 DCI.CombineTo(N, Res, false); 9164 return SDValue(); 9165 } else if (ARM::isBitFieldInvertedMask(~Mask) && 9166 (~Mask == Mask2)) { 9167 // The pack halfword instruction works better for masks that fit it, 9168 // so use that when it's available. 9169 if (Subtarget->hasT2ExtractPack() && 9170 (Mask2 == 0xffff || Mask2 == 0xffff0000)) 9171 return SDValue(); 9172 // 2b 9173 unsigned lsb = countTrailingZeros(Mask); 9174 Res = DAG.getNode(ISD::SRL, DL, VT, N00, 9175 DAG.getConstant(lsb, DL, MVT::i32)); 9176 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res, 9177 DAG.getConstant(Mask2, DL, MVT::i32)); 9178 // Do not add new nodes to DAG combiner worklist. 9179 DCI.CombineTo(N, Res, false); 9180 return SDValue(); 9181 } 9182 } 9183 9184 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) && 9185 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) && 9186 ARM::isBitFieldInvertedMask(~Mask)) { 9187 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask 9188 // where lsb(mask) == #shamt and masked bits of B are known zero. 9189 SDValue ShAmt = N00.getOperand(1); 9190 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue(); 9191 unsigned LSB = countTrailingZeros(Mask); 9192 if (ShAmtC != LSB) 9193 return SDValue(); 9194 9195 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0), 9196 DAG.getConstant(~Mask, DL, MVT::i32)); 9197 9198 // Do not add new nodes to DAG combiner worklist. 9199 DCI.CombineTo(N, Res, false); 9200 } 9201 9202 return SDValue(); 9203 } 9204 9205 static SDValue PerformXORCombine(SDNode *N, 9206 TargetLowering::DAGCombinerInfo &DCI, 9207 const ARMSubtarget *Subtarget) { 9208 EVT VT = N->getValueType(0); 9209 SelectionDAG &DAG = DCI.DAG; 9210 9211 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT)) 9212 return SDValue(); 9213 9214 if (!Subtarget->isThumb1Only()) { 9215 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c)) 9216 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI)) 9217 return Result; 9218 } 9219 9220 return SDValue(); 9221 } 9222 9223 // ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it, 9224 // and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and 9225 // their position in "to" (Rd). 9226 static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) { 9227 assert(N->getOpcode() == ARMISD::BFI); 9228 9229 SDValue From = N->getOperand(1); 9230 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue(); 9231 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation()); 9232 9233 // If the Base came from a SHR #C, we can deduce that it is really testing bit 9234 // #C in the base of the SHR. 9235 if (From->getOpcode() == ISD::SRL && 9236 isa<ConstantSDNode>(From->getOperand(1))) { 9237 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue(); 9238 assert(Shift.getLimitedValue() < 32 && "Shift too large!"); 9239 FromMask <<= Shift.getLimitedValue(31); 9240 From = From->getOperand(0); 9241 } 9242 9243 return From; 9244 } 9245 9246 // If A and B contain one contiguous set of bits, does A | B == A . B? 9247 // 9248 // Neither A nor B must be zero. 9249 static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) { 9250 unsigned LastActiveBitInA = A.countTrailingZeros(); 9251 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1; 9252 return LastActiveBitInA - 1 == FirstActiveBitInB; 9253 } 9254 9255 static SDValue FindBFIToCombineWith(SDNode *N) { 9256 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with, 9257 // if one exists. 9258 APInt ToMask, FromMask; 9259 SDValue From = ParseBFI(N, ToMask, FromMask); 9260 SDValue To = N->getOperand(0); 9261 9262 // Now check for a compatible BFI to merge with. We can pass through BFIs that 9263 // aren't compatible, but not if they set the same bit in their destination as 9264 // we do (or that of any BFI we're going to combine with). 9265 SDValue V = To; 9266 APInt CombinedToMask = ToMask; 9267 while (V.getOpcode() == ARMISD::BFI) { 9268 APInt NewToMask, NewFromMask; 9269 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask); 9270 if (NewFrom != From) { 9271 // This BFI has a different base. Keep going. 9272 CombinedToMask |= NewToMask; 9273 V = V.getOperand(0); 9274 continue; 9275 } 9276 9277 // Do the written bits conflict with any we've seen so far? 9278 if ((NewToMask & CombinedToMask).getBoolValue()) 9279 // Conflicting bits - bail out because going further is unsafe. 9280 return SDValue(); 9281 9282 // Are the new bits contiguous when combined with the old bits? 9283 if (BitsProperlyConcatenate(ToMask, NewToMask) && 9284 BitsProperlyConcatenate(FromMask, NewFromMask)) 9285 return V; 9286 if (BitsProperlyConcatenate(NewToMask, ToMask) && 9287 BitsProperlyConcatenate(NewFromMask, FromMask)) 9288 return V; 9289 9290 // We've seen a write to some bits, so track it. 9291 CombinedToMask |= NewToMask; 9292 // Keep going... 9293 V = V.getOperand(0); 9294 } 9295 9296 return SDValue(); 9297 } 9298 9299 static SDValue PerformBFICombine(SDNode *N, 9300 TargetLowering::DAGCombinerInfo &DCI) { 9301 SDValue N1 = N->getOperand(1); 9302 if (N1.getOpcode() == ISD::AND) { 9303 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff 9304 // the bits being cleared by the AND are not demanded by the BFI. 9305 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1)); 9306 if (!N11C) 9307 return SDValue(); 9308 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(); 9309 unsigned LSB = countTrailingZeros(~InvMask); 9310 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB; 9311 assert(Width < 9312 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) && 9313 "undefined behavior"); 9314 unsigned Mask = (1u << Width) - 1; 9315 unsigned Mask2 = N11C->getZExtValue(); 9316 if ((Mask & (~Mask2)) == 0) 9317 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0), 9318 N->getOperand(0), N1.getOperand(0), 9319 N->getOperand(2)); 9320 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) { 9321 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes. 9322 // Keep track of any consecutive bits set that all come from the same base 9323 // value. We can combine these together into a single BFI. 9324 SDValue CombineBFI = FindBFIToCombineWith(N); 9325 if (CombineBFI == SDValue()) 9326 return SDValue(); 9327 9328 // We've found a BFI. 9329 APInt ToMask1, FromMask1; 9330 SDValue From1 = ParseBFI(N, ToMask1, FromMask1); 9331 9332 APInt ToMask2, FromMask2; 9333 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2); 9334 assert(From1 == From2); 9335 (void)From2; 9336 9337 // First, unlink CombineBFI. 9338 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0)); 9339 // Then create a new BFI, combining the two together. 9340 APInt NewFromMask = FromMask1 | FromMask2; 9341 APInt NewToMask = ToMask1 | ToMask2; 9342 9343 EVT VT = N->getValueType(0); 9344 SDLoc dl(N); 9345 9346 if (NewFromMask[0] == 0) 9347 From1 = DCI.DAG.getNode( 9348 ISD::SRL, dl, VT, From1, 9349 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT)); 9350 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1, 9351 DCI.DAG.getConstant(~NewToMask, dl, VT)); 9352 } 9353 return SDValue(); 9354 } 9355 9356 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for 9357 /// ARMISD::VMOVRRD. 9358 static SDValue PerformVMOVRRDCombine(SDNode *N, 9359 TargetLowering::DAGCombinerInfo &DCI, 9360 const ARMSubtarget *Subtarget) { 9361 // vmovrrd(vmovdrr x, y) -> x,y 9362 SDValue InDouble = N->getOperand(0); 9363 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP()) 9364 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1)); 9365 9366 // vmovrrd(load f64) -> (load i32), (load i32) 9367 SDNode *InNode = InDouble.getNode(); 9368 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() && 9369 InNode->getValueType(0) == MVT::f64 && 9370 InNode->getOperand(1).getOpcode() == ISD::FrameIndex && 9371 !cast<LoadSDNode>(InNode)->isVolatile()) { 9372 // TODO: Should this be done for non-FrameIndex operands? 9373 LoadSDNode *LD = cast<LoadSDNode>(InNode); 9374 9375 SelectionDAG &DAG = DCI.DAG; 9376 SDLoc DL(LD); 9377 SDValue BasePtr = LD->getBasePtr(); 9378 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, 9379 LD->getPointerInfo(), LD->isVolatile(), 9380 LD->isNonTemporal(), LD->isInvariant(), 9381 LD->getAlignment()); 9382 9383 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 9384 DAG.getConstant(4, DL, MVT::i32)); 9385 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, 9386 LD->getPointerInfo(), LD->isVolatile(), 9387 LD->isNonTemporal(), LD->isInvariant(), 9388 std::min(4U, LD->getAlignment() / 2)); 9389 9390 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1)); 9391 if (DCI.DAG.getDataLayout().isBigEndian()) 9392 std::swap (NewLD1, NewLD2); 9393 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2); 9394 return Result; 9395 } 9396 9397 return SDValue(); 9398 } 9399 9400 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for 9401 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands. 9402 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) { 9403 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X) 9404 SDValue Op0 = N->getOperand(0); 9405 SDValue Op1 = N->getOperand(1); 9406 if (Op0.getOpcode() == ISD::BITCAST) 9407 Op0 = Op0.getOperand(0); 9408 if (Op1.getOpcode() == ISD::BITCAST) 9409 Op1 = Op1.getOperand(0); 9410 if (Op0.getOpcode() == ARMISD::VMOVRRD && 9411 Op0.getNode() == Op1.getNode() && 9412 Op0.getResNo() == 0 && Op1.getResNo() == 1) 9413 return DAG.getNode(ISD::BITCAST, SDLoc(N), 9414 N->getValueType(0), Op0.getOperand(0)); 9415 return SDValue(); 9416 } 9417 9418 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node 9419 /// are normal, non-volatile loads. If so, it is profitable to bitcast an 9420 /// i64 vector to have f64 elements, since the value can then be loaded 9421 /// directly into a VFP register. 9422 static bool hasNormalLoadOperand(SDNode *N) { 9423 unsigned NumElts = N->getValueType(0).getVectorNumElements(); 9424 for (unsigned i = 0; i < NumElts; ++i) { 9425 SDNode *Elt = N->getOperand(i).getNode(); 9426 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile()) 9427 return true; 9428 } 9429 return false; 9430 } 9431 9432 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for 9433 /// ISD::BUILD_VECTOR. 9434 static SDValue PerformBUILD_VECTORCombine(SDNode *N, 9435 TargetLowering::DAGCombinerInfo &DCI, 9436 const ARMSubtarget *Subtarget) { 9437 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X): 9438 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value 9439 // into a pair of GPRs, which is fine when the value is used as a scalar, 9440 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD. 9441 SelectionDAG &DAG = DCI.DAG; 9442 if (N->getNumOperands() == 2) 9443 if (SDValue RV = PerformVMOVDRRCombine(N, DAG)) 9444 return RV; 9445 9446 // Load i64 elements as f64 values so that type legalization does not split 9447 // them up into i32 values. 9448 EVT VT = N->getValueType(0); 9449 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N)) 9450 return SDValue(); 9451 SDLoc dl(N); 9452 SmallVector<SDValue, 8> Ops; 9453 unsigned NumElts = VT.getVectorNumElements(); 9454 for (unsigned i = 0; i < NumElts; ++i) { 9455 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i)); 9456 Ops.push_back(V); 9457 // Make the DAGCombiner fold the bitcast. 9458 DCI.AddToWorklist(V.getNode()); 9459 } 9460 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts); 9461 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops); 9462 return DAG.getNode(ISD::BITCAST, dl, VT, BV); 9463 } 9464 9465 /// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR. 9466 static SDValue 9467 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 9468 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR. 9469 // At that time, we may have inserted bitcasts from integer to float. 9470 // If these bitcasts have survived DAGCombine, change the lowering of this 9471 // BUILD_VECTOR in something more vector friendly, i.e., that does not 9472 // force to use floating point types. 9473 9474 // Make sure we can change the type of the vector. 9475 // This is possible iff: 9476 // 1. The vector is only used in a bitcast to a integer type. I.e., 9477 // 1.1. Vector is used only once. 9478 // 1.2. Use is a bit convert to an integer type. 9479 // 2. The size of its operands are 32-bits (64-bits are not legal). 9480 EVT VT = N->getValueType(0); 9481 EVT EltVT = VT.getVectorElementType(); 9482 9483 // Check 1.1. and 2. 9484 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse()) 9485 return SDValue(); 9486 9487 // By construction, the input type must be float. 9488 assert(EltVT == MVT::f32 && "Unexpected type!"); 9489 9490 // Check 1.2. 9491 SDNode *Use = *N->use_begin(); 9492 if (Use->getOpcode() != ISD::BITCAST || 9493 Use->getValueType(0).isFloatingPoint()) 9494 return SDValue(); 9495 9496 // Check profitability. 9497 // Model is, if more than half of the relevant operands are bitcast from 9498 // i32, turn the build_vector into a sequence of insert_vector_elt. 9499 // Relevant operands are everything that is not statically 9500 // (i.e., at compile time) bitcasted. 9501 unsigned NumOfBitCastedElts = 0; 9502 unsigned NumElts = VT.getVectorNumElements(); 9503 unsigned NumOfRelevantElts = NumElts; 9504 for (unsigned Idx = 0; Idx < NumElts; ++Idx) { 9505 SDValue Elt = N->getOperand(Idx); 9506 if (Elt->getOpcode() == ISD::BITCAST) { 9507 // Assume only bit cast to i32 will go away. 9508 if (Elt->getOperand(0).getValueType() == MVT::i32) 9509 ++NumOfBitCastedElts; 9510 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt)) 9511 // Constants are statically casted, thus do not count them as 9512 // relevant operands. 9513 --NumOfRelevantElts; 9514 } 9515 9516 // Check if more than half of the elements require a non-free bitcast. 9517 if (NumOfBitCastedElts <= NumOfRelevantElts / 2) 9518 return SDValue(); 9519 9520 SelectionDAG &DAG = DCI.DAG; 9521 // Create the new vector type. 9522 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); 9523 // Check if the type is legal. 9524 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9525 if (!TLI.isTypeLegal(VecVT)) 9526 return SDValue(); 9527 9528 // Combine: 9529 // ARMISD::BUILD_VECTOR E1, E2, ..., EN. 9530 // => BITCAST INSERT_VECTOR_ELT 9531 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1), 9532 // (BITCAST EN), N. 9533 SDValue Vec = DAG.getUNDEF(VecVT); 9534 SDLoc dl(N); 9535 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) { 9536 SDValue V = N->getOperand(Idx); 9537 if (V.isUndef()) 9538 continue; 9539 if (V.getOpcode() == ISD::BITCAST && 9540 V->getOperand(0).getValueType() == MVT::i32) 9541 // Fold obvious case. 9542 V = V.getOperand(0); 9543 else { 9544 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V); 9545 // Make the DAGCombiner fold the bitcasts. 9546 DCI.AddToWorklist(V.getNode()); 9547 } 9548 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32); 9549 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx); 9550 } 9551 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec); 9552 // Make the DAGCombiner fold the bitcasts. 9553 DCI.AddToWorklist(Vec.getNode()); 9554 return Vec; 9555 } 9556 9557 /// PerformInsertEltCombine - Target-specific dag combine xforms for 9558 /// ISD::INSERT_VECTOR_ELT. 9559 static SDValue PerformInsertEltCombine(SDNode *N, 9560 TargetLowering::DAGCombinerInfo &DCI) { 9561 // Bitcast an i64 load inserted into a vector to f64. 9562 // Otherwise, the i64 value will be legalized to a pair of i32 values. 9563 EVT VT = N->getValueType(0); 9564 SDNode *Elt = N->getOperand(1).getNode(); 9565 if (VT.getVectorElementType() != MVT::i64 || 9566 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile()) 9567 return SDValue(); 9568 9569 SelectionDAG &DAG = DCI.DAG; 9570 SDLoc dl(N); 9571 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 9572 VT.getVectorNumElements()); 9573 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0)); 9574 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1)); 9575 // Make the DAGCombiner fold the bitcasts. 9576 DCI.AddToWorklist(Vec.getNode()); 9577 DCI.AddToWorklist(V.getNode()); 9578 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT, 9579 Vec, V, N->getOperand(2)); 9580 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt); 9581 } 9582 9583 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for 9584 /// ISD::VECTOR_SHUFFLE. 9585 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) { 9586 // The LLVM shufflevector instruction does not require the shuffle mask 9587 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does 9588 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the 9589 // operands do not match the mask length, they are extended by concatenating 9590 // them with undef vectors. That is probably the right thing for other 9591 // targets, but for NEON it is better to concatenate two double-register 9592 // size vector operands into a single quad-register size vector. Do that 9593 // transformation here: 9594 // shuffle(concat(v1, undef), concat(v2, undef)) -> 9595 // shuffle(concat(v1, v2), undef) 9596 SDValue Op0 = N->getOperand(0); 9597 SDValue Op1 = N->getOperand(1); 9598 if (Op0.getOpcode() != ISD::CONCAT_VECTORS || 9599 Op1.getOpcode() != ISD::CONCAT_VECTORS || 9600 Op0.getNumOperands() != 2 || 9601 Op1.getNumOperands() != 2) 9602 return SDValue(); 9603 SDValue Concat0Op1 = Op0.getOperand(1); 9604 SDValue Concat1Op1 = Op1.getOperand(1); 9605 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef()) 9606 return SDValue(); 9607 // Skip the transformation if any of the types are illegal. 9608 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9609 EVT VT = N->getValueType(0); 9610 if (!TLI.isTypeLegal(VT) || 9611 !TLI.isTypeLegal(Concat0Op1.getValueType()) || 9612 !TLI.isTypeLegal(Concat1Op1.getValueType())) 9613 return SDValue(); 9614 9615 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, 9616 Op0.getOperand(0), Op1.getOperand(0)); 9617 // Translate the shuffle mask. 9618 SmallVector<int, 16> NewMask; 9619 unsigned NumElts = VT.getVectorNumElements(); 9620 unsigned HalfElts = NumElts/2; 9621 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); 9622 for (unsigned n = 0; n < NumElts; ++n) { 9623 int MaskElt = SVN->getMaskElt(n); 9624 int NewElt = -1; 9625 if (MaskElt < (int)HalfElts) 9626 NewElt = MaskElt; 9627 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts)) 9628 NewElt = HalfElts + MaskElt - NumElts; 9629 NewMask.push_back(NewElt); 9630 } 9631 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat, 9632 DAG.getUNDEF(VT), NewMask.data()); 9633 } 9634 9635 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP, 9636 /// NEON load/store intrinsics, and generic vector load/stores, to merge 9637 /// base address updates. 9638 /// For generic load/stores, the memory type is assumed to be a vector. 9639 /// The caller is assumed to have checked legality. 9640 static SDValue CombineBaseUpdate(SDNode *N, 9641 TargetLowering::DAGCombinerInfo &DCI) { 9642 SelectionDAG &DAG = DCI.DAG; 9643 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID || 9644 N->getOpcode() == ISD::INTRINSIC_W_CHAIN); 9645 const bool isStore = N->getOpcode() == ISD::STORE; 9646 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1); 9647 SDValue Addr = N->getOperand(AddrOpIdx); 9648 MemSDNode *MemN = cast<MemSDNode>(N); 9649 SDLoc dl(N); 9650 9651 // Search for a use of the address operand that is an increment. 9652 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), 9653 UE = Addr.getNode()->use_end(); UI != UE; ++UI) { 9654 SDNode *User = *UI; 9655 if (User->getOpcode() != ISD::ADD || 9656 UI.getUse().getResNo() != Addr.getResNo()) 9657 continue; 9658 9659 // Check that the add is independent of the load/store. Otherwise, folding 9660 // it would create a cycle. 9661 if (User->isPredecessorOf(N) || N->isPredecessorOf(User)) 9662 continue; 9663 9664 // Find the new opcode for the updating load/store. 9665 bool isLoadOp = true; 9666 bool isLaneOp = false; 9667 unsigned NewOpc = 0; 9668 unsigned NumVecs = 0; 9669 if (isIntrinsic) { 9670 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 9671 switch (IntNo) { 9672 default: llvm_unreachable("unexpected intrinsic for Neon base update"); 9673 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD; 9674 NumVecs = 1; break; 9675 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD; 9676 NumVecs = 2; break; 9677 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD; 9678 NumVecs = 3; break; 9679 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD; 9680 NumVecs = 4; break; 9681 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD; 9682 NumVecs = 2; isLaneOp = true; break; 9683 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD; 9684 NumVecs = 3; isLaneOp = true; break; 9685 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD; 9686 NumVecs = 4; isLaneOp = true; break; 9687 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD; 9688 NumVecs = 1; isLoadOp = false; break; 9689 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD; 9690 NumVecs = 2; isLoadOp = false; break; 9691 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD; 9692 NumVecs = 3; isLoadOp = false; break; 9693 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD; 9694 NumVecs = 4; isLoadOp = false; break; 9695 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD; 9696 NumVecs = 2; isLoadOp = false; isLaneOp = true; break; 9697 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD; 9698 NumVecs = 3; isLoadOp = false; isLaneOp = true; break; 9699 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD; 9700 NumVecs = 4; isLoadOp = false; isLaneOp = true; break; 9701 } 9702 } else { 9703 isLaneOp = true; 9704 switch (N->getOpcode()) { 9705 default: llvm_unreachable("unexpected opcode for Neon base update"); 9706 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break; 9707 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break; 9708 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break; 9709 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD; 9710 NumVecs = 1; isLaneOp = false; break; 9711 case ISD::STORE: NewOpc = ARMISD::VST1_UPD; 9712 NumVecs = 1; isLaneOp = false; isLoadOp = false; break; 9713 } 9714 } 9715 9716 // Find the size of memory referenced by the load/store. 9717 EVT VecTy; 9718 if (isLoadOp) { 9719 VecTy = N->getValueType(0); 9720 } else if (isIntrinsic) { 9721 VecTy = N->getOperand(AddrOpIdx+1).getValueType(); 9722 } else { 9723 assert(isStore && "Node has to be a load, a store, or an intrinsic!"); 9724 VecTy = N->getOperand(1).getValueType(); 9725 } 9726 9727 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8; 9728 if (isLaneOp) 9729 NumBytes /= VecTy.getVectorNumElements(); 9730 9731 // If the increment is a constant, it must match the memory ref size. 9732 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0); 9733 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) { 9734 uint64_t IncVal = CInc->getZExtValue(); 9735 if (IncVal != NumBytes) 9736 continue; 9737 } else if (NumBytes >= 3 * 16) { 9738 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two 9739 // separate instructions that make it harder to use a non-constant update. 9740 continue; 9741 } 9742 9743 // OK, we found an ADD we can fold into the base update. 9744 // Now, create a _UPD node, taking care of not breaking alignment. 9745 9746 EVT AlignedVecTy = VecTy; 9747 unsigned Alignment = MemN->getAlignment(); 9748 9749 // If this is a less-than-standard-aligned load/store, change the type to 9750 // match the standard alignment. 9751 // The alignment is overlooked when selecting _UPD variants; and it's 9752 // easier to introduce bitcasts here than fix that. 9753 // There are 3 ways to get to this base-update combine: 9754 // - intrinsics: they are assumed to be properly aligned (to the standard 9755 // alignment of the memory type), so we don't need to do anything. 9756 // - ARMISD::VLDx nodes: they are only generated from the aforementioned 9757 // intrinsics, so, likewise, there's nothing to do. 9758 // - generic load/store instructions: the alignment is specified as an 9759 // explicit operand, rather than implicitly as the standard alignment 9760 // of the memory type (like the intrisics). We need to change the 9761 // memory type to match the explicit alignment. That way, we don't 9762 // generate non-standard-aligned ARMISD::VLDx nodes. 9763 if (isa<LSBaseSDNode>(N)) { 9764 if (Alignment == 0) 9765 Alignment = 1; 9766 if (Alignment < VecTy.getScalarSizeInBits() / 8) { 9767 MVT EltTy = MVT::getIntegerVT(Alignment * 8); 9768 assert(NumVecs == 1 && "Unexpected multi-element generic load/store."); 9769 assert(!isLaneOp && "Unexpected generic load/store lane."); 9770 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8); 9771 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts); 9772 } 9773 // Don't set an explicit alignment on regular load/stores that we want 9774 // to transform to VLD/VST 1_UPD nodes. 9775 // This matches the behavior of regular load/stores, which only get an 9776 // explicit alignment if the MMO alignment is larger than the standard 9777 // alignment of the memory type. 9778 // Intrinsics, however, always get an explicit alignment, set to the 9779 // alignment of the MMO. 9780 Alignment = 1; 9781 } 9782 9783 // Create the new updating load/store node. 9784 // First, create an SDVTList for the new updating node's results. 9785 EVT Tys[6]; 9786 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0); 9787 unsigned n; 9788 for (n = 0; n < NumResultVecs; ++n) 9789 Tys[n] = AlignedVecTy; 9790 Tys[n++] = MVT::i32; 9791 Tys[n] = MVT::Other; 9792 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2)); 9793 9794 // Then, gather the new node's operands. 9795 SmallVector<SDValue, 8> Ops; 9796 Ops.push_back(N->getOperand(0)); // incoming chain 9797 Ops.push_back(N->getOperand(AddrOpIdx)); 9798 Ops.push_back(Inc); 9799 9800 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) { 9801 // Try to match the intrinsic's signature 9802 Ops.push_back(StN->getValue()); 9803 } else { 9804 // Loads (and of course intrinsics) match the intrinsics' signature, 9805 // so just add all but the alignment operand. 9806 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i) 9807 Ops.push_back(N->getOperand(i)); 9808 } 9809 9810 // For all node types, the alignment operand is always the last one. 9811 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32)); 9812 9813 // If this is a non-standard-aligned STORE, the penultimate operand is the 9814 // stored value. Bitcast it to the aligned type. 9815 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) { 9816 SDValue &StVal = Ops[Ops.size()-2]; 9817 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal); 9818 } 9819 9820 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys, 9821 Ops, AlignedVecTy, 9822 MemN->getMemOperand()); 9823 9824 // Update the uses. 9825 SmallVector<SDValue, 5> NewResults; 9826 for (unsigned i = 0; i < NumResultVecs; ++i) 9827 NewResults.push_back(SDValue(UpdN.getNode(), i)); 9828 9829 // If this is an non-standard-aligned LOAD, the first result is the loaded 9830 // value. Bitcast it to the expected result type. 9831 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) { 9832 SDValue &LdVal = NewResults[0]; 9833 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal); 9834 } 9835 9836 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain 9837 DCI.CombineTo(N, NewResults); 9838 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs)); 9839 9840 break; 9841 } 9842 return SDValue(); 9843 } 9844 9845 static SDValue PerformVLDCombine(SDNode *N, 9846 TargetLowering::DAGCombinerInfo &DCI) { 9847 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) 9848 return SDValue(); 9849 9850 return CombineBaseUpdate(N, DCI); 9851 } 9852 9853 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a 9854 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic 9855 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and 9856 /// return true. 9857 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) { 9858 SelectionDAG &DAG = DCI.DAG; 9859 EVT VT = N->getValueType(0); 9860 // vldN-dup instructions only support 64-bit vectors for N > 1. 9861 if (!VT.is64BitVector()) 9862 return false; 9863 9864 // Check if the VDUPLANE operand is a vldN-dup intrinsic. 9865 SDNode *VLD = N->getOperand(0).getNode(); 9866 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN) 9867 return false; 9868 unsigned NumVecs = 0; 9869 unsigned NewOpc = 0; 9870 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue(); 9871 if (IntNo == Intrinsic::arm_neon_vld2lane) { 9872 NumVecs = 2; 9873 NewOpc = ARMISD::VLD2DUP; 9874 } else if (IntNo == Intrinsic::arm_neon_vld3lane) { 9875 NumVecs = 3; 9876 NewOpc = ARMISD::VLD3DUP; 9877 } else if (IntNo == Intrinsic::arm_neon_vld4lane) { 9878 NumVecs = 4; 9879 NewOpc = ARMISD::VLD4DUP; 9880 } else { 9881 return false; 9882 } 9883 9884 // First check that all the vldN-lane uses are VDUPLANEs and that the lane 9885 // numbers match the load. 9886 unsigned VLDLaneNo = 9887 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue(); 9888 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 9889 UI != UE; ++UI) { 9890 // Ignore uses of the chain result. 9891 if (UI.getUse().getResNo() == NumVecs) 9892 continue; 9893 SDNode *User = *UI; 9894 if (User->getOpcode() != ARMISD::VDUPLANE || 9895 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue()) 9896 return false; 9897 } 9898 9899 // Create the vldN-dup node. 9900 EVT Tys[5]; 9901 unsigned n; 9902 for (n = 0; n < NumVecs; ++n) 9903 Tys[n] = VT; 9904 Tys[n] = MVT::Other; 9905 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1)); 9906 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) }; 9907 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD); 9908 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, 9909 Ops, VLDMemInt->getMemoryVT(), 9910 VLDMemInt->getMemOperand()); 9911 9912 // Update the uses. 9913 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end(); 9914 UI != UE; ++UI) { 9915 unsigned ResNo = UI.getUse().getResNo(); 9916 // Ignore uses of the chain result. 9917 if (ResNo == NumVecs) 9918 continue; 9919 SDNode *User = *UI; 9920 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo)); 9921 } 9922 9923 // Now the vldN-lane intrinsic is dead except for its chain result. 9924 // Update uses of the chain. 9925 std::vector<SDValue> VLDDupResults; 9926 for (unsigned n = 0; n < NumVecs; ++n) 9927 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n)); 9928 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs)); 9929 DCI.CombineTo(VLD, VLDDupResults); 9930 9931 return true; 9932 } 9933 9934 /// PerformVDUPLANECombine - Target-specific dag combine xforms for 9935 /// ARMISD::VDUPLANE. 9936 static SDValue PerformVDUPLANECombine(SDNode *N, 9937 TargetLowering::DAGCombinerInfo &DCI) { 9938 SDValue Op = N->getOperand(0); 9939 9940 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses 9941 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation. 9942 if (CombineVLDDUP(N, DCI)) 9943 return SDValue(N, 0); 9944 9945 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is 9946 // redundant. Ignore bit_converts for now; element sizes are checked below. 9947 while (Op.getOpcode() == ISD::BITCAST) 9948 Op = Op.getOperand(0); 9949 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM) 9950 return SDValue(); 9951 9952 // Make sure the VMOV element size is not bigger than the VDUPLANE elements. 9953 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits(); 9954 // The canonical VMOV for a zero vector uses a 32-bit element size. 9955 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 9956 unsigned EltBits; 9957 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0) 9958 EltSize = 8; 9959 EVT VT = N->getValueType(0); 9960 if (EltSize > VT.getVectorElementType().getSizeInBits()) 9961 return SDValue(); 9962 9963 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); 9964 } 9965 9966 static SDValue PerformLOADCombine(SDNode *N, 9967 TargetLowering::DAGCombinerInfo &DCI) { 9968 EVT VT = N->getValueType(0); 9969 9970 // If this is a legal vector load, try to combine it into a VLD1_UPD. 9971 if (ISD::isNormalLoad(N) && VT.isVector() && 9972 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 9973 return CombineBaseUpdate(N, DCI); 9974 9975 return SDValue(); 9976 } 9977 9978 /// PerformSTORECombine - Target-specific dag combine xforms for 9979 /// ISD::STORE. 9980 static SDValue PerformSTORECombine(SDNode *N, 9981 TargetLowering::DAGCombinerInfo &DCI) { 9982 StoreSDNode *St = cast<StoreSDNode>(N); 9983 if (St->isVolatile()) 9984 return SDValue(); 9985 9986 // Optimize trunc store (of multiple scalars) to shuffle and store. First, 9987 // pack all of the elements in one place. Next, store to memory in fewer 9988 // chunks. 9989 SDValue StVal = St->getValue(); 9990 EVT VT = StVal.getValueType(); 9991 if (St->isTruncatingStore() && VT.isVector()) { 9992 SelectionDAG &DAG = DCI.DAG; 9993 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9994 EVT StVT = St->getMemoryVT(); 9995 unsigned NumElems = VT.getVectorNumElements(); 9996 assert(StVT != VT && "Cannot truncate to the same type"); 9997 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits(); 9998 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits(); 9999 10000 // From, To sizes and ElemCount must be pow of two 10001 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue(); 10002 10003 // We are going to use the original vector elt for storing. 10004 // Accumulated smaller vector elements must be a multiple of the store size. 10005 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue(); 10006 10007 unsigned SizeRatio = FromEltSz / ToEltSz; 10008 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits()); 10009 10010 // Create a type on which we perform the shuffle. 10011 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(), 10012 NumElems*SizeRatio); 10013 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); 10014 10015 SDLoc DL(St); 10016 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal); 10017 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); 10018 for (unsigned i = 0; i < NumElems; ++i) 10019 ShuffleVec[i] = DAG.getDataLayout().isBigEndian() 10020 ? (i + 1) * SizeRatio - 1 10021 : i * SizeRatio; 10022 10023 // Can't shuffle using an illegal type. 10024 if (!TLI.isTypeLegal(WideVecVT)) return SDValue(); 10025 10026 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec, 10027 DAG.getUNDEF(WideVec.getValueType()), 10028 ShuffleVec.data()); 10029 // At this point all of the data is stored at the bottom of the 10030 // register. We now need to save it to mem. 10031 10032 // Find the largest store unit 10033 MVT StoreType = MVT::i8; 10034 for (MVT Tp : MVT::integer_valuetypes()) { 10035 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz) 10036 StoreType = Tp; 10037 } 10038 // Didn't find a legal store type. 10039 if (!TLI.isTypeLegal(StoreType)) 10040 return SDValue(); 10041 10042 // Bitcast the original vector into a vector of store-size units 10043 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(), 10044 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits()); 10045 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); 10046 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff); 10047 SmallVector<SDValue, 8> Chains; 10048 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL, 10049 TLI.getPointerTy(DAG.getDataLayout())); 10050 SDValue BasePtr = St->getBasePtr(); 10051 10052 // Perform one or more big stores into memory. 10053 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits(); 10054 for (unsigned I = 0; I < E; I++) { 10055 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, 10056 StoreType, ShuffWide, 10057 DAG.getIntPtrConstant(I, DL)); 10058 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr, 10059 St->getPointerInfo(), St->isVolatile(), 10060 St->isNonTemporal(), St->getAlignment()); 10061 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr, 10062 Increment); 10063 Chains.push_back(Ch); 10064 } 10065 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 10066 } 10067 10068 if (!ISD::isNormalStore(St)) 10069 return SDValue(); 10070 10071 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and 10072 // ARM stores of arguments in the same cache line. 10073 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR && 10074 StVal.getNode()->hasOneUse()) { 10075 SelectionDAG &DAG = DCI.DAG; 10076 bool isBigEndian = DAG.getDataLayout().isBigEndian(); 10077 SDLoc DL(St); 10078 SDValue BasePtr = St->getBasePtr(); 10079 SDValue NewST1 = DAG.getStore(St->getChain(), DL, 10080 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ), 10081 BasePtr, St->getPointerInfo(), St->isVolatile(), 10082 St->isNonTemporal(), St->getAlignment()); 10083 10084 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr, 10085 DAG.getConstant(4, DL, MVT::i32)); 10086 return DAG.getStore(NewST1.getValue(0), DL, 10087 StVal.getNode()->getOperand(isBigEndian ? 0 : 1), 10088 OffsetPtr, St->getPointerInfo(), St->isVolatile(), 10089 St->isNonTemporal(), 10090 std::min(4U, St->getAlignment() / 2)); 10091 } 10092 10093 if (StVal.getValueType() == MVT::i64 && 10094 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 10095 10096 // Bitcast an i64 store extracted from a vector to f64. 10097 // Otherwise, the i64 value will be legalized to a pair of i32 values. 10098 SelectionDAG &DAG = DCI.DAG; 10099 SDLoc dl(StVal); 10100 SDValue IntVec = StVal.getOperand(0); 10101 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, 10102 IntVec.getValueType().getVectorNumElements()); 10103 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec); 10104 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, 10105 Vec, StVal.getOperand(1)); 10106 dl = SDLoc(N); 10107 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt); 10108 // Make the DAGCombiner fold the bitcasts. 10109 DCI.AddToWorklist(Vec.getNode()); 10110 DCI.AddToWorklist(ExtElt.getNode()); 10111 DCI.AddToWorklist(V.getNode()); 10112 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(), 10113 St->getPointerInfo(), St->isVolatile(), 10114 St->isNonTemporal(), St->getAlignment(), 10115 St->getAAInfo()); 10116 } 10117 10118 // If this is a legal vector store, try to combine it into a VST1_UPD. 10119 if (ISD::isNormalStore(N) && VT.isVector() && 10120 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT)) 10121 return CombineBaseUpdate(N, DCI); 10122 10123 return SDValue(); 10124 } 10125 10126 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD) 10127 /// can replace combinations of VMUL and VCVT (floating-point to integer) 10128 /// when the VMUL has a constant operand that is a power of 2. 10129 /// 10130 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 10131 /// vmul.f32 d16, d17, d16 10132 /// vcvt.s32.f32 d16, d16 10133 /// becomes: 10134 /// vcvt.s32.f32 d16, d16, #3 10135 static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG, 10136 const ARMSubtarget *Subtarget) { 10137 if (!Subtarget->hasNEON()) 10138 return SDValue(); 10139 10140 SDValue Op = N->getOperand(0); 10141 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() || 10142 Op.getOpcode() != ISD::FMUL) 10143 return SDValue(); 10144 10145 SDValue ConstVec = Op->getOperand(1); 10146 if (!isa<BuildVectorSDNode>(ConstVec)) 10147 return SDValue(); 10148 10149 MVT FloatTy = Op.getSimpleValueType().getVectorElementType(); 10150 uint32_t FloatBits = FloatTy.getSizeInBits(); 10151 MVT IntTy = N->getSimpleValueType(0).getVectorElementType(); 10152 uint32_t IntBits = IntTy.getSizeInBits(); 10153 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 10154 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) { 10155 // These instructions only exist converting from f32 to i32. We can handle 10156 // smaller integers by generating an extra truncate, but larger ones would 10157 // be lossy. We also can't handle more then 4 lanes, since these intructions 10158 // only support v2i32/v4i32 types. 10159 return SDValue(); 10160 } 10161 10162 BitVector UndefElements; 10163 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 10164 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33); 10165 if (C == -1 || C == 0 || C > 32) 10166 return SDValue(); 10167 10168 SDLoc dl(N); 10169 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT; 10170 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs : 10171 Intrinsic::arm_neon_vcvtfp2fxu; 10172 SDValue FixConv = DAG.getNode( 10173 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 10174 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0), 10175 DAG.getConstant(C, dl, MVT::i32)); 10176 10177 if (IntBits < FloatBits) 10178 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv); 10179 10180 return FixConv; 10181 } 10182 10183 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD) 10184 /// can replace combinations of VCVT (integer to floating-point) and VDIV 10185 /// when the VDIV has a constant operand that is a power of 2. 10186 /// 10187 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>): 10188 /// vcvt.f32.s32 d16, d16 10189 /// vdiv.f32 d16, d17, d16 10190 /// becomes: 10191 /// vcvt.f32.s32 d16, d16, #3 10192 static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG, 10193 const ARMSubtarget *Subtarget) { 10194 if (!Subtarget->hasNEON()) 10195 return SDValue(); 10196 10197 SDValue Op = N->getOperand(0); 10198 unsigned OpOpcode = Op.getNode()->getOpcode(); 10199 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() || 10200 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP)) 10201 return SDValue(); 10202 10203 SDValue ConstVec = N->getOperand(1); 10204 if (!isa<BuildVectorSDNode>(ConstVec)) 10205 return SDValue(); 10206 10207 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType(); 10208 uint32_t FloatBits = FloatTy.getSizeInBits(); 10209 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType(); 10210 uint32_t IntBits = IntTy.getSizeInBits(); 10211 unsigned NumLanes = Op.getValueType().getVectorNumElements(); 10212 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) { 10213 // These instructions only exist converting from i32 to f32. We can handle 10214 // smaller integers by generating an extra extend, but larger ones would 10215 // be lossy. We also can't handle more then 4 lanes, since these intructions 10216 // only support v2i32/v4i32 types. 10217 return SDValue(); 10218 } 10219 10220 BitVector UndefElements; 10221 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec); 10222 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33); 10223 if (C == -1 || C == 0 || C > 32) 10224 return SDValue(); 10225 10226 SDLoc dl(N); 10227 bool isSigned = OpOpcode == ISD::SINT_TO_FP; 10228 SDValue ConvInput = Op.getOperand(0); 10229 if (IntBits < FloatBits) 10230 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 10231 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32, 10232 ConvInput); 10233 10234 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp : 10235 Intrinsic::arm_neon_vcvtfxu2fp; 10236 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, 10237 Op.getValueType(), 10238 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), 10239 ConvInput, DAG.getConstant(C, dl, MVT::i32)); 10240 } 10241 10242 /// Getvshiftimm - Check if this is a valid build_vector for the immediate 10243 /// operand of a vector shift operation, where all the elements of the 10244 /// build_vector must have the same constant integer value. 10245 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) { 10246 // Ignore bit_converts. 10247 while (Op.getOpcode() == ISD::BITCAST) 10248 Op = Op.getOperand(0); 10249 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); 10250 APInt SplatBits, SplatUndef; 10251 unsigned SplatBitSize; 10252 bool HasAnyUndefs; 10253 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, 10254 HasAnyUndefs, ElementBits) || 10255 SplatBitSize > ElementBits) 10256 return false; 10257 Cnt = SplatBits.getSExtValue(); 10258 return true; 10259 } 10260 10261 /// isVShiftLImm - Check if this is a valid build_vector for the immediate 10262 /// operand of a vector shift left operation. That value must be in the range: 10263 /// 0 <= Value < ElementBits for a left shift; or 10264 /// 0 <= Value <= ElementBits for a long left shift. 10265 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) { 10266 assert(VT.isVector() && "vector shift count is not a vector type"); 10267 int64_t ElementBits = VT.getVectorElementType().getSizeInBits(); 10268 if (! getVShiftImm(Op, ElementBits, Cnt)) 10269 return false; 10270 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits); 10271 } 10272 10273 /// isVShiftRImm - Check if this is a valid build_vector for the immediate 10274 /// operand of a vector shift right operation. For a shift opcode, the value 10275 /// is positive, but for an intrinsic the value count must be negative. The 10276 /// absolute value must be in the range: 10277 /// 1 <= |Value| <= ElementBits for a right shift; or 10278 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift. 10279 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic, 10280 int64_t &Cnt) { 10281 assert(VT.isVector() && "vector shift count is not a vector type"); 10282 int64_t ElementBits = VT.getVectorElementType().getSizeInBits(); 10283 if (! getVShiftImm(Op, ElementBits, Cnt)) 10284 return false; 10285 if (!isIntrinsic) 10286 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits)); 10287 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) { 10288 Cnt = -Cnt; 10289 return true; 10290 } 10291 return false; 10292 } 10293 10294 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics. 10295 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) { 10296 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 10297 switch (IntNo) { 10298 default: 10299 // Don't do anything for most intrinsics. 10300 break; 10301 10302 // Vector shifts: check for immediate versions and lower them. 10303 // Note: This is done during DAG combining instead of DAG legalizing because 10304 // the build_vectors for 64-bit vector element shift counts are generally 10305 // not legal, and it is hard to see their values after they get legalized to 10306 // loads from a constant pool. 10307 case Intrinsic::arm_neon_vshifts: 10308 case Intrinsic::arm_neon_vshiftu: 10309 case Intrinsic::arm_neon_vrshifts: 10310 case Intrinsic::arm_neon_vrshiftu: 10311 case Intrinsic::arm_neon_vrshiftn: 10312 case Intrinsic::arm_neon_vqshifts: 10313 case Intrinsic::arm_neon_vqshiftu: 10314 case Intrinsic::arm_neon_vqshiftsu: 10315 case Intrinsic::arm_neon_vqshiftns: 10316 case Intrinsic::arm_neon_vqshiftnu: 10317 case Intrinsic::arm_neon_vqshiftnsu: 10318 case Intrinsic::arm_neon_vqrshiftns: 10319 case Intrinsic::arm_neon_vqrshiftnu: 10320 case Intrinsic::arm_neon_vqrshiftnsu: { 10321 EVT VT = N->getOperand(1).getValueType(); 10322 int64_t Cnt; 10323 unsigned VShiftOpc = 0; 10324 10325 switch (IntNo) { 10326 case Intrinsic::arm_neon_vshifts: 10327 case Intrinsic::arm_neon_vshiftu: 10328 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) { 10329 VShiftOpc = ARMISD::VSHL; 10330 break; 10331 } 10332 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) { 10333 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ? 10334 ARMISD::VSHRs : ARMISD::VSHRu); 10335 break; 10336 } 10337 return SDValue(); 10338 10339 case Intrinsic::arm_neon_vrshifts: 10340 case Intrinsic::arm_neon_vrshiftu: 10341 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) 10342 break; 10343 return SDValue(); 10344 10345 case Intrinsic::arm_neon_vqshifts: 10346 case Intrinsic::arm_neon_vqshiftu: 10347 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 10348 break; 10349 return SDValue(); 10350 10351 case Intrinsic::arm_neon_vqshiftsu: 10352 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) 10353 break; 10354 llvm_unreachable("invalid shift count for vqshlu intrinsic"); 10355 10356 case Intrinsic::arm_neon_vrshiftn: 10357 case Intrinsic::arm_neon_vqshiftns: 10358 case Intrinsic::arm_neon_vqshiftnu: 10359 case Intrinsic::arm_neon_vqshiftnsu: 10360 case Intrinsic::arm_neon_vqrshiftns: 10361 case Intrinsic::arm_neon_vqrshiftnu: 10362 case Intrinsic::arm_neon_vqrshiftnsu: 10363 // Narrowing shifts require an immediate right shift. 10364 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt)) 10365 break; 10366 llvm_unreachable("invalid shift count for narrowing vector shift " 10367 "intrinsic"); 10368 10369 default: 10370 llvm_unreachable("unhandled vector shift"); 10371 } 10372 10373 switch (IntNo) { 10374 case Intrinsic::arm_neon_vshifts: 10375 case Intrinsic::arm_neon_vshiftu: 10376 // Opcode already set above. 10377 break; 10378 case Intrinsic::arm_neon_vrshifts: 10379 VShiftOpc = ARMISD::VRSHRs; break; 10380 case Intrinsic::arm_neon_vrshiftu: 10381 VShiftOpc = ARMISD::VRSHRu; break; 10382 case Intrinsic::arm_neon_vrshiftn: 10383 VShiftOpc = ARMISD::VRSHRN; break; 10384 case Intrinsic::arm_neon_vqshifts: 10385 VShiftOpc = ARMISD::VQSHLs; break; 10386 case Intrinsic::arm_neon_vqshiftu: 10387 VShiftOpc = ARMISD::VQSHLu; break; 10388 case Intrinsic::arm_neon_vqshiftsu: 10389 VShiftOpc = ARMISD::VQSHLsu; break; 10390 case Intrinsic::arm_neon_vqshiftns: 10391 VShiftOpc = ARMISD::VQSHRNs; break; 10392 case Intrinsic::arm_neon_vqshiftnu: 10393 VShiftOpc = ARMISD::VQSHRNu; break; 10394 case Intrinsic::arm_neon_vqshiftnsu: 10395 VShiftOpc = ARMISD::VQSHRNsu; break; 10396 case Intrinsic::arm_neon_vqrshiftns: 10397 VShiftOpc = ARMISD::VQRSHRNs; break; 10398 case Intrinsic::arm_neon_vqrshiftnu: 10399 VShiftOpc = ARMISD::VQRSHRNu; break; 10400 case Intrinsic::arm_neon_vqrshiftnsu: 10401 VShiftOpc = ARMISD::VQRSHRNsu; break; 10402 } 10403 10404 SDLoc dl(N); 10405 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 10406 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32)); 10407 } 10408 10409 case Intrinsic::arm_neon_vshiftins: { 10410 EVT VT = N->getOperand(1).getValueType(); 10411 int64_t Cnt; 10412 unsigned VShiftOpc = 0; 10413 10414 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt)) 10415 VShiftOpc = ARMISD::VSLI; 10416 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt)) 10417 VShiftOpc = ARMISD::VSRI; 10418 else { 10419 llvm_unreachable("invalid shift count for vsli/vsri intrinsic"); 10420 } 10421 10422 SDLoc dl(N); 10423 return DAG.getNode(VShiftOpc, dl, N->getValueType(0), 10424 N->getOperand(1), N->getOperand(2), 10425 DAG.getConstant(Cnt, dl, MVT::i32)); 10426 } 10427 10428 case Intrinsic::arm_neon_vqrshifts: 10429 case Intrinsic::arm_neon_vqrshiftu: 10430 // No immediate versions of these to check for. 10431 break; 10432 } 10433 10434 return SDValue(); 10435 } 10436 10437 /// PerformShiftCombine - Checks for immediate versions of vector shifts and 10438 /// lowers them. As with the vector shift intrinsics, this is done during DAG 10439 /// combining instead of DAG legalizing because the build_vectors for 64-bit 10440 /// vector element shift counts are generally not legal, and it is hard to see 10441 /// their values after they get legalized to loads from a constant pool. 10442 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG, 10443 const ARMSubtarget *ST) { 10444 EVT VT = N->getValueType(0); 10445 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) { 10446 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high 10447 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16. 10448 SDValue N1 = N->getOperand(1); 10449 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { 10450 SDValue N0 = N->getOperand(0); 10451 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP && 10452 DAG.MaskedValueIsZero(N0.getOperand(0), 10453 APInt::getHighBitsSet(32, 16))) 10454 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1); 10455 } 10456 } 10457 10458 // Nothing to be done for scalar shifts. 10459 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 10460 if (!VT.isVector() || !TLI.isTypeLegal(VT)) 10461 return SDValue(); 10462 10463 assert(ST->hasNEON() && "unexpected vector shift"); 10464 int64_t Cnt; 10465 10466 switch (N->getOpcode()) { 10467 default: llvm_unreachable("unexpected shift opcode"); 10468 10469 case ISD::SHL: 10470 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) { 10471 SDLoc dl(N); 10472 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0), 10473 DAG.getConstant(Cnt, dl, MVT::i32)); 10474 } 10475 break; 10476 10477 case ISD::SRA: 10478 case ISD::SRL: 10479 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) { 10480 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ? 10481 ARMISD::VSHRs : ARMISD::VSHRu); 10482 SDLoc dl(N); 10483 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0), 10484 DAG.getConstant(Cnt, dl, MVT::i32)); 10485 } 10486 } 10487 return SDValue(); 10488 } 10489 10490 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND, 10491 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND. 10492 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG, 10493 const ARMSubtarget *ST) { 10494 SDValue N0 = N->getOperand(0); 10495 10496 // Check for sign- and zero-extensions of vector extract operations of 8- 10497 // and 16-bit vector elements. NEON supports these directly. They are 10498 // handled during DAG combining because type legalization will promote them 10499 // to 32-bit types and it is messy to recognize the operations after that. 10500 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { 10501 SDValue Vec = N0.getOperand(0); 10502 SDValue Lane = N0.getOperand(1); 10503 EVT VT = N->getValueType(0); 10504 EVT EltVT = N0.getValueType(); 10505 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 10506 10507 if (VT == MVT::i32 && 10508 (EltVT == MVT::i8 || EltVT == MVT::i16) && 10509 TLI.isTypeLegal(Vec.getValueType()) && 10510 isa<ConstantSDNode>(Lane)) { 10511 10512 unsigned Opc = 0; 10513 switch (N->getOpcode()) { 10514 default: llvm_unreachable("unexpected opcode"); 10515 case ISD::SIGN_EXTEND: 10516 Opc = ARMISD::VGETLANEs; 10517 break; 10518 case ISD::ZERO_EXTEND: 10519 case ISD::ANY_EXTEND: 10520 Opc = ARMISD::VGETLANEu; 10521 break; 10522 } 10523 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane); 10524 } 10525 } 10526 10527 return SDValue(); 10528 } 10529 10530 static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero, 10531 APInt &KnownOne) { 10532 if (Op.getOpcode() == ARMISD::BFI) { 10533 // Conservatively, we can recurse down the first operand 10534 // and just mask out all affected bits. 10535 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne); 10536 10537 // The operand to BFI is already a mask suitable for removing the bits it 10538 // sets. 10539 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2)); 10540 APInt Mask = CI->getAPIntValue(); 10541 KnownZero &= Mask; 10542 KnownOne &= Mask; 10543 return; 10544 } 10545 if (Op.getOpcode() == ARMISD::CMOV) { 10546 APInt KZ2(KnownZero.getBitWidth(), 0); 10547 APInt KO2(KnownOne.getBitWidth(), 0); 10548 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne); 10549 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2); 10550 10551 KnownZero &= KZ2; 10552 KnownOne &= KO2; 10553 return; 10554 } 10555 return DAG.computeKnownBits(Op, KnownZero, KnownOne); 10556 } 10557 10558 SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const { 10559 // If we have a CMOV, OR and AND combination such as: 10560 // if (x & CN) 10561 // y |= CM; 10562 // 10563 // And: 10564 // * CN is a single bit; 10565 // * All bits covered by CM are known zero in y 10566 // 10567 // Then we can convert this into a sequence of BFI instructions. This will 10568 // always be a win if CM is a single bit, will always be no worse than the 10569 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is 10570 // three bits (due to the extra IT instruction). 10571 10572 SDValue Op0 = CMOV->getOperand(0); 10573 SDValue Op1 = CMOV->getOperand(1); 10574 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2)); 10575 auto CC = CCNode->getAPIntValue().getLimitedValue(); 10576 SDValue CmpZ = CMOV->getOperand(4); 10577 10578 // The compare must be against zero. 10579 if (!isNullConstant(CmpZ->getOperand(1))) 10580 return SDValue(); 10581 10582 assert(CmpZ->getOpcode() == ARMISD::CMPZ); 10583 SDValue And = CmpZ->getOperand(0); 10584 if (And->getOpcode() != ISD::AND) 10585 return SDValue(); 10586 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1)); 10587 if (!AndC || !AndC->getAPIntValue().isPowerOf2()) 10588 return SDValue(); 10589 SDValue X = And->getOperand(0); 10590 10591 if (CC == ARMCC::EQ) { 10592 // We're performing an "equal to zero" compare. Swap the operands so we 10593 // canonicalize on a "not equal to zero" compare. 10594 std::swap(Op0, Op1); 10595 } else { 10596 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?"); 10597 } 10598 10599 if (Op1->getOpcode() != ISD::OR) 10600 return SDValue(); 10601 10602 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1)); 10603 if (!OrC) 10604 return SDValue(); 10605 SDValue Y = Op1->getOperand(0); 10606 10607 if (Op0 != Y) 10608 return SDValue(); 10609 10610 // Now, is it profitable to continue? 10611 APInt OrCI = OrC->getAPIntValue(); 10612 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2; 10613 if (OrCI.countPopulation() > Heuristic) 10614 return SDValue(); 10615 10616 // Lastly, can we determine that the bits defined by OrCI 10617 // are zero in Y? 10618 APInt KnownZero, KnownOne; 10619 computeKnownBits(DAG, Y, KnownZero, KnownOne); 10620 if ((OrCI & KnownZero) != OrCI) 10621 return SDValue(); 10622 10623 // OK, we can do the combine. 10624 SDValue V = Y; 10625 SDLoc dl(X); 10626 EVT VT = X.getValueType(); 10627 unsigned BitInX = AndC->getAPIntValue().logBase2(); 10628 10629 if (BitInX != 0) { 10630 // We must shift X first. 10631 X = DAG.getNode(ISD::SRL, dl, VT, X, 10632 DAG.getConstant(BitInX, dl, VT)); 10633 } 10634 10635 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits(); 10636 BitInY < NumActiveBits; ++BitInY) { 10637 if (OrCI[BitInY] == 0) 10638 continue; 10639 APInt Mask(VT.getSizeInBits(), 0); 10640 Mask.setBit(BitInY); 10641 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X, 10642 // Confusingly, the operand is an *inverted* mask. 10643 DAG.getConstant(~Mask, dl, VT)); 10644 } 10645 10646 return V; 10647 } 10648 10649 /// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND. 10650 SDValue 10651 ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const { 10652 SDValue Cmp = N->getOperand(4); 10653 if (Cmp.getOpcode() != ARMISD::CMPZ) 10654 // Only looking at NE cases. 10655 return SDValue(); 10656 10657 EVT VT = N->getValueType(0); 10658 SDLoc dl(N); 10659 SDValue LHS = Cmp.getOperand(0); 10660 SDValue RHS = Cmp.getOperand(1); 10661 SDValue Chain = N->getOperand(0); 10662 SDValue BB = N->getOperand(1); 10663 SDValue ARMcc = N->getOperand(2); 10664 ARMCC::CondCodes CC = 10665 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 10666 10667 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0)) 10668 // -> (brcond Chain BB CC CPSR Cmp) 10669 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() && 10670 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV && 10671 LHS->getOperand(0)->hasOneUse()) { 10672 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0)); 10673 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1)); 10674 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); 10675 auto *RHSC = dyn_cast<ConstantSDNode>(RHS); 10676 if ((LHS00C && LHS00C->getZExtValue() == 0) && 10677 (LHS01C && LHS01C->getZExtValue() == 1) && 10678 (LHS1C && LHS1C->getZExtValue() == 1) && 10679 (RHSC && RHSC->getZExtValue() == 0)) { 10680 return DAG.getNode( 10681 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2), 10682 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4)); 10683 } 10684 } 10685 10686 return SDValue(); 10687 } 10688 10689 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV. 10690 SDValue 10691 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const { 10692 SDValue Cmp = N->getOperand(4); 10693 if (Cmp.getOpcode() != ARMISD::CMPZ) 10694 // Only looking at EQ and NE cases. 10695 return SDValue(); 10696 10697 EVT VT = N->getValueType(0); 10698 SDLoc dl(N); 10699 SDValue LHS = Cmp.getOperand(0); 10700 SDValue RHS = Cmp.getOperand(1); 10701 SDValue FalseVal = N->getOperand(0); 10702 SDValue TrueVal = N->getOperand(1); 10703 SDValue ARMcc = N->getOperand(2); 10704 ARMCC::CondCodes CC = 10705 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue(); 10706 10707 // BFI is only available on V6T2+. 10708 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) { 10709 SDValue R = PerformCMOVToBFICombine(N, DAG); 10710 if (R) 10711 return R; 10712 } 10713 10714 // Simplify 10715 // mov r1, r0 10716 // cmp r1, x 10717 // mov r0, y 10718 // moveq r0, x 10719 // to 10720 // cmp r0, x 10721 // movne r0, y 10722 // 10723 // mov r1, r0 10724 // cmp r1, x 10725 // mov r0, x 10726 // movne r0, y 10727 // to 10728 // cmp r0, x 10729 // movne r0, y 10730 /// FIXME: Turn this into a target neutral optimization? 10731 SDValue Res; 10732 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) { 10733 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc, 10734 N->getOperand(3), Cmp); 10735 } else if (CC == ARMCC::EQ && TrueVal == RHS) { 10736 SDValue ARMcc; 10737 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl); 10738 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc, 10739 N->getOperand(3), NewCmp); 10740 } 10741 10742 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0)) 10743 // -> (cmov F T CC CPSR Cmp) 10744 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) { 10745 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)); 10746 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1)); 10747 auto *RHSC = dyn_cast<ConstantSDNode>(RHS); 10748 if ((LHS0C && LHS0C->getZExtValue() == 0) && 10749 (LHS1C && LHS1C->getZExtValue() == 1) && 10750 (RHSC && RHSC->getZExtValue() == 0)) { 10751 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, 10752 LHS->getOperand(2), LHS->getOperand(3), 10753 LHS->getOperand(4)); 10754 } 10755 } 10756 10757 if (Res.getNode()) { 10758 APInt KnownZero, KnownOne; 10759 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne); 10760 // Capture demanded bits information that would be otherwise lost. 10761 if (KnownZero == 0xfffffffe) 10762 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 10763 DAG.getValueType(MVT::i1)); 10764 else if (KnownZero == 0xffffff00) 10765 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 10766 DAG.getValueType(MVT::i8)); 10767 else if (KnownZero == 0xffff0000) 10768 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res, 10769 DAG.getValueType(MVT::i16)); 10770 } 10771 10772 return Res; 10773 } 10774 10775 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N, 10776 DAGCombinerInfo &DCI) const { 10777 switch (N->getOpcode()) { 10778 default: break; 10779 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget); 10780 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget); 10781 case ISD::SUB: return PerformSUBCombine(N, DCI); 10782 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget); 10783 case ISD::OR: return PerformORCombine(N, DCI, Subtarget); 10784 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget); 10785 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget); 10786 case ARMISD::BFI: return PerformBFICombine(N, DCI); 10787 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget); 10788 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG); 10789 case ISD::STORE: return PerformSTORECombine(N, DCI); 10790 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget); 10791 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI); 10792 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG); 10793 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI); 10794 case ISD::FP_TO_SINT: 10795 case ISD::FP_TO_UINT: 10796 return PerformVCVTCombine(N, DCI.DAG, Subtarget); 10797 case ISD::FDIV: 10798 return PerformVDIVCombine(N, DCI.DAG, Subtarget); 10799 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG); 10800 case ISD::SHL: 10801 case ISD::SRA: 10802 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget); 10803 case ISD::SIGN_EXTEND: 10804 case ISD::ZERO_EXTEND: 10805 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget); 10806 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG); 10807 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG); 10808 case ISD::LOAD: return PerformLOADCombine(N, DCI); 10809 case ARMISD::VLD2DUP: 10810 case ARMISD::VLD3DUP: 10811 case ARMISD::VLD4DUP: 10812 return PerformVLDCombine(N, DCI); 10813 case ARMISD::BUILD_VECTOR: 10814 return PerformARMBUILD_VECTORCombine(N, DCI); 10815 case ISD::INTRINSIC_VOID: 10816 case ISD::INTRINSIC_W_CHAIN: 10817 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) { 10818 case Intrinsic::arm_neon_vld1: 10819 case Intrinsic::arm_neon_vld2: 10820 case Intrinsic::arm_neon_vld3: 10821 case Intrinsic::arm_neon_vld4: 10822 case Intrinsic::arm_neon_vld2lane: 10823 case Intrinsic::arm_neon_vld3lane: 10824 case Intrinsic::arm_neon_vld4lane: 10825 case Intrinsic::arm_neon_vst1: 10826 case Intrinsic::arm_neon_vst2: 10827 case Intrinsic::arm_neon_vst3: 10828 case Intrinsic::arm_neon_vst4: 10829 case Intrinsic::arm_neon_vst2lane: 10830 case Intrinsic::arm_neon_vst3lane: 10831 case Intrinsic::arm_neon_vst4lane: 10832 return PerformVLDCombine(N, DCI); 10833 default: break; 10834 } 10835 break; 10836 } 10837 return SDValue(); 10838 } 10839 10840 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc, 10841 EVT VT) const { 10842 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE); 10843 } 10844 10845 bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 10846 unsigned, 10847 unsigned, 10848 bool *Fast) const { 10849 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus 10850 bool AllowsUnaligned = Subtarget->allowsUnalignedMem(); 10851 10852 switch (VT.getSimpleVT().SimpleTy) { 10853 default: 10854 return false; 10855 case MVT::i8: 10856 case MVT::i16: 10857 case MVT::i32: { 10858 // Unaligned access can use (for example) LRDB, LRDH, LDR 10859 if (AllowsUnaligned) { 10860 if (Fast) 10861 *Fast = Subtarget->hasV7Ops(); 10862 return true; 10863 } 10864 return false; 10865 } 10866 case MVT::f64: 10867 case MVT::v2f64: { 10868 // For any little-endian targets with neon, we can support unaligned ld/st 10869 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8. 10870 // A big-endian target may also explicitly support unaligned accesses 10871 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) { 10872 if (Fast) 10873 *Fast = true; 10874 return true; 10875 } 10876 return false; 10877 } 10878 } 10879 } 10880 10881 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign, 10882 unsigned AlignCheck) { 10883 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) && 10884 (DstAlign == 0 || DstAlign % AlignCheck == 0)); 10885 } 10886 10887 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size, 10888 unsigned DstAlign, unsigned SrcAlign, 10889 bool IsMemset, bool ZeroMemset, 10890 bool MemcpyStrSrc, 10891 MachineFunction &MF) const { 10892 const Function *F = MF.getFunction(); 10893 10894 // See if we can use NEON instructions for this... 10895 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() && 10896 !F->hasFnAttribute(Attribute::NoImplicitFloat)) { 10897 bool Fast; 10898 if (Size >= 16 && 10899 (memOpAlign(SrcAlign, DstAlign, 16) || 10900 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) { 10901 return MVT::v2f64; 10902 } else if (Size >= 8 && 10903 (memOpAlign(SrcAlign, DstAlign, 8) || 10904 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) && 10905 Fast))) { 10906 return MVT::f64; 10907 } 10908 } 10909 10910 // Lowering to i32/i16 if the size permits. 10911 if (Size >= 4) 10912 return MVT::i32; 10913 else if (Size >= 2) 10914 return MVT::i16; 10915 10916 // Let the target-independent logic figure it out. 10917 return MVT::Other; 10918 } 10919 10920 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { 10921 if (Val.getOpcode() != ISD::LOAD) 10922 return false; 10923 10924 EVT VT1 = Val.getValueType(); 10925 if (!VT1.isSimple() || !VT1.isInteger() || 10926 !VT2.isSimple() || !VT2.isInteger()) 10927 return false; 10928 10929 switch (VT1.getSimpleVT().SimpleTy) { 10930 default: break; 10931 case MVT::i1: 10932 case MVT::i8: 10933 case MVT::i16: 10934 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits. 10935 return true; 10936 } 10937 10938 return false; 10939 } 10940 10941 bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const { 10942 EVT VT = ExtVal.getValueType(); 10943 10944 if (!isTypeLegal(VT)) 10945 return false; 10946 10947 // Don't create a loadext if we can fold the extension into a wide/long 10948 // instruction. 10949 // If there's more than one user instruction, the loadext is desirable no 10950 // matter what. There can be two uses by the same instruction. 10951 if (ExtVal->use_empty() || 10952 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode())) 10953 return true; 10954 10955 SDNode *U = *ExtVal->use_begin(); 10956 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB || 10957 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL)) 10958 return false; 10959 10960 return true; 10961 } 10962 10963 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const { 10964 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) 10965 return false; 10966 10967 if (!isTypeLegal(EVT::getEVT(Ty1))) 10968 return false; 10969 10970 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop"); 10971 10972 // Assuming the caller doesn't have a zeroext or signext return parameter, 10973 // truncation all the way down to i1 is valid. 10974 return true; 10975 } 10976 10977 10978 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) { 10979 if (V < 0) 10980 return false; 10981 10982 unsigned Scale = 1; 10983 switch (VT.getSimpleVT().SimpleTy) { 10984 default: return false; 10985 case MVT::i1: 10986 case MVT::i8: 10987 // Scale == 1; 10988 break; 10989 case MVT::i16: 10990 // Scale == 2; 10991 Scale = 2; 10992 break; 10993 case MVT::i32: 10994 // Scale == 4; 10995 Scale = 4; 10996 break; 10997 } 10998 10999 if ((V & (Scale - 1)) != 0) 11000 return false; 11001 V /= Scale; 11002 return V == (V & ((1LL << 5) - 1)); 11003 } 11004 11005 static bool isLegalT2AddressImmediate(int64_t V, EVT VT, 11006 const ARMSubtarget *Subtarget) { 11007 bool isNeg = false; 11008 if (V < 0) { 11009 isNeg = true; 11010 V = - V; 11011 } 11012 11013 switch (VT.getSimpleVT().SimpleTy) { 11014 default: return false; 11015 case MVT::i1: 11016 case MVT::i8: 11017 case MVT::i16: 11018 case MVT::i32: 11019 // + imm12 or - imm8 11020 if (isNeg) 11021 return V == (V & ((1LL << 8) - 1)); 11022 return V == (V & ((1LL << 12) - 1)); 11023 case MVT::f32: 11024 case MVT::f64: 11025 // Same as ARM mode. FIXME: NEON? 11026 if (!Subtarget->hasVFP2()) 11027 return false; 11028 if ((V & 3) != 0) 11029 return false; 11030 V >>= 2; 11031 return V == (V & ((1LL << 8) - 1)); 11032 } 11033 } 11034 11035 /// isLegalAddressImmediate - Return true if the integer value can be used 11036 /// as the offset of the target addressing mode for load / store of the 11037 /// given type. 11038 static bool isLegalAddressImmediate(int64_t V, EVT VT, 11039 const ARMSubtarget *Subtarget) { 11040 if (V == 0) 11041 return true; 11042 11043 if (!VT.isSimple()) 11044 return false; 11045 11046 if (Subtarget->isThumb1Only()) 11047 return isLegalT1AddressImmediate(V, VT); 11048 else if (Subtarget->isThumb2()) 11049 return isLegalT2AddressImmediate(V, VT, Subtarget); 11050 11051 // ARM mode. 11052 if (V < 0) 11053 V = - V; 11054 switch (VT.getSimpleVT().SimpleTy) { 11055 default: return false; 11056 case MVT::i1: 11057 case MVT::i8: 11058 case MVT::i32: 11059 // +- imm12 11060 return V == (V & ((1LL << 12) - 1)); 11061 case MVT::i16: 11062 // +- imm8 11063 return V == (V & ((1LL << 8) - 1)); 11064 case MVT::f32: 11065 case MVT::f64: 11066 if (!Subtarget->hasVFP2()) // FIXME: NEON? 11067 return false; 11068 if ((V & 3) != 0) 11069 return false; 11070 V >>= 2; 11071 return V == (V & ((1LL << 8) - 1)); 11072 } 11073 } 11074 11075 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM, 11076 EVT VT) const { 11077 int Scale = AM.Scale; 11078 if (Scale < 0) 11079 return false; 11080 11081 switch (VT.getSimpleVT().SimpleTy) { 11082 default: return false; 11083 case MVT::i1: 11084 case MVT::i8: 11085 case MVT::i16: 11086 case MVT::i32: 11087 if (Scale == 1) 11088 return true; 11089 // r + r << imm 11090 Scale = Scale & ~1; 11091 return Scale == 2 || Scale == 4 || Scale == 8; 11092 case MVT::i64: 11093 // r + r 11094 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 11095 return true; 11096 return false; 11097 case MVT::isVoid: 11098 // Note, we allow "void" uses (basically, uses that aren't loads or 11099 // stores), because arm allows folding a scale into many arithmetic 11100 // operations. This should be made more precise and revisited later. 11101 11102 // Allow r << imm, but the imm has to be a multiple of two. 11103 if (Scale & 1) return false; 11104 return isPowerOf2_32(Scale); 11105 } 11106 } 11107 11108 /// isLegalAddressingMode - Return true if the addressing mode represented 11109 /// by AM is legal for this target, for a load/store of the specified type. 11110 bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL, 11111 const AddrMode &AM, Type *Ty, 11112 unsigned AS) const { 11113 EVT VT = getValueType(DL, Ty, true); 11114 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget)) 11115 return false; 11116 11117 // Can never fold addr of global into load/store. 11118 if (AM.BaseGV) 11119 return false; 11120 11121 switch (AM.Scale) { 11122 case 0: // no scale reg, must be "r+i" or "r", or "i". 11123 break; 11124 case 1: 11125 if (Subtarget->isThumb1Only()) 11126 return false; 11127 // FALL THROUGH. 11128 default: 11129 // ARM doesn't support any R+R*scale+imm addr modes. 11130 if (AM.BaseOffs) 11131 return false; 11132 11133 if (!VT.isSimple()) 11134 return false; 11135 11136 if (Subtarget->isThumb2()) 11137 return isLegalT2ScaledAddressingMode(AM, VT); 11138 11139 int Scale = AM.Scale; 11140 switch (VT.getSimpleVT().SimpleTy) { 11141 default: return false; 11142 case MVT::i1: 11143 case MVT::i8: 11144 case MVT::i32: 11145 if (Scale < 0) Scale = -Scale; 11146 if (Scale == 1) 11147 return true; 11148 // r + r << imm 11149 return isPowerOf2_32(Scale & ~1); 11150 case MVT::i16: 11151 case MVT::i64: 11152 // r + r 11153 if (((unsigned)AM.HasBaseReg + Scale) <= 2) 11154 return true; 11155 return false; 11156 11157 case MVT::isVoid: 11158 // Note, we allow "void" uses (basically, uses that aren't loads or 11159 // stores), because arm allows folding a scale into many arithmetic 11160 // operations. This should be made more precise and revisited later. 11161 11162 // Allow r << imm, but the imm has to be a multiple of two. 11163 if (Scale & 1) return false; 11164 return isPowerOf2_32(Scale); 11165 } 11166 } 11167 return true; 11168 } 11169 11170 /// isLegalICmpImmediate - Return true if the specified immediate is legal 11171 /// icmp immediate, that is the target has icmp instructions which can compare 11172 /// a register against the immediate without having to materialize the 11173 /// immediate into a register. 11174 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const { 11175 // Thumb2 and ARM modes can use cmn for negative immediates. 11176 if (!Subtarget->isThumb()) 11177 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1; 11178 if (Subtarget->isThumb2()) 11179 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1; 11180 // Thumb1 doesn't have cmn, and only 8-bit immediates. 11181 return Imm >= 0 && Imm <= 255; 11182 } 11183 11184 /// isLegalAddImmediate - Return true if the specified immediate is a legal add 11185 /// *or sub* immediate, that is the target has add or sub instructions which can 11186 /// add a register with the immediate without having to materialize the 11187 /// immediate into a register. 11188 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const { 11189 // Same encoding for add/sub, just flip the sign. 11190 int64_t AbsImm = std::abs(Imm); 11191 if (!Subtarget->isThumb()) 11192 return ARM_AM::getSOImmVal(AbsImm) != -1; 11193 if (Subtarget->isThumb2()) 11194 return ARM_AM::getT2SOImmVal(AbsImm) != -1; 11195 // Thumb1 only has 8-bit unsigned immediate. 11196 return AbsImm >= 0 && AbsImm <= 255; 11197 } 11198 11199 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT, 11200 bool isSEXTLoad, SDValue &Base, 11201 SDValue &Offset, bool &isInc, 11202 SelectionDAG &DAG) { 11203 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 11204 return false; 11205 11206 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) { 11207 // AddressingMode 3 11208 Base = Ptr->getOperand(0); 11209 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 11210 int RHSC = (int)RHS->getZExtValue(); 11211 if (RHSC < 0 && RHSC > -256) { 11212 assert(Ptr->getOpcode() == ISD::ADD); 11213 isInc = false; 11214 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 11215 return true; 11216 } 11217 } 11218 isInc = (Ptr->getOpcode() == ISD::ADD); 11219 Offset = Ptr->getOperand(1); 11220 return true; 11221 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) { 11222 // AddressingMode 2 11223 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 11224 int RHSC = (int)RHS->getZExtValue(); 11225 if (RHSC < 0 && RHSC > -0x1000) { 11226 assert(Ptr->getOpcode() == ISD::ADD); 11227 isInc = false; 11228 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 11229 Base = Ptr->getOperand(0); 11230 return true; 11231 } 11232 } 11233 11234 if (Ptr->getOpcode() == ISD::ADD) { 11235 isInc = true; 11236 ARM_AM::ShiftOpc ShOpcVal= 11237 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode()); 11238 if (ShOpcVal != ARM_AM::no_shift) { 11239 Base = Ptr->getOperand(1); 11240 Offset = Ptr->getOperand(0); 11241 } else { 11242 Base = Ptr->getOperand(0); 11243 Offset = Ptr->getOperand(1); 11244 } 11245 return true; 11246 } 11247 11248 isInc = (Ptr->getOpcode() == ISD::ADD); 11249 Base = Ptr->getOperand(0); 11250 Offset = Ptr->getOperand(1); 11251 return true; 11252 } 11253 11254 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store. 11255 return false; 11256 } 11257 11258 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT, 11259 bool isSEXTLoad, SDValue &Base, 11260 SDValue &Offset, bool &isInc, 11261 SelectionDAG &DAG) { 11262 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB) 11263 return false; 11264 11265 Base = Ptr->getOperand(0); 11266 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) { 11267 int RHSC = (int)RHS->getZExtValue(); 11268 if (RHSC < 0 && RHSC > -0x100) { // 8 bits. 11269 assert(Ptr->getOpcode() == ISD::ADD); 11270 isInc = false; 11271 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0)); 11272 return true; 11273 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero. 11274 isInc = Ptr->getOpcode() == ISD::ADD; 11275 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0)); 11276 return true; 11277 } 11278 } 11279 11280 return false; 11281 } 11282 11283 /// getPreIndexedAddressParts - returns true by value, base pointer and 11284 /// offset pointer and addressing mode by reference if the node's address 11285 /// can be legally represented as pre-indexed load / store address. 11286 bool 11287 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, 11288 SDValue &Offset, 11289 ISD::MemIndexedMode &AM, 11290 SelectionDAG &DAG) const { 11291 if (Subtarget->isThumb1Only()) 11292 return false; 11293 11294 EVT VT; 11295 SDValue Ptr; 11296 bool isSEXTLoad = false; 11297 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 11298 Ptr = LD->getBasePtr(); 11299 VT = LD->getMemoryVT(); 11300 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 11301 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 11302 Ptr = ST->getBasePtr(); 11303 VT = ST->getMemoryVT(); 11304 } else 11305 return false; 11306 11307 bool isInc; 11308 bool isLegal = false; 11309 if (Subtarget->isThumb2()) 11310 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 11311 Offset, isInc, DAG); 11312 else 11313 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, 11314 Offset, isInc, DAG); 11315 if (!isLegal) 11316 return false; 11317 11318 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC; 11319 return true; 11320 } 11321 11322 /// getPostIndexedAddressParts - returns true by value, base pointer and 11323 /// offset pointer and addressing mode by reference if this node can be 11324 /// combined with a load / store to form a post-indexed load / store. 11325 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op, 11326 SDValue &Base, 11327 SDValue &Offset, 11328 ISD::MemIndexedMode &AM, 11329 SelectionDAG &DAG) const { 11330 if (Subtarget->isThumb1Only()) 11331 return false; 11332 11333 EVT VT; 11334 SDValue Ptr; 11335 bool isSEXTLoad = false; 11336 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { 11337 VT = LD->getMemoryVT(); 11338 Ptr = LD->getBasePtr(); 11339 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD; 11340 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { 11341 VT = ST->getMemoryVT(); 11342 Ptr = ST->getBasePtr(); 11343 } else 11344 return false; 11345 11346 bool isInc; 11347 bool isLegal = false; 11348 if (Subtarget->isThumb2()) 11349 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 11350 isInc, DAG); 11351 else 11352 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset, 11353 isInc, DAG); 11354 if (!isLegal) 11355 return false; 11356 11357 if (Ptr != Base) { 11358 // Swap base ptr and offset to catch more post-index load / store when 11359 // it's legal. In Thumb2 mode, offset must be an immediate. 11360 if (Ptr == Offset && Op->getOpcode() == ISD::ADD && 11361 !Subtarget->isThumb2()) 11362 std::swap(Base, Offset); 11363 11364 // Post-indexed load / store update the base pointer. 11365 if (Ptr != Base) 11366 return false; 11367 } 11368 11369 AM = isInc ? ISD::POST_INC : ISD::POST_DEC; 11370 return true; 11371 } 11372 11373 void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, 11374 APInt &KnownZero, 11375 APInt &KnownOne, 11376 const SelectionDAG &DAG, 11377 unsigned Depth) const { 11378 unsigned BitWidth = KnownOne.getBitWidth(); 11379 KnownZero = KnownOne = APInt(BitWidth, 0); 11380 switch (Op.getOpcode()) { 11381 default: break; 11382 case ARMISD::ADDC: 11383 case ARMISD::ADDE: 11384 case ARMISD::SUBC: 11385 case ARMISD::SUBE: 11386 // These nodes' second result is a boolean 11387 if (Op.getResNo() == 0) 11388 break; 11389 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); 11390 break; 11391 case ARMISD::CMOV: { 11392 // Bits are known zero/one if known on the LHS and RHS. 11393 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); 11394 if (KnownZero == 0 && KnownOne == 0) return; 11395 11396 APInt KnownZeroRHS, KnownOneRHS; 11397 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1); 11398 KnownZero &= KnownZeroRHS; 11399 KnownOne &= KnownOneRHS; 11400 return; 11401 } 11402 case ISD::INTRINSIC_W_CHAIN: { 11403 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1)); 11404 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue()); 11405 switch (IntID) { 11406 default: return; 11407 case Intrinsic::arm_ldaex: 11408 case Intrinsic::arm_ldrex: { 11409 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT(); 11410 unsigned MemBits = VT.getScalarType().getSizeInBits(); 11411 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); 11412 return; 11413 } 11414 } 11415 } 11416 } 11417 } 11418 11419 //===----------------------------------------------------------------------===// 11420 // ARM Inline Assembly Support 11421 //===----------------------------------------------------------------------===// 11422 11423 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const { 11424 // Looking for "rev" which is V6+. 11425 if (!Subtarget->hasV6Ops()) 11426 return false; 11427 11428 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); 11429 std::string AsmStr = IA->getAsmString(); 11430 SmallVector<StringRef, 4> AsmPieces; 11431 SplitString(AsmStr, AsmPieces, ";\n"); 11432 11433 switch (AsmPieces.size()) { 11434 default: return false; 11435 case 1: 11436 AsmStr = AsmPieces[0]; 11437 AsmPieces.clear(); 11438 SplitString(AsmStr, AsmPieces, " \t,"); 11439 11440 // rev $0, $1 11441 if (AsmPieces.size() == 3 && 11442 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" && 11443 IA->getConstraintString().compare(0, 4, "=l,l") == 0) { 11444 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); 11445 if (Ty && Ty->getBitWidth() == 32) 11446 return IntrinsicLowering::LowerToByteSwap(CI); 11447 } 11448 break; 11449 } 11450 11451 return false; 11452 } 11453 11454 /// getConstraintType - Given a constraint letter, return the type of 11455 /// constraint it is for this target. 11456 ARMTargetLowering::ConstraintType 11457 ARMTargetLowering::getConstraintType(StringRef Constraint) const { 11458 if (Constraint.size() == 1) { 11459 switch (Constraint[0]) { 11460 default: break; 11461 case 'l': return C_RegisterClass; 11462 case 'w': return C_RegisterClass; 11463 case 'h': return C_RegisterClass; 11464 case 'x': return C_RegisterClass; 11465 case 't': return C_RegisterClass; 11466 case 'j': return C_Other; // Constant for movw. 11467 // An address with a single base register. Due to the way we 11468 // currently handle addresses it is the same as an 'r' memory constraint. 11469 case 'Q': return C_Memory; 11470 } 11471 } else if (Constraint.size() == 2) { 11472 switch (Constraint[0]) { 11473 default: break; 11474 // All 'U+' constraints are addresses. 11475 case 'U': return C_Memory; 11476 } 11477 } 11478 return TargetLowering::getConstraintType(Constraint); 11479 } 11480 11481 /// Examine constraint type and operand type and determine a weight value. 11482 /// This object must already have been set up with the operand type 11483 /// and the current alternative constraint selected. 11484 TargetLowering::ConstraintWeight 11485 ARMTargetLowering::getSingleConstraintMatchWeight( 11486 AsmOperandInfo &info, const char *constraint) const { 11487 ConstraintWeight weight = CW_Invalid; 11488 Value *CallOperandVal = info.CallOperandVal; 11489 // If we don't have a value, we can't do a match, 11490 // but allow it at the lowest weight. 11491 if (!CallOperandVal) 11492 return CW_Default; 11493 Type *type = CallOperandVal->getType(); 11494 // Look at the constraint type. 11495 switch (*constraint) { 11496 default: 11497 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 11498 break; 11499 case 'l': 11500 if (type->isIntegerTy()) { 11501 if (Subtarget->isThumb()) 11502 weight = CW_SpecificReg; 11503 else 11504 weight = CW_Register; 11505 } 11506 break; 11507 case 'w': 11508 if (type->isFloatingPointTy()) 11509 weight = CW_Register; 11510 break; 11511 } 11512 return weight; 11513 } 11514 11515 typedef std::pair<unsigned, const TargetRegisterClass*> RCPair; 11516 RCPair ARMTargetLowering::getRegForInlineAsmConstraint( 11517 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { 11518 if (Constraint.size() == 1) { 11519 // GCC ARM Constraint Letters 11520 switch (Constraint[0]) { 11521 case 'l': // Low regs or general regs. 11522 if (Subtarget->isThumb()) 11523 return RCPair(0U, &ARM::tGPRRegClass); 11524 return RCPair(0U, &ARM::GPRRegClass); 11525 case 'h': // High regs or no regs. 11526 if (Subtarget->isThumb()) 11527 return RCPair(0U, &ARM::hGPRRegClass); 11528 break; 11529 case 'r': 11530 if (Subtarget->isThumb1Only()) 11531 return RCPair(0U, &ARM::tGPRRegClass); 11532 return RCPair(0U, &ARM::GPRRegClass); 11533 case 'w': 11534 if (VT == MVT::Other) 11535 break; 11536 if (VT == MVT::f32) 11537 return RCPair(0U, &ARM::SPRRegClass); 11538 if (VT.getSizeInBits() == 64) 11539 return RCPair(0U, &ARM::DPRRegClass); 11540 if (VT.getSizeInBits() == 128) 11541 return RCPair(0U, &ARM::QPRRegClass); 11542 break; 11543 case 'x': 11544 if (VT == MVT::Other) 11545 break; 11546 if (VT == MVT::f32) 11547 return RCPair(0U, &ARM::SPR_8RegClass); 11548 if (VT.getSizeInBits() == 64) 11549 return RCPair(0U, &ARM::DPR_8RegClass); 11550 if (VT.getSizeInBits() == 128) 11551 return RCPair(0U, &ARM::QPR_8RegClass); 11552 break; 11553 case 't': 11554 if (VT == MVT::f32) 11555 return RCPair(0U, &ARM::SPRRegClass); 11556 break; 11557 } 11558 } 11559 if (StringRef("{cc}").equals_lower(Constraint)) 11560 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass); 11561 11562 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 11563 } 11564 11565 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops 11566 /// vector. If it is invalid, don't add anything to Ops. 11567 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op, 11568 std::string &Constraint, 11569 std::vector<SDValue>&Ops, 11570 SelectionDAG &DAG) const { 11571 SDValue Result; 11572 11573 // Currently only support length 1 constraints. 11574 if (Constraint.length() != 1) return; 11575 11576 char ConstraintLetter = Constraint[0]; 11577 switch (ConstraintLetter) { 11578 default: break; 11579 case 'j': 11580 case 'I': case 'J': case 'K': case 'L': 11581 case 'M': case 'N': case 'O': 11582 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); 11583 if (!C) 11584 return; 11585 11586 int64_t CVal64 = C->getSExtValue(); 11587 int CVal = (int) CVal64; 11588 // None of these constraints allow values larger than 32 bits. Check 11589 // that the value fits in an int. 11590 if (CVal != CVal64) 11591 return; 11592 11593 switch (ConstraintLetter) { 11594 case 'j': 11595 // Constant suitable for movw, must be between 0 and 11596 // 65535. 11597 if (Subtarget->hasV6T2Ops()) 11598 if (CVal >= 0 && CVal <= 65535) 11599 break; 11600 return; 11601 case 'I': 11602 if (Subtarget->isThumb1Only()) { 11603 // This must be a constant between 0 and 255, for ADD 11604 // immediates. 11605 if (CVal >= 0 && CVal <= 255) 11606 break; 11607 } else if (Subtarget->isThumb2()) { 11608 // A constant that can be used as an immediate value in a 11609 // data-processing instruction. 11610 if (ARM_AM::getT2SOImmVal(CVal) != -1) 11611 break; 11612 } else { 11613 // A constant that can be used as an immediate value in a 11614 // data-processing instruction. 11615 if (ARM_AM::getSOImmVal(CVal) != -1) 11616 break; 11617 } 11618 return; 11619 11620 case 'J': 11621 if (Subtarget->isThumb1Only()) { 11622 // This must be a constant between -255 and -1, for negated ADD 11623 // immediates. This can be used in GCC with an "n" modifier that 11624 // prints the negated value, for use with SUB instructions. It is 11625 // not useful otherwise but is implemented for compatibility. 11626 if (CVal >= -255 && CVal <= -1) 11627 break; 11628 } else { 11629 // This must be a constant between -4095 and 4095. It is not clear 11630 // what this constraint is intended for. Implemented for 11631 // compatibility with GCC. 11632 if (CVal >= -4095 && CVal <= 4095) 11633 break; 11634 } 11635 return; 11636 11637 case 'K': 11638 if (Subtarget->isThumb1Only()) { 11639 // A 32-bit value where only one byte has a nonzero value. Exclude 11640 // zero to match GCC. This constraint is used by GCC internally for 11641 // constants that can be loaded with a move/shift combination. 11642 // It is not useful otherwise but is implemented for compatibility. 11643 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal)) 11644 break; 11645 } else if (Subtarget->isThumb2()) { 11646 // A constant whose bitwise inverse can be used as an immediate 11647 // value in a data-processing instruction. This can be used in GCC 11648 // with a "B" modifier that prints the inverted value, for use with 11649 // BIC and MVN instructions. It is not useful otherwise but is 11650 // implemented for compatibility. 11651 if (ARM_AM::getT2SOImmVal(~CVal) != -1) 11652 break; 11653 } else { 11654 // A constant whose bitwise inverse can be used as an immediate 11655 // value in a data-processing instruction. This can be used in GCC 11656 // with a "B" modifier that prints the inverted value, for use with 11657 // BIC and MVN instructions. It is not useful otherwise but is 11658 // implemented for compatibility. 11659 if (ARM_AM::getSOImmVal(~CVal) != -1) 11660 break; 11661 } 11662 return; 11663 11664 case 'L': 11665 if (Subtarget->isThumb1Only()) { 11666 // This must be a constant between -7 and 7, 11667 // for 3-operand ADD/SUB immediate instructions. 11668 if (CVal >= -7 && CVal < 7) 11669 break; 11670 } else if (Subtarget->isThumb2()) { 11671 // A constant whose negation can be used as an immediate value in a 11672 // data-processing instruction. This can be used in GCC with an "n" 11673 // modifier that prints the negated value, for use with SUB 11674 // instructions. It is not useful otherwise but is implemented for 11675 // compatibility. 11676 if (ARM_AM::getT2SOImmVal(-CVal) != -1) 11677 break; 11678 } else { 11679 // A constant whose negation can be used as an immediate value in a 11680 // data-processing instruction. This can be used in GCC with an "n" 11681 // modifier that prints the negated value, for use with SUB 11682 // instructions. It is not useful otherwise but is implemented for 11683 // compatibility. 11684 if (ARM_AM::getSOImmVal(-CVal) != -1) 11685 break; 11686 } 11687 return; 11688 11689 case 'M': 11690 if (Subtarget->isThumb1Only()) { 11691 // This must be a multiple of 4 between 0 and 1020, for 11692 // ADD sp + immediate. 11693 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0)) 11694 break; 11695 } else { 11696 // A power of two or a constant between 0 and 32. This is used in 11697 // GCC for the shift amount on shifted register operands, but it is 11698 // useful in general for any shift amounts. 11699 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0)) 11700 break; 11701 } 11702 return; 11703 11704 case 'N': 11705 if (Subtarget->isThumb()) { // FIXME thumb2 11706 // This must be a constant between 0 and 31, for shift amounts. 11707 if (CVal >= 0 && CVal <= 31) 11708 break; 11709 } 11710 return; 11711 11712 case 'O': 11713 if (Subtarget->isThumb()) { // FIXME thumb2 11714 // This must be a multiple of 4 between -508 and 508, for 11715 // ADD/SUB sp = sp + immediate. 11716 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) 11717 break; 11718 } 11719 return; 11720 } 11721 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType()); 11722 break; 11723 } 11724 11725 if (Result.getNode()) { 11726 Ops.push_back(Result); 11727 return; 11728 } 11729 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 11730 } 11731 11732 static RTLIB::Libcall getDivRemLibcall( 11733 const SDNode *N, MVT::SimpleValueType SVT) { 11734 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM || 11735 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) && 11736 "Unhandled Opcode in getDivRemLibcall"); 11737 bool isSigned = N->getOpcode() == ISD::SDIVREM || 11738 N->getOpcode() == ISD::SREM; 11739 RTLIB::Libcall LC; 11740 switch (SVT) { 11741 default: llvm_unreachable("Unexpected request for libcall!"); 11742 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 11743 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 11744 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 11745 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 11746 } 11747 return LC; 11748 } 11749 11750 static TargetLowering::ArgListTy getDivRemArgList( 11751 const SDNode *N, LLVMContext *Context) { 11752 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM || 11753 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) && 11754 "Unhandled Opcode in getDivRemArgList"); 11755 bool isSigned = N->getOpcode() == ISD::SDIVREM || 11756 N->getOpcode() == ISD::SREM; 11757 TargetLowering::ArgListTy Args; 11758 TargetLowering::ArgListEntry Entry; 11759 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 11760 EVT ArgVT = N->getOperand(i).getValueType(); 11761 Type *ArgTy = ArgVT.getTypeForEVT(*Context); 11762 Entry.Node = N->getOperand(i); 11763 Entry.Ty = ArgTy; 11764 Entry.isSExt = isSigned; 11765 Entry.isZExt = !isSigned; 11766 Args.push_back(Entry); 11767 } 11768 return Args; 11769 } 11770 11771 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const { 11772 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() || 11773 Subtarget->isTargetGNUAEABI()) && 11774 "Register-based DivRem lowering only"); 11775 unsigned Opcode = Op->getOpcode(); 11776 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) && 11777 "Invalid opcode for Div/Rem lowering"); 11778 bool isSigned = (Opcode == ISD::SDIVREM); 11779 EVT VT = Op->getValueType(0); 11780 Type *Ty = VT.getTypeForEVT(*DAG.getContext()); 11781 11782 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(), 11783 VT.getSimpleVT().SimpleTy); 11784 SDValue InChain = DAG.getEntryNode(); 11785 11786 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(), 11787 DAG.getContext()); 11788 11789 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 11790 getPointerTy(DAG.getDataLayout())); 11791 11792 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr); 11793 11794 SDLoc dl(Op); 11795 TargetLowering::CallLoweringInfo CLI(DAG); 11796 CLI.setDebugLoc(dl).setChain(InChain) 11797 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0) 11798 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned); 11799 11800 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); 11801 return CallInfo.first; 11802 } 11803 11804 // Lowers REM using divmod helpers 11805 // see RTABI section 4.2/4.3 11806 SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const { 11807 // Build return types (div and rem) 11808 std::vector<Type*> RetTyParams; 11809 Type *RetTyElement; 11810 11811 switch (N->getValueType(0).getSimpleVT().SimpleTy) { 11812 default: llvm_unreachable("Unexpected request for libcall!"); 11813 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break; 11814 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break; 11815 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break; 11816 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break; 11817 } 11818 11819 RetTyParams.push_back(RetTyElement); 11820 RetTyParams.push_back(RetTyElement); 11821 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams); 11822 Type *RetTy = StructType::get(*DAG.getContext(), ret); 11823 11824 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT(). 11825 SimpleTy); 11826 SDValue InChain = DAG.getEntryNode(); 11827 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext()); 11828 bool isSigned = N->getOpcode() == ISD::SREM; 11829 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), 11830 getPointerTy(DAG.getDataLayout())); 11831 11832 // Lower call 11833 CallLoweringInfo CLI(DAG); 11834 CLI.setChain(InChain) 11835 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0) 11836 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N)); 11837 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); 11838 11839 // Return second (rem) result operand (first contains div) 11840 SDNode *ResNode = CallResult.first.getNode(); 11841 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands"); 11842 return ResNode->getOperand(1); 11843 } 11844 11845 SDValue 11846 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { 11847 assert(Subtarget->isTargetWindows() && "unsupported target platform"); 11848 SDLoc DL(Op); 11849 11850 // Get the inputs. 11851 SDValue Chain = Op.getOperand(0); 11852 SDValue Size = Op.getOperand(1); 11853 11854 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size, 11855 DAG.getConstant(2, DL, MVT::i32)); 11856 11857 SDValue Flag; 11858 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag); 11859 Flag = Chain.getValue(1); 11860 11861 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 11862 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag); 11863 11864 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32); 11865 Chain = NewSP.getValue(1); 11866 11867 SDValue Ops[2] = { NewSP, Chain }; 11868 return DAG.getMergeValues(Ops, DL); 11869 } 11870 11871 SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { 11872 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() && 11873 "Unexpected type for custom-lowering FP_EXTEND"); 11874 11875 RTLIB::Libcall LC; 11876 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType()); 11877 11878 SDValue SrcVal = Op.getOperand(0); 11879 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false, 11880 SDLoc(Op)).first; 11881 } 11882 11883 SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 11884 assert(Op.getOperand(0).getValueType() == MVT::f64 && 11885 Subtarget->isFPOnlySP() && 11886 "Unexpected type for custom-lowering FP_ROUND"); 11887 11888 RTLIB::Libcall LC; 11889 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType()); 11890 11891 SDValue SrcVal = Op.getOperand(0); 11892 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false, 11893 SDLoc(Op)).first; 11894 } 11895 11896 bool 11897 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 11898 // The ARM target isn't yet aware of offsets. 11899 return false; 11900 } 11901 11902 bool ARM::isBitFieldInvertedMask(unsigned v) { 11903 if (v == 0xffffffff) 11904 return false; 11905 11906 // there can be 1's on either or both "outsides", all the "inside" 11907 // bits must be 0's 11908 return isShiftedMask_32(~v); 11909 } 11910 11911 /// isFPImmLegal - Returns true if the target can instruction select the 11912 /// specified FP immediate natively. If false, the legalizer will 11913 /// materialize the FP immediate as a load from a constant pool. 11914 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 11915 if (!Subtarget->hasVFP3()) 11916 return false; 11917 if (VT == MVT::f32) 11918 return ARM_AM::getFP32Imm(Imm) != -1; 11919 if (VT == MVT::f64 && !Subtarget->isFPOnlySP()) 11920 return ARM_AM::getFP64Imm(Imm) != -1; 11921 return false; 11922 } 11923 11924 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as 11925 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment 11926 /// specified in the intrinsic calls. 11927 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 11928 const CallInst &I, 11929 unsigned Intrinsic) const { 11930 switch (Intrinsic) { 11931 case Intrinsic::arm_neon_vld1: 11932 case Intrinsic::arm_neon_vld2: 11933 case Intrinsic::arm_neon_vld3: 11934 case Intrinsic::arm_neon_vld4: 11935 case Intrinsic::arm_neon_vld2lane: 11936 case Intrinsic::arm_neon_vld3lane: 11937 case Intrinsic::arm_neon_vld4lane: { 11938 Info.opc = ISD::INTRINSIC_W_CHAIN; 11939 // Conservatively set memVT to the entire set of vectors loaded. 11940 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 11941 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64; 11942 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 11943 Info.ptrVal = I.getArgOperand(0); 11944 Info.offset = 0; 11945 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 11946 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 11947 Info.vol = false; // volatile loads with NEON intrinsics not supported 11948 Info.readMem = true; 11949 Info.writeMem = false; 11950 return true; 11951 } 11952 case Intrinsic::arm_neon_vst1: 11953 case Intrinsic::arm_neon_vst2: 11954 case Intrinsic::arm_neon_vst3: 11955 case Intrinsic::arm_neon_vst4: 11956 case Intrinsic::arm_neon_vst2lane: 11957 case Intrinsic::arm_neon_vst3lane: 11958 case Intrinsic::arm_neon_vst4lane: { 11959 Info.opc = ISD::INTRINSIC_VOID; 11960 // Conservatively set memVT to the entire set of vectors stored. 11961 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 11962 unsigned NumElts = 0; 11963 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) { 11964 Type *ArgTy = I.getArgOperand(ArgI)->getType(); 11965 if (!ArgTy->isVectorTy()) 11966 break; 11967 NumElts += DL.getTypeSizeInBits(ArgTy) / 64; 11968 } 11969 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts); 11970 Info.ptrVal = I.getArgOperand(0); 11971 Info.offset = 0; 11972 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1); 11973 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue(); 11974 Info.vol = false; // volatile stores with NEON intrinsics not supported 11975 Info.readMem = false; 11976 Info.writeMem = true; 11977 return true; 11978 } 11979 case Intrinsic::arm_ldaex: 11980 case Intrinsic::arm_ldrex: { 11981 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 11982 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); 11983 Info.opc = ISD::INTRINSIC_W_CHAIN; 11984 Info.memVT = MVT::getVT(PtrTy->getElementType()); 11985 Info.ptrVal = I.getArgOperand(0); 11986 Info.offset = 0; 11987 Info.align = DL.getABITypeAlignment(PtrTy->getElementType()); 11988 Info.vol = true; 11989 Info.readMem = true; 11990 Info.writeMem = false; 11991 return true; 11992 } 11993 case Intrinsic::arm_stlex: 11994 case Intrinsic::arm_strex: { 11995 auto &DL = I.getCalledFunction()->getParent()->getDataLayout(); 11996 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType()); 11997 Info.opc = ISD::INTRINSIC_W_CHAIN; 11998 Info.memVT = MVT::getVT(PtrTy->getElementType()); 11999 Info.ptrVal = I.getArgOperand(1); 12000 Info.offset = 0; 12001 Info.align = DL.getABITypeAlignment(PtrTy->getElementType()); 12002 Info.vol = true; 12003 Info.readMem = false; 12004 Info.writeMem = true; 12005 return true; 12006 } 12007 case Intrinsic::arm_stlexd: 12008 case Intrinsic::arm_strexd: { 12009 Info.opc = ISD::INTRINSIC_W_CHAIN; 12010 Info.memVT = MVT::i64; 12011 Info.ptrVal = I.getArgOperand(2); 12012 Info.offset = 0; 12013 Info.align = 8; 12014 Info.vol = true; 12015 Info.readMem = false; 12016 Info.writeMem = true; 12017 return true; 12018 } 12019 case Intrinsic::arm_ldaexd: 12020 case Intrinsic::arm_ldrexd: { 12021 Info.opc = ISD::INTRINSIC_W_CHAIN; 12022 Info.memVT = MVT::i64; 12023 Info.ptrVal = I.getArgOperand(0); 12024 Info.offset = 0; 12025 Info.align = 8; 12026 Info.vol = true; 12027 Info.readMem = true; 12028 Info.writeMem = false; 12029 return true; 12030 } 12031 default: 12032 break; 12033 } 12034 12035 return false; 12036 } 12037 12038 /// \brief Returns true if it is beneficial to convert a load of a constant 12039 /// to just the constant itself. 12040 bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 12041 Type *Ty) const { 12042 assert(Ty->isIntegerTy()); 12043 12044 unsigned Bits = Ty->getPrimitiveSizeInBits(); 12045 if (Bits == 0 || Bits > 32) 12046 return false; 12047 return true; 12048 } 12049 12050 Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder, 12051 ARM_MB::MemBOpt Domain) const { 12052 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 12053 12054 // First, if the target has no DMB, see what fallback we can use. 12055 if (!Subtarget->hasDataBarrier()) { 12056 // Some ARMv6 cpus can support data barriers with an mcr instruction. 12057 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get 12058 // here. 12059 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) { 12060 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr); 12061 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0), 12062 Builder.getInt32(0), Builder.getInt32(7), 12063 Builder.getInt32(10), Builder.getInt32(5)}; 12064 return Builder.CreateCall(MCR, args); 12065 } else { 12066 // Instead of using barriers, atomic accesses on these subtargets use 12067 // libcalls. 12068 llvm_unreachable("makeDMB on a target so old that it has no barriers"); 12069 } 12070 } else { 12071 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb); 12072 // Only a full system barrier exists in the M-class architectures. 12073 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain; 12074 Constant *CDomain = Builder.getInt32(Domain); 12075 return Builder.CreateCall(DMB, CDomain); 12076 } 12077 } 12078 12079 // Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html 12080 Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder, 12081 AtomicOrdering Ord, bool IsStore, 12082 bool IsLoad) const { 12083 switch (Ord) { 12084 case AtomicOrdering::NotAtomic: 12085 case AtomicOrdering::Unordered: 12086 llvm_unreachable("Invalid fence: unordered/non-atomic"); 12087 case AtomicOrdering::Monotonic: 12088 case AtomicOrdering::Acquire: 12089 return nullptr; // Nothing to do 12090 case AtomicOrdering::SequentiallyConsistent: 12091 if (!IsStore) 12092 return nullptr; // Nothing to do 12093 /*FALLTHROUGH*/ 12094 case AtomicOrdering::Release: 12095 case AtomicOrdering::AcquireRelease: 12096 if (Subtarget->isSwift()) 12097 return makeDMB(Builder, ARM_MB::ISHST); 12098 // FIXME: add a comment with a link to documentation justifying this. 12099 else 12100 return makeDMB(Builder, ARM_MB::ISH); 12101 } 12102 llvm_unreachable("Unknown fence ordering in emitLeadingFence"); 12103 } 12104 12105 Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder, 12106 AtomicOrdering Ord, bool IsStore, 12107 bool IsLoad) const { 12108 switch (Ord) { 12109 case AtomicOrdering::NotAtomic: 12110 case AtomicOrdering::Unordered: 12111 llvm_unreachable("Invalid fence: unordered/not-atomic"); 12112 case AtomicOrdering::Monotonic: 12113 case AtomicOrdering::Release: 12114 return nullptr; // Nothing to do 12115 case AtomicOrdering::Acquire: 12116 case AtomicOrdering::AcquireRelease: 12117 case AtomicOrdering::SequentiallyConsistent: 12118 return makeDMB(Builder, ARM_MB::ISH); 12119 } 12120 llvm_unreachable("Unknown fence ordering in emitTrailingFence"); 12121 } 12122 12123 // Loads and stores less than 64-bits are already atomic; ones above that 12124 // are doomed anyway, so defer to the default libcall and blame the OS when 12125 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 12126 // anything for those. 12127 bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const { 12128 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits(); 12129 return (Size == 64) && !Subtarget->isMClass(); 12130 } 12131 12132 // Loads and stores less than 64-bits are already atomic; ones above that 12133 // are doomed anyway, so defer to the default libcall and blame the OS when 12134 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit 12135 // anything for those. 12136 // FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that 12137 // guarantee, see DDI0406C ARM architecture reference manual, 12138 // sections A8.8.72-74 LDRD) 12139 TargetLowering::AtomicExpansionKind 12140 ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const { 12141 unsigned Size = LI->getType()->getPrimitiveSizeInBits(); 12142 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly 12143 : AtomicExpansionKind::None; 12144 } 12145 12146 // For the real atomic operations, we have ldrex/strex up to 32 bits, 12147 // and up to 64 bits on the non-M profiles 12148 TargetLowering::AtomicExpansionKind 12149 ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { 12150 unsigned Size = AI->getType()->getPrimitiveSizeInBits(); 12151 return (Size <= (Subtarget->isMClass() ? 32U : 64U)) 12152 ? AtomicExpansionKind::LLSC 12153 : AtomicExpansionKind::None; 12154 } 12155 12156 bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR( 12157 AtomicCmpXchgInst *AI) const { 12158 return true; 12159 } 12160 12161 bool ARMTargetLowering::shouldInsertFencesForAtomic( 12162 const Instruction *I) const { 12163 return InsertFencesForAtomic; 12164 } 12165 12166 // This has so far only been implemented for MachO. 12167 bool ARMTargetLowering::useLoadStackGuardNode() const { 12168 return Subtarget->isTargetMachO(); 12169 } 12170 12171 bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx, 12172 unsigned &Cost) const { 12173 // If we do not have NEON, vector types are not natively supported. 12174 if (!Subtarget->hasNEON()) 12175 return false; 12176 12177 // Floating point values and vector values map to the same register file. 12178 // Therefore, although we could do a store extract of a vector type, this is 12179 // better to leave at float as we have more freedom in the addressing mode for 12180 // those. 12181 if (VectorTy->isFPOrFPVectorTy()) 12182 return false; 12183 12184 // If the index is unknown at compile time, this is very expensive to lower 12185 // and it is not possible to combine the store with the extract. 12186 if (!isa<ConstantInt>(Idx)) 12187 return false; 12188 12189 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type"); 12190 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth(); 12191 // We can do a store + vector extract on any vector that fits perfectly in a D 12192 // or Q register. 12193 if (BitWidth == 64 || BitWidth == 128) { 12194 Cost = 0; 12195 return true; 12196 } 12197 return false; 12198 } 12199 12200 bool ARMTargetLowering::isCheapToSpeculateCttz() const { 12201 return Subtarget->hasV6T2Ops(); 12202 } 12203 12204 bool ARMTargetLowering::isCheapToSpeculateCtlz() const { 12205 return Subtarget->hasV6T2Ops(); 12206 } 12207 12208 Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr, 12209 AtomicOrdering Ord) const { 12210 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 12211 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType(); 12212 bool IsAcquire = isAcquireOrStronger(Ord); 12213 12214 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd 12215 // intrinsic must return {i32, i32} and we have to recombine them into a 12216 // single i64 here. 12217 if (ValTy->getPrimitiveSizeInBits() == 64) { 12218 Intrinsic::ID Int = 12219 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd; 12220 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int); 12221 12222 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 12223 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi"); 12224 12225 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo"); 12226 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi"); 12227 if (!Subtarget->isLittle()) 12228 std::swap (Lo, Hi); 12229 Lo = Builder.CreateZExt(Lo, ValTy, "lo64"); 12230 Hi = Builder.CreateZExt(Hi, ValTy, "hi64"); 12231 return Builder.CreateOr( 12232 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64"); 12233 } 12234 12235 Type *Tys[] = { Addr->getType() }; 12236 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex; 12237 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys); 12238 12239 return Builder.CreateTruncOrBitCast( 12240 Builder.CreateCall(Ldrex, Addr), 12241 cast<PointerType>(Addr->getType())->getElementType()); 12242 } 12243 12244 void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance( 12245 IRBuilder<> &Builder) const { 12246 if (!Subtarget->hasV7Ops()) 12247 return; 12248 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 12249 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex)); 12250 } 12251 12252 Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val, 12253 Value *Addr, 12254 AtomicOrdering Ord) const { 12255 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 12256 bool IsRelease = isReleaseOrStronger(Ord); 12257 12258 // Since the intrinsics must have legal type, the i64 intrinsics take two 12259 // parameters: "i32, i32". We must marshal Val into the appropriate form 12260 // before the call. 12261 if (Val->getType()->getPrimitiveSizeInBits() == 64) { 12262 Intrinsic::ID Int = 12263 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd; 12264 Function *Strex = Intrinsic::getDeclaration(M, Int); 12265 Type *Int32Ty = Type::getInt32Ty(M->getContext()); 12266 12267 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo"); 12268 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi"); 12269 if (!Subtarget->isLittle()) 12270 std::swap (Lo, Hi); 12271 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext())); 12272 return Builder.CreateCall(Strex, {Lo, Hi, Addr}); 12273 } 12274 12275 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex; 12276 Type *Tys[] = { Addr->getType() }; 12277 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys); 12278 12279 return Builder.CreateCall( 12280 Strex, {Builder.CreateZExtOrBitCast( 12281 Val, Strex->getFunctionType()->getParamType(0)), 12282 Addr}); 12283 } 12284 12285 /// \brief Lower an interleaved load into a vldN intrinsic. 12286 /// 12287 /// E.g. Lower an interleaved load (Factor = 2): 12288 /// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4 12289 /// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements 12290 /// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements 12291 /// 12292 /// Into: 12293 /// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4) 12294 /// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0 12295 /// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1 12296 bool ARMTargetLowering::lowerInterleavedLoad( 12297 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles, 12298 ArrayRef<unsigned> Indices, unsigned Factor) const { 12299 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 12300 "Invalid interleave factor"); 12301 assert(!Shuffles.empty() && "Empty shufflevector input"); 12302 assert(Shuffles.size() == Indices.size() && 12303 "Unmatched number of shufflevectors and indices"); 12304 12305 VectorType *VecTy = Shuffles[0]->getType(); 12306 Type *EltTy = VecTy->getVectorElementType(); 12307 12308 const DataLayout &DL = LI->getModule()->getDataLayout(); 12309 unsigned VecSize = DL.getTypeSizeInBits(VecTy); 12310 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64; 12311 12312 // Skip if we do not have NEON and skip illegal vector types and vector types 12313 // with i64/f64 elements (vldN doesn't support i64/f64 elements). 12314 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits) 12315 return false; 12316 12317 // A pointer vector can not be the return type of the ldN intrinsics. Need to 12318 // load integer vectors first and then convert to pointer vectors. 12319 if (EltTy->isPointerTy()) 12320 VecTy = 12321 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements()); 12322 12323 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2, 12324 Intrinsic::arm_neon_vld3, 12325 Intrinsic::arm_neon_vld4}; 12326 12327 IRBuilder<> Builder(LI); 12328 SmallVector<Value *, 2> Ops; 12329 12330 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace()); 12331 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr)); 12332 Ops.push_back(Builder.getInt32(LI->getAlignment())); 12333 12334 Type *Tys[] = { VecTy, Int8Ptr }; 12335 Function *VldnFunc = 12336 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys); 12337 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN"); 12338 12339 // Replace uses of each shufflevector with the corresponding vector loaded 12340 // by ldN. 12341 for (unsigned i = 0; i < Shuffles.size(); i++) { 12342 ShuffleVectorInst *SV = Shuffles[i]; 12343 unsigned Index = Indices[i]; 12344 12345 Value *SubVec = Builder.CreateExtractValue(VldN, Index); 12346 12347 // Convert the integer vector to pointer vector if the element is pointer. 12348 if (EltTy->isPointerTy()) 12349 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType()); 12350 12351 SV->replaceAllUsesWith(SubVec); 12352 } 12353 12354 return true; 12355 } 12356 12357 /// \brief Get a mask consisting of sequential integers starting from \p Start. 12358 /// 12359 /// I.e. <Start, Start + 1, ..., Start + NumElts - 1> 12360 static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start, 12361 unsigned NumElts) { 12362 SmallVector<Constant *, 16> Mask; 12363 for (unsigned i = 0; i < NumElts; i++) 12364 Mask.push_back(Builder.getInt32(Start + i)); 12365 12366 return ConstantVector::get(Mask); 12367 } 12368 12369 /// \brief Lower an interleaved store into a vstN intrinsic. 12370 /// 12371 /// E.g. Lower an interleaved store (Factor = 3): 12372 /// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1, 12373 /// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11> 12374 /// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4 12375 /// 12376 /// Into: 12377 /// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3> 12378 /// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7> 12379 /// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11> 12380 /// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4) 12381 /// 12382 /// Note that the new shufflevectors will be removed and we'll only generate one 12383 /// vst3 instruction in CodeGen. 12384 bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI, 12385 ShuffleVectorInst *SVI, 12386 unsigned Factor) const { 12387 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() && 12388 "Invalid interleave factor"); 12389 12390 VectorType *VecTy = SVI->getType(); 12391 assert(VecTy->getVectorNumElements() % Factor == 0 && 12392 "Invalid interleaved store"); 12393 12394 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor; 12395 Type *EltTy = VecTy->getVectorElementType(); 12396 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts); 12397 12398 const DataLayout &DL = SI->getModule()->getDataLayout(); 12399 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy); 12400 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64; 12401 12402 // Skip if we do not have NEON and skip illegal vector types and vector types 12403 // with i64/f64 elements (vstN doesn't support i64/f64 elements). 12404 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) || 12405 EltIs64Bits) 12406 return false; 12407 12408 Value *Op0 = SVI->getOperand(0); 12409 Value *Op1 = SVI->getOperand(1); 12410 IRBuilder<> Builder(SI); 12411 12412 // StN intrinsics don't support pointer vectors as arguments. Convert pointer 12413 // vectors to integer vectors. 12414 if (EltTy->isPointerTy()) { 12415 Type *IntTy = DL.getIntPtrType(EltTy); 12416 12417 // Convert to the corresponding integer vector. 12418 Type *IntVecTy = 12419 VectorType::get(IntTy, Op0->getType()->getVectorNumElements()); 12420 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy); 12421 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy); 12422 12423 SubVecTy = VectorType::get(IntTy, NumSubElts); 12424 } 12425 12426 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2, 12427 Intrinsic::arm_neon_vst3, 12428 Intrinsic::arm_neon_vst4}; 12429 SmallVector<Value *, 6> Ops; 12430 12431 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace()); 12432 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr)); 12433 12434 Type *Tys[] = { Int8Ptr, SubVecTy }; 12435 Function *VstNFunc = Intrinsic::getDeclaration( 12436 SI->getModule(), StoreInts[Factor - 2], Tys); 12437 12438 // Split the shufflevector operands into sub vectors for the new vstN call. 12439 for (unsigned i = 0; i < Factor; i++) 12440 Ops.push_back(Builder.CreateShuffleVector( 12441 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts))); 12442 12443 Ops.push_back(Builder.getInt32(SI->getAlignment())); 12444 Builder.CreateCall(VstNFunc, Ops); 12445 return true; 12446 } 12447 12448 enum HABaseType { 12449 HA_UNKNOWN = 0, 12450 HA_FLOAT, 12451 HA_DOUBLE, 12452 HA_VECT64, 12453 HA_VECT128 12454 }; 12455 12456 static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base, 12457 uint64_t &Members) { 12458 if (auto *ST = dyn_cast<StructType>(Ty)) { 12459 for (unsigned i = 0; i < ST->getNumElements(); ++i) { 12460 uint64_t SubMembers = 0; 12461 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers)) 12462 return false; 12463 Members += SubMembers; 12464 } 12465 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) { 12466 uint64_t SubMembers = 0; 12467 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers)) 12468 return false; 12469 Members += SubMembers * AT->getNumElements(); 12470 } else if (Ty->isFloatTy()) { 12471 if (Base != HA_UNKNOWN && Base != HA_FLOAT) 12472 return false; 12473 Members = 1; 12474 Base = HA_FLOAT; 12475 } else if (Ty->isDoubleTy()) { 12476 if (Base != HA_UNKNOWN && Base != HA_DOUBLE) 12477 return false; 12478 Members = 1; 12479 Base = HA_DOUBLE; 12480 } else if (auto *VT = dyn_cast<VectorType>(Ty)) { 12481 Members = 1; 12482 switch (Base) { 12483 case HA_FLOAT: 12484 case HA_DOUBLE: 12485 return false; 12486 case HA_VECT64: 12487 return VT->getBitWidth() == 64; 12488 case HA_VECT128: 12489 return VT->getBitWidth() == 128; 12490 case HA_UNKNOWN: 12491 switch (VT->getBitWidth()) { 12492 case 64: 12493 Base = HA_VECT64; 12494 return true; 12495 case 128: 12496 Base = HA_VECT128; 12497 return true; 12498 default: 12499 return false; 12500 } 12501 } 12502 } 12503 12504 return (Members > 0 && Members <= 4); 12505 } 12506 12507 /// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of 12508 /// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when 12509 /// passing according to AAPCS rules. 12510 bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters( 12511 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const { 12512 if (getEffectiveCallingConv(CallConv, isVarArg) != 12513 CallingConv::ARM_AAPCS_VFP) 12514 return false; 12515 12516 HABaseType Base = HA_UNKNOWN; 12517 uint64_t Members = 0; 12518 bool IsHA = isHomogeneousAggregate(Ty, Base, Members); 12519 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump()); 12520 12521 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy(); 12522 return IsHA || IsIntArray; 12523 } 12524 12525 unsigned ARMTargetLowering::getExceptionPointerRegister( 12526 const Constant *PersonalityFn) const { 12527 // Platforms which do not use SjLj EH may return values in these registers 12528 // via the personality function. 12529 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0; 12530 } 12531 12532 unsigned ARMTargetLowering::getExceptionSelectorRegister( 12533 const Constant *PersonalityFn) const { 12534 // Platforms which do not use SjLj EH may return values in these registers 12535 // via the personality function. 12536 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1; 12537 } 12538 12539 void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { 12540 // Update IsSplitCSR in ARMFunctionInfo. 12541 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>(); 12542 AFI->setIsSplitCSR(true); 12543 } 12544 12545 void ARMTargetLowering::insertCopiesSplitCSR( 12546 MachineBasicBlock *Entry, 12547 const SmallVectorImpl<MachineBasicBlock *> &Exits) const { 12548 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo(); 12549 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); 12550 if (!IStart) 12551 return; 12552 12553 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 12554 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); 12555 MachineBasicBlock::iterator MBBI = Entry->begin(); 12556 for (const MCPhysReg *I = IStart; *I; ++I) { 12557 const TargetRegisterClass *RC = nullptr; 12558 if (ARM::GPRRegClass.contains(*I)) 12559 RC = &ARM::GPRRegClass; 12560 else if (ARM::DPRRegClass.contains(*I)) 12561 RC = &ARM::DPRRegClass; 12562 else 12563 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 12564 12565 unsigned NewVR = MRI->createVirtualRegister(RC); 12566 // Create copy from CSR to a virtual register. 12567 // FIXME: this currently does not emit CFI pseudo-instructions, it works 12568 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be 12569 // nounwind. If we want to generalize this later, we may need to emit 12570 // CFI pseudo-instructions. 12571 assert(Entry->getParent()->getFunction()->hasFnAttribute( 12572 Attribute::NoUnwind) && 12573 "Function should be nounwind in insertCopiesSplitCSR!"); 12574 Entry->addLiveIn(*I); 12575 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) 12576 .addReg(*I); 12577 12578 // Insert the copy-back instructions right before the terminator. 12579 for (auto *Exit : Exits) 12580 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), 12581 TII->get(TargetOpcode::COPY), *I) 12582 .addReg(NewVR); 12583 } 12584 } 12585